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 test_struct_w_array_decode(self): res = _encode_from_native_and_then_decode( { 0: [uint(5), uint(6)], 1: 23 }, TestClusterObjects.StructWithArray) self.assertEqual(res, TestClusterObjects.StructWithArray(X=[5, 6], Y=23))
def test_struct_w_string_and_string_decode(self): res = _encode_from_native_and_then_decode( {0: 'test-str', 1: {0: uint(12), 1: 34}, 2: b'byte-string'}, TestClusterObjects.StructWithEmbeddedStructAndString) self.assertEqual( res, TestClusterObjects.StructWithEmbeddedStructAndString( X='test-str', Y=TestClusterObjects.C(X=12, Y=34), Z=b'byte-string'))
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 test_basic_decode(self): res = _encode_from_native_and_then_decode( {0: uint(5), 1: 23}, TestClusterObjects.C) self.assertEqual(res, TestClusterObjects.C(X=5, Y=23))
def test_struct_decode(self): res = _encode_from_native_and_then_decode( {0: uint(42), 1: 24}, TestClusterObjects.C) self.assertEqual(res, TestClusterObjects.C(X=42, Y=24))
def test_struct_with_array_of_struct_with_array_decode(self): res = _encode_from_native_and_then_decode( {0: ['test-str1', 'test-str2', 'test-str3'], 1: [{0: uint(12), 1: 34}, {0: uint(56), 1: 78}, {0: uint(23), 1: 33}], 2: [{0: [uint(12), uint(34)], 1: 5678}, {0: [uint(1), uint(3)], 1: 57}, {0: [uint(2), uint(4)], 1: 68}, {0: [uint(233), uint(333)], 1: 333}], 3: [{0: ['test-str4'], 1: [{0: uint(55), 1: 66}], 2: [{0: [uint(123)], 1: 456}], 3: []}]}, TestClusterObjects.StructWithArrayOfStructWithArray) C = TestClusterObjects.C SWA = TestClusterObjects.StructWithArray SWAOSWA = TestClusterObjects.StructWithArrayOfStructWithArray data = TestClusterObjects.StructWithArrayOfStructWithArray( X=['test-str1', 'test-str2', 'test-str3'], Y=[C(X=12, Y=34), C(X=56, Y=78), C(X=23, Y=33)], Z=[SWA(X=[12, 34], Y=5678), SWA(X=[1, 3], Y=57), SWA(X=[2, 4], Y=68), SWA(X=[233, 333], Y=333)], W=[SWAOSWA(X=['test-str4'], Y=[C(X=55, Y=66)], Z=[SWA(X=[123], Y=456)], W=[])]) self.assertEqual(res, data)