Skip to content

Commit 3da3847

Browse files
committed
refactor(config): new message for reboot behavior
use a new SetConfig message for the reboot behavior, better than reusing the RebootOrb, which could lead to unwanted behavior in case jetson binary tried to set a config with a firmware not handling the new fields Signed-off-by: Cyril Fougeray <cyril.fougeray@toolsforhumanity.com>
1 parent 941a7ac commit 3da3847

7 files changed

Lines changed: 177 additions & 127 deletions

File tree

main_board/src/cli/cli.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -807,17 +807,22 @@ execute_boot_config(const struct shell *sh, size_t argc, char **argv)
807807
{
808808
if (argc < 2) {
809809
/* No argument: print current config */
810-
reboot_behavior_t behavior = config_get_reboot_behavior();
811-
shell_print(sh, "boot config: %s",
812-
behavior == BOOT_ALWAYS ? "always_on" : "button");
810+
orb_mcu_main_SetConfig_RebootBehavior behavior =
811+
config_get_reboot_behavior();
812+
shell_print(
813+
sh, "boot config: %s",
814+
behavior ==
815+
orb_mcu_main_SetConfig_RebootBehavior_BOOT_AUTO_ALWAYS_ON
816+
? "always_on"
817+
: "button");
813818
return 0;
814819
}
815820

816-
reboot_behavior_t behavior;
821+
orb_mcu_main_SetConfig_RebootBehavior behavior;
817822
if (strcmp(argv[1], "always_on") == 0) {
818-
behavior = BOOT_ALWAYS;
823+
behavior = orb_mcu_main_SetConfig_RebootBehavior_BOOT_AUTO_ALWAYS_ON;
819824
} else if (strcmp(argv[1], "button") == 0) {
820-
behavior = BOOT_BUTTON;
825+
behavior = orb_mcu_main_SetConfig_RebootBehavior_BOOT_BUTTON_PRESS;
821826
} else {
822827
shell_error(sh, "Usage: orb boot_config [button|always_on]");
823828
return -EINVAL;

main_board/src/power/boot/boot.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,9 @@ app_init_state(void)
812812
/* read persistent boot behavior from flash config */
813813
ret = config_init();
814814
ASSERT_SOFT(ret);
815-
config_auto_boot = (config_get_reboot_behavior() == BOOT_ALWAYS);
815+
config_auto_boot =
816+
(config_get_reboot_behavior() ==
817+
orb_mcu_main_SetConfig_RebootBehavior_BOOT_AUTO_ALWAYS_ON);
816818

817819
LOG_INF_IMM("insta-boot: %d, post ota: %d, auto-boot: %d, "
818820
"config-auto: %d",

main_board/src/runner/runner.c

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -389,67 +389,63 @@ handle_reboot_orb(job_t *job)
389389
MAKE_ASSERTS(orb_mcu_main_JetsonToMcu_reboot_orb_tag);
390390

391391
uint32_t delay = msg->payload.reboot_orb.force_reboot_timeout_s;
392-
orb_mcu_main_RebootOrb_BootBehavior behavior =
393-
msg->payload.reboot_orb.reboot_behavior;
394392

395393
if (delay != 0 && (delay > 60 || delay < 10)) {
396394
job_ack(orb_mcu_Ack_ErrorCode_RANGE, job);
397395
LOG_ERR("Reboot with delay > 60s or < 10s: %u", delay);
398396
} else {
399-
int ret = RET_SUCCESS;
400-
// Persist the requested reboot behavior if explicitly set
401-
// BOOT_REBOOT_NEXT (0 / default) means no change to the boot config
402-
if (behavior ==
403-
orb_mcu_main_RebootOrb_BootBehavior_BOOT_BUTTON_PRESS_ALWAYS) {
404-
ret = config_set_reboot_behavior(BOOT_BUTTON);
405-
if (ret != RET_SUCCESS) {
406-
LOG_ERR("Failed to persist reboot behavior: %d", ret);
407-
}
408-
} else if (behavior ==
409-
orb_mcu_main_RebootOrb_BootBehavior_BOOT_AUTO_ALWAYS_ON) {
410-
ret = config_set_reboot_behavior(BOOT_ALWAYS);
411-
if (ret != RET_SUCCESS) {
412-
LOG_ERR("Failed to persist reboot behavior: %d", ret);
397+
int ret =
398+
backup_regs_write_byte(REBOOT_FLAG_OFFSET_BYTE, REBOOT_INSTABOOT);
399+
if (ret == 0) {
400+
if (delay != 0) {
401+
// force reboot after `delay` seconds,
402+
// but a shutdown request from the Jetson is preferred
403+
// (SHUTDOWN_REQ gpio)
404+
ret = reboot(delay);
405+
} else {
406+
LOG_INF("waiting for reboot request from Jetson");
413407
}
414-
} else if (behavior ==
415-
orb_mcu_main_RebootOrb_BootBehavior_BOOT_REBOOT_NEXT) {
416-
// on BOOT_REBOOT_NEXT, we keep the next reboot state in bbram
417-
// (won't persist in case of power loss)
418-
ret = backup_regs_write_byte(REBOOT_FLAG_OFFSET_BYTE,
419-
REBOOT_INSTABOOT);
420-
if (ret == 0) {
421-
if (delay != 0) {
422-
// force reboot after `delay` seconds,
423-
// but a shutdown request from the Jetson is preferred
424-
// (SHUTDOWN_REQ gpio)
425-
ret = reboot(delay);
426-
} else {
427-
LOG_INF("waiting for reboot request from Jetson");
428-
}
429408

430-
if (ret == 0) {
409+
if (ret == 0) {
431410
#ifdef CONFIG_MEMFAULT
432-
MEMFAULT_REBOOT_MARK_RESET_IMMINENT(
433-
kMfltRebootReason_JetsonRequestedRebootOrb);
411+
MEMFAULT_REBOOT_MARK_RESET_IMMINENT(
412+
kMfltRebootReason_JetsonRequestedRebootOrb);
434413
#endif
435-
job_ack(orb_mcu_Ack_ErrorCode_SUCCESS, job);
436-
return;
437-
}
414+
job_ack(orb_mcu_Ack_ErrorCode_SUCCESS, job);
415+
return;
438416
}
439-
// failure setting flag or initiating reboot
440-
// reset flag
441-
backup_regs_write_byte(REBOOT_FLAG_OFFSET_BYTE, 0);
442-
} else {
443-
ret = RET_ERROR_INVALID_PARAM;
444417
}
418+
}
445419

446-
if (ret == 0) {
420+
// failure setting flag or initiating reboot
421+
// reset flag
422+
backup_regs_write_byte(REBOOT_FLAG_OFFSET_BYTE, 0);
423+
job_ack(orb_mcu_Ack_ErrorCode_FAIL, job);
424+
}
425+
426+
static void
427+
handle_set_config(job_t *job)
428+
{
429+
orb_mcu_main_JetsonToMcu *msg = &job->message.jetson_cmd;
430+
MAKE_ASSERTS(orb_mcu_main_JetsonToMcu_set_config_tag);
431+
432+
switch (msg->payload.set_config.which_config) {
433+
case orb_mcu_main_SetConfig_reboot_behavior_tag: {
434+
int ret = config_set_reboot_behavior(
435+
msg->payload.set_config.config.reboot_behavior);
436+
if (ret == RET_SUCCESS) {
447437
job_ack(orb_mcu_Ack_ErrorCode_SUCCESS, job);
448-
} else if (ret == RET_ERROR_INVALID_PARAM) {
449-
job_ack(orb_mcu_Ack_ErrorCode_RANGE, job);
450438
} else {
439+
LOG_ERR("Failed to persist reboot behavior: %d", ret);
451440
job_ack(orb_mcu_Ack_ErrorCode_FAIL, job);
452441
}
442+
break;
443+
}
444+
default:
445+
LOG_WRN("Unknown config field: %u",
446+
msg->payload.set_config.which_config);
447+
job_ack(orb_mcu_Ack_ErrorCode_RANGE, job);
448+
break;
453449
}
454450
}
455451

@@ -1824,6 +1820,7 @@ static const hm_callback handle_message_callbacks[] = {
18241820
[orb_mcu_main_JetsonToMcu_set_time_tag] = handle_set_time,
18251821
[orb_mcu_main_JetsonToMcu_reboot_orb_tag] = handle_reboot_orb,
18261822
[orb_mcu_main_JetsonToMcu_boot_complete_tag] = handle_boot_complete,
1823+
[orb_mcu_main_JetsonToMcu_set_config_tag] = handle_set_config,
18271824
[orb_mcu_main_JetsonToMcu_start_triggering_rgb_face_camera_tag] =
18281825
handle_start_triggering_rgb_face_camera_message,
18291826
[orb_mcu_main_JetsonToMcu_stop_triggering_rgb_face_camera_tag] =
@@ -1846,7 +1843,7 @@ static const hm_callback handle_message_callbacks[] = {
18461843
#endif
18471844
};
18481845

1849-
BUILD_ASSERT((ARRAY_SIZE(handle_message_callbacks) <= 56),
1846+
BUILD_ASSERT((ARRAY_SIZE(handle_message_callbacks) <= 57),
18501847
"It seems like the `handle_message_callbacks` array is too large");
18511848

18521849
_Noreturn static void

main_board/src/system/config/config.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "config.h"
22
#include "orb_logs.h"
3+
#include "zephyr/toolchain.h"
34
#include <errors.h>
45
#include <storage.h>
56
#include <string.h>
@@ -18,10 +19,14 @@ LOG_MODULE_REGISTER(config, CONFIG_CONFIG_LOG_LEVEL);
1819
* maintain backward compatibility. New fields in a larger record will
1920
* be zero-initialized when reading an older (smaller) config.
2021
*/
21-
typedef struct {
22-
reboot_behavior_t boot;
22+
typedef struct __PACKED {
23+
orb_mcu_main_SetConfig_RebootBehavior boot;
24+
uint8_t _pad[7]; /* padding to ensure memory alignment */
2325
} persistent_config_t;
2426

27+
BUILD_ASSERT(sizeof(persistent_config_t) % FLASH_WRITE_BLOCK_SIZE == 0,
28+
"persistent_config_t not aligned to flash write block size: ");
29+
2530
static struct storage_area_s config_storage_area;
2631
static persistent_config_t current_config;
2732
static bool initialized;
@@ -125,15 +130,15 @@ config_init(void)
125130
return ret;
126131
}
127132

128-
reboot_behavior_t
133+
orb_mcu_main_SetConfig_RebootBehavior
129134
config_get_reboot_behavior(void)
130135
{
131136
__ASSERT(initialized, "config module not initialized");
132137
return current_config.boot;
133138
}
134139

135140
int
136-
config_set_reboot_behavior(reboot_behavior_t behavior)
141+
config_set_reboot_behavior(orb_mcu_main_SetConfig_RebootBehavior behavior)
137142
{
138143
k_mutex_lock(&config_mutex, K_FOREVER);
139144

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
#pragma once
22

3-
#include <stdint.h>
4-
5-
/**
6-
* @brief Reboot behavior configuration
7-
*
8-
* Determines how the Orb boots after a reboot.
9-
*/
10-
typedef enum {
11-
/// Wait for button press before booting (default/nominal behavior)
12-
BOOT_BUTTON = 0,
13-
/// Always boot immediately without waiting for button press
14-
BOOT_ALWAYS = 1,
15-
16-
/// Force enum to 32-bit width
17-
_REBOOT_BEHAVIOR_FORCE_32BIT = 0x7FFFFFFF,
18-
} reboot_behavior_t;
19-
_Static_assert(sizeof(reboot_behavior_t) == 4,
20-
"reboot_behavior_t must be 4 bytes");
3+
#include <main.pb.h>
214

225
/**
236
* Initialize the persistent config module.
@@ -38,7 +21,7 @@ config_init(void);
3821
*
3922
* @return current reboot behavior (BOOT_BUTTON if no config was persisted)
4023
*/
41-
reboot_behavior_t
24+
orb_mcu_main_SetConfig_RebootBehavior
4225
config_get_reboot_behavior(void);
4326

4427
/**
@@ -53,4 +36,4 @@ config_get_reboot_behavior(void);
5336
* @retval RET_ERROR_INTERNAL flash write failure
5437
*/
5538
int
56-
config_set_reboot_behavior(reboot_behavior_t behavior);
39+
config_set_reboot_behavior(orb_mcu_main_SetConfig_RebootBehavior behavior);

0 commit comments

Comments
 (0)