ESP32/components/app_console/cmd_nvs.c

160 lines
5.4 KiB
C

/*
* cmd_nvs.c
*
* Copyright (c) 2025 Umber Networks & Robert McMahon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "esp_log.h"
#include "esp_console.h"
#include "argtable3/argtable3.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "wifi_cfg.h"
#include "iperf.h"
#include "app_console.h"
// ============================================================================
// COMMAND: nvs (Storage Management)
// ============================================================================
static struct {
struct arg_lit *dump;
struct arg_lit *clear_all;
struct arg_lit *help;
struct arg_end *end;
} nvs_args;
// --- Helper Functions for Dumping ---
static void print_nvs_key_str(nvs_handle_t h, const char *key, const char *label) {
char buf[64] = {0};
size_t len = sizeof(buf);
if (nvs_get_str(h, key, buf, &len) == ESP_OK) {
printf(" %-12s : %s\n", label, buf);
} else {
printf(" %-12s : <empty>\n", label);
}
}
static void print_nvs_key_u32(nvs_handle_t h, const char *key, const char *label) {
uint32_t val = 0;
if (nvs_get_u32(h, key, &val) == ESP_OK) {
printf(" %-12s : %" PRIu32 "\n", label, val);
} else {
printf(" %-12s : <empty>\n", label);
}
}
static void print_nvs_key_u8(nvs_handle_t h, const char *key, const char *label) {
uint8_t val = 0;
if (nvs_get_u8(h, key, &val) == ESP_OK) {
printf(" %-12s : %u\n", label, val);
} else {
printf(" %-12s : <empty>\n", label);
}
}
static int cmd_nvs(int argc, char **argv) {
int nerrors = arg_parse(argc, argv, (void **)&nvs_args);
if (nerrors > 0) {
arg_print_errors(stderr, nvs_args.end, argv[0]);
return 1;
}
if (nvs_args.help->count > 0) {
printf("Usage: nvs [--dump] [--clear-all]\n");
return 0;
}
// --- CLEAR ALL ---
if (nvs_args.clear_all->count > 0) {
printf("Erasing ALL settings from NVS...\n");
wifi_cfg_clear_credentials();
wifi_cfg_clear_monitor_channel();
iperf_param_clear();
printf("Done. Please reboot.\n");
app_console_update_prompt();
return 0;
}
// --- DUMP ---
// Note: NVS iterators are complex in ESP-IDF; explicit key listing is often simpler for known keys.
printf("\n--- [WiFi Config (netcfg)] ---\n");
nvs_handle_t h;
if (nvs_open("netcfg", NVS_READONLY, &h) == ESP_OK) {
print_nvs_key_str(h, "ssid", "SSID");
print_nvs_key_str(h, "pass", "Password");
print_nvs_key_str(h, "ip", "Static IP");
print_nvs_key_str(h, "mask", "Netmask");
print_nvs_key_str(h, "gw", "Gateway");
print_nvs_key_u8 (h, "dhcp", "DHCP");
print_nvs_key_u8 (h, "mon_ch", "Monitor Ch");
nvs_close(h);
} else {
printf("Failed to open 'netcfg' namespace (not initialized?).\n");
}
printf("\n--- [iPerf Config (storage)] ---\n");
if (nvs_open("storage", NVS_READONLY, &h) == ESP_OK) {
print_nvs_key_str(h, "iperf_dst_ip", "Dest IP");
print_nvs_key_u32(h, "iperf_port", "Port");
print_nvs_key_u32(h, "iperf_pps", "Target PPS");
print_nvs_key_u32(h, "iperf_len", "Packet Len");
print_nvs_key_u32(h, "iperf_burst", "Burst");
print_nvs_key_u8 (h, "gps_enabled", "GPS En");
nvs_close(h);
} else {
printf("Failed to open 'storage' namespace.\n");
}
printf("\n");
app_console_update_prompt();
return 0;
}
void register_nvs_cmd(void) {
nvs_args.dump = arg_lit0(NULL, "dump", "Show all");
nvs_args.clear_all = arg_lit0(NULL, "clear-all", "Factory Reset");
nvs_args.help = arg_lit0("h", "help", "Help");
nvs_args.end = arg_end(1);
const esp_console_cmd_t cmd = {
.command = "nvs",
.help = "Storage Management",
.func = &cmd_nvs,
.argtable = &nvs_args
};
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}