Cli: reset, wifi_ap_clients, config_get\set, nvs_dump

This commit is contained in:
DrZlo13 2021-12-07 01:41:33 +10:00
parent 0ca098b103
commit 35df50691c
4 changed files with 324 additions and 16 deletions

View File

@ -2,6 +2,7 @@ set(SOURCES
"main.c"
"usb-cdc.c"
"nvs.c"
"nvs-config.c"
"led.c"
"cli-uart.c"
"i2c.c"
@ -15,6 +16,7 @@ set(SOURCES
"cli-commands.c"
"cli-commands-gpio.c"
"cli-commands-wifi.c"
"cli-commands-config.c"
"cli-commands-device-info.c"
"cli-args.c"
)

214
main/cli-commands-config.c Normal file
View File

@ -0,0 +1,214 @@
#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));
cli_write_eol(cli);
mstring_free(value);
}
void cli_config_set_wifi_mode_usage(Cli* cli) {
cli_write_str(cli, "config_set_wifi_mode <AP|STA>");
}
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");
}

View File

@ -96,4 +96,38 @@ void cli_wifi_scan(Cli* cli, mstring_t* args) {
cli_write_str(cli, "OK");
free(ap_info);
}
void cli_wifi_ap_clients(Cli* cli, mstring_t* args) {
wifi_sta_list_t* wifi_sta_list = malloc(sizeof(wifi_sta_list_t));
esp_err_t err = esp_wifi_ap_get_sta_list(wifi_sta_list);
if(err == ESP_OK) {
for(size_t i = 0; i < wifi_sta_list->num; i++) {
cli_printf(
cli,
"%02x:%02x:%02x:%02x:%02x:%02x ",
wifi_sta_list->sta[i].mac[0],
wifi_sta_list->sta[i].mac[1],
wifi_sta_list->sta[i].mac[2],
wifi_sta_list->sta[i].mac[3],
wifi_sta_list->sta[i].mac[4],
wifi_sta_list->sta[i].mac[5]);
cli_printf(cli, "%idBm ", wifi_sta_list->sta[i].rssi);
if(wifi_sta_list->sta[i].phy_11b) cli_write_str(cli, "b");
if(wifi_sta_list->sta[i].phy_11g) cli_write_str(cli, "g");
if(wifi_sta_list->sta[i].phy_11n) cli_write_str(cli, "n");
if(wifi_sta_list->sta[i].phy_lr) cli_write_str(cli, "lr");
if(wifi_sta_list->sta[i].is_mesh_child) cli_write_str(cli, "m");
cli_write_eol(cli);
}
cli_write_str(cli, "OK");
} else {
cli_write_str(cli, "FAIL");
}
free(wifi_sta_list);
}

View File

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdint.h>
#include <esp_system.h>
#include "cli.h"
#include "led.h"
#include "nvs.h"
@ -7,14 +8,25 @@
#include "cli-args.h"
#include "cli-commands.h"
void cli_device_info(Cli* cli, mstring_t* args);
void cli_factory_reset(Cli* cli, mstring_t* args);
void cli_gpio_get(Cli* cli, mstring_t* args);
void cli_gpio_set(Cli* cli, mstring_t* args);
void cli_led(Cli* cli, mstring_t* args);
void cli_help(Cli* cli, mstring_t* args);
void cli_ping(Cli* cli, mstring_t* args);
void cli_gpio_set(Cli* cli, mstring_t* args);
void cli_gpio_get(Cli* cli, mstring_t* args);
void cli_device_info(Cli* cli, mstring_t* args);
void cli_factory_reset(Cli* cli, mstring_t* args);
void cli_sw_reset(Cli* cli, mstring_t* args);
void cli_wifi_scan(Cli* cli, mstring_t* args);
void cli_wifi_ap_clients(Cli* cli, mstring_t* args);
void cli_config_get(Cli* cli, mstring_t* args);
void cli_config_set_wifi_mode(Cli* cli, mstring_t* args);
void cli_config_set_ap_pass(Cli* cli, mstring_t* args);
void cli_config_set_ap_ssid(Cli* cli, mstring_t* args);
void cli_config_set_sta_pass(Cli* cli, mstring_t* args);
void cli_config_set_sta_ssid(Cli* cli, mstring_t* args);
void cli_nvs_dump(Cli* cli, mstring_t* args);
const CliItem cli_items[] = {
{
@ -26,28 +38,28 @@ const CliItem cli_items[] = {
.callback = cli_help,
},
{
.name = "led",
.callback = cli_led,
.name = "config_get",
.callback = cli_config_get,
},
{
.name = "help",
.callback = cli_help,
.name = "config_set_wifi_mode",
.callback = cli_config_set_wifi_mode,
},
{
.name = "ping",
.callback = cli_ping,
.name = "config_set_ap_pass",
.callback = cli_config_set_ap_pass,
},
{
.name = "gpio_set",
.callback = cli_gpio_set,
.name = "config_set_ap_ssid",
.callback = cli_config_set_ap_ssid,
},
{
.name = "gpio_get",
.callback = cli_gpio_get,
.name = "config_set_sta_pass",
.callback = cli_config_set_sta_pass,
},
{
.name = "wifi_scan",
.callback = cli_wifi_scan,
.name = "config_set_sta_ssid",
.callback = cli_config_set_sta_ssid,
},
{
.name = "device_info",
@ -57,6 +69,42 @@ const CliItem cli_items[] = {
.name = "factory_reset",
.callback = cli_factory_reset,
},
{
.name = "gpio_get",
.callback = cli_gpio_get,
},
{
.name = "gpio_set",
.callback = cli_gpio_set,
},
{
.name = "help",
.callback = cli_help,
},
{
.name = "led",
.callback = cli_led,
},
{
.name = "nvs_dump",
.callback = cli_nvs_dump,
},
{
.name = "ping",
.callback = cli_ping,
},
{
.name = "reset",
.callback = cli_sw_reset,
},
{
.name = "wifi_ap_clients",
.callback = cli_wifi_ap_clients,
},
{
.name = "wifi_scan",
.callback = cli_wifi_scan,
},
};
size_t cli_items_count = COUNT_OF(cli_items);
@ -71,9 +119,18 @@ void cli_help(Cli* cli, mstring_t* args) {
}
}
void cli_sw_reset(Cli* cli, mstring_t* args) {
cli_write_str(cli, "SW RESET");
cli_write_eol(cli);
cli_flush(cli);
esp_restart();
cli_write_str(cli, "OK");
}
void cli_factory_reset(Cli* cli, mstring_t* args) {
cli_write_str(cli, "Erasing NVS");
cli_write_eol(cli);
cli_flush(cli);
nvs_erase();
cli_write_str(cli, "OK");
}
@ -85,6 +142,7 @@ void cli_ping(Cli* cli, mstring_t* args) {
void cli_led_print_usage(Cli* cli) {
cli_write_str(cli, "Usage: led <red> <green> <blue>");
}
void cli_led(Cli* cli, mstring_t* args) {
int red;
int green;