Skip to content

Commit e509403

Browse files
MirkoCovizzieivindj-nordic
authored andcommitted
lib: peer_manager: fix multiple peers delete
Fixes a bug where only the first bonded peer would be deleted when requesting for all peers to be deleted. Signed-off-by: Mirko Covizzi <[email protected]>
1 parent 81a4bd6 commit e509403

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/peer_manager/modules/peer_data_storage.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ static volatile bool m_peer_delete_deferred;
5050

5151
static struct bm_zms_fs fs;
5252

53+
/* Keeps track of the number of peers currently under delete processing. */
54+
static atomic_t delete_counter;
55+
5356
/* Function for dispatching events to all registered event handlers. */
5457
static void pds_evt_send(pm_evt_t *p_event)
5558
{
@@ -244,6 +247,9 @@ static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt)
244247
} else if (p_evt->result < 0) {
245248
/* Unrecoverable error. */
246249
LOG_ERR("BM_ZMS delete failed with error %d", p_evt->result);
250+
251+
atomic_dec(&delete_counter);
252+
247253
pds_evt.evt_id = PM_EVT_PEER_DELETE_FAILED;
248254
pds_evt.params.peer_delete_failed.error = NRF_ERROR_INTERNAL;
249255
pds_evt_send(&pds_evt);
@@ -253,8 +259,14 @@ static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt)
253259

254260
ret = find_next_data_entry_in_peer(peer_id, &next_entry_id);
255261
if (ret == NRF_SUCCESS) {
262+
/* Process the next entry for the peer. */
256263
m_peer_delete_deferred = true;
257264
} else if (ret == NRF_ERROR_NOT_FOUND) {
265+
atomic_dec(&delete_counter);
266+
267+
/* Process the next deleted peers, if any are present. */
268+
m_peer_delete_deferred = true;
269+
258270
pds_evt.evt_id = PM_EVT_PEER_DELETE_SUCCEEDED;
259271
peer_id_free(pds_evt.peer_id);
260272
pds_evt_send(&pds_evt);
@@ -467,7 +479,14 @@ uint32_t pds_peer_id_free(pm_peer_id_t peer_id)
467479
VERIFY_PEER_ID_IN_RANGE(peer_id);
468480

469481
(void)peer_id_delete(peer_id);
470-
peer_data_delete_process();
482+
483+
/* Only start processing on the first delete request.
484+
* `peer_data_delete_process` will iteratively take care of processing all the peers marked
485+
* for deletion.
486+
*/
487+
if (atomic_inc(&delete_counter) == 0) {
488+
peer_data_delete_process();
489+
}
471490

472491
return NRF_SUCCESS;
473492
}

0 commit comments

Comments
 (0)