def test_fails(): try: Packet.create(PACKET.RESPONSE, 0xA5, 0x01, 0x01) assert False except ValueError: assert True try: Packet.create(PACKET.RADIO_ERP1, 0xA6, 0x01, 0x01) assert False except ValueError: assert True try: Packet.create(PACKET.RADIO_ERP1, 0xA5, 0x01, 0x01, destination='ASDASDASD') assert False except ValueError: assert True try: Packet.create(PACKET.RADIO_ERP1, 0xA5, 0x01, 0x01, sender='ASDASDASD') assert False except ValueError: assert True
def test_packet_assembly(): PACKET_CONTENT_1 = bytearray([ 0x55, 0x00, 0x0A, 0x00, 0x01, 0x80, 0xA5, 0x00, 0x00, 0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x18 ]) PACKET_CONTENT_2 = bytearray([ 0x55, 0x00, 0x0A, 0x07, 0x01, 0xEB, 0xA5, 0x00, 0x00, 0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xE4 ]) PACKET_CONTENT_3 = bytearray([ 0x55, 0x00, 0x0A, 0x07, 0x01, 0xEB, 0xA5, 0x32, 0x20, 0x89, 0x00, 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x43 ]) PACKET_CONTENT_4 = bytearray([ 0x55, 0x00, 0x0A, 0x07, 0x01, 0xEB, 0xA5, 0x32, 0x00, 0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x80 ]) # manually assemble packet packet = Packet(PACKET.RADIO_ERP1) packet.rorg = RORG.BS4 sender_bytes = [(0xdeadbeef >> i & 0xff) for i in (24, 16, 8, 0)] data = [0, 0, 0, 0] packet.data = [packet.rorg] + data + sender_bytes + [0] # test content packet_serialized = packet.build() assert len(packet_serialized) == len(PACKET_CONTENT_1) assert list(packet_serialized) == list(PACKET_CONTENT_1) # set optional data sub_tel_num = 3 destination = [255, 255, 255, 255] # broadcast dbm = 0xff security = 0 packet.optional = [sub_tel_num] + destination + [dbm] + [security] # test content packet_serialized = packet.build() assert len(packet_serialized) == len(PACKET_CONTENT_2) assert list(packet_serialized) == list(PACKET_CONTENT_2) # update data based on EEP packet.select_eep(0x20, 0x01, 1) prop = { 'CV': 50, 'TMP': 21.5, 'ES': 'true', } packet.set_eep(prop) # test content packet_serialized = packet.build() assert len(packet_serialized) == len(PACKET_CONTENT_3) assert list(packet_serialized) == list(PACKET_CONTENT_3) assert packet.rorg_func == 0x20 assert packet.rorg_type == 0x01 # Test the easier method of sending packets. packet = Packet.create(PACKET.RADIO_ERP1, rorg=RORG.BS4, rorg_func=0x20, rorg_type=0x01, learn=True, direction=1, **prop) packet_serialized = packet.build() assert len(packet_serialized) == len(PACKET_CONTENT_3) assert list(packet_serialized) == list(PACKET_CONTENT_3) assert packet.rorg_func == 0x20 assert packet.rorg_type == 0x01 # Test creating RadioPacket directly. packet = RadioPacket.create(rorg=RORG.BS4, rorg_func=0x20, rorg_type=0x01, learn=True, direction=2, SP=50) packet_serialized = packet.build() assert len(packet_serialized) == len(PACKET_CONTENT_4) assert list(packet_serialized) == list(PACKET_CONTENT_4) assert packet.rorg_func == 0x20 assert packet.rorg_type == 0x01