2019-06-24 21:03:58 -04:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "host/ble_hs.h"
|
|
|
|
#include "host/ble_uuid.h"
|
|
|
|
#include "services/gap/ble_svc_gap.h"
|
|
|
|
#include "services/gatt/ble_svc_gatt.h"
|
|
|
|
#include "bleprph.h"
|
2021-12-06 04:38:11 -05:00
|
|
|
#include "services/ans/ble_svc_ans.h"
|
2019-06-24 21:03:58 -04:00
|
|
|
|
2022-11-02 09:04:47 -04:00
|
|
|
/*** Maximum number of characteristics with the notify flag ***/
|
|
|
|
#define MAX_NOTIFY 5
|
2019-06-24 21:03:58 -04:00
|
|
|
|
2022-11-02 09:04:47 -04:00
|
|
|
static const ble_uuid128_t gatt_svr_svc_uuid =
|
2019-06-24 21:03:58 -04:00
|
|
|
BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
|
|
|
|
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
|
|
|
|
|
2022-11-02 09:04:47 -04:00
|
|
|
/* A characteristic that can be subscribed to */
|
|
|
|
static uint8_t gatt_svr_chr_val;
|
|
|
|
static uint16_t gatt_svr_chr_val_handle;
|
|
|
|
static const ble_uuid128_t gatt_svr_chr_uuid =
|
|
|
|
BLE_UUID128_INIT(0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,
|
|
|
|
0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33);
|
|
|
|
|
|
|
|
/* A custom descriptor */
|
|
|
|
static uint8_t gatt_svr_dsc_val;
|
|
|
|
static const ble_uuid128_t gatt_svr_dsc_uuid =
|
|
|
|
BLE_UUID128_INIT(0x01, 0x01, 0x01, 0x01, 0x12, 0x12, 0x12, 0x12,
|
|
|
|
0x23, 0x23, 0x23, 0x23, 0x34, 0x34, 0x34, 0x34);
|
|
|
|
|
2019-06-24 21:03:58 -04:00
|
|
|
static int
|
2022-11-02 09:04:47 -04:00
|
|
|
gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
|
|
|
|
struct ble_gatt_access_ctxt *ctxt,
|
|
|
|
void *arg);
|
|
|
|
|
2019-06-24 21:03:58 -04:00
|
|
|
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
|
|
|
|
{
|
2022-11-02 09:04:47 -04:00
|
|
|
/*** Service ***/
|
2019-06-24 21:03:58 -04:00
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
2022-11-02 09:04:47 -04:00
|
|
|
.uuid = &gatt_svr_svc_uuid.u,
|
2019-06-24 21:03:58 -04:00
|
|
|
.characteristics = (struct ble_gatt_chr_def[])
|
|
|
|
{ {
|
2022-11-02 09:04:47 -04:00
|
|
|
/*** This characteristic can be subscribed to by writing 0x00 and 0x01 to the CCCD ***/
|
|
|
|
.uuid = &gatt_svr_chr_uuid.u,
|
|
|
|
.access_cb = gatt_svc_access,
|
|
|
|
#if CONFIG_EXAMPLE_ENCRYPTION
|
|
|
|
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE |
|
|
|
|
BLE_GATT_CHR_F_READ_ENC | BLE_GATT_CHR_F_WRITE_ENC |
|
2023-02-13 07:25:09 -05:00
|
|
|
BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
|
2022-11-02 09:04:47 -04:00
|
|
|
#else
|
2023-02-13 07:25:09 -05:00
|
|
|
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
|
2022-11-02 09:04:47 -04:00
|
|
|
#endif
|
|
|
|
.val_handle = &gatt_svr_chr_val_handle,
|
|
|
|
.descriptors = (struct ble_gatt_dsc_def[])
|
|
|
|
{ {
|
|
|
|
.uuid = &gatt_svr_dsc_uuid.u,
|
|
|
|
#if CONFIG_EXAMPLE_ENCRYPTION
|
|
|
|
.att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_ENC,
|
|
|
|
#else
|
|
|
|
.att_flags = BLE_ATT_F_READ,
|
|
|
|
#endif
|
|
|
|
.access_cb = gatt_svc_access,
|
|
|
|
}, {
|
|
|
|
0, /* No more descriptors in this characteristic */
|
|
|
|
}
|
|
|
|
},
|
2019-06-24 21:03:58 -04:00
|
|
|
}, {
|
|
|
|
0, /* No more characteristics in this service. */
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
0, /* No more services. */
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2022-11-02 09:04:47 -04:00
|
|
|
gatt_svr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
|
|
|
|
void *dst, uint16_t *len)
|
2019-06-24 21:03:58 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-02 09:04:47 -04:00
|
|
|
/**
|
|
|
|
* Access callback whenever a characteristic/descriptor is read or written to.
|
|
|
|
* Here reads and writes need to be handled.
|
|
|
|
* ctxt->op tells weather the operation is read or write and
|
|
|
|
* weather it is on a characteristic or descriptor,
|
|
|
|
* ctxt->dsc->uuid tells which characteristic/descriptor is accessed.
|
2023-02-13 07:25:09 -05:00
|
|
|
* attr_handle give the value handle of the attribute being accessed.
|
2022-11-02 09:04:47 -04:00
|
|
|
* Accordingly do:
|
|
|
|
* Append the value to ctxt->om if the operation is READ
|
|
|
|
* Write ctxt->om to the value if the operation is WRITE
|
|
|
|
**/
|
2019-06-24 21:03:58 -04:00
|
|
|
static int
|
2022-11-02 09:04:47 -04:00
|
|
|
gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
|
|
|
|
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
2019-06-24 21:03:58 -04:00
|
|
|
{
|
|
|
|
const ble_uuid_t *uuid;
|
|
|
|
int rc;
|
|
|
|
|
2022-11-02 09:04:47 -04:00
|
|
|
switch (ctxt->op) {
|
|
|
|
case BLE_GATT_ACCESS_OP_READ_CHR:
|
2023-02-13 07:25:09 -05:00
|
|
|
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
MODLOG_DFLT(INFO, "Characteristic read; conn_handle=%d attr_handle=%d\n",
|
|
|
|
conn_handle, attr_handle);
|
|
|
|
} else {
|
|
|
|
MODLOG_DFLT(INFO, "Characteristic read by NimBLE stack; attr_handle=%d\n",
|
|
|
|
attr_handle);
|
|
|
|
}
|
2022-11-02 09:04:47 -04:00
|
|
|
uuid = ctxt->chr->uuid;
|
2023-02-13 07:25:09 -05:00
|
|
|
if (attr_handle == gatt_svr_chr_val_handle) {
|
2022-11-02 09:04:47 -04:00
|
|
|
rc = os_mbuf_append(ctxt->om,
|
|
|
|
&gatt_svr_chr_val,
|
|
|
|
sizeof(gatt_svr_chr_val));
|
2019-06-24 21:03:58 -04:00
|
|
|
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
2022-11-02 09:04:47 -04:00
|
|
|
}
|
|
|
|
goto unknown;
|
|
|
|
|
|
|
|
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
2023-02-13 07:25:09 -05:00
|
|
|
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
MODLOG_DFLT(INFO, "Characteristic write; conn_handle=%d attr_handle=%d",
|
|
|
|
conn_handle, attr_handle);
|
|
|
|
} else {
|
|
|
|
MODLOG_DFLT(INFO, "Characteristic write by NimBLE stack; attr_handle=%d",
|
|
|
|
attr_handle);
|
|
|
|
}
|
2022-11-02 09:04:47 -04:00
|
|
|
uuid = ctxt->chr->uuid;
|
2023-02-13 07:25:09 -05:00
|
|
|
if (attr_handle == gatt_svr_chr_val_handle) {
|
2022-11-02 09:04:47 -04:00
|
|
|
rc = gatt_svr_write(ctxt->om,
|
|
|
|
sizeof(gatt_svr_chr_val),
|
|
|
|
sizeof(gatt_svr_chr_val),
|
|
|
|
&gatt_svr_chr_val, NULL);
|
2023-02-13 07:25:09 -05:00
|
|
|
ble_gatts_chr_updated(attr_handle);
|
|
|
|
MODLOG_DFLT(INFO, "Notification/Indication scheduled for "
|
|
|
|
"all subscribed peers.\n");
|
2019-06-24 21:03:58 -04:00
|
|
|
return rc;
|
|
|
|
}
|
2022-11-02 09:04:47 -04:00
|
|
|
goto unknown;
|
|
|
|
|
|
|
|
case BLE_GATT_ACCESS_OP_READ_DSC:
|
2023-02-13 07:25:09 -05:00
|
|
|
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
MODLOG_DFLT(INFO, "Descriptor read; conn_handle=%d attr_handle=%d\n",
|
|
|
|
conn_handle, attr_handle);
|
|
|
|
} else {
|
|
|
|
MODLOG_DFLT(INFO, "Descriptor read by NimBLE stack; attr_handle=%d\n",
|
|
|
|
attr_handle);
|
|
|
|
}
|
2022-11-02 09:04:47 -04:00
|
|
|
uuid = ctxt->dsc->uuid;
|
|
|
|
if (ble_uuid_cmp(uuid, &gatt_svr_dsc_uuid.u) == 0) {
|
|
|
|
rc = os_mbuf_append(ctxt->om,
|
|
|
|
&gatt_svr_dsc_val,
|
|
|
|
sizeof(gatt_svr_chr_val));
|
|
|
|
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
|
|
}
|
|
|
|
goto unknown;
|
|
|
|
|
|
|
|
case BLE_GATT_ACCESS_OP_WRITE_DSC:
|
|
|
|
goto unknown;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto unknown;
|
2019-06-24 21:03:58 -04:00
|
|
|
}
|
|
|
|
|
2022-11-02 09:04:47 -04:00
|
|
|
unknown:
|
|
|
|
/* Unknown characteristic/descriptor;
|
|
|
|
* The NimBLE host should not have called this function;
|
2019-06-24 21:03:58 -04:00
|
|
|
*/
|
|
|
|
assert(0);
|
|
|
|
return BLE_ATT_ERR_UNLIKELY;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2021-12-06 04:38:11 -05:00
|
|
|
ble_svc_ans_init();
|
2019-06-24 21:03:58 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-02 09:04:47 -04:00
|
|
|
/* Setting a value for the read-only descriptor */
|
|
|
|
gatt_svr_dsc_val = 0x99;
|
|
|
|
|
2019-06-24 21:03:58 -04:00
|
|
|
return 0;
|
|
|
|
}
|