<?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_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/116261/sd_ble_gatts_hvx-gives-error-as-nrf_error_resources-and-my-code-gets-stuck</link><description>Hi Nordic team 
 Currently I am trying to send a big chunk of data close to 100 KB from my nRF52832 to nRF connect for mobile app in Android phone. I am able to send few bytes in one shot. But when I kept a loop for sending more than 10 data packets,</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 18 Nov 2024 14:42:20 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/116261/sd_ble_gatts_hvx-gives-error-as-nrf_error_resources-and-my-code-gets-stuck" /><item><title>RE: sd_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck</title><link>https://devzone.nordicsemi.com/thread/510920?ContentTypeID=1</link><pubDate>Mon, 18 Nov 2024 14:42:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4fa49598-8856-498d-8134-7e92ce365cbc</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;So what does it return?&lt;/p&gt;
&lt;p&gt;I did notice that you didn&amp;#39;t follow the suggestion that I mentioned in my pseudo code snippet, to leave the event and trigger the sending from another place (see what I did with the send_data_flag parameter).&lt;/p&gt;
&lt;p&gt;However, when you are stuck, what does ble_cus_sensor_state_update() return? It is not NRF_ERROR_RESOURCES, because according to your&amp;nbsp;ble_cus_send_sensor_data(), because if it did, it would be passed onto the APP_ERROR_CHECK(err_code), and the application would restart.&lt;/p&gt;
&lt;p&gt;Does it return&amp;nbsp;BLE_ERROR_GATTS_SYS_ATTR_MISSING? If so, what is the expected behavior? Because now it will keep trying, but your timer parameter will not change.&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: sd_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck</title><link>https://devzone.nordicsemi.com/thread/510821?ContentTypeID=1</link><pubDate>Mon, 18 Nov 2024 05:24:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4fb7de6-80ff-46ac-9444-6b0bb770db79</guid><dc:creator>Mushtakh Shaikh</dc:creator><description>&lt;p&gt;Hi Edwin,&lt;/p&gt;
&lt;p&gt;Can you please suggest any changes if required?&lt;/p&gt;
&lt;p&gt;Or any mistake in my implementations??&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sd_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck</title><link>https://devzone.nordicsemi.com/thread/510600?ContentTypeID=1</link><pubDate>Fri, 15 Nov 2024 06:54:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4d0a8ee2-704c-426e-8e1c-9faeced2b1a5</guid><dc:creator>Mushtakh Shaikh</dc:creator><description>&lt;p&gt;Yes, Edwin, it is stuck in ble_cus_send_sensor_data and the below function returns the error.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;uint32_t ble_cus_sensor_state_update(ble_cus_t * p_cus, int8_t * p_buttons_states, int length, uint16_t conn_handle)&lt;br /&gt;{&lt;br /&gt; ble_gatts_hvx_params_t params;&lt;br /&gt; uint16_t len = length;&lt;br /&gt; uint32_t err_code;&lt;/p&gt;
&lt;p&gt;memset(&amp;amp;params, 0, sizeof(params));&lt;br /&gt; params.type = BLE_GATT_HVX_NOTIFICATION;//BLE_GATT_HVX_NOTIFICATION;&lt;br /&gt; params.handle = p_cus-&amp;gt;leds_states_char_handles.value_handle;&lt;br /&gt; params.p_data = p_buttons_states;&lt;br /&gt; params.p_len = &amp;amp;len;&lt;/p&gt;
&lt;p&gt;return sd_ble_gatts_hvx(conn_handle, &amp;amp;params);&lt;br /&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sd_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck</title><link>https://devzone.nordicsemi.com/thread/510523?ContentTypeID=1</link><pubDate>Thu, 14 Nov 2024 14:17:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:02a6ddd2-472c-41ef-bbe8-1076f3841eb6</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Where is it stuck? In&amp;nbsp;ble_cus_send_sensor_data()?&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sd_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck</title><link>https://devzone.nordicsemi.com/thread/510264?ContentTypeID=1</link><pubDate>Wed, 13 Nov 2024 10:42:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ef2058ce-05ef-4ea8-a7ba-6b99d5e9b028</guid><dc:creator>Mushtakh Shaikh</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/4861.main.c"&gt;devzone.nordicsemi.com/.../4861.main.c&lt;/a&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/7838.ble_5F00_cus.c"&gt;devzone.nordicsemi.com/.../7838.ble_5F00_cus.c&lt;/a&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/5775.ble_5F00_cus.h"&gt;devzone.nordicsemi.com/.../5775.ble_5F00_cus.h&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Hi Edvin,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now,as you mentioned, there are two different event hanler,&lt;br /&gt;1. ble_evt_handler in main.c &lt;br /&gt;2. ble_cus_on_ble_evt in ble_cus.c&lt;/p&gt;
&lt;p&gt;Now, when I trying to send packets continuously,my code is getting stuck and I am unable to send the data to BLE.&lt;br /&gt;I tried above suggestions, somehow it doesn&amp;#39;t seem to work for me, as the code is getting stuck. Please check the code and let me know if any changes required in order to solve the issue.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I would really appreciate if you can let me know on how to apply your suggested changes in my code.&lt;/p&gt;
&lt;p&gt;thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sd_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck</title><link>https://devzone.nordicsemi.com/thread/509933?ContentTypeID=1</link><pubDate>Mon, 11 Nov 2024 14:54:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b9df3423-5f47-4f16-9f46-3b05823d718b</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I believe you are waiting in an interrupt with equal or higher priority than the BLE_GATTS_EVT_HVN_TX_COMPLETE event (ble_evt_handler()).&lt;/p&gt;
&lt;p&gt;In fact, I suspect that the interrupt you are waiting in is the ble_evt_handler(), or the equivalent handler that you registered for your custom service handler. As long as you are not leaving the interrupt, the BLE_GATTS_EVT_HVN_TX_COMPLETE event will not be able to execute, to tell your application that the softdevice has freed up some buffers.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So one way to handle this is to keep track of what you have queued successfully. So you need to queue up as much as you can, until your&amp;nbsp;ble_cus_sensor_state_update() returns NRF_ERROR_RESOURCES. When it does, keep track of how much data you queued in your&amp;nbsp;send_current_data(), and return from this function, so that the interrupt is released. Then you can wait for the BLE_GATTS_EVT_HVN_TX_COMPLETE event, which means that at least one of your packets was acked. Then you can set a flag in the TX_COMPLETE callback, that you can check in your main-loop, and use this to continue where you left off, queuing more packets, until it returns NRF_ERROR_RESOURCES again, or you have successfully queued all your packets.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Something like this:&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;volatile uint32_t packet_counter = 0;
volatile bool send_data_flag = false;

ble_evt_handler()
{
    ...
    case BLE_GATTS_EVT_HVN_TX_COMPLETE:
        if ( **check if more data needs to be sent**)
        {
            send_data_flag = true;
        }
}

send_data()
{
    for (int i=0; i&amp;lt;100; i++)
    {
        err = NRF_SUCCESS;
        while(err = NRF_SUCCESS)
        {
            sd_ble_gatts_hvx(...);
            if (err = NRF_SUCCESS)
            {
                packet_counter = i;
                i++;
            }
        }
        break;
    }
}
cust_data_handler()
{
    send_data_flag = true;
}

int main()
{
...
    while (true)
    {
        if (send_data_flag)
        {
            send_data_flag = false;
            send_data();
        }
        idle_state_handle();
    }
}&lt;/pre&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></channel></rss>