def calculate_crc(self): """calculate_crc() -> CRC value Returns the frame's calculated CRC, based on the information present in the header. This is the value that _should_ be in the bytestream, not the one that is. Use header._crc16 to get this. """ # TODO: Check for empty side info? # First two bytes of the header are skipped buf = self.bytes(include_crc = False) return crc16(memoryview(buf)[2:])
def bytes(self, include_crc = True): """bytes(include_crc = True) -> bytearray Returns a representation of the header in packed binary format. If include_crc is False, the CRC will be omitted regardless of the header.crc flag. """ buf = bytearray(self._FORMAT.length) bitpack_into(self._FORMAT, buf, 0, *[self.__dict__[k] for k in self._FIELDS]) pos = len(buf) buf.extend(self._side_info) if include_crc and self.crc: crc = crc16(memoryview(buf)[2:]) buf[pos:pos] = 2 # Inserts two bytes at pos struct.pack_into('>H', buf, pos, crc) return buf