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

Using the new Connection State module to decide role

I'm developing an application supporting concurrent central and peripheral connections and using the new Connection State module in SDK 10 to decide whether a BLE stack event should be handled by the central or peripheral event handler.

My ble_evt_dispatch() function is based closely on the Nordic ble_app_hrs_rscs_relay example, i.e.:

static void ble_evt_dispatch(ble_evt_t *p_ble_evt)
{
    uint16_t conn_handle;
    uint16_t role;

    ble_conn_state_on_ble_evt(p_ble_evt);   // Must be called before any event handler that uses conn state module

    // The connection handle should really be retrievable for any event type.
    conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    role = ble_conn_state_role(conn_handle);

    if (role == BLE_GAP_ROLE_PERIPH)
    {   
        on_ble_peripheral_evt();
        ...
    }
    else if (role == BLE_GAP_ROLE_CENTRAL)
    {
        on_ble_central_evt();
        ...
    }
}

This worked fine until I had a scan timeout and discovered that the BLE_GAP_EVT_TIMEOUT handler in my on_ble_central_evt() was not getting called. On investigation I found that ble_conn_state_role() is returning BLE_GAP_ROLE_INVALID on a scan timeout because there is no connection so conn_handle is not set.

The ble_app_hrs_rscs_relay example has a BLE_GAP_EVT_TIMEOUT case in its on_ble_central_evt() function, but I wonder if this can ever get called. Shouldn't the ble_evt_dispatch() function include an explicit check for role == BLE_GAP_ROLE_INVALID in which it call functions to handle connectionless events such as BLE_GAP_EVT_TIMEOUT, or have I misunderstood how to use the ble_conn_state_role() function?

Parents
  • So ble_gap_evt_timeout_t contains the src data fields(source). This will give you the GAP Timeout sources.

    In the Relay example we handle
    BLE_GAP_TIMEOUT_SRC_ADVERTISING in ble_advertising_on_ble_evt,
    BLE_GAP_TIMEOUT_SRC_CONN are handled in on_ble_central_evt.
    BLE_GAP_TIMEOUT_SRC_SCAN are not handled as there is no timeout configured in the example, so this would have to be stopped using sd_ble_gap_scan_stop.
    BLE_GAP_TIMEOUT_SRC_SECURITY_REQUEST is not handled in the experimental release of the peer manager, but we have a task to add this to the "production" release.

    If you want to handle BLE_GAP_TIMEOUT_SRC_SCAN I suggest you do this in on_ble_central_evt. Note that connecting and scanning is only used in the central role (or observer role for scanning), which is why this is handled as a central event. Check src like this:
    if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)

  • Thanks. I assume it's because there is no connection so p_ble_evt->evt.gap_evt.conn_handle is set to BLE_CONN_HANDLE_INVALID

Reply Children
No Data
Related