2022-07-03 20:58:32 +02:00
|
|
|
import logging
|
2022-05-18 23:40:13 +02:00
|
|
|
import sys
|
2022-09-09 14:23:18 +02:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional, Sequence
|
2022-07-03 20:58:32 +02:00
|
|
|
|
2022-09-09 14:23:18 +02:00
|
|
|
from spacepackets import SpacePacket, SpacePacketHeader, PacketTypes
|
2022-09-09 14:30:15 +02:00
|
|
|
from spacepackets.cfdp import (
|
|
|
|
TransmissionModes,
|
|
|
|
PduType,
|
|
|
|
DirectiveType,
|
|
|
|
GenericPduPacket,
|
|
|
|
PduHolder,
|
|
|
|
PduFactory,
|
|
|
|
)
|
2022-09-09 14:23:18 +02:00
|
|
|
from tmtccmd.cfdp import (
|
|
|
|
RemoteEntityCfgTable,
|
|
|
|
RemoteEntityCfg,
|
|
|
|
LocalEntityCfg,
|
|
|
|
CfdpUserBase,
|
|
|
|
)
|
2022-09-09 14:30:15 +02:00
|
|
|
from spacepackets.util import UnsignedByteField
|
2022-09-09 14:23:18 +02:00
|
|
|
from tmtccmd.cfdp.handler import SourceHandler, DestHandler
|
|
|
|
from tmtccmd.cfdp.request import PutRequest, PutRequestCfg
|
2022-07-03 20:58:32 +02:00
|
|
|
from tmtccmd.logging import get_current_time_string
|
2022-07-27 20:43:52 +02:00
|
|
|
from tmtccmd.pus.pus_11_tc_sched import Subservices as Pus11Subservices
|
2022-07-28 16:37:01 +02:00
|
|
|
from tmtccmd.tc.queue import DefaultPusQueueHelper
|
2022-09-09 14:23:18 +02:00
|
|
|
from tmtccmd.util import FileSeqCountProvider, PusFileSeqCountProvider, ProvidesSeqCount
|
2022-07-27 14:40:25 +02:00
|
|
|
from tmtccmd.util.tmtc_printer import FsfwTmTcPrinter
|
2022-05-18 23:40:13 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import spacepackets
|
|
|
|
except ImportError as error:
|
|
|
|
print(error)
|
2022-07-03 20:58:32 +02:00
|
|
|
print("Python tmtccmd module could not be imported. Make sure it is installed")
|
2022-05-18 23:40:13 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
2022-07-03 20:58:32 +02:00
|
|
|
import tmtccmd
|
2022-05-18 23:40:13 +02:00
|
|
|
except ImportError as error:
|
2022-07-03 20:58:32 +02:00
|
|
|
print(error)
|
|
|
|
print("Python tmtccmd module could not be imported. Make sure it is installed")
|
2022-05-18 23:40:13 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2022-07-28 16:37:01 +02:00
|
|
|
from spacepackets.ecss import PusVerificator, PusTelecommand, PusServices
|
2022-07-03 20:58:32 +02:00
|
|
|
|
|
|
|
from common_tmtc.pus_tc.pus_11_tc_sched import pack_service_11_commands
|
|
|
|
from common_tmtc.pus_tc.pus_17_test import pack_service_17_commands
|
|
|
|
from common_tmtc.pus_tc.pus_200_mode import pack_service_200_commands_into
|
|
|
|
from common_tmtc.pus_tc.service_20_parameters import pack_service20_commands_into
|
|
|
|
from common_tmtc.pus_tc.service_2_raw_cmd import pack_service_2_commands_into
|
|
|
|
from common_tmtc.pus_tc.service_3_housekeeping import pack_service_3_commands_into
|
|
|
|
from common_tmtc.pus_tc.service_8_func_cmd import pack_service_8_commands_into
|
|
|
|
from examples.tmtcc import EXAMPLE_APID
|
|
|
|
from tmtccmd import TcHandlerBase, get_console_logger, TmTcCfgHookBase, BackendBase
|
2022-07-27 14:40:25 +02:00
|
|
|
from tmtccmd.pus import VerificationWrapper
|
2022-07-03 20:58:32 +02:00
|
|
|
from tmtccmd.tc import (
|
|
|
|
ProcedureHelper,
|
|
|
|
FeedWrapper,
|
|
|
|
TcProcedureType,
|
2022-07-27 20:43:52 +02:00
|
|
|
TcQueueEntryType,
|
|
|
|
SendCbParams,
|
2022-07-03 20:58:32 +02:00
|
|
|
)
|
|
|
|
from tmtccmd.tc.pus_5_event import pack_generic_service_5_test_into
|
|
|
|
from tmtccmd.tm import SpecificApidHandlerBase, CcsdsTmHandler
|
|
|
|
from tmtccmd.logging.pus import RawTmtcTimedLogWrapper
|
|
|
|
from tmtccmd.config import CoreServiceList, SetupWrapper, SetupParams, ArgParserWrapper
|
|
|
|
from common_tmtc.pus_tm.factory_hook import pus_factory_hook
|
|
|
|
|
|
|
|
|
|
|
|
LOGGER = get_console_logger()
|
|
|
|
|
|
|
|
|
2022-09-09 14:23:18 +02:00
|
|
|
class CfdpCcsdsWrapper:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
cfg: LocalEntityCfg,
|
|
|
|
user: CfdpUserBase,
|
|
|
|
cfdp_seq_cnt_provider: ProvidesSeqCount,
|
|
|
|
remote_cfg: Sequence[RemoteEntityCfg],
|
|
|
|
ccsds_seq_cnt_provider: ProvidesSeqCount,
|
|
|
|
ccsds_apid: int,
|
|
|
|
):
|
|
|
|
self.handler = CfdpHandler(cfg, user, cfdp_seq_cnt_provider, remote_cfg)
|
|
|
|
self.ccsds_seq_cnt_provider = ccsds_seq_cnt_provider
|
|
|
|
self.ccsds_apid = ccsds_apid
|
|
|
|
|
|
|
|
def pull_next_dest_packet(self) -> Optional[SpacePacket]:
|
|
|
|
"""Retrieves the next PDU to send and wraps it into a space packet"""
|
|
|
|
next_packet = self.handler.pull_next_dest_packet()
|
|
|
|
if next_packet is None:
|
|
|
|
return next_packet
|
|
|
|
sp_header = SpacePacketHeader(
|
|
|
|
packet_type=PacketTypes.TC,
|
|
|
|
apid=self.ccsds_apid,
|
|
|
|
seq_count=self.ccsds_seq_cnt_provider.get_and_increment(),
|
|
|
|
data_len=next_packet.packet_len - 1,
|
|
|
|
)
|
|
|
|
return SpacePacket(sp_header, None, next_packet.pack())
|
|
|
|
|
|
|
|
def confirm_dest_packet_sent(self):
|
|
|
|
self.handler.confirm_dest_packet_sent()
|
|
|
|
|
|
|
|
def pass_packet(self, packet: SpacePacket):
|
|
|
|
# Unwrap the user data and pass it to the handler
|
|
|
|
pdu_raw = packet.user_data
|
|
|
|
pdu_base = PduFactory.from_raw(pdu_raw)
|
|
|
|
self.handler.pass_packet(pdu_base)
|
|
|
|
|
|
|
|
|
|
|
|
class CfdpHandler:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
cfg: LocalEntityCfg,
|
|
|
|
user: CfdpUserBase,
|
|
|
|
seq_cnt_provider: ProvidesSeqCount,
|
|
|
|
remote_cfg: Sequence[RemoteEntityCfg],
|
|
|
|
):
|
|
|
|
self.dest_id = UnsignedByteField(EXAMPLE_APID, 2)
|
|
|
|
self.remote_cfg_table = RemoteEntityCfgTable()
|
|
|
|
self.remote_cfg_table.add_remote_entities(remote_cfg)
|
|
|
|
self.dest_handler = DestHandler(cfg, user, self.remote_cfg_table)
|
|
|
|
self.source_handler = SourceHandler(cfg, seq_cnt_provider, user)
|
|
|
|
|
|
|
|
def put_request_file(
|
|
|
|
self,
|
|
|
|
source_path: Path,
|
|
|
|
dest_path: Path,
|
|
|
|
trans_mode: TransmissionModes,
|
|
|
|
closure_requested: bool,
|
|
|
|
):
|
|
|
|
put_request_cfg = PutRequestCfg(
|
|
|
|
destination_id=self.dest_id,
|
|
|
|
source_file=source_path,
|
|
|
|
dest_file=dest_path.as_posix(),
|
|
|
|
trans_mode=trans_mode,
|
|
|
|
closure_requested=closure_requested,
|
|
|
|
)
|
|
|
|
put_request = PutRequest(put_request_cfg)
|
|
|
|
self.source_handler.put_request(
|
|
|
|
put_request, self.remote_cfg_table.get_remote_entity(self.dest_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
def pull_next_dest_packet(self) -> Optional[PduHolder]:
|
|
|
|
res = self.dest_handler.state_machine()
|
|
|
|
if res.states.packet_ready:
|
|
|
|
return self.dest_handler.pdu_holder
|
|
|
|
return None
|
|
|
|
|
|
|
|
def confirm_dest_packet_sent(self):
|
|
|
|
self.dest_handler.confirm_packet_sent_advance_fsm()
|
|
|
|
|
|
|
|
def pass_packet(self, packet: GenericPduPacket):
|
|
|
|
"""This function routes the packets based on PDU type and directive type if applicable.
|
|
|
|
|
|
|
|
The routing is based on section 4.5 of the CFDP standard whcih specifies the PDU forwarding
|
|
|
|
procedure.
|
|
|
|
"""
|
|
|
|
if packet.pdu_type == PduType.FILE_DATA:
|
|
|
|
self.dest_handler.pass_packet(packet)
|
|
|
|
else:
|
|
|
|
if packet.directive_type in [
|
|
|
|
DirectiveType.METADATA_PDU,
|
|
|
|
DirectiveType.EOF_PDU,
|
|
|
|
DirectiveType.PROMPT_PDU,
|
|
|
|
]:
|
|
|
|
# Section b) of 4.5.3: These PDUs should always be targeted towards the file
|
|
|
|
# receiver a.k.a. the destination handler
|
|
|
|
self.dest_handler.pass_packet(packet)
|
|
|
|
elif packet.directive_type in [
|
|
|
|
DirectiveType.FINISHED_PDU,
|
|
|
|
DirectiveType.NAK_PDU,
|
|
|
|
DirectiveType.KEEP_ALIVE_PDU,
|
|
|
|
]:
|
|
|
|
# Section c) of 4.5.3: These PDUs should always be targeted towards the file sender
|
|
|
|
# a.k.a. the source handler
|
|
|
|
self.source_handler.pass_packet(packet)
|
|
|
|
elif packet.directive_type == DirectiveType.ACK_PDU:
|
|
|
|
# Section a): Recipient depends on the type of PDU that is being acknowledged.
|
|
|
|
# We can simply extract the PDU type from the raw stream. If it is an EOF PDU,
|
|
|
|
# this packet is passed to the source handler. For a finished PDU, it is
|
|
|
|
# passed to the destination handler
|
|
|
|
pdu_holder = PduHolder(packet)
|
|
|
|
ack_pdu = pdu_holder.to_ack_pdu()
|
|
|
|
if ack_pdu.directive_code_of_acked_pdu == DirectiveType.EOF_PDU:
|
|
|
|
self.source_handler.pass_packet(packet)
|
|
|
|
elif ack_pdu.directive_code_of_acked_pdu == DirectiveType.FINISHED_PDU:
|
|
|
|
self.dest_handler.pass_packet(packet)
|
|
|
|
|
|
|
|
|
2022-07-03 20:58:32 +02:00
|
|
|
class PusHandler(SpecificApidHandlerBase):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
wrapper: VerificationWrapper,
|
|
|
|
printer: FsfwTmTcPrinter,
|
|
|
|
raw_logger: RawTmtcTimedLogWrapper,
|
|
|
|
):
|
|
|
|
super().__init__(EXAMPLE_APID, None)
|
|
|
|
self.printer = printer
|
|
|
|
self.verif_wrapper = wrapper
|
|
|
|
self.raw_logger = raw_logger
|
|
|
|
|
|
|
|
def handle_tm(self, packet: bytes, _user_args: any):
|
|
|
|
pus_factory_hook(
|
|
|
|
packet=packet,
|
|
|
|
wrapper=self.verif_wrapper,
|
|
|
|
raw_logger=self.raw_logger,
|
|
|
|
printer=self.printer,
|
|
|
|
)
|
2022-05-18 23:40:13 +02:00
|
|
|
|
|
|
|
|
2022-07-03 20:58:32 +02:00
|
|
|
class TcHandler(TcHandlerBase):
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-07-28 16:49:19 +02:00
|
|
|
pus_apid: int,
|
2022-07-03 20:58:32 +02:00
|
|
|
seq_count_provider: FileSeqCountProvider,
|
|
|
|
pus_verificator: PusVerificator,
|
|
|
|
file_logger: logging.Logger,
|
|
|
|
raw_logger: RawTmtcTimedLogWrapper,
|
|
|
|
):
|
|
|
|
super().__init__()
|
|
|
|
self.seq_count_provider = seq_count_provider
|
|
|
|
self.pus_verificator = pus_verificator
|
|
|
|
self.file_logger = file_logger
|
|
|
|
self.raw_logger = raw_logger
|
2022-07-28 16:37:01 +02:00
|
|
|
self.queue_helper = DefaultPusQueueHelper(
|
|
|
|
queue_wrapper=None,
|
2022-07-28 16:49:19 +02:00
|
|
|
pus_apid=pus_apid,
|
2022-07-28 16:37:01 +02:00
|
|
|
seq_cnt_provider=seq_count_provider,
|
|
|
|
pus_verificator=pus_verificator,
|
|
|
|
)
|
2022-05-18 23:40:13 +02:00
|
|
|
|
2022-07-03 20:58:32 +02:00
|
|
|
def feed_cb(self, info: ProcedureHelper, wrapper: FeedWrapper):
|
2022-07-28 16:37:01 +02:00
|
|
|
self.queue_helper.queue_wrapper = wrapper.queue_wrapper
|
2022-07-03 20:58:32 +02:00
|
|
|
if info.proc_type == TcProcedureType.DEFAULT:
|
|
|
|
def_proc = info.to_def_procedure()
|
|
|
|
service = def_proc.service
|
|
|
|
op_code = def_proc.op_code
|
|
|
|
if service == CoreServiceList.SERVICE_2.value:
|
|
|
|
return pack_service_2_commands_into(
|
2022-07-28 16:37:01 +02:00
|
|
|
op_code=op_code, q=self.queue_helper
|
2022-07-03 20:58:32 +02:00
|
|
|
)
|
|
|
|
if service == CoreServiceList.SERVICE_3.value:
|
|
|
|
return pack_service_3_commands_into(
|
2022-07-28 16:37:01 +02:00
|
|
|
op_code=op_code, q=self.queue_helper
|
2022-07-03 20:58:32 +02:00
|
|
|
)
|
|
|
|
if service == CoreServiceList.SERVICE_5.value:
|
2022-07-28 16:37:01 +02:00
|
|
|
return pack_generic_service_5_test_into(q=self.queue_helper)
|
2022-07-03 20:58:32 +02:00
|
|
|
if service == CoreServiceList.SERVICE_8.value:
|
|
|
|
return pack_service_8_commands_into(
|
2022-07-28 16:37:01 +02:00
|
|
|
op_code=op_code, q=self.queue_helper
|
2022-07-03 20:58:32 +02:00
|
|
|
)
|
|
|
|
if service == CoreServiceList.SERVICE_11.value:
|
2022-07-28 16:37:01 +02:00
|
|
|
return pack_service_11_commands(op_code=op_code, q=self.queue_helper)
|
2022-07-03 20:58:32 +02:00
|
|
|
if service == CoreServiceList.SERVICE_17.value:
|
2022-07-28 16:37:01 +02:00
|
|
|
return pack_service_17_commands(op_code=op_code, q=self.queue_helper)
|
2022-07-03 20:58:32 +02:00
|
|
|
if service == CoreServiceList.SERVICE_20.value:
|
|
|
|
return pack_service20_commands_into(
|
2022-07-28 16:37:01 +02:00
|
|
|
q=self.queue_helper, op_code=op_code
|
2022-07-03 20:58:32 +02:00
|
|
|
)
|
|
|
|
if service == CoreServiceList.SERVICE_200.value:
|
|
|
|
return pack_service_200_commands_into(
|
2022-07-28 16:37:01 +02:00
|
|
|
q=self.queue_helper, op_code=op_code
|
2022-07-03 20:58:32 +02:00
|
|
|
)
|
|
|
|
LOGGER.warning("Invalid Service !")
|
2022-05-18 23:40:13 +02:00
|
|
|
|
2022-07-27 14:40:25 +02:00
|
|
|
def send_cb(self, params: SendCbParams):
|
|
|
|
if params.entry.is_tc:
|
|
|
|
if params.entry.entry_type == TcQueueEntryType.PUS_TC:
|
2022-07-28 15:25:06 +02:00
|
|
|
self.handle_tc_send_cb(params)
|
|
|
|
elif params.entry.entry_type == TcQueueEntryType.LOG:
|
2022-07-28 15:26:37 +02:00
|
|
|
log_entry = params.entry.to_log_entry()
|
|
|
|
LOGGER.info(log_entry.log_str)
|
|
|
|
self.file_logger.info(log_entry.log_str)
|
2022-07-28 15:25:06 +02:00
|
|
|
|
|
|
|
def handle_tc_send_cb(self, params: SendCbParams):
|
|
|
|
pus_tc_wrapper = params.entry.to_pus_tc_entry()
|
|
|
|
if (
|
2022-07-28 16:37:01 +02:00
|
|
|
pus_tc_wrapper.pus_tc.service == PusServices.S11_TC_SCHED
|
|
|
|
and pus_tc_wrapper.pus_tc.subservice == Pus11Subservices.TC_INSERT
|
2022-07-28 15:25:06 +02:00
|
|
|
):
|
2022-07-28 16:37:01 +02:00
|
|
|
wrapped_tc = PusTelecommand.unpack(pus_tc_wrapper.pus_tc.app_data[4:])
|
|
|
|
tc_info_string = f"Sending time-tagged command {wrapped_tc}"
|
|
|
|
LOGGER.info(tc_info_string)
|
|
|
|
self.file_logger.info(f"{get_current_time_string(True)}: {tc_info_string}")
|
2022-07-28 15:25:06 +02:00
|
|
|
raw_tc = pus_tc_wrapper.pus_tc.pack()
|
|
|
|
self.raw_logger.log_tc(pus_tc_wrapper.pus_tc)
|
2022-07-28 16:37:01 +02:00
|
|
|
tc_info_string = f"Sending {pus_tc_wrapper.pus_tc}"
|
2022-07-28 15:25:06 +02:00
|
|
|
LOGGER.info(tc_info_string)
|
2022-07-28 16:37:01 +02:00
|
|
|
self.file_logger.info(f"{get_current_time_string(True)}: {tc_info_string}")
|
2022-07-28 15:25:06 +02:00
|
|
|
params.com_if.send(raw_tc)
|
2022-07-03 20:58:32 +02:00
|
|
|
|
|
|
|
def queue_finished_cb(self, info: ProcedureHelper):
|
|
|
|
if info is not None and info.proc_type == TcQueueEntryType.PUS_TC:
|
|
|
|
def_proc = info.to_def_procedure()
|
|
|
|
LOGGER.info(
|
|
|
|
f"Finished queue for service {def_proc.service} and op code {def_proc.op_code}"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_params(hook_obj: TmTcCfgHookBase) -> SetupWrapper:
|
|
|
|
print(f"-- eive TMTC Commander --")
|
2022-07-27 20:43:52 +02:00
|
|
|
print(f"-- spacepackets v{spacepackets.__version__} --")
|
2022-07-03 20:58:32 +02:00
|
|
|
params = SetupParams()
|
|
|
|
parser_wrapper = ArgParserWrapper(hook_obj)
|
2022-08-24 15:31:06 +02:00
|
|
|
parser_wrapper.create_default_parent_parser()
|
|
|
|
parser_wrapper.create_default_parser()
|
2022-09-08 17:52:09 +02:00
|
|
|
parser_wrapper.add_def_proc_and_cfdp_as_subparsers()
|
2022-07-03 20:58:32 +02:00
|
|
|
parser_wrapper.parse()
|
|
|
|
tmtccmd.init_printout(parser_wrapper.use_gui)
|
|
|
|
parser_wrapper.set_params(params)
|
|
|
|
params.apid = EXAMPLE_APID
|
|
|
|
setup_wrapper = SetupWrapper(hook_obj=hook_obj, setup_params=params)
|
|
|
|
return setup_wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def setup_tmtc_handlers(
|
|
|
|
verif_wrapper: VerificationWrapper,
|
|
|
|
printer: FsfwTmTcPrinter,
|
|
|
|
raw_logger: RawTmtcTimedLogWrapper,
|
|
|
|
) -> (CcsdsTmHandler, TcHandler):
|
|
|
|
|
|
|
|
pus_handler = PusHandler(
|
|
|
|
printer=printer, raw_logger=raw_logger, wrapper=verif_wrapper
|
|
|
|
)
|
|
|
|
ccsds_handler = CcsdsTmHandler(None)
|
|
|
|
ccsds_handler.add_apid_handler(pus_handler)
|
|
|
|
tc_handler = TcHandler(
|
2022-07-28 16:49:19 +02:00
|
|
|
pus_apid=EXAMPLE_APID,
|
2022-07-03 20:58:32 +02:00
|
|
|
file_logger=printer.file_logger,
|
|
|
|
raw_logger=raw_logger,
|
|
|
|
pus_verificator=verif_wrapper.pus_verificator,
|
2022-07-27 14:40:25 +02:00
|
|
|
seq_count_provider=PusFileSeqCountProvider(),
|
2022-07-03 20:58:32 +02:00
|
|
|
)
|
|
|
|
return ccsds_handler, tc_handler
|
|
|
|
|
|
|
|
|
|
|
|
def setup_backend(
|
|
|
|
setup_wrapper: SetupWrapper,
|
|
|
|
tc_handler: TcHandler,
|
|
|
|
ccsds_handler: CcsdsTmHandler,
|
|
|
|
) -> BackendBase:
|
2022-05-18 23:40:13 +02:00
|
|
|
tmtc_backend = tmtccmd.create_default_tmtc_backend(
|
2022-07-03 20:58:32 +02:00
|
|
|
setup_wrapper=setup_wrapper, tm_handler=ccsds_handler, tc_handler=tc_handler
|
2022-05-18 23:40:13 +02:00
|
|
|
)
|
2022-07-03 20:58:32 +02:00
|
|
|
tmtccmd.start(tmtc_backend=tmtc_backend, hook_obj=setup_wrapper.hook_obj)
|
|
|
|
return tmtc_backend
|