xref: /btstack/test/hfp/dump_test_sequence_from_pklg.py (revision 6ccd8248590f666db07dd7add13fecb4f5664fb5)
1#!/usr/bin/env python3
2# BlueKitchen GmbH (c) 2014
3
4# primitive dump for PacketLogger format
5
6# APPLE PacketLogger
7# typedef struct {
8#   uint32_t    len;
9#   uint32_t    ts_sec;
10#   uint32_t    ts_usec;
11#   uint8_t     type;   // 0xfc for note
12# }
13
14import re
15import sys
16import time
17import datetime
18
19packet_types = [ "CMD =>", "EVT <=", "ACL =>", "ACL <="]
20
21def read_net_32(f):
22    a = f.read(1)
23    b = f.read(1)
24    c = f.read(1)
25    d = f.read(1)
26    return ord(a) << 24 | ord(b) << 16 | ord(c) << 8 | ord(d)
27
28def as_hex(data):
29    str_list = []
30    for byte in data:
31        str_list.append("{0:02x} ".format(ord(byte)))
32    return ''.join(str_list)
33
34if len(sys.argv) < 2:
35    print ('Dump PacketLogger file')
36    print ('Copyright 2014, BlueKitchen GmbH')
37    print ('')
38    print ('Usage: ', sys.argv[0], 'hci_dump.pklg test_name')
39    exit(0)
40
41infile = sys.argv[1]
42test_name = sys.argv[2]
43separator = ""
44spaces = "    "
45print ("const char * "+test_name+"[] = {")
46
47
48with open (infile, 'rb') as fin:
49    try:
50        while True:
51            len     = read_net_32(fin)
52            ts_sec  = read_net_32(fin)
53            ts_usec = read_net_32(fin)
54            type    = ord(fin.read(1))
55            packet_len = len - 9;
56            packet  = fin.read(packet_len)
57            time    = "[%s.%03u]" % (datetime.datetime.fromtimestamp(ts_sec).strftime("%Y-%m-%d %H:%M:%S"), ts_usec / 1000)
58            if type == 0xfc:
59                packet = packet.replace("\n","\\n")
60                packet = packet.replace("\r","\\r")
61                packet = packet.replace("\"","\\\"")
62
63                parts = re.match('HFP_RX(.*)',packet)
64                if not parts:
65                    parts = re.match('HFP_TX(.*)',packet)
66
67                cmd = 0
68                if parts:
69                    hfp_cmds = parts.groups()[0].split('\\r\\n')
70                    for cmd in hfp_cmds:
71                        cmd = cmd.strip()
72                        if cmd != "":
73                            cmd = cmd.replace("\\r","")
74                            print (separator+spaces+"\""+cmd+"\"",)
75                            separator = ",\n"
76
77                else:
78                    parts = re.match('USER:\'(.*)\'.*',packet)
79                    if parts:
80                        cmd = 'USER:'+parts.groups()[0]
81                        print (separator+spaces+"\""+cmd+"\"",)
82                        separator = ",\n"
83
84
85    except TypeError:
86        print ("\n};\n")
87        exit(0)
88
89print ("\n};\n")
90