blackmagic-esp32-s2/main/cli-commands-config.c

218 lines
5.7 KiB
C
Raw Normal View History

#include "cli.h"
#include "cli-args.h"
#include "cli-commands.h"
#include "helpers.h"
#include "nvs-config.h"
#include <nvs_flash.h>
#include <string.h>
void cli_config_get(Cli* cli, mstring_t* args) {
mstring_t* value = mstring_alloc();
WiFiMode wifi_mode;
nvs_config_get_ap_ssid(value);
cli_printf(cli, "ap_ssid: %s", mstring_get_cstr(value));
cli_write_eol(cli);
nvs_config_get_ap_pass(value);
cli_printf(cli, "ap_pass: %s", mstring_get_cstr(value));
cli_write_eol(cli);
nvs_config_get_sta_ssid(value);
cli_printf(cli, "sta_ssid: %s", mstring_get_cstr(value));
cli_write_eol(cli);
nvs_config_get_sta_pass(value);
cli_printf(cli, "sta_pass: %s", mstring_get_cstr(value));
cli_write_eol(cli);
nvs_config_get_wifi_mode(&wifi_mode);
switch(wifi_mode) {
case WiFiModeAP:
mstring_set(value, "AP");
break;
case WiFiModeSTA:
mstring_set(value, "STA");
break;
}
cli_printf(cli, "wifi_mode: %s", mstring_get_cstr(value));
mstring_free(value);
}
void cli_config_set_wifi_mode_usage(Cli* cli) {
cli_write_str(cli, "config_set_wifi_mode <AP|STA>");
2021-12-07 08:20:59 +00:00
cli_write_eol(cli);
cli_write_str(cli, " AP (make own WiFi AP)");
cli_write_eol(cli);
cli_write_str(cli, " STA (connect to WiFi)");
cli_write_eol(cli);
}
void cli_config_set_wifi_mode(Cli* cli, mstring_t* args) {
mstring_t* mode = mstring_alloc();
WiFiMode wifi_mode;
do {
if(!cli_args_read_string_and_trim(args, mode)) {
cli_config_set_wifi_mode_usage(cli);
break;
}
if(mstring_cmp_cstr(mode, "AP") == 0) {
wifi_mode = WiFiModeAP;
} else if(mstring_cmp_cstr(mode, "STA") == 0) {
wifi_mode = WiFiModeSTA;
} else {
cli_config_set_wifi_mode_usage(cli);
break;
}
if(nvs_config_set_wifi_mode(wifi_mode) == ESP_OK) {
cli_write_str(cli, "OK");
} else {
cli_write_str(cli, "ERR");
}
} while(false);
mstring_free(mode);
}
void cli_config_set_ap_pass(Cli* cli, mstring_t* args) {
mstring_t* pass = mstring_alloc();
do {
if(!cli_args_read_quoted_string_and_trim(args, pass)) {
cli_write_str(cli, "config_set_ap_pass \"<pass>\", min 8 symbols or empty quotes");
break;
}
if(nvs_config_set_ap_pass(pass) == ESP_OK) {
cli_write_str(cli, "OK");
} else {
cli_write_str(cli, "config_set_ap_pass \"<pass>\", min 8 symbols or empty quotes");
}
} while(false);
mstring_free(pass);
}
void cli_config_set_ap_ssid(Cli* cli, mstring_t* args) {
mstring_t* ssid = mstring_alloc();
do {
if(!cli_args_read_quoted_string_and_trim(args, ssid)) {
cli_write_str(cli, "config_set_ap_ssid \"<ssid>\", 1-32 symbols");
break;
}
if(nvs_config_set_ap_ssid(ssid) == ESP_OK) {
cli_write_str(cli, "OK");
} else {
cli_write_str(cli, "config_set_ap_ssid \"<ssid>\", 1-32 symbols");
}
} while(false);
mstring_free(ssid);
}
void cli_config_set_sta_pass(Cli* cli, mstring_t* args) {
mstring_t* pass = mstring_alloc();
do {
if(!cli_args_read_quoted_string_and_trim(args, pass)) {
cli_write_str(cli, "config_set_sta_pass \"<pass>\", min 8 symbols or empty quotes");
break;
}
if(nvs_config_set_sta_pass(pass) == ESP_OK) {
cli_write_str(cli, "OK");
} else {
cli_write_str(cli, "config_set_sta_pass \"<pass>\", min 8 symbols or empty quotes");
}
} while(false);
mstring_free(pass);
}
void cli_config_set_sta_ssid(Cli* cli, mstring_t* args) {
mstring_t* ssid = mstring_alloc();
do {
if(!cli_args_read_quoted_string_and_trim(args, ssid)) {
cli_write_str(cli, "config_set_sta_ssid \"<ssid>\", 1-32 symbols");
break;
}
if(nvs_config_set_sta_ssid(ssid) == ESP_OK) {
cli_write_str(cli, "OK");
} else {
cli_write_str(cli, "config_set_sta_ssid \"<ssid>\", 1-32 symbols");
}
} while(false);
mstring_free(ssid);
}
const char* nvs_type_to_str(nvs_type_t type) {
switch(type) {
case NVS_TYPE_U8:
return "U8";
case NVS_TYPE_I8:
return "I8";
case NVS_TYPE_U16:
return "U16";
case NVS_TYPE_I16:
return "I16";
case NVS_TYPE_U32:
return "U32";
case NVS_TYPE_I32:
return "I32";
case NVS_TYPE_U64:
return "U64";
case NVS_TYPE_I64:
return "I64";
case NVS_TYPE_STR:
return "STR";
case NVS_TYPE_BLOB:
return "BLOB";
case NVS_TYPE_ANY:
return "ANY";
default:
return "UNK";
}
}
void cli_nvs_dump(Cli* cli, mstring_t* args) {
const char* partitions[] = {
"nvs",
"nvs_storage",
};
for(size_t i = 0; i < COUNT_OF(partitions); i++) {
cli_write_str(cli, partitions[i]);
cli_write_str(cli, ":");
cli_write_eol(cli);
do {
nvs_iterator_t it = nvs_entry_find(partitions[i], NULL, NVS_TYPE_ANY);
while(it != NULL) {
nvs_entry_info_t info;
nvs_entry_info(it, &info);
it = nvs_entry_next(it);
if(strlen(info.namespace_name)) {
cli_printf(cli, " \"%s:%s\"", info.namespace_name, info.key);
} else {
cli_printf(cli, " \"%s\"", info.key);
}
cli_printf(cli, " %s", nvs_type_to_str(info.type));
cli_write_eol(cli);
};
} while(false);
}
cli_write_str(cli, "OK");
}