BLE notification stack overflow

BARS2022
Posts: 6
Joined: Mon Nov 14, 2022 1:04 pm

BLE notification stack overflow

Postby BARS2022 » Mon Nov 14, 2022 1:43 pm

Hi!
I want to start BLE server with the notifications on ESP32. I was run a "gatts_demo" example in Visual Studio 2022. I make notification sends with RTOS function:
  1. void V_notif_task(void *p)
  2. {
  3.  
  4.     uint8_t notify_data[32];
  5.    
  6.     memcpy(notify_data, "notify sended", 13);
  7.    
  8.     while (1)
  9.     {
  10.         if (ne == true)
  11.         {
  12.             if (gatts_if_for_indicate == ESP_GATT_IF_NONE)
  13.             {
  14.                 printf("cannot indicate because gatts_if_for_indicate is NONE\n");
  15.             }
  16.             else
  17.             {
  18.                 uint16_t attr_handle = 0x002a;
  19.  
  20.                 //the size of notify_data[] need less than MTU size
  21.                 esp_ble_gatts_send_indicate(gatts_if_for_indicate, 0, attr_handle, 13, notify_data, false);
  22.             }
  23.         }
  24.  
  25.         vTaskDelay(1000);
  26.     }
  27. }
  28.  
  29.     case ESP_GATTS_WRITE_EVT: {
  30.         ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d", param->write.conn_id, param->write.trans_id,
  31.  param->write.handle);
  32.         if (!param->write.is_prep){
  33.             ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
  34.             esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
  35.             if (gl_profile_tab[PROFILE_A_APP_ID].descr_handle == param->write.handle && param->write.len == 2){
  36.                 uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
  37.                 if (descr_value == 0x0001){
  38.                     if (a_property & ESP_GATT_CHAR_PROP_BIT_NOTIFY){
  39.                         ESP_LOGI(GATTS_TAG, "notify enable");
  40.                         uint8_t notify_data[15];
  41.                         for (int i = 0; i < sizeof(notify_data); ++i)
  42.                         {
  43.                             notify_data[i] = i%0xff;
  44.                         }
  45.                         //the size of notify_data[] need less than MTU size
  46.                         esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle, sizeof(notify_data), notify_data, false);
  47.  
  48.             ne = true;
  49.         }
  50.         }
  51.  
  52. xTaskCreate(v_test_task, "test_task", configMINIMAL_STACK_SIZE, NULL, 8, NULL);
I receive a notifications on BLE client, but after several notifications ESP32 crush with error:

Code: Select all

 ***ERROR*** A stack overflow in task test_task has been detected.
Why is this happening? Function esp_ble_gatts_send_indicate don't clear memory after work ending? How to clear the memory?

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE notification stack overflow

Postby chegewara » Tue Nov 15, 2022 10:41 am

This is one of most common issues.
When you are using printf like functions in task then minimal stack is not enough.

BARS2022
Posts: 6
Joined: Mon Nov 14, 2022 1:04 pm

Re: BLE notification stack overflow

Postby BARS2022 » Wed Nov 16, 2022 1:01 pm

The function printf() is not called during normal operation of the software. I deleted the printf(), but the ESP32 is also crashing.

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: BLE notification stack overflow

Postby ESP_Sprite » Thu Nov 17, 2022 1:15 am

'printf-like functions' also include logging functions like ESP_LOGI and esp_log_buffer_hex. An easier fix may be to give more stack space to the task: take out configMINIMAL_STACK_SIZE and change it to something like 4096 or 8192.

Who is online

Users browsing this forum: No registered users and 144 guests