Skip to content

Add custom nvs namespace feature.#1125

Open
h2zero wants to merge 1 commit intomasterfrom
nvs-custom-namespace
Open

Add custom nvs namespace feature.#1125
h2zero wants to merge 1 commit intomasterfrom
nvs-custom-namespace

Conversation

@h2zero
Copy link
Owner

@h2zero h2zero commented Mar 25, 2026

Adds the function set_nvs_namespace(const char*) for users to switch bond data stores.

Fixes #959
Fixes #1102

Summary by CodeRabbit

  • New Features

    • Non-volatile storage namespace can now be customized at runtime instead of being fixed at compile time.
    • Added a runtime API to change the active namespace with C/C++ compatibility.
  • Bug Fixes

    • Input validation prevents too-long or NULL namespace values; invalid inputs log an error and revert to the default.

@coderabbitai
Copy link

coderabbitai bot commented Mar 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a9d82cf2-6087-4f4f-8795-6ba92cdb987e

📥 Commits

Reviewing files that changed from the base of the PR and between 9e9daac and 8e60f7c.

📒 Files selected for processing (2)
  • src/nimble/nimble/host/store/config/src/ble_store_nvs.c
  • src/nimconfig.h
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/nimconfig.h
  • src/nimble/nimble/host/store/config/src/ble_store_nvs.c

📝 Walkthrough

Walkthrough

Replaced the compile-time NVS namespace with a runtime-configurable namespace and added set_nimble_nvs_namespace(const char *ns) to change it (with length validation and fallback to the default). nimconfig.h exports the function with C linkage when compiled as C++ under ESP_PLATFORM.

Changes

Cohort / File(s) Summary
NVS namespace implementation
src/nimble/nimble/host/store/config/src/ble_store_nvs.c
Replaced fixed NIMBLE_NVS_NAMESPACE with a mutable variable initialized from NIMBLE_NVS_DEFAULT_NAMESPACE. Added set_nimble_nvs_namespace(const char *ns) which validates length, copies into an internal buffer, logs on error, and reverts to default when needed. All NVS opens use the runtime namespace.
Public declaration
src/nimconfig.h
Added C-linkage declaration extern "C" void set_nimble_nvs_namespace(const char *ns); under #ifdef ESP_PLATFORM when compiled as C++.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

⚓️ Avast! A namespace set to roam,

A tiny flag no longer glued to home,
Switch yer bonds and sail anew,
Names aplenty on the blue,
Yarrr — configurations bid adieu!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: introducing a custom NVS namespace feature for managing separate bond stores.
Linked Issues check ✅ Passed The implementation provides the requested namespace-switching mechanism via set_nimble_nvs_namespace(), directly addressing requirements from issues #959 and #1102.
Out of Scope Changes check ✅ Passed All changes are scoped to implementing the custom NVS namespace feature; no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nvs-custom-namespace

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/nimble/nimble/host/store/config/src/ble_store_nvs.c`:
- Around line 65-69: The current branch treats a NULL ns the same as an overly
long string and logs "Namespace string is too long", which is misleading; update
the conditional in the setter so you first check if ns == NULL and in that case
quietly assign NIMBLE_NVS_NAMESPACE = NIMBLE_NVS_DEFAULT_NAMESPACE (or log at
debug/verbose level), otherwise validate strlen(ns) against
NIMBLE_NVS_STR_NAME_MAX_LEN and only log an ESP_LOGE via LOG_TAG when the length
check fails before assigning the default; reference the symbols ns,
NIMBLE_NVS_NAMESPACE, NIMBLE_NVS_DEFAULT_NAMESPACE, NIMBLE_NVS_STR_NAME_MAX_LEN
and LOG_TAG when making this change.
- Around line 62-71: set_nvs_namespace currently stores the caller's pointer
into NIMBLE_NVS_NAMESPACE which can lead to dangling pointers; change it to copy
the incoming string into a static buffer and point NIMBLE_NVS_NAMESPACE at that
buffer (ensure the buffer size uses NIMBLE_NVS_STR_NAME_MAX_LEN, always
null-terminate, and fall back to NIMBLE_NVS_DEFAULT_NAMESPACE on overflow);
validate ns != NULL and strlen(ns) < NIMBLE_NVS_STR_NAME_MAX_LEN before copying,
log via ESP_LOGE(LOG_TAG) on overflow and use the static buffer contents
thereafter so callers may pass stack or heap strings safely.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2bece196-cfc6-4b74-8f9b-673b941bd580

📥 Commits

Reviewing files that changed from the base of the PR and between 4746c60 and 9e9daac.

📒 Files selected for processing (2)
  • src/nimble/nimble/host/store/config/src/ble_store_nvs.c
  • src/nimconfig.h

* Adds the function `set_nimble_nvs_namespace(const char*)` for users to switch bond data stores.
@h2zero h2zero force-pushed the nvs-custom-namespace branch from 1e0f0f3 to 8e60f7c Compare March 27, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: ability to change NVS namespace Mimicking multiple devices: bonding

1 participant