Skip to content

Cleanup compiler warnings when using HID device or bonds disabled.#406

Merged
h2zero merged 2 commits intomasterfrom
fix-compile-warnings
Mar 18, 2026
Merged

Cleanup compiler warnings when using HID device or bonds disabled.#406
h2zero merged 2 commits intomasterfrom
fix-compile-warnings

Conversation

@h2zero
Copy link
Owner

@h2zero h2zero commented Mar 18, 2026

Fixes #397

Summary by CodeRabbit

  • Refactor

    • HID device service lifecycle simplified; services are now managed by the server rather than explicit startup calls.
  • Security

    • Bonding and passkey/confirmation behavior now respect build configuration, provide safe defaults, and emit clearer errors when related features are disabled.
  • Deprecations

    • startServices() marked deprecated; services are automatically started by the server.

@coderabbitai
Copy link

coderabbitai bot commented Mar 18, 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: 19e0d28a-8a43-4aa1-9965-867001fe102e

📥 Commits

Reviewing files that changed from the base of the PR and between 8977376 and 5a301f1.

📒 Files selected for processing (3)
  • src/NimBLEDevice.cpp
  • src/NimBLEHIDDevice.cpp
  • src/NimBLEHIDDevice.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/NimBLEHIDDevice.cpp

📝 Walkthrough

Walkthrough

Arrr — This PR gates bond/security logic behind compile-time flags (returnin' safe defaults or logging when disabled) and deprecates NimBLEHIDDevice::startServices(), removin' explicit service starts so the server now handles service startup.

Changes

Cohort / File(s) Summary
Bond & Security Methods
src/NimBLEDevice.cpp
Added MYNEWT_VAL(...) and BLE_SM_* conditional compilation guards around getNumBonds, isBonded, getBondedAddress, injectPassKey, and injectConfirmPasskey; unused params cast, safe defaults or error logs when features disabled.
HID Service Lifecycle
src/NimBLEHIDDevice.cpp, src/NimBLEHIDDevice.h
Removed explicit m_deviceInfoSvc->start(), m_hidSvc->start(), m_batterySvc->start() from startServices() (now a no-op) and marked startServices() deprecated with a compiler attribute; services are started by the server on start().
Build/Manifest
CMakeLists.txt
Minor manifest change recorded (1 line changed) — inspect for build/config edits related to component handling.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant HID as NimBLEHIDDevice
participant SVC as Services (DeviceInfo, HID, Battery)
participant SRV as Server
HID->>SRV: start()
SRV->>SVC: initialize & start services
SRV-->>HID: services running
Note right of SVC: individual service.start() calls removed from HID

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

Yarrr, flags raised on the code's salty shores,
Bonds be guarded and old calls shown the door.
Services heed the server's thunderous roar,
Deprecated plankfalls creak — still we code for more. ⚓

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive The NimBLEHIDDevice.cpp and NimBLEHIDDevice.h changes deprecate startServices() to align with server-managed lifecycle, which is a reasonable refactoring but not explicitly mentioned in issue #397. Clarify whether the HIDDevice deprecation changes are part of the original issue scope or if they should be separated into a distinct PR for better change isolation.
✅ 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 summarizes the main changes: adding conditional compilation guards and deprecation to cleanup compiler warnings when HID services or bonds features are disabled.
Linked Issues check ✅ Passed The PR addresses issue #397 by adding conditional compilation guards in NimBLEDevice.cpp to prevent array-bounds warnings when BLE_STORE_MAX_BONDS is 0, fixing the compilation failure reported for ESP-IDF 5.5.3.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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 fix-compile-warnings
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Caution

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

⚠️ Outside diff range comments (1)
src/NimBLEDevice.cpp (1)

684-693: ⚠️ Potential issue | 🔴 Critical

Critical: off-by-one bound check can read past the bonded array.

Arrr, on Line 688 the condition uses index > num_peers; when index == num_peers, Line 692 indexes out o’ bounds. Use index >= num_peers.

⚙️ Patch fer the bound check
-    if (rc != 0 || index > num_peers || index < 0) {
+    if (rc != 0 || index >= num_peers || index < 0) {
         return NimBLEAddress{};
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/NimBLEDevice.cpp` around lines 684 - 693, The bound check in the block
that calls ble_store_util_bonded_peers can read past peer_id_addrs because it
uses `index > num_peers`; change the condition in that function (the block that
returns NimBLEAddress(peer_id_addrs[index])) to check `index >= num_peers` (and
still keep index < 0 and rc != 0 checks) so that accessing peer_id_addrs[index]
cannot go out-of-bounds; look for the code around ble_store_util_bonded_peers,
peer_id_addrs, and the NimBLEAddress return to apply the fix.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/NimBLEDevice.cpp`:
- Around line 684-693: The bound check in the block that calls
ble_store_util_bonded_peers can read past peer_id_addrs because it uses `index >
num_peers`; change the condition in that function (the block that returns
NimBLEAddress(peer_id_addrs[index])) to check `index >= num_peers` (and still
keep index < 0 and rc != 0 checks) so that accessing peer_id_addrs[index] cannot
go out-of-bounds; look for the code around ble_store_util_bonded_peers,
peer_id_addrs, and the NimBLEAddress return to apply the fix.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 659b0053-785e-4f87-bb84-c0a47e44b4ee

📥 Commits

Reviewing files that changed from the base of the PR and between a672ecd and 0606e21.

📒 Files selected for processing (3)
  • src/NimBLEDevice.cpp
  • src/NimBLEHIDDevice.cpp
  • src/NimBLEHIDDevice.h

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

Caution

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

⚠️ Outside diff range comments (1)
src/NimBLEDevice.cpp (1)

659-676: ⚠️ Potential issue | 🟡 Minor

Shiver me timbers! The address parameter be unused when bonds be disabled, cap'n!

When BLE_STORE_MAX_BONDS be zero, this whole function becomes just return false; — leavin' the address parameter adrift with no use. This could summon another compiler warnin' about unused parameters, which be the very sea monster ye be tryin' to vanquish!

Ye might want to add a (void)address; cast like ye did fer the injectPassKey and injectConfirmPasskey functions down below.

🏴‍☠️ Proposed fix to silence the unused parameter warnin'
 bool NimBLEDevice::isBonded(const NimBLEAddress& address) {
 #  if MYNEWT_VAL(BLE_STORE_MAX_BONDS)
     ble_addr_t peer_id_addrs[MYNEWT_VAL(BLE_STORE_MAX_BONDS)];
     int        num_peers, rc;
 
     rc = ble_store_util_bonded_peers(&peer_id_addrs[0], &num_peers, MYNEWT_VAL(BLE_STORE_MAX_BONDS));
     if (rc != 0) {
         return false;
     }
 
     for (int i = 0; i < num_peers; i++) {
         NimBLEAddress storedAddr(peer_id_addrs[i]);
         if (storedAddr == address) {
             return true;
         }
     }
-#  endif
+#  else
+    (void)address;
+#  endif
     return false;
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/NimBLEDevice.cpp` around lines 659 - 676, The function leaves the
parameter 'address' unused when MYNEWT_VAL(BLE_STORE_MAX_BONDS) is zero, causing
compiler warnings; to fix, add a cast-to-void for the unused parameter (e.g.,
(void)address;) in the branch where the bond-check code is disabled so 'address'
is referenced; locate the function using the 'address' parameter in this diff
and mirror the pattern used in 'injectPassKey' and 'injectConfirmPasskey' to
silence the warning, then return false as before.
🤖 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/NimBLEDevice.cpp`:
- Around line 684-696: When BLE_STORE_MAX_BONDS is zero the function's index
parameter is unused and can trigger compiler warnings; update the `#else` block so
the unused parameter is explicitly marked (e.g. cast to void: (void)index;)
before returning NimBLEAddress{} to silence the warning. Locate the code around
the conditional compilation that returns NimBLEAddress{} (references:
BLE_STORE_MAX_BONDS, NimBLEAddress, and the index parameter) and add the
unused-parameter cast in that `#else` path.

---

Outside diff comments:
In `@src/NimBLEDevice.cpp`:
- Around line 659-676: The function leaves the parameter 'address' unused when
MYNEWT_VAL(BLE_STORE_MAX_BONDS) is zero, causing compiler warnings; to fix, add
a cast-to-void for the unused parameter (e.g., (void)address;) in the branch
where the bond-check code is disabled so 'address' is referenced; locate the
function using the 'address' parameter in this diff and mirror the pattern used
in 'injectPassKey' and 'injectConfirmPasskey' to silence the warning, then
return false as before.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0201025d-fd8a-4e72-8254-ec382b8f397d

📥 Commits

Reviewing files that changed from the base of the PR and between 0606e21 and 8977376.

📒 Files selected for processing (1)
  • src/NimBLEDevice.cpp

@h2zero h2zero force-pushed the fix-compile-warnings branch from 8977376 to 5a301f1 Compare March 18, 2026 14:47
@h2zero h2zero merged commit 2cb68e2 into master Mar 18, 2026
66 checks passed
@h2zero h2zero deleted the fix-compile-warnings branch March 18, 2026 15:08
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.

esp-idf 5.5.3 compatibility issue

1 participant