-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
blufi_init.c
245 lines (199 loc) · 5.51 KB
/
blufi_init.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
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include "esp_err.h"
#include "esp_blufi_api.h"
#include "esp_log.h"
#include "esp_blufi.h"
#include "blufi_example.h"
#ifdef CONFIG_BT_BLUEDROID_ENABLED
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#endif
#ifdef CONFIG_BT_NIMBLE_ENABLED
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
#include "console/console.h"
#endif
#ifdef CONFIG_BT_BLUEDROID_ENABLED
esp_err_t esp_blufi_host_init(void)
{
int ret;
ret = esp_bluedroid_init();
if (ret) {
BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
return ESP_FAIL;
}
ret = esp_bluedroid_enable();
if (ret) {
BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
return ESP_FAIL;
}
BLUFI_INFO("BD ADDR: "ESP_BD_ADDR_STR"\n", ESP_BD_ADDR_HEX(esp_bt_dev_get_address()));
return ESP_OK;
}
esp_err_t esp_blufi_host_deinit(void)
{
int ret;
ret = esp_blufi_profile_deinit();
if(ret != ESP_OK) {
return ret;
}
ret = esp_bluedroid_disable();
if (ret) {
BLUFI_ERROR("%s deinit bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
return ESP_FAIL;
}
ret = esp_bluedroid_deinit();
if (ret) {
BLUFI_ERROR("%s deinit bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
return ESP_FAIL;
}
ESP_ERROR_CHECK(esp_bt_controller_disable());
ret = esp_bt_controller_deinit();
if (ret) {
BLUFI_ERROR("%s deinit bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
return ESP_FAIL;
}
return ESP_OK;
}
esp_err_t esp_blufi_gap_register_callback(void)
{
int rc;
rc = esp_ble_gap_register_callback(esp_blufi_gap_event_handler);
if(rc){
return rc;
}
return esp_blufi_profile_init();
}
esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks)
{
esp_err_t ret = ESP_OK;
ret = esp_blufi_host_init();
if (ret) {
BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret));
return ret;
}
ret = esp_blufi_register_callbacks(example_callbacks);
if(ret){
BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret);
return ret;
}
ret = esp_blufi_gap_register_callback();
if(ret){
BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret);
return ret;
}
return ESP_OK;
}
#endif /* CONFIG_BT_BLUEDROID_ENABLED */
#ifdef CONFIG_BT_NIMBLE_ENABLED
void ble_store_config_init(void);
static void blufi_on_reset(int reason)
{
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
}
static void
blufi_on_sync(void)
{
esp_blufi_profile_init();
}
void bleprph_host_task(void *param)
{
ESP_LOGI("BLUFI_EXAMPLE", "BLE Host Task Started");
/* This function will return only when nimble_port_stop() is executed */
nimble_port_run();
nimble_port_freertos_deinit();
}
esp_err_t esp_blufi_host_init(void)
{
esp_err_t err;
err = esp_nimble_init();
if (err) {
BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err));
return ESP_FAIL;
}
/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = blufi_on_reset;
ble_hs_cfg.sync_cb = blufi_on_sync;
ble_hs_cfg.gatts_register_cb = esp_blufi_gatt_svr_register_cb;
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
ble_hs_cfg.sm_io_cap = 4;
#ifdef CONFIG_EXAMPLE_BONDING
ble_hs_cfg.sm_bonding = 1;
#endif
#ifdef CONFIG_EXAMPLE_MITM
ble_hs_cfg.sm_mitm = 1;
#endif
#ifdef CONFIG_EXAMPLE_USE_SC
ble_hs_cfg.sm_sc = 1;
#else
ble_hs_cfg.sm_sc = 0;
#ifdef CONFIG_EXAMPLE_BONDING
ble_hs_cfg.sm_our_key_dist = 1;
ble_hs_cfg.sm_their_key_dist = 1;
#endif
#endif
int rc;
rc = esp_blufi_gatt_svr_init();
assert(rc == 0);
/* Set the default device name. */
rc = ble_svc_gap_device_name_set(BLUFI_DEVICE_NAME);
assert(rc == 0);
/* XXX Need to have template for store */
ble_store_config_init();
esp_blufi_btc_init();
err = esp_nimble_enable(bleprph_host_task);
if (err) {
BLUFI_ERROR("%s failed: %s\n", __func__, esp_err_to_name(err));
return ESP_FAIL;
}
return ESP_OK;
}
esp_err_t esp_blufi_host_deinit(void)
{
esp_err_t ret = ESP_OK;
ret = esp_blufi_profile_deinit();
if(ret != ESP_OK) {
return ret;
}
esp_blufi_btc_deinit();
ret = nimble_port_stop();
if (ret == 0) {
nimble_port_deinit();
}
return ret;
}
esp_err_t esp_blufi_gap_register_callback(void)
{
return ESP_OK;
}
esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks)
{
esp_err_t ret = ESP_OK;
ret = esp_blufi_register_callbacks(example_callbacks);
if(ret){
BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret);
return ret;
}
ret = esp_blufi_gap_register_callback();
if(ret){
BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret);
return ret;
}
ret = esp_blufi_host_init();
if (ret) {
BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret));
return ret;
}
return ret;
}
#endif /* CONFIG_BT_NIMBLE_ENABLED */