blob: a29e718d7b770d63bd318a864ec118385ea5158c [file] [log] [blame]
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -08001#!/usr/bin/env python
2"""
3This script extracts btsnooz content from bugreports and generates
4a valid btsnoop log file which can be viewed using standard tools
5like Wireshark.
6
7btsnooz is a custom format designed to be included in bugreports.
8It can be described as:
9
10base64 {
11 file_header
12 deflate {
13 repeated {
14 record_header
15 record_data
16 }
17 }
18}
19
20where the file_header and record_header are modified versions of
21the btsnoop headers.
22"""
23
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080024import base64
Sharvil Nanavati700b1632016-01-22 17:03:03 -080025import fileinput
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080026import struct
27import sys
28import zlib
29
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080030# Enumeration of the values the 'type' field can take in a btsnooz
31# header. These values come from the Bluetooth stack's internal
32# representation of packet types.
33TYPE_IN_EVT = 0x10
34TYPE_IN_ACL = 0x11
35TYPE_IN_SCO = 0x12
36TYPE_OUT_CMD = 0x20
37TYPE_OUT_ACL = 0x21
38TYPE_OUT_SCO = 0x22
39
40
41def type_to_direction(type):
Jack He1d2b1422019-12-10 16:41:59 -080042 """
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080043 Returns the inbound/outbound direction of a packet given its type.
44 0 = sent packet
45 1 = received packet
46 """
Jack He1d2b1422019-12-10 16:41:59 -080047 if type in [TYPE_IN_EVT, TYPE_IN_ACL, TYPE_IN_SCO]:
48 return 1
49 return 0
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080050
51
52def type_to_hci(type):
Jack He1d2b1422019-12-10 16:41:59 -080053 """
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080054 Returns the HCI type of a packet given its btsnooz type.
55 """
Jack He1d2b1422019-12-10 16:41:59 -080056 if type == TYPE_OUT_CMD:
57 return '\x01'
58 if type == TYPE_IN_ACL or type == TYPE_OUT_ACL:
59 return '\x02'
60 if type == TYPE_IN_SCO or type == TYPE_OUT_SCO:
61 return '\x03'
62 if type == TYPE_IN_EVT:
63 return '\x04'
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080064
65
66def decode_snooz(snooz):
Jack He1d2b1422019-12-10 16:41:59 -080067 """
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080068 Decodes all known versions of a btsnooz file into a btsnoop file.
69 """
Jack He1d2b1422019-12-10 16:41:59 -080070 version, last_timestamp_ms = struct.unpack_from('=bQ', snooz)
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080071
Jack He1d2b1422019-12-10 16:41:59 -080072 if version != 1 and version != 2:
73 sys.stderr.write('Unsupported btsnooz version: %s\n' % version)
74 exit(1)
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080075
Jack He1d2b1422019-12-10 16:41:59 -080076 # Oddly, the file header (9 bytes) is not compressed, but the rest is.
77 decompressed = zlib.decompress(snooz[9:])
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080078
Jack He1d2b1422019-12-10 16:41:59 -080079 sys.stdout.write('btsnoop\x00\x00\x00\x00\x01\x00\x00\x03\xea')
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080080
Jack He1d2b1422019-12-10 16:41:59 -080081 if version == 1:
82 decode_snooz_v1(decompressed, last_timestamp_ms)
83 elif version == 2:
84 decode_snooz_v2(decompressed, last_timestamp_ms)
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080085
86
87def decode_snooz_v1(decompressed, last_timestamp_ms):
Jack He1d2b1422019-12-10 16:41:59 -080088 """
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -080089 Decodes btsnooz v1 files into a btsnoop file.
90 """
Jack He1d2b1422019-12-10 16:41:59 -080091 # An unfortunate consequence of the file format design: we have to do a
92 # pass of the entire file to determine the timestamp of the first packet.
93 first_timestamp_ms = last_timestamp_ms + 0x00dcddb30f2f8000
94 offset = 0
95 while offset < len(decompressed):
96 length, delta_time_ms, type = struct.unpack_from(
97 '=HIb', decompressed, offset)
98 offset += 7 + length - 1
99 first_timestamp_ms -= delta_time_ms
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -0800100
Jack He1d2b1422019-12-10 16:41:59 -0800101 # Second pass does the actual writing out to stdout.
102 offset = 0
103 while offset < len(decompressed):
104 length, delta_time_ms, type = struct.unpack_from(
105 '=HIb', decompressed, offset)
106 first_timestamp_ms += delta_time_ms
107 offset += 7
108 sys.stdout.write(struct.pack('>II', length, length))
109 sys.stdout.write(struct.pack('>II', type_to_direction(type), 0))
110 sys.stdout.write(
111 struct.pack('>II', (first_timestamp_ms >> 32),
112 (first_timestamp_ms & 0xFFFFFFFF)))
113 sys.stdout.write(type_to_hci(type))
114 sys.stdout.write(decompressed[offset:offset + length - 1])
115 offset += length - 1
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -0800116
117
118def decode_snooz_v2(decompressed, last_timestamp_ms):
Jack He1d2b1422019-12-10 16:41:59 -0800119 """
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -0800120 Decodes btsnooz v2 files into a btsnoop file.
121 """
Jack He1d2b1422019-12-10 16:41:59 -0800122 # An unfortunate consequence of the file format design: we have to do a
123 # pass of the entire file to determine the timestamp of the first packet.
124 first_timestamp_ms = last_timestamp_ms + 0x00dcddb30f2f8000
125 offset = 0
126 while offset < len(decompressed):
127 length, packet_length, delta_time_ms, snooz_type = struct.unpack_from(
128 '=HHIb', decompressed, offset)
129 offset += 9 + length - 1
130 first_timestamp_ms -= delta_time_ms
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -0800131
Jack He1d2b1422019-12-10 16:41:59 -0800132 # Second pass does the actual writing out to stdout.
133 offset = 0
134 while offset < len(decompressed):
135 length, packet_length, delta_time_ms, snooz_type = struct.unpack_from(
136 '=HHIb', decompressed, offset)
137 first_timestamp_ms += delta_time_ms
138 offset += 9
139 sys.stdout.write(struct.pack('>II', packet_length, length))
140 sys.stdout.write(struct.pack('>II', type_to_direction(snooz_type), 0))
141 sys.stdout.write(
142 struct.pack('>II', (first_timestamp_ms >> 32),
143 (first_timestamp_ms & 0xFFFFFFFF)))
144 sys.stdout.write(type_to_hci(snooz_type))
145 sys.stdout.write(decompressed[offset:offset + length - 1])
146 offset += length - 1
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -0800147
148
149def main():
Jack He1d2b1422019-12-10 16:41:59 -0800150 if len(sys.argv) > 2:
151 sys.stderr.write('Usage: %s [bugreport]\n' % sys.argv[0])
152 exit(1)
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -0800153
Jack He1d2b1422019-12-10 16:41:59 -0800154 iterator = fileinput.input()
155 found = False
156 base64_string = ""
157 for line in iterator:
158 if found:
159 if line.find('--- END:BTSNOOP_LOG_SUMMARY') != -1:
160 decode_snooz(base64.standard_b64decode(base64_string))
161 sys.exit(0)
162 base64_string += line.strip()
Andre Eisenbach2b70aa42016-04-01 17:47:54 -0700163
Jack He1d2b1422019-12-10 16:41:59 -0800164 if line.find('--- BEGIN:BTSNOOP_LOG_SUMMARY') != -1:
165 found = True
Andre Eisenbach2b70aa42016-04-01 17:47:54 -0700166
Jack He1d2b1422019-12-10 16:41:59 -0800167 if not found:
168 sys.stderr.write('No btsnooz section found in bugreport.\n')
169 sys.exit(1)
Sharvil Nanavati1c50abb2016-01-20 09:14:47 -0800170
171
172if __name__ == '__main__':
Jack He1d2b1422019-12-10 16:41:59 -0800173 main()