# -*- coding: utf-8 -*- from . import devices from struct import pack, unpack TLV_TYPES = { 0x0001: "Checksum rectifier", 0x0002: "Padding", 0x0003: "Part number?", 0x0005: "Copyright notice", 0x0006: "Block Type 7 format definition", 0x0007: "Binary descriptor", 0x0008: "Binary Region 0C (boot.bin)", 0x0401: "Binary Component Firmware (SensorHub, ANT_BLE_BT, GPS, WiFi)", 0x0505: "Binary Region 05", 0x0555: "Binary Region 55", 0x0557: "Binary Region 57", 0x02bd: "Binary Region 0E (fw_all.bin)", 0xffff: "EOF marker", } class TLV: def __init__(self, type_id: int, expected_length: int, value=None, offset: int=None): self.type_id = type_id self.offset = offset self.comment = TLV_TYPES.get(type_id, "Type {:04x} / {:d}".format(type_id, type_id)) self.length = expected_length self.value = None self.is_parsed = False if value is not None: self.value = bytes(value) @staticmethod def factory(header: bytes, offset: int = None): (type_id, length) = unpack("