eive-tmtc/eive_tmtc/tmtc/core.py

725 lines
26 KiB
Python
Raw Normal View History

2021-08-03 15:28:28 +02:00
import enum
2023-02-01 11:17:04 +01:00
import logging
2023-04-16 02:56:14 +02:00
import os
2023-01-11 14:19:47 +01:00
import struct
2023-04-16 02:56:14 +02:00
from pathlib import Path
2023-10-19 15:01:23 +02:00
from typing import Tuple
2023-01-11 14:19:47 +01:00
2022-09-26 22:25:47 +02:00
from spacepackets.ecss import PusTelecommand
2024-03-06 11:02:10 +01:00
from tmtccmd.config import CmdTreeNode
2022-07-04 17:59:09 +02:00
2023-11-10 19:23:06 +01:00
from tmtccmd.tmtc import DefaultPusQueueHelper
from tmtccmd.pus.s8_fsfw_action import create_action_cmd
from tmtccmd.pus.tc.s3_fsfw_hk import make_sid, generate_one_hk_command
from tmtccmd.pus.s20_fsfw_param import (
2023-04-12 18:51:38 +02:00
create_scalar_u8_parameter,
create_load_param_cmd,
)
2023-05-25 11:31:06 +02:00
from tmtccmd.fsfw.tmtc_printer import FsfwTmTcPrinter
from tmtccmd.pus.s11_tc_sched import (
create_enable_tc_sched_cmd,
create_disable_tc_sched_cmd,
)
2022-11-29 16:53:29 +01:00
from eive_tmtc.config.object_ids import CORE_CONTROLLER_ID
from eive_tmtc.pus_tm.defs import PrintWrapper
2022-07-05 02:12:54 +02:00
2023-02-01 11:17:04 +01:00
_LOGGER = logging.getLogger(__name__)
2021-08-03 15:28:28 +02:00
2023-07-21 11:47:37 +02:00
class SdState(enum.IntEnum):
OFF = 0
ON = 1
MOUNTED = 2
class SdCardSelect(enum.IntEnum):
SD_0 = 0
SD_1 = 1
BOTH = 2
NONE = 3
class ActionId(enum.IntEnum):
2023-02-08 11:21:00 +01:00
ANNOUNCE_VERSION = 1
2023-02-08 11:51:55 +01:00
ANNOUNCE_CURRENT_IMAGE = 2
ANNOUNCE_BOOT_COUNTS = 3
2022-02-24 10:56:36 +01:00
SWITCH_REBOOT_FILE_HANDLING = 5
2022-09-26 22:25:47 +02:00
RESET_REBOOT_COUNTER = 6
SWITCH_IMG_LOCK = 7
SET_MAX_REBOOT_CNT = 8
2023-06-22 14:40:46 +02:00
READ_REBOOT_MECHANISM_INFO = 9
2022-09-26 22:25:47 +02:00
UPDATE_OBSW_FROM_SD_0 = 10
UPDATE_OBSW_FROM_SD_1 = 11
UPDATE_OBSW_FROM_TMP = 12
2022-09-27 12:05:36 +02:00
SWITCH_TO_SD_0 = 16
SWITCH_TO_SD_1 = 17
SWITCH_TO_BOTH_SD_CARDS = 18
2023-10-19 15:01:23 +02:00
AUTO_SWITCH_ENABLE = 19
AUTO_SWITCH_DISABLE = 20
2022-05-24 01:13:21 +02:00
XSC_REBOOT = 32
FULL_REBOOT = 34
2023-04-14 00:06:22 +02:00
EXECUTE_SHELL_CMD_BLOCKING = 40
EXECUTE_SHELL_CMD_NON_BLOCKING = 41
2023-04-14 00:06:22 +02:00
SYSTEMCTL_CMD_EXECUTOR = 42
2023-04-15 20:51:08 +02:00
LIST_DIR_INTO_FILE = 50
LIST_DIR_DUMP_DIRECTLY = 51
CP_HELPER = 52
MV_HELPER = 53
RM_HELPER = 54
2023-04-15 21:53:11 +02:00
MKDIR_HELPER = 55
ENABLE_SCHEDULER = 56
2024-03-05 11:43:18 +01:00
UPDATE_LEAP_SECONRS = 60
2021-08-03 15:28:28 +02:00
2023-04-12 18:51:38 +02:00
class ParamId(enum.IntEnum):
PREF_SD = 0
class SetId(enum.IntEnum):
HK = 5
2022-03-13 21:35:12 +01:00
class OpCode:
2023-02-08 11:51:55 +01:00
ANNOUNCE_VERSION = "announce_version"
ANNOUNCE_CURRENT_IMAGE = "announce_current_image"
ANNOUNCE_BOOT_COUNTS = "announce_boot_counts"
EXECUTE_SHELL_CMD_BLOCKING = "exec_cmd_blocking"
EXECUTE_SHELL_CMD_NON_BLOCKING = "exec_cmd_non_blocking"
2023-04-14 00:06:22 +02:00
SYSTEMCTL_CMD_EXECUTOR = "systemctl_cmd"
2023-04-15 20:51:08 +02:00
LIST_DIR_INTO_FILE = "list_dir_into_file"
LIST_DIR_DUMP_DIRECTLY = "list_dir_dump_directly"
CP_HELPER = "cp_helper"
MV_HELPER = "mv_helper"
RM_HELPER = "rm_helper"
2023-04-15 21:53:11 +02:00
MKDIR_HELPER = "mkdir_helper"
2023-04-12 18:51:38 +02:00
SET_PREF_SD = "set_pref_sd"
2023-11-22 16:40:27 +01:00
REBOOT_XSC = "reboot_xsc"
XSC_REBOOT_SELF = "reboot_self"
XSC_REBOOT_0_0 = "reboot_00"
XSC_REBOOT_0_1 = "reboot_01"
XSC_REBOOT_1_0 = "reboot_10"
XSC_REBOOT_1_1 = "reboot_11"
REBOOT_FULL = "reboot_regular"
GET_HK = "get_hk"
OBSW_UPDATE_FROM_SD_0 = "obsw_update_sd0"
OBSW_UPDATE_FROM_SD_1 = "obsw_update_sd1"
OBSW_UPDATE_FROM_TMP = "obsw_update_tmp"
SWITCH_TO_SD_0 = "switch_to_sd_0"
SWITCH_TO_SD_1 = "switch_to_sd_1"
SWITCH_TO_BOTH_SD_CARDS = "switch_to_both_sd_cards"
2023-06-22 14:40:46 +02:00
READ_REBOOT_MECHANISM_INFO = "rbh_info"
ENABLE_REBOOT_FILE_HANDLING = "rwd_on"
DISABLE_REBOOT_FILE_HANDLING = "rwd_off"
RESET_ALL_REBOOT_COUNTERS = "rwd_reset_a"
RWD_RESET_REBOOT_COUNTER_00 = "rwd_reset_00"
RWD_RESET_REBOOT_COUNTER_01 = "rwd_reset_01"
RWD_RESET_REBOOT_COUNTER_10 = "rwd_reset_10"
RWD_RESET_REBOOT_COUNTER_11 = "rwd_reset_11"
RWD_SET_MAX_REBOOT_CNT = "rwd_max_cnt"
2023-10-19 15:01:23 +02:00
AUTO_SWITCH_ENABLE = "auto_switch_enable"
AUTO_SWITCH_DISABLE = "auto_switch_disable"
ENABLE_SCHEDULER = "enable_scheduler"
DISABLE_SCHEDULER = "disable_scheduler"
2024-03-05 11:43:18 +01:00
UPDATE_LEAP_SECONDS = "leap_seconds_update"
2022-05-24 01:13:21 +02:00
class Info:
2023-02-08 11:21:00 +01:00
ANNOUNCE_VERSION = "Announce version"
ANNOUNCE_CURRENT_IMAGE = "Announce current image"
ANNOUNCE_BOOT_COUNTS = "Announce boot counts"
2023-04-14 00:06:22 +02:00
SYSTEMCTL_CMD_EXECUTOR = "Perform systemctl command"
EXECUTE_SHELL_CMD_BLOCKING = "Execute shell command blocking"
EXECUTE_SHELL_CMD_NON_BLOCKING = "Execute shell command non-blocking"
2023-04-12 18:51:38 +02:00
SET_PREF_SD = "Set preferred SD card"
2022-05-24 01:13:21 +02:00
REBOOT_XSC = "XSC reboot with prompt"
REBOOT_FULL = "Full regular reboot"
2023-11-22 16:40:27 +01:00
XSC_REBOOT_SELF = "Reboot Self"
XSC_REBOOT_0_0 = "Reboot to 0 0"
XSC_REBOOT_0_1 = "Reboot to 0 1"
XSC_REBOOT_1_0 = "Reboot to 1 0"
XSC_REBOOT_1_1 = "Reboot to 1 1"
2022-09-26 22:25:47 +02:00
OBSW_UPDATE_FROM_SD_0 = "Update OBSW from SD Card 0"
OBSW_UPDATE_FROM_SD_1 = "Update OBSW from SD Card 1"
OBSW_UPDATE_FROM_TMP = "Update OBSW from tmp folder"
2023-06-22 14:40:46 +02:00
READ_REBOOT_MECHANISM_INFO = "Read reboot mechansm information"
2022-09-27 12:05:36 +02:00
SWITCH_TO_SD_0 = "Switch to SD card 0"
SWITCH_TO_SD_1 = "Switch to SD card 1"
SWITCH_TO_BOTH_SD_CARDS = "Switch to both SD cards with specified active card"
2023-04-15 20:51:08 +02:00
LIST_DIR_INTO_FILE = "List directory, dump output into file"
LIST_DIR_DUMP_DIRECTLY = "List directory, dump content directly"
CP_HELPER = "Filesystem Copy Helper"
MV_HELPER = "Filesystem Move Helper"
RM_HELPER = "Filesystem Removal Helper"
2023-04-15 21:53:11 +02:00
MKDIR_HELPER = "Filesystem Directory Creation Helper"
2023-11-22 16:40:27 +01:00
ENABLE_REBOOT_FILE_HANDLING = "Enable reboot file handling"
DISABLE_REBOOT_FILE_HANDLING = "Disable reboot file handling"
RESET_ALL_REBOOT_COUNTERS = "Reset all reboot counters"
RWD_RESET_REBOOT_COUNTER_00 = "Reset reboot counter 0 0"
RWD_RESET_REBOOT_COUNTER_01 = "Reset reboot counter 0 0"
RWD_RESET_REBOOT_COUNTER_10 = "Reset reboot counter 1 0"
2024-01-24 17:59:16 +01:00
GET_HK = "Get HK set"
2023-11-22 16:40:27 +01:00
RWD_RESET_REBOOT_COUNTER_11 = "Reset reboot counter 1 1"
RWD_SET_MAX_REBOOT_CNT = "rwd_max_cnt"
2023-10-19 15:01:23 +02:00
AUTO_SWITCH_ENABLE = "Enable Auto-Switch Feature with a specific target image"
AUTO_SWITCH_DISABLE = "Disable Auto-Switch Feature"
ENABLE_SCHEDULER = "Enable scheduler"
DISABLE_SCHEDULER = "Disable scheduler"
2024-03-05 11:43:18 +01:00
UPDATE_LEAP_SECONDS = "Updates the Leap Seconds"
2021-08-03 15:28:28 +02:00
class Chip(enum.IntEnum):
CHIP_0 = 0
CHIP_1 = 1
NONE = 2
class Copy(enum.IntEnum):
COPY_0_NOM = 0
COPY_1_GOLD = 1
NONE = 2
2023-04-14 00:06:22 +02:00
class SystemctlCmd(enum.IntEnum):
START = 0
STOP = 1
RESTART = 2
2023-11-22 16:40:27 +01:00
def create_core_node() -> CmdTreeNode:
op_code_strs = [
getattr(OpCode, key) for key in dir(OpCode) if not key.startswith("__")
]
info_strs = [getattr(Info, key) for key in dir(OpCode) if not key.startswith("__")]
combined_dict = dict(zip(op_code_strs, info_strs))
node = CmdTreeNode("core", "Core Controller", hide_children_for_print=True)
for op_code, info in combined_dict.items():
node.add_child(CmdTreeNode(op_code, info))
return node
2023-06-19 17:16:00 +02:00
def pack_core_commands( # noqa C901
2023-11-22 10:17:05 +01:00
q: DefaultPusQueueHelper, cmd_str: str
2023-06-19 17:16:00 +02:00
): # noqa: C901 , complexity okay here
2023-11-22 10:17:05 +01:00
if cmd_str == OpCode.ANNOUNCE_VERSION:
2023-02-08 11:21:00 +01:00
q.add_log_cmd(f"{Info.ANNOUNCE_VERSION}")
q.add_pus_tc(create_action_cmd(CORE_CONTROLLER_ID, ActionId.ANNOUNCE_VERSION))
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.ANNOUNCE_CURRENT_IMAGE:
2023-02-08 11:21:00 +01:00
q.add_log_cmd(f"{Info.ANNOUNCE_CURRENT_IMAGE}")
2023-02-08 11:51:55 +01:00
q.add_pus_tc(
create_action_cmd(CORE_CONTROLLER_ID, ActionId.ANNOUNCE_CURRENT_IMAGE)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.ANNOUNCE_BOOT_COUNTS:
q.add_log_cmd(f"{Info.ANNOUNCE_BOOT_COUNTS}")
q.add_pus_tc(
create_action_cmd(CORE_CONTROLLER_ID, ActionId.ANNOUNCE_BOOT_COUNTS)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.REBOOT_XSC:
2021-08-03 15:28:28 +02:00
reboot_self, chip_select, copy_select = determine_reboot_params()
2023-01-11 14:23:03 +01:00
add_xsc_reboot_cmd(
2022-07-04 17:59:09 +02:00
q=q,
2022-01-18 14:03:56 +01:00
reboot_self=reboot_self,
chip=chip_select,
copy=copy_select,
2021-08-03 15:28:28 +02:00
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.REBOOT_FULL:
2022-07-04 17:59:09 +02:00
q.add_log_cmd(f"Core Command: {Info.REBOOT_FULL}")
q.add_pus_tc(
create_action_cmd(
object_id=CORE_CONTROLLER_ID, action_id=ActionId.FULL_REBOOT
2022-07-04 17:59:09 +02:00
)
2022-05-24 17:24:09 +02:00
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.XSC_REBOOT_SELF:
2023-01-11 14:23:03 +01:00
add_xsc_reboot_cmd(q=q, reboot_self=True)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.SYSTEMCTL_CMD_EXECUTOR:
2023-04-14 00:06:22 +02:00
print("systemctl command types: ")
2023-04-14 00:08:28 +02:00
for entry in SystemctlCmd:
print(f"{entry}: {entry.name}")
2023-04-14 19:21:51 +02:00
systemctl_cmd = SystemctlCmd(
int(input("Specify systemctl command type by key: "))
)
2023-04-14 00:06:22 +02:00
unit_name = input("Specify unit name: ")
2023-05-19 11:01:06 +02:00
q.add_pus_tc(create_systemctl_cmd(systemctl_cmd, unit_name))
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.EXECUTE_SHELL_CMD_BLOCKING:
2023-04-14 00:06:22 +02:00
custom_cmd = input("Please specify command to execute: ")
2023-04-14 19:21:51 +02:00
q.add_pus_tc(
create_action_cmd(
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.EXECUTE_SHELL_CMD_BLOCKING,
user_data=custom_cmd.encode(),
)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.EXECUTE_SHELL_CMD_NON_BLOCKING:
custom_cmd = input("Please specify command to execute: ")
2023-04-14 19:21:51 +02:00
q.add_pus_tc(
create_action_cmd(
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.EXECUTE_SHELL_CMD_NON_BLOCKING,
user_data=custom_cmd.encode(),
)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.XSC_REBOOT_0_0:
2023-01-11 14:23:03 +01:00
add_xsc_reboot_cmd(
2022-07-04 17:59:09 +02:00
q=q, reboot_self=False, chip=Chip.CHIP_0, copy=Copy.COPY_0_NOM
2021-08-03 15:28:28 +02:00
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.XSC_REBOOT_0_1:
2023-01-11 14:23:03 +01:00
add_xsc_reboot_cmd(
2022-07-04 17:59:09 +02:00
q=q,
2022-01-18 14:03:56 +01:00
reboot_self=False,
chip=Chip.CHIP_0,
copy=Copy.COPY_1_GOLD,
2021-08-03 15:28:28 +02:00
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.XSC_REBOOT_1_0:
2023-01-11 14:23:03 +01:00
add_xsc_reboot_cmd(
2022-07-04 17:59:09 +02:00
q=q, reboot_self=False, chip=Chip.CHIP_1, copy=Copy.COPY_0_NOM
2021-08-03 15:28:28 +02:00
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.XSC_REBOOT_1_1:
2023-01-11 14:23:03 +01:00
add_xsc_reboot_cmd(
2022-07-04 17:59:09 +02:00
q=q,
2022-01-18 14:03:56 +01:00
reboot_self=False,
chip=Chip.CHIP_1,
copy=Copy.COPY_1_GOLD,
2021-08-03 15:28:28 +02:00
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.READ_REBOOT_MECHANISM_INFO:
2023-06-22 14:40:46 +02:00
q.add_log_cmd(Info.READ_REBOOT_MECHANISM_INFO)
q.add_pus_tc(
create_action_cmd(
object_id=CORE_CONTROLLER_ID,
2023-06-22 14:52:28 +02:00
action_id=ActionId.READ_REBOOT_MECHANISM_INFO,
2023-06-22 14:40:46 +02:00
)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.DISABLE_REBOOT_FILE_HANDLING:
2022-07-04 17:59:09 +02:00
q.add_log_cmd("Disabling reboot file handling")
2022-09-26 22:25:47 +02:00
user_data = bytearray([0])
q.add_pus_tc(
create_action_cmd(
2022-09-26 22:25:47 +02:00
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.SWITCH_REBOOT_FILE_HANDLING,
2022-09-26 22:25:47 +02:00
user_data=user_data,
)
2022-02-24 10:56:36 +01:00
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.ENABLE_REBOOT_FILE_HANDLING:
2022-07-04 17:59:09 +02:00
q.add_log_cmd("Enabling reboot file handling")
2022-09-26 22:25:47 +02:00
user_data = bytearray([1])
q.add_pus_tc(
create_action_cmd(
2022-09-26 22:25:47 +02:00
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.SWITCH_REBOOT_FILE_HANDLING,
2022-09-26 22:25:47 +02:00
user_data=user_data,
)
2022-02-24 10:56:36 +01:00
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.RESET_ALL_REBOOT_COUNTERS:
2022-07-04 17:59:09 +02:00
q.add_log_cmd("Resetting all reboot counters")
2022-09-26 22:25:47 +02:00
q.add_pus_tc(
create_action_cmd(
2022-09-26 22:25:47 +02:00
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.RESET_REBOOT_COUNTER,
2022-09-26 22:25:47 +02:00
)
2022-02-24 10:56:36 +01:00
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.RWD_RESET_REBOOT_COUNTER_00:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 0, 0)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.RWD_RESET_REBOOT_COUNTER_01:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 0, 1)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.RWD_RESET_REBOOT_COUNTER_10:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 1, 0)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.RWD_RESET_REBOOT_COUNTER_11:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 1, 1)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.RWD_SET_MAX_REBOOT_CNT:
max_count = int(input("Set new maximum reboot threshold [1, 50]: "))
if max_count < 1 or max_count > 50:
raise ValueError("Invalid value, must be in range 1 to 50")
q.add_pus_tc(
create_action_cmd(
CORE_CONTROLLER_ID,
ActionId.SET_MAX_REBOOT_CNT,
user_data=bytes([max_count]),
)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.OBSW_UPDATE_FROM_SD_0:
2022-09-26 22:25:47 +02:00
q.add_log_cmd(Info.OBSW_UPDATE_FROM_SD_0)
q.add_pus_tc(pack_obsw_update_cmd(ActionId.UPDATE_OBSW_FROM_SD_0))
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.OBSW_UPDATE_FROM_SD_1:
2022-09-26 22:25:47 +02:00
q.add_log_cmd(Info.OBSW_UPDATE_FROM_SD_1)
q.add_pus_tc(pack_obsw_update_cmd(ActionId.UPDATE_OBSW_FROM_SD_1))
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.OBSW_UPDATE_FROM_TMP:
2022-09-26 22:25:47 +02:00
q.add_log_cmd(Info.OBSW_UPDATE_FROM_TMP)
q.add_pus_tc(pack_obsw_update_cmd(ActionId.UPDATE_OBSW_FROM_TMP))
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.AUTO_SWITCH_ENABLE:
2023-10-19 15:01:23 +02:00
q.add_log_cmd(Info.AUTO_SWITCH_ENABLE)
chip, copy = determine_chip_and_copy()
user_data = bytes([chip, copy])
q.add_pus_tc(
create_action_cmd(
CORE_CONTROLLER_ID, ActionId.AUTO_SWITCH_ENABLE, user_data
)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.AUTO_SWITCH_DISABLE:
2023-10-19 15:02:24 +02:00
q.add_log_cmd(Info.AUTO_SWITCH_DISABLE)
2023-10-19 15:01:23 +02:00
q.add_pus_tc(
create_action_cmd(CORE_CONTROLLER_ID, ActionId.AUTO_SWITCH_DISABLE)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.SWITCH_TO_SD_0:
2022-09-27 12:05:36 +02:00
q.add_log_cmd(Info.SWITCH_TO_SD_0)
q.add_pus_tc(
create_action_cmd(
object_id=CORE_CONTROLLER_ID, action_id=ActionId.SWITCH_TO_SD_0
2022-09-27 12:05:36 +02:00
)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.SWITCH_TO_SD_1:
2022-09-27 12:05:36 +02:00
q.add_log_cmd(Info.SWITCH_TO_SD_1)
q.add_pus_tc(
create_action_cmd(
object_id=CORE_CONTROLLER_ID, action_id=ActionId.SWITCH_TO_SD_1
2022-09-27 12:05:36 +02:00
)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.SWITCH_TO_BOTH_SD_CARDS:
2022-09-27 12:05:36 +02:00
while True:
2023-04-08 13:23:06 +02:00
active_sd_card = int(input("Please specify active SD card [0/1]: "))
2022-09-27 12:05:36 +02:00
if active_sd_card not in [0, 1]:
2023-02-01 11:17:04 +01:00
_LOGGER.warning("Invalid SD card specified. Try again")
2022-09-27 12:05:36 +02:00
break
q.add_log_cmd(Info.SWITCH_TO_BOTH_SD_CARDS)
q.add_pus_tc(
create_action_cmd(
2022-09-27 12:05:36 +02:00
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.SWITCH_TO_BOTH_SD_CARDS,
2022-09-27 12:05:36 +02:00
user_data=bytes([active_sd_card]),
)
)
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.GET_HK:
2022-07-04 17:59:09 +02:00
q.add_log_cmd("Requesting housekeeping set")
sid = make_sid(object_id=CORE_CONTROLLER_ID, set_id=SetId.HK)
2022-07-04 17:59:09 +02:00
q.add_pus_tc(generate_one_hk_command(sid))
2023-11-22 16:40:27 +01:00
elif cmd_str == OpCode.SET_PREF_SD:
2023-04-12 18:51:38 +02:00
q.add_log_cmd("Set preferred SD card")
pref_sd = int(
input("Specify which SD card to set as the preferred one (0/1): ")
)
if pref_sd not in [0, 1]:
raise ValueError("Only 0 or 1 allowed for preferred SD card")
q.add_pus_tc(
create_load_param_cmd(
create_scalar_u8_parameter(
object_id=CORE_CONTROLLER_ID,
domain_id=0,
unique_id=ParamId.PREF_SD,
parameter=pref_sd,
2023-06-22 16:26:47 +02:00
)
2023-04-12 18:51:38 +02:00
)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.CP_HELPER:
2023-04-15 20:51:08 +02:00
cp_recursive = int(input("Copy recursively (0/1) ?: "))
if cp_recursive not in [0, 1]:
raise ValueError("Invalid value, only 0 or 1 allowed")
2023-06-22 16:26:33 +02:00
cp_force = int(input("Copy with force option(0/1) ?: "))
if cp_force not in [0, 1]:
raise ValueError("Invalid value, only 0 or 1 allowed")
user_data = bytearray([cp_recursive, cp_force])
2023-04-15 20:51:08 +02:00
user_data.extend(packet_source_dest_path("Copy"))
q.add_log_cmd(Info.CP_HELPER)
q.add_pus_tc(
create_action_cmd(CORE_CONTROLLER_ID, ActionId.CP_HELPER, user_data)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.MV_HELPER:
2023-04-15 20:51:08 +02:00
user_data = packet_source_dest_path("Move")
q.add_log_cmd(Info.MV_HELPER)
q.add_pus_tc(
create_action_cmd(CORE_CONTROLLER_ID, ActionId.MV_HELPER, user_data)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.RM_HELPER:
2023-04-15 20:51:08 +02:00
rm_recursive = int(input("Remove with recursive (-r) option (0/1) ?: "))
if rm_recursive not in [0, 1]:
raise ValueError("Invalid value, only 0 or 1 allowed")
rm_force = int(input("Remove with force (-f) option (0/1) ?: "))
if rm_force not in [0, 1]:
raise ValueError("Invalid value, only 0 or 1 allowed")
user_data = bytearray([rm_recursive, rm_force])
2023-04-15 21:53:11 +02:00
removed_file_or_dir = input("Specify absolute path to be removed: ")
user_data.extend(removed_file_or_dir.encode())
user_data.append(0)
2023-04-15 20:51:08 +02:00
q.add_log_cmd(Info.RM_HELPER)
q.add_pus_tc(
create_action_cmd(CORE_CONTROLLER_ID, ActionId.RM_HELPER, user_data)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.LIST_DIR_INTO_FILE:
2023-04-15 20:51:08 +02:00
q.add_log_cmd(Info.LIST_DIR_INTO_FILE)
user_data = list_directory_base_user_data()
dest_file_path = input("Destination file path: ")
user_data.extend(dest_file_path.encode())
user_data.append(0)
q.add_pus_tc(
create_action_cmd(
CORE_CONTROLLER_ID, ActionId.LIST_DIR_INTO_FILE, user_data
)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.LIST_DIR_DUMP_DIRECTLY:
2023-04-15 20:51:08 +02:00
q.add_log_cmd(Info.LIST_DIR_DUMP_DIRECTLY)
user_data = list_directory_base_user_data()
q.add_pus_tc(
create_action_cmd(
CORE_CONTROLLER_ID, ActionId.LIST_DIR_DUMP_DIRECTLY, user_data
)
)
2023-11-22 10:17:05 +01:00
elif cmd_str == OpCode.MKDIR_HELPER:
2023-04-15 21:53:11 +02:00
q.add_log_cmd(Info.MKDIR_HELPER)
user_data = input("Specify absolute path of newly created directory: ")
user_data = bytearray(user_data.encode())
user_data.append(0)
q.add_pus_tc(
create_action_cmd(CORE_CONTROLLER_ID, ActionId.MKDIR_HELPER, user_data)
)
elif cmd_str == OpCode.ENABLE_SCHEDULER:
q.add_log_cmd(Info.ENABLE_SCHEDULER)
q.add_pus_tc(create_enable_tc_sched_cmd())
elif cmd_str == OpCode.DISABLE_SCHEDULER:
q.add_log_cmd(Info.DISABLE_SCHEDULER)
q.add_pus_tc(create_disable_tc_sched_cmd())
2024-03-05 11:43:18 +01:00
elif cmd_str == OpCode.UPDATE_LEAP_SECONDS:
q.add_log_cmd(Info.UPDATE_LEAP_SECONDS)
leap_seconds = int(input("Specify new Leap Seconds Value: ")).to_bytes(
length=2, signed=False, byteorder="big"
)
q.add_pus_tc(
create_action_cmd(
CORE_CONTROLLER_ID, ActionId.UPDATE_LEAP_SECONRS, leap_seconds
)
)
else:
2023-03-13 10:31:27 +01:00
_LOGGER.warning(
2023-11-22 10:17:05 +01:00
f"Unknown operation code {cmd_str} for core controller commands"
2023-03-13 10:31:27 +01:00
)
2021-08-03 15:28:28 +02:00
2023-05-19 11:01:06 +02:00
def create_systemctl_cmd(systemctl_cmd: SystemctlCmd, unit_name: str):
cmd_data = bytearray([systemctl_cmd])
cmd_data.extend(unit_name.encode())
return create_action_cmd(
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.SYSTEMCTL_CMD_EXECUTOR,
user_data=cmd_data,
)
2023-04-15 20:51:08 +02:00
def list_directory_base_user_data() -> bytearray:
all_opt = int(input("Use all (-a) option (0/1) ?: "))
if all_opt not in [0, 1]:
raise ValueError("Invalid value, only 0 or 1 allowed")
recursive_opt = int(input("Use recursive (-R) option (0/1) ?: "))
if recursive_opt not in [0, 1]:
raise ValueError("Invalid value, only 0 or 1 allowed")
compression_opt = int(input("Compress target file (0/1) ?: "))
if compression_opt not in [0, 1]:
raise ValueError("Invalid value, only 0 or 1 allowed")
listing_path = input("Specify listing path (absolute path): ")
user_data = bytearray([all_opt, recursive_opt, compression_opt])
user_data.extend(listing_path.encode())
user_data.append(0)
2023-04-15 20:51:19 +02:00
return user_data
2023-04-15 20:51:08 +02:00
def packet_source_dest_path(context: str) -> bytes:
2023-04-15 21:53:11 +02:00
source = input(f"Specify {context} source file: ")
dest = input(f"Specify {context} destination file: ")
2023-04-15 20:51:08 +02:00
raw_src_dest = bytearray(source.encode())
raw_src_dest.append(0)
raw_src_dest.extend(dest.encode())
raw_src_dest.append(0)
return raw_src_dest
2022-09-26 22:25:47 +02:00
def reset_specific_boot_counter(q: DefaultPusQueueHelper, chip: int, copy: int):
q.add_log_cmd(f"Resetting boot counter {chip} {copy}")
q.add_pus_tc(
create_action_cmd(
2022-09-26 22:25:47 +02:00
object_id=CORE_CONTROLLER_ID,
action_id=ActionId.RESET_REBOOT_COUNTER,
2022-09-26 22:25:47 +02:00
user_data=bytes([chip, copy]),
)
)
2023-01-11 14:28:45 +01:00
def create_full_reboot_cmds() -> PusTelecommand:
return create_action_cmd(
object_id=CORE_CONTROLLER_ID, action_id=ActionId.FULL_REBOOT
2023-01-11 14:28:45 +01:00
)
2023-10-19 15:01:23 +02:00
def determine_reboot_params() -> Tuple[bool, Chip, Copy]:
2022-01-18 14:03:56 +01:00
reboot_self = input("Reboot self? [y/n]: ")
if reboot_self in ["y", "yes", "1"]:
2023-02-01 11:17:04 +01:00
_LOGGER.info("Rebooting currently running image")
2023-10-19 15:01:23 +02:00
return True, Chip.NONE, Copy.NONE
2023-02-01 11:17:04 +01:00
_LOGGER.info("Rebooting image specified by chip and copy")
2023-10-19 15:01:23 +02:00
chip, copy = determine_chip_and_copy()
return False, chip, copy
2022-09-26 22:25:47 +02:00
2023-10-19 15:01:23 +02:00
def determine_chip_and_copy() -> Tuple[Chip, Copy]:
2021-08-03 15:28:28 +02:00
while True:
2022-01-18 14:03:56 +01:00
chip_select = input("Chip select [0/1]: ")
if chip_select in ["0", "1"]:
if chip_select == "0":
2021-08-03 15:28:28 +02:00
chip_select = Chip.CHIP_0
else:
chip_select = Chip.CHIP_1
break
else:
2023-02-01 11:17:04 +01:00
_LOGGER.warning("Invalid chip select value. Try again")
2021-08-03 15:28:28 +02:00
while True:
2022-01-18 14:03:56 +01:00
copy_select = input("Copy select [0/1]: ")
if copy_select in ["0", "1"]:
if copy_select == "0":
2021-08-03 15:28:28 +02:00
copy_select = Copy.COPY_0_NOM
else:
copy_select = Copy.COPY_1_GOLD
break
else:
2023-02-01 11:17:04 +01:00
_LOGGER.warning("Invalid copy select value. Try again")
2022-09-26 22:25:47 +02:00
return chip_select, copy_select
def pack_obsw_update_cmd(action_id: int) -> PusTelecommand:
chip, copy = determine_chip_and_copy()
2023-04-08 11:39:41 +02:00
user_data = bytearray([chip, copy])
custom_file_name = input("Use custom filename [y/n] ?: ")
if custom_file_name.lower() in ["y", "yes", "1"]:
custom_file_name = input("Specify custom filename: ")
user_data.extend(custom_file_name.encode())
return create_action_cmd(
2022-09-26 22:25:47 +02:00
object_id=CORE_CONTROLLER_ID, action_id=action_id, user_data=user_data
)
2021-08-03 15:28:28 +02:00
2023-01-11 14:23:03 +01:00
def add_xsc_reboot_cmd(
2022-08-08 16:32:18 +02:00
q: DefaultPusQueueHelper,
2022-01-18 14:03:56 +01:00
reboot_self: bool,
chip: Chip = Chip.NONE,
copy: Copy = Copy.NONE,
2021-08-03 15:28:28 +02:00
):
if reboot_self:
2022-07-04 17:59:09 +02:00
q.add_log_cmd("Packing reboot command for current image")
2023-01-11 14:23:03 +01:00
else:
q.add_log_cmd(f"Packing reboot command for chip {chip} and copy {copy}")
q.add_pus_tc(create_xsc_reboot_cmds(reboot_self, chip, copy))
def create_xsc_reboot_cmds(
reboot_self: bool,
chip: Chip = Chip.NONE,
copy: Copy = Copy.NONE,
) -> PusTelecommand:
tc_data = bytearray()
if reboot_self:
2021-08-03 15:28:28 +02:00
tc_data.append(True)
else:
tc_data.append(False)
tc_data.append(chip)
tc_data.append(copy)
return create_action_cmd(
object_id=CORE_CONTROLLER_ID, action_id=ActionId.XSC_REBOOT, user_data=tc_data
2021-08-03 15:28:28 +02:00
)
2023-01-11 14:19:47 +01:00
2023-05-24 13:50:37 +02:00
def handle_core_hk_data(pw: PrintWrapper, set_id: int, hk_data: bytes):
if set_id == SetId.HK:
2023-01-11 14:19:47 +01:00
fmt_str = "!fff"
inc_len = struct.calcsize(fmt_str)
(temperature, ps_voltage, pl_voltage) = struct.unpack(
fmt_str, hk_data[0 : 0 + inc_len]
)
printout = (
f"Chip Temperature [°C] {temperature} | PS Voltage [mV] {ps_voltage} | "
f"PL Voltage [mV] {pl_voltage}"
)
pw.dlog(printout)
2023-05-24 13:50:37 +02:00
FsfwTmTcPrinter.get_validity_buffer(
validity_buffer=hk_data[inc_len:], num_vars=3
)
2023-04-16 02:00:50 +02:00
def handle_core_ctrl_action_replies(
2023-05-24 13:50:37 +02:00
action_id: int, pw: PrintWrapper, custom_data: bytes
2023-04-16 02:00:50 +02:00
):
2023-06-22 14:40:46 +02:00
if action_id == ActionId.READ_REBOOT_MECHANISM_INFO:
handle_reboot_mechanism_info_reply(pw, custom_data)
elif action_id == ActionId.LIST_DIR_DUMP_DIRECTLY:
handle_list_dir_dump_reply(pw, custom_data)
2023-06-22 14:52:28 +02:00
def handle_reboot_mechanism_info_reply(pw: PrintWrapper, custom_data: bytes):
2023-06-22 14:40:46 +02:00
pw.dlog("Received reboot mechansm information")
fmt_str = "!BIIIIIBBBBBBBB"
inc_len = struct.calcsize(fmt_str)
if len(custom_data) < inc_len:
raise ValueError(f"Received custom data shorter than expected {inc_len}")
(
enabled,
max_count,
img00_count,
img01_count,
img10_count,
img11_count,
img00_lock,
img01_lock,
img10_lock,
img11_lock,
last_chip,
last_copy,
next_chip,
2023-06-22 14:52:28 +02:00
next_copy,
) = struct.unpack(fmt_str, custom_data[:inc_len])
2023-06-22 14:40:46 +02:00
pw.dlog(f"Enabled: {enabled}")
pw.dlog(f"Max Count: {max_count}")
pw.dlog(f"Count 00: {img00_count}")
pw.dlog(f"Count 01: {img01_count}")
pw.dlog(f"Count 10: {img10_count}")
pw.dlog(f"Count 11: {img11_count}")
pw.dlog(f"Lock 00: {img00_lock}")
pw.dlog(f"Lock 01: {img01_lock}")
pw.dlog(f"Lock 10: {img10_lock}")
pw.dlog(f"Lock 11: {img11_lock}")
pw.dlog(f"Last Chip: {last_chip}")
pw.dlog(f"Last Copy: {last_copy}")
pw.dlog(f"Next Chip: {next_chip}")
pw.dlog(f"Next Copy: {next_copy}")
2023-06-22 14:52:28 +02:00
def handle_list_dir_dump_reply(pw: PrintWrapper, custom_data: bytes):
2023-06-22 14:40:46 +02:00
if len(custom_data) < 4:
_LOGGER.warning("Data unexpectedly small")
return
seq_idx = struct.unpack("!I", custom_data[0:4])[0]
total_chunks = struct.unpack("!I", custom_data[4:8])[0]
compressed = custom_data[8]
ls_cmd = custom_data[9:].split(b"\x00")[0].decode()
# Include length of NULL termination
file_data_offset = 9 + len(ls_cmd) + 1
pw.dlog(
f"Received directory listing dump for ls command {ls_cmd}. "
f"Chunk {seq_idx + 1}/{total_chunks}"
)
2023-04-16 02:56:14 +02:00
2023-06-22 14:40:46 +02:00
def remove_if_exists_and_new(seq_idx_: int, path_: Path):
if seq_idx_ == 0 and path_.exists():
os.remove(path_)
2023-04-16 02:56:14 +02:00
2023-06-22 14:40:46 +02:00
if compressed:
path = Path("dir_listing.txt.gz")
remove_if_exists_and_new(seq_idx, path)
pw.dlog(
f"Compression option: {compressed}. Dumping file into dir_listing.txt.gz"
)
with open(path, "ab") as listing_file:
listing_file.write(custom_data[file_data_offset:])
else:
path = Path("dir_listing.txt")
remove_if_exists_and_new(seq_idx, path)
2023-06-22 14:52:28 +02:00
pw.dlog(f"Compression option: {compressed}. Dumping file into dir_listing.txt")
2023-06-22 14:40:46 +02:00
with open(path, "a") as listing_file:
listing_file_str = custom_data[file_data_offset:].decode()
listing_file.write(listing_file_str)
if seq_idx + 1 == total_chunks:
pw.dlog("Full directory listing: ")
with open("dir_listing.txt", "r") as listing_file:
print(listing_file.read())