<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>how to initialize event handler??</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/3339/how-to-initialize-event-handler</link><description>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()</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 07 Aug 2014 06:55:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/3339/how-to-initialize-event-handler" /><item><title>RE: how to initialize event handler??</title><link>https://devzone.nordicsemi.com/thread/12167?ContentTypeID=1</link><pubDate>Thu, 07 Aug 2014 06:55:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2dc8e26f-26d8-4d8d-8cb9-9fa0842da573</guid><dc:creator>renu</dc:creator><description>&lt;p&gt;Hi Kristin,
Here is my confusion.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In main.c [PROJECT == BLE_APP_PROXIMITY.UVPROJ]
services_init(void)===&amp;gt;ias_init()===&amp;gt;ias_init_obj.evt_handler = on_ias_evt;
so both p_ias-&amp;gt;evt_handler = p_ias_init-&amp;gt;evt_handler are pointing towards
on_ias_evt() function.&lt;/p&gt;
&lt;p&gt;whereas in ble_hrs project in main.c
services_init(void)===&amp;gt;ble_hrs_init()==&amp;gt; memset(&amp;amp;hrs_init, 0, sizeof(hrs_init));
so hrs_init is NULL and so is code as well
p_hrs-&amp;gt;evt_handler                 = p_hrs_init-&amp;gt;evt_handler;
so both p_hrs-&amp;gt;evt_handler = p_hrs_init-&amp;gt;evt_handler are pointing towards
NULL
if both are null, then this below function does NOTHING,am i right on this ?&lt;/p&gt;
&lt;p&gt;static void on_hrm_cccd_write(ble_hrs_t * p_hrs, ble_gatts_evt_write_t * p_evt_write)
{
if (p_evt_write-&amp;gt;len == 2)
{
// CCCD written, update notification state
if (p_hrs-&amp;gt;evt_handler != NULL)
{
ble_hrs_evt_t evt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;        if (ble_srv_is_notification_enabled(p_evt_write-&amp;gt;data))
        {
            evt.evt_type = BLE_HRS_EVT_NOTIFICATION_ENABLED;
        }
        else
        {
            evt.evt_type = BLE_HRS_EVT_NOTIFICATION_DISABLED;
        }
        
        p_hrs-&amp;gt;evt_handler(p_hrs, &amp;amp;evt);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;ol start="2"&gt;
&lt;li&gt;why is there no FUNCTION [meaning there is NULL = p_hrs-&amp;gt;evt_handler = p_hrs_init-&amp;gt;evt_handler] in the evt_handler in BLE HRS code,
whereas there is one event handler [ias_init =p_ias-&amp;gt;evt_handler =
p_ias_init-&amp;gt;evt_handler] in case of proximity code?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;3.how are these two functions different from each other in functionality.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// 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);
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start="4"&gt;
&lt;li&gt;
&lt;p&gt;From your answer, looks like there are 2 things
a. event
b. ble event, can you please explain what is the difference between two?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In case of Proximity code, there are two paths to evt_handler code&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;way1&lt;/h1&gt;
&lt;p&gt;services_init(void)===&amp;gt;ias_init()===&amp;gt;ias_init_obj.evt_handler&lt;/p&gt;
&lt;h1&gt;way2&lt;/h1&gt;
&lt;p&gt;err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch)==&amp;gt;ble_evt_dispatch==&amp;gt;
ble_ias_on_ble_evt(&amp;amp;m_ias, p_ble_evt);===&amp;gt;on_write(p_ias, p_ble_evt);==&amp;gt;
on_write(ble_ias_t * p_ias, ble_evt_t * p_ble_evt)
{
ble_gatts_evt_write_t * p_evt_write = &amp;amp;p_ble_evt-&amp;gt;evt.gatts_evt.params.write;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if ((p_evt_write-&amp;gt;handle == p_ias-&amp;gt;alert_level_handles.value_handle) &amp;amp;&amp;amp; (p_evt_write-&amp;gt;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-&amp;gt;data[0];

    p_ias-&amp;gt;evt_handler(p_ias, &amp;amp;evt);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this proximity case, the evt_handler is assigned through way1 and called through
way 2&lt;/p&gt;
&lt;p&gt;In case of ble hrs case, the evt_handler is NOT assigned, but rather called, how is
this possible ?&lt;/p&gt;
&lt;p&gt;thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to initialize event handler??</title><link>https://devzone.nordicsemi.com/thread/12166?ContentTypeID=1</link><pubDate>Wed, 06 Aug 2014 07:03:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3e64aa7b-016a-4dc9-b23e-f79d6f65b62d</guid><dc:creator>renu</dc:creator><description>&lt;p&gt;thanks for replying Kristin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to initialize event handler??</title><link>https://devzone.nordicsemi.com/thread/12165?ContentTypeID=1</link><pubDate>Tue, 05 Aug 2014 07:35:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bfd6e3b2-3881-4168-bce9-c917ec2f3aa2</guid><dc:creator>FormerMember</dc:creator><description>&lt;ol&gt;
&lt;li&gt;The second parameter of the event handler should be &lt;code&gt;ble_evt_t&lt;/code&gt;. When receiving a BLE event, it is in the format of &lt;code&gt;ble_evt_t&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When receiving an event, &lt;code&gt;ble_evt_t&lt;/code&gt; should used, see 1. As you can see in ble_hrs.c, &lt;code&gt;ble_hrs_evt_t&lt;/code&gt; is used to &amp;quot;process&amp;quot; a received event in &lt;code&gt;on_hrm_cccd_write(..)&lt;/code&gt;. &lt;code&gt;on_hrm_cccd_write&lt;/code&gt; is called in &lt;code&gt;on_write&lt;/code&gt;, and &lt;code&gt;on_write(..)&lt;/code&gt;receives the BLE event in the format of &lt;code&gt;ble_evt_t&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Edit:&lt;/p&gt;
&lt;p&gt;ble_app_proximity: the purpose of the event handler &lt;code&gt;on_ias_evt&lt;/code&gt; is to handle immediate alert service events. BLE_IAS_EVT_ALERT_LEVEL_UPDATED-is passed to the application from
&lt;code&gt;ble_evt_dispatch(..) --&amp;gt; ble_ias_on_ble_evt(..) --&amp;gt; on_write(..) --&amp;gt; p_ias-&amp;gt;evt_handler(..)&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; static void on_write(ble_ias_t * p_ias, ble_evt_t * p_ble_evt)
{
    ble_gatts_evt_write_t * p_evt_write = &amp;amp;p_ble_evt-&amp;gt;evt.gatts_evt.params.write;

    if ((p_evt_write-&amp;gt;handle == p_ias-&amp;gt;alert_level_handles.value_handle) &amp;amp;&amp;amp; (p_evt_write-&amp;gt;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-&amp;gt;data[0];

        p_ias-&amp;gt;evt_handler(p_ias, &amp;amp;evt);
    }
} 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ble_app_hrs is built the same way: &lt;code&gt;ble_evt_dispatch(..) --&amp;gt; ble_hrs_on_ble_evt(..) --&amp;gt; on_write(..) --&amp;gt; on_hrm_cccd_write(..)&lt;/code&gt; . 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:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void on_hrm_cccd_write(ble_hrs_t * p_hrs, ble_gatts_evt_write_t * p_evt_write)
{
    if (p_evt_write-&amp;gt;len == 2)
    {
        // CCCD written, update notification state
        if (p_hrs-&amp;gt;evt_handler != NULL)
        {
            ble_hrs_evt_t evt;

            if (ble_srv_is_notification_enabled(p_evt_write-&amp;gt;data))
            {
                evt.evt_type = BLE_HRS_EVT_NOTIFICATION_ENABLED;
            }
            else
            {
                evt.evt_type = BLE_HRS_EVT_NOTIFICATION_DISABLED;
            }

            p_hrs-&amp;gt;evt_handler(p_hrs, &amp;amp;evt);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The purpose of  &lt;code&gt;softdevice_sys_evt_handler_set(..)&lt;/code&gt; is to register system events from the softDevice, see its documentation &lt;a href="https://devzone.nordicsemi.com/documentation/nrf51/6.0.0/s110/html/a00742.html#ga200d4e1f1c70a46e6de6343f0a7d84b1"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The purpose of &lt;code&gt;softdevice_ble_evt_handler_set(..)&lt;/code&gt;is to register a handler for BLE events, see its documentation &lt;a href="https://devzone.nordicsemi.com/documentation/nrf51/6.0.0/s110/html/a00741.html#gae54951e939156d5f05fa4d27fed1dc65"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to initialize event handler??</title><link>https://devzone.nordicsemi.com/thread/12164?ContentTypeID=1</link><pubDate>Mon, 04 Aug 2014 04:18:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:63348e49-662b-4b54-aa95-6e960de5cbca</guid><dc:creator>renu</dc:creator><description>&lt;p&gt;can anyone please share information related to this quickly it will be really helpful.
thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>