blob: b14b08945516c1d5e4024643aa9f7ef00f5076c1 [file] [log] [blame]
Dan Williamsc9d0dea2011-12-30 20:36:01 -06001#!/usr/bin/python
2# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details:
13#
14# Copyright (C) 2011 Red Hat, Inc.
15#
16# --- Dumps UsbSnoopy XML export files
17
18from xml.sax import saxutils
19from xml.sax import handler
20
21packets = []
22counts = {}
23
24class FindPackets(handler.ContentHandler):
25 def __init__(self):
26 self.inFunction = False
27 self.inPayload = False
28 self.ignore = False
29 self.inTimestamp = False
30 self.timestamp = None
31 self.packet = None
32
33 def startElement(self, name, attrs):
34 if name == "function":
35 self.inFunction = True
36 elif name == "payloadbytes":
37 self.inPayload = True
38 elif name == "timestamp":
39 self.inTimestamp = True
40
41 def characters(self, ch):
42 if self.ignore:
43 return
44
45 stripped = ch.strip()
46 if self.inFunction and ch != "BULK_OR_INTERRUPT_TRANSFER":
47 self.ignore = True
48 return
49 elif self.inTimestamp:
50 self.timestamp = stripped
51 elif self.inPayload and len(stripped) > 0:
52 if self.packet == None:
53 self.packet = stripped
54 else:
55 self.packet += stripped
56
57 def endElement(self, name):
58 if name == "function":
59 self.inFunction = False
60 elif name == "payloadbytes":
61 self.inPayload = False
62 elif name == "payload":
63 if self.packet:
64 import binascii
65 bytes = binascii.a2b_hex(self.packet)
66 print bytes
67 self.packet = None
68
69 self.ignore = False
70 self.timestamp = None
71 elif name == "timestamp":
72 self.inTimestamp = False
73
74
75from xml.sax import make_parser
76from xml.sax import parse
77import sys
78
79if __name__ == "__main__":
80 dh = FindPackets()
81 parse(sys.argv[1], dh)
82