#include "config.h" #include LOG_MODULE_REGISTER(config, CONFIG_LOG_SETTINGS_LEVEL); extern int canbus_node_id; // Default node ID for CAN bus // Only compile shell commands if CONFIG_SHELL is enabled // and CONFIG_REBOOT is enabled, as rebooting is required to apply changes #ifdef CONFIG_SHELL #ifndef CONFIG_REBOOT #error You must enable CONFIG_REBOOT to use the shell commands #endif // CONFIG_REBOOT #include #include #include int reboot_system(const struct shell *shell, size_t argc, char **argv) { // Reboot the system shell_print(shell, "Rebooting node in 1 second..."); k_sleep(K_MSEC(1000)); // Wait for 1 second before rebooting sys_reboot(SYS_REBOOT_COLD); // Perform a cold reboot return 0; // Return 0 on success } int shell_print_config(const struct shell *shell, size_t argc, char **argv) { // Print the current settings for the CAN bus node ID shell_print(shell, "Current configuration settings:"); shell_print(shell, "%26s <%d>", "CANBUS node ID:", canbus_node_id); return 0; } int shell_set_canbus_node_id(const struct shell *shell, size_t argc, char **argv) { shell_print(shell, "argument count: %zu", argc); if (argc != 2) { shell_error(shell, "Usage: config set_nodeid "); return -EINVAL; // Invalid argument } int new_node_id = atoi(argv[1]); if (new_node_id < 1 || new_node_id > 7) { shell_error(shell, "Invalid CAN bus node ID: <%d>. Must be between 1 and 7.", new_node_id); return -EINVAL; // Invalid argument } canbus_node_id = new_node_id; LOG_INF("Set CAN bus node ID to <%d>", canbus_node_id); // Save the new node ID to settings int rc = settings_save_one("canbus/node_id", &canbus_node_id, sizeof(canbus_node_id)); if (rc < 0) { shell_error(shell, "Failed to save CAN bus node ID: %d", rc); return rc; // Save error } shell_print(shell, "CAN bus node ID set to <%d> and saved to settings", canbus_node_id); shell_warn(shell, "Reboot the node (command: 'reboot') to apply changes."); return 0; // Return 0 on success } SHELL_STATIC_SUBCMD_SET_CREATE( config_cmds, SHELL_CMD_ARG(print, NULL, "Print current configuration settings", shell_print_config, 0, 0), SHELL_CMD_ARG(set_nodeid, NULL, "Set canbus node id", shell_set_canbus_node_id, 2, 0), SHELL_SUBCMD_SET_END ); SHELL_CMD_REGISTER(config, &config_cmds, "Configuration commands", NULL); SHELL_CMD_REGISTER(reboot, NULL, "Reboot the node", reboot_system); #endif // CONFIG_SHELL static int settings_canbus(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg) { const char *next; int rc; // Handle setting values for CAN bus configuration LOG_DBG("Trying to read CAN bus configuration: key=%s, len=%zu", key, len); if (settings_name_steq(key, "node_id", &next) && next == NULL) { if (len != sizeof(canbus_node_id)) { LOG_ERR("Invalid length for node_id setting: %zu", len); return -EINVAL; // Invalid argument } rc = read_cb(cb_arg, &canbus_node_id, sizeof(canbus_node_id)); if (rc < 0) { LOG_ERR("Failed to read node_id setting: %d", rc); return rc; // Read error } LOG_INF("Set CAN bus node ID to <%d> from settings", canbus_node_id); } return 0; // Return 0 on success } static struct settings_handler settings_handler_canbus = { .name = "canbus", .h_set = settings_canbus, // No settings set handler }; int config_init(void) { int rc; rc=settings_subsys_init(); if (rc < 0) { LOG_ERR("Failed to initialize settings subsystem: %d", rc); return rc; // Initialization error } rc = settings_register(&settings_handler_canbus); if (rc < 0) { LOG_ERR("Failed to register settings handler: %d", rc); return rc; // Registration error } rc = settings_load(); if (rc < 0) { LOG_ERR("Failed to load settings: %d", rc); return rc; // Load error } return 0; // Return 0 on success }