aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/parsedump.py
blob: 93d8d14e22225ca820bb71cab64ec8f2a5c4a681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# Script to parse a serial port data dump
#
# Copyright (C) 2016  Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os, sys, logging
import msgproto


def read_dictionary(filename):
    dfile = open(filename, "rb")
    dictionary = dfile.read()
    dfile.close()
    return dictionary


def main():
    dict_filename, data_filename = sys.argv[1:]

    dictionary = read_dictionary(dict_filename)

    mp = msgproto.MessageParser()
    mp.process_identify(dictionary, decompress=False)

    f = open(data_filename, "rb")
    fd = f.fileno()
    data = bytearray()
    while 1:
        newdata = os.read(fd, 4096)
        if not newdata:
            break
        data += bytearray(newdata)
        while 1:
            l = mp.check_packet(data)
            if l == 0:
                break
            if l < 0:
                logging.error("Invalid data")
                data = data[-l:]
                continue
            msgs = mp.dump(data[:l])
            sys.stdout.write("\n".join(msgs[1:]) + "\n")
            data = data[l:]


if __name__ == "__main__":
    main()