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?

Parents
  • Hi,

    sd_ble_gap_connect_cancel() cancels an initiated connection procedure. It will never perform a disconnect or remove connection events, and this is also true in cases where the connection procedure has successfully finished and there is a pending connect event.

    Upon success the return value is NRF_SUCCESS. In the case when a connection has been established it will return NRF_ERROR_INVALID_STATE. You will then have to pull out the CONNECTED event and do a regular sd_ble_gap_disconnect(). You will also get NRF_ERROR_INVALID_STATE if you have not started a connection procedure, or if the connection procedure has timed out. See the Central Connection Establishment and Termination Message Sequence Chart for reference.

    The race condition between sd_ble_gap_connect_cancel() and the part of the SoftDevice initiating the connection is accounted for by the SoftDevice. (I.e. the return value of sd_ble_gap_connect_cancel() can be safely trusted.)

    Regards, Terje

Reply
  • Hi,

    sd_ble_gap_connect_cancel() cancels an initiated connection procedure. It will never perform a disconnect or remove connection events, and this is also true in cases where the connection procedure has successfully finished and there is a pending connect event.

    Upon success the return value is NRF_SUCCESS. In the case when a connection has been established it will return NRF_ERROR_INVALID_STATE. You will then have to pull out the CONNECTED event and do a regular sd_ble_gap_disconnect(). You will also get NRF_ERROR_INVALID_STATE if you have not started a connection procedure, or if the connection procedure has timed out. See the Central Connection Establishment and Termination Message Sequence Chart for reference.

    The race condition between sd_ble_gap_connect_cancel() and the part of the SoftDevice initiating the connection is accounted for by the SoftDevice. (I.e. the return value of sd_ble_gap_connect_cancel() can be safely trusted.)

    Regards, Terje

Children
Related