irrigation_system/software/lib/config.c

62 lines
1.7 KiB
C

#include "config.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(config, CONFIG_LOG_SETTINGS_LEVEL);
extern int canbus_node_id; // Default node ID for CAN bus
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
}