xref: /btstack/tool/dump_tlv.py (revision b50405356dba963d08b5536899c9a5751f8ca370)
15c544019SMatthias 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
81b466dbeSBoris Zweimüller
9a30fb010SMatthias Ringwalddef read_net_32(f):
10a30fb010SMatthias Ringwald    a = f.read(1)
111b466dbeSBoris Zweimüller    if not a:
12a30fb010SMatthias Ringwald        return -1
13a30fb010SMatthias Ringwald    b = f.read(1)
141b466dbeSBoris Zweimüller    if not b:
15a30fb010SMatthias Ringwald        return -1
16a30fb010SMatthias Ringwald    c = f.read(1)
171b466dbeSBoris Zweimüller    if not c:
18a30fb010SMatthias Ringwald        return -1
19a30fb010SMatthias Ringwald    d = f.read(1)
201b466dbeSBoris Zweimüller    if not d:
21a30fb010SMatthias Ringwald        return -1
22a30fb010SMatthias Ringwald    return ord(a) << 24 | ord(b) << 16 | ord(c) << 8 | ord(d)
23a30fb010SMatthias Ringwald
24*b5040535SMatthias Ringwalddef format_tag(tag):
25*b5040535SMatthias Ringwald    return ''.join([ '%c' % i if (i > 0x20 and i < 128) else '.'  for i in tag])
261b466dbeSBoris Zweimüller
27a30fb010SMatthias Ringwalddef as_hex(data):
28a30fb010SMatthias Ringwald    str_list = []
29a30fb010SMatthias Ringwald    for byte in data:
305c544019SMatthias Ringwald        str_list.append("{0:02x} ".format(byte))
31a30fb010SMatthias Ringwald    return ''.join(str_list)
32a30fb010SMatthias Ringwald
331b466dbeSBoris Zweimüller
34a30fb010SMatthias Ringwaldif len(sys.argv) == 1:
355c544019SMatthias Ringwald    print('Dump TLV file')
365c544019SMatthias Ringwald    print('Copyright 2017, BlueKitchen GmbH')
375c544019SMatthias Ringwald    print('')
385c544019SMatthias Ringwald    print('Usage: ', sys.argv[0], 'file.tlv')
39a30fb010SMatthias Ringwald    exit(0)
40a30fb010SMatthias Ringwald
41a30fb010SMatthias Ringwaldinfile = sys.argv[1]
42a30fb010SMatthias Ringwald
43a30fb010SMatthias Ringwaldwith open(infile, 'rb') as fin:
44a30fb010SMatthias Ringwald    pos = 0
45a30fb010SMatthias Ringwald    # check header
46a30fb010SMatthias Ringwald    magic_0 = read_net_32(fin)
47a30fb010SMatthias Ringwald    magic_1 = read_net_32(fin)
48a30fb010SMatthias Ringwald    if magic_0 != 0x42547374 or magic_1 != 0x61636b00:
49a30fb010SMatthias Ringwald        print("%x" % magic_0)
50a30fb010SMatthias Ringwald        print("%x" % magic_1)
51a30fb010SMatthias Ringwald        print("Not a valid BTstack .tlv file\n")
52a30fb010SMatthias Ringwald        exit(0)
53a30fb010SMatthias Ringwald    pos += 8
54a30fb010SMatthias Ringwald    print("Valid .tlv file")
55a30fb010SMatthias Ringwald    while True:
56*b5040535SMatthias Ringwald        tag = fin.read(4)
57*b5040535SMatthias Ringwald        if not tag:
58a30fb010SMatthias Ringwald            break
59a30fb010SMatthias Ringwald        pos += 4
60a30fb010SMatthias Ringwald        len = read_net_32(fin)
61a30fb010SMatthias Ringwald        pos += 4
62a30fb010SMatthias Ringwald        packet = fin.read(len)
63a30fb010SMatthias Ringwald        pos += len
64*b5040535SMatthias Ringwald        print("'%s' (%s): %s" % (format_tag(tag), as_hex(tag)[0:11], as_hex(packet)))
65a30fb010SMatthias Ringwald    print("Done")
66