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

Events handling

I am looking at this example: github.com/.../main.c

In ble_evt_dispatch (line 315), this function forwards an BLE event to all of the modules. By looking at ble_evt_t, none of the fields does not signify which event is for which. So how does the individual modules knows that for example, BLE_GAP_EVT_CONNECTED is suppose to be for that module?

If there is an way to identify the events based on the UUID, then we can properly route the events to the correct BLE event handler.

Or am I just missing something here? Thank you.

  • In ble_evt_t there is a header which holds the event id. The different modules use the event id to check if it should handle the event or not. This is usually done using a switch statement. Example code:

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            break; // BLE_GAP_EVT_CONNECTED
    
        case BLE_GAP_EVT_DISCONNECTED:
            err_code = bsp_indication_set(BSP_INDICATE_IDLE);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            break; // BLE_GAP_EVT_DISCONNECTED
    
        default:
            // No implementation needed.
            break;
    }
    
  • Thanks for your reply.

    I do understand that part. However, lets say that we organize the services into several different modules. So the event forwarding function would look like:

    static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
    {
        service1_on_ble_evt(p_ble_evt);
        service2_on_ble_evt(p_ble_evt);
        service3_on_ble_evt(p_ble_evt);
        service4_on_ble_evt(p_ble_evt);
        service5_on_ble_evt(p_ble_evt);
    }
    

    Then, if the event is BLE_GAP_EVT_CONNECTED, then how does the individual event handler knows which service is BLE_GAP_EVT_CONNECTED event for?

    Traditionally, event handlers that I have done usually returns a boolean which signifies that the particular event handler knew the event and properly handled it. So it saves from other handlers from determining that the event wasn't meant for that handler.

  • The BLE_GAP_EVT_CONNECTED is sent from the SoftDevice to the application code. This is a type of BLE stack event, and usually only the on_ble_evt() function handles these events. But the event itself is not sent specifically to any event handler. It’s up to the application code on how these events should be handled.

Related