fsfw-example-tmtc-common/common.py

428 lines
16 KiB
Python
Raw Normal View History

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
2022-09-14 19:01:47 +02:00
from typing import cast
2022-07-03 20:58:32 +02:00
2022-09-14 19:01:47 +02:00
from spacepackets import SpacePacket, SpacePacketHeader
2022-10-19 11:55:58 +02:00
from spacepackets.ccsds import SPACE_PACKET_HEADER_SIZE
2022-09-09 14:30:15 +02:00
from spacepackets.cfdp import (
2022-09-15 18:46:28 +02:00
TransmissionMode,
ChecksumType,
2022-09-15 16:28:58 +02:00
ConditionCode,
PduHolder,
2022-09-15 18:46:28 +02:00
DirectiveType,
2022-10-19 11:49:44 +02:00
PduFactory,
PduType,
2022-09-09 14:30:15 +02:00
)
2022-09-14 17:48:15 +02:00
from spacepackets.cfdp.pdu import MetadataPdu, FileDataPdu
2022-09-09 14:23:18 +02:00
from tmtccmd.cfdp import (
RemoteEntityCfg,
LocalEntityCfg,
CfdpUserBase,
2022-09-09 16:55:04 +02:00
IndicationCfg,
TransactionId,
2022-09-09 14:23:18 +02:00
)
2022-09-14 19:01:47 +02:00
from tmtccmd.cfdp.defs import CfdpRequestType
from tmtccmd.cfdp.handler import CfdpInCcsdsHandler
2022-09-09 16:55:04 +02:00
from tmtccmd.cfdp.mib import DefaultFaultHandlerBase
from tmtccmd.cfdp.user import (
FileSegmentRecvdParams,
MetadataRecvParams,
TransactionFinishedParams,
)
2022-09-09 17:41:44 +02:00
from tmtccmd.config.args import ProcedureParamsWrapper
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
from tmtccmd.tc.queue import DefaultPusQueueHelper
2022-09-14 19:01:47 +02:00
from tmtccmd.util import FileSeqCountProvider, PusFileSeqCountProvider
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)
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
2022-09-09 16:55:04 +02:00
from examples.tmtcc import (
EXAMPLE_PUS_APID,
EXAMPLE_CFDP_APID,
CFDP_LOCAL_ENTITY_ID,
CFDP_REMOTE_ENTITY_ID,
)
2022-09-14 18:19:56 +02:00
from tmtccmd import (
TcHandlerBase,
get_console_logger,
TmTcCfgHookBase,
CcsdsTmtcBackend,
)
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 (
2022-09-12 00:53:15 +02:00
ProcedureWrapper,
2022-07-03 20:58:32 +02:00
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
2022-09-09 17:41:44 +02:00
from tmtccmd.config import (
CoreServiceList,
SetupWrapper,
SetupParams,
2022-09-12 09:39:37 +02:00
PreArgsParsingWrapper,
params_to_procedure_conversion,
2022-09-09 17:41:44 +02:00
)
2022-07-03 20:58:32 +02:00
from common_tmtc.pus_tm.factory_hook import pus_factory_hook
LOGGER = get_console_logger()
2022-09-09 17:41:44 +02:00
class ExampleCfdpFaultHandler(DefaultFaultHandlerBase):
def notice_of_suspension_cb(self, cond: ConditionCode):
pass
def notice_of_cancellation_cb(self, cond: ConditionCode):
pass
def abandoned_cb(self, cond: ConditionCode):
pass
def ignore_cb(self, cond: ConditionCode):
pass
2022-09-09 16:55:04 +02:00
class ExampleCfdpUser(CfdpUserBase):
def transaction_indication(self, transaction_id: TransactionId):
2022-09-14 17:35:33 +02:00
LOGGER.info(f"CFDP User: Start of File {transaction_id}")
2022-09-09 16:55:04 +02:00
def eof_sent_indication(self, transaction_id: TransactionId):
2022-09-14 17:35:33 +02:00
LOGGER.info(f"CFDP User: EOF sent for {transaction_id}")
2022-09-09 16:55:04 +02:00
def transaction_finished_indication(self, params: TransactionFinishedParams):
2022-09-14 17:35:33 +02:00
LOGGER.info(f"CFDP User: {params.transaction_id} finished")
2022-09-09 16:55:04 +02:00
def metadata_recv_indication(self, params: MetadataRecvParams):
pass
def file_segment_recv_indication(self, params: FileSegmentRecvdParams):
pass
def report_indication(self, transaction_id: TransactionId, status_report: any):
pass
def suspended_indication(
self, transaction_id: TransactionId, cond_code: ConditionCode
):
pass
def resumed_indication(self, transaction_id: TransactionId, progress: int):
pass
def fault_indication(
self, transaction_id: TransactionId, cond_code: ConditionCode, progress: int
):
pass
def abandoned_indication(
self, transaction_id: TransactionId, cond_code: ConditionCode, progress: int
):
pass
def eof_recv_indication(self, transaction_id: TransactionId):
pass
2022-09-14 19:01:47 +02:00
class CfdpInCcsdsWrapper(SpecificApidHandlerBase):
def __init__(self, cfdp_in_ccsds_handler: CfdpInCcsdsHandler):
2022-09-09 14:58:34 +02:00
super().__init__(EXAMPLE_CFDP_APID, None)
2022-09-14 19:01:47 +02:00
self.handler = cfdp_in_ccsds_handler
2022-09-09 14:23:18 +02:00
2022-09-09 14:58:34 +02:00
def handle_tm(self, packet: bytes, _user_args: any):
2022-10-19 11:55:58 +02:00
# Ignore the space packet header. Its only purpose is to use the same protocol and
# have a seaprate APID for space packets. If this function is called, the APID is correct.
pdu = packet[SPACE_PACKET_HEADER_SIZE:]
2022-10-19 11:49:44 +02:00
pdu_base = PduFactory.from_raw(pdu)
if pdu_base.pdu_type == PduType.FILE_DATA:
LOGGER.info("Received File Data PDU TM")
else:
if pdu_base.directive_type == DirectiveType.FINISHED_PDU:
LOGGER.info(f"Received Finished PDU TM")
else:
LOGGER.info(
f"Received File Directive PDU with type {pdu_base.directive_type!r} TM"
)
self.handler.pass_pdu_packet(pdu_base)
2022-09-09 14:23:18 +02:00
2022-07-03 20:58:32 +02:00
class PusHandler(SpecificApidHandlerBase):
def __init__(
self,
wrapper: VerificationWrapper,
printer: FsfwTmTcPrinter,
raw_logger: RawTmtcTimedLogWrapper,
):
2022-09-09 14:58:34 +02:00
super().__init__(EXAMPLE_PUS_APID, None)
2022-07-03 20:58:32 +02:00
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,
seq_count_provider: FileSeqCountProvider,
2022-09-14 19:01:47 +02:00
cfdp_in_ccsds_wrapper: CfdpInCcsdsWrapper,
2022-07-03 20:58:32 +02:00
pus_verificator: PusVerificator,
file_logger: logging.Logger,
raw_logger: RawTmtcTimedLogWrapper,
):
super().__init__()
2022-09-13 11:59:29 +02:00
self.cfdp_handler_started = False
2022-09-12 09:39:37 +02:00
self.cfdp_dest_id = CFDP_REMOTE_ENTITY_ID
2022-07-03 20:58:32 +02:00
self.seq_count_provider = seq_count_provider
self.pus_verificator = pus_verificator
self.file_logger = file_logger
self.raw_logger = raw_logger
self.queue_helper = DefaultPusQueueHelper(
queue_wrapper=None,
2022-09-09 14:58:34 +02:00
pus_apid=EXAMPLE_PUS_APID,
seq_cnt_provider=seq_count_provider,
pus_verificator=pus_verificator,
)
2022-09-09 15:21:14 +02:00
self.cfdp_in_ccsds_wrapper = cfdp_in_ccsds_wrapper
2022-05-18 23:40:13 +02:00
2022-10-17 12:26:00 +02:00
def cfdp_done(self) -> bool:
return not self.cfdp_in_ccsds_wrapper.handler.put_request_pending()
2022-09-12 00:53:15 +02:00
def feed_cb(self, info: ProcedureWrapper, wrapper: FeedWrapper):
self.queue_helper.queue_wrapper = wrapper.queue_wrapper
2022-07-03 20:58:32 +02:00
if info.proc_type == TcProcedureType.DEFAULT:
2022-09-09 14:46:41 +02:00
self.handle_default_procedure(info)
elif info.proc_type == TcProcedureType.CFDP:
self.handle_cfdp_procedure(info)
2022-09-12 00:53:15 +02:00
def handle_default_procedure(self, info: ProcedureWrapper):
2022-09-09 14:46:41 +02:00
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(op_code=op_code, q=self.queue_helper)
if service == CoreServiceList.SERVICE_3.value:
return pack_service_3_commands_into(op_code=op_code, q=self.queue_helper)
if service == CoreServiceList.SERVICE_5.value:
return pack_generic_service_5_test_into(q=self.queue_helper)
if service == CoreServiceList.SERVICE_8.value:
return pack_service_8_commands_into(op_code=op_code, q=self.queue_helper)
if service == CoreServiceList.SERVICE_11.value:
return pack_service_11_commands(op_code=op_code, q=self.queue_helper)
if service == CoreServiceList.SERVICE_17.value:
return pack_service_17_commands(op_code=op_code, q=self.queue_helper)
if service == CoreServiceList.SERVICE_20.value:
return pack_service20_commands_into(q=self.queue_helper, op_code=op_code)
if service == CoreServiceList.SERVICE_200.value:
return pack_service_200_commands_into(q=self.queue_helper, op_code=op_code)
LOGGER.warning("Invalid Service !")
2022-09-12 00:53:15 +02:00
def handle_cfdp_procedure(self, info: ProcedureWrapper):
2022-09-09 14:58:34 +02:00
cfdp_procedure = info.to_cfdp_procedure()
if cfdp_procedure.cfdp_request_type == CfdpRequestType.PUT:
2022-09-13 11:59:29 +02:00
if (
not self.cfdp_in_ccsds_wrapper.handler.put_request_pending()
and not self.cfdp_handler_started
):
2022-09-12 09:39:37 +02:00
put_req = cfdp_procedure.request_wrapper.to_put_request()
put_req.cfg.destination_id = self.cfdp_dest_id
2022-09-14 17:35:33 +02:00
LOGGER.info(
f"CFDP: Starting file put request with parameters:\n{put_req}"
)
2022-09-14 19:01:47 +02:00
self.cfdp_in_ccsds_wrapper.handler.cfdp_handler.put_request(put_req)
2022-09-13 11:59:29 +02:00
self.cfdp_handler_started = True
2022-09-15 14:31:13 +02:00
for source_pair, dest_pair in self.cfdp_in_ccsds_wrapper.handler:
pdu, sp = source_pair
pdu = cast(PduHolder, pdu)
if pdu.is_file_directive:
2022-09-15 18:46:28 +02:00
if pdu.pdu_directive_type == DirectiveType.METADATA_PDU:
2022-09-15 14:31:13 +02:00
metadata = pdu.to_metadata_pdu()
self.queue_helper.add_log_cmd(
f"CFDP Source: Sending Metadata PDU for file with size "
f"{metadata.file_size}"
)
2022-09-15 18:46:28 +02:00
elif pdu.pdu_directive_type == DirectiveType.EOF_PDU:
2022-09-15 16:28:58 +02:00
self.queue_helper.add_log_cmd(
f"CFDP Source: Sending EOF PDU"
)
2022-09-15 14:31:13 +02:00
else:
fd_pdu = pdu.to_file_data_pdu()
self.queue_helper.add_log_cmd(
f"CFDP Source: Sending File Data PDU for segment at offset "
f"{fd_pdu.offset} with length {len(fd_pdu.file_data)}"
)
self.queue_helper.add_ccsds_tc(sp)
self.cfdp_in_ccsds_wrapper.handler.confirm_source_packet_sent()
2022-09-14 17:40:39 +02:00
self.cfdp_in_ccsds_wrapper.handler.source_handler.state_machine()
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:
self.handle_tc_send_cb(params)
elif params.entry.entry_type == TcQueueEntryType.CCSDS_TC:
cfdp_packet_in_ccsds = params.entry.to_space_packet_entry()
params.com_if.send(cfdp_packet_in_ccsds.space_packet.pack())
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)
def handle_tc_send_cb(self, params: SendCbParams):
pus_tc_wrapper = params.entry.to_pus_tc_entry()
if (
pus_tc_wrapper.pus_tc.service == PusServices.S11_TC_SCHED
and pus_tc_wrapper.pus_tc.subservice == Pus11Subservices.TC_INSERT
):
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}")
raw_tc = pus_tc_wrapper.pus_tc.pack()
self.raw_logger.log_tc(pus_tc_wrapper.pus_tc)
tc_info_string = f"Sending {pus_tc_wrapper.pus_tc}"
LOGGER.info(tc_info_string)
self.file_logger.info(f"{get_current_time_string(True)}: {tc_info_string}")
params.com_if.send(raw_tc)
2022-07-03 20:58:32 +02:00
2022-09-12 00:53:15 +02:00
def queue_finished_cb(self, info: ProcedureWrapper):
2022-09-14 17:54:57 +02:00
if info is not None:
if 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}"
)
elif info.proc_type == TcProcedureType.CFDP:
LOGGER.info(f"Finished CFDP queue")
2022-10-17 12:26:00 +02:00
self.cfdp_sending_done = True
2022-07-03 20:58:32 +02:00
2022-09-14 18:19:56 +02:00
def setup_params(hook_obj: TmTcCfgHookBase) -> SetupWrapper:
2022-07-03 20:58:32 +02:00
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()
2022-09-09 17:41:44 +02:00
parser_wrapper = PreArgsParsingWrapper()
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-09-09 17:41:44 +02:00
post_arg_parsing_wrapper = parser_wrapper.parse(hook_obj)
tmtccmd.init_printout(post_arg_parsing_wrapper.use_gui)
use_prompts = not post_arg_parsing_wrapper.use_gui
proc_param_wrapper = ProcedureParamsWrapper()
2022-09-09 17:41:44 +02:00
if use_prompts:
post_arg_parsing_wrapper.set_params_with_prompts(params, proc_param_wrapper)
else:
post_arg_parsing_wrapper.set_params_without_prompts(params, proc_param_wrapper)
2022-09-09 14:58:34 +02:00
params.apid = EXAMPLE_PUS_APID
setup_wrapper = SetupWrapper(
hook_obj=hook_obj, setup_params=params, proc_param_wrapper=proc_param_wrapper
)
2022-07-03 20:58:32 +02:00
return setup_wrapper
2022-09-15 16:28:58 +02:00
def setup_cfdp_handler() -> CfdpInCcsdsWrapper:
2022-09-09 17:41:44 +02:00
fh_base = ExampleCfdpFaultHandler()
2022-09-09 16:55:04 +02:00
cfdp_cfg = LocalEntityCfg(
local_entity_id=CFDP_LOCAL_ENTITY_ID,
indication_cfg=IndicationCfg(),
default_fault_handlers=fh_base,
)
remote_cfg = RemoteEntityCfg(
closure_requested=False,
entity_id=CFDP_REMOTE_ENTITY_ID,
max_file_segment_len=1024,
check_limit=None,
crc_on_transmission=False,
2022-09-15 18:46:28 +02:00
crc_type=ChecksumType.CRC_32,
default_transmission_mode=TransmissionMode.UNACKNOWLEDGED,
2022-09-09 16:55:04 +02:00
)
cfdp_seq_count_provider = FileSeqCountProvider(
2022-09-09 17:41:44 +02:00
max_bit_width=16, file_name=Path("seqcnt_cfdp_transaction.txt")
2022-09-09 16:55:04 +02:00
)
cfdp_ccsds_seq_count_provider = PusFileSeqCountProvider(
2022-09-09 17:41:44 +02:00
file_name=Path("seqcnt_cfdp_ccsds_.txt")
2022-09-09 16:55:04 +02:00
)
cfdp_user = ExampleCfdpUser()
2022-09-14 19:01:47 +02:00
cfdp_in_ccsds_handler = CfdpInCcsdsHandler(
2022-09-09 16:55:04 +02:00
cfg=cfdp_cfg,
remote_cfgs=[remote_cfg],
ccsds_apid=EXAMPLE_CFDP_APID,
ccsds_seq_cnt_provider=cfdp_ccsds_seq_count_provider,
cfdp_seq_cnt_provider=cfdp_seq_count_provider,
user=cfdp_user,
)
2022-09-15 16:28:58 +02:00
return CfdpInCcsdsWrapper(cfdp_in_ccsds_handler)
2022-09-09 16:55:04 +02:00
2022-09-15 16:28:58 +02:00
def setup_tmtc_handlers(
verif_wrapper: VerificationWrapper,
printer: FsfwTmTcPrinter,
raw_logger: RawTmtcTimedLogWrapper,
) -> (CcsdsTmHandler, TcHandler):
cfdp_in_ccsds_wrapper = setup_cfdp_handler()
2022-07-03 20:58:32 +02:00
pus_handler = PusHandler(
2022-09-09 15:06:58 +02:00
printer=printer, raw_logger=raw_logger, wrapper=verif_wrapper
2022-07-03 20:58:32 +02:00
)
ccsds_handler = CcsdsTmHandler(None)
ccsds_handler.add_apid_handler(pus_handler)
2022-09-15 16:28:58 +02:00
ccsds_handler.add_apid_handler(cfdp_in_ccsds_wrapper)
2022-07-03 20:58:32 +02:00
tc_handler = TcHandler(
file_logger=printer.file_logger,
raw_logger=raw_logger,
pus_verificator=verif_wrapper.pus_verificator,
2022-09-09 16:55:04 +02:00
seq_count_provider=PusFileSeqCountProvider(
2022-09-09 17:41:44 +02:00
file_name=Path("seqcnt_pus_ccsds.txt")
2022-09-09 16:55:04 +02:00
),
2022-09-14 19:01:47 +02:00
cfdp_in_ccsds_wrapper=cfdp_in_ccsds_wrapper,
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,
2022-09-14 18:19:56 +02:00
) -> CcsdsTmtcBackend:
2022-09-12 09:39:37 +02:00
init_proc = params_to_procedure_conversion(setup_wrapper.proc_param_wrapper)
2022-05-18 23:40:13 +02:00
tmtc_backend = tmtccmd.create_default_tmtc_backend(
setup_wrapper=setup_wrapper,
tm_handler=ccsds_handler,
tc_handler=tc_handler,
2022-09-14 19:01:47 +02:00
init_procedure=init_proc,
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)
2022-09-14 18:19:56 +02:00
return cast(CcsdsTmtcBackend, tmtc_backend)