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