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