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) < 2: 35 print 'Dump PacketLogger file' 36 print 'Copyright 2014, BlueKitchen GmbH' 37 print '' 38 print 'Usage: ', sys.argv[0], 'hci_dump.pklg test_name' 39 exit(0) 40 41infile = sys.argv[1] 42test_name = sys.argv[2] 43separator = "" 44spaces = " " 45print "const char * "+test_name+"[] = {" 46 47with open (infile, 'rb') as fin: 48 try: 49 while True: 50 len = read_net_32(fin) 51 ts_sec = read_net_32(fin) 52 ts_usec = read_net_32(fin) 53 type = ord(fin.read(1)) 54 packet_len = len - 9; 55 packet = fin.read(packet_len) 56 time = "[%s.%03u]" % (datetime.datetime.fromtimestamp(ts_sec).strftime("%Y-%m-%d %H:%M:%S"), ts_usec / 1000) 57 if type == 0xfc: 58 packet = packet.replace("\n","\\n"); 59 packet = packet.replace("\r","\\r"); 60 packet = packet.replace("\"","\\\""); 61 62 parts = re.match('HFP_RX(.*)',packet) 63 if not parts: 64 parts = re.match('PTS_TEST_TX(.*)',packet) 65 66 cmd = 0 67 if parts: 68 cmd = parts.groups()[0].strip() 69 cmd = cmd.replace("\\n",""); 70 cmd = cmd.replace("\\r",""); 71 72 if cmd: 73 #print "CMD ", packet 74 cmd_parts = re.match('(.*)OK', cmd) 75 if (cmd_parts): 76 if cmd_parts.groups()[0] <> "": 77 print separator+spaces+"\""+cmd_parts.groups()[0]+"\"", 78 print separator+spaces+"\"OK\"", 79 else: 80 print separator+spaces+"\""+cmd+"\"", 81 82 separator = ",\n" 83 84 except TypeError: 85 print "\n};\n" 86 exit(0) 87 88print "\n};\n" 89 90