25static const char*
TAG =
"NvsEdit";
81 case NVS_TYPE_U8:
return "u8";
82 case NVS_TYPE_I8:
return "i8";
83 case NVS_TYPE_U16:
return "u16";
84 case NVS_TYPE_I16:
return "i16";
85 case NVS_TYPE_U32:
return "u32";
86 case NVS_TYPE_I32:
return "i32";
87 case NVS_TYPE_U64:
return "u64";
88 case NVS_TYPE_I64:
return "i64";
89 case NVS_TYPE_STR:
return "str";
90 case NVS_TYPE_BLOB:
return "blob";
102 nvs_iterator_t it =
nullptr;
103 esp_err_t err = nvs_entry_find(NVS_DEFAULT_PART_NAME,
nullptr, NVS_TYPE_ANY, &it);
105 char lastNs[16] = {};
107 nvs_entry_info_t info;
108 nvs_entry_info(it, &info);
111 if (strcmp(info.namespace_name, lastNs) != 0) {
114 strncpy(lastNs, info.namespace_name, 15);
118 err = nvs_entry_next(&it);
121 if (it) nvs_release_iterator(it);
133 nvs_iterator_t it =
nullptr;
134 esp_err_t err = nvs_entry_find(NVS_DEFAULT_PART_NAME, ns, NVS_TYPE_ANY, &it);
137 nvs_entry_info_t info;
138 nvs_entry_info(it, &info);
145 err = nvs_entry_next(&it);
148 if (it) nvs_release_iterator(it);
160 if (nvs_open(ns, NVS_READWRITE, &handle) != ESP_OK) {
164 esp_err_t err = nvs_erase_key(handle, key);
170 LOG_I(
TAG,
"Deleted key '%s' from '%s': %s", key, ns, esp_err_to_name(err));
171 return err == ESP_OK;
181 if (nvs_open(ns, NVS_READWRITE, &handle) != ESP_OK) {
185 esp_err_t err = nvs_erase_all(handle);
191 LOG_I(
TAG,
"Deleted namespace '%s': %s", ns, esp_err_to_name(err));
192 return err == ESP_OK;
203static void formatValue(
const char* ns,
const char* key, nvs_type_t type,
char* buf,
size_t bufLen) {
205 if (nvs_open(ns, NVS_READONLY, &handle) != ESP_OK) {
206 snprintf(buf, bufLen,
"(error opening)");
210 esp_err_t err = ESP_FAIL;
215 err = nvs_get_u8(handle, key, &val);
216 if (err == ESP_OK) snprintf(buf, bufLen,
"%u (0x%02X)", val, val);
221 err = nvs_get_i8(handle, key, &val);
222 if (err == ESP_OK) snprintf(buf, bufLen,
"%d", val);
227 err = nvs_get_u16(handle, key, &val);
228 if (err == ESP_OK) snprintf(buf, bufLen,
"%u (0x%04X)", val, val);
233 err = nvs_get_i16(handle, key, &val);
234 if (err == ESP_OK) snprintf(buf, bufLen,
"%d", val);
239 err = nvs_get_u32(handle, key, &val);
240 if (err == ESP_OK) snprintf(buf, bufLen,
"%lu (0x%08lX)", (
unsigned long)val, (
unsigned long)val);
245 err = nvs_get_i32(handle, key, &val);
246 if (err == ESP_OK) snprintf(buf, bufLen,
"%ld", (
long)val);
251 err = nvs_get_u64(handle, key, &val);
252 if (err == ESP_OK) snprintf(buf, bufLen,
"%llu", (
unsigned long long)val);
257 err = nvs_get_i64(handle, key, &val);
258 if (err == ESP_OK) snprintf(buf, bufLen,
"%lld", (
long long)val);
263 err = nvs_get_str(handle, key,
nullptr, &len);
264 if (err == ESP_OK && len > 0 && len < bufLen) {
265 err = nvs_get_str(handle, key, buf, &len);
266 }
else if (err == ESP_OK) {
267 snprintf(buf, bufLen,
"(str len=%zu)", len);
271 case NVS_TYPE_BLOB: {
273 err = nvs_get_blob(handle, key,
nullptr, &len);
277 err = nvs_get_blob(handle, key, data, &len);
280 for (
size_t i = 0; i < len && (p - buf) < (int)(bufLen - 4); i++) {
281 p += snprintf(p, bufLen - (p - buf),
"%02X ", data[i]);
285 snprintf(buf, bufLen,
"(blob %zu bytes)", len);
291 snprintf(buf, bufLen,
"(unknown type)");
296 snprintf(buf, bufLen,
"(read error)");
365static void showValueView(
const char* ns,
const char* key, nvs_type_t type);
490static void showValueView(
const char* ns,
const char* key, nvs_type_t type) {
521 ?
"NVS Editor is privileged. Deletes are irreversible. Continue?"
522 :
"NVS Browser is read-only. Continue?";
533 LOG_I(
TAG,
"NVS Editor module initialized");
544 if (maxItems < 1)
return 0;
567 auto&
module = cdc::mod_nvsedit::s_module;
void mod_nvsedit_register()
Registers NVS editor initializer with module registry.
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_I(tag, fmt,...)
const char * getName() const override
Returns the module name supplied to the constructor.
static ModuleRegistry & instance()
Returns the singleton module registry instance.
void registerInitializer(ModuleInitFunc initFunc)
Registers a deferred module initializer callback.
bool init() override
Initializes NVS editor module state.
uint8_t getMenuItems(cdc::core::ModuleMenuItem *items, uint8_t maxItems) override
Exposes NVS editor entry in the tools menu.
static ViewStack & instance()
Returns singleton view-stack instance.
void push(IView *view, void *context=nullptr)
static ContextMenuItem s_nsContextItems[]
static ListView * s_namespaceListView
Lazily created views and backing list item arrays.
static char s_valueBuffer[MAX_VALUE_DISPLAY]
static constexpr uint8_t MAX_KEYS
static void formatValue(const char *ns, const char *key, nvs_type_t type, char *buf, size_t bufLen)
Formats one NVS value into a readable preview string.
static void loadKeys(const char *ns)
Loads keys and types for one namespace.
static bool deleteKey(const char *ns, const char *key)
Deletes a single key from a namespace.
static uint8_t s_namespaceCount
static void onKeySelect(uint16_t index, void *userData)
Handles key selection and opens detailed value view.
static char s_selectedNamespace[16]
static ListItem s_namespaceItems[MAX_NAMESPACES]
static constexpr uint8_t MAX_NAMESPACES
Upper bounds for list sizes and value preview buffers.
static const char * nvsTypeToString(nvs_type_t type)
Converts NVS value type enum to a short display string.
static void loadNamespaces()
Loads unique namespace names from the default NVS partition.
static void onDeleteNamespace()
Deletes the currently selected namespace via context menu action.
static void onDeleteKey()
Deletes the currently selected key via context menu action.
static char s_keys[MAX_KEYS][16]
static void rebuildKeyListView()
Rebuilds key list labels into s_keyLabels and refreshes the key list view.
static void onNamespaceMenu(uint16_t index, void *userData)
Opens namespace context menu for selected item.
static constexpr size_t MAX_VALUE_DISPLAY
static void showNamespaceListView()
Creates and shows the namespace list view.
static ListItem s_keyItems[MAX_KEYS]
static ContextMenuItem s_keyContextItems[]
static bool deleteEnabled()
Indicates whether destructive delete actions are enabled.
static void showDeleteDisabled()
Shows a toast describing that delete actions are disabled.
static InfoView * s_valueView
static bool deleteNamespace(const char *ns)
Deletes all keys in a namespace.
static void onKeyMenu(uint16_t index, void *userData)
Opens key context menu for the selected key.
static nvs_type_t s_keyTypes[MAX_KEYS]
static char s_namespaces[MAX_NAMESPACES][16]
Runtime state for namespace/key browsing context.
static NvsEditModule s_module
Static module instance used by registration callback.
static char s_selectedKey[16]
static void onNamespaceSelect(uint16_t index, void *userData)
Handles namespace list selection.
static char s_keyLabels[MAX_KEYS][24]
Persistent key label storage used by list items.
static ListView * s_keyListView
static void showValueView(const char *ns, const char *key, nvs_type_t type)
Shows detailed value view for a selected key.
static char s_valueTitle[32]
static uint8_t s_keyCount
static IView * getNvsEditorView()
Returns the NVS editor entry view callback target.
static void onNvsEditorConfirm(void *userData)
Confirm callback that opens namespace browser after warning dialog.
static void showKeyListView(const char *ns)
Shows key list view for the selected namespace.
Centralized key-code constants for cdc_views.
void showConfirm(const char *message, ConfirmView::ConfirmCallback onConfirm, ConfirmView::CancelCallback onCancel=nullptr, ConfirmView::Icon icon=ConfirmView::Icon::QUESTION, void *userData=nullptr)
Shows a shared modal confirmation dialog instance.
void hideContextMenu()
Hides the active context menu modal.
ContextMenuView * showContextMenu(const char *title, const ContextMenuItem *items, uint8_t count)
Shows the shared context menu instance as modal.
void showToastInfo(const char *message, uint16_t durationMs=1500)
Shows an informational toast message.
void showToastError(const char *message, uint16_t durationMs=1500)
Shows an error toast message.