def test_not_equal(self): c1 = UbxCID(0x05, 0x00) c2 = UbxCID(0x05, 0x01) assert c1 != c2 c1 = UbxCID(0x05, 0x01) c2 = UbxCID(0x06, 0x01) assert c1 != c2
def test_immutable(self): c1 = UbxCID(0x05, 0x01) with pytest.raises(AttributeError): c1.cls = 0x06 with pytest.raises(AttributeError): c1.id = 0x44 res = str(c1) assert res == 'cls:05 id:01'
def test_change_filter(self): uut = UbxParser(UbxCID(0x00, 0x02)) uut.set_filter(UbxCID(0x13, 0x40)) uut.process(self.FRAME_1) cid, packet = uut.packet() assert cid == UbxCID(0x13, 0x40) uut.set_filter(UbxCID(0x00, 0x00)) uut.process(self.FRAME_1) cid, packet = uut.packet() assert cid is None and packet is None
def test_cinvalid_length(self): frame = [ 0xB5, 0x62, 0x13, 0x40, 0xe9, 0x03, 0x10, 0x00, 0x00, 0x12, 0xE4, 0x07, 0x09, 0x05, 0x06, 0x28, 0x30, 0x00, 0x40, 0x28, 0xEF, 0x0C, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0xAC ] uut = UbxParser(UbxCID(0x00, 0x02)) uut.set_filter(UbxCID(0x13, 0x41)) uut.process(frame) # CRC packet cid, packet = uut.packet( ) # Should be None because frame is too long (MAX_MESSAGE_LENGTH) assert cid is None and packet is None
def test_crc_error(self): # B5 62 13 40 18 00 10 00 00 12 E4 07 09 05 06 28 30 00 40 28 EF 0C 0A 00 00 00 00 00 00 00 51 AC # hdr | <-- checksum --> | chksum frame = [ 0xB5, 0x62, 0x13, 0x40, 0x18, 0x00, 0x10, 0x00, 0x00, 0x12, 0xE4, 0x07, 0x09, 0x05, 0x06, 0x28, 0x30, 0x00, 0x40, 0x28, 0xEF, 0x0C, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0xAC + 1 ] uut = UbxParser(UbxCID(0x00, 0x02)) uut.set_filter(UbxCID(0x13, 0x41)) uut.process(frame) # CRC packet cid, packet = uut.packet() assert cid == UbxCID(0x00, 0x02)
def test_throws_when_unknown_frame_shall_be_built(self, frame_factory): unregistered_cid = UbxCID(0xCA, 0xFE) with pytest.raises(KeyError): frame_factory.build(unregistered_cid) with pytest.raises(KeyError): frame_factory.build_with_data(unregistered_cid, "12345")
def _state_crc2(self, d): self.ckb = d # if checksum matches received checksum .. if self.checksum.matches(self.cka, self.ckb): self.frames_rx += 1 # .. and frame passes filter .. cid = UbxCID(self.msg_class, self.msg_id) if self.wait_cids and cid in self.wait_cids: # .. queue packet (CID and data) packet = (cid, self.msg_data) self.rx_queue.append(packet) else: if logger.isEnabledFor(logging.DEBUG): logger.debug( f'no match - dropping {cid}, {self.msg_len} bytes') else: logger.warning('checksum error in frame, discarding') logger.warning( f'{self.msg_class:02x} {self.msg_id:02x} {binascii.hexlify(self.msg_data)}' ) crc_error_message = (self.crc_error_cid, None) self.rx_queue.append(crc_error_message) self.state = __class__.State.INIT
def test_frame_construct(self, frame_factory): frame_factory.register(UbxAckAck) data = bytearray.fromhex('11 22') f = frame_factory.build_with_data(UbxAckAck.CID, data) assert f.CID == UbxCID(0x05, 0x01) assert f.f.clsId == 0x11 assert f.f.msgId == 0x22
def __init__(self): super().__init__() self.cid_crc_error = UbxCID(0x00, 0x02) self.parser = UbxParser(self.cid_crc_error) self.frame_factory = FrameFactory.getInstance() self.max_retries = 2 self.retry_delay_in_ms = 1800
class UbxAckAck(UbxFrame): CID = UbxCID(UbxCID.CLASS_ACK, 0x01) NAME = 'UBX-ACK-ACK' def __init__(self): super().__init__() self.f.add(U1('clsId')) self.f.add(U1('msgId'))
def _check_ack_nak(self, request, res): if res.CID == UbxAckAck.CID: ack_cid = UbxCID(res.f.clsId, res.f.msgId) if ack_cid == request.CID: # if logger.isEnabledFor(logging.DEBUG): # logger.debug('ACK matches request') return "ACK" else: # ACK is for another request logger.warning( f'ACK {ack_cid} does not match request {request.CID}') elif res.CID == UbxAckNak.CID: logger.warning(f'request {request.CID} rejected, NAK received') return "NAK" else: # Must never happen. Only ACK/NAK in expected list logger.error(f'invalid frame received {res.CID}')
def test_multiple_filters(self): uut = UbxParser(UbxCID(0x00, 0x02)) cids = [ UbxCID(0x12, 0x12), UbxCID(0x13, 0x40), UbxCID(0xFF, 0x00), UbxCID(0xFF, 0x00) ] uut.set_filters(cids) uut.process(self.FRAME_1) cid, packet = uut.packet() assert cid == UbxCID(0x13, 0x40)
def test_passes_filter(self): uut = UbxParser(UbxCID(0x00, 0x02)) uut.set_filter(UbxCID(0x13, 0x40)) uut.process(self.FRAME_1) cid, packet = uut.packet() assert cid == UbxCID(0x13, 0x40)
def test_no_frames(self): uut = UbxParser(UbxCID(0x00, 0x02)) packet = uut.packet() assert packet == (None, None)
def test_filters(self): uut = UbxParser(UbxCID(0x00, 0x02)) uut.set_filters([UbxCID(0x05, 0x00), UbxCID(0x05, 0x01)]) assert len(uut.wait_cids) == 2 assert UbxCID(0x05, 0x00) in uut.wait_cids assert UbxCID(0x05, 0x01) in uut.wait_cids
def test_registration(self, frame_factory): frame_factory.register(UbxAckAck) f1 = frame_factory.build(UbxCID(5, 1)) assert type(f1) == UbxAckAck
def test_creation(self): c = UbxCID(0x05, 0x01) assert c.cls == 0x05 assert c.id == 0x01
class UbxCfgEsfAlg_(UbxFrame): CID = UbxCID(UbxCID.CLASS_CFG, 0x56) NAME = 'UBX-CFG-ESFALG'
def test_equal(self): c1 = UbxCID(0x05, 0x01) c2 = UbxCID(0x05, 0x01) assert c1 == c2
class UbxCfgNmea_(UbxFrame): CID = UbxCID(UbxCID.CLASS_CFG, 0x17) NAME = 'UBX-CFG-NMEA'
class UbxCfgEsfla_(UbxFrame): CID = UbxCID(UbxCID.CLASS_CFG, 0x2f) NAME = 'UBX-CFG-ESFLA' TYPE_VRP_Antenna = 0 TYPE_VRP_IMU = 1
class UbxMonVer_(UbxFrame): CID = UbxCID(UbxCID.CLASS_MON, 0x04) NAME = 'UBX-MON-VER'
class UbxEsfStatus_(UbxFrame): CID = UbxCID(UbxCID.CLASS_ESF, 0x10) NAME = 'UBX-ESF-STATUS'
def test_dropped_id(self): uut = UbxParser(UbxCID(0x00, 0x02)) uut.set_filter(UbxCID(0x13, 0x41)) uut.process(self.FRAME_1) cid, packet = uut.packet() assert cid is None and packet is None
class UbxUpdSos_(UbxFrame): NAME = 'UBX-UPD-SOS' CID = UbxCID(UbxCID.CLASS_UPD, 0x14)
def test_unkown_frame(self, frame_factory): frame_factory.register(UbxAckAck) with pytest.raises(KeyError): frame_factory.build(UbxCID(99, 99))
class UbxCfgNav5_(UbxFrame): CID = UbxCID(UbxCID.CLASS_CFG, 0x24) NAME = 'UBX-CFG-NAV5'
class UbxCfgPrt_(UbxFrame): CID = UbxCID(UbxCID.CLASS_CFG, 0x00) NAME = 'UBX-CFG-PRT' PORTID_Uart = 1
class UbxMgaAckData0_(UbxFrame): CID = UbxCID(UbxCID.CLASS_MGA, 0x60) NAME = 'UBX-MGA-ACK-DATA0'
def test_print(self): c = UbxCID(0x05, 0x01) res = str(c) assert res == 'cls:05 id:01'