aboutsummaryrefslogtreecommitdiff
path: root/decode/decode.py
blob: 5be72cdc01bf84b66fe79e2528629127213fb529 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details:
#
# Copyright (C) 2011 Red Hat, Inc.
#

import binascii
import string
import sys
import defs
from packet import Packet

packets = []
control = None
transfer = None

def get_protocol(arg):
    return arg[arg.index("=") + 1:]

if __name__ == "__main__":
    i = 1
    if sys.argv[i].startswith("--control="):
        control = get_protocol(sys.argv[i])
        i = i + 1
    if sys.argv[i].startswith("--transfer="):
        transfer = get_protocol(sys.argv[i])
        i = i + 1

    path = sys.argv[i]
    f = open(path, 'r')
    lines = f.readlines()
    f.close()

    in_packet = False
    finish_packet = False
    pkt_lines = []
    for l in lines:
        if l[0] == '[':
            # Start of a packet
            if "]  >>>  URB" in l or "]  <<<  URB" in l:
                if in_packet == True:
                    in_packet = False
                    finish_packet = True
                else:
                    in_packet = True
            elif "] UsbSnoop - " in l:
                # Packet done?
                if in_packet == True:
                    in_packet = False
                    finish_packet = True

        if finish_packet == True:
            packets.append(Packet(pkt_lines, control, transfer))
            pkt_lines = []
            finish_packet = False

        if in_packet == True:
            pkt_lines.append(l)

    for p in packets:
        p.show()