eive-tmtc/eive_tmtc/tmtc/core.py

396 lines
14 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-01-11 14:19:47 +01:00
import struct
from eive_tmtc.pus_tm.defs import PrintWrapper
2021-08-03 15:28:28 +02:00
2022-11-29 16:53:29 +01:00
from eive_tmtc.config.definitions import CustomServiceList
2022-09-26 22:25:47 +02:00
from spacepackets.ecss import PusTelecommand
from tmtccmd.config import TmtcDefinitionWrapper
2022-07-04 17:59:09 +02:00
2022-08-08 16:32:18 +02:00
from tmtccmd.tc import DefaultPusQueueHelper
from tmtccmd.pus.s8_fsfw_funccmd import create_action_cmd
from tmtccmd.tc.pus_3_fsfw_hk import make_sid, generate_one_hk_command
from tmtccmd.config.tmtc import OpCodeEntry, tmtc_definitions_provider
2022-11-29 16:53:29 +01:00
from eive_tmtc.config.object_ids import CORE_CONTROLLER_ID
2023-01-11 14:19:47 +01:00
from tmtccmd.util.tmtc_printer import FsfwTmTcPrinter
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
class ActionId(enum.IntEnum):
2022-02-24 10:56:36 +01:00
LIST_DIR_INTO_FILE = 0
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
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
2022-05-24 01:13:21 +02:00
XSC_REBOOT = 32
FULL_REBOOT = 34
2021-08-03 15:28:28 +02:00
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"
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"]
2022-09-26 22:25:47 +02:00
OBSW_UPDATE_FROM_SD_0 = ["obsw_update_sd0"]
OBSW_UPDATE_FROM_SD_1 = ["obsw_update_sd1"]
OBSW_UPDATE_FROM_TMP = ["obsw_update_tmp"]
2022-09-27 12:05:36 +02:00
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"]
ENABLE_REBOOT_FILE_HANDLING = ["rbh_off"]
DISABLE_REBOOT_FILE_HANDLING = ["rbh_on"]
RESET_ALL_REBOOT_COUNTERS = ["rbh_reset_a"]
RESET_REBOOT_COUNTER_00 = ["rbh_reset_00"]
RESET_REBOOT_COUNTER_01 = ["rbh_reset_01"]
RESET_REBOOT_COUNTER_10 = ["rbh_reset_10"]
RESET_REBOOT_COUNTER_11 = ["rbh_reset_11"]
SET_MAX_REBOOT_CNT = ["rbh_max_cnt"]
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"
2022-05-24 01:13:21 +02:00
REBOOT_XSC = "XSC reboot with prompt"
REBOOT_FULL = "Full regular reboot"
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"
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"
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
@tmtc_definitions_provider
def add_core_controller_definitions(defs: TmtcDefinitionWrapper):
2022-07-05 02:12:54 +02:00
oce = OpCodeEntry()
2023-02-08 11:21:00 +01:00
oce.add(keys=OpCode.ANNOUNCE_VERSION, info=Info.ANNOUNCE_VERSION)
oce.add(keys=OpCode.ANNOUNCE_CURRENT_IMAGE, info=Info.ANNOUNCE_CURRENT_IMAGE)
oce.add(keys=OpCode.ANNOUNCE_BOOT_COUNTS, info=Info.ANNOUNCE_BOOT_COUNTS)
oce.add(keys=OpCode.REBOOT_XSC, info=Info.REBOOT_XSC)
oce.add(keys=OpCode.REBOOT_XSC, info=Info.REBOOT_XSC)
oce.add(keys=OpCode.REBOOT_FULL, info=Info.REBOOT_FULL)
oce.add(keys=OpCode.XSC_REBOOT_SELF, info="Reboot Self")
oce.add(keys=OpCode.XSC_REBOOT_0_0, info="Reboot 0 0")
oce.add(keys=OpCode.XSC_REBOOT_0_1, info="Reboot 0 1")
oce.add(keys=OpCode.XSC_REBOOT_1_0, info="Reboot 1 0")
oce.add(keys=OpCode.XSC_REBOOT_1_1, info="Reboot 1 1")
oce.add(keys=OpCode.OBSW_UPDATE_FROM_TMP, info=Info.OBSW_UPDATE_FROM_TMP)
oce.add(keys=OpCode.OBSW_UPDATE_FROM_SD_0, info=Info.OBSW_UPDATE_FROM_SD_0)
oce.add(keys=OpCode.OBSW_UPDATE_FROM_SD_1, info=Info.OBSW_UPDATE_FROM_SD_1)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.GET_HK,
2022-05-24 01:13:21 +02:00
info="Request housekeeping set",
)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.ENABLE_REBOOT_FILE_HANDLING,
2022-05-24 01:13:21 +02:00
info="Enable reboot file handling",
)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.DISABLE_REBOOT_FILE_HANDLING,
2022-05-24 01:13:21 +02:00
info="Disable reboot file handling",
)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.RESET_ALL_REBOOT_COUNTERS,
2022-05-24 01:13:21 +02:00
info="Reset all reboot counters",
)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.RESET_REBOOT_COUNTER_00,
2022-05-24 01:13:21 +02:00
info="Reset reboot counter 0 0",
)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.RESET_REBOOT_COUNTER_01,
2022-05-24 01:13:21 +02:00
info="Reset reboot counter 0 1",
)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.RESET_REBOOT_COUNTER_10,
2022-05-24 01:13:21 +02:00
info="Reset reboot counter 1 0",
)
2022-07-05 02:12:54 +02:00
oce.add(
keys=OpCode.RESET_REBOOT_COUNTER_11,
2022-05-24 01:13:21 +02:00
info="Reset reboot counter 1 1",
)
oce.add(keys=OpCode.OBSW_UPDATE_FROM_SD_0, info=Info.OBSW_UPDATE_FROM_SD_0)
oce.add(keys=OpCode.OBSW_UPDATE_FROM_SD_1, info=Info.OBSW_UPDATE_FROM_SD_1)
oce.add(keys=OpCode.OBSW_UPDATE_FROM_TMP, info=Info.OBSW_UPDATE_FROM_TMP)
oce.add(keys=OpCode.SWITCH_TO_SD_0, info=Info.SWITCH_TO_SD_0)
oce.add(keys=OpCode.SWITCH_TO_SD_1, info=Info.SWITCH_TO_SD_1)
oce.add(keys=OpCode.SWITCH_TO_BOTH_SD_CARDS, info=Info.SWITCH_TO_BOTH_SD_CARDS)
2022-08-08 16:47:41 +02:00
defs.add_service(CustomServiceList.CORE.value, "Core Controller", oce)
2022-05-24 01:13:21 +02:00
2022-08-08 16:32:18 +02:00
def pack_core_commands(q: DefaultPusQueueHelper, op_code: str):
2023-02-08 11:21:00 +01:00
if op_code == OpCode.ANNOUNCE_VERSION:
q.add_log_cmd(f"{Info.ANNOUNCE_VERSION}")
q.add_pus_tc(create_action_cmd(CORE_CONTROLLER_ID, ActionId.ANNOUNCE_VERSION))
elif op_code == 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)
)
elif op_code == 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)
)
elif op_code in 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
)
elif op_code in 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
)
elif op_code in OpCode.XSC_REBOOT_SELF:
2023-01-11 14:23:03 +01:00
add_xsc_reboot_cmd(q=q, reboot_self=True)
elif op_code in 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
)
elif op_code in 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
)
elif op_code in 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
)
elif op_code in 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
)
elif op_code in 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
)
elif op_code in 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
)
elif op_code in 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
)
elif op_code in OpCode.RESET_REBOOT_COUNTER_00:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 0, 0)
elif op_code in OpCode.RESET_REBOOT_COUNTER_01:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 0, 1)
elif op_code in OpCode.RESET_REBOOT_COUNTER_10:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 1, 0)
elif op_code in OpCode.RESET_REBOOT_COUNTER_11:
2022-09-26 22:25:47 +02:00
reset_specific_boot_counter(q, 1, 1)
elif op_code in 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))
elif op_code in 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))
elif op_code in 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))
elif op_code in 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
)
)
elif op_code in 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
)
)
elif op_code in OpCode.SWITCH_TO_BOTH_SD_CARDS:
2022-09-27 12:05:36 +02:00
while True:
active_sd_card = int(input("Please specify active SD cqrd [0/1]: "))
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]),
)
)
elif op_code in 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))
else:
2023-03-13 10:31:27 +01:00
_LOGGER.warning(
f"Unknown operation code {op_code} for core controller commands"
)
2021-08-03 15:28:28 +02:00
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
)
2021-08-03 15:28:28 +02:00
def determine_reboot_params() -> (bool, Chip, Copy):
chip_select = -1
copy_select = -1
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")
2021-08-03 15:28:28 +02:00
return True, chip_select, copy_select
2023-02-01 11:17:04 +01:00
_LOGGER.info("Rebooting image specified by chip and copy")
2022-09-26 22:25:47 +02:00
return False, determine_chip_and_copy()
def determine_chip_and_copy() -> (int, int):
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()
user_data = bytes([chip, copy])
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
def handle_core_hk_data(printer: FsfwTmTcPrinter, set_id: int, hk_data: bytes):
if set_id == SetId.HK:
2023-01-11 14:19:47 +01:00
pw = PrintWrapper(printer)
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)
printer.print_validity_buffer(validity_buffer=hk_data[inc_len:], num_vars=3)