Beispiel #1
0
    def test_chars_too_long(self):
        u = Fields()
        u.add(CH(6, 'string'))
        u.string = '1234567'  # One byte too long

        with pytest.raises(ValueError):
            u.pack()
Beispiel #2
0
    def test_chars_padding_added(self):
        u = Fields()
        u.add(CH(6, 'string'))
        u.string = '1234'

        data = u.pack()
        assert data == bytearray.fromhex('31 32 33 34 00 00')
Beispiel #3
0
    def test_basic_pack(self):
        u = Fields()
        u.add(CH(4, 'string'))
        u.string = '1234'

        data = u.pack()
        assert data == bytearray.fromhex("31 32 33 34")
Beispiel #4
0
    def test_pack2(self):
        u = Fields()
        u.add(I4('test'))
        u.add(U1('val'))
        u.add(Padding(2, 'res1'))

        u.test = 0x76543210
        u.val = 0xAF

        data = u.pack()
        assert data == bytearray.fromhex('10 32 54 76 AF 00 00')
Beispiel #5
0
    def test_pack1(self):
        u = Fields()
        u.add(I4('test'))
        u.add(U1('val'))
        u.add(Padding(3, 'res1'))
        u.add(U4('test2'))

        u.test = 0x76543210
        u.val = 0xAF
        u.test2 = 0xcafebabe

        data = u.pack()
        print(data)
        assert data == bytearray.fromhex('10 32 54 76 AF 00 00 00 be ba fe ca')
Beispiel #6
0
class UbxFrame(object):
    CID = UbxCID(0, 0)
    NAME = 'UBX'

    SYNC_1 = 0xb5
    SYNC_2 = 0x62

    @classmethod
    def construct(cls, data):
        obj = cls()
        obj.data = data
        obj.unpack()
        return obj

    @classmethod
    def MATCHES(cls, cid):
        return cls.CID == cid

    def __init__(self):
        super().__init__()
        self.data = bytearray()
        # TODO: Do we need checksum as member?
        self.checksum = Checksum()
        self.f = Fields()

    def to_bytes(self):
        self._calc_checksum()

        msg = bytearray([UbxFrame.SYNC_1, UbxFrame.SYNC_2])
        msg.append(self.CID.cls)
        msg.append(self.CID.id)

        length = len(self.data)
        msg.append((length >> 0) % 0xFF)
        msg.append((length >> 8) % 0xFF)

        msg += self.data
        msg.append(self.cka)
        msg.append(self.ckb)

        return msg

    def get(self, name):
        return self.f.get(name)

    def pack(self):
        self.data = self.f.pack()

    def unpack(self):
        self.f.unpack(self.data)

    def _calc_checksum(self):
        self.checksum.reset()

        self.checksum.add(self.CID.cls)
        self.checksum.add(self.CID.id)

        length = len(self.data)
        self.checksum.add((length >> 0) & 0xFF)
        self.checksum.add((length >> 8) & 0xFF)

        for d in self.data:
            self.checksum.add(d)

        self.cka, self.ckb = self.checksum.value()

    def __str__(self):
        res = f'{self.NAME} {self.CID}'
        res += str(self.f)
        return res