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

Timing behavior of GAP SoftDevice calls and events

Hi everyone,

I was wondering how GAP calls and events are synchronized within the SoftDevice.

Example:

// conn_handle is updated in the CONNECTED and DISCONNECTED event handlers
sd_ble_gap_connect_cancel();
if (conn_handle != BLE_CONN_HANDLE_INVALID)
    sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);

Considering this code is executed without interruption (e.g. on APP_PRIORITY_LOW level).

What happens if before sd_ble_gap_connect_cancel(), a connection has already been established and the connection event is still pending in the SoftDevice event queue? I cannot disconnect before I receive the connection ID in the CONNECTED event handler. So I cannot trigger the disconnect manually from this code location.

I know that the possibility that this scenario occurs is pretty low, but I want to create a implementation that works 100% safe/reliable by design.

Question: Will the sd_ble_gap_connect_cancel() function implicitly detect that a connection has already been established, trigger the disconnect and remove the CONNECTED event from the queue? Or is it possible that I may still receive a connected event after this code section?

Related