Skip to content

Add custom nvs namespace feature.#1125

Merged
h2zero merged 1 commit intomasterfrom
nvs-custom-namespace
Apr 9, 2026
Merged

Add custom nvs namespace feature.#1125
h2zero merged 1 commit intomasterfrom
nvs-custom-namespace

Conversation

@h2zero
Copy link
Copy Markdown
Owner

@h2zero h2zero commented Mar 25, 2026

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

Fixes #959
Fixes #1102

Summary by CodeRabbit

  • New Features

    • NVS namespace is now configurable at runtime (no rebuild required).
    • Added a runtime API to change the active namespace, usable from both C and C++ builds.
  • Bug Fixes

    • Input validation for namespace values: NULL resets to default; overly long values are rejected with an error and default fallback.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 25, 2026

📝 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 exposes the function with C linkage when compiled as C++ under ESP_PLATFORM. Yarrr!

Changes

Cohort / File(s) Summary
NVS namespace implementation
src/nimble/nimble/host/store/config/src/ble_store_nvs.c
Replaced fixed NIMBLE_NVS_NAMESPACE macro with NIMBLE_NVS_DEFAULT_NAMESPACE, introduced a mutable namespace buffer and a NIMBLE_NVS_NAMESPACE pointer, added set_nimble_nvs_namespace(const char *ns) to update the namespace at runtime (NULL to revert, length-checked copy, error log and fallback on overflow), updated all nvs_open() calls to use the runtime namespace, and renamed internal log tag TAGLOG_TAG.
Public declaration
src/nimconfig.h
Added C-linkage declaration extern "C" void set_nimble_nvs_namespace(const char *ns); inside #ifdef ESP_PLATFORM when compiling as C++.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

Avast! A namespace now sails free,
Swap yer bonds across the briny sea,
Buffers set and defaults near,
Change at will — no need to fear,
Yarrr, multiple devices cheer!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% 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 'Add custom nvs namespace feature' directly and clearly summarizes the main change - adding a runtime-configurable NVS namespace via the new set_nimble_nvs_namespace() function.
Linked Issues check ✅ Passed The pull request fully addresses both linked issues (#959 and #1102) by implementing runtime namespace switching to allow separate bond storage for multiple virtual devices.
Out of Scope Changes check ✅ Passed All changes directly support the core objective: the NVS namespace refactoring, the new set_nimble_nvs_namespace() function, header declarations, and the LOG_TAG rename are all integral to enabling runtime namespace configuration.

✏️ 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

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Copy Markdown

@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

@h2zero h2zero force-pushed the nvs-custom-namespace branch 2 times, most recently from 1e0f0f3 to 8e60f7c Compare March 27, 2026 13:03
@h2zero h2zero force-pushed the nvs-custom-namespace branch from 8e60f7c to 6d4b2e8 Compare April 9, 2026 00:02
Copy link
Copy Markdown

@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: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/nimble/nimble/host/store/config/src/ble_store_nvs.c (1)

160-164: ⚠️ Potential issue | 🔴 Critical

Shiver me timbers! TAG be undefined and will scuttle yer compilation, cap'n!

Arr, ye renamed TAG to LOG_TAG at line 54, but several functions still be usin' the old TAG identifier that now be walkin' the plank! This will cause compilation errors faster than ye can say "Davy Jones!"

Affected locations using undefined TAG:

  • Line 162: get_nvs_peer_record()
  • Lines 459, 465: ble_store_nvs_peer_records()
  • Lines 677, 682: ble_nvs_restore_peer_records()
  • Lines 761, 767, 775, 778: ble_store_config_persist_eads()

Replace all occurrences of TAG with LOG_TAG in these functions to fix the compilation errors.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/nimble/nimble/host/store/config/src/ble_store_nvs.c` around lines 160 -
164, Several logging calls still use the old undefined symbol TAG (causing
compile errors); update all uses of TAG to LOG_TAG in the affected functions —
specifically replace TAG with LOG_TAG in get_nvs_peer_record(),
ble_store_nvs_peer_records(), ble_nvs_restore_peer_records(), and
ble_store_config_persist_eads() so the ESP_LOGx calls use the existing LOG_TAG
identifier used at the top of the file.
🤖 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 62-78: The function set_nimble_nvs_namespace currently accepts
empty strings because it only checks strlen(ns) < NIMBLE_NVS_STR_NAME_MAX_LEN;
update it to reject empty strings by ensuring ns is non-NULL and not empty
(e.g., check ns[0] != '\0' or strlen(ns) > 0) before copying into
nimble_nvs_namespace_buf; if the string is empty or too long, log the error and
set NIMBLE_NVS_NAMESPACE to NIMBLE_NVS_DEFAULT_NAMESPACE (references:
set_nimble_nvs_namespace, nimble_nvs_namespace_buf, NIMBLE_NVS_NAMESPACE,
NIMBLE_NVS_DEFAULT_NAMESPACE, NIMBLE_NVS_STR_NAME_MAX_LEN).

---

Outside diff comments:
In `@src/nimble/nimble/host/store/config/src/ble_store_nvs.c`:
- Around line 160-164: Several logging calls still use the old undefined symbol
TAG (causing compile errors); update all uses of TAG to LOG_TAG in the affected
functions — specifically replace TAG with LOG_TAG in get_nvs_peer_record(),
ble_store_nvs_peer_records(), ble_nvs_restore_peer_records(), and
ble_store_config_persist_eads() so the ESP_LOGx calls use the existing LOG_TAG
identifier used at the top of the file.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 834bca33-1904-485e-8b30-1efcfe92924e

📥 Commits

Reviewing files that changed from the base of the PR and between 8e60f7c and 6d4b2e8.

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

* 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 6d4b2e8 to 5c45baa Compare April 9, 2026 00:11
Copy link
Copy Markdown

@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.

🧹 Nitpick comments (1)
src/nimble/nimble/host/store/config/src/ble_store_nvs.c (1)

62-78: Avast! A word o' caution about when to hoist this flag, captain!

Changin' the namespace at runtime while NVS operations be in progress could lead to unpredictable results - there be no synchronization guardin' the NIMBLE_NVS_NAMESPACE pointer. Consider documentatin' that this function should only be called before NimBLE initialization or after a full deinit, when no NVS operations be underway.

📜 Suggested documentation addition
+/**
+ * Set the NVS namespace for storing BLE bond data.
+ * 
+ * `@note` This function should be called before NimBLE initialization
+ *       or after NimBLE_deinit(). Calling while NimBLE is active may
+ *       result in undefined behavior.
+ * 
+ * `@param` ns The namespace string (max 15 chars), or NULL to reset to default.
+ */
 void
 set_nimble_nvs_namespace(const char *ns)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/nimble/nimble/host/store/config/src/ble_store_nvs.c` around lines 62 -
78, The set_nimble_nvs_namespace function mutates the global
NIMBLE_NVS_NAMESPACE pointer without synchronization which can cause races if
called while NVS operations run; update the code and/or docs to prevent misuse:
either document clearly in the function comment and public API that
set_nimble_nvs_namespace must only be called before NimBLE initialization or
after full deinitialization (no NVS activity), or add proper synchronization
(e.g., a mutex or atomic swap) around writes to nimble_nvs_namespace_buf and
updates to NIMBLE_NVS_NAMESPACE so runtime changes are safe; reference the
symbols set_nimble_nvs_namespace, NIMBLE_NVS_NAMESPACE, and
nimble_nvs_namespace_buf when applying the fix.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/nimble/nimble/host/store/config/src/ble_store_nvs.c`:
- Around line 62-78: The set_nimble_nvs_namespace function mutates the global
NIMBLE_NVS_NAMESPACE pointer without synchronization which can cause races if
called while NVS operations run; update the code and/or docs to prevent misuse:
either document clearly in the function comment and public API that
set_nimble_nvs_namespace must only be called before NimBLE initialization or
after full deinitialization (no NVS activity), or add proper synchronization
(e.g., a mutex or atomic swap) around writes to nimble_nvs_namespace_buf and
updates to NIMBLE_NVS_NAMESPACE so runtime changes are safe; reference the
symbols set_nimble_nvs_namespace, NIMBLE_NVS_NAMESPACE, and
nimble_nvs_namespace_buf when applying the fix.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2d4ee851-e510-4fac-acbc-61581af97bca

📥 Commits

Reviewing files that changed from the base of the PR and between 6d4b2e8 and 5c45baa.

📒 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 (1)
  • src/nimconfig.h

@h2zero h2zero merged commit a3f9a6e into master Apr 9, 2026
42 checks passed
@h2zero h2zero deleted the nvs-custom-namespace branch April 9, 2026 00:20
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