1
6
7
8
9
10
11
12
13
14
15
20
21
22
23
24
36
37
38
39
40
41
42
46
47
51
52
53
54
58
59
60
61
62
63
67
68
72
73
74
75
76
77
78
79
80
81
82
83
87
88
89
90
94
95
96
97
98
99
103
104
108
109
110
111
112
113
114
119
120
121
122
123
124
125
126
130
131
132
137
138
142
143
144
145
146
147
148
152
153
156
157
161
162
163
164
168
169
170
171
172
173
177
178
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
202
203
207
208
209
210
214
215
216
217
218
219
223
224
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* ... */
#include "common/bt_target.h"
#include "esp_bt_main.h"
#include "btc/btc_task.h"
#include "btc/btc_main.h"
#if (BT_CONTROLLER_INCLUDED == TRUE)
#include "esp_bt.h"
#endif
#include "osi/future.h"
#include "osi/allocator.h"
#include "config/stack_config.h"
#include "hci_log/bt_hci_log.h"
#include "bt_common.h"5 includes
static bool bd_already_enable = false;
static bool bd_already_init = false;
esp_bluedroid_status_t esp_bluedroid_get_status(void)
{
if (bd_already_init) {
if (bd_already_enable) {
return ESP_BLUEDROID_STATUS_ENABLED;
}{...} else {
return ESP_BLUEDROID_STATUS_INITIALIZED;
}{...}
}{...} else {
return ESP_BLUEDROID_STATUS_UNINITIALIZED;
}{...}
}{ ... }
esp_err_t esp_bluedroid_enable(void)
{
btc_msg_t msg;
future_t **future_p;
if (!bd_already_init) {
LOG_ERROR("Bludroid not initialised\n");
return ESP_ERR_INVALID_STATE;
}{...}
if (bd_already_enable) {
LOG_ERROR("Bluedroid already enabled\n");
return ESP_ERR_INVALID_STATE;
}{...}
future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE);
*future_p = future_new();
if (*future_p == NULL) {
LOG_ERROR("Bluedroid enable failed\n");
return ESP_ERR_NO_MEM;
}{...}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_ENABLE;
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid enable failed\n");
return ESP_FAIL;
}{...}
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid enable failed\n");
return ESP_FAIL;
}{...}
bd_already_enable = true;
return ESP_OK;
}{ ... }
esp_err_t esp_bluedroid_disable(void)
{
btc_msg_t msg;
future_t **future_p;
if (!bd_already_enable) {
LOG_ERROR("Bluedroid already disabled\n");
return ESP_ERR_INVALID_STATE;
}{...}
future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE);
*future_p = future_new();
if (*future_p == NULL) {
LOG_ERROR("Bluedroid disable failed\n");
return ESP_ERR_NO_MEM;
}{...}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_DISABLE;
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid disable failed\n");
return ESP_FAIL;
}{...}
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid disable failed\n");
return ESP_FAIL;
}{...}
bd_already_enable = false;
return ESP_OK;
}{ ... }
esp_err_t esp_bluedroid_init(void)
{
esp_bluedroid_config_t cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();
return esp_bluedroid_init_with_cfg(&cfg);
}{ ... }
esp_err_t esp_bluedroid_init_with_cfg(esp_bluedroid_config_t *cfg)
{
btc_msg_t msg;
future_t **future_p;
bt_status_t ret;
if (!cfg) {
LOG_ERROR("%s cfg is NULL", __func__);
return ESP_ERR_INVALID_ARG;
}{...}
#if (BT_CONTROLLER_INCLUDED == TRUE)
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
LOG_ERROR("Controller not initialised\n");
return ESP_ERR_INVALID_STATE;
}{...}
#endif/* ... */
if (bd_already_init) {
LOG_ERROR("Bluedroid already initialised\n");
return ESP_ERR_INVALID_STATE;
}{...}
#if HEAP_MEMORY_DEBUG
osi_mem_dbg_init();
#endif
ret = bluedriod_config_init(cfg);
if (ret != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid stack initialize fail, ret:%d", ret);
return ESP_FAIL;
}{...}
/* ... */
ret = btc_init();
if (ret != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid Initialize Fail");
return ESP_FAIL;
}{...}
future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
*future_p = future_new();
if (*future_p == NULL) {
LOG_ERROR("Bluedroid Initialize Fail!");
return ESP_ERR_NO_MEM;
}{...}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_INIT;
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid Initialize Fail");
return ESP_FAIL;
}{...}
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid Initialize Fail");
return ESP_FAIL;
}{...}
bd_already_init = true;
#if (BT_HCI_LOG_INCLUDED == TRUE)
bt_hci_log_init();
#endif
return ESP_OK;
}{ ... }
esp_err_t esp_bluedroid_deinit(void)
{
btc_msg_t msg;
future_t **future_p;
if (!bd_already_init) {
LOG_ERROR("Bluedroid already de-initialised\n");
return ESP_ERR_INVALID_STATE;
}{...}
if (bd_already_enable) {
LOG_ERROR("Bludroid already enabled, do disable first\n");
return ESP_ERR_INVALID_STATE;
}{...}
future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
*future_p = future_new();
if (*future_p == NULL) {
LOG_ERROR("Bluedroid de-initialise failed\n");
return ESP_ERR_NO_MEM;
}{...}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_DEINIT;
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid de-initialise failed\n");
return ESP_FAIL;
}{...}
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid de-initialise failed\n");
return ESP_FAIL;
}{...}
btc_deinit();
bluedriod_config_deinit();
#if (BT_HCI_LOG_INCLUDED == TRUE)
bt_hci_log_deinit();
#endif
bd_already_init = false;
return ESP_OK;
}{ ... }
#if defined(CONFIG_EXAMPLE_CI_ID) && defined(CONFIG_EXAMPLE_CI_PIPELINE_ID)
char *esp_bluedroid_get_example_name(void)
{
static char example_name[ESP_BLE_ADV_NAME_LEN_MAX];
memset(example_name, 0, sizeof(example_name));
sprintf(example_name, "BE%02X_%05X_%02X", CONFIG_EXAMPLE_CI_ID & 0xFF,
CONFIG_EXAMPLE_CI_PIPELINE_ID & 0xFFFFF, CONFIG_IDF_FIRMWARE_CHIP_ID & 0xFF);
return example_name;
}{...}
/* ... */#endif