Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at: |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 15 | import collections |
Paul Vinciguerra | 2e87606 | 2020-05-12 10:45:17 -0400 | [diff] [blame] | 16 | from enum import IntFlag |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 17 | import logging |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 18 | import socket |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 19 | import struct |
| 20 | import sys |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 21 | |
Paul Vinciguerra | 2e87606 | 2020-05-12 10:45:17 -0400 | [diff] [blame] | 22 | from . import vpp_format |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 23 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 24 | |
| 25 | # |
| 26 | # Set log-level in application by doing e.g.: |
| 27 | # logger = logging.getLogger('vpp_serializer') |
| 28 | # logger.setLevel(logging.DEBUG) |
| 29 | # |
Paul Vinciguerra | 5395c6a | 2020-12-02 17:43:59 -0500 | [diff] [blame] | 30 | logger = logging.getLogger('vpp_papi.serializer') |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 31 | |
Paul Vinciguerra | d7adc29 | 2020-12-02 14:34:27 -0500 | [diff] [blame] | 32 | |
| 33 | def check(d): |
| 34 | return type(d) is dict or type(d) is bytes |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 35 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 36 | |
| 37 | def conversion_required(data, field_type): |
| 38 | if check(data): |
| 39 | return False |
| 40 | try: |
| 41 | if type(data).__name__ in vpp_format.conversion_table[field_type]: |
| 42 | return True |
| 43 | except KeyError: |
| 44 | return False |
| 45 | |
| 46 | |
| 47 | def conversion_packer(data, field_type): |
| 48 | t = type(data).__name__ |
| 49 | return types[field_type].pack(vpp_format. |
| 50 | conversion_table[field_type][t](data)) |
| 51 | |
| 52 | |
| 53 | def conversion_unpacker(data, field_type): |
| 54 | if field_type not in vpp_format.conversion_unpacker_table: |
| 55 | return data |
| 56 | return vpp_format.conversion_unpacker_table[field_type](data) |
| 57 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 58 | |
Paul Vinciguerra | d7adc29 | 2020-12-02 14:34:27 -0500 | [diff] [blame] | 59 | class Packer: |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 60 | options = {} |
| 61 | |
| 62 | def pack(self, data, kwargs): |
| 63 | raise NotImplementedError |
| 64 | |
| 65 | def unpack(self, data, offset, result=None, ntc=False): |
| 66 | raise NotImplementedError |
| 67 | |
| 68 | # override as appropriate in subclasses |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 69 | @staticmethod |
| 70 | def _get_packer_with_options(f_type, options): |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 71 | return types[f_type] |
| 72 | |
| 73 | def get_packer_with_options(self, f_type, options): |
| 74 | if options is not None: |
| 75 | try: |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 76 | c = types[f_type].__class__ |
| 77 | return c._get_packer_with_options(f_type, options) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 78 | except IndexError: |
| 79 | raise VPPSerializerValueError( |
| 80 | "Options not supported for {}{} ({})". |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 81 | format(f_type, types[f_type].__class__, |
| 82 | options)) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 83 | |
| 84 | |
| 85 | class BaseTypes(Packer): |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 86 | def __init__(self, type, elements=0, options=None): |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 87 | self._type = type |
| 88 | self._elements = elements |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 89 | base_types = {'u8': '>B', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 90 | 'i8': '>b', |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 91 | 'string': '>s', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 92 | 'u16': '>H', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 93 | 'i16': '>h', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 94 | 'u32': '>I', |
| 95 | 'i32': '>i', |
| 96 | 'u64': '>Q', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 97 | 'i64': '>q', |
Paul Vinciguerra | 888640a | 2019-05-14 21:01:28 -0400 | [diff] [blame] | 98 | 'f64': '=d', |
Ole Troan | f47e9b6 | 2018-10-16 15:14:03 +0200 | [diff] [blame] | 99 | 'bool': '>?', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 100 | 'header': '>HI'} |
| 101 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 102 | if elements > 0 and (type == 'u8' or type == 'string'): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 103 | self.packer = struct.Struct('>%ss' % elements) |
| 104 | else: |
| 105 | self.packer = struct.Struct(base_types[type]) |
| 106 | self.size = self.packer.size |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 107 | self.options = options |
| 108 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 109 | def pack(self, data, kwargs=None): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 110 | if data is None: # Default to zero if not specified |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 111 | if self.options and 'default' in self.options: |
| 112 | data = self.options['default'] |
| 113 | else: |
| 114 | data = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 115 | return self.packer.pack(data) |
| 116 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 117 | def unpack(self, data, offset, result=None, ntc=False): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 118 | return self.packer.unpack_from(data, offset)[0], self.packer.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 119 | |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 120 | @staticmethod |
| 121 | def _get_packer_with_options(f_type, options): |
| 122 | return BaseTypes(f_type, options=options) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 123 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 124 | def __repr__(self): |
| 125 | return "BaseTypes(type=%s, elements=%s, options=%s)" % (self._type, |
| 126 | self._elements, |
| 127 | self.options) |
| 128 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 129 | |
| 130 | class String(Packer): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 131 | def __init__(self, name, num, options): |
| 132 | self.name = name |
| 133 | self.num = num |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 134 | self.size = 1 |
| 135 | self.length_field_packer = BaseTypes('u32') |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 136 | self.limit = options['limit'] if 'limit' in options else num |
| 137 | self.fixed = True if num else False |
| 138 | if self.fixed and not self.limit: |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 139 | raise VPPSerializerValueError( |
Paul Vinciguerra | cebed41 | 2020-12-01 23:45:38 -0500 | [diff] [blame] | 140 | "Invalid combination for: {}, {} fixed:{} limit:{}". |
| 141 | format(name, options, self.fixed, self.limit)) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 142 | |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 143 | def pack(self, list, kwargs=None): |
| 144 | if not list: |
| 145 | if self.fixed: |
| 146 | return b"\x00" * self.limit |
| 147 | return self.length_field_packer.pack(0) + b"" |
| 148 | if self.limit and len(list) > self.limit - 1: |
| 149 | raise VPPSerializerValueError( |
| 150 | "Invalid argument length for: {}, {} maximum {}". |
| 151 | format(list, len(list), self.limit - 1)) |
| 152 | if self.fixed: |
| 153 | return list.encode('ascii').ljust(self.limit, b'\x00') |
| 154 | return self.length_field_packer.pack(len(list)) + list.encode('ascii') |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 155 | |
| 156 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 157 | if self.fixed: |
| 158 | p = BaseTypes('u8', self.num) |
| 159 | s = p.unpack(data, offset) |
| 160 | s2 = s[0].split(b'\0', 1)[0] |
| 161 | return (s2.decode('ascii'), self.num) |
| 162 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 163 | length, length_field_size = self.length_field_packer.unpack(data, |
| 164 | offset) |
| 165 | if length == 0: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 166 | return '', 0 |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 167 | p = BaseTypes('u8', length) |
| 168 | x, size = p.unpack(data, offset + length_field_size) |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 169 | return (x.decode('ascii', errors='replace'), size + length_field_size) |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 170 | |
| 171 | |
Paul Vinciguerra | ba2c7fa | 2019-11-23 21:02:27 -0500 | [diff] [blame] | 172 | types = {'u8': BaseTypes('u8'), 'i8': BaseTypes('i8'), |
| 173 | 'u16': BaseTypes('u16'), 'i16': BaseTypes('i16'), |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 174 | 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'), |
Paul Vinciguerra | ba2c7fa | 2019-11-23 21:02:27 -0500 | [diff] [blame] | 175 | 'u64': BaseTypes('u64'), 'i64': BaseTypes('i64'), |
| 176 | 'f64': BaseTypes('f64'), |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 177 | 'bool': BaseTypes('bool'), 'string': String} |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 178 | |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 179 | class_types = {} |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 180 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 181 | |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 182 | def vpp_get_type(name): |
| 183 | try: |
| 184 | return types[name] |
| 185 | except KeyError: |
| 186 | return None |
| 187 | |
| 188 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 189 | class VPPSerializerValueError(ValueError): |
| 190 | pass |
| 191 | |
| 192 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 193 | class FixedList_u8(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 194 | def __init__(self, name, field_type, num): |
| 195 | self.name = name |
| 196 | self.num = num |
| 197 | self.packer = BaseTypes(field_type, num) |
| 198 | self.size = self.packer.size |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 199 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 200 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 201 | def pack(self, data, kwargs=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 202 | """Packs a fixed length bytestring. Left-pads with zeros |
| 203 | if input data is too short.""" |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 204 | if not data: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 205 | return b'\x00' * self.size |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 206 | |
| 207 | if len(data) > self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 208 | raise VPPSerializerValueError( |
| 209 | 'Fixed list length error for "{}", got: {}' |
| 210 | ' expected: {}' |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 211 | .format(self.name, len(data), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 212 | |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 213 | try: |
| 214 | return self.packer.pack(data) |
| 215 | except struct.error: |
| 216 | raise VPPSerializerValueError( |
| 217 | 'Packing failed for "{}" {}' |
| 218 | .format(self.name, kwargs)) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 219 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 220 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 221 | if len(data[offset:]) < self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 222 | raise VPPSerializerValueError( |
| 223 | 'Invalid array length for "{}" got {}' |
| 224 | ' expected {}' |
| 225 | .format(self.name, len(data[offset:]), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 226 | return self.packer.unpack(data, offset) |
| 227 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 228 | def __repr__(self): |
| 229 | return "FixedList_u8(name=%s, field_type=%s, num=%s)" % ( |
| 230 | self.name, self.field_type, self.num |
| 231 | ) |
| 232 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 233 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 234 | class FixedList(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 235 | def __init__(self, name, field_type, num): |
| 236 | self.num = num |
| 237 | self.packer = types[field_type] |
| 238 | self.size = self.packer.size * num |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 239 | self.name = name |
| 240 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 241 | |
| 242 | def pack(self, list, kwargs): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 243 | if len(list) != self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 244 | raise VPPSerializerValueError( |
| 245 | 'Fixed list length error, got: {} expected: {}' |
| 246 | .format(len(list), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 247 | b = bytes() |
| 248 | for e in list: |
| 249 | b += self.packer.pack(e) |
| 250 | return b |
| 251 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 252 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 253 | # Return a list of arguments |
| 254 | result = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 255 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 256 | for e in range(self.num): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 257 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 258 | result.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 259 | offset += size |
| 260 | total += size |
| 261 | return result, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 262 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 263 | def __repr__(self): |
Paul Vinciguerra | 99fbf05 | 2019-11-26 14:15:53 -0500 | [diff] [blame] | 264 | return "FixedList(name=%s, field_type=%s, num=%s)" % ( |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 265 | self.name, self.field_type, self.num) |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 266 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 267 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 268 | class VLAList(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 269 | def __init__(self, name, field_type, len_field_name, index): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 270 | self.name = name |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 271 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 272 | self.index = index |
| 273 | self.packer = types[field_type] |
| 274 | self.size = self.packer.size |
| 275 | self.length_field = len_field_name |
| 276 | |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 277 | def pack(self, lst, kwargs=None): |
| 278 | if not lst: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 279 | return b"" |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 280 | if len(lst) != kwargs[self.length_field]: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 281 | raise VPPSerializerValueError( |
| 282 | 'Variable length error, got: {} expected: {}' |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 283 | .format(len(lst), kwargs[self.length_field])) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 284 | |
| 285 | # u8 array |
| 286 | if self.packer.size == 1: |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 287 | if isinstance(lst, list): |
| 288 | return b''.join(lst) |
| 289 | return bytes(lst) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 290 | |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 291 | b = bytes() |
| 292 | for e in lst: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 293 | b += self.packer.pack(e) |
| 294 | return b |
| 295 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 296 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 297 | # Return a list of arguments |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 298 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 299 | |
| 300 | # u8 array |
| 301 | if self.packer.size == 1: |
| 302 | if result[self.index] == 0: |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 303 | return b'', 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 304 | p = BaseTypes('u8', result[self.index]) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 305 | return p.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 306 | |
| 307 | r = [] |
| 308 | for e in range(result[self.index]): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 309 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 310 | r.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 311 | offset += size |
| 312 | total += size |
| 313 | return r, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 314 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 315 | def __repr__(self): |
| 316 | return "VLAList(name=%s, field_type=%s, " \ |
| 317 | "len_field_name=%s, index=%s)" % ( |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 318 | self.name, self.field_type, self.length_field, self.index |
| 319 | ) |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 320 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 321 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 322 | class VLAList_legacy(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 323 | def __init__(self, name, field_type): |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 324 | self.name = name |
| 325 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 326 | self.packer = types[field_type] |
| 327 | self.size = self.packer.size |
| 328 | |
| 329 | def pack(self, list, kwargs=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 330 | if self.packer.size == 1: |
| 331 | return bytes(list) |
| 332 | |
| 333 | b = bytes() |
| 334 | for e in list: |
| 335 | b += self.packer.pack(e) |
| 336 | return b |
| 337 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 338 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 339 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 340 | # Return a list of arguments |
| 341 | if (len(data) - offset) % self.packer.size: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 342 | raise VPPSerializerValueError( |
| 343 | 'Legacy Variable Length Array length mismatch.') |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 344 | elements = int((len(data) - offset) / self.packer.size) |
| 345 | r = [] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 346 | for e in range(elements): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 347 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 348 | r.append(x) |
| 349 | offset += self.packer.size |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 350 | total += size |
| 351 | return r, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 352 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 353 | def __repr__(self): |
| 354 | return "VLAList_legacy(name=%s, field_type=%s)" % ( |
| 355 | self.name, self.field_type |
| 356 | ) |
| 357 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 358 | |
Paul Vinciguerra | 3825d93 | 2020-12-03 21:06:28 -0500 | [diff] [blame] | 359 | # Will change to IntEnum after 21.04 release |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 360 | class VPPEnumType(Packer): |
Paul Vinciguerra | 3825d93 | 2020-12-03 21:06:28 -0500 | [diff] [blame] | 361 | output_class = IntFlag |
| 362 | |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 363 | def __init__(self, name, msgdef, options=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 364 | self.size = types['u32'].size |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 365 | self.name = name |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 366 | self.enumtype = 'u32' |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 367 | self.msgdef = msgdef |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 368 | e_hash = {} |
| 369 | for f in msgdef: |
| 370 | if type(f) is dict and 'enumtype' in f: |
| 371 | if f['enumtype'] != 'u32': |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 372 | self.size = types[f['enumtype']].size |
| 373 | self.enumtype = f['enumtype'] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 374 | continue |
| 375 | ename, evalue = f |
| 376 | e_hash[ename] = evalue |
Paul Vinciguerra | 3825d93 | 2020-12-03 21:06:28 -0500 | [diff] [blame] | 377 | self.enum = self.output_class(name, e_hash) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 378 | types[name] = self |
Paul Vinciguerra | 3825d93 | 2020-12-03 21:06:28 -0500 | [diff] [blame] | 379 | class_types[name] = self.__class__ |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 380 | self.options = options |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 381 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 382 | def __getattr__(self, name): |
| 383 | return self.enum[name] |
| 384 | |
Paul Vinciguerra | a089ae1 | 2019-07-08 15:41:07 -0400 | [diff] [blame] | 385 | def __bool__(self): |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 386 | return True |
| 387 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 388 | def pack(self, data, kwargs=None): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 389 | if data is None: # Default to zero if not specified |
| 390 | if self.options and 'default' in self.options: |
| 391 | data = self.options['default'] |
| 392 | else: |
| 393 | data = 0 |
| 394 | |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 395 | return types[self.enumtype].pack(data) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 396 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 397 | def unpack(self, data, offset=0, result=None, ntc=False): |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 398 | x, size = types[self.enumtype].unpack(data, offset) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 399 | return self.enum(x), size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 400 | |
Paul Vinciguerra | 3825d93 | 2020-12-03 21:06:28 -0500 | [diff] [blame] | 401 | @classmethod |
| 402 | def _get_packer_with_options(cls, f_type, options): |
| 403 | return cls(f_type, types[f_type].msgdef, options=options) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 404 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 405 | def __repr__(self): |
Paul Vinciguerra | 3825d93 | 2020-12-03 21:06:28 -0500 | [diff] [blame] | 406 | return "%s(name=%s, msgdef=%s, options=%s)" % ( |
| 407 | self.__class__.__name__, self.name, self.msgdef, self.options |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 408 | ) |
| 409 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 410 | |
Paul Vinciguerra | 3825d93 | 2020-12-03 21:06:28 -0500 | [diff] [blame] | 411 | class VPPEnumFlagType(VPPEnumType): |
| 412 | output_class = IntFlag |
| 413 | |
| 414 | def __init__(self, name, msgdef, options=None): |
| 415 | super(VPPEnumFlagType, self).__init__(name, msgdef, options) |
| 416 | |
| 417 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 418 | class VPPUnionType(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 419 | def __init__(self, name, msgdef): |
| 420 | self.name = name |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 421 | self.msgdef = msgdef |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 422 | self.size = 0 |
| 423 | self.maxindex = 0 |
| 424 | fields = [] |
| 425 | self.packers = collections.OrderedDict() |
| 426 | for i, f in enumerate(msgdef): |
| 427 | if type(f) is dict and 'crc' in f: |
| 428 | self.crc = f['crc'] |
| 429 | continue |
| 430 | f_type, f_name = f |
| 431 | if f_type not in types: |
| 432 | logger.debug('Unknown union type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 433 | raise VPPSerializerValueError( |
| 434 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 435 | fields.append(f_name) |
| 436 | size = types[f_type].size |
| 437 | self.packers[f_name] = types[f_type] |
| 438 | if size > self.size: |
| 439 | self.size = size |
| 440 | self.maxindex = i |
| 441 | |
| 442 | types[name] = self |
| 443 | self.tuple = collections.namedtuple(name, fields, rename=True) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 444 | |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 445 | # Union of variable length? |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 446 | def pack(self, data, kwargs=None): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 447 | if not data: |
| 448 | return b'\x00' * self.size |
| 449 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 450 | for k, v in data.items(): |
| 451 | logger.debug("Key: {} Value: {}".format(k, v)) |
| 452 | b = self.packers[k].pack(v, kwargs) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 453 | break |
| 454 | r = bytearray(self.size) |
Ole Troan | b199e98 | 2018-08-02 19:19:21 +0200 | [diff] [blame] | 455 | r[:len(b)] = b |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 456 | return r |
| 457 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 458 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 459 | r = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 460 | maxsize = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 461 | for k, p in self.packers.items(): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 462 | x, size = p.unpack(data, offset, ntc=ntc) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 463 | if size > maxsize: |
| 464 | maxsize = size |
| 465 | r.append(x) |
| 466 | return self.tuple._make(r), maxsize |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 467 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 468 | def __repr__(self): |
| 469 | return"VPPUnionType(name=%s, msgdef=%r)" % (self.name, self.msgdef) |
| 470 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 471 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 472 | class VPPTypeAlias(Packer): |
| 473 | def __init__(self, name, msgdef, options=None): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 474 | self.name = name |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 475 | self.msgdef = msgdef |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 476 | t = vpp_get_type(msgdef['type']) |
| 477 | if not t: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 478 | raise ValueError('No such type: {}'.format(msgdef['type'])) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 479 | if 'length' in msgdef: |
| 480 | if msgdef['length'] == 0: |
| 481 | raise ValueError() |
| 482 | if msgdef['type'] == 'u8': |
| 483 | self.packer = FixedList_u8(name, msgdef['type'], |
| 484 | msgdef['length']) |
| 485 | self.size = self.packer.size |
| 486 | else: |
| 487 | self.packer = FixedList(name, msgdef['type'], msgdef['length']) |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 488 | else: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 489 | self.packer = t |
| 490 | self.size = t.size |
| 491 | |
| 492 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 493 | self.toplevelconversion = False |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 494 | self.options = options |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 495 | |
| 496 | def pack(self, data, kwargs=None): |
| 497 | if data and conversion_required(data, self.name): |
| 498 | try: |
| 499 | return conversion_packer(data, self.name) |
| 500 | # Python 2 and 3 raises different exceptions from inet_pton |
| 501 | except(OSError, socket.error, TypeError): |
| 502 | pass |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 503 | if data is None: # Default to zero if not specified |
| 504 | if self.options and 'default' in self.options: |
| 505 | data = self.options['default'] |
| 506 | else: |
| 507 | data = 0 |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 508 | |
| 509 | return self.packer.pack(data, kwargs) |
| 510 | |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 511 | @staticmethod |
| 512 | def _get_packer_with_options(f_type, options): |
| 513 | return VPPTypeAlias(f_type, types[f_type].msgdef, options=options) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 514 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 515 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 516 | if ntc is False and self.name in vpp_format.conversion_unpacker_table: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 517 | # Disable type conversion for dependent types |
| 518 | ntc = True |
| 519 | self.toplevelconversion = True |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 520 | t, size = self.packer.unpack(data, offset, result, ntc=ntc) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 521 | if self.toplevelconversion: |
| 522 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 523 | return conversion_unpacker(t, self.name), size |
| 524 | return t, size |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 525 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 526 | def __repr__(self): |
| 527 | return "VPPTypeAlias(name=%s, msgdef=%s, options=%s)" % ( |
| 528 | self.name, self.msgdef, self.options) |
| 529 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 530 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 531 | class VPPType(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 532 | # Set everything up to be able to pack / unpack |
| 533 | def __init__(self, name, msgdef): |
| 534 | self.name = name |
| 535 | self.msgdef = msgdef |
| 536 | self.packers = [] |
| 537 | self.fields = [] |
| 538 | self.fieldtypes = [] |
| 539 | self.field_by_name = {} |
| 540 | size = 0 |
| 541 | for i, f in enumerate(msgdef): |
| 542 | if type(f) is dict and 'crc' in f: |
| 543 | self.crc = f['crc'] |
| 544 | continue |
| 545 | f_type, f_name = f[:2] |
| 546 | self.fields.append(f_name) |
| 547 | self.field_by_name[f_name] = None |
| 548 | self.fieldtypes.append(f_type) |
| 549 | if f_type not in types: |
| 550 | logger.debug('Unknown type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 551 | raise VPPSerializerValueError( |
| 552 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 553 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 554 | fieldlen = len(f) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 555 | options = [x for x in f if type(x) is dict] |
| 556 | if len(options): |
| 557 | self.options = options[0] |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 558 | fieldlen -= 1 |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 559 | else: |
| 560 | self.options = {} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 561 | if fieldlen == 3: # list |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 562 | list_elements = f[2] |
| 563 | if list_elements == 0: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 564 | if f_type == 'string': |
| 565 | p = String(f_name, 0, self.options) |
| 566 | else: |
| 567 | p = VLAList_legacy(f_name, f_type) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 568 | self.packers.append(p) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 569 | elif f_type == 'u8': |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 570 | p = FixedList_u8(f_name, f_type, list_elements) |
| 571 | self.packers.append(p) |
| 572 | size += p.size |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 573 | elif f_type == 'string': |
| 574 | p = String(f_name, list_elements, self.options) |
| 575 | self.packers.append(p) |
| 576 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 577 | else: |
| 578 | p = FixedList(f_name, f_type, list_elements) |
| 579 | self.packers.append(p) |
| 580 | size += p.size |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 581 | elif fieldlen == 4: # Variable length list |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 582 | length_index = self.fields.index(f[3]) |
| 583 | p = VLAList(f_name, f_type, f[3], length_index) |
| 584 | self.packers.append(p) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 585 | else: |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 586 | # default support for types that decay to basetype |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 587 | if 'default' in self.options: |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 588 | p = self.get_packer_with_options(f_type, self.options) |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 589 | else: |
| 590 | p = types[f_type] |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 591 | |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 592 | self.packers.append(p) |
| 593 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 594 | |
| 595 | self.size = size |
| 596 | self.tuple = collections.namedtuple(name, self.fields, rename=True) |
| 597 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 598 | self.toplevelconversion = False |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 599 | |
| 600 | def pack(self, data, kwargs=None): |
| 601 | if not kwargs: |
| 602 | kwargs = data |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 603 | b = bytes() |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 604 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 605 | # Try one of the format functions |
| 606 | if data and conversion_required(data, self.name): |
| 607 | return conversion_packer(data, self.name) |
| 608 | |
| 609 | for i, a in enumerate(self.fields): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 610 | if data and type(data) is not dict and a not in data: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 611 | raise VPPSerializerValueError( |
| 612 | "Invalid argument: {} expected {}.{}". |
| 613 | format(data, self.name, a)) |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 614 | |
| 615 | # Defaulting to zero. |
| 616 | if not data or a not in data: # Default to 0 |
| 617 | arg = None |
| 618 | kwarg = None # No default for VLA |
| 619 | else: |
| 620 | arg = data[a] |
| 621 | kwarg = kwargs[a] if a in kwargs else None |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 622 | if isinstance(self.packers[i], VPPType): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 623 | b += self.packers[i].pack(arg, kwarg) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 624 | else: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 625 | b += self.packers[i].pack(arg, kwargs) |
| 626 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 627 | return b |
| 628 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 629 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 630 | # Return a list of arguments |
| 631 | result = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 632 | total = 0 |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 633 | if ntc is False and self.name in vpp_format.conversion_unpacker_table: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 634 | # Disable type conversion for dependent types |
| 635 | ntc = True |
| 636 | self.toplevelconversion = True |
| 637 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 638 | for p in self.packers: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 639 | x, size = p.unpack(data, offset, result, ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 640 | if type(x) is tuple and len(x) == 1: |
| 641 | x = x[0] |
| 642 | result.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 643 | offset += size |
| 644 | total += size |
| 645 | t = self.tuple._make(result) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 646 | |
| 647 | if self.toplevelconversion: |
| 648 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 649 | t = conversion_unpacker(t, self.name) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 650 | return t, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 651 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 652 | def __repr__(self): |
| 653 | return "%s(name=%s, msgdef=%s)" % ( |
| 654 | self.__class__.__name__, self.name, self.msgdef |
| 655 | ) |
| 656 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 657 | |
| 658 | class VPPMessage(VPPType): |
| 659 | pass |