Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nimble/store: Fix nimble store behavior when CCCDs exceed static defined limit #790

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions nimble/host/include/host/ble_gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,20 @@ int ble_gap_unpair(const ble_addr_t *peer_addr);
*/
int ble_gap_unpair_oldest_peer(void);

/**
* Similar to `ble_gap_unpair_oldest_peer()`, except it makes sure that the
* peer received in input parameters is not deleted.
*
* @param peer_addr Address of the peer (not to be deleted)
*
* @return 0 on success;
* A BLE host HCI return code if the controller
* rejected the request;
* A BLE host core return code on unexpected
* error.
*/
int ble_gap_unpair_oldest_except(const ble_addr_t *peer_addr);

#define BLE_GAP_PRIVATE_MODE_NETWORK 0
#define BLE_GAP_PRIVATE_MODE_DEVICE 1

Expand Down
32 changes: 31 additions & 1 deletion nimble/host/src/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -5594,7 +5594,7 @@ ble_gap_unpair_oldest_peer(void)
}

if (num_peers == 0) {
return 0;
return BLE_HS_ENOENT;
prasad-alatkar marked this conversation as resolved.
Show resolved Hide resolved
}

rc = ble_gap_unpair(&oldest_peer_id_addr);
Expand All @@ -5605,6 +5605,36 @@ ble_gap_unpair_oldest_peer(void)
return 0;
}

int
ble_gap_unpair_oldest_except(const ble_addr_t *peer_addr)
{
ble_addr_t peer_id_addrs[MYNEWT_VAL(BLE_STORE_MAX_BONDS)];
int num_peers;
int rc, i;

rc = ble_store_util_bonded_peers(
&peer_id_addrs[0], &num_peers, MYNEWT_VAL(BLE_STORE_MAX_BONDS));
if (rc != 0) {
return rc;
}

if (num_peers == 0) {
return BLE_HS_ENOENT;
}

for (i = 0; i < num_peers; i++) {
if (ble_addr_cmp(peer_addr, &peer_id_addrs[i]) != 0) {
break;
}
}

if (i >= num_peers) {
return BLE_HS_ENOMEM;
}

return ble_gap_unpair(&peer_id_addrs[i]);
}

void
ble_gap_passkey_event(uint16_t conn_handle,
struct ble_gap_passkey_params *passkey_params)
Expand Down
16 changes: 9 additions & 7 deletions nimble/host/src/ble_store_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,15 @@ ble_store_util_status_rr(struct ble_store_status_event *event, void *arg)
switch (event->event_code) {
case BLE_STORE_EVENT_OVERFLOW:
switch (event->overflow.obj_type) {
case BLE_STORE_OBJ_TYPE_OUR_SEC:
case BLE_STORE_OBJ_TYPE_PEER_SEC:
case BLE_STORE_OBJ_TYPE_CCCD:
return ble_gap_unpair_oldest_peer();
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that for CCCD we should have different handling and if we want to unpair older peer we should not unpair peer for which we store CCCD. If there is nothing to unpair just return the error.

In the CCCD event there is and ble_addr we should use to make sure that one is no unpaired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rymanluk agreed !! I have made relevant changes. For CCCD, if ble_gap_unpair_oldest_peer is unable to delete any peer, we continue to find other peers occupying CCCDs in storage. I have also modified callback function (ble_store_util_iter_peer_cccd) to skip the current (i.e. for which CCCDs need to be stored) peer entry.


default:
return BLE_HS_EUNKNOWN;
case BLE_STORE_OBJ_TYPE_OUR_SEC:
case BLE_STORE_OBJ_TYPE_PEER_SEC:
return ble_gap_unpair_oldest_peer();
case BLE_STORE_OBJ_TYPE_CCCD:
/* Try unpairing oldest peer except current peer */
return ble_gap_unpair_oldest_except(&event->overflow.value->cccd.peer_addr);

default:
return BLE_HS_EUNKNOWN;
}

case BLE_STORE_EVENT_FULL:
Expand Down