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

Handling SoftDevice events yourself and the need for nrf_sdh_ related calls

I handle events directly by invoking the following code after the sd_app_evt_wait(); call:

        while(true)
        {
           // uint32_t evt_id;

            // Don't do this if disabled, for example when writing flash at the end
            sd_softdevice_is_enabled(&enabled); // Don't do this if disabled, for example when writing flash at the end
            if (enabled != 1)
            {
                NRF_LOG_DEBUG("Restarting application");
                while(NRF_LOG_PROCESS());
                NVIC_SystemReset();
                return;
            }
            result = sd_ble_evt_get(NULL, &len);    // Get size of event
            if (result == NRF_ERROR_NOT_FOUND)      // If there aren't any, go back to wait
            {
                break;
            }
            evt_buf = (uint8_t *)calloc(1, len);                // Make space for event. evt_buf is 4-byte aligned
            result = sd_ble_evt_get((uint8_t *)evt_buf, &len);  // get the event
            if (result == NRF_SUCCESS)
            {
                ble_evt_t *evt = (ble_evt_t *)evt_buf;
                ble_evt_dispatch(evt);                          // dispatch event to handler. This handles only BTLE related events
            }
            else                                                // Hopefully no error but just in case log it.
            {                                                   // Should I do an NVIC_SystemReset() here?
                NRF_LOG_DEBUG("PENDING BLE Event return error: %u", result);
                free(evt_buf);  // Clean up
                break;          // back to wait
            }
            free(evt_buf);      // clean up and get the next event
        }

I poll the events and dispatch them to my handler as needed.

Now if I do this, do I need to make calls to nrf_sdh_enable_request()

and

nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start); ?

I will need to make some sd_ble_cfg_set() calls but what I am really wondering about is the so-called 'observers'. My app already makes the sd_ble_evt_get() calls so it doesn't seem that I need to tell any other 'observers' who is using SoftDevice. Am I missing something?

I am mainly concerned about these calls in nrf_sdh_ble_default_cfg_set()

ble_cfg.gap_cfg.role_count_cfg.central_role_count = NRF_SDH_BLE_CENTRAL_LINK_COUNT;
    ble_cfg.gap_cfg.role_count_cfg.central_sec_count  = MIN(NRF_SDH_BLE_CENTRAL_LINK_COUNT,
                                                            BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT);

    ret_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, *p_ram_start);

since I need to use both central and peripheral roles -  well at least partly. I have a peripheral that gets its data from unconnectable broadcasts, so I need to implement a scanner to get these broadcasts while the peripheral is connected.

Related