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