Select one of the symbols to view example projects that use it.
 
Outline
#include "gatt_svc.h"
#include "common.h"
#include "gap.h"
#include "heart_rate.h"
#include "led.h"
heart_rate_svc_uuid
heart_rate_chr_val
heart_rate_chr_val_handle
heart_rate_chr_uuid
heart_rate_chr_conn_handle
heart_rate_chr_conn_handle_inited
heart_rate_ind_status
auto_io_svc_uuid
led_chr_val_handle
led_chr_uuid
gatt_svr_svcs
heart_rate_chr_access(uint16_t, uint16_t, struct ble_gatt_access_ctxt *, void *)
led_chr_access(uint16_t, uint16_t, struct ble_gatt_access_ctxt *, void *)
send_heart_rate_indication()
gatt_svr_register_cb(struct ble_gatt_register_ctxt *, void *)
gatt_svr_subscribe_cb(struct ble_gap_event *)
gatt_svc_init()
Files
loading...
SourceVuESP-IDF Framework and ExamplesNimBLE_Security samplemain/src/gatt_svc.c
 
1
2
3
4
5
6
7
8
9
10
11
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ /* Includes */ #include "gatt_svc.h" #include "common.h" #include "gap.h" #include "heart_rate.h" #include "led.h"5 includes /* Private function declarations */ 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); /* Private variables */ /* Heart rate service */ 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; /* Automation IO service */ 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); /* GATT services table */ static const struct ble_gatt_svc_def gatt_svr_svcs[] = { /* Heart rate service */ {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &heart_rate_svc_uuid.u, .characteristics = (struct ble_gatt_chr_def[]){ {/* Heart rate characteristic */ .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, /* No more characteristics in this service. */ }{...}}{...}}{...}, /* Automation IO service */ { .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &auto_io_svc_uuid.u, .characteristics = (struct ble_gatt_chr_def[]){ /* LED characteristic */ {.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, /* No more services. */ }{...}, }{...}; /* Private functions */ static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { /* Local variables */ int rc; /* Handle access events */ /* Note: Heart rate characteristic is read only */ switch (ctxt->op) { /* Read characteristic event */ case BLE_GATT_ACCESS_OP_READ_CHR: /* Verify connection handle */ 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); }{...} /* Verify attribute handle */ if (attr_handle == heart_rate_chr_val_handle) { /* Update access buffer value */ 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; /* Unknown event */... 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) { /* Local variables */ int rc; /* Handle access events */ /* Note: LED characteristic is write only */ switch (ctxt->op) { /* Write characteristic event */ case BLE_GATT_ACCESS_OP_WRITE_CHR: /* Verify connection handle */ 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); }{...} /* Verify attribute handle */ if (attr_handle == led_chr_val_handle) { /* Verify access buffer length */ if (ctxt->om->om_len == 1) { /* Turn the LED on or off according to the operation bit */ 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; /* Unknown event */... default: goto error;... }{...} error: ESP_LOGE(TAG, "unexpected access operation to led characteristic, opcode: %d", ctxt->op); return BLE_ATT_ERR_UNLIKELY; }{ ... } /* Public functions */ void send_heart_rate_indication(void) { /* Check if connection handle is initialized */ if (!heart_rate_chr_conn_handle_inited) { return; }{...} /* Check indication and security status */ 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); }{...} }{ ... } /* * Handle GATT attribute register events * - Service register event * - Characteristic register event * - Descriptor register event *//* ... */ void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) { /* Local variables */ char buf[BLE_UUID_STR_LEN]; /* Handle GATT attributes register events */ switch (ctxt->op) { /* Service register event */ 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; /* Characteristic register event */... 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; /* Descriptor register event */... 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; /* Unknown event */... default: assert(0); break;... }{...} }{ ... } /* * GATT server subscribe event callback * 1. Update heart rate subscription status *//* ... */ int gatt_svr_subscribe_cb(struct ble_gap_event *event) { /* Check attribute handle */ if (event->subscribe.attr_handle == heart_rate_chr_val_handle) { /* Update heart rate subscription status */ heart_rate_chr_conn_handle = event->subscribe.conn_handle; heart_rate_chr_conn_handle_inited = true; heart_rate_ind_status = event->subscribe.cur_indicate; /* Check security status */ 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; }{ ... } /* * GATT server initialization * 1. Initialize GATT service * 2. Update NimBLE host GATT services counter * 3. Add GATT services to server *//* ... */ int gatt_svc_init(void) { /* Local variables */ int rc; /* 1. GATT service initialization */ ble_svc_gatt_init(); /* 2. Update GATT services counter */ rc = ble_gatts_count_cfg(gatt_svr_svcs); if (rc != 0) { return rc; }{...} /* 3. Add GATT services */ rc = ble_gatts_add_svcs(gatt_svr_svcs); if (rc != 0) { return rc; }{...} return 0; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.