def test_space_packet_decoder_segmented(): """ Test the decoder with a list of packets """ random_data = os.urandom(MAX_BYTES_PER_PACKET + 1) # one more byte than space packet can handle expected_space_packet_0 = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.SEGMENT_FIRST, packet_seq_count=0, data=random_data[0:MAX_BYTES_PER_PACKET], sec_hdr_flag=False ) expected_space_packet_1 = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.SEGMENT_LAST, packet_seq_count=1, data=bytes(random_data[MAX_BYTES_PER_PACKET:]), sec_hdr_flag=False ) space_packet_list = [expected_space_packet_0, expected_space_packet_1] decoder = SpacePacketProtocolDecoder(apid=TEST_APID_VAL) user_data = decoder.decode(space_packet_list) assert(user_data == random_data)
def test_segmented_space_packet_protocol(): """ Test formation of segmented space packet (length of data greater than 16384 bytes) """ spp_encoder = SpacePacketProtocolEncoder( packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL ) random_data = os.urandom(MAX_BYTES_PER_PACKET + 1) # one more byte than space packet can handle encoded_data_list = spp_encoder.encode_packets(random_data) expected_space_packet_0 = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.SEGMENT_FIRST, packet_seq_count=0, data=random_data[0:MAX_BYTES_PER_PACKET], sec_hdr_flag=False ) expected_space_packet_1 = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.SEGMENT_LAST, packet_seq_count=1, data=bytes(random_data[MAX_BYTES_PER_PACKET:]), sec_hdr_flag=False ) # assert that only one packet exists and that the packets match assert(len(encoded_data_list) == 2) assert(encoded_data_list[0] == expected_space_packet_0) assert(encoded_data_list[1] == expected_space_packet_1)
def test_space_packet_to_bytes(): """ Test serializing space packets to bytes """ expected_byte_sequence = b'\x05\x39\xc0\x11\x00\x04\xde\xad\xbe\xef' space_packet = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.UNSEGMENTED, packet_seq_count=17, data=b'\xde\xad\xbe\xef', sec_hdr_flag=False ) assert(expected_byte_sequence == space_packet.to_bytes())
def test_space_packet_from_bytes(): """ Test deserialization of space packets :return: """ byte_sequence = b'\x05\x39\xc0\x11\x00\x04\xde\xad\xbe\xef' expected_space_packet = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.UNSEGMENTED, packet_seq_count=17, data=b'\xde\xad\xbe\xef', sec_hdr_flag=False ) space_packet = SpacePacket.from_bytes(byte_sequence) assert(space_packet == expected_space_packet)
def decode_to_spp_packets( self, datalink_list: List[TCDataLinkPacket] ): """ Given a list of datalink packest, reconstruct the SPP packets :param datalink_list: :return: """ spp_packet_list = [] segmented_packet_list = [] # TODO : Support reordering of packets, right now it assumes that packets must come in order reordered_datalink_list = [None] * len(datalink_list) for packet in datalink_list: reordered_datalink_list[packet.frame_seq_num] = packet for packet in reordered_datalink_list: # filter out packets that do not match spacecraft_id, virtual_channel_id and map_id if packet.spacecraft_id != self.spacecraft_id or packet.virtual_channel_id != self.virtual_channel_id or \ packet.map_id != self.map_id: continue # handle different case scenarios if packet.seq_flags == SegmentHeaderSequenceFlags.UNSEGMENTED: spp_packet_list.append(SpacePacket.from_bytes(packet.data)) elif packet.seq_flags == SegmentHeaderSequenceFlags.SEGMENT_FIRST: segmented_packet_list = [packet] elif packet.seq_flags == SegmentHeaderSequenceFlags.SEGMENT_CONTINUATION: assert (len(segmented_packet_list) > 0) segmented_packet_list.append(packet) elif packet.seq_flags == SegmentHeaderSequenceFlags.SEGMENT_LAST: segmented_packet_list.append(packet) # receive the last segmented packet, reconstruct the packet here data_bytestream = b'' for segmented_packet in segmented_packet_list: data_bytestream += segmented_packet.data segmented_packet_list = [] spp_packet_list.append(SpacePacket.from_bytes(data_bytestream)) else: raise RuntimeError("Sequence Flag {} not implemented".format(packet.seq_flags)) return spp_packet_list
def test_space_packet_decoder_unsegmented(): """ Test the decoder on ONE space packet """ expected_data = b'\xde\xad\xbe\xef' space_packet = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.UNSEGMENTED, packet_seq_count=17, data=expected_data, sec_hdr_flag=False ) decoder = SpacePacketProtocolDecoder(apid=TEST_APID_VAL) user_data = decoder.decode([space_packet]) assert (user_data == expected_data)
def test_unsegmented_spp_packet_tc_datalnk_encoding(): """ Test encoding a Space Packet :return: """ data = b'\xde\xad\xbe\xef' expected_spp_byte_sequence = b'\x05\x39\xc0\x11\x00\x04\xde\xad\xbe\xef' virtual_channel_id = 36 map_id = 18 spacecraft_id = 555 space_packet = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.UNSEGMENTED, packet_seq_count=17, data=data, sec_hdr_flag=False ) tc_datalink_encoder = TCDataLinkProtocolEncoder( control_command_flag=ControlCommandFlag.CONTROL, spacecraft_id=spacecraft_id, virtual_channel_id=virtual_channel_id, map_id=map_id ) datalink_packet_list = tc_datalink_encoder.encode_spp_list([space_packet]) expected_tc_datalink_packet = TCDataLinkPacket( tx_frame_version=0, bypass_flag=True, control_command_flag=ControlCommandFlag.CONTROL, spacecraft_id=spacecraft_id, virtual_channel_id=virtual_channel_id, frame_seq_num=0, seq_flags=SegmentHeaderSequenceFlags.UNSEGMENTED, map_id=map_id, data=expected_spp_byte_sequence ) assert(datalink_packet_list[0] == expected_tc_datalink_packet) assert(datalink_packet_list[0].to_bytes() == expected_tc_datalink_packet.to_bytes())
def test_unsegmented_space_packet_protocol(): """ Test formation of unsegmented space packet (length of data less than 16384 bytes) """ spp_encoder = SpacePacketProtocolEncoder( packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL ) random_data = os.urandom(256) # generate 256 bytes of random data encoded_data_list = spp_encoder.encode_packets(random_data) expected_space_packet = SpacePacket( ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.UNSEGMENTED, packet_seq_count=0, data=random_data, sec_hdr_flag=False ) # assert that only one packet exists and that the packets match assert(len(encoded_data_list) == 1) assert(encoded_data_list[0] == expected_space_packet)
def test_tc_datalink_encoder_decoder_compatibility(setup_encoder_decoder): """ Encode one space packet and try to decode it to make sure encoder/decoder are compatible :return: """ _, tc_datalink_encoder, _, _, tc_datalink_decoder, _ = setup_encoder_decoder # The IDs are arbitrarily set random_data = os.urandom(256) # generate 256 bytes of random data spp_list = [ SpacePacket(ccsds_version=0, packet_type=SpacePacketType.TELECOMMAND, apid=TEST_APID_VAL, seq_flags=SpacePacketSequenceFlags.UNSEGMENTED, packet_seq_count=17, data=random_data, sec_hdr_flag=False) ] encoded_data_list = tc_datalink_encoder.encode_spp_list(spp_list) assert (len(encoded_data_list) == 1) decoded_spp_list = tc_datalink_decoder.decode_to_spp_packets( encoded_data_list) assert (len(decoded_spp_list) == 1) assert (decoded_spp_list[0] == spp_list[0])