]> jspc29.x-matter.uni-frankfurt.de Git - mvdsensorcontrol.git/commitdiff
Adding SubSubEvent support to the unpacker
authorPhilipp Klaus <klaus@physik.uni-frankfurt.de>
Fri, 19 Sep 2014 14:19:43 +0000 (16:19 +0200)
committerPhilipp Klaus <klaus@physik.uni-frankfurt.de>
Fri, 19 Sep 2014 14:19:43 +0000 (16:19 +0200)
tools/python_hld_unpacker/hld_unpacker/unpacker.py

index 751636a4e337e1e38685b8d54508e451452278dc..0314ed9d44527c9ebc2a4ebf3f8740b210730cfc 100644 (file)
@@ -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)