1
6
7
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
94
95
96
103
104
105
106
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
138
139
140
156
157
158
159
161
162
163
164
165
166
167
168
169
170
171
172
173
176
177
178
179
183
184
185
191
192
193
194
195
196
197
198
199
206
207
208
209
210
211
212
213
214
215
222
225
226
227
228
232
233
234
235
236
237
238
239
240
241
242
247
248
249
250
251
257
258
259
260
261
262
263
264
265
266
269
270
271
272
275
276
277
/* ... */
#include "gatt_svc.h"
#include "common.h"
#include "gap.h"
#include "heart_rate.h"
#include "led.h"5 includes
static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
static int led_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
static const ble_uuid16_t heart_rate_svc_uuid = BLE_UUID16_INIT(0x180D);
static uint8_t heart_rate_chr_val[2] = {0};
static uint16_t heart_rate_chr_val_handle;
static const ble_uuid16_t heart_rate_chr_uuid = BLE_UUID16_INIT(0x2A37);
static uint16_t heart_rate_chr_conn_handle = 0;
static bool heart_rate_chr_conn_handle_inited = false;
static bool heart_rate_ind_status = false;
static const ble_uuid16_t auto_io_svc_uuid = BLE_UUID16_INIT(0x1815);
static uint16_t led_chr_val_handle;
static const ble_uuid128_t led_chr_uuid =
BLE_UUID128_INIT(0x23, 0xd1, 0xbc, 0xea, 0x5f, 0x78, 0x23, 0x15, 0xde, 0xef,
0x12, 0x12, 0x25, 0x15, 0x00, 0x00);
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
{.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &heart_rate_svc_uuid.u,
.characteristics =
(struct ble_gatt_chr_def[]){
{
.uuid = &heart_rate_chr_uuid.u,
.access_cb = heart_rate_chr_access,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_INDICATE |
BLE_GATT_CHR_F_READ_ENC,
.val_handle = &heart_rate_chr_val_handle}{...},
{
0,
}{...}}{...}}{...},
{
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &auto_io_svc_uuid.u,
.characteristics =
(struct ble_gatt_chr_def[]){
{.uuid = &led_chr_uuid.u,
.access_cb = led_chr_access,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
.val_handle = &led_chr_val_handle}{...},
{0}}{...},
}{...},
{
0,
}{...},
}{...};
static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg) {
int rc;
switch (ctxt->op) {
case BLE_GATT_ACCESS_OP_READ_CHR:
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
ESP_LOGI(TAG, "characteristic read; conn_handle=%d attr_handle=%d",
conn_handle, attr_handle);
}{...} else {
ESP_LOGI(TAG, "characteristic read by nimble stack; attr_handle=%d",
attr_handle);
}{...}
if (attr_handle == heart_rate_chr_val_handle) {
heart_rate_chr_val[1] = get_heart_rate();
rc = os_mbuf_append(ctxt->om, &heart_rate_chr_val,
sizeof(heart_rate_chr_val));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}{...}
goto error;
...
default:
goto error;...
}{...}
error:
ESP_LOGE(
TAG,
"unexpected access operation to heart rate characteristic, opcode: %d",
ctxt->op);
return BLE_ATT_ERR_UNLIKELY;
}{ ... }
static int led_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg) {
int rc;
switch (ctxt->op) {
case BLE_GATT_ACCESS_OP_WRITE_CHR:
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
ESP_LOGI(TAG, "characteristic write; conn_handle=%d attr_handle=%d",
conn_handle, attr_handle);
}{...} else {
ESP_LOGI(TAG,
"characteristic write by nimble stack; attr_handle=%d",
attr_handle);
}{...}
if (attr_handle == led_chr_val_handle) {
if (ctxt->om->om_len == 1) {
if (ctxt->om->om_data[0]) {
led_on();
ESP_LOGI(TAG, "led turned on!");
}{...} else {
led_off();
ESP_LOGI(TAG, "led turned off!");
}{...}
}{...} else {
goto error;
}{...}
return rc;
}{...}
goto error;
...
default:
goto error;...
}{...}
error:
ESP_LOGE(TAG,
"unexpected access operation to led characteristic, opcode: %d",
ctxt->op);
return BLE_ATT_ERR_UNLIKELY;
}{ ... }
void send_heart_rate_indication(void) {
if (!heart_rate_chr_conn_handle_inited) {
return;
}{...}
if (heart_rate_ind_status &&
is_connection_encrypted(heart_rate_chr_conn_handle)) {
ble_gatts_indicate(heart_rate_chr_conn_handle,
heart_rate_chr_val_handle);
}{...}
}{ ... }
/* ... */
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:
ESP_LOGD(TAG, "registered service %s with handle=%d",
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
ctxt->svc.handle);
break;
...
case BLE_GATT_REGISTER_OP_CHR:
ESP_LOGD(TAG,
"registering characteristic %s with "
"def_handle=%d val_handle=%d",
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:
ESP_LOGD(TAG, "registering descriptor %s with handle=%d",
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
ctxt->dsc.handle);
break;
...
default:
assert(0);
break;...
}{...}
}{ ... }
/* ... */
int gatt_svr_subscribe_cb(struct ble_gap_event *event) {
if (event->subscribe.attr_handle == heart_rate_chr_val_handle) {
heart_rate_chr_conn_handle = event->subscribe.conn_handle;
heart_rate_chr_conn_handle_inited = true;
heart_rate_ind_status = event->subscribe.cur_indicate;
if (!is_connection_encrypted(event->subscribe.conn_handle)) {
ESP_LOGE(TAG, "failed to subscribe to heart rate measurement, "
"connection not encrypted!");
return BLE_ATT_ERR_INSUFFICIENT_AUTHEN;
}{...}
}{...}
return 0;
}{ ... }
/* ... */
int gatt_svc_init(void) {
int rc;
ble_svc_gatt_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;
}{ ... }