From eabb54f15df7df69951ce8f49a62d3bd702dd686 Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 15 Mar 2020 18:00:41 +0100 Subject: [PATCH] Workaround for a issue when the FDS (flash data storage for BLE softdevice)) memory is full (it prevent the app from running correctly and just display a black screen) : if the fds cannot init, erase the memory and try again. --- src/BLE/BleManager.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/BLE/BleManager.c b/src/BLE/BleManager.c index cf56dd48..48653982 100644 --- a/src/BLE/BleManager.c +++ b/src/BLE/BleManager.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "nrf_sdh_soc.h" #include "BleManager.h" @@ -270,11 +271,42 @@ void ble_manager_advertising_event_handler(ble_adv_evt_t ble_adv_evt) { } } +bool record_delete_next(void) +{ + fds_find_token_t tok = {0}; + fds_record_desc_t desc = {0}; + + if (fds_record_iterate(&desc, &tok) == FDS_SUCCESS) + { + ret_code_t rc = fds_record_delete(&desc); + if (rc != FDS_SUCCESS) + { + return false; + } + + return true; + } + else + { + /* No records left to delete. */ + return false; + } +} + void ble_manager_init_peer_manager() { ble_gap_sec_params_t sec_param; ret_code_t err_code; err_code = pm_init(); + if(err_code != NRF_SUCCESS) { + // Many errors can occur here, but the most probable is the "Storage full" error from sdk/components/libraries/fds/fds.c : FDS_ERR_NO_PAGES. + // TODO : it erases the whole memory, it's not nice to do that... + NRF_LOG_WARNING("Error while initializing BLE peer management, Erasing all record from memory"); + do { + + } while(record_delete_next()); + err_code = pm_init(); + } APP_ERROR_CHECK(err_code); memset(&sec_param, 0, sizeof(ble_gap_sec_params_t)); @@ -320,6 +352,19 @@ void ble_manager_peer_manager_event_handler(pm_evt_t const *p_evt) { ble_manager_start_advertising(&delete_bonds); break; + case PM_EVT_STORAGE_FULL: { + // Run garbage collection on the flash. + err_code = fds_gc(); + if (err_code == FDS_ERR_BUSY || err_code == FDS_ERR_NO_SPACE_IN_QUEUES) + { + // Retry. + } + else + { + APP_ERROR_CHECK(err_code); + } + }break; + default: break; }