Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | This script extracts btsnooz content from bugreports and generates |
| 4 | a valid btsnoop log file which can be viewed using standard tools |
| 5 | like Wireshark. |
| 6 | |
| 7 | btsnooz is a custom format designed to be included in bugreports. |
| 8 | It can be described as: |
| 9 | |
| 10 | base64 { |
| 11 | file_header |
| 12 | deflate { |
| 13 | repeated { |
| 14 | record_header |
| 15 | record_data |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | where the file_header and record_header are modified versions of |
| 21 | the btsnoop headers. |
| 22 | """ |
| 23 | |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 24 | import base64 |
Sharvil Nanavati | 700b163 | 2016-01-22 17:03:03 -0800 | [diff] [blame] | 25 | import fileinput |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 26 | import struct |
| 27 | import sys |
| 28 | import zlib |
| 29 | |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 30 | # 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. |
| 33 | TYPE_IN_EVT = 0x10 |
| 34 | TYPE_IN_ACL = 0x11 |
| 35 | TYPE_IN_SCO = 0x12 |
| 36 | TYPE_OUT_CMD = 0x20 |
| 37 | TYPE_OUT_ACL = 0x21 |
| 38 | TYPE_OUT_SCO = 0x22 |
| 39 | |
| 40 | |
| 41 | def type_to_direction(type): |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 42 | """ |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 43 | Returns the inbound/outbound direction of a packet given its type. |
| 44 | 0 = sent packet |
| 45 | 1 = received packet |
| 46 | """ |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 47 | if type in [TYPE_IN_EVT, TYPE_IN_ACL, TYPE_IN_SCO]: |
| 48 | return 1 |
| 49 | return 0 |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def type_to_hci(type): |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 53 | """ |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 54 | Returns the HCI type of a packet given its btsnooz type. |
| 55 | """ |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 56 | 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 Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def decode_snooz(snooz): |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 67 | """ |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 68 | Decodes all known versions of a btsnooz file into a btsnoop file. |
| 69 | """ |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 70 | version, last_timestamp_ms = struct.unpack_from('=bQ', snooz) |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 71 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 72 | if version != 1 and version != 2: |
| 73 | sys.stderr.write('Unsupported btsnooz version: %s\n' % version) |
| 74 | exit(1) |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 75 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 76 | # Oddly, the file header (9 bytes) is not compressed, but the rest is. |
| 77 | decompressed = zlib.decompress(snooz[9:]) |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 78 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 79 | sys.stdout.write('btsnoop\x00\x00\x00\x00\x01\x00\x00\x03\xea') |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 80 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 81 | if version == 1: |
| 82 | decode_snooz_v1(decompressed, last_timestamp_ms) |
| 83 | elif version == 2: |
| 84 | decode_snooz_v2(decompressed, last_timestamp_ms) |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def decode_snooz_v1(decompressed, last_timestamp_ms): |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 88 | """ |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 89 | Decodes btsnooz v1 files into a btsnoop file. |
| 90 | """ |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 91 | # 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 Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 100 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 101 | # 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 Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def decode_snooz_v2(decompressed, last_timestamp_ms): |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 119 | """ |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 120 | Decodes btsnooz v2 files into a btsnoop file. |
| 121 | """ |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 122 | # 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 Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 131 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 132 | # 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 Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def main(): |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 150 | if len(sys.argv) > 2: |
| 151 | sys.stderr.write('Usage: %s [bugreport]\n' % sys.argv[0]) |
| 152 | exit(1) |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 153 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 154 | 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 Eisenbach | 2b70aa4 | 2016-04-01 17:47:54 -0700 | [diff] [blame] | 163 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 164 | if line.find('--- BEGIN:BTSNOOP_LOG_SUMMARY') != -1: |
| 165 | found = True |
Andre Eisenbach | 2b70aa4 | 2016-04-01 17:47:54 -0700 | [diff] [blame] | 166 | |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 167 | if not found: |
| 168 | sys.stderr.write('No btsnooz section found in bugreport.\n') |
| 169 | sys.exit(1) |
Sharvil Nanavati | 1c50abb | 2016-01-20 09:14:47 -0800 | [diff] [blame] | 170 | |
| 171 | |
| 172 | if __name__ == '__main__': |
Jack He | 1d2b142 | 2019-12-10 16:41:59 -0800 | [diff] [blame] | 173 | main() |