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 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 359 | class VPPEnumType(Packer): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 360 | def __init__(self, name, msgdef, options=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 361 | self.size = types['u32'].size |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 362 | self.name = name |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 363 | self.enumtype = 'u32' |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 364 | self.msgdef = msgdef |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 365 | e_hash = {} |
| 366 | for f in msgdef: |
| 367 | if type(f) is dict and 'enumtype' in f: |
| 368 | if f['enumtype'] != 'u32': |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 369 | self.size = types[f['enumtype']].size |
| 370 | self.enumtype = f['enumtype'] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 371 | continue |
| 372 | ename, evalue = f |
| 373 | e_hash[ename] = evalue |
Ole Troan | 48ae19e | 2019-03-07 11:28:32 +0100 | [diff] [blame] | 374 | self.enum = IntFlag(name, e_hash) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 375 | types[name] = self |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 376 | class_types[name] = VPPEnumType |
| 377 | self.options = options |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 378 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 379 | def __getattr__(self, name): |
| 380 | return self.enum[name] |
| 381 | |
Paul Vinciguerra | a089ae1 | 2019-07-08 15:41:07 -0400 | [diff] [blame] | 382 | def __bool__(self): |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 383 | return True |
| 384 | |
Paul Vinciguerra | a089ae1 | 2019-07-08 15:41:07 -0400 | [diff] [blame] | 385 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 386 | def pack(self, data, kwargs=None): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 387 | if data is None: # Default to zero if not specified |
| 388 | if self.options and 'default' in self.options: |
| 389 | data = self.options['default'] |
| 390 | else: |
| 391 | data = 0 |
| 392 | |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 393 | return types[self.enumtype].pack(data) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 394 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 395 | def unpack(self, data, offset=0, result=None, ntc=False): |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 396 | x, size = types[self.enumtype].unpack(data, offset) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 397 | return self.enum(x), size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 398 | |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 399 | @staticmethod |
| 400 | def _get_packer_with_options(f_type, options): |
| 401 | return VPPEnumType(f_type, types[f_type].msgdef, options=options) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 402 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 403 | def __repr__(self): |
| 404 | return "VPPEnumType(name=%s, msgdef=%s, options=%s)" % ( |
| 405 | self.name, self.msgdef, self.options |
| 406 | ) |
| 407 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 408 | |
| 409 | class VPPUnionType(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 410 | def __init__(self, name, msgdef): |
| 411 | self.name = name |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 412 | self.msgdef = msgdef |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 413 | self.size = 0 |
| 414 | self.maxindex = 0 |
| 415 | fields = [] |
| 416 | self.packers = collections.OrderedDict() |
| 417 | for i, f in enumerate(msgdef): |
| 418 | if type(f) is dict and 'crc' in f: |
| 419 | self.crc = f['crc'] |
| 420 | continue |
| 421 | f_type, f_name = f |
| 422 | if f_type not in types: |
| 423 | logger.debug('Unknown union type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 424 | raise VPPSerializerValueError( |
| 425 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 426 | fields.append(f_name) |
| 427 | size = types[f_type].size |
| 428 | self.packers[f_name] = types[f_type] |
| 429 | if size > self.size: |
| 430 | self.size = size |
| 431 | self.maxindex = i |
| 432 | |
| 433 | types[name] = self |
| 434 | self.tuple = collections.namedtuple(name, fields, rename=True) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 435 | |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 436 | # Union of variable length? |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 437 | def pack(self, data, kwargs=None): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 438 | if not data: |
| 439 | return b'\x00' * self.size |
| 440 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 441 | for k, v in data.items(): |
| 442 | logger.debug("Key: {} Value: {}".format(k, v)) |
| 443 | b = self.packers[k].pack(v, kwargs) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 444 | break |
| 445 | r = bytearray(self.size) |
Ole Troan | b199e98 | 2018-08-02 19:19:21 +0200 | [diff] [blame] | 446 | r[:len(b)] = b |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 447 | return r |
| 448 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 449 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 450 | r = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 451 | maxsize = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 452 | for k, p in self.packers.items(): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 453 | x, size = p.unpack(data, offset, ntc=ntc) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 454 | if size > maxsize: |
| 455 | maxsize = size |
| 456 | r.append(x) |
| 457 | return self.tuple._make(r), maxsize |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 458 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 459 | def __repr__(self): |
| 460 | return"VPPUnionType(name=%s, msgdef=%r)" % (self.name, self.msgdef) |
| 461 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 462 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 463 | class VPPTypeAlias(Packer): |
| 464 | def __init__(self, name, msgdef, options=None): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 465 | self.name = name |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 466 | self.msgdef = msgdef |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 467 | t = vpp_get_type(msgdef['type']) |
| 468 | if not t: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 469 | raise ValueError('No such type: {}'.format(msgdef['type'])) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 470 | if 'length' in msgdef: |
| 471 | if msgdef['length'] == 0: |
| 472 | raise ValueError() |
| 473 | if msgdef['type'] == 'u8': |
| 474 | self.packer = FixedList_u8(name, msgdef['type'], |
| 475 | msgdef['length']) |
| 476 | self.size = self.packer.size |
| 477 | else: |
| 478 | self.packer = FixedList(name, msgdef['type'], msgdef['length']) |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 479 | else: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 480 | self.packer = t |
| 481 | self.size = t.size |
| 482 | |
| 483 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 484 | self.toplevelconversion = False |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 485 | self.options = options |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 486 | |
| 487 | def pack(self, data, kwargs=None): |
| 488 | if data and conversion_required(data, self.name): |
| 489 | try: |
| 490 | return conversion_packer(data, self.name) |
| 491 | # Python 2 and 3 raises different exceptions from inet_pton |
| 492 | except(OSError, socket.error, TypeError): |
| 493 | pass |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 494 | if data is None: # Default to zero if not specified |
| 495 | if self.options and 'default' in self.options: |
| 496 | data = self.options['default'] |
| 497 | else: |
| 498 | data = 0 |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 499 | |
| 500 | return self.packer.pack(data, kwargs) |
| 501 | |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 502 | @staticmethod |
| 503 | def _get_packer_with_options(f_type, options): |
| 504 | return VPPTypeAlias(f_type, types[f_type].msgdef, options=options) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 505 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 506 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 507 | 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] | 508 | # Disable type conversion for dependent types |
| 509 | ntc = True |
| 510 | self.toplevelconversion = True |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 511 | t, size = self.packer.unpack(data, offset, result, ntc=ntc) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 512 | if self.toplevelconversion: |
| 513 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 514 | return conversion_unpacker(t, self.name), size |
| 515 | return t, size |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 516 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 517 | def __repr__(self): |
| 518 | return "VPPTypeAlias(name=%s, msgdef=%s, options=%s)" % ( |
| 519 | self.name, self.msgdef, self.options) |
| 520 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 521 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 522 | class VPPType(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 523 | # Set everything up to be able to pack / unpack |
| 524 | def __init__(self, name, msgdef): |
| 525 | self.name = name |
| 526 | self.msgdef = msgdef |
| 527 | self.packers = [] |
| 528 | self.fields = [] |
| 529 | self.fieldtypes = [] |
| 530 | self.field_by_name = {} |
| 531 | size = 0 |
| 532 | for i, f in enumerate(msgdef): |
| 533 | if type(f) is dict and 'crc' in f: |
| 534 | self.crc = f['crc'] |
| 535 | continue |
| 536 | f_type, f_name = f[:2] |
| 537 | self.fields.append(f_name) |
| 538 | self.field_by_name[f_name] = None |
| 539 | self.fieldtypes.append(f_type) |
| 540 | if f_type not in types: |
| 541 | logger.debug('Unknown type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 542 | raise VPPSerializerValueError( |
| 543 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 544 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 545 | fieldlen = len(f) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 546 | options = [x for x in f if type(x) is dict] |
| 547 | if len(options): |
| 548 | self.options = options[0] |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 549 | fieldlen -= 1 |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 550 | else: |
| 551 | self.options = {} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 552 | if fieldlen == 3: # list |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 553 | list_elements = f[2] |
| 554 | if list_elements == 0: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 555 | if f_type == 'string': |
| 556 | p = String(f_name, 0, self.options) |
| 557 | else: |
| 558 | p = VLAList_legacy(f_name, f_type) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 559 | self.packers.append(p) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 560 | elif f_type == 'u8': |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 561 | p = FixedList_u8(f_name, f_type, list_elements) |
| 562 | self.packers.append(p) |
| 563 | size += p.size |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 564 | elif f_type == 'string': |
| 565 | p = String(f_name, list_elements, self.options) |
| 566 | self.packers.append(p) |
| 567 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 568 | else: |
| 569 | p = FixedList(f_name, f_type, list_elements) |
| 570 | self.packers.append(p) |
| 571 | size += p.size |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 572 | elif fieldlen == 4: # Variable length list |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 573 | length_index = self.fields.index(f[3]) |
| 574 | p = VLAList(f_name, f_type, f[3], length_index) |
| 575 | self.packers.append(p) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 576 | else: |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 577 | # default support for types that decay to basetype |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 578 | if 'default' in self.options: |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 579 | p = self.get_packer_with_options(f_type, self.options) |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 580 | else: |
| 581 | p = types[f_type] |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 582 | |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 583 | self.packers.append(p) |
| 584 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 585 | |
| 586 | self.size = size |
| 587 | self.tuple = collections.namedtuple(name, self.fields, rename=True) |
| 588 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 589 | self.toplevelconversion = False |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 590 | |
| 591 | def pack(self, data, kwargs=None): |
| 592 | if not kwargs: |
| 593 | kwargs = data |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 594 | b = bytes() |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 595 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 596 | # Try one of the format functions |
| 597 | if data and conversion_required(data, self.name): |
| 598 | return conversion_packer(data, self.name) |
| 599 | |
| 600 | for i, a in enumerate(self.fields): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 601 | 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] | 602 | raise VPPSerializerValueError( |
| 603 | "Invalid argument: {} expected {}.{}". |
| 604 | format(data, self.name, a)) |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 605 | |
| 606 | # Defaulting to zero. |
| 607 | if not data or a not in data: # Default to 0 |
| 608 | arg = None |
| 609 | kwarg = None # No default for VLA |
| 610 | else: |
| 611 | arg = data[a] |
| 612 | kwarg = kwargs[a] if a in kwargs else None |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 613 | if isinstance(self.packers[i], VPPType): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 614 | b += self.packers[i].pack(arg, kwarg) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 615 | else: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 616 | b += self.packers[i].pack(arg, kwargs) |
| 617 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 618 | return b |
| 619 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 620 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 621 | # Return a list of arguments |
| 622 | result = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 623 | total = 0 |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 624 | 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] | 625 | # Disable type conversion for dependent types |
| 626 | ntc = True |
| 627 | self.toplevelconversion = True |
| 628 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 629 | for p in self.packers: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 630 | x, size = p.unpack(data, offset, result, ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 631 | if type(x) is tuple and len(x) == 1: |
| 632 | x = x[0] |
| 633 | result.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 634 | offset += size |
| 635 | total += size |
| 636 | t = self.tuple._make(result) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 637 | |
| 638 | if self.toplevelconversion: |
| 639 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 640 | t = conversion_unpacker(t, self.name) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 641 | return t, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 642 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 643 | def __repr__(self): |
| 644 | return "%s(name=%s, msgdef=%s)" % ( |
| 645 | self.__class__.__name__, self.name, self.msgdef |
| 646 | ) |
| 647 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 648 | |
| 649 | class VPPMessage(VPPType): |
| 650 | pass |