From 86698fca30941772a3b77106fc2b2e83a40bcfeb Mon Sep 17 00:00:00 2001 From: Ole Troan Date: Wed, 7 Dec 2022 15:30:58 +0100 Subject: papi: vla list of fixed strings Handle a variable length array of fixed strings. Like: fixed_string = VPPType("fixed_string", [["string", "data", 32]]) s = VPPType("string_vla", [["u32", "length"], ["fixed_string", "services", 0, "length"]]) Previously instead of packing and unpacking as strings, exception packed as u8 instead of list. Type: fix Signed-off-by: Ole Troan Change-Id: I501a8a4755828042e1539fd5a54eacec21c5e364 Signed-off-by: Ole Troan --- .../python/vpp_papi/tests/test_vpp_serializer.py | 26 ++++++++++++++++++++++ src/vpp-api/python/vpp_papi/vpp_serializer.py | 6 ++--- 2 files changed, 28 insertions(+), 4 deletions(-) (limited to 'src/vpp-api') diff --git a/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py b/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py index eee38f00632..ab6ce81b64b 100755 --- a/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py @@ -426,6 +426,32 @@ class TestAddType(unittest.TestCase): nt, size = s.unpack(b) self.assertEqual(len(b), size) + # Try same with VLA u8 + byte_array = [b"\0"] * (10) + vla_u8 = VPPType("vla_u8", [["u8", "length"], ["u8", "data", 0, "length"]]) + b = vla_u8.pack({"length": len(byte_array), "data": byte_array}) + nt, size = vla_u8.unpack(b) + + # VLA Array of fixed length strings + fixed_string = VPPType("fixed_string", [["string", "data", 32]]) + s = VPPType( + "string_vla", [["u32", "length"], ["fixed_string", "services", 0, "length"]] + ) + + string_list = [{"data": "foobar1"}, {"data": "foobar2"}] + b = s.pack({"length": 2, "services": string_list}) + nt, size = s.unpack(b) + + # Try same with u8 + fixed_u8 = VPPType("fixed_u8", [["u8", "data", 32]]) + s = VPPType( + "u8_vla", [["u32", "length"], ["fixed_string", "services", 0, "length"]] + ) + + u8_list = [{"data": "foobar1"}, {"data": "foobar2"}] + b = s.pack({"length": 2, "services": u8_list}) + nt, size = s.unpack(b) + def test_message(self): foo = VPPMessage( "foo", diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py index d7da457ea1a..d8461398765 100644 --- a/src/vpp-api/python/vpp_papi/vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py @@ -304,9 +304,8 @@ class VLAList(Packer): len(lst), kwargs[self.length_field] ) ) - # u8 array - if self.packer.size == 1: + if self.packer.size == 1 and self.field_type == "u8": if isinstance(lst, list): return b"".join(lst) return bytes(lst) @@ -321,7 +320,7 @@ class VLAList(Packer): total = 0 # u8 array - if self.packer.size == 1: + if self.packer.size == 1 and self.field_type == "u8": if result[self.index] == 0: return b"", 0 p = BaseTypes("u8", result[self.index]) @@ -618,7 +617,6 @@ class VPPType(Packer): self.packers.append(p) size += p.size - self.size = size self.tuple = collections.namedtuple(name, self.fields, rename=True) types[name] = self -- cgit 1.2.3-korg