def DictToTLVWithWriter(self, debugPath: str, tag, data: Mapping, writer: tlv.TLVWriter): writer.startStructure(tag) for field in self.Fields: val = data.get(field.Label, None) field.PutFieldToTLV(field.Tag, val, writer, debugPath + f'.{field.Label}') writer.endContainer()
def generate_header_tlv(args: object, payload_size: int, payload_digest: bytes): """ Generate anonymous TLV structure with fields describing the OTA image contents """ fields = { HeaderTag.VENDOR_ID: uint(args.vendor_id), HeaderTag.PRODUCT_ID: uint(args.product_id), HeaderTag.VERSION: uint(args.version), HeaderTag.VERSION_STRING: args.version_str, HeaderTag.PAYLOAD_SIZE: uint(payload_size), HeaderTag.DIGEST_TYPE: uint(DIGEST_ALGORITHM_ID[args.digest_algorithm]), HeaderTag.DIGEST: payload_digest, } if args.min_version is not None: fields.update({HeaderTag.MIN_VERSION: uint(args.min_version)}) if args.max_version is not None: fields.update({HeaderTag.MAX_VERSION: uint(args.max_version)}) if args.release_notes is not None: fields.update({HeaderTag.RELEASE_NOTES_URL: args.release_notes}) writer = TLVWriter() writer.put(None, fields) return writer.encoding
def generate_header_tlv(file_infos: list): """ Generate TLV structure describing OTA image contents: Header ::= [0] FileInfoList: [ [0]: [0] FileId: <file_id> [1] FileSize: <file_size> [1]: [0] FileId: <file_id> [1] FileSize: <file_size> ... ] """ writer = TLVWriter() writer.put(None, { HeaderTag.FILE_INFO_LIST: [{ FileInfoTag.FILE_ID: uint(file_id), FileInfoTag.FILE_SIZE: uint(file_size), } for file_id, file_size in file_infos] }) return writer.encoding
def DictToTLVWithWriter(self, debugPath: str, tag, data: Mapping, writer: tlv.TLVWriter): writer.startStructure(tag) for field in self.Fields: val = data.get(field.Label, None) if val is None: raise ValueError( f"Field {debugPath}.{field.Label} is missing in the given dict" ) field.PutFieldToTLV(field.Tag, val, writer, debugPath + f'.{field.Label}') writer.endContainer()
def _PutSingleElementToTLV(self, tag, val, elementType, writer: tlv.TLVWriter, debugPath: str = '?'): if issubclass(elementType, ClusterObject): if not isinstance(val, dict): raise ValueError( f"Field {debugPath}.{self.Label} expected a struct, but got {type(val)}") elementType.descriptor.DictToTLVWithWriter( f'{debugPath}.{self.Label}', tag, val, writer) return try: val = elementType(val) except Exception: raise ValueError( f"Field {debugPath}.{self.Label} expected {elementType}, but got {type(val)}") writer.put(tag, val)
def PutFieldToTLV(self, tag, val, writer: tlv.TLVWriter, debugPath: str = '?'): if not self.IsArray: self._PutSingleElementToTLV(tag, val, writer, debugPath) return if not isinstance(val, List): raise ValueError( f"Field {debugPath}.{self.Label} expected List[{self.Type}], but got {type(val)}" ) writer.startArray(tag) for i, v in enumerate(val): self._PutSingleElementToTLV(None, v, writer, debugPath + f'[{i}]') writer.endContainer()
def PutFieldToTLV(self, tag, val, writer: tlv.TLVWriter, debugPath: str = '?'): if (val == NullValue): if (GetUnionUnderlyingType(self.Type, Nullable) is None): raise ValueError( f"Field {debugPath}.{self.Label} was not nullable, but got a null" ) writer.put(tag, None) elif (val is None): if (GetUnionUnderlyingType(self.Type, type(None)) is None): raise ValueError( f"Field {debugPath}.{self.Label} was not optional, but encountered None" ) else: # # If it is an optional or nullable type, it's going to be a union. # So, let's get at the 'real' type within that union before proceeding, # since at this point, we're guarenteed to not get None or Null as values. # elementType = GetUnionUnderlyingType(self.Type) if (elementType is None): elementType = self.Type if not isinstance(val, List): self._PutSingleElementToTLV(tag, val, elementType, writer, debugPath) return writer.startArray(tag) # Get the type of the list. This is a generic, which has its sub-type information of the list element # inside its type argument. (elementType, ) = typing.get_args(elementType) for i, v in enumerate(val): self._PutSingleElementToTLV(None, v, elementType, writer, debugPath + f'[{i}]') writer.endContainer()
def _encode_from_native_and_then_decode(data, cls: typing.Union[ClusterObjects.ClusterObject, ClusterObjects.ClusterAttributeDescriptor]): tlv = TLVWriter() tlv.put(None, data) return cls.FromTLV(bytes(tlv.encoding))
def _getEncoded(self, val, tag=None): writer = TLVWriter() writer.put(tag, val) return writer.encoding