def build_packet(self): """ Create and save packet from raw bytes """ logger.debug(f"Starting of packet build") packet = Packet.from_response(self.data) self.packets[packet.id] = packet self.data = []
def test_packet_from_response(): raw_packet = [0x8d, 0x0a, 0x23, 0x42, 0x01, 0x15, 0x16, 0x64, 0xd8] packet = Packet.from_response(raw_packet) assert packet.flags == 0x0a assert packet.source_id is None assert packet.target_id is None assert packet.device_id == 0x23 assert packet.command_id == 0x42 assert packet.sequence == 0x01 assert packet.data == [0x15, 0x16] assert packet.checksum == 0x64
def test_packet_from_response_with_source_id(): raw_packet = [0x8d, 0x2a, 0xfe, 0x23, 0x42, 0x01, 0xab, 0x23, 0xab, 0x05, 0x39, 0xd8] packet = Packet.from_response(raw_packet) assert packet.flags == 0x2a assert packet.source_id == 0xfe assert packet.target_id is None assert packet.device_id == 0x23 assert packet.command_id == 0x42 assert packet.sequence == 0x01 assert packet.data == [0xab, 0x8d] assert packet.checksum == 0x39
def test_packet_from_response_bad_data(): # bad start raw_packet = [0xad, 0x0a, 0x23, 0x42, 0x01, 0x15, 0x16, 0x64, 0xd8] with pytest.raises(PySpheroRuntimeError): Packet.from_response(raw_packet) # bad end raw_packet = [0x8d, 0x0a, 0x23, 0x42, 0x01, 0x15, 0x16, 0x64, 0xdd] with pytest.raises(PySpheroRuntimeError): Packet.from_response(raw_packet) # bad checksum raw_packet = [0x8d, 0x0a, 0x23, 0x42, 0x01, 0x15, 0x16, 0xff, 0xd8] with pytest.raises(PySpheroRuntimeError): Packet.from_response(raw_packet)