1#!/usr/bin/env python 2# BlueKitchen GmbH (c) 2014 3 4# primitive dump for PacketLogger format 5 6# APPLE PacketLogger 7# typedef struct { 8# uint32_t len; 9# uint32_t ts_sec; 10# uint32_t ts_usec; 11# uint8_t type; // 0xfc for note 12# } 13 14import re 15import sys 16import time 17import datetime 18 19packet_types = [ "CMD =>", "EVT <=", "ACL =>", "ACL <="] 20 21def read_net_32(f): 22 a = f.read(1) 23 b = f.read(1) 24 c = f.read(1) 25 d = f.read(1) 26 return ord(a) << 24 | ord(b) << 16 | ord(c) << 8 | ord(d) 27 28def as_hex(data): 29 str_list = [] 30 for byte in data: 31 str_list.append("{0:02x} ".format(ord(byte))) 32 return ''.join(str_list) 33 34if len(sys.argv) == 1: 35 print 'Dump PacketLogger file' 36 print 'Copyright 2014, BlueKitchen GmbH' 37 print '' 38 print 'Usage: ', sys.argv[0], 'hci_dump.pklg' 39 exit(0) 40 41infile = sys.argv[1] 42 43with open (infile, 'rb') as fin: 44 pos = 0 45 try: 46 while True: 47 len = read_net_32(fin) 48 ts_sec = read_net_32(fin) 49 ts_usec = read_net_32(fin) 50 type = ord(fin.read(1)) 51 packet_len = len - 9; 52 if (packet_len > 66000): 53 print ("Error parsing pklg at offset %u (%x)." % (pos, pos)) 54 break 55 packet = fin.read(packet_len) 56 pos = pos + 4 + len 57 time = "[%s.%03u]" % (datetime.datetime.fromtimestamp(ts_sec).strftime("%Y-%m-%d %H:%M:%S"), ts_usec / 1000) 58 if type == 0xfc: 59 print time, "LOG", packet 60 continue 61 if type <= 0x03: 62 print time, packet_types[type], as_hex(packet) 63 except TypeError: 64 print ("Error parsing pklg at offset %u (%x)." % (pos, pos)) 65 66