1 /*
2  * SPDX-FileCopyrightText: 2015-2024 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 #include "hci_log/bt_hci_log.h"
15 #include "bt_common.h"
16 
17 static bool bd_already_enable = false;
18 static bool bd_already_init = false;
19 
esp_bluedroid_get_status(void)20 esp_bluedroid_status_t esp_bluedroid_get_status(void)
21 {
22     if (bd_already_init) {
23         if (bd_already_enable) {
24             return ESP_BLUEDROID_STATUS_ENABLED;
25         } else {
26             return ESP_BLUEDROID_STATUS_INITIALIZED;
27         }
28     } else {
29         return ESP_BLUEDROID_STATUS_UNINITIALIZED;
30     }
31 }
32 
esp_bluedroid_enable(void)33 esp_err_t esp_bluedroid_enable(void)
34 {
35     btc_msg_t msg;
36     future_t **future_p;
37 
38     if (!bd_already_init) {
39         LOG_ERROR("Bludroid not initialised\n");
40         return ESP_ERR_INVALID_STATE;
41     }
42 
43     if (bd_already_enable) {
44         LOG_ERROR("Bluedroid already enabled\n");
45         return ESP_ERR_INVALID_STATE;
46     }
47 
48     future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE);
49     *future_p = future_new();
50     if (*future_p == NULL) {
51         LOG_ERROR("Bluedroid enable failed\n");
52         return ESP_ERR_NO_MEM;
53     }
54 
55     msg.sig = BTC_SIG_API_CALL;
56     msg.pid = BTC_PID_MAIN_INIT;
57     msg.act = BTC_MAIN_ACT_ENABLE;
58 
59     if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
60         LOG_ERROR("Bluedroid enable failed\n");
61         return ESP_FAIL;
62     }
63 
64     if (future_await(*future_p) == FUTURE_FAIL) {
65         LOG_ERROR("Bluedroid enable failed\n");
66         return ESP_FAIL;
67     }
68 
69     bd_already_enable = true;
70 
71     return ESP_OK;
72 }
73 
esp_bluedroid_disable(void)74 esp_err_t esp_bluedroid_disable(void)
75 {
76     btc_msg_t msg;
77     future_t **future_p;
78 
79     if (!bd_already_enable) {
80         LOG_ERROR("Bluedroid already disabled\n");
81         return ESP_ERR_INVALID_STATE;
82     }
83 
84     future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE);
85     *future_p = future_new();
86     if (*future_p == NULL) {
87         LOG_ERROR("Bluedroid disable failed\n");
88         return ESP_ERR_NO_MEM;
89     }
90 
91     msg.sig = BTC_SIG_API_CALL;
92     msg.pid = BTC_PID_MAIN_INIT;
93     msg.act = BTC_MAIN_ACT_DISABLE;
94 
95     if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
96         LOG_ERROR("Bluedroid disable failed\n");
97         return ESP_FAIL;
98     }
99 
100     if (future_await(*future_p) == FUTURE_FAIL) {
101         LOG_ERROR("Bluedroid disable failed\n");
102         return ESP_FAIL;
103     }
104 
105     bd_already_enable = false;
106 
107     return ESP_OK;
108 }
109 
esp_bluedroid_init(void)110 esp_err_t esp_bluedroid_init(void)
111 {
112     btc_msg_t msg;
113     future_t **future_p;
114     bt_status_t ret;
115 
116     if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
117         LOG_ERROR("Controller not initialised\n");
118         return ESP_ERR_INVALID_STATE;
119     }
120 
121     if (bd_already_init) {
122         LOG_ERROR("Bluedroid already initialised\n");
123         return ESP_ERR_INVALID_STATE;
124     }
125 
126 #if HEAP_MEMORY_DEBUG
127     osi_mem_dbg_init();
128 #endif
129 
130     /*
131     * BTC Init
132     */
133     ret = btc_init();
134     if (ret != BT_STATUS_SUCCESS) {
135         LOG_ERROR("Bluedroid Initialize Fail");
136         return ESP_FAIL;
137     }
138 
139     future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
140     *future_p = future_new();
141     if (*future_p == NULL) {
142         LOG_ERROR("Bluedroid Initialize Fail!");
143         return ESP_ERR_NO_MEM;
144     }
145 
146     msg.sig = BTC_SIG_API_CALL;
147     msg.pid = BTC_PID_MAIN_INIT;
148     msg.act = BTC_MAIN_ACT_INIT;
149 
150     if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
151         LOG_ERROR("Bluedroid Initialize Fail");
152         return ESP_FAIL;
153     }
154 
155     if (future_await(*future_p) == FUTURE_FAIL) {
156         LOG_ERROR("Bluedroid Initialize Fail");
157         return ESP_FAIL;
158     }
159 
160     bd_already_init = true;
161 
162 #if (BT_HCI_LOG_INCLUDED == TRUE)
163     bt_hci_log_init();
164 #endif // (BT_HCI_LOG_INCLUDED == TRUE)
165 
166     return ESP_OK;
167 }
168 
169 
esp_bluedroid_deinit(void)170 esp_err_t esp_bluedroid_deinit(void)
171 {
172     btc_msg_t msg;
173     future_t **future_p;
174 
175     if (!bd_already_init) {
176         LOG_ERROR("Bluedroid already de-initialised\n");
177         return ESP_ERR_INVALID_STATE;
178     }
179 
180     if (bd_already_enable) {
181         LOG_ERROR("Bludroid already enabled, do disable first\n");
182         return ESP_ERR_INVALID_STATE;
183     }
184 
185     future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
186     *future_p = future_new();
187     if (*future_p == NULL) {
188         LOG_ERROR("Bluedroid de-initialise failed\n");
189         return ESP_ERR_NO_MEM;
190     }
191 
192     msg.sig = BTC_SIG_API_CALL;
193     msg.pid = BTC_PID_MAIN_INIT;
194     msg.act = BTC_MAIN_ACT_DEINIT;
195 
196     if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
197         LOG_ERROR("Bluedroid de-initialise failed\n");
198         return ESP_FAIL;
199     }
200 
201     if (future_await(*future_p) == FUTURE_FAIL) {
202         LOG_ERROR("Bluedroid de-initialise failed\n");
203         return ESP_FAIL;
204     }
205 
206     btc_deinit();
207 
208 #if (BT_HCI_LOG_INCLUDED == TRUE)
209     bt_hci_log_deinit();
210 #endif // (BT_HCI_LOG_INCLUDED == TRUE)
211 
212     bd_already_init = false;
213 
214     return ESP_OK;
215 }
216 
217 #if defined(CONFIG_EXAMPLE_CI_ID) && defined(CONFIG_EXAMPLE_CI_PIPELINE_ID)
esp_bluedroid_get_example_name(void)218 char *esp_bluedroid_get_example_name(void)
219 {
220     static char example_name[ESP_BLE_ADV_NAME_LEN_MAX];
221     memset(example_name, 0, sizeof(example_name));
222     sprintf(example_name, "BE%02X_%05X_%02X", CONFIG_EXAMPLE_CI_ID & 0xFF,
223             CONFIG_EXAMPLE_CI_PIPELINE_ID & 0xFFFFF, CONFIG_IDF_FIRMWARE_CHIP_ID & 0xFF);
224     return example_name;
225 }
226 #endif
227