<?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>BLE event handlers, why so many! Plz explain structure!</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/85024/ble-event-handlers-why-so-many-plz-explain-structure</link><description>Afternoon folks, 
 I&amp;#39;m new to nordic, previously a TI developer. I&amp;#39;m still getting my head around the SDK. I&amp;#39;ve unfortunately been left with some poorly organised, very-un-annotated firmware to try and decipher. I am currently trying to get my head around</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 25 Feb 2022 07:41:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/85024/ble-event-handlers-why-so-many-plz-explain-structure" /><item><title>RE: BLE event handlers, why so many! Plz explain structure!</title><link>https://devzone.nordicsemi.com/thread/354928?ContentTypeID=1</link><pubDate>Fri, 25 Feb 2022 07:41:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:353e111e-cfad-4396-b017-7d9aea13ffb2</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Sean&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think there is any simple way to create a list like that, unless you had some powerful code analysis tool that could figure it out.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It should be possible to find all the places where the event handler is registered with other modules. Either the event handler has to be passed to another module as a function pointer, where it would be stored in some local variable so that it can be called later, or it would have to be called directly by other modules using the function name (in which case the function would have to be declared &amp;quot;extern&amp;quot; in some of the header files).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Starting with a global file search for the function name would hopefully show all the places the callback is registered, or if it is declared extern.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE event handlers, why so many! Plz explain structure!</title><link>https://devzone.nordicsemi.com/thread/354798?ContentTypeID=1</link><pubDate>Thu, 24 Feb 2022 11:58:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:975916e9-2466-4024-85c8-af5f01afb404</guid><dc:creator>SeanHowsonTB</dc:creator><description>&lt;p&gt;Torbj&amp;oslash;rn, &lt;/p&gt;
&lt;p&gt;Thanks for the detailed reply.&lt;/p&gt;
&lt;p&gt;To be honest, i was second guessing myself a little with the whole, multi-languge stuff.&lt;/p&gt;
&lt;p&gt;I think my current aim is to gain a deeper understanding of the current code, and produce a working build for our upcoming prototyes, but eventually my aim will be to get rid of the C++&lt;/p&gt;
[quote userid="2116" url="~/f/nordic-q-a/85024/ble-event-handlers-why-so-many-plz-explain-structure/354793#354793"]Using C++ is very rare for Nordic projects, [/quote]
&lt;p&gt;...Thats all you had to say! &lt;/p&gt;
&lt;p&gt;I&amp;#39;m really not sure why it&amp;#39;s been done, besides the preference of the previous developer. But it seems to be causing numerous problems.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m still digesting the technical aspects to your reply, but in the meantime, one question has cropped up that i can&amp;#39;t seem to find an immediate clear answer to:&lt;/p&gt;
&lt;p&gt;We have talked about the &amp;quot;ble_evt_handler&amp;quot; being a &amp;quot;global/big&amp;quot; handler for all events and the other handlers &amp;quot;hrs_evt_handler&amp;quot;, for example, only responding to events relevant to them.&lt;/p&gt;
&lt;p&gt;This custom handler, which i&amp;#39;m assuming has been made up of butchered code, seems to respond to a great number of events, where might i look for a &amp;quot;list of things that trigger it&amp;quot;, in plain english, i&amp;#39;m looking for a list of events from various modules that say &amp;quot;when one of these events happens, trigger the registered observer&amp;quot;. Any clues as to where such lists are typically declared for other similar handlers (hrs handler, bas handler, for example)?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks again for all the help. Always appreciated!&lt;/p&gt;
&lt;p&gt;Sean&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE event handlers, why so many! Plz explain structure!</title><link>https://devzone.nordicsemi.com/thread/354793?ContentTypeID=1</link><pubDate>Thu, 24 Feb 2022 11:46:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4271af1c-a938-4252-9b64-45aeb6374dbf</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Sean&lt;/p&gt;
&lt;p&gt;Thanks to Markku for the clarification. I thought &amp;quot;cus&amp;quot; sounded familiar, but when I didn&amp;#39;t find it in the SDK I stopped searching for it.&amp;nbsp;&lt;/p&gt;
[quote user="SeanHowsonTB"]Do i really need to run a handler, constaining an enormous switch statment, the each of the cases for which, runs a function which converts the event to a custom event type (yet still identical in it&amp;#39;s raw value) and then redirects the execution, to the next handler.... the next handler also containing an equally large and identical switch statement, the cases for this statement ACTUALLY handle the event. ALSO the event is handled completely separately by the ble_event_handler.[/quote]
&lt;p&gt;No, running the switch statement twice in a nested fashion makes little sense to me, and&amp;nbsp;to my knowledge this is not done by any of the standard examples. The normal approach is for the top level handler to do the filtering, and cast the event to the specific event type (which as you indicate is not a conversion per se, just a type cast), and then forward the event to a more specific handler that is only used for a single type of event, or a small subset of events.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code snippet below is from the ble_nus.c implementation, which does it in this way for events relevant to the NUS service:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void ble_nus_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    if ((p_context == NULL) || (p_ble_evt == NULL))
    {
        return;
    }

    ble_nus_t * p_nus = (ble_nus_t *)p_context;

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_nus, p_ble_evt);
            break;

        case BLE_GATTS_EVT_WRITE:
            on_write(p_nus, p_ble_evt);
            break;

        case BLE_GATTS_EVT_HVN_TX_COMPLETE:
            on_hvx_tx_complete(p_nus, p_ble_evt);
            break;

        default:
            // No implementation needed.
            break;
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In this case the casting of the event is handled by the on_write(), on_connect() and on_hvx_tx_complete() functions, but since the event type is checked by the top level handler it is not checked again in the event specific handler.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user="SeanHowsonTB"]I just can&amp;#39;t see a reason to process, for example, the &amp;quot;on connect&amp;quot; event, in both the &amp;quot;big&amp;quot; ble_evt_handler AND the &amp;quot;small&amp;quot; ble_gap_evt_handler, seems to lack organisation/clarity?[/quote]
&lt;p&gt;I agree. Within one file or module each event should only be processed once, but you could have multiple files that all have to process the connected event, which is normal.&amp;nbsp;&lt;/p&gt;
[quote user="SeanHowsonTB"]Is this normal practice within the Nordic world? It&amp;#39;s certainly not been normal for me in previous roles.[/quote]
&lt;p&gt;Using C++ is very rare for Nordic projects, but it happens from time to time. It is definitely not something I recommend unless you have previous experience using C++ in embedded systems, and have a good reason for doing so.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Many of the drivers and modules in the SDK are instance based, which would make them well suited to be re-implemented in an object oriented language like C++, but all the time the entire SDK is written in C it is quite a bit of work to adapt it to C++.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Also, many of the modules in the SDK won&amp;#39;t build in C++ because they use syntax structures that are only supported in C (those do exist unforunately), and in this case the best workaround is to only call these libraries from C files, or rewriting the library files to be C++ compliant, which I would strongly discourage.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE event handlers, why so many! Plz explain structure!</title><link>https://devzone.nordicsemi.com/thread/354646?ContentTypeID=1</link><pubDate>Wed, 23 Feb 2022 15:01:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:63bda1b2-f12c-4c58-96fb-04cbfbce616b</guid><dc:creator>SeanHowsonTB</dc:creator><description>&lt;p&gt;Hi Guys,&lt;/p&gt;
&lt;p&gt;Thank you both for your replies and thanks for answering all my questions.&lt;/p&gt;
&lt;p&gt;After posting i did find the app_uart project and actually found some annotations in the code i have that had clearly been copied and pasted from the uart project.&lt;/p&gt;
&lt;p&gt;Thanks for breaking down the different types of handler.&lt;/p&gt;
&lt;p&gt;So to add clarity to my previous question:&lt;/p&gt;
[quote userid="111510" url="~/f/nordic-q-a/85024/ble-event-handlers-why-so-many-plz-explain-structure"]3: Do i really need 3 handlers for a simple event?[/quote]
&lt;p&gt;What i should have said was: &lt;/p&gt;
&lt;p&gt;Do i really need to run a handler, constaining an enormous switch statment, the each of the cases for which, runs a function which converts the event to a custom event type (yet still identical in it&amp;#39;s raw value) and then redirects the execution, to the next handler.... the next handler also containing an equally large and identical switch statement, the cases for this statement ACTUALLY handle the event. ALSO the event is handled completely separately by the ble_event_handler.&lt;/p&gt;
&lt;p&gt;For now, i have removed most of the gubbins from the handler registered as an observer and simply passed the event to the secondary handler (without any form of sorting/processing)&lt;/p&gt;
&lt;p&gt;I now understand the origins of the re-direct and the way each handler is aimed at a specific subgroup of events (illustrated by their names i suppose).&lt;/p&gt;
&lt;p&gt;I can understand why there would be several of the &amp;quot;smaller&amp;quot; handlers running for different categories of event. I just can&amp;#39;t see a reason to process, for example, the &amp;quot;on connect&amp;quot; event, in both the &amp;quot;big&amp;quot; ble_evt_handler AND the &amp;quot;small&amp;quot; ble_gap_evt_handler, seems to lack organisation/clarity?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;General opinion question here:&lt;/p&gt;
&lt;p&gt;Some of my issues are currently stemming from the multiple file types present in the project. A number of the main handler files have been written in Cpp,&amp;nbsp; with a &amp;quot;be careful not to make more than one instance of each class&amp;quot; - notice issued against each of them. For me this set off red flags.&lt;/p&gt;
&lt;p&gt;The previous author came from a non industrial background and used cpp because it was the language that made most sense to him, but the more i read through the code, the more it seems to be cramming a round peg into a square hole. All sorts of things have been modified, to try to accomadate the cpp code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Is this normal practice within the Nordic world? It&amp;#39;s certainly not been normal for me in previous roles.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Sean&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;P.S. for now i think you&amp;#39;ve clarified enough for me that i will avoid posting any code, but i appreciate the offer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE event handlers, why so many! Plz explain structure!</title><link>https://devzone.nordicsemi.com/thread/354585?ContentTypeID=1</link><pubDate>Wed, 23 Feb 2022 13:15:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e2f11380-940d-4a7d-b587-b30a8ab04b64</guid><dc:creator>mrono</dc:creator><description>[quote userid="2116" url="~/f/nordic-q-a/85024/ble-event-handlers-why-so-many-plz-explain-structure/354569#354569"]It sounds a bit like the project you are looking at is based on the ble_app_uart project, and they replaced the ble_nus module (Nordic UART Service) with their own service called ble_cus.[/quote]
&lt;p&gt;I think the project may be based on this tutorial by Nordic:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/NordicPlayground/nRF5x-custom-ble-service-tutorial"&gt;https://github.com/NordicPlayground/nRF5x-custom-ble-service-tutorial&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It uses the name &amp;quot;cus&amp;quot; for the custom service, and also does the event handling in the way described.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE event handlers, why so many! Plz explain structure!</title><link>https://devzone.nordicsemi.com/thread/354569?ContentTypeID=1</link><pubDate>Wed, 23 Feb 2022 12:54:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a8e80392-df46-49d4-b6a4-0441606cb90f</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Sean&lt;/p&gt;
&lt;p&gt;It sounds a bit like the project you are looking at is based on the ble_app_uart project, and they replaced the ble_nus module (Nordic UART Service) with their own service called ble_cus.&amp;nbsp;&lt;/p&gt;
[quote user=""]1: What is the difference between all of the &amp;quot;smaller&amp;quot;, more specific handlers, such as &amp;quot;gatt_evt_handler&amp;quot;, &amp;quot;hrs_evt_handler&amp;quot; etc. and the seemingly more global &amp;quot;ble_evt_handler&amp;quot;?[/quote]
&lt;p&gt;The main difference is that the ble_evt_handler will receive every single BLE related event from the Bluetooth stack (SoftDevice), and as such can receive a very wide number of different events. These are sorted into groups based on what part of the stack they originate from, such as the common BLE events, GAP , GATT client and GATT server events.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The gatt_evt_handler and hrs_evt_handler are associated with specific SDK modules, and will only forward event related to their domain. For example the gatt module will only forward events related to data length or MTU exchange, while the hrs module will only forward events related to the heart rate service.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Both of these modules will have their own BLE event handler (similar to ble_evt_handler), but will only filter out events that they care about, and trigger the gatt_evt_handler or hrs_evt_handler when&amp;nbsp;there is something happening that the application might need to respond to.&amp;nbsp;&lt;/p&gt;
[quote user=""]2: Why do these &amp;quot;smaller&amp;quot; handlers need to contain functions that trigger a secondary handler, can the first handler not just service the event itself?[/quote]
&lt;p&gt;Typically triggering a secondary handler is done to pass events up one layer in the application (from the gatt module to the application layer for instance). These modules will be self contained as much as it is reasonable, but certain events or state changes need to be forwarded to the caller of the module, which would be the application main file in most examples.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I agree it would be odd to have several handler forwards happening within one file or module, I don&amp;#39;t think this is happening in any of the standard modules or examples, but I have seen cases where the ble_evt_handler would do some pre-filtering of events, and then forward the events to various other handlers based on the event type (you could have a ble_gap_evt_handler for only handling BLE_GAP events for instance).&amp;nbsp;&lt;/p&gt;
[quote user=""]3: Do i really need 3 handlers for a simple event?[/quote]
&lt;p&gt;In the same file or module, no, but in the case of BLE events it is common to have a large number of modules that all register for BLE events, even if they all just care about a small subset of the events available.&amp;nbsp;&lt;/p&gt;
[quote user=""]4: I come from a background of &amp;quot;simplicity leads to functioning reliable code&amp;quot; - If i were to rationalise this, knowing the application will likely not have a great number of different events to process, is there some way i can do without one or two of these handlers?&amp;nbsp;[/quote]
&lt;p&gt;It is a bit hard to answer this question without having a look at the implementation. Would it be possible for you to share the related code snippets, if not the whole application?&lt;/p&gt;
&lt;p&gt;I can make the case private,&amp;nbsp;in case you don&amp;#39;t want to share any code in a public ticket.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>