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