Dan Williams | c9d0dea | 2011-12-30 20:36:01 -0600 | [diff] [blame^] | 1 | #!/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 | |
| 18 | from xml.sax import saxutils |
| 19 | from xml.sax import handler |
| 20 | |
| 21 | packets = [] |
| 22 | counts = {} |
| 23 | |
| 24 | class 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 | |
| 75 | from xml.sax import make_parser |
| 76 | from xml.sax import parse |
| 77 | import sys |
| 78 | |
| 79 | if __name__ == "__main__": |
| 80 | dh = FindPackets() |
| 81 | parse(sys.argv[1], dh) |
| 82 | |