Beispiel #1
0
    def test_to_bytes(self):
        vals = {'buf': self.MAGIC}

        with self.subTest('with no length constraints'):
            field = codec.Buf('buf')  # default: len=0
            self.assertEqual(field.to_bytes(vals), self.MAGIC)

        with self.subTest('with length constraints'):
            field = codec.Buf('buf', len=len(self.MAGIC))
            self.assertEqual(field.to_bytes(vals), self.MAGIC)

        with self.subTest('EncodeError due to length mismatch'):
            field = codec.Buf('buf', len=4)
            with self.assertRaises(codec.EncodeError):
                field.to_bytes(vals)
Beispiel #2
0
class PDUv0Rx(codec.Envelope):
    STRUCT = (
        Header(ver=0),
        codec.Uint32BE('fn'),
        codec.Uint('rssi', mult=-1),
        codec.Int16BE('toa256'),
        codec.Buf('soft-bits'),
        codec.Buf('pad'),  # Optional
    )

    def __init__(self, *args, **kw):
        codec.Envelope.__init__(self, *args, **kw)

        # Field 'soft-bits' is either 148 (GMSK) or 444 (8-PSK) octets long
        self.STRUCT[-2].get_len = lambda _, data: 444 if len(data
                                                             ) > 148 else 148
Beispiel #3
0
class PDUv0Tx(codec.Envelope):
    STRUCT = (
        Header(ver=0),
        codec.Uint32BE('fn'),
        codec.Uint('pwr'),
        codec.Buf('hard-bits'),
    )
Beispiel #4
0
    def test_from_bytes(self):
        vals = {}

        with self.subTest('with no length constraints'):
            field = codec.Buf('buf')  # default: len=0
            offset = field.from_bytes(vals, self.MAGIC)
            self.assertEqual(vals['buf'], self.MAGIC)
            self.assertEqual(offset, len(self.MAGIC))

        with self.subTest('with length constraints'):
            field = codec.Buf('buf', len=2)
            offset = field.from_bytes(vals, self.MAGIC)
            self.assertEqual(vals['buf'], self.MAGIC[:2])
            self.assertEqual(offset, len(self.MAGIC[:2]))

        with self.subTest('DecodeError due to not enough bytes'):
            field = codec.Buf('buf', len=64)
            with self.assertRaises(codec.DecodeError):
                field.from_bytes(vals, self.MAGIC)
Beispiel #5
0
    class TLV(codec.Envelope):
        STRUCT = (
            codec.Uint('T'),
            codec.Uint('L'),
            codec.Buf('V'),
        )

        def __init__(self, *args, **kw) -> None:
            codec.Envelope.__init__(self, *args, **kw)
            self.STRUCT[-2].get_val = lambda v: len(v['V'])
            self.STRUCT[-1].get_len = lambda v, _: v['L']
Beispiel #6
0
class TestPDU(codec.Envelope):
	STRUCT = (
		codec.BitFieldSet(len=2, set=(
			codec.BitField('ver', bl=4),
			codec.BitField('flag', bl=1),
		)),
		codec.Uint16BE('len'),
		codec.Buf('data'),
		codec.Buf('tail', len=2),
	)

	def __init__(self, *args, **kw):
		codec.Envelope.__init__(self, *args, **kw)
		self.STRUCT[-3].get_val = lambda v: len(v['data'])
		self.STRUCT[-2].get_len = lambda v, _: v['len']
		self.STRUCT[-1].get_pres = lambda v: bool(v['flag'])

	def check(self, vals: dict) -> None:
		if not vals['ver'] in (0, 1, 2):
			raise ValueError('Unknown version %d' % vals['ver'])