xref: /btstack/tool/dump_tlv.py (revision 5c54401929043df982e040eba652f5fd7763ce15)
1*5c544019SMatthias Ringwald#!/usr/bin/env python3
2a30fb010SMatthias Ringwald# BlueKitchen GmbH (c) 2017
3a30fb010SMatthias Ringwald
4a30fb010SMatthias Ringwald# primitive dump for .tlv format
5a30fb010SMatthias Ringwald
6a30fb010SMatthias Ringwaldimport sys
7a30fb010SMatthias Ringwald
8a30fb010SMatthias Ringwalddef read_net_32(f):
9a30fb010SMatthias Ringwald    a = f.read(1)
10a30fb010SMatthias Ringwald    if a == '':
11a30fb010SMatthias Ringwald    	return -1
12a30fb010SMatthias Ringwald    b = f.read(1)
13a30fb010SMatthias Ringwald    if b == '':
14a30fb010SMatthias Ringwald    	return -1
15a30fb010SMatthias Ringwald    c = f.read(1)
16a30fb010SMatthias Ringwald    if c == '':
17a30fb010SMatthias Ringwald    	return -1
18a30fb010SMatthias Ringwald    d = f.read(1)
19a30fb010SMatthias Ringwald    if d == '':
20a30fb010SMatthias Ringwald    	return -1
21a30fb010SMatthias Ringwald    return ord(a) << 24 | ord(b) << 16 | ord(c) << 8 | ord(d)
22a30fb010SMatthias Ringwald
23a30fb010SMatthias Ringwalddef as_hex(data):
24a30fb010SMatthias Ringwald	str_list = []
25a30fb010SMatthias Ringwald	for byte in data:
26*5c544019SMatthias Ringwald	    str_list.append("{0:02x} ".format(byte))
27a30fb010SMatthias Ringwald	return ''.join(str_list)
28a30fb010SMatthias Ringwald
29a30fb010SMatthias Ringwaldif len(sys.argv) == 1:
30*5c544019SMatthias Ringwald	print ('Dump TLV file')
31*5c544019SMatthias Ringwald	print ('Copyright 2017, BlueKitchen GmbH')
32*5c544019SMatthias Ringwald	print ('')
33*5c544019SMatthias Ringwald	print ('Usage: ', sys.argv[0], 'file.tlv')
34a30fb010SMatthias Ringwald	exit(0)
35a30fb010SMatthias Ringwald
36a30fb010SMatthias Ringwaldinfile = sys.argv[1]
37a30fb010SMatthias Ringwald
38a30fb010SMatthias Ringwaldwith open (infile, 'rb') as fin:
39a30fb010SMatthias Ringwald	pos = 0
40a30fb010SMatthias Ringwald	try:
41a30fb010SMatthias Ringwald		# check header
42a30fb010SMatthias Ringwald		magic_0 = read_net_32(fin)
43a30fb010SMatthias Ringwald		magic_1 = read_net_32(fin)
44a30fb010SMatthias Ringwald		if magic_0 != 0x42547374 or magic_1 != 0x61636b00:
45a30fb010SMatthias Ringwald			print("%x" % magic_0)
46a30fb010SMatthias Ringwald			print("%x" % magic_1)
47a30fb010SMatthias Ringwald			print ("Not a valid BTstack .tlv file\n")
48a30fb010SMatthias Ringwald			exit(0)
49a30fb010SMatthias Ringwald		pos += 8
50a30fb010SMatthias Ringwald		print("Valid .tlv file")
51a30fb010SMatthias Ringwald		while True:
52a30fb010SMatthias Ringwald			tag     = read_net_32(fin)
53a30fb010SMatthias Ringwald			if tag < 0:
54a30fb010SMatthias Ringwald				break
55a30fb010SMatthias Ringwald			pos += 4
56a30fb010SMatthias Ringwald			len     = read_net_32(fin)
57a30fb010SMatthias Ringwald			pos += 4
58a30fb010SMatthias Ringwald			packet  = fin.read(len)
59a30fb010SMatthias Ringwald			pos += len
6046412789SMatthias Ringwald			print('%04x: ' % tag + as_hex(packet))
61a30fb010SMatthias Ringwald		print("Done")
62a30fb010SMatthias Ringwald
63a30fb010SMatthias Ringwald	except TypeError:
64a30fb010SMatthias Ringwald		print ("Error parsing tlv at offset %u (%x)." % (pos, pos))
65