- def status(self):
- """Debug function: report current VPP API status to stdout."""
- print('Connected') if self.connected else print('Not Connected')
- print('Read API definitions from', ', '.join(self.apifiles))
-
- def __struct (self, t, n = None, e = -1, vl = None):
- """Create a packing structure for a message."""
- base_types = { 'u8' : 'B',
- 'u16' : 'H',
- 'u32' : 'I',
- 'i32' : 'i',
- 'u64' : 'Q',
- 'f64' : 'd',
- }
- pack = None
- if t in base_types:
- pack = base_types[t]
- if not vl:
- if e > 0 and t == 'u8':
- # Fixed byte array
- return struct.Struct('>' + str(e) + 's')
- if e > 0:
- # Fixed array of base type
- return [e, struct.Struct('>' + base_types[t])]
- elif e == 0:
- # Old style variable array
- return [-1, struct.Struct('>' + base_types[t])]
- else:
- # Variable length array
- return [vl, struct.Struct('>s')] if t == 'u8' else \
- [vl, struct.Struct('>' + base_types[t])]
-
- return struct.Struct('>' + base_types[t])
-
- if t in self.messages:
- ### Return a list in case of array ###
- if e > 0 and not vl:
- return [e, lambda self, encode, buf, offset, args: (
- self.__struct_type(encode, self.messages[t], buf, offset,
- args))]
- if vl:
- return [vl, lambda self, encode, buf, offset, args: (
- self.__struct_type(encode, self.messages[t], buf, offset,
- args))]
- elif e == 0:
- # Old style VLA
- raise NotImplementedError(1, 'No support for compound types ' + t)
- return lambda self, encode, buf, offset, args: (
- self.__struct_type(encode, self.messages[t], buf, offset, args)
- )
-
- raise ValueError(1, 'Invalid message type: ' + t)
-
- def __struct_type(self, encode, msgdef, buf, offset, kwargs):
- """Get a message packer or unpacker."""
- if encode:
- return self.__struct_type_encode(msgdef, buf, offset, kwargs)
- else:
- return self.__struct_type_decode(msgdef, buf, offset)
-
- def __struct_type_encode(self, msgdef, buf, offset, kwargs):
- off = offset
- size = 0
-
- for k in kwargs:
- if k not in msgdef['args']:
- raise ValueError(1, 'Invalid field-name in message call ' + k)
-
- for k,v in msgdef['args'].iteritems():
- off += size
- if k in kwargs:
- if type(v) is list:
- if callable(v[1]):
- e = kwargs[v[0]] if v[0] in kwargs else v[0]
- size = 0
- for i in range(e):
- size += v[1](self, True, buf, off + size,
- kwargs[k][i])
- else:
- if v[0] in kwargs:
- l = kwargs[v[0]]
- else:
- l = len(kwargs[k])
- if v[1].size == 1:
- buf[off:off + l] = bytearray(kwargs[k])
- size = l
- else:
- size = 0
- for i in kwargs[k]:
- v[1].pack_into(buf, off + size, i)
- size += v[1].size
- else:
- if callable(v):
- size = v(self, True, buf, off, kwargs[k])
- else:
- v.pack_into(buf, off, kwargs[k])
- size = v.size
- else:
- size = v.size if not type(v) is list else 0
-
- return off + size - offset