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

Differentiation of Events in BLE Libraries

I'm looking at the ble_app_hrs example and I'm confused about how the drivers are able to differentiate between events originating from various services/characteristics. For example, in main.c under the ble_evt_dispatch we have:

void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
    dm_ble_evt_handler(p_ble_evt);
    ble_hrs_on_ble_evt(&m_hrs, p_ble_evt);
    ble_bas_on_ble_evt(&m_bas, p_ble_evt);
    ble_conn_params_on_ble_evt(p_ble_evt);
    on_ble_evt(p_ble_evt);
}

So on any ble event the ble_bas_on_ble_evt function will be called, with the p_ble_evt

void ble_bas_on_ble_evt(ble_bas_t * p_bas, ble_evt_t * p_ble_evt) {
    switch (p_ble_evt->header.evt_id) {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_bas, p_ble_evt);
            break;
            ...
    }
}

Which in turn calls on_connect which associates the handle of the event with the BAS

static void on_connect(ble_bas_t * p_bas, ble_evt_t * p_ble_evt)
{
    p_bas->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
}

My question is where does it differentiate between the source of the event, so that it doesn't store the handle every time anything triggers a BLE_GAP_EVT_CONNECTED event.

  • It doesn't have to differentiate the source for a connection event, a connection event is global to the peripheral, not restricted to a given service, so in this case each service which cares about whether or not it's connected stores the connection handle in its own data structure.

    If you look at other events, like handling reads and writes, you'll see that those are differentiated by checking the handle of the characteristic being read/written which is available for those events. For an example of that, look at the ble_rscs code, there are many other examples too.

    if (p_evt_write->handle == p_rscs->meas_handles.cccd_handle)
    {
        on_meas_cccd_write(p_rscs, p_evt_write);
    }
    
Related