<?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 Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/91486/ble-error-when-calling-disconnect-inside-callback-for-connected</link><description>Hi, 
 I am developing a peripheral in ble, and I receive the following error when I disconnect a new connection within the connection callback. 
 
 bt_conn_disconnect ( conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN ); 
 
 [00:00:07.533,966] &amp;lt;wrn&amp;gt; bt_att: att_get</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 09 Sep 2022 21:38:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/91486/ble-error-when-calling-disconnect-inside-callback-for-connected" /><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/385574?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2022 21:38:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf73d87a-d09a-4671-8b73-9e753cebe430</guid><dc:creator>douglas.malnati</dc:creator><description>&lt;p&gt;I understand.&amp;nbsp; Thank you.&lt;/p&gt;
&lt;p&gt;The work queue introduces the issues as well.&lt;/p&gt;
&lt;p&gt;Regarding nRF5 SDK, is that not deprecated in favor of NCS (Nordic Connect SDK)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/384518?ContentTypeID=1</link><pubDate>Mon, 05 Sep 2022 09:13:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5c3ba1a3-589a-49b0-9e6b-20d46e03c59d</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;For follow up questions, please consider creating a new ticket. I will be working on another project in the coming month, so I will not monitor my queue until October. Please feel free to link to this one for background information.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So in Zephyr, there are a lot of callback handlers in the different modules. One for your main application, one for the flash management (in case you are storing bonding data, and so on), and there are several callback handlers within the Bluetooth stack. When this event occurs, these event handlers are all called, and the order in which they are called is a random order. Therefore errors like this can happen when all the handlers have not yet had time to execute.&lt;/p&gt;
&lt;p&gt;Therefore, my suggestion to you is that you wait until all of these handlers have had the chance to run. The way that I suggested was to use a timer, but if you are not too keen on that, then I suggest you look into the k_work handler. The meaning of this (and the handler) is to handle the disconnect job at a different priority level than the Bluetooth callback itself. So that we know that they are handled in the correct order. Please see the section &amp;quot;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/kernel/services/threads/workqueue.html#submitting-a-work-item"&gt;Submitting a work item&lt;/a&gt;&amp;quot; in the NCS documentation.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I am not sure whether or not you are familiar with the nRF5 SDK, which is a more &amp;quot;bare metal&amp;quot; SDK. In there, you will not see these kinds of issues, because the SoftDevice would run a bigger part of the Bluetooth Stack, which would handle these events completely before letting the application know that they had happened. However, the complexity of an RTOS means you also need to take these scenarios into consideration. After all, it is still a single core chip running a complex RTOS with many threads running &amp;quot;concurrently&amp;quot;, and interrupt priorities still need to be taken into consideration.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/384396?ContentTypeID=1</link><pubDate>Fri, 02 Sep 2022 01:38:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e1fb1997-9b3f-4de1-89f1-245b83fcd736</guid><dc:creator>douglas.malnati</dc:creator><description>&lt;p&gt;Nothing obviously not working, but that&amp;#39;s not a confirmation, It&amp;#39;s hard for me to tell if there are any bugs because I&amp;#39;m in the initial development phases and haven&amp;#39;t really put the system to hard work yet, or prolonged and rigorous exercise.&lt;/p&gt;
&lt;p&gt;My primary concern is corruption of state within the zephyr code which will manifest itself much later in time (and will be very difficult to diagnose).&lt;/p&gt;
[quote userid="109356" url="~/f/nordic-q-a/91486/ble-error-when-calling-disconnect-inside-callback-for-connected/384385"]it is totally allowed to initiate a disconnection immediately after the connection has been created[/quote]
&lt;p&gt;Not clear your specific meaning here.&amp;nbsp; Do you mean specifically it is ok to call the disconnect function while still within the &amp;quot;connected&amp;quot; callback?&amp;nbsp; If you do mean that, what makes you sure that&amp;#39;s correct?&amp;nbsp; I have not seen any statements or documentation so far that have asserted that.&lt;/p&gt;
[quote userid="26071" url="~/f/nordic-q-a/91486/ble-error-when-calling-disconnect-inside-callback-for-connected/384138"]Remember that there are many handlers that subscribe to the BT events, such as the connected event[/quote]
&lt;p&gt;My thought is around this statement, that perhaps other subsystems within zephyr haven&amp;#39;t yet received the connected callback (if there are any) before I have, and there&amp;#39;s some kind of race condition.&amp;nbsp; Or the order in which the event is distributed is non-deterministic, or something.&amp;nbsp; But because it&amp;#39;s an intermittent problem, it tells me that the same code path isn&amp;#39;t being executed each time for equal events, and that&amp;#39;s within the zephyr functionality (and wrong).&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/384385?ContentTypeID=1</link><pubDate>Thu, 01 Sep 2022 22:18:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d3d06dd-8db9-4d13-87d7-c48b6c3f35b0</guid><dc:creator>Emil Lenngren</dc:creator><description>&lt;p&gt;If we disregard what errors/warnings you see in the log, is something buggy or not working as expected?&lt;/p&gt;
&lt;p&gt;If everything appears to work as expected, Nordic should consider removing these warnings/errors when the state is &amp;quot;&lt;span&gt;BT_CONN_DISCONNECTING&amp;quot;&lt;/span&gt; as it is totally allowed to initiate a disconnection immediately after the connection has been created.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/384377?ContentTypeID=1</link><pubDate>Thu, 01 Sep 2022 18:23:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b1be4a1c-ec41-4b23-bd79-ce07d6478ad5</guid><dc:creator>douglas.malnati</dc:creator><description>&lt;p&gt;Thank you for your response, however I feel the API needs to be written and clarified.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not seeing concrete statements about what I can/cannot do, and that is the function of an API, and it should be documented.&amp;nbsp; I am not finding documentation describing what I can/cannot do either and I asked to be directed to any which didn&amp;#39;t happen either.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span&gt;In BLE, as a peripheral, you are supposed to allow anyone to connect&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As a peripheral, I did let anyone connect, and subsequently chose to disconnect them.&amp;nbsp; There is a disconnect function I used to do that, so clearly this is a concept that is supported.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span&gt;I am just saying that the error says that you are not connected, and it is triggered from the action of trying to disconnect before the connection event has propagated though the operating system&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span&gt;I understand you are saying that.&amp;nbsp; But what you aren&amp;#39;t saying is when it is supported for me to disconnect, and you aren&amp;#39;t pointing me to documentation that tells me.&amp;nbsp; I&amp;#39;ve been suggested to &amp;quot;try&amp;quot; waiting, as though there isn&amp;#39;t a definitive supported answer to that question, which is exactly the point I&amp;#39;m struggling with here.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The API as I&amp;#39;m using it is already inconsistent, there is an intermittent&amp;nbsp;issue where sometimes there is a warning, other times not, for the exact same code path execution.&amp;nbsp; How should I know that when I &amp;quot;try&amp;quot; waiting I&amp;#39;m simply not seeing another instance of an intermittent issue?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The answer to all of these things is having an API which conclusively explains the right way to operate the system.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Please think if there is something specific you can say about how to use this API correctly.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Also please pass along a report that the API is not clear in this (and so many other) places.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/384347?ContentTypeID=1</link><pubDate>Thu, 01 Sep 2022 12:51:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:49c52535-0d17-4ddd-89c7-6d34e862f512</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I suggest that you look into the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/kernel/services/timing/timers.html?highlight=k_timer#using-a-timer-expiry-function"&gt;&amp;quot;Using a Timer Expiry function&amp;quot; section&lt;/a&gt;&amp;nbsp;in the NCS documentation.&amp;nbsp;&lt;/p&gt;
[quote user="douglas.malnati"] That&amp;#39;s the zephyr API&amp;#39;s job to present an interface which I can reliably work with to build an application.[/quote]
&lt;p&gt;In BLE, as a peripheral, you are supposed to allow anyone to connect. Whether you take action on the events generated from the connected device is up to you. There are mechanisms (bonding and so on) to prevent malicious devices from doing the wrong things.&lt;/p&gt;
[quote user="douglas.malnati"]Regarding ignoring the problem, should I ship a commercial product with outright errors being produced by the underlying library?[/quote]
&lt;p&gt;I am just saying that the error says that you are not connected, and it is triggered from the action of trying to disconnect before the connection event has propagated though the operating system (Zephyr).&lt;/p&gt;
&lt;p&gt;Sorry for the short answer, but I need to leave the office, and will be out for the weekend.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/384178?ContentTypeID=1</link><pubDate>Wed, 31 Aug 2022 14:24:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cfcf4540-aa28-472b-8407-040db2b80bc4</guid><dc:creator>douglas.malnati</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;1 - I already do call bt_conn_ref(conn).&lt;/p&gt;
&lt;p&gt;3 - conn-&amp;gt;state ==&amp;nbsp;&lt;span&gt;BT_CONN_DISCONNECTING.&amp;nbsp; When the connected callback fired, the state was&amp;nbsp;&lt;/span&gt;BT_CONN_CONNECTED and err=0.&lt;/p&gt;
&lt;p&gt;The zephyr disconnect callback does fire after I bt_conn_disconnect within the connect callback.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regarding points 2 and 4 - This sounds like &amp;quot;work around or ignore&amp;quot; the problem.&amp;nbsp; My specific question on this case is what the right way to use the API is.&amp;nbsp; I cannot find documentation that explains when I&amp;#39;m allowed to call which functions under which scenarios.&amp;nbsp; Can you please direct me?&lt;/p&gt;
&lt;p&gt;Adding a timer leads to many many negative issues, like my code now having to track that a connected conn &amp;quot;isn&amp;#39;t really supposed to be connected so ignore any callbacks from it.&amp;quot;&amp;nbsp; That&amp;#39;s the zephyr API&amp;#39;s job to present an interface which I can reliably work with to build an application.&lt;/p&gt;
&lt;p&gt;Regarding ignoring the problem, should I ship a commercial product with outright errors being produced by the underlying library?&amp;nbsp; Is there library state corruption occurring?&amp;nbsp; What specifically is the consequence of this error such that I can safely ignore it?&amp;nbsp; My concern is that non-deterministic errors are being produced when my code executes in exactly the same sequence, and it is highly unsettling.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Error when calling disconnect inside callback for connected</title><link>https://devzone.nordicsemi.com/thread/384138?ContentTypeID=1</link><pubDate>Wed, 31 Aug 2022 12:28:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2dd5e762-ba5a-4c22-97fa-ab7cfb6061c0</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Without digging too deep into the call stack, I guess that this is just caused by the connection not being set in the correct instance before you call bt_conn_disconnect().&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Remember that there are many handlers that subscribe to the BT events, such as the connected event. If then, you disconnect from within the connected event, and this triggers some internal event, which checks the connection state before that handler ever got the connected event, you will get these scenarios.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;A couple of suggestions that you can check:&lt;/p&gt;
&lt;p&gt;1: Make sure to call&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;current_conn&lt;/span&gt;&lt;span&gt; = &lt;/span&gt;&lt;span&gt;bt_conn_ref&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;conn&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;before you disconnect. This will tell the bluetooth stack that it is connected, and update some internal connection handle. I am not sure whether this is the one that it is complaining about or not.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;2: Try to add a delay from the connected event. Add a delay of e.g. 50ms by starting a timer in the connected event, and then disconnect in the timeout handler.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;3: Try to set a breakpoint in bt_conn_send_cb() on line 381 in conn.c (the one printing &amp;quot;not connected!&amp;quot;), and look at the callstack. See if you can see what the conn pointer is, and what the conn-&amp;gt;state is.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;4: Try to ignore this error message. You are trying to disconnect either way. Does it trigger the disconnected event? Are you in a connection at a later point in time, or does it start advertising again?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Edvin&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>