<?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>Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/94474/why-is-the-softdevice-automatically-naking-packets</link><description>I&amp;#39;m developing a BLE application on the nRF52832 that uses NRF_SDH_DISPATCH_MODEL_APPSH to receive events from the SoftDevice. My application involves using some software-implemented elliptic-curve cryptographic computations, which can take up to a second</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 07 Dec 2022 15:36:23 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/94474/why-is-the-softdevice-automatically-naking-packets" /><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/399493?ContentTypeID=1</link><pubDate>Wed, 07 Dec 2022 15:36:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:483b40a9-7f4d-4416-8505-ba27df1adc51</guid><dc:creator>Elias</dc:creator><description>&lt;p&gt;Yes, I understand all that.&amp;nbsp;I think there might be some confusion over what I&amp;#39;m asking. I&amp;#39;ve figured out the &amp;quot;&lt;span&gt;What is the SoftDevice doing here?&amp;quot; in concept, and now I&amp;#39;m trying to figure out the second part: &amp;quot;Is there a recommendation on how&amp;nbsp;to determine the appropriate size of my App Scheduler based on this &amp;quot;automatic NAK&amp;quot; behavior?&amp;quot; I&amp;#39;ve opened a &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/94634/how-do-i-size-the-app-scheduler-to-take-advantage-of-naks-from-the-softdevice"&gt;new thread&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/399330?ContentTypeID=1</link><pubDate>Wed, 07 Dec 2022 08:22:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3e2e993-7925-403b-a810-9a4e8af671d9</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user="Elias Simon"]During ECC computations, the loop which calls&amp;nbsp;&lt;span&gt;nrf_sdh_evts_poll() is blocked. So, the SoftDevice can continue receiving events, but the application will not process them. If the&amp;nbsp;SoftDevice has 6 events in its own buffer, wouldn&amp;#39;t that mean that it called app_sched_event_put() only 6 times before the next time nrf_sdh_evts_poll() is called?&lt;/span&gt;[/quote]
&lt;p&gt;The events buffer is inside the softdevice memory region and not the application memory region. The size of app_scheduler event buffer does not correlate directly to the size of the softdevice events buffer. If the call to nrf_sdh_evts_poll is blocked, during the ECC computations, then the application is not pulling the events out of the softdevice events buffer. Which means that the softdevice event buffer gets filled up quite fast.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The size of the app_scheduler buffer corresponds to how many times you can call&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.0.0/group__app__scheduler.html#ga65ba3e30627bb62882d15cde87b610f0"&gt;app_sched_event_put&lt;/a&gt;&amp;nbsp;without calling&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.0.0/group__app__scheduler.html#gab725820c32a8d05379db4567cf30e862"&gt;app_sched_execute&lt;/a&gt;. The sched events can be any (in our case these are softdevice events) data.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In simple design, the longer the thread which calls nrf_sdh_evt_poll is blocked in the midst of BLE activity that can generate softdevice events, the faster it is possible to fill up softdevice specific internal event buffer (not controlled by app and not the same as app_scheduler event buffer)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/399288?ContentTypeID=1</link><pubDate>Tue, 06 Dec 2022 22:15:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3bad0eb6-55ad-4594-ad2f-75ff64152635</guid><dc:creator>Elias</dc:creator><description>&lt;p&gt;Then&amp;nbsp;why can I induce NRF_ERROR_NO_MEM during ECC computations when my App Scheduler is size 50?&lt;/p&gt;
&lt;p&gt;During ECC computations, the loop which calls&amp;nbsp;&lt;span&gt;nrf_sdh_evts_poll() is blocked. So, the SoftDevice can continue receiving events, but the application will not process them. If the&amp;nbsp;SoftDevice has 6 events in its own buffer, wouldn&amp;#39;t that mean that it called app_sched_event_put() only 6 times before the next time nrf_sdh_evts_poll() is called?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now, my application does support 4 concurrent connections, but that only accounts for (6*4=)24 events.&amp;nbsp; All my testing for this issue has been done with only one connection. There are no&amp;nbsp;events in the application that&amp;nbsp;happen comparably frequently to SoftDevice events, so the App Scheduler isn&amp;#39;t filling for some other reason.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/399091?ContentTypeID=1</link><pubDate>Tue, 06 Dec 2022 07:20:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:122ba5dc-7642-4e25-b1c4-65ed5eb43c31</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Maximum number is 6 events/connection as this is the buffer set inside the softdevice. So softdevice will start nacking the connection packets if there are already 6 events due to be pulled by the application at any time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/399030?ContentTypeID=1</link><pubDate>Mon, 05 Dec 2022 16:25:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0a080bba-804a-4c07-8f60-6a20466fbdd8</guid><dc:creator>Elias</dc:creator><description>&lt;p&gt;I meant: &amp;quot;How do I determine the maximum number of events that the SoftDevice can hold?&amp;quot;. &amp;quot;Worst-case&amp;quot; meaning with events that can happen arbitrarily&amp;nbsp;fast&amp;nbsp;and have the smallest corresponding data structure (e.g. zero-length writes).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/398923?ContentTypeID=1</link><pubDate>Mon, 05 Dec 2022 08:37:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8550d08e-7ca3-48de-92f9-8cdd6430338c</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user="Elias Simon"]Oh! I was&amp;nbsp;about to ask how this works, but I just realized that it&amp;#39;s because the application hasn&amp;#39;t called&amp;nbsp;&lt;span&gt;nrf_sdh_evts_poll()! The difference between&amp;nbsp;NRF_ERROR_NO_MEM&amp;nbsp;and LL NAKs is that it depends on whether the App Scheduler or the SoftDevice&amp;#39;s buffers can hold more events!&lt;/span&gt;[/quote]
&lt;p&gt;Yes, that is correct.&lt;/p&gt;
[quote user="Elias Simon"]So, I want to keep leveraging the LL NAK behavior. This means that I just have to make sure that my&amp;nbsp;App Scheduler is larger than the maximum number of events that the SoftDevice can hold[/quote]
&lt;p&gt;Correct.&lt;/p&gt;
[quote user="Elias Simon"]How do I determine this number for the worst-case (i.e. events from a malicious Central)?[/quote]
&lt;p&gt;You only remain connected to a trusted peer. After the connection your establish that the peer is in fact the one you want to be connected and then the rest of the communication works on trust basis that the peer would behave in a predictable way. If you are trying to make an application that connects to any central that sent a connect request, then you probably can keep a count of how many events/given_time are being received from peer and disconnect from them if the events/given_time exceed your application expectations.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/398898?ContentTypeID=1</link><pubDate>Mon, 05 Dec 2022 03:05:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6606c0a4-c3df-4566-acc6-8f3d2b01dad9</guid><dc:creator>Elias</dc:creator><description>&lt;blockquote&gt;
&lt;p&gt;&lt;span&gt;It is just waiting for the application to pull the events that are already notified.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Oh! I was&amp;nbsp;about to ask how this works, but I just realized that it&amp;#39;s because the application hasn&amp;#39;t called&amp;nbsp;&lt;span&gt;nrf_sdh_evts_poll()! The difference between&amp;nbsp;NRF_ERROR_NO_MEM&amp;nbsp;and LL NAKs is that it depends on whether the App Scheduler or the SoftDevice&amp;#39;s buffers can hold more events!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;So, I want to keep leveraging the LL NAK behavior. This means that I just have to make sure that my&amp;nbsp;App Scheduler is larger than the maximum number of events that the SoftDevice can hold. How do I determine this number for the worst-case (i.e. events from a malicious Central)?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/398883?ContentTypeID=1</link><pubDate>Sun, 04 Dec 2022 18:44:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:48978a02-84c1-40ee-8d5c-cd77d2d679c4</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;The softdevice is not doing anything wrong here. It is just waiting for the application to pull the events that are already notified. You could create a TIMER interrupt at higher priority and print&amp;nbsp;on RTT&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s140.api.v7.3.0%2Fgroup___b_l_e___c_o_m_m_o_n___f_u_n_c_t_i_o_n_s.html&amp;amp;anchor=ga412b12b43c253dd744bcf574d6e86f43"&gt;sd_ble_evt_get&lt;/a&gt;&amp;nbsp;like every 50ms or every 100ms and see the periods where where your application is not pulling the events and making the event buffer full forcing the softdevice to NACK incoming packets. The question as to why this happens depends on your application contexts. If you provide me the information of all the contexts in your applicaiton and the packet processing times that your application take, then I might be able to answer why you see this behavior when you change the app_scheduler buffer size to 60.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/398827?ContentTypeID=1</link><pubDate>Fri, 02 Dec 2022 17:49:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:92a32fc2-600f-4c01-aa79-9169b8d58d5a</guid><dc:creator>Elias</dc:creator><description>&lt;p&gt;I&amp;#39;ve edited my original question to clarify.&lt;/p&gt;
&lt;p&gt;1. My ECC computations are already running in&amp;nbsp;APP_IRQ_PRIORITY_THREAD.&lt;/p&gt;
&lt;p&gt;2. This is irrelevant. I&amp;#39;m not looking to solve a particular issue, I&amp;#39;m looking to learn more about&amp;nbsp;the&amp;nbsp;solution&amp;nbsp;I&amp;#39;ve already found (increasing the app scheduler to 60).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why is the SoftDevice automatically NAKing packets?</title><link>https://devzone.nordicsemi.com/thread/398676?ContentTypeID=1</link><pubDate>Fri, 02 Dec 2022 06:35:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a475b196-221a-4d95-8584-4281805f1340</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user=""]Once I discovered this, I tried finding the upper limits of how many packets&amp;nbsp;my device might have to receive over the air during&amp;nbsp;the ECC computations. What I&amp;#39;ve found is that if I increase the size of the App Scheduler to about 60, then no matter how long the ECC computations take, the SoftDevice will not enqueue more than about 50 packets. On a sniffer, I see that the SoftDevice starts NAKing a packet at the Link Layer for an extended period (definitely not due to interference) after it correctly receives a stream of about 50.[/quote]
&lt;p&gt;What you are seeing might not be just due to the app_Scheduler. Each connection in your application have arond 6-8 buffers for the incoming events from the softdevice. It is totally upto the application as to when it can pull these events from the softdevice. The softdevice notifies the application using the &lt;a href="https://infocenter.nordicsemi.com/topic/sds_s132/SDS/s1xx/sd_resource_reqs/app_signal_sw_interrupt.html?cp=4_7_3_0_6_1"&gt;SWI2_IRQn&lt;/a&gt;&amp;nbsp;at interrupt priority 6. If your application is doing the cryptographic computations at an higher interrupt priority then it will easily mask this event irq for longer periods of time. And it is also possible that more softdevice events are generated which are not being pulled by the application as it was busy doing something else in other higher priority contexts.&amp;nbsp; &lt;em&gt;&lt;strong&gt;Once these event buffers are full, the softdevice will not accept any more packets from the peer and starts to NACK them.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You have two choices.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Do your ECC computations at a lower priority than the SWI2_IRQn priority (that is lower than priority 6)&lt;/li&gt;
&lt;li&gt;reduce the BLE activities whiles doing ECC computations so there are less events generated inside the softdevice.&lt;/li&gt;
&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>