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 rand_num;
int rc;
uint8_t dats[50]={0};
uuid = ctxt->chr->uuid;
/* Determine which characteristic is being accessed by examining its
* 128-bit UUID.
*/
if (ble_uuid_cmp(uuid, BLE_UUID16_DECLARE(0xFEF4)) == 0) {
if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR){
// /* Respond with a 32-bit random number. */
// rand_num = rand();
// rc = os_mbuf_append(ctxt->om, &rand_num, sizeof rand_num);
if(state == 0){
rc = os_mbuf_append(ctxt->om, &send_data,
sizeof (send_data));
}
else{
rc = os_mbuf_append(ctxt->om, &send_data1,
sizeof (send_data1));
}
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}}
// esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
else if (ble_uuid_cmp(uuid, BLE_UUID16_DECLARE(0xDEAD)) == 0) {
switch (ctxt->op) {
// case BLE_GATT_ACCESS_OP_READ_CHR:
//
// /* rc = os_mbuf_append(ctxt->om, &gatt_svr_sec_test_static_val,
// sizeof gatt_svr_sec_test_static_val);*/
// // dats[11] = "Hello there";
// 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 0;
default:
assert(0);
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 = BLE_UUID16_DECLARE(0x180),
.characteristics = (struct ble_gatt_chr_def[])
{ {
/*** Characteristic: Random number generator. */
.uuid = BLE_UUID16_DECLARE(0xFEF4),
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
.access_cb = gatt_svr_chr_access_sec_test,
}, {
/*** Characteristic: Static value. */
.uuid = BLE_UUID16_DECLARE(0xDEAD),
.flags = BLE_GATT_CHR_F_WRITE,
.access_cb = gatt_svr_chr_access_sec_test,
}, {
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;
}
}