2022-11-20 18:07:38 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Example client for the sat-rs example application"""
|
2023-02-04 13:52:39 +01:00
|
|
|
import logging
|
2022-11-20 18:07:38 +01:00
|
|
|
import sys
|
|
|
|
import time
|
2024-04-04 15:18:53 +02:00
|
|
|
from typing import Any, Optional
|
2024-01-31 12:28:45 +01:00
|
|
|
from prompt_toolkit.history import History
|
|
|
|
from prompt_toolkit.history import FileHistory
|
2022-11-20 18:07:38 +01:00
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
from spacepackets.ccsds import PacketId, PacketType
|
2022-11-20 18:07:38 +01:00
|
|
|
import tmtccmd
|
2023-02-16 01:02:14 +01:00
|
|
|
from spacepackets.ecss import PusTelemetry, PusVerificator
|
2022-11-20 18:07:38 +01:00
|
|
|
from spacepackets.ecss.pus_17_test import Service17Tm
|
|
|
|
from spacepackets.ecss.pus_1_verification import UnpackParams, Service1Tm
|
2023-01-30 13:49:37 +01:00
|
|
|
from spacepackets.ccsds.time import CdsShortTimestamp
|
2022-11-20 18:07:38 +01:00
|
|
|
|
2024-01-31 12:28:45 +01:00
|
|
|
from tmtccmd import TcHandlerBase, ProcedureParamsWrapper
|
2022-11-20 18:07:38 +01:00
|
|
|
from tmtccmd.core.base import BackendRequest
|
|
|
|
from tmtccmd.pus import VerificationWrapper
|
2024-04-04 15:18:53 +02:00
|
|
|
from tmtccmd.tmtc import CcsdsTmHandler, GenericApidHandlerBase
|
2023-01-30 13:49:37 +01:00
|
|
|
from tmtccmd.com import ComInterface
|
2022-11-20 18:07:38 +01:00
|
|
|
from tmtccmd.config import (
|
2024-01-31 12:28:45 +01:00
|
|
|
CmdTreeNode,
|
2022-11-20 18:07:38 +01:00
|
|
|
default_json_path,
|
|
|
|
SetupParams,
|
2023-02-04 13:52:39 +01:00
|
|
|
HookBase,
|
2022-11-20 18:07:38 +01:00
|
|
|
params_to_procedure_conversion,
|
|
|
|
)
|
|
|
|
from tmtccmd.config import PreArgsParsingWrapper, SetupWrapper
|
2023-02-04 13:52:39 +01:00
|
|
|
from tmtccmd.logging import add_colorlog_console_logger
|
2022-11-20 18:07:38 +01:00
|
|
|
from tmtccmd.logging.pus import (
|
|
|
|
RegularTmtcLogWrapper,
|
|
|
|
RawTmtcTimedLogWrapper,
|
|
|
|
TimedLogWhen,
|
|
|
|
)
|
2023-11-11 16:33:43 +01:00
|
|
|
from tmtccmd.tmtc import (
|
2022-11-20 18:07:38 +01:00
|
|
|
TcQueueEntryType,
|
|
|
|
ProcedureWrapper,
|
|
|
|
TcProcedureType,
|
|
|
|
FeedWrapper,
|
|
|
|
SendCbParams,
|
|
|
|
DefaultPusQueueHelper,
|
2023-02-04 13:52:39 +01:00
|
|
|
QueueWrapper,
|
2022-11-20 18:07:38 +01:00
|
|
|
)
|
2024-01-31 12:28:45 +01:00
|
|
|
from spacepackets.seqcount import FileSeqCountProvider, PusFileSeqCountProvider
|
2022-11-20 18:07:38 +01:00
|
|
|
from tmtccmd.util.obj_id import ObjectIdDictT
|
|
|
|
|
|
|
|
|
2023-02-16 01:02:14 +01:00
|
|
|
import pus_tc
|
2024-04-04 15:18:53 +02:00
|
|
|
from common import Apid, EventU32
|
2022-11-20 18:07:38 +01:00
|
|
|
|
2023-07-02 17:47:47 +02:00
|
|
|
_LOGGER = logging.getLogger()
|
2022-11-20 18:07:38 +01:00
|
|
|
|
|
|
|
|
2023-02-04 13:52:39 +01:00
|
|
|
class SatRsConfigHook(HookBase):
|
2022-11-20 18:07:38 +01:00
|
|
|
def __init__(self, json_cfg_path: str):
|
|
|
|
super().__init__(json_cfg_path=json_cfg_path)
|
|
|
|
|
2024-01-31 12:28:45 +01:00
|
|
|
def get_communication_interface(self, com_if_key: str) -> Optional[ComInterface]:
|
2023-01-30 13:49:37 +01:00
|
|
|
from tmtccmd.config.com import (
|
2022-11-20 18:07:38 +01:00
|
|
|
create_com_interface_default,
|
|
|
|
create_com_interface_cfg_default,
|
|
|
|
)
|
|
|
|
|
2024-01-31 12:28:45 +01:00
|
|
|
assert self.cfg_path is not None
|
2024-04-04 15:18:53 +02:00
|
|
|
packet_id_list = []
|
|
|
|
for apid in Apid:
|
|
|
|
packet_id_list.append(PacketId(PacketType.TM, True, apid))
|
2022-11-20 18:07:38 +01:00
|
|
|
cfg = create_com_interface_cfg_default(
|
|
|
|
com_if_key=com_if_key,
|
|
|
|
json_cfg_path=self.cfg_path,
|
2024-04-04 15:18:53 +02:00
|
|
|
space_packet_ids=packet_id_list,
|
2022-11-20 18:07:38 +01:00
|
|
|
)
|
2024-01-31 12:28:45 +01:00
|
|
|
assert cfg is not None
|
2022-11-20 18:07:38 +01:00
|
|
|
return create_com_interface_default(cfg)
|
|
|
|
|
2024-01-31 12:28:45 +01:00
|
|
|
def get_command_definitions(self) -> CmdTreeNode:
|
|
|
|
"""This function should return the root node of the command definition tree."""
|
|
|
|
return pus_tc.create_cmd_definition_tree()
|
2022-11-20 18:07:38 +01:00
|
|
|
|
2024-01-31 12:28:45 +01:00
|
|
|
def get_cmd_history(self) -> Optional[History]:
|
|
|
|
"""Optionlly return a history class for the past command paths which will be used
|
|
|
|
when prompting a command path from the user in CLI mode."""
|
|
|
|
return FileHistory(".tmtc-history.txt")
|
2022-11-20 18:07:38 +01:00
|
|
|
|
|
|
|
def get_object_ids(self) -> ObjectIdDictT:
|
|
|
|
from tmtccmd.config.objects import get_core_object_ids
|
|
|
|
|
|
|
|
return get_core_object_ids()
|
|
|
|
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
class PusHandler(GenericApidHandlerBase):
|
2022-11-20 18:07:38 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-07-02 17:47:47 +02:00
|
|
|
file_logger: logging.Logger,
|
2022-11-20 18:07:38 +01:00
|
|
|
verif_wrapper: VerificationWrapper,
|
|
|
|
raw_logger: RawTmtcTimedLogWrapper,
|
|
|
|
):
|
2024-04-04 15:18:53 +02:00
|
|
|
super().__init__(None)
|
2023-07-02 17:47:47 +02:00
|
|
|
self.file_logger = file_logger
|
2022-11-20 18:07:38 +01:00
|
|
|
self.raw_logger = raw_logger
|
|
|
|
self.verif_wrapper = verif_wrapper
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
def handle_tm(self, apid: int, packet: bytes, _user_args: Any):
|
2022-11-20 18:07:38 +01:00
|
|
|
try:
|
2024-04-23 14:22:50 +02:00
|
|
|
pus_tm = PusTelemetry.unpack(
|
|
|
|
packet, timestamp_len=CdsShortTimestamp.TIMESTAMP_SIZE
|
|
|
|
)
|
2022-11-20 18:45:11 +01:00
|
|
|
except ValueError as e:
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.warning("Could not generate PUS TM object from raw data")
|
|
|
|
_LOGGER.warning(f"Raw Packet: [{packet.hex(sep=',')}], REPR: {packet!r}")
|
2022-11-20 18:45:11 +01:00
|
|
|
raise e
|
2024-01-31 12:28:45 +01:00
|
|
|
service = pus_tm.service
|
2022-11-20 18:07:38 +01:00
|
|
|
if service == 1:
|
2023-02-04 13:52:39 +01:00
|
|
|
tm_packet = Service1Tm.unpack(
|
2024-04-23 14:22:50 +02:00
|
|
|
data=packet, params=UnpackParams(CdsShortTimestamp.TIMESTAMP_SIZE, 1, 2)
|
2023-02-04 13:52:39 +01:00
|
|
|
)
|
2022-11-20 18:07:38 +01:00
|
|
|
res = self.verif_wrapper.add_tm(tm_packet)
|
|
|
|
if res is None:
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info(
|
2022-11-20 18:07:38 +01:00
|
|
|
f"Received Verification TM[{tm_packet.service}, {tm_packet.subservice}] "
|
|
|
|
f"with Request ID {tm_packet.tc_req_id.as_u32():#08x}"
|
|
|
|
)
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.warning(
|
2022-11-20 18:07:38 +01:00
|
|
|
f"No matching telecommand found for {tm_packet.tc_req_id}"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.verif_wrapper.log_to_console(tm_packet, res)
|
|
|
|
self.verif_wrapper.log_to_file(tm_packet, res)
|
2024-01-31 12:28:45 +01:00
|
|
|
elif service == 3:
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info("No handling for HK packets implemented")
|
|
|
|
_LOGGER.info(f"Raw packet: 0x[{packet.hex(sep=',')}]")
|
2024-04-23 14:22:50 +02:00
|
|
|
pus_tm = PusTelemetry.unpack(
|
|
|
|
packet, timestamp_len=CdsShortTimestamp.TIMESTAMP_SIZE
|
|
|
|
)
|
2022-12-22 09:15:59 +01:00
|
|
|
if pus_tm.subservice == 25:
|
|
|
|
if len(pus_tm.source_data) < 8:
|
|
|
|
raise ValueError("No addressable ID in HK packet")
|
|
|
|
json_str = pus_tm.source_data[8:]
|
2023-11-11 16:36:13 +01:00
|
|
|
_LOGGER.info(json_str)
|
2024-01-31 12:28:45 +01:00
|
|
|
elif service == 5:
|
2023-02-16 01:02:14 +01:00
|
|
|
tm_packet = PusTelemetry.unpack(
|
2024-04-23 14:22:50 +02:00
|
|
|
packet, timestamp_len=CdsShortTimestamp.TIMESTAMP_SIZE
|
2023-02-16 01:02:14 +01:00
|
|
|
)
|
|
|
|
src_data = tm_packet.source_data
|
|
|
|
event_u32 = EventU32.unpack(src_data)
|
|
|
|
_LOGGER.info(f"Received event packet. Event: {event_u32}")
|
|
|
|
if event_u32.group_id == 0 and event_u32.unique_id == 0:
|
|
|
|
_LOGGER.info("Received test event")
|
2024-01-31 12:28:45 +01:00
|
|
|
elif service == 17:
|
2023-02-04 13:52:39 +01:00
|
|
|
tm_packet = Service17Tm.unpack(
|
2024-04-23 14:22:50 +02:00
|
|
|
packet, timestamp_len=CdsShortTimestamp.TIMESTAMP_SIZE
|
2023-02-04 13:52:39 +01:00
|
|
|
)
|
2022-11-20 18:07:38 +01:00
|
|
|
if tm_packet.subservice == 2:
|
2023-07-02 17:47:47 +02:00
|
|
|
self.file_logger.info("Received Ping Reply TM[17,2]")
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info("Received Ping Reply TM[17,2]")
|
2022-11-20 18:07:38 +01:00
|
|
|
else:
|
2023-07-02 17:47:47 +02:00
|
|
|
self.file_logger.info(
|
2022-11-20 18:07:38 +01:00
|
|
|
f"Received Test Packet with unknown subservice {tm_packet.subservice}"
|
|
|
|
)
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info(
|
2022-11-20 18:07:38 +01:00
|
|
|
f"Received Test Packet with unknown subservice {tm_packet.subservice}"
|
|
|
|
)
|
2024-01-31 12:28:45 +01:00
|
|
|
else:
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info(
|
2022-11-20 18:07:38 +01:00
|
|
|
f"The service {service} is not implemented in Telemetry Factory"
|
|
|
|
)
|
2023-02-04 13:52:39 +01:00
|
|
|
tm_packet = PusTelemetry.unpack(
|
2024-04-23 14:22:50 +02:00
|
|
|
packet, timestamp_len=CdsShortTimestamp.TIMESTAMP_SIZE
|
2023-02-04 13:52:39 +01:00
|
|
|
)
|
2024-01-31 12:28:45 +01:00
|
|
|
self.raw_logger.log_tm(pus_tm)
|
2022-11-20 18:07:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TcHandler(TcHandlerBase):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
seq_count_provider: FileSeqCountProvider,
|
|
|
|
verif_wrapper: VerificationWrapper,
|
|
|
|
):
|
|
|
|
super(TcHandler, self).__init__()
|
|
|
|
self.seq_count_provider = seq_count_provider
|
|
|
|
self.verif_wrapper = verif_wrapper
|
|
|
|
self.queue_helper = DefaultPusQueueHelper(
|
2023-02-04 13:52:39 +01:00
|
|
|
queue_wrapper=QueueWrapper.empty(),
|
2023-01-30 13:49:37 +01:00
|
|
|
tc_sched_timestamp_len=CdsShortTimestamp.TIMESTAMP_SIZE,
|
2022-11-20 18:07:38 +01:00
|
|
|
seq_cnt_provider=seq_count_provider,
|
2023-01-30 13:49:37 +01:00
|
|
|
pus_verificator=self.verif_wrapper.pus_verificator,
|
2024-04-04 15:18:53 +02:00
|
|
|
default_pus_apid=None,
|
2022-11-20 18:07:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def send_cb(self, send_params: SendCbParams):
|
|
|
|
entry_helper = send_params.entry
|
|
|
|
if entry_helper.is_tc:
|
|
|
|
if entry_helper.entry_type == TcQueueEntryType.PUS_TC:
|
|
|
|
pus_tc_wrapper = entry_helper.to_pus_tc_entry()
|
|
|
|
raw_tc = pus_tc_wrapper.pus_tc.pack()
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info(f"Sending {pus_tc_wrapper.pus_tc}")
|
2022-11-20 18:07:38 +01:00
|
|
|
send_params.com_if.send(raw_tc)
|
|
|
|
elif entry_helper.entry_type == TcQueueEntryType.LOG:
|
|
|
|
log_entry = entry_helper.to_log_entry()
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info(log_entry.log_str)
|
2022-11-20 18:07:38 +01:00
|
|
|
|
2024-01-31 12:28:45 +01:00
|
|
|
def queue_finished_cb(self, info: ProcedureWrapper):
|
2024-04-23 14:22:50 +02:00
|
|
|
if info.proc_type == TcProcedureType.TREE_COMMANDING:
|
|
|
|
def_proc = info.to_tree_commanding_procedure()
|
2024-01-31 12:28:45 +01:00
|
|
|
_LOGGER.info(f"Queue handling finished for command {def_proc.cmd_path}")
|
2022-11-20 18:07:38 +01:00
|
|
|
|
2024-01-31 12:28:45 +01:00
|
|
|
def feed_cb(self, info: ProcedureWrapper, wrapper: FeedWrapper):
|
2022-12-21 10:01:19 +01:00
|
|
|
q = self.queue_helper
|
|
|
|
q.queue_wrapper = wrapper.queue_wrapper
|
2024-04-23 14:22:50 +02:00
|
|
|
if info.proc_type == TcProcedureType.TREE_COMMANDING:
|
|
|
|
def_proc = info.to_tree_commanding_procedure()
|
2024-01-31 12:28:45 +01:00
|
|
|
assert def_proc.cmd_path is not None
|
|
|
|
pus_tc.pack_pus_telecommands(q, def_proc.cmd_path)
|
2022-11-20 18:07:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-02-04 13:52:39 +01:00
|
|
|
add_colorlog_console_logger(_LOGGER)
|
2022-11-20 18:07:38 +01:00
|
|
|
tmtccmd.init_printout(False)
|
|
|
|
hook_obj = SatRsConfigHook(json_cfg_path=default_json_path())
|
|
|
|
parser_wrapper = PreArgsParsingWrapper()
|
|
|
|
parser_wrapper.create_default_parent_parser()
|
|
|
|
parser_wrapper.create_default_parser()
|
|
|
|
parser_wrapper.add_def_proc_args()
|
|
|
|
params = SetupParams()
|
2023-02-04 13:52:39 +01:00
|
|
|
post_args_wrapper = parser_wrapper.parse(hook_obj, params)
|
2022-11-20 18:07:38 +01:00
|
|
|
proc_wrapper = ProcedureParamsWrapper()
|
|
|
|
if post_args_wrapper.use_gui:
|
2023-02-04 13:52:39 +01:00
|
|
|
post_args_wrapper.set_params_without_prompts(proc_wrapper)
|
2022-11-20 18:07:38 +01:00
|
|
|
else:
|
2023-02-04 13:52:39 +01:00
|
|
|
post_args_wrapper.set_params_with_prompts(proc_wrapper)
|
2022-11-20 18:07:38 +01:00
|
|
|
setup_args = SetupWrapper(
|
|
|
|
hook_obj=hook_obj, setup_params=params, proc_param_wrapper=proc_wrapper
|
|
|
|
)
|
|
|
|
# Create console logger helper and file loggers
|
|
|
|
tmtc_logger = RegularTmtcLogWrapper()
|
2023-07-02 17:47:47 +02:00
|
|
|
file_logger = tmtc_logger.logger
|
2022-11-20 18:07:38 +01:00
|
|
|
raw_logger = RawTmtcTimedLogWrapper(when=TimedLogWhen.PER_HOUR, interval=1)
|
|
|
|
verificator = PusVerificator()
|
2023-07-02 17:47:47 +02:00
|
|
|
verification_wrapper = VerificationWrapper(verificator, _LOGGER, file_logger)
|
2022-11-20 18:07:38 +01:00
|
|
|
# Create primary TM handler and add it to the CCSDS Packet Handler
|
2023-07-02 17:47:47 +02:00
|
|
|
tm_handler = PusHandler(file_logger, verification_wrapper, raw_logger)
|
2024-04-04 15:18:53 +02:00
|
|
|
ccsds_handler = CcsdsTmHandler(generic_handler=tm_handler)
|
|
|
|
# TODO: We could add the CFDP handler for the CFDP APID at a later stage.
|
|
|
|
# ccsds_handler.add_apid_handler(tm_handler)
|
2022-11-20 18:07:38 +01:00
|
|
|
|
|
|
|
# Create TC handler
|
|
|
|
seq_count_provider = PusFileSeqCountProvider()
|
|
|
|
tc_handler = TcHandler(seq_count_provider, verification_wrapper)
|
|
|
|
tmtccmd.setup(setup_args=setup_args)
|
|
|
|
init_proc = params_to_procedure_conversion(setup_args.proc_param_wrapper)
|
|
|
|
tmtc_backend = tmtccmd.create_default_tmtc_backend(
|
|
|
|
setup_wrapper=setup_args,
|
|
|
|
tm_handler=ccsds_handler,
|
|
|
|
tc_handler=tc_handler,
|
|
|
|
init_procedure=init_proc,
|
|
|
|
)
|
|
|
|
tmtccmd.start(tmtc_backend=tmtc_backend, hook_obj=hook_obj)
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
state = tmtc_backend.periodic_op(None)
|
|
|
|
if state.request == BackendRequest.TERMINATION_NO_ERROR:
|
2024-04-22 20:29:14 +02:00
|
|
|
tmtc_backend.close_com_if()
|
2022-11-20 18:07:38 +01:00
|
|
|
sys.exit(0)
|
|
|
|
elif state.request == BackendRequest.DELAY_IDLE:
|
2023-02-04 13:52:39 +01:00
|
|
|
_LOGGER.info("TMTC Client in IDLE mode")
|
2022-11-20 18:07:38 +01:00
|
|
|
time.sleep(3.0)
|
|
|
|
elif state.request == BackendRequest.DELAY_LISTENER:
|
|
|
|
time.sleep(0.8)
|
|
|
|
elif state.request == BackendRequest.DELAY_CUSTOM:
|
2022-11-20 18:45:11 +01:00
|
|
|
if state.next_delay.total_seconds() <= 0.4:
|
|
|
|
time.sleep(state.next_delay.total_seconds())
|
|
|
|
else:
|
|
|
|
time.sleep(0.4)
|
2022-11-20 18:07:38 +01:00
|
|
|
elif state.request == BackendRequest.CALL_NEXT:
|
|
|
|
pass
|
|
|
|
except KeyboardInterrupt:
|
2024-04-22 20:29:14 +02:00
|
|
|
tmtc_backend.close_com_if()
|
2022-11-20 18:07:38 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|