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