def decode(self, payload, dikt): """ 1.) Determine length of child and slice off respective payload 2.) Slice off checksum bytes 3.) Calculate checksum from child payload 4.) decode child 5.) Return remaining payload If the checksum is not correct an exception is raised """ self.logger.info("data payload %r" % payload) child_len = self.child.length(payload) if child_len == len(payload): # consumer case child_len -= self.length # reduce child-length by checksum child_payload = payload[:child_len] chk_bytes = payload[child_len:self.length] _payload = payload[child_len + self.length:] # remaining payload chk_bytes, dikt = Integer.decode(self, chk_bytes, dikt) chk_sum = self.checksum(self, child_payload) if dikt[self.name] != chk_sum: raise ValueError('Checksum failure for %s' % self.child.name) else: child_payload, dikt = self.child.decode(child_payload, dikt) return _payload, dikt
def encode(self, dikt, payload): _payload = self.child.encode(dikt, '') chksum = self.checksum(_payload) dikt[self.name] = chksum dikt, _payload = Integer.encode(self, dikt, _payload) return dikt, payload + _payload
def __init__(self, name, length, endian='!'): self.child = None Integer.__init__(self, name, False, length, endian)