<?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>SD shutdown &amp;amp; Gatt Queue &amp;amp; Discovery</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/56604/sd-shutdown-gatt-queue-discovery</link><description>Hello ! I have a few questions related to shutting down (disabling) the soft device, and the effects on the gatt queue and discovery: 
 Question 1: The gatt queue module performs a lot of actions in the background and I haven&amp;#39;t quite figured it all out</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 20 Jan 2020 10:30:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/56604/sd-shutdown-gatt-queue-discovery" /><item><title>RE: SD shutdown &amp; Gatt Queue &amp; Discovery</title><link>https://devzone.nordicsemi.com/thread/229809?ContentTypeID=1</link><pubDate>Mon, 20 Jan 2020 10:30:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8a12aa28-2374-4642-88e2-6599096ec926</guid><dc:creator>bjorn-spockeli</dc:creator><description>[quote user="Shockel"]Thanks, Bjorn, maybe I wasn&amp;#39;t using the right terminology. In Question 1, I was referring to the&amp;nbsp;m_ble_gatt_queue that many services use in the example apps (for example ANCS). I believe it is for queued writes. looking at nrf_ble_gq.c it uses a number of queues internally. I was wondering if that needs special attention to &amp;quot;clean up&amp;quot;. And whether some queue content may still exist when I restart the SoftDevice.[/quote]
&lt;p&gt;&amp;nbsp;Ah, now I understand. Yes, the Gatt Queue Module is used by some of our central examples.&amp;nbsp;The SoftDevice does not keep track of requests through a re-initialization, but it seems that the GATT Queue module may try to process a queued item later if the SD returns&amp;nbsp;NRF_ERROR_BUSY to one of the relevant API calls. If the SoftDevice is disabled when such a retry is attempted, then you&amp;#39;ll enter the&amp;nbsp;request_err_code_handle. Looking at the GATT Queue code, it seems that you could check the&amp;nbsp;nrf_ble_gq_t::p_req_queue with&amp;nbsp;nrf_queue_is_empty. If the queue is empty then you can disable the SD without risking the GATT queue module attempting to process a request when the sd is disabled.&amp;nbsp;&lt;/p&gt;
[quote user="Shockel"]Question 3: So you think I should just manually set &amp;quot;discovery in progress&amp;quot; to false after closing ?[/quote]
&lt;p&gt;No, I would call&amp;nbsp;&amp;nbsp;&lt;span&gt;ble_db_discovery_close() when you get the&amp;nbsp;BLE_GAP_EVT_DISCONNECTED. Then the discorvery module will handle the&amp;nbsp;discovery_in_progress&amp;nbsp;flag internally.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void on_disconnected(ble_db_discovery_t       * p_db_discovery,
                            ble_gap_evt_t      const * p_evt)
{
    if (p_evt-&amp;gt;conn_handle == p_db_discovery-&amp;gt;conn_handle)
    {
        p_db_discovery-&amp;gt;discovery_in_progress = false;
        p_db_discovery-&amp;gt;conn_handle           = BLE_CONN_HANDLE_INVALID;
    }
}
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Shockel"]BTW. Do I need to disconnect before shutting down the SoftDevice ? (I am the peripheral)[/quote]
&lt;p&gt;&amp;nbsp;Yes, I would disconnect all active connections before disabling the Softdevice.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;/p&gt;
&lt;p&gt;Bjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SD shutdown &amp; Gatt Queue &amp; Discovery</title><link>https://devzone.nordicsemi.com/thread/229448?ContentTypeID=1</link><pubDate>Thu, 16 Jan 2020 15:00:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f10a1c35-521f-4338-b3f0-97ed5d57ceca</guid><dc:creator>Shockel</dc:creator><description>&lt;p&gt;Thanks, Bjorn, maybe I wasn&amp;#39;t using the right terminology. In Question 1, I was referring to the&amp;nbsp;m_ble_gatt_queue that many services use in the example apps (for example ANCS). I believe it is for queued writes. looking at nrf_ble_gq.c it uses a number of queues internally. I was wondering if that needs special attention to &amp;quot;clean up&amp;quot;. And whether some queue content may still exist when I restart the SoftDevice.&lt;/p&gt;
&lt;p&gt;Question 2: clear. Thanks.&lt;/p&gt;
&lt;p&gt;Question 3: So you think I should just manually set &amp;quot;discovery in progress&amp;quot; to false after closing ?&lt;/p&gt;
&lt;p&gt;ble_db_discovery_close(&amp;amp;m_db_disc);&lt;br /&gt; m_db_disc.discovery_in_progress = false;&lt;/p&gt;
&lt;p&gt;BTW. Do I need to disconnect before shutting down the SoftDevice ? (I am the peripheral)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SD shutdown &amp; Gatt Queue &amp; Discovery</title><link>https://devzone.nordicsemi.com/thread/229440?ContentTypeID=1</link><pubDate>Thu, 16 Jan 2020 14:44:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b1dede1a-ad70-4be3-9357-6602a4037c92</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Hi Shockel,&lt;/p&gt;
[quote user=""]Question 1: The gatt queue module performs a lot of actions in the background and I haven&amp;#39;t quite figured it all out.&amp;nbsp; If the soft device is disabled, does that automatically clear the gatt queue ? Should it ? And how would I go about it if I &amp;quot;manually&amp;quot; wanted to do so ?[/quote]
&lt;p&gt;&amp;nbsp;The GATT module handle the connection parameter for the active Bluetooth connections and handle events like BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST and&amp;nbsp;BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST, see&lt;strong&gt;&amp;nbsp;&lt;a title="Software Development Kit" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/lib_ble_gatt.html?resultof=%22%47%61%74%74%22%20%22%67%61%74%74%22%20%22%6d%6f%64%75%6c%65%22%20%22%6d%6f%64%75%6c%22%20"&gt;nRF5 SDK v16.0.0: Experimental: GATT Module&lt;/a&gt;.&amp;nbsp;&lt;/strong&gt;When the SoftDevice is disabled, then all the connections are terminated and you will no longer receive any BLE events from the SoftDevice. So in short, there is no queue or similar that needs to be cleared.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;[quote user=""][/quote]&lt;/p&gt;
&lt;p&gt;Question 2: The example project ble_app_gzll shows some steps to perform when shutting down the soft device, and what to do when you re-enable it. For example, it shows that you don&amp;#39;t want to re-initiablize&amp;nbsp;nrf_ble_qwr_init(), while other init()&amp;#39;s you do. But other examples use quite a few more modules/services. Can you advise what to do with the following:&lt;/p&gt;
&lt;p&gt;gap_params_init()&lt;/p&gt;
&lt;p&gt;gatt_init()&lt;/p&gt;
&lt;p&gt;peer_manager_init()&lt;/p&gt;
&lt;p&gt;Do they need care on shutdown or re-enabling ? Especially the peer manager seems to call&amp;nbsp;gcm_init() and runs out of memory ? So maybe not re-init the peer manager ? I am just concerned about lingering states that shouldn&amp;#39;t survive a soft device shutdown.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;gap_params_init() and&amp;nbsp;gatt_init() should be called after you re-enable the SoftDevice, i.e. after you call nrf_sdh_ble_enable()&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;The Peer Manager should only be initialized once.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;[quote user=""][/quote]&lt;/p&gt;
&lt;p&gt;Question 3: Is the DB discovery properly terminated when disabling the soft device ?&lt;/p&gt;
&lt;p&gt;As I debugged the DB discovery, I found a few peculiarities. It appears that the module is designed such that you can have multiple instances. Yet when you call&amp;nbsp;&lt;/p&gt;
&lt;p&gt;ble_db_discovery_close()&lt;/p&gt;
&lt;p&gt;it seems to &amp;quot;brutally&amp;quot; set&amp;nbsp;m_num_of_handlers_reg=0. Not sure if that should be the case. (this is really only for your internal purposes, I don&amp;#39;t have multiple instances).&lt;/p&gt;
&lt;p&gt;But I do see that the above ...close() call, which I figured I had to call when shutting down the soft device, it doesn&amp;#39;t set&lt;/p&gt;
&lt;p&gt;p_db_discovery-&amp;gt;discovery_in_progress = false;&lt;/p&gt;
&lt;p&gt;And since the discovery module is created as a static variable via a macro with initialization, when re-enabling the soft device, it thinks that the discovery is still in progress.&lt;/p&gt;
&lt;p&gt;Could be a bug, or it is supposed to be like that ... Maybe you can share your thoughts.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;The SoftDevice is does not touch any of the software modules used in the application. The DB discovery module simply calls the SD API to start service/characteristic and descriptor and handles the received events indicating that a service, characteristic or descriptor has been found, e.g.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP&lt;/li&gt;
&lt;li&gt;BLE_GATTC_EVT_CHAR_DISC_RSP&lt;/li&gt;
&lt;li&gt;BLE_GATTC_EVT_DESC_DISC_RSP&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The SoftDevice does not keep track of if there was a service discovery in progress when it was disabled and then resume this when the SoftDevice is re-enabled.&lt;/p&gt;
&lt;p&gt;Also note that w&lt;span&gt;hen using more than one DB Discovery instance, the ble_db_discovery_close function should be called for each instance.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bjørn&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>