<?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>Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/16539/triggering-event-handler-upon-cccd-for-notifications-change</link><description>In the ble_nus service (SDK11.0.0 S132 nRF52), what is the simplest way to modify on_write() to send inform the main application the CCCD for notifications has changed: 
 static void on_write(ble_nus_t * p_nus, ble_evt_t * p_ble_evt){
ble_gatts_evt_write_t</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 20 Sep 2016 19:32:25 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/16539/triggering-event-handler-upon-cccd-for-notifications-change" /><item><title>RE: Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/thread/63291?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 19:32:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4fc4e58-5d96-402e-8fa1-2919cf0d6880</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;in that case just add one more variable in ble_nus_t&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;bool cccd_changed;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and on write&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void on_write(ble_nus_t * p_nus, 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_nus-&amp;gt;rx_handles.cccd_handle)
    &amp;amp;&amp;amp;
    (p_evt_write-&amp;gt;len == 2)
   )
{
    if (ble_srv_is_notification_enabled(p_evt_write-&amp;gt;data))
    {
        p_nus-&amp;gt;is_notification_enabled = true;
    }
    else
    {
        p_nus-&amp;gt;is_notification_enabled = false;
    }
    p_nus-&amp;gt;cccd_changed = true;
    p_nus-&amp;gt;data_handler(p_nus, p_evt_write-&amp;gt;data, p_evt_write-&amp;gt;len);
}
else if (
         (p_evt_write-&amp;gt;handle == p_nus-&amp;gt;tx_handles.value_handle)
         &amp;amp;&amp;amp;
         (p_nus-&amp;gt;data_handler != NULL)
        )
{
    p_nus-&amp;gt;data_handler(p_nus, p_evt_write-&amp;gt;data, p_evt_write-&amp;gt;len);
}
else
{
    // Do Nothing. This event is not relevant for this service.
}}


static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
  if(p_nus-&amp;gt;cccd_changed)
   {
     p_nus-&amp;gt;cccd_changed = false;
     // do your processing here
   }
   
   ....
   ....
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/thread/63290?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 19:12:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7eef5a9-7f85-4a9c-b478-88495d078131</guid><dc:creator>Tosa</dc:creator><description>&lt;p&gt;I think I confused you by saying &amp;quot;main application&amp;quot;. What I mean is that I want the event handler p_nus-&amp;gt;data_handler() to be called with some information stating that the CCCD has changed. What is the simplest way to do this?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/thread/63289?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 19:05:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bdc7d87a-1821-422b-bb23-f48d875ff3d3</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;then it will still be executed in SWI interrupt context, not in the main context.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/thread/63288?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 15:07:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:060b4bf0-d74e-4018-9db0-affaa4a824a7</guid><dc:creator>Tosa</dc:creator><description>&lt;p&gt;Is it possible to call this handler p_nus-&amp;gt;data_handler(p_nus, p_evt_write-&amp;gt;data, p_evt_write-&amp;gt;len); when the CCCD event occurs? The handler would need to be able to check the fields and see if it&amp;#39;s a CCCD event. Using the scheduler seems overkill and it&amp;#39;s not used in the example to call the event handler, right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/thread/63287?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 15:05:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4ff0762f-a80d-465e-9fde-5e51b65c83db</guid><dc:creator>Tosa</dc:creator><description>&lt;p&gt;Seems overkill to poll it when there&amp;#39;s a stack event available that just needs to be sent back to app&amp;#39;s handler function, no?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/thread/63285?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 13:02:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05278262-0547-4464-8ca6-865cd04c19f8</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;You can use app_scheduler to queue this event when notification is changed and pull this in main thread.&lt;/p&gt;
&lt;p&gt;You can find documentation here for &lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk52.v0.9.0%2Fgroup__app__scheduler.html&amp;amp;resultof=%22scheduler%22%20%22schedul%22%20"&gt;SDK scheduler&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Also find some explanation about how scheduler works &lt;a href="https://devzone.nordicsemi.com/question/38180/ble-stack-event-handling/#38216"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Triggering event handler upon CCCD for notifications change</title><link>https://devzone.nordicsemi.com/thread/63286?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 05:21:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ca8509f8-4093-4daf-acb6-5cc8f31d61b8</guid><dc:creator>frogofmagic</dc:creator><description>&lt;p&gt;Maybe set a timer to check p_nus-&amp;gt;is_notification_enabled every 100ms?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>