xref: /btstack/tool/dump_tlv.py (revision 1b466dbe99438b8cf68092abf65489055a2ef521)
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
8*1b466dbeSBoris Zweimüller
9a30fb010SMatthias Ringwalddef read_net_32(f):
10a30fb010SMatthias Ringwald    a = f.read(1)
11*1b466dbeSBoris Zweimüller    if not a:
12a30fb010SMatthias Ringwald        return -1
13a30fb010SMatthias Ringwald    b = f.read(1)
14*1b466dbeSBoris Zweimüller    if not b:
15a30fb010SMatthias Ringwald        return -1
16a30fb010SMatthias Ringwald    c = f.read(1)
17*1b466dbeSBoris Zweimüller    if not c:
18a30fb010SMatthias Ringwald        return -1
19a30fb010SMatthias Ringwald    d = f.read(1)
20*1b466dbeSBoris 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*1b466dbeSBoris Zweimüller
25a30fb010SMatthias Ringwalddef as_hex(data):
26a30fb010SMatthias Ringwald    str_list = []
27a30fb010SMatthias Ringwald    for byte in data:
285c544019SMatthias Ringwald        str_list.append("{0:02x} ".format(byte))
29a30fb010SMatthias Ringwald    return ''.join(str_list)
30a30fb010SMatthias Ringwald
31*1b466dbeSBoris Zweimüller
32a30fb010SMatthias Ringwaldif len(sys.argv) == 1:
335c544019SMatthias Ringwald    print('Dump TLV file')
345c544019SMatthias Ringwald    print('Copyright 2017, BlueKitchen GmbH')
355c544019SMatthias Ringwald    print('')
365c544019SMatthias Ringwald    print('Usage: ', sys.argv[0], 'file.tlv')
37a30fb010SMatthias Ringwald    exit(0)
38a30fb010SMatthias Ringwald
39a30fb010SMatthias Ringwaldinfile = sys.argv[1]
40a30fb010SMatthias Ringwald
41a30fb010SMatthias Ringwaldwith open(infile, 'rb') as fin:
42a30fb010SMatthias Ringwald    pos = 0
43a30fb010SMatthias Ringwald    # check header
44a30fb010SMatthias Ringwald    magic_0 = read_net_32(fin)
45a30fb010SMatthias Ringwald    magic_1 = read_net_32(fin)
46a30fb010SMatthias Ringwald    if magic_0 != 0x42547374 or magic_1 != 0x61636b00:
47a30fb010SMatthias Ringwald        print("%x" % magic_0)
48a30fb010SMatthias Ringwald        print("%x" % magic_1)
49a30fb010SMatthias Ringwald        print("Not a valid BTstack .tlv file\n")
50a30fb010SMatthias Ringwald        exit(0)
51a30fb010SMatthias Ringwald    pos += 8
52a30fb010SMatthias Ringwald    print("Valid .tlv file")
53a30fb010SMatthias Ringwald    while True:
54a30fb010SMatthias Ringwald        tag = read_net_32(fin)
55a30fb010SMatthias Ringwald        if tag < 0:
56a30fb010SMatthias Ringwald            break
57a30fb010SMatthias Ringwald        pos += 4
58a30fb010SMatthias Ringwald        len = read_net_32(fin)
59a30fb010SMatthias Ringwald        pos += 4
60a30fb010SMatthias Ringwald        packet = fin.read(len)
61a30fb010SMatthias Ringwald        pos += len
6246412789SMatthias Ringwald        print('%04x: ' % tag + as_hex(packet))
63a30fb010SMatthias Ringwald    print("Done")
64