only one command is running that is read or write to phone in nimble ble. i am using bleprh

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

only one command is running that is read or write to phone in nimble ble. i am using bleprh

Postby madhusudan_jadhav » Fri May 05, 2023 7:10 am

I am using Bleprph example for security purposes. I want to send and receive data to the phone to esp32. I used gatt_svr_svc_sec_test_uuid for primary declaration and gatt_svr_chr_sec_test_static_uuid for rad and write.

but i can only able to send the data to phone by esp32. but data sent by phone doesnt appear on esp32.
  1. static const ble_uuid128_t gatt_svr_svc_sec_test_uuid =
  2.     BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
  3.                      0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
  4.  
  5. ///* 5c3a659e-897e-45e1-b016-007107c96df6 */
  6. //static const ble_uuid128_t gatt_svr_chr_sec_test_rand_uuid =
  7. //    BLE_UUID128_INIT(0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
  8. //                     0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
  9. //
  10. ///* 5c3a659e-897e-45e1-b016-007107c96df7 */
  11. static const ble_uuid128_t gatt_svr_chr_sec_test_static_uuid =
  12.     BLE_UUID128_INIT(0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
  13.                      0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
  14.  
  15.  
  16. //static int device_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
  17. //{
  18. //     // send_data[6]="Hello";
  19. //  ctxt->om=0;
  20. //  os_mbuf_append(ctxt->om, &arg, sizeof(arg));
  21. //  for (int i =0;i < sizeof(arg);i++ ){
  22. //
  23. //          printf("%c",(char)send_data[i]);
  24. //      }
  25. //  //y = 1 for no cred found, y =2 wifi cred found y3 Y4 s1,y5y6 s2,y7y8 s3,y9 y10 s4
  26. //
  27. //return 0;
  28. //}
  29. //static int device_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
  30. //{
  31. //    printf("Data from the client: %.*s\n", ctxt->om->om_len, ctxt->om->om_data);
  32. //
  33. //
  34. //    for (int i =0;i < ctxt->om->om_len;i++ ){
  35. //      recieve_data[i]=ctxt->om->om_data[i];
  36. //      printf("%c",recieve_data[i]);
  37. //    }
  38. //
  39. //    return 0;
  40. //}
  41. //
  42.  
  43.  
  44. static int
  45. gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
  46.                    void *dst, uint16_t *len)
  47. {
  48.     uint16_t om_len;
  49.     int rc;
  50.  
  51.     om_len = OS_MBUF_PKTLEN(om);
  52.     if (om_len < min_len || om_len > max_len) {
  53.         return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
  54.     }
  55.  
  56.     rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
  57.     if (rc != 0) {
  58.         return BLE_ATT_ERR_UNLIKELY;
  59.     }
  60.  
  61.     return 0;
  62. }
  63.  
  64. static int
  65. gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
  66.                              struct ble_gatt_access_ctxt *ctxt,
  67.                              void *arg)
  68. {
  69.     const ble_uuid_t *uuid;
  70.     int rc;
  71.      uint8_t dats[50]={0};
  72.     uuid = ctxt->chr->uuid;
  73.  
  74.      if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_uuid.u) == 0) {
  75.         switch (ctxt->op) {
  76.         case BLE_GATT_ACCESS_OP_READ_CHR:
  77.             if(state == 0){
  78.             rc = os_mbuf_append(ctxt->om, &send_data,
  79.                     sizeof (send_data));
  80.  
  81.             return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;}
  82.             else{
  83.                         rc = os_mbuf_append(ctxt->om, &send_data1,
  84.                                 sizeof (send_data1));
  85.                         }
  86.             return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  87.         case BLE_GATT_ACCESS_OP_WRITE_CHR:
  88.             /*   rc = gatt_svr_chr_write(ctxt->om,
  89.                                     sizeof gatt_svr_sec_test_static_val,
  90.                                     sizeof gatt_svr_sec_test_static_val,
  91.                                     &gatt_svr_sec_test_static_val, NULL);*/
  92.             rc = gatt_svr_chr_write(ctxt->om,
  93.                     sizeof dats,
  94.                     sizeof dats,
  95.                     &dats, NULL);
  96.             for (int i =0;i < ctxt->om->om_len;i++ ){
  97.                     recieve_data[i]=(char)dats[i];
  98.                     printf("%c",recieve_data[i]);
  99.                 }
  100.  
  101.             printf("writing done");return rc;
  102.  
  103.         default:
  104.  
  105.             assert(0);
  106.             printf("writing error");
  107.             return BLE_ATT_ERR_UNLIKELY;
  108.         }
  109.  
  110.     }
  111.  
  112.     /* Unknown characteristic; the nimble stack should not have called this
  113.      * function.
  114.      */
  115.     assert(0);
  116.     return BLE_ATT_ERR_UNLIKELY;
  117. }
  118.     static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
  119.         {
  120.             /*** Service: Security test. */
  121.             .type = BLE_GATT_SVC_TYPE_PRIMARY,
  122.             .uuid = &gatt_svr_svc_sec_test_uuid.u,
  123.             .characteristics = (struct ble_gatt_chr_def[])
  124.             {
  125.  
  126.                 {
  127.                     /*** Characteristic: Static value. */
  128.                     .uuid = &gatt_svr_chr_sec_test_static_uuid.u,
  129.                     .access_cb = gatt_svr_chr_access_sec_test,
  130.                     .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC |
  131.                     BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
  132.                 }, {
  133.                     0, /* No more characteristics in this service. */
  134.                 }
  135.             },
  136.         },
  137.  
  138.         {
  139.             0, /* No more services. */
  140.         },
  141.     };
  142.  
  143. void
  144. gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
  145. {
  146.     char buf[BLE_UUID_STR_LEN];
  147.  
  148.     switch (ctxt->op) {
  149.     case BLE_GATT_REGISTER_OP_SVC:
  150.         MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
  151.                     ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
  152.                     ctxt->svc.handle);
  153.         break;
  154.  
  155.     case BLE_GATT_REGISTER_OP_CHR:
  156.         MODLOG_DFLT(DEBUG, "registering characteristic %s with "
  157.                     "def_handle=%d val_handle=%d\n",
  158.                     ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
  159.                     ctxt->chr.def_handle,
  160.                     ctxt->chr.val_handle);
  161.         break;
  162.  
  163.     case BLE_GATT_REGISTER_OP_DSC:
  164.         MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
  165.                     ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
  166.                     ctxt->dsc.handle);
  167.         break;
  168.  
  169.     default:
  170.         assert(0);
  171.         break;
  172.     }
  173. }
  174.  
  175. int
  176. gatt_svr_init(void)
  177. {
  178.     int rc;
  179.  
  180.     ble_svc_gap_init();
  181.     ble_svc_gatt_init();
  182.     ble_svc_ans_init();
  183.  
  184.     rc = ble_gatts_count_cfg(gatt_svr_svcs);
  185.     if (rc != 0) {
  186.         return rc;
  187.     }
  188.  
  189.     rc = ble_gatts_add_svcs(gatt_svr_svcs);
  190.     if (rc != 0) {
  191.         return rc;
  192.     }
  193.  
  194.     return 0;
  195. }
  196.  

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

Re: only one command is running that is read or write to phone in nimble ble. i am using bleprh

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

problem solved. i didnt corrctly passed data from om_data to char array

sokolsok
Posts: 2
Joined: Sun Feb 25, 2024 12:58 pm

Re: only one command is running that is read or write to phone in nimble ble. i am using bleprh

Postby sokolsok » Sun Feb 25, 2024 1:01 pm

I'm right where you are.
Could you tell me the correct way to pass data from om_data to char array?

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 62 guests