diff --git a/components/settings/CMakeLists.txt b/components/settings/CMakeLists.txt index 0ea4c8d..7dae848 100644 --- a/components/settings/CMakeLists.txt +++ b/components/settings/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "settings.c" INCLUDE_DIRS "." - REQUIRES fatal nvs_flash + REQUIRES console_wrapper fatal nvs_flash ) diff --git a/components/settings/settings.c b/components/settings/settings.c index c48a843..9799de5 100644 --- a/components/settings/settings.c +++ b/components/settings/settings.c @@ -5,6 +5,7 @@ #include "settings.h" +#include "console.h" #include "fatal.h" #include "esp_log.h" @@ -165,6 +166,34 @@ static void add_callback(ItemIndex item, SettingsCallback callback) } } +static int command_func(int argc, char **argv) +{ + if (argc == 2) { + for (unsigned i = 0; i < ITEM_COUNT; ++i) { + if (strcmp(state[i].id, argv[1]) != 0) + continue; + char buffer[SETTINGS_MAX_VALUE_SIZE]; + (void)get(i, buffer, SETTINGS_MAX_VALUE_SIZE); + printf("%s\n", buffer); + return 0; + } + printf("Setting not found\n"); + return 1; + } else if (argc == 3) { + for (unsigned i = 0; i < ITEM_COUNT; ++i) { + if (strcmp(state[i].id, argv[1]) != 0) + continue; + set(i, argv[2]); + return 0; + } + printf("Setting not found\n"); + return 1; + } else { + printf("Invalid number of arguments\n"); + return 1; + } +} + void settings_init() { esp_err_t error; @@ -187,6 +216,10 @@ void settings_init() save(item); } } + + console_register( + "settings", "Get or set a setting", + "settings OR settings ", command_func); } void settings_set_hostname(const char *hostname)