Add a settings console command

This commit is contained in:
2023-05-15 18:32:56 +01:00
parent 0634787df3
commit f115ed7394
2 changed files with 34 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
idf_component_register(
SRCS "settings.c"
INCLUDE_DIRS "."
REQUIRES fatal nvs_flash
REQUIRES console_wrapper fatal nvs_flash
)

View File

@@ -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 <id> OR settings <id> <value>", command_func);
}
void settings_set_hostname(const char *hostname)