1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8 #include "esp_bt_main.h"
9 #include "btc/btc_task.h"
10 #include "btc/btc_main.h"
11 #include "esp_bt.h"
12 #include "osi/future.h"
13 #include "osi/allocator.h"
14
15 static bool bd_already_enable = false;
16 static bool bd_already_init = false;
17
esp_bluedroid_get_status(void)18 esp_bluedroid_status_t esp_bluedroid_get_status(void)
19 {
20 if (bd_already_init) {
21 if (bd_already_enable) {
22 return ESP_BLUEDROID_STATUS_ENABLED;
23 } else {
24 return ESP_BLUEDROID_STATUS_INITIALIZED;
25 }
26 } else {
27 return ESP_BLUEDROID_STATUS_UNINITIALIZED;
28 }
29 }
30
esp_bluedroid_enable(void)31 esp_err_t esp_bluedroid_enable(void)
32 {
33 btc_msg_t msg;
34 future_t **future_p;
35
36 if (!bd_already_init) {
37 LOG_ERROR("Bludroid not initialised\n");
38 return ESP_ERR_INVALID_STATE;
39 }
40
41 if (bd_already_enable) {
42 LOG_ERROR("Bluedroid already enabled\n");
43 return ESP_ERR_INVALID_STATE;
44 }
45
46 future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE);
47 *future_p = future_new();
48 if (*future_p == NULL) {
49 LOG_ERROR("Bluedroid enable failed\n");
50 return ESP_ERR_NO_MEM;
51 }
52
53 msg.sig = BTC_SIG_API_CALL;
54 msg.pid = BTC_PID_MAIN_INIT;
55 msg.act = BTC_MAIN_ACT_ENABLE;
56
57 if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
58 LOG_ERROR("Bluedroid enable failed\n");
59 return ESP_FAIL;
60 }
61
62 if (future_await(*future_p) == FUTURE_FAIL) {
63 LOG_ERROR("Bluedroid enable failed\n");
64 return ESP_FAIL;
65 }
66
67 bd_already_enable = true;
68
69 return ESP_OK;
70 }
71
esp_bluedroid_disable(void)72 esp_err_t esp_bluedroid_disable(void)
73 {
74 btc_msg_t msg;
75 future_t **future_p;
76
77 if (!bd_already_enable) {
78 LOG_ERROR("Bluedroid already disabled\n");
79 return ESP_ERR_INVALID_STATE;
80 }
81
82 future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE);
83 *future_p = future_new();
84 if (*future_p == NULL) {
85 LOG_ERROR("Bluedroid disable failed\n");
86 return ESP_ERR_NO_MEM;
87 }
88
89 msg.sig = BTC_SIG_API_CALL;
90 msg.pid = BTC_PID_MAIN_INIT;
91 msg.act = BTC_MAIN_ACT_DISABLE;
92
93 if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
94 LOG_ERROR("Bluedroid disable failed\n");
95 return ESP_FAIL;
96 }
97
98 if (future_await(*future_p) == FUTURE_FAIL) {
99 LOG_ERROR("Bluedroid disable failed\n");
100 return ESP_FAIL;
101 }
102
103 bd_already_enable = false;
104
105 return ESP_OK;
106 }
107
esp_bluedroid_init(void)108 esp_err_t esp_bluedroid_init(void)
109 {
110 btc_msg_t msg;
111 future_t **future_p;
112 bt_status_t ret;
113
114 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
115 LOG_ERROR("Controller not initialised\n");
116 return ESP_ERR_INVALID_STATE;
117 }
118
119 if (bd_already_init) {
120 LOG_ERROR("Bluedroid already initialised\n");
121 return ESP_ERR_INVALID_STATE;
122 }
123
124 #if HEAP_MEMORY_DEBUG
125 osi_mem_dbg_init();
126 #endif
127
128 /*
129 * BTC Init
130 */
131 ret = btc_init();
132 if (ret != BT_STATUS_SUCCESS) {
133 LOG_ERROR("Bluedroid Initialize Fail");
134 return ESP_FAIL;
135 }
136
137 future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
138 *future_p = future_new();
139 if (*future_p == NULL) {
140 LOG_ERROR("Bluedroid Initialize Fail!");
141 return ESP_ERR_NO_MEM;
142 }
143
144 msg.sig = BTC_SIG_API_CALL;
145 msg.pid = BTC_PID_MAIN_INIT;
146 msg.act = BTC_MAIN_ACT_INIT;
147
148 if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
149 LOG_ERROR("Bluedroid Initialize Fail");
150 return ESP_FAIL;
151 }
152
153 if (future_await(*future_p) == FUTURE_FAIL) {
154 LOG_ERROR("Bluedroid Initialize Fail");
155 return ESP_FAIL;
156 }
157
158 bd_already_init = true;
159
160 return ESP_OK;
161 }
162
163
esp_bluedroid_deinit(void)164 esp_err_t esp_bluedroid_deinit(void)
165 {
166 btc_msg_t msg;
167 future_t **future_p;
168
169 if (!bd_already_init) {
170 LOG_ERROR("Bluedroid already de-initialised\n");
171 return ESP_ERR_INVALID_STATE;
172 }
173
174 if (bd_already_enable) {
175 LOG_ERROR("Bludroid already enabled, do disable first\n");
176 return ESP_ERR_INVALID_STATE;
177 }
178
179 future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
180 *future_p = future_new();
181 if (*future_p == NULL) {
182 LOG_ERROR("Bluedroid de-initialise failed\n");
183 return ESP_ERR_NO_MEM;
184 }
185
186 msg.sig = BTC_SIG_API_CALL;
187 msg.pid = BTC_PID_MAIN_INIT;
188 msg.act = BTC_MAIN_ACT_DEINIT;
189
190 if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
191 LOG_ERROR("Bluedroid de-initialise failed\n");
192 return ESP_FAIL;
193 }
194
195 if (future_await(*future_p) == FUTURE_FAIL) {
196 LOG_ERROR("Bluedroid de-initialise failed\n");
197 return ESP_FAIL;
198 }
199
200 btc_deinit();
201
202 bd_already_init = false;
203
204 return ESP_OK;
205 }
206