-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
simple_ble.c
333 lines (301 loc) · 11.5 KB
/
simple_ble.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <inttypes.h>
#include <freertos/FreeRTOS.h>
#include <esp_system.h>
#include <esp_log.h>
#include "esp_bt.h"
#include <esp_gap_ble_api.h>
#include <esp_gatts_api.h>
#include <esp_bt_main.h>
#include <esp_gatt_common_api.h>
#include "simple_ble.h"
static uint16_t g_ble_max_gatt_table_size;
static const char *TAG = "simple_ble";
static simple_ble_cfg_t *g_ble_cfg_p;
static uint16_t *g_gatt_table_map;
static uint8_t adv_config_done;
static esp_bd_addr_t s_cached_remote_bda = {0x0,};
#define adv_config_flag (1 << 0)
#define scan_rsp_config_flag (1 << 1)
const uint8_t *simple_ble_get_uuid128(uint16_t handle)
{
const uint8_t *uuid128_ptr;
for (int i = 0; i < g_ble_max_gatt_table_size; i++) {
if (g_gatt_table_map[i] == handle) {
uuid128_ptr = (const uint8_t *) g_ble_cfg_p->gatt_db[i].att_desc.uuid_p;
return uuid128_ptr;
}
}
return NULL;
}
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~adv_config_flag);
if (adv_config_done == 0) {
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
}
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~scan_rsp_config_flag);
if (adv_config_done == 0) {
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
}
break;
case ESP_GAP_BLE_SEC_REQ_EVT:
esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
break;
default:
break;
}
}
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t p_gatts_if, esp_ble_gatts_cb_param_t *param)
{
static esp_gatt_if_t gatts_if = ESP_GATT_IF_NONE;
esp_err_t ret;
uint8_t service_instance_id = 0;
if (event == ESP_GATTS_REG_EVT) {
if (param->reg.status == ESP_GATT_OK) {
gatts_if = p_gatts_if;
} else {
ESP_LOGE(TAG, "reg app failed, app_id 0x0x%x, status %d",
param->reg.app_id,
param->reg.status);
return;
}
}
if (gatts_if != ESP_GATT_IF_NONE && gatts_if != p_gatts_if) {
return;
}
switch (event) {
case ESP_GATTS_REG_EVT:
ret = esp_ble_gatts_create_attr_tab(g_ble_cfg_p->gatt_db, gatts_if, g_ble_cfg_p->gatt_db_count, service_instance_id);
if (ret) {
ESP_LOGE(TAG, "create attr table failed, error code = 0x%x", ret);
return;
}
ret = esp_ble_gap_set_device_name(g_ble_cfg_p->device_name);
if (ret) {
ESP_LOGE(TAG, "set device name failed, error code = 0x%x", ret);
return;
}
ret = esp_ble_gap_config_adv_data(g_ble_cfg_p->adv_data_p);
if (ret) {
ESP_LOGE(TAG, "config raw adv data failed, error code = 0x%x ", ret);
return;
}
adv_config_done |= adv_config_flag;
ret = esp_ble_gap_config_adv_data(g_ble_cfg_p->scan_rsp_data_p);
if (ret) {
ESP_LOGE(TAG, "config raw scan rsp data failed, error code = 0x%x", ret);
return;
}
adv_config_done |= scan_rsp_config_flag;
break;
case ESP_GATTS_READ_EVT:
g_ble_cfg_p->read_fn(event, gatts_if, param);
break;
case ESP_GATTS_WRITE_EVT:
g_ble_cfg_p->write_fn(event, gatts_if, param);
break;
case ESP_GATTS_EXEC_WRITE_EVT:
g_ble_cfg_p->exec_write_fn(event, gatts_if, param);
break;
case ESP_GATTS_MTU_EVT:
ESP_LOGD(TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
if (g_ble_cfg_p->set_mtu_fn) {
g_ble_cfg_p->set_mtu_fn(event, gatts_if, param);
}
break;
case ESP_GATTS_CONF_EVT:
ESP_LOGD(TAG, "ESP_GATTS_CONF_EVT, status = %d", param->conf.status);
break;
case ESP_GATTS_START_EVT:
ESP_LOGD(TAG, "SERVICE_START_EVT, status %d, service_handle %d", param->start.status, param->start.service_handle);
break;
case ESP_GATTS_CONNECT_EVT:
ESP_LOGD(TAG, "ESP_GATTS_CONNECT_EVT, conn_id = %d", param->connect.conn_id);
g_ble_cfg_p->connect_fn(event, gatts_if, param);
esp_ble_conn_update_params_t conn_params = {0};
memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
memcpy(s_cached_remote_bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
/* For the iOS system, please refer the official Apple documents about BLE connection parameters restrictions. */
conn_params.latency = 0;
conn_params.max_int = 0x20; // max_int = 0x20*1.25ms = 40ms
conn_params.min_int = 0x10; // min_int = 0x10*1.25ms = 20ms
conn_params.timeout = 400; // timeout = 400*10ms = 4000ms
esp_ble_gap_update_conn_params(&conn_params);
break;
case ESP_GATTS_DISCONNECT_EVT:
ESP_LOGD(TAG, "ESP_GATTS_DISCONNECT_EVT, reason = %d", param->disconnect.reason);
g_ble_cfg_p->disconnect_fn(event, gatts_if, param);
memset(s_cached_remote_bda, 0, sizeof(esp_bd_addr_t));
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
break;
case ESP_GATTS_CREAT_ATTR_TAB_EVT: {
if (param->add_attr_tab.status != ESP_GATT_OK) {
ESP_LOGE(TAG, "creating the attribute table failed, error code=0x%x", param->add_attr_tab.status);
} else if (param->add_attr_tab.num_handle != g_ble_cfg_p->gatt_db_count) {
ESP_LOGE(TAG, "created attribute table abnormally ");
} else {
ESP_LOGD(TAG, "created attribute table successfully, the number handle = %d", param->add_attr_tab.num_handle);
g_gatt_table_map = (uint16_t *) calloc(param->add_attr_tab.num_handle, sizeof(uint16_t));
if (g_gatt_table_map == NULL) {
ESP_LOGE(TAG, "Memory allocation for GATT_TABLE_MAP failed ");
break;
}
/* Update g_ble_max_gatt_table_size with number of handles */
g_ble_max_gatt_table_size = param->add_attr_tab.num_handle;
memcpy(g_gatt_table_map, param->add_attr_tab.handles, param->add_attr_tab.num_handle * sizeof(g_gatt_table_map[0]));
/* We assume, for now, that the first entry is always the index to the 'service' definition */
esp_ble_gatts_start_service(g_gatt_table_map[0]);
}
break;
}
case ESP_GATTS_STOP_EVT:
case ESP_GATTS_OPEN_EVT:
case ESP_GATTS_CANCEL_OPEN_EVT:
case ESP_GATTS_CLOSE_EVT:
case ESP_GATTS_LISTEN_EVT:
case ESP_GATTS_CONGEST_EVT:
case ESP_GATTS_UNREG_EVT:
case ESP_GATTS_DELETE_EVT:
default:
break;
}
}
simple_ble_cfg_t *simple_ble_init(void)
{
simple_ble_cfg_t *ble_cfg_p = (simple_ble_cfg_t *) malloc(sizeof(simple_ble_cfg_t));
if (ble_cfg_p == NULL) {
ESP_LOGE(TAG, "No memory for simple_ble_cfg_t");
return NULL;
}
return ble_cfg_p;
}
esp_err_t simple_ble_deinit(void)
{
free(g_ble_cfg_p->gatt_db);
g_ble_cfg_p->gatt_db = NULL;
free(g_ble_cfg_p);
g_ble_cfg_p = NULL;
free(g_gatt_table_map);
g_gatt_table_map = NULL;
g_ble_max_gatt_table_size = 0;
return ESP_OK;
}
/* Expects the pointer stays valid throughout */
esp_err_t simple_ble_start(simple_ble_cfg_t *cfg)
{
g_ble_cfg_p = cfg;
ESP_LOGD(TAG, "Free mem at start of simple_ble_init %" PRIu32, esp_get_free_heap_size());
esp_err_t ret;
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(TAG, "%s enable controller failed %d", __func__, ret);
return ret;
}
#ifdef CONFIG_BTDM_CTRL_MODE_BTDM
ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
#elif defined CONFIG_BTDM_CTRL_MODE_BLE_ONLY || CONFIG_BT_CTRL_MODE_EFF
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
#else
ESP_LOGE(TAG, "Configuration mismatch. Select BLE Only or BTDM mode from menuconfig");
return ESP_FAIL;
#endif
if (ret) {
ESP_LOGE(TAG, "%s enable controller failed %d", __func__, ret);
return ret;
}
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(TAG, "%s init bluetooth failed %d", __func__, ret);
return ret;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(TAG, "%s enable bluetooth failed %d", __func__, ret);
return ret;
}
ret = esp_ble_gatts_register_callback(gatts_profile_event_handler);
if (ret) {
ESP_LOGE(TAG, "gatts register error, error code = 0x%x", ret);
return ret;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret) {
ESP_LOGE(TAG, "gap register error, error code = 0x%x", ret);
return ret;
}
uint16_t app_id = 0x55;
ret = esp_ble_gatts_app_register(app_id);
if (ret) {
ESP_LOGE(TAG, "gatts app register error, error code = 0x%x", ret);
return ret;
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret) {
ESP_LOGE(TAG, "set local MTU failed, error code = 0x%x", local_mtu_ret);
}
ESP_LOGD(TAG, "Free mem at end of simple_ble_init %" PRIu32, esp_get_free_heap_size());
/* set the security iocap & auth_req & key size & init key response key parameters to the stack*/
esp_ble_auth_req_t auth_req= ESP_LE_AUTH_REQ_MITM;
if (cfg->ble_bonding) {
auth_req |= ESP_LE_AUTH_BOND; //bonding with peer device after authentication
}
if (cfg->ble_sm_sc) {
auth_req |= ESP_LE_AUTH_REQ_SC_ONLY;
}
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE; //set the IO capability to No output No input
uint8_t key_size = 16; //the key size should be 7~16 bytes
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
return ESP_OK;
}
esp_err_t simple_ble_stop(void)
{
esp_err_t err;
ESP_LOGD(TAG, "Free mem at start of simple_ble_stop %" PRIu32, esp_get_free_heap_size());
err = esp_bluedroid_disable();
if (err != ESP_OK) {
return ESP_FAIL;
}
ESP_LOGD(TAG, "esp_bluedroid_disable called successfully");
err = esp_bluedroid_deinit();
if (err != ESP_OK) {
return err;
}
ESP_LOGD(TAG, "esp_bluedroid_deinit called successfully");
err = esp_bt_controller_disable();
if (err != ESP_OK) {
return ESP_FAIL;
}
/* The API `esp_bt_controller_deinit` will have to be removed when we add support for
* `reset to provisioning`
*/
ESP_LOGD(TAG, "esp_bt_controller_disable called successfully");
err = esp_bt_controller_deinit();
if (err != ESP_OK) {
return ESP_FAIL;
}
ESP_LOGD(TAG, "esp_bt_controller_deinit called successfully");
ESP_LOGD(TAG, "Free mem at end of simple_ble_stop %" PRIu32, esp_get_free_heap_size());
return ESP_OK;
}
#ifdef CONFIG_WIFI_PROV_DISCONNECT_AFTER_PROV
esp_err_t simple_ble_disconnect(void)
{
return esp_ble_gap_disconnect(s_cached_remote_bda);
}
#endif