ESP_GATTS_READ_EVT infinite loop

newsettler_AI
Posts: 121
Joined: Wed Apr 05, 2017 12:49 pm

ESP_GATTS_READ_EVT infinite loop

Postby newsettler_AI » Tue Nov 27, 2018 7:56 pm

I have created custom characteristic using gatts table demo.

I'm trying to establish manual responding on ESP_GATTS_READ_EVT .

Problem: I got infinite loop when trying read characteristic value.

my characteristic configuration:
  1. static uint8_t my_char_uuid[16] = {
  2.     /* LSB <--------------------------------------------------------------------------------> MSB */
  3.         0x32, 0xd1, 0xb0, 0x53, 0x68, 0x8c,   0x9a, 0x81,   0x83, 0x49,   0x0c, 0x88,   0x75, 0xf2, 0x2f, 0xe3
  4. };
  5.  
  6. static uint8_t my_data[150] = { 0x00};
  7.  
  8.  
  9.         /* Characteristic Declaration */
  10.         [IDX_CHAR_C]     =
  11.         {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
  12.           CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
  13.  
  14.         /* Characteristic Value */ //MEASUREMENTS
  15.         [IDX_CHAR_VAL_C] = // ESP_GATT_RSP_BY_APP
  16.         {{ESP_GATT_RSP_BY_APP}, {ESP_UUID_LEN_128, (uint8_t *)&my_char_uuid, ESP_GATT_PERM_READ,
  17.           GATTS_DEMO_CHAR_VAL_LEN_MAX, sizeof(my_data), (uint8_t *)my_data}},
  18.  
  19.         /* Client Characteristic Configuration Descriptor */
  20.         [IDX_CHAR_CFG_C]  =
  21.         {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
  22.           sizeof(uint16_t), sizeof(charact_3_descr), (uint8_t *)charact_3_descr}},

part from gatts_profile_event_handler:
  1.         case ESP_GATTS_READ_EVT:
  2.             ESP_LOGI(BLE_TAG, "ESP_GATTS_READ_EVT");
  3.  
  4.  
  5.             printf("param->reg.app_id:%x\n", param->reg.app_id);
  6.             printf("param->read.trans_id:%x\n", param->read.trans_id);
  7.             printf("param->read.conn_id:%x\n", param->read.conn_id);
  8.             printf("param->read.handle:%X\n", param->read.handle);
  9.  
  10.  
  11.             if(handle_table[IDX_CHAR_VAL_C] == param->read.handle)
  12.             {  
  13.                 printf("IDX_CHAR_VAL_C\n");
  14.                 uint8_t buf[] = "[my_data_less_150_length]";
  15.                 esp_gatt_rsp_t rsp;
  16.                 memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
  17.                 rsp.attr_value.handle = param->read.handle;
  18.                 rsp.attr_value.len = sizeof(buf);
  19.  
  20.                 memcpy(rsp.attr_value.value, buf, sizeof(buf));
  21.  
  22.                 printf("rsp.attr_value.value: %s\n",rsp.attr_value.value);
  23.                 esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id,
  24.                                             ESP_GATT_OK, &rsp);
  25.             }
  26.             else if(avocado_handle_table[IDX_CHAR_CFG_C] == param->read.handle)
  27.             {
  28.                 printf("IDX_CHAR_CFG_C\n");
  29.             }
  30.  
  31.         break;

Here is what I got in output when trying read characteristic value:
  1. [2018-11-27_21:06:56][0;33mW (180130) BT_GATT: attribute value too long, to be truncated to 22[0m
  2. [2018-11-27_21:06:56][0;32mI (180181) BLE: ESP_GATTS_READ_EVT[0m
  3. [2018-11-27_21:06:56]param->reg.app_id:30
  4. [2018-11-27_21:06:56]param->read.trans_id:30
  5. [2018-11-27_21:06:56]param->read.conn_id:0
  6. [2018-11-27_21:06:56]param->read.handle:30
  7. [2018-11-27_21:06:56]IDX_CHAR_VAL_C
  8. [2018-11-27_21:06:56]rsp.attr_value.value: [my_data_less_150_length]
  9. [2018-11-27_21:06:56][0;33mW (180197) BT_GATT: attribute value too long, to be truncated to 22[0m
  10. [2018-11-27_21:06:56][0;32mI (180248) BLE: ESP_GATTS_READ_EVT[0m
  11. [2018-11-27_21:06:56]param->reg.app_id:31
  12. [2018-11-27_21:06:56]param->read.trans_id:31
  13. [2018-11-27_21:06:56]param->read.conn_id:0
  14. [2018-11-27_21:06:56]param->read.handle:30
  15. [2018-11-27_21:06:56]IDX_CHAR_VAL_C
  16. [2018-11-27_21:06:56]rsp.attr_value.value: [my_data_less_150_length]
  17. [2018-11-27_21:06:56][0;33mW (180265) BT_GATT: attribute value too long, to be truncated to 22[0m
  18. [2018-11-27_21:06:56][0;32mI (180316) BLE: ESP_GATTS_READ_EVT[0m
  19. [2018-11-27_21:06:56]param->reg.app_id:32
  20. [2018-11-27_21:06:56]param->read.trans_id:32
  21. [2018-11-27_21:06:56]param->read.conn_id:0
  22. [2018-11-27_21:06:56]param->read.handle:30
  23. [2018-11-27_21:06:56]IDX_CHAR_VAL_C
  24. [2018-11-27_21:06:56]rsp.attr_value.value: [my_data_less_150_length]
  25. [2018-11-27_21:06:56][0;33mW (180332) BT_GATT: attribute value too long, to be truncated to 22[0m
  26. [2018-11-27_21:06:56][0;32mI (180383) BLE: ESP_GATTS_READ_EVT[0m
  27. [2018-11-27_21:06:56]param->reg.app_id:33
  28. [2018-11-27_21:06:56]param->read.trans_id:33
  29. [2018-11-27_21:06:56]param->read.conn_id:0
  30. [2018-11-27_21:06:56]param->read.handle:30
  31. [2018-11-27_21:06:56]IDX_CHAR_VAL_C
  32. [2018-11-27_21:06:56]rsp.attr_value.value: [my_data_less_150_length]
  33. [2018-11-27_21:06:56][0;33mW (180400) BT_GATT: attribute value too long, to be truncated to 22[0m
  34. [2018-11-27_21:06:56][0;32mI (180451) BLE: ESP_GATTS_READ_EVT[0m
  35. [2018-11-27_21:06:56]param->reg.app_id:34
  36. [2018-11-27_21:06:56]param->read.trans_id:34
  37. [2018-11-27_21:06:56]param->read.conn_id:0
  38. [2018-11-27_21:06:56]param->read.handle:30
  39. [2018-11-27_21:06:56]IDX_CHAR_VAL_C
  40. [2018-11-27_21:06:56]rsp.attr_value.value: [my_data_less_150_length]
  41. [2018-11-27_21:06:57][0;33mW (180467) BT_GATT: attribute value too long, to be truncated to 22[0m

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

Re: ESP_GATTS_READ_EVT infinite loop

Postby chegewara » Tue Nov 27, 2018 8:20 pm

Lets start from beginning. param is type of esp_ble_gatts_cb_param_t which is union:
https://github.com/espressif/esp-idf/bl ... h#L59-L279
You cant get param->reg from read event, because you will get not related data.

Now go to the point. From this log line we can read that your devices are connected with mtu 25:
BT_GATT: attribute value too long, to be truncated to 22
Now you have to know when characteristic value is being read you can send in response up to MTU-1 bytes. When you send MTU - 1 bytes then connected peer device will assume that value has not been read completely and will send another request to read with offset which will tell you app how many bytes has been read so far and from which byte you have to send next response. When response is with value length less than MTU - 1 (0 including) then peer device knows that all bytes has been read from characteristic value.
https://github.com/espressif/esp-idf/bl ... _api.h#L76

All this login needs to be implemented in your ESP_GATTS_READ_EVT.

Before you ask next question. There is very similar situation with write characteristic with one difference. Before you write characteristic value all bytes has to be sent and write to characteristic only when proper event is being fired or discarded on cancel write.

newsettler_AI
Posts: 121
Joined: Wed Apr 05, 2017 12:49 pm

Re: ESP_GATTS_READ_EVT infinite loop

Postby newsettler_AI » Wed Nov 28, 2018 4:42 pm

Maybe as alternative I should use ESP_GATT_AUTO_RSP instead?

Can I just maually edit my_data buffer?

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

Re: ESP_GATTS_READ_EVT infinite loop

Postby chegewara » Wed Nov 28, 2018 5:11 pm

Yes, you can try with AUTO_RSP.

You can write simple logic:

Code: Select all

.....
      case ESP_GATTS_READ_EVT:
            ESP_LOGI(BLE_TAG, "ESP_GATTS_READ_EVT");
            size_t size = sizeof(MTU-1) =< sizeof(buf) - param.read.offset ? sizeof(MTU-1) : sizeof(buf) - param.read.offset; //as long as remaining data is greater or equal MTU - 1 then send MTU - 1 bytes, then send remaining bytes even if remaining is equal 0
 memcpy(rsp.attr_value.value, buf + param->read.offset, size);
 
 .....
 

newsettler_AI
Posts: 121
Joined: Wed Apr 05, 2017 12:49 pm

Re: ESP_GATTS_READ_EVT infinite loop

Postby newsettler_AI » Thu Nov 29, 2018 2:39 pm

Thanks for suggestion.

Still I got some issues (now not loop, but on nrfConnect I got all zeroes in value field)


My log:
  1. [2018-11-29_16:26:23][0;32mI (831083) BLE: update connection params status = 0, min_int = 16, max_int = 32,conn_int = 6,latency = 0, timeout = 2000[0m
  2. [2018-11-29_16:26:24][0;32mI (831470) BLE: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 32,latency = 0, timeout = 400[0m
  3. [2018-11-29_16:26:24]------------------
  4. [2018-11-29_16:26:26][0;32mI (833671) BLE: ESP_GATTS_READ_EVT[0m
  5. [2018-11-29_16:26:26]param->read.is_long == FALSE
  6. [2018-11-29_16:26:26]param->read.conn_id:0x0
  7. [2018-11-29_16:26:26]param->read.trans_id:0x1
  8. [2018-11-29_16:26:26]param->read.handle:0x30
  9. [2018-11-29_16:26:26]param->read.offset:0x0
  10. [2018-11-29_16:26:26]IDX_CHAR_VAL_C
  11. [2018-11-29_16:26:26]size1:4
  12. [2018-11-29_16:26:26]rsp.attr_value.value: my_d
  13. [2018-11-29_16:26:26][0;31mE (833681) BT_GATT: GATTS_SendRsp conn_id: 3  waiting for op_code = 00
  14. [2018-11-29_16:26:26][0m
  15. [2018-11-29_16:26:26][0;31mE (833688) BT_APPL: Sending response failed
  16. [2018-11-29_16:26:26][0m
  17. [2018-11-29_16:26:26][0;32mI (833791) BLE: ESP_GATTS_READ_EVT[0m
  18. [2018-11-29_16:26:26]param->read.is_long == TRUE
  19. [2018-11-29_16:26:26]param->read.conn_id:0x0
  20. [2018-11-29_16:26:26]param->read.trans_id:0x2
  21. [2018-11-29_16:26:26]param->read.handle:0x30
  22. [2018-11-29_16:26:26]param->read.offset:0x16
  23. [2018-11-29_16:26:26]IDX_CHAR_VAL_C
  24. [2018-11-29_16:26:26]size2:2
  25. [2018-11-29_16:26:26]rsp.attr_value.value: h
  26. [2018-11-29_16:26:26][0;31mE (833801) BT_GATT: GATTS_SendRsp conn_id: 3  waiting for op_code = 00
  27. [2018-11-29_16:26:26][0m
  28. [2018-11-29_16:26:26][0;31mE (833808) BT_APPL: Sending response failed
  29. [2018-11-29_16:26:26][0m
  30. [2018-11-29_16:26:26]------------------
  31. [2018-11-29_16:26:26][0;32mI (833912) BLE: ESP_GATTS_READ_EVT[0m
  32. [2018-11-29_16:26:26]param->read.is_long == TRUE
  33. [2018-11-29_16:26:26]param->read.conn_id:0x0
  34. [2018-11-29_16:26:26]param->read.trans_id:0x3
  35. [2018-11-29_16:26:26]param->read.handle:0x30
  36. [2018-11-29_16:26:26]param->read.offset:0x2C
  37. [2018-11-29_16:26:26]IDX_CHAR_VAL_C
  38. [2018-11-29_16:26:26]size1:4
  39. [2018-11-29_16:26:26]rsp.attr_value.value: °|ы?
  40. [2018-11-29_16:26:26][0;31mE (833925) BT_GATT: GATTS_SendRsp conn_id: 3  waiting for op_code = 00
  41. [2018-11-29_16:26:26][0m
  42. [2018-11-29_16:26:26][0;31mE (833932) BT_APPL: Sending response failed
  43. [2018-11-29_16:26:26][0m
  44. [2018-11-29_16:26:26][0;32mI (833991) BLE: ESP_GATTS_READ_EVT[0m
  45. [2018-11-29_16:26:26]param->read.is_long == TRUE
  46. [2018-11-29_16:26:26]param->read.conn_id:0x0
  47. [2018-11-29_16:26:26]param->read.trans_id:0x4
  48. [2018-11-29_16:26:26]param->read.handle:0x30
  49. [2018-11-29_16:26:26]param->read.offset:0x42
  50. [2018-11-29_16:26:26]IDX_CHAR_VAL_C
  51. [2018-11-29_16:26:26]size1:4
  52. [2018-11-29_16:26:26]rsp.attr_value.value:
  53. [2018-11-29_16:26:26][0;31mE (834000) BT_GATT: GATTS_SendRsp conn_id: 3  waiting for op_code = 00
  54. [2018-11-29_16:26:26][0m
  55. [2018-11-29_16:26:26][0;31mE (834008) BT_APPL: Sending response failed
  56. [2018-11-29_16:26:26][0m
  57. [2018-11-29_16:26:26][0;32mI (834071) BLE: ESP_GATTS_READ_EVT[0m
  58. [2018-11-29_16:26:26]param->read.is_long == TRUE
  59. [2018-11-29_16:26:26]param->read.conn_id:0x0
  60. [2018-11-29_16:26:26]param->read.trans_id:0x5
  61. [2018-11-29_16:26:26]param->read.handle:0x30
  62. [2018-11-29_16:26:26]param->read.offset:0x58
  63. [2018-11-29_16:26:26]IDX_CHAR_VAL_C
  64. [2018-11-29_16:26:26]size1:4
  65. [2018-11-29_16:26:26]rsp.attr_value.value:
  66. [2018-11-29_16:26:26][0;31mE (834080) BT_GATT: GATTS_SendRsp conn_id: 3  waiting for op_code = 00
  67. [2018-11-29_16:26:26][0m
  68. [2018-11-29_16:26:26][0;31mE (834088) BT_APPL: Sending response failed
  69. [2018-11-29_16:26:26][0m
  70. [2018-11-29_16:26:26][0;32mI (834151) BLE: ESP_GATTS_READ_EVT[0m
  71. [2018-11-29_16:26:26]param->read.is_long == TRUE
  72. [2018-11-29_16:26:26]param->read.conn_id:0x0
  73. [2018-11-29_16:26:26]param->read.trans_id:0x6
  74. [2018-11-29_16:26:26]param->read.handle:0x30
  75. [2018-11-29_16:26:26]param->read.offset:0x6E
  76. [2018-11-29_16:26:26]IDX_CHAR_VAL_C
  77. [2018-11-29_16:26:26]size1:4
  78. [2018-11-29_16:26:26]rsp.attr_value.value:
  79. [2018-11-29_16:26:26][0;31mE (834160) BT_GATT: GATTS_SendRsp conn_id: 3  waiting for op_code = 00
  80. [2018-11-29_16:26:26][0m
  81. [2018-11-29_16:26:26][0;31mE (834168) BT_APPL: Sending response failed
  82. [2018-11-29_16:26:26][0m
  83. [2018-11-29_16:26:26][0;32mI (834231) BLE: ESP_GATTS_READ_EVT[0m
  84. [2018-11-29_16:26:26]param->read.is_long == TRUE
  85. [2018-11-29_16:26:26]param->read.conn_id:0x0
  86. [2018-11-29_16:26:26]param->read.trans_id:0x7
  87. [2018-11-29_16:26:26]param->read.handle:0x30
  88. [2018-11-29_16:26:26]param->read.offset:0x84
  89. [2018-11-29_16:26:26]IDX_CHAR_VAL_C
  90. [2018-11-29_16:26:26]size1:4
  91. [2018-11-29_16:26:26]rsp.attr_value.value: а4ы?
  92. [2018-11-29_16:26:26][0;31mE (834241) BT_GATT: GATTS_SendRsp conn_id: 3  waiting for op_code = 00
  93. [2018-11-29_16:26:26][0m
  94. [2018-11-29_16:26:26][0;31mE (834248) BT_APPL: Sending response failed
  95. [2018-11-29_16:26:26][0m
  96. [2018-11-29_16:26:27]------------------

I have changed responce parameter to ESP_GATT_AUTO_RSP

and here is my read event code:

  1. case ESP_GATTS_READ_EVT:
  2.     ESP_LOGI(BLE_TAG, "ESP_GATTS_READ_EVT");
  3.     if(handle_table[IDX_CHAR_VAL_C] == param->read.handle)// read.handle
  4.     {
  5.         printf("IDX_CHAR_VAL_C\n");
  6.         uint8_t buf[] = "my_data_less_150_length";
  7.         esp_gatt_rsp_t rsp;
  8.         memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
  9.  
  10.         //int size = sizeof(ESP_GATT_DEF_BLE_MTU_SIZE-1) <= sizeof(buf) - param->read.offset ? sizeof(ESP_GATT_DEF_BLE_MTU_SIZE-1) : sizeof(buf) - param->read.offset;
  11.  
  12.         int size = sizeof(ESP_GATT_DEF_BLE_MTU_SIZE - 1);
  13.  
  14.         if( size <= ((sizeof(buf) - param->read.offset)) )
  15.         {
  16.             size = sizeof(ESP_GATT_DEF_BLE_MTU_SIZE - 1);
  17.             printf("size1:%d\n", size);
  18.         }
  19.         else
  20.         {
  21.             size = sizeof(buf) - param->read.offset;
  22.             printf("size2:%d\n", size);
  23.         }
  24.  
  25.          memcpy(rsp.attr_value.value, buf + param->read.offset, size);
  26.  
  27.         rsp.attr_value.handle = param->read.handle;
  28.         rsp.attr_value.len = sizeof(buf); // should set here value with ofset or keep total buf len?
  29.         rsp.attr_value.offset = param->read.offset;
  30.  
  31.         printf("rsp.attr_value.value: %s\n",rsp.attr_value.value);
  32.  
  33.         esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id,
  34.                                     ESP_GATT_OK, &rsp);
  35.     }
  36. break;
It looks like values of characteristic cant be modified

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

Re: ESP_GATTS_READ_EVT infinite loop

Postby chegewara » Thu Nov 29, 2018 3:37 pm

It will be easier if you study some working code:
https://github.com/nkolban/esp32-snippe ... ristic.cpp

Who is online

Users browsing this forum: Bing [Bot] and 118 guests