<?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>Queued write response fails</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/56936/queued-write-response-fails</link><description>iOS is using queued writes to send data to my nRF52832 application. For some reason, on the third queued write reply (in nrf_ble_qwr.c) sd_ble_gatts_rw_authorize_reply returns NRF_ERROR_INVALID_STATE. 
 I modified nrf_ble_qwr.c to log every time it calls</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 28 Jan 2020 15:08:05 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/56936/queued-write-response-fails" /><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/231457?ContentTypeID=1</link><pubDate>Tue, 28 Jan 2020 15:08:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9222f2f7-572d-415d-aeef-ea7d5dfb08e2</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;It depends on the overhead of queued writes you have. There are 5 bytes of overhead per Prepare Write Request:&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/509x202/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-4b5d21a55d4a42acb1394e2150bb2774/pastedimage1580223889391v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So you need to know roughly the size of the data you going to receive. And the ATT MTU size to calculate the number of Prepare Write Request.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Again, the queued write is designed so that you can write multiple characteristic at the same time. If you plan to write only one characteristic, or if you don&amp;#39;t have the requirement of updating multiple of them at the same time, you can just use normal write.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/231444?ContentTypeID=1</link><pubDate>Tue, 28 Jan 2020 14:29:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3809ce03-be21-422e-a834-a9830acc16da</guid><dc:creator>nrbrook</dc:creator><description>&lt;p&gt;Ok thanks. So for receiving 512 bytes of data via queued write, what should MEM_BUFF_SIZE be? iOS uses queued writes for large data size, so I don&amp;#39;t really have control over it. Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/231434?ContentTypeID=1</link><pubDate>Tue, 28 Jan 2020 14:11:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5f952ea3-b3bc-49d5-b93e-b4782c76d639</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Nick,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;I think I might find&amp;nbsp;the&amp;nbsp;root cause. In our queued write example we set the&amp;nbsp;MEM_BUFF_SIZE to 512. This is the user memory provided to the softdevice to receive the queued writes (I assume you also use stack handled queued write, not app handled).&lt;/p&gt;
&lt;p&gt;It&amp;#39;s not the same as the&amp;nbsp;rcvd_data[NRF_BLE_QWRS_MAX_RCV_SIZE]; which is handled by the qwrs module.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If it&amp;#39;s the case please try to test increase the User mem buffer to for example 1024.&lt;/p&gt;
&lt;p&gt;In my test here if I leave the&amp;nbsp;&lt;span&gt;MEM_BUFF_SIZE&amp;nbsp;=512&amp;nbsp; (thus&amp;nbsp;m_buffer size array of 512 bytes) I got INVALID_STATE after 400+ bytes transmitted (there are lots of overhead in queued writes) it fixed after I increase the user mem.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Please note that, queued write usually used when you want to update multiple characteristics at once. If you want to do a normal write, you can simply use normal write (with or without authorization).&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/231250?ContentTypeID=1</link><pubDate>Mon, 27 Jan 2020 16:52:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd363597-9077-4e45-9cb7-fbebb301627d</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Nick,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;rcvd_data is defined as an array not a pointer:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; uint8_t&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;rcvd_data[NRF_BLE_QWRS_MAX_RCV_SIZE];&lt;/p&gt;
&lt;p&gt;So the array has the buffer, it&amp;#39;s just not initialized to 0 and I&amp;#39;m not sure initialized to 0 is really needed anyway.&amp;nbsp;&lt;br /&gt;Please try to test with&amp;nbsp;&lt;span&gt;ble_app_queued_writes&amp;nbsp;so we have an aligned test base.&amp;nbsp;&lt;br /&gt;I will try to test here with what you described.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/231098?ContentTypeID=1</link><pubDate>Mon, 27 Jan 2020 11:47:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0edc5b77-3744-45ba-8ef3-2821c7eda9e6</guid><dc:creator>nrbrook</dc:creator><description>&lt;p&gt;No, the issue is not related to the example bug, I just meant I can&amp;#39;t test the example as-is. That would also not fix it as no buffer is set to cur_evt.rcvd_data.&lt;/p&gt;
&lt;p&gt;The event is prepare write request (BLE_GATTS_OP_PREP_WRITE_REQ) (I logged &amp;quot;prepare reply&amp;quot; on this event in nrf_ble_qwr.c on_prepare_write just before the call to sd_ble_gatts_rw_authorize_reply).&lt;/p&gt;
&lt;p&gt;The App uses &lt;strong&gt;peripheral.maximumWriteValueLength(for: .withResponse)&lt;/strong&gt; to get the max write length which is 512 in my testing. Then I write a 512 byte Data with &lt;strong&gt;peripheral.writeValue(remaining.subdata(in: 0..&amp;lt;lengthToWrite), for: c, type: .withResponse)&lt;/strong&gt;. iOS then uses queued writes to send that data. It should be fairly easy to reproduce, I&amp;#39;m using an iPad Pro 11&amp;quot; with iOS 13.3. I&amp;#39;ve already spent a lot of time on this issue though so I can&amp;#39;t really investigate further right now. I&amp;#39;m just reducing sent data length in the app for now.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/231058?ContentTypeID=1</link><pubDate>Mon, 27 Jan 2020 09:46:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cb15db49-e602-4a42-8af9-9a5ed33126bc</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Nick,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for the link. I think it&amp;#39;s not related to the issue you are having here. You can add the fix to initialize the&amp;nbsp;cur_evt.rcvd_data array before calling&amp;nbsp;nrf_ble_qwr_value_get() inside&amp;nbsp;nrf_ble_qwrs_on_qwr_evt(). &lt;strong&gt;memset(cur_evt.rcvd_data,0,NRF_BLE_QWRS_MAX_RCV_SIZE);&lt;/strong&gt; should fix it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you tried to add logging inside&amp;nbsp;&lt;span&gt;nrf_ble_qwr_on_ble_evt() event ? We would need to find which function throw the error.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m looking for a way to reproduce the issue here. Could you describe your iphone app ? I can see in the log it&amp;#39;s doing 3 queued writes, each has 180 bytes. Is it possible to replace the iphone with a nRF52 as a central device ? So we can reproduce here ? It&amp;#39;s the best if you can reproduce with&amp;nbsp;&lt;span&gt;ble_app_queued_writes, this example has both sides running on nRF52.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/230920?ContentTypeID=1</link><pubDate>Fri, 24 Jan 2020 15:44:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4244e67d-57c1-4c1a-8a5e-6561793cdff1</guid><dc:creator>nrbrook</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;KFPAL_Nordic_bluetooth.c line 446 is the QWR error callback&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void queued_write_error_handler(uint32_t nrf_error)
{
    APP_ERROR_HANDLER(nrf_error);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I am now confident that I am logging everywhere &lt;span&gt;sd_ble_gatts_rw_authorize_reply&lt;/span&gt; is called including in libraries.&lt;/p&gt;
&lt;p&gt;I am logging BLE events from the softdevice as you can see in the logs (NRF_SDH_BLE_LOG_ENABLED=1), so these are the only BLE events occurring.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://we.tl/t-P8QIQz9U1P"&gt;Here&amp;#39;s a packet log&lt;/a&gt; captured from the iPad. You can see it never receives a response to the third write. I suspect this could actually be a softdevice bug. Queued writes with only two writes work fine.&lt;/p&gt;
&lt;p&gt;I haven&amp;#39;t tried your example but I &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/56881/bug-in-ble_app_queued_writes-example"&gt;think it is broken&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Queued write response fails</title><link>https://devzone.nordicsemi.com/thread/230918?ContentTypeID=1</link><pubDate>Fri, 24 Jan 2020 15:30:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d3b8888e-6214-4e31-80a3-74c03ee1e049</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Nick,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What do you have at&amp;nbsp;KFPAL_Nordic_bluetooth.c line 446 ?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you are not sure where&amp;nbsp;&lt;span&gt;sd_ble_gatts_rw_authorize_reply&amp;nbsp;() being called, and if other module is using it you can do a &amp;quot;Find in Files&amp;quot; and find all location that function is called.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Please add the log in each of the events inside&amp;nbsp;nrf_ble_qwr_on_ble_evt() it would help to detect the last event before the assert.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Have you tried to test with the&amp;nbsp;\examples\ble_peripheral\experimental\ble_app_queued_writes example ? &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;Please also capture a &lt;a href="https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Sniffer"&gt;sniffer trace,&lt;/a&gt; it&amp;nbsp;help analyzing what happens over the air.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>