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

how to initialize event handler??

Hi all, i am doing a BLE project using nrf51822 hardware, in this project my requirement is to initialize the event, for doing that i took ble_app_proximity,ble_app_hrs as reference code ,

  so what i did to meet my requirement is ,
  from main() i called services_init()
  and in this i intialize 
  ble_hrs_init_t->evt_handler=evt_handler;[refered from ble_app_hrs ]
  and my evt_handlert defination is like this 
  evt_handler(ble_hrs_t *p_nus,  ble_hrs_evt_t * p_evt)
  it seems there is two type of structure related  to evnt
  1)typedef struct
	{
		ble_hrs_evt_type_t evt_type;                        /**< Type of event. */
	} ble_hrs_evt_t;
 2)typedef struct
	{
		ble_evt_hdr_t header;                 /**< Event header. */
		union
		{
			ble_common_evt_t  common_evt;         /**< Common Event, evt_id in BLE_EVT_* series. */
			ble_gap_evt_t     gap_evt;            /**< GAP originated event, evt_id in BLE_GAP_EVT_* series. */
			ble_l2cap_evt_t   l2cap_evt;          /**< L2CAP originated event, evt_id in BLE_L2CAP_EVT* series. */
			ble_gattc_evt_t   gattc_evt;          /**< GATT client originated event, evt_id in BLE_GATTC_EVT* series. */
			ble_gatts_evt_t   gatts_evt;          /**< GATT server originated event, evt_id in BLE_GATTS_EVT* series. */
		} evt;
	} ble_evt_t;
  so my doubt is:
  1) which data structure should use as second pararameter of  evt_handler either ble_hrs_evt_t or ble_evt_t?
  2)if i use  ble_hrs_evt_t as second parameter, then this has member  ble_hrs_evt_type_t which is enum of this type
  typedef enum
 {
   BLE_HRS_EVT_NOTIFICATION_ENABLED,                   /**< Heart Rate value notification enabled event. */
   BLE_HRS_EVT_NOTIFICATION_DISABLED                   /**< Heart Rate value notification disabled event. */
 } ble_hrs_evt_type_t;
 so in this can i define my own event type?
     3) under what situations is one data structure preferred over other, for example in ble_hrs ble_hrs_evt_t; is used 
     4) under ble_app_proximity project, the ble_evt_t is used and i wanted to know the reason behind using this structure as compared to 
         point 3.

Can any one please share information related to this....? Thanks Renu

  • can anyone please share information related to this quickly it will be really helpful. thanks

  • FormerMember
    0 FormerMember
    1. The second parameter of the event handler should be ble_evt_t. When receiving a BLE event, it is in the format of ble_evt_t.
    2. When receiving an event, ble_evt_t should used, see 1. As you can see in ble_hrs.c, ble_hrs_evt_t is used to "process" a received event in on_hrm_cccd_write(..). on_hrm_cccd_write is called in on_write, and on_write(..)receives the BLE event in the format of ble_evt_t.

    Edit:

    ble_app_proximity: the purpose of the event handler on_ias_evt is to handle immediate alert service events. BLE_IAS_EVT_ALERT_LEVEL_UPDATED-is passed to the application from ble_evt_dispatch(..) --> ble_ias_on_ble_evt(..) --> on_write(..) --> p_ias->evt_handler(..):

     static void on_write(ble_ias_t * p_ias, ble_evt_t * p_ble_evt)
    {
        ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    
        if ((p_evt_write->handle == p_ias->alert_level_handles.value_handle) && (p_evt_write->len == 1))
        {
            // Alert level written, call application event handler
            ble_ias_evt_t evt;
    
            evt.evt_type           = BLE_IAS_EVT_ALERT_LEVEL_UPDATED;
            evt.params.alert_level = p_evt_write->data[0];
    
            p_ias->evt_handler(p_ias, &evt);
        }
    } 
    

    ble_app_hrs is built the same way: ble_evt_dispatch(..) --> ble_hrs_on_ble_evt(..) --> on_write(..) --> on_hrm_cccd_write(..) . However, there are no events associated with notifications enabled or disabled. And therefore, it is no need for an event handler, so the event handler that is called is null:

    static void on_hrm_cccd_write(ble_hrs_t * p_hrs, ble_gatts_evt_write_t * p_evt_write)
    {
        if (p_evt_write->len == 2)
        {
            // CCCD written, update notification state
            if (p_hrs->evt_handler != NULL)
            {
                ble_hrs_evt_t evt;
    
                if (ble_srv_is_notification_enabled(p_evt_write->data))
                {
                    evt.evt_type = BLE_HRS_EVT_NOTIFICATION_ENABLED;
                }
                else
                {
                    evt.evt_type = BLE_HRS_EVT_NOTIFICATION_DISABLED;
                }
    
                p_hrs->evt_handler(p_hrs, &evt);
            }
        }
    }
    

    The purpose of softdevice_sys_evt_handler_set(..) is to register system events from the softDevice, see its documentation here.

    The purpose of softdevice_ble_evt_handler_set(..)is to register a handler for BLE events, see its documentation here.

  • Hi Kristin, Here is my confusion.

    In main.c [PROJECT == BLE_APP_PROXIMITY.UVPROJ] services_init(void)===>ias_init()===>ias_init_obj.evt_handler = on_ias_evt; so both p_ias->evt_handler = p_ias_init->evt_handler are pointing towards on_ias_evt() function.

    whereas in ble_hrs project in main.c services_init(void)===>ble_hrs_init()==> memset(&hrs_init, 0, sizeof(hrs_init)); so hrs_init is NULL and so is code as well p_hrs->evt_handler = p_hrs_init->evt_handler; so both p_hrs->evt_handler = p_hrs_init->evt_handler are pointing towards NULL if both are null, then this below function does NOTHING,am i right on this ?

    static void on_hrm_cccd_write(ble_hrs_t * p_hrs, ble_gatts_evt_write_t * p_evt_write) { if (p_evt_write->len == 2) { // CCCD written, update notification state if (p_hrs->evt_handler != NULL) { ble_hrs_evt_t evt;

            if (ble_srv_is_notification_enabled(p_evt_write->data))
            {
                evt.evt_type = BLE_HRS_EVT_NOTIFICATION_ENABLED;
            }
            else
            {
                evt.evt_type = BLE_HRS_EVT_NOTIFICATION_DISABLED;
            }
            
            p_hrs->evt_handler(p_hrs, &evt);
        }
    }
    

    }

    1. why is there no FUNCTION [meaning there is NULL = p_hrs->evt_handler = p_hrs_init->evt_handler] in the evt_handler in BLE HRS code, whereas there is one event handler [ias_init =p_ias->evt_handler = p_ias_init->evt_handler] in case of proximity code?

    3.how are these two functions different from each other in functionality.

    // Register with the SoftDevice handler module for BLE events.
    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    APP_ERROR_CHECK(err_code);
    
    // Register with the SoftDevice handler module for BLE events.
    err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
    
    1. From your answer, looks like there are 2 things a. event b. ble event, can you please explain what is the difference between two?

    2. In case of Proximity code, there are two paths to evt_handler code

    way1

    services_init(void)===>ias_init()===>ias_init_obj.evt_handler

    way2

    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch)==>ble_evt_dispatch==> ble_ias_on_ble_evt(&m_ias, p_ble_evt);===>on_write(p_ias, p_ble_evt);==> on_write(ble_ias_t * p_ias, ble_evt_t * p_ble_evt) { ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;

    if ((p_evt_write->handle == p_ias->alert_level_handles.value_handle) && (p_evt_write->len == 1))
    {
        // Alert level written, call application event handler
        ble_ias_evt_t evt;
        
        evt.evt_type           = BLE_IAS_EVT_ALERT_LEVEL_UPDATED;
        evt.params.alert_level = p_evt_write->data[0];
    
        p_ias->evt_handler(p_ias, &evt);
    

    In this proximity case, the evt_handler is assigned through way1 and called through way 2

    In case of ble hrs case, the evt_handler is NOT assigned, but rather called, how is this possible ?

    thanks

Related