This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

device manager, bonding informations not restored from flash at reset

I'm trying to secure my peripheral by allowing only bonded devices to connect. I'm using device manager, when I bond a central with nrf master control, the bonding succeed ( BLE_GAP_EVT_AUTH_STATUS is called on device_manager_peripheral ), whitelist.irk_count is not null (> 0) and when I disconnect and turn off Bluetooth, no other central can connect, so the bonding succeeded.

but when I turn off the peripheral and on again, any device can connect. the whitelist.irk_count is null, so no key is not restored from persistent memory. how can I restore the bonding information on persistnat memory. I think the key is saved on flash as shown in the image below (on the left is the memory dump when no bonding performed, on the right is memory dump when bonding performed)

main () {
..
bool erase_bonds;
..
device_manager_init(erase_bonds);
..
}

static void device_manager_init(bool erase_bonds)
{
    uint32_t               err_code;
    dm_init_param_t        init_param = {.clear_persistent_data = erase_bonds};
    dm_application_param_t register_param;


        // Initialize peer device handle.
    err_code = dm_handle_initialize(&m_bonded_peer_handle);
    APP_ERROR_CHECK(err_code);
    
    // Initialize persistent storage module.
    err_code = pstorage_init();
    APP_ERROR_CHECK(err_code);

    err_code = dm_init(&init_param);
    APP_ERROR_CHECK(err_code);

    memset(&register_param.sec_param, 0, sizeof(ble_gap_sec_params_t));

    register_param.sec_param.bond         = SEC_PARAM_BOND;
    register_param.sec_param.mitm         = SEC_PARAM_MITM;
    register_param.sec_param.io_caps      = SEC_PARAM_IO_CAPABILITIES;
    register_param.sec_param.oob          = SEC_PARAM_OOB;
    register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE;
    register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE;
    register_param.evt_handler            = device_manager_evt_handler;
    register_param.service_type           = DM_PROTOCOL_CNTXT_GATT_SRVR_ID;

    err_code = dm_register(&m_app_handle, &register_param);
    APP_ERROR_CHECK(err_code);
}

static uint32_t device_manager_evt_handler(dm_handle_t const * p_handle,
                                           dm_event_t const  * p_event,
                                           ret_code_t        event_result)
{
      APP_ERROR_CHECK(event_result);

    switch (p_event->event_id)
    {
        case DM_EVT_DEVICE_CONTEXT_LOADED: // Fall through.
        case DM_EVT_SECURITY_SETUP_COMPLETE:
            m_bonded_peer_handle = (*p_handle);
            break;
    }

    return NRF_SUCCESS;
}

image description

Related