From: Philipp Klaus Date: Fri, 19 Sep 2014 14:19:43 +0000 (+0200) Subject: Adding SubSubEvent support to the unpacker X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=0cc18400fec46b8f3dd84eb70cee2a8ec8179ffa;p=mvdsensorcontrol.git Adding SubSubEvent support to the unpacker --- diff --git a/tools/python_hld_unpacker/hld_unpacker/unpacker.py b/tools/python_hld_unpacker/hld_unpacker/unpacker.py index 751636a..0314ed9 100644 --- a/tools/python_hld_unpacker/hld_unpacker/unpacker.py +++ b/tools/python_hld_unpacker/hld_unpacker/unpacker.py @@ -24,14 +24,14 @@ BLOCKSIZE = WORDSIZE * 2 class Event(object): - # the event header consists of 8 words + # the Event header consists of 8 words HEADERSIZE = WORDSIZE * 8 + HEADER_FMT = '>8I' def __init__(self, header_bytes, payload_bytes): self.header_bytes = header_bytes self.payload_bytes = payload_bytes - struct_fmt = '<{:d}I'.format(Event.HEADERSIZE//WORDSIZE) - self.header_words = struct.unpack(struct_fmt, header_bytes) + self.header_words = struct.unpack(self.HEADER_FMT, header_bytes) self._set_properties(*self.header_words) self.subevents = list(read_subevents(io.BytesIO(self.payload_bytes))) @@ -59,15 +59,16 @@ class Event(object): class SubEvent(object): - # the event header consists of 8 words + # the SubEvent header consists of 4 words HEADERSIZE = WORDSIZE * 4 + HEADER_FMT = '>4I' def __init__(self, header_bytes, payload_bytes): self.header_bytes = header_bytes self.payload_bytes = payload_bytes - struct_fmt = '>{:d}I'.format(SubEvent.HEADERSIZE//WORDSIZE) - self.header_words = struct.unpack(struct_fmt, header_bytes) + self.header_words = struct.unpack(self.HEADER_FMT, header_bytes) self._set_properties(*self.header_words) + self.subsubevents = list(read_subsubevents(io.BytesIO(self.payload_bytes))) def _set_properties(self, size, decoding, id, trignr): self.size = size @@ -76,8 +77,34 @@ class SubEvent(object): self.trignr = trignr def __str__(self): - fmt = "size: 0x{:08x} decoding: 0x{:08x} id: 0x{:08x} trignr: 0x{:08x}\n" + fmt = " |-> size: 0x{:08x} decoding: 0x{:08x} id: 0x{:08x} trignr: 0x{:08x}\n" out = fmt.format(self.size, self.decoding, self.id, self.trignr) + #out += "\n" + #out += str_32bit_chunks(self.payload_bytes) + #out += "\n" + for subsubevent in self.subsubevents: + out += str(subsubevent) + return out + +class SubSubEvent(object): + + # The SubSubEvent header consists of a single words + HEADERSIZE = WORDSIZE * 1 + HEADER_FMT = '>HH' + + def __init__(self, header_bytes, payload_bytes): + self.header_bytes = header_bytes + self.payload_bytes = payload_bytes + self.header_words = struct.unpack(self.HEADER_FMT, header_bytes) + self._set_properties(*self.header_words) + + def _set_properties(self, size, address): + self.size = size + self.address = address + + def __str__(self): + fmt = " |-> size: 0x{:04x} address: 0x{:04x}\n" + out = fmt.format(self.size, self.address) out += "\n" out += str_32bit_chunks(self.payload_bytes) return out @@ -104,7 +131,7 @@ def read_subevents(subevents_stream): while True: header_bytes = subevents_stream.read(SubEvent.HEADERSIZE) if len(header_bytes) < SubEvent.HEADERSIZE: break - # The size of the event in bytes is given in the first data word of the event: + # The size of the subevent in bytes is given in the first data word: size = struct.unpack('>I', header_bytes[:WORDSIZE])[0] # We read already all bytes belonging to the header, so we need to subtract them actual_num_bytes = size - SubEvent.HEADERSIZE @@ -114,3 +141,12 @@ def read_subevents(subevents_stream): #pdb.set_trace() yield SubEvent(header_bytes, data_bytes) +def read_subsubevents(subsubevents_stream): + while True: + header_bytes = subsubevents_stream.read(SubSubEvent.HEADERSIZE) + if len(header_bytes) < SubSubEvent.HEADERSIZE: break + # The remaining size of the subsubevent in four-byte/32-bit words is given in the first two bytes: + size = struct.unpack('>HH', header_bytes[:WORDSIZE])[0] + data_bytes = subsubevents_stream.read(size*WORDSIZE) + #pdb.set_trace() + yield SubSubEvent(header_bytes, data_bytes)