The error is coming ctxt is used initialized for sending. data to phone

madhusudan_jadhav
Posts: 28
Joined: Fri Mar 10, 2023 9:05 am

The error is coming ctxt is used initialized for sending. data to phone

Postby madhusudan_jadhav » Thu May 04, 2023 3:22 am

Hi there,
I have used bleprph for bonding and
  1. static int
  2. gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
  3.                              struct ble_gatt_access_ctxt *ctxt,
  4.                              void *arg);
  5.  
  6. static const struct ble_gatt_svc_def gatt_svcs[] = {
  7. {.type = BLE_GATT_SVC_TYPE_PRIMARY,
  8.         .uuid = BLE_UUID16_DECLARE(0x180),                 // Define UUID for device type
  9.         .characteristics = (struct ble_gatt_chr_def[]){
  10.     {.uuid = BLE_UUID16_DECLARE(0xFEF4),           // Define UUID for reading
  11.         .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_AUTHEN,
  12.         .access_cb = device_read},
  13.         {.uuid = BLE_UUID16_DECLARE(0xDEAD),           // Define UUID for writing
  14.                 .flags = BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_WRITE_AUTHEN,
  15.                 .access_cb = device_write},
  16.                 {0}}},
  17.                 {0}};
device read
  1. static int device_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
  2. {
  3.      // send_data[6]="Hello";
  4.     ctxt->om=0;
  5.     os_mbuf_append(ctxt->om, &arg, sizeof(arg));
  6.     for (int i =0;i < sizeof(arg);i++ ){
  7.  
  8.             printf("%c",(char)send_data[i]);
  9.         }
  10.    
  11.  
  12. return 0;
  13. }
but when i call this function in code
  1. void switch_send(void){
  2.     struct ble_gatt_access_ctxt *ctxt;
  3.     uint16_t con_handle =0;
  4.     uint16_t attr_handle=0;
  5.  
  6.     switch(send_dat[0]){
  7.     case 'A': send_data[0]='R';
  8.     send_data[1]='E';
  9.     send_data[2]='L';
  10.     send_data[3]='A';
  11.     send_data[4]='Y';
  12.     send_data[5]=' ';
  13.     send_data[6]='1';
  14.     send_data[7]=' ';
  15.     send_data[8]='O';
  16.     send_data[9]='N';
  17.     device_read(con_handle, attr_handle, ctxt, "RELAY 1 ON");
  18.  
  19.     break;
'ctxt' may be used uninitialized [-Werror=maybe-uninitialized] error is coming.

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

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby ESP_Sprite » Thu May 04, 2023 3:36 am

So what is your question? The error is exactly as the compiler says: you're using ctxt without initializing it (and then the device_read dereferences it, so even if this compiles, it'll crash the esp32)

madhusudan_jadhav
Posts: 28
Joined: Fri Mar 10, 2023 9:05 am

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby madhusudan_jadhav » Thu May 04, 2023 3:48 am

I can't send the data to the phone that is thing. And i hvae initiallized in the code with cxt->om=0;

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

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby ESP_Sprite » Thu May 04, 2023 6:02 am

No, you have not. ctx is not a sane value (as it comes from the stack and as such is uninitialized) and ctx->om=0 will crash as the processor will try to dereference the ctx pointer, which contains a random address because it's uninitialized, and write 0 there.

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby MicroController » Thu May 04, 2023 10:39 pm

You are not supposed to call "device_read" at all.
It is the access callback function for the 0xFEF4 characteristic you declared and as such will be called by the BLE stack whenever a remote device accesses (read or write) that characteristic. When that happens, the function's arguments will be set appropriately by the BLE stack for your function to provide (upon read) or process (upon write) the data exchanged with the other device.

madhusudan_jadhav
Posts: 28
Joined: Fri Mar 10, 2023 9:05 am

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby madhusudan_jadhav » Fri May 05, 2023 5:12 am

Ok I called it by the switch. But now only i can read from my mobile. but data sent from mobile is not appearing on the esp32 log.
  1. static int
  2. gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
  3.                    void *dst, uint16_t *len)
  4. {
  5.     uint16_t om_len;
  6.     int rc;
  7.  
  8.     om_len = OS_MBUF_PKTLEN(om);
  9.     if (om_len < min_len || om_len > max_len) {
  10.         return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
  11.     }
  12.  
  13.     rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
  14.     if (rc != 0) {
  15.         return BLE_ATT_ERR_UNLIKELY;
  16.     }
  17.  
  18.     return 0;
  19. }
  20.  
  21. static int
  22. gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
  23.                              struct ble_gatt_access_ctxt *ctxt,
  24.                              void *arg)
  25. {
  26.     const ble_uuid_t *uuid;
  27.     //int rand_num;
  28.     int rc;
  29.      uint8_t dats[50]={0};
  30.     uuid = ctxt->chr->uuid;
  31.     /* Determine which characteristic is being accessed by examining its
  32.      * 128-bit UUID.
  33.      */
  34.  
  35.     if (ble_uuid_cmp(uuid, BLE_UUID16_DECLARE(0xFEF4)) == 0) {
  36.         if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR){
  37.  
  38. //      /* Respond with a 32-bit random number. */
  39. //      rand_num = rand();
  40. //      rc = os_mbuf_append(ctxt->om, &rand_num, sizeof rand_num);
  41.         if(state == 0){
  42.                     rc = os_mbuf_append(ctxt->om, &send_data,
  43.                             sizeof (send_data));
  44.  
  45.                     }
  46.                     else{
  47.                                 rc = os_mbuf_append(ctxt->om, &send_data1,
  48.                                         sizeof (send_data1));
  49.  
  50.                                 }
  51.  
  52.         return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  53.     }}
  54.  
  55.     //  esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
  56.     else if (ble_uuid_cmp(uuid, BLE_UUID16_DECLARE(0xDEAD)) == 0) {
  57.         switch (ctxt->op) {
  58. //      case BLE_GATT_ACCESS_OP_READ_CHR:
  59. //
  60. //          /*   rc = os_mbuf_append(ctxt->om, &gatt_svr_sec_test_static_val,
  61. //                                sizeof gatt_svr_sec_test_static_val);*/
  62. //          // dats[11] = "Hello there";
  63. //          if(state == 0){
  64. //          rc = os_mbuf_append(ctxt->om, &send_data,
  65. //                  sizeof (send_data));
  66. //
  67. //          return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;}
  68. //          else{
  69. //                      rc = os_mbuf_append(ctxt->om, &send_data1,
  70. //                              sizeof (send_data1));
  71. //
  72. //                      return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;}
  73.  
  74.         case BLE_GATT_ACCESS_OP_WRITE_CHR:
  75.             /*   rc = gatt_svr_chr_write(ctxt->om,
  76.                                     sizeof gatt_svr_sec_test_static_val,
  77.                                     sizeof gatt_svr_sec_test_static_val,
  78.                                     &gatt_svr_sec_test_static_val, NULL);*/
  79.             rc = gatt_svr_chr_write(ctxt->om,
  80.                     sizeof dats,
  81.                     sizeof dats,
  82.                     &dats, NULL);
  83.             for (int i =0;i < ctxt->om->om_len;i++ ){
  84.                     recieve_data[i]=(char)dats[i];
  85.                     printf("%c",recieve_data[i]);
  86.                 }
  87.  
  88.             printf("writing done");return 0;
  89.  
  90.         default:
  91.  
  92.             assert(0);
  93.             return BLE_ATT_ERR_UNLIKELY;
  94.         }
  95.  
  96.     }
  97.  
  98.     /* Unknown characteristic; the nimble stack should not have called this
  99.      * function.
  100.      */
  101.     assert(0);
  102.     return BLE_ATT_ERR_UNLIKELY;
  103. }
  104. static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
  105.         {
  106.                 /*** Service: Security test. */
  107.                 .type = BLE_GATT_SVC_TYPE_PRIMARY,
  108.                 .uuid = BLE_UUID16_DECLARE(0x180),
  109.                 .characteristics = (struct ble_gatt_chr_def[])
  110.                 { {
  111.                     /*** Characteristic: Random number generator. */
  112.                     .uuid = BLE_UUID16_DECLARE(0xFEF4),
  113.  
  114.                             .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
  115.                             .access_cb = gatt_svr_chr_access_sec_test,
  116.                 }, {
  117.                         /*** Characteristic: Static value. */
  118.                         .uuid = BLE_UUID16_DECLARE(0xDEAD),
  119.  
  120.                         .flags = BLE_GATT_CHR_F_WRITE,
  121.                         .access_cb = gatt_svr_chr_access_sec_test,
  122.                 }, {
  123.                         0, /* No more characteristics in this service. */
  124.                 }
  125.                 },
  126.         },
  127.  
  128.         {
  129.                 0, /* No more services. */
  130.         },
  131. };
  132. void
  133. gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
  134. {
  135.     char buf[BLE_UUID_STR_LEN];
  136.  
  137.     switch (ctxt->op) {
  138.     case BLE_GATT_REGISTER_OP_SVC:
  139.         MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
  140.                     ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
  141.                     ctxt->svc.handle);
  142.         break;
  143.  
  144.     case BLE_GATT_REGISTER_OP_CHR:
  145.         MODLOG_DFLT(DEBUG, "registering characteristic %s with "
  146.                     "def_handle=%d val_handle=%d\n",
  147.                     ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
  148.                     ctxt->chr.def_handle,
  149.                     ctxt->chr.val_handle);
  150.         break;
  151.  
  152.     case BLE_GATT_REGISTER_OP_DSC:
  153.         MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
  154.                     ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
  155.                     ctxt->dsc.handle);
  156.         break;
  157.  
  158.     default:
  159.         assert(0);
  160.         break;
  161.     }
  162. }

madhusudan_jadhav
Posts: 28
Joined: Fri Mar 10, 2023 9:05 am

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby madhusudan_jadhav » Fri May 05, 2023 5:13 am

dats is uint8_t dats[50];

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby MicroController » Fri May 05, 2023 8:26 am

You may want to check the return code of the functions you call and e.g. log its value if not 0.

Code: Select all

rc = gatt_svr_chr_write(ctxt->om,
                    sizeof dats, // <- min_len
                    sizeof dats, // <- max_len
                    &dats, NULL);
I figure this returns BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN because it will only accept data of exactly sizeof dats bytes, not less and not more.

madhusudan_jadhav
Posts: 28
Joined: Fri Mar 10, 2023 9:05 am

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby madhusudan_jadhav » Fri May 05, 2023 8:55 am

I changed the code to
  1. case BLE_GATT_ACCESS_OP_WRITE_CHR:
  2.             /*   rc = gatt_svr_chr_write(ctxt->om,
  3.                                     sizeof gatt_svr_sec_test_static_val,
  4.                                     sizeof gatt_svr_sec_test_static_val,
  5. //                                    &gatt_svr_sec_test_static_val, NULL);*/
  6. //          rc = gatt_svr_chr_write(ctxt->om,
  7. //                  sizeof dats,
  8. //                  sizeof dats,
  9. //                  &dats, NULL);
  10.             for (int i =0;i < ctxt->om->om_len;i++ ){
  11.                     recieve_data[i]=(char)ctxt->om->om_data[i];
  12.                     printf("%c",recieve_data[i]);
  13.                 }
  14.  
  15.             printf("writing done");return 0;
  16.  
  17.         default:
  18.  
  19.             assert(0);
  20.             printf("writing error");
  21.             return BLE_ATT_ERR_UNLIKELY;
  22.         }
But this log is coming while I press the write button on phone, type and send it.
  1. lld_pdu_get_tx_flush_nb HCI packet count mismatch (0, 1)
  2. I (24190) NimBLE: disconnect; reason=520
  3. I (24190) NimBLE: handle=0 our_ota_addr_type=0 our_ota_addr=
  4. I (24190) NimBLE:  our_id_addr_type=0 our_id_addr=
  5. I (24190) NimBLE:  peer_ota_addr_type=1 peer_ota_addr=
  6. I (24200) NimBLE:  peer_id_addr_type=1 peer_id_addr=
  7. I (24210) NimBLE:  conn_itvl=39 conn_latency=0 supervision_timeout=500 encrypted=0 authenticated=0 bonded=0
  8.  
  9. I (24220) NimBLE:
  10.  
  11. I (24220) NimBLE: GAP procedure initiated: advertise;
  12. I (24220) NimBLE: disc_mode=2
  13. I (24230) NimBLE:  adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=0 adv_itvl_max=0
  14. I (24240) NimBLE:
  15.  
  16. I (51370) NimBLE: connection established; status=0
  17. I (51370) NimBLE: handle=0 our_ota_addr_type=0 our_ota_addr=
  18. I (51370) NimBLE:  our_id_addr_type=0 our_id_addr=
  19. I (51380) NimBLE:  peer_ota_addr_type=1 peer_ota_addr=
  20. I (51380) NimBLE:  peer_id_addr_type=1 peer_id_addr=
  21. I (51390) NimBLE:  conn_itvl=39 conn_latency=0 supervision_timeout=500 encrypted=0 authenticated=0 bonded=0
  22.  
  23. I (51400) NimBLE:
  24.  
  25. I (52050) NimBLE: connection updated; status=0
  26. I (52050) NimBLE: handle=0 our_ota_addr_type=0 our_ota_addr=
  27. I (52050) NimBLE:  our_id_addr_type=0 our_id_addr=
  28. I (52050) NimBLE:  peer_ota_addr_type=1 peer_ota_addr=
  29. I (52060) NimBLE:  peer_id_addr_type=1 peer_id_addr=
  30. I (52060) NimBLE:  conn_itvl=6 conn_latency=0 supervision_timeout=500 encrypted=0 authenticated=0 bonded=0
  31.  
  32. I (52070) NimBLE:
  33.  
  34. I (53730) NimBLE: connection updated; status=0
  35. I (53730) NimBLE: handle=0 our_ota_addr_type=0 our_ota_addr=
  36. I (53730) NimBLE:  our_id_addr_type=0 our_id_addr=
  37. I (53730) NimBLE:  peer_ota_addr_type=1 peer_ota_addr=
  38. I (53740) NimBLE:  peer_id_addr_type=1 peer_id_addr=
  39. I (53740) NimBLE:  conn_itvl=39 conn_latency=0 supervision_timeout=500 encrypted=0 authenticated=0 bonded=0
  40.  
  41. I (53750) NimBLE:

madhusudan_jadhav
Posts: 28
Joined: Fri Mar 10, 2023 9:05 am

Re: The error is coming ctxt is used initialized for sending. data to phone

Postby madhusudan_jadhav » Fri May 05, 2023 9:18 am

Ok problem solved. I didnt correctly passed the data from om_data to char array

Who is online

Users browsing this forum: Baidu [Spider] and 112 guests