only one command is running that is read or write to phone in nimble ble. i am using bleprh
Posted: 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.
but i can only able to send the data to phone by esp32. but data sent by phone doesnt appear on esp32.
Code: Untitled.txt Select all
static const ble_uuid128_t gatt_svr_svc_sec_test_uuid =
BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
///* 5c3a659e-897e-45e1-b016-007107c96df6 */
//static const ble_uuid128_t gatt_svr_chr_sec_test_rand_uuid =
// BLE_UUID128_INIT(0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
// 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
//
///* 5c3a659e-897e-45e1-b016-007107c96df7 */
static const ble_uuid128_t gatt_svr_chr_sec_test_static_uuid =
BLE_UUID128_INIT(0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
//static int device_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
//{
// // send_data[6]="Hello";
// ctxt->om=0;
// os_mbuf_append(ctxt->om, &arg, sizeof(arg));
// for (int i =0;i < sizeof(arg);i++ ){
//
// printf("%c",(char)send_data[i]);
// }
// //y = 1 for no cred found, y =2 wifi cred found y3 Y4 s1,y5y6 s2,y7y8 s3,y9 y10 s4
//
//return 0;
//}
//static int device_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
//{
// printf("Data from the client: %.*s\n", ctxt->om->om_len, ctxt->om->om_data);
//
//
// for (int i =0;i < ctxt->om->om_len;i++ ){
// recieve_data[i]=ctxt->om->om_data[i];
// printf("%c",recieve_data[i]);
// }
//
// return 0;
//}
//
static int
gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
void *dst, uint16_t *len)
{
uint16_t om_len;
int rc;
om_len = OS_MBUF_PKTLEN(om);
if (om_len < min_len || om_len > max_len) {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
if (rc != 0) {
return BLE_ATT_ERR_UNLIKELY;
}
return 0;
}
static int
gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt,
void *arg)
{
const ble_uuid_t *uuid;
int rc;
uint8_t dats[50]={0};
uuid = ctxt->chr->uuid;
if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_uuid.u) == 0) {
switch (ctxt->op) {
case BLE_GATT_ACCESS_OP_READ_CHR:
if(state == 0){
rc = os_mbuf_append(ctxt->om, &send_data,
sizeof (send_data));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;}
else{
rc = os_mbuf_append(ctxt->om, &send_data1,
sizeof (send_data1));
}
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
case BLE_GATT_ACCESS_OP_WRITE_CHR:
/* rc = gatt_svr_chr_write(ctxt->om,
sizeof gatt_svr_sec_test_static_val,
sizeof gatt_svr_sec_test_static_val,
&gatt_svr_sec_test_static_val, NULL);*/
rc = gatt_svr_chr_write(ctxt->om,
sizeof dats,
sizeof dats,
&dats, NULL);
for (int i =0;i < ctxt->om->om_len;i++ ){
recieve_data[i]=(char)dats[i];
printf("%c",recieve_data[i]);
}
printf("writing done");return rc;
default:
assert(0);
printf("writing error");
return BLE_ATT_ERR_UNLIKELY;
}
}
/* Unknown characteristic; the nimble stack should not have called this
* function.
*/
assert(0);
return BLE_ATT_ERR_UNLIKELY;
}
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
{
/*** Service: Security test. */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &gatt_svr_svc_sec_test_uuid.u,
.characteristics = (struct ble_gatt_chr_def[])
{
{
/*** Characteristic: Static value. */
.uuid = &gatt_svr_chr_sec_test_static_uuid.u,
.access_cb = gatt_svr_chr_access_sec_test,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC |
BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
}, {
0, /* No more characteristics in this service. */
}
},
},
{
0, /* No more services. */
},
};
void
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
{
char buf[BLE_UUID_STR_LEN];
switch (ctxt->op) {
case BLE_GATT_REGISTER_OP_SVC:
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
ctxt->svc.handle);
break;
case BLE_GATT_REGISTER_OP_CHR:
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
"def_handle=%d val_handle=%d\n",
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
ctxt->chr.def_handle,
ctxt->chr.val_handle);
break;
case BLE_GATT_REGISTER_OP_DSC:
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
ctxt->dsc.handle);
break;
default:
assert(0);
break;
}
}
int
gatt_svr_init(void)
{
int rc;
ble_svc_gap_init();
ble_svc_gatt_init();
ble_svc_ans_init();
rc = ble_gatts_count_cfg(gatt_svr_svcs);
if (rc != 0) {
return rc;
}
rc = ble_gatts_add_svcs(gatt_svr_svcs);
if (rc != 0) {
return rc;
}
return 0;
}