2016-11-16 03:26:02 -05:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-11-15 22:28:41 -05:00
|
|
|
|
|
|
|
|
2016-11-16 03:26:02 -05:00
|
|
|
#include "esp_bt_main.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "btc/btc_task.h"
|
|
|
|
#include "btc/btc_main.h"
|
2017-12-07 08:48:27 -05:00
|
|
|
#include "esp_bt.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "osi/future.h"
|
|
|
|
#include "osi/allocator.h"
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
static bool bd_already_enable = false;
|
|
|
|
static bool bd_already_init = false;
|
2016-11-15 22:28:41 -05:00
|
|
|
|
2017-01-10 22:04:41 -05:00
|
|
|
esp_bluedroid_status_t esp_bluedroid_get_status(void)
|
|
|
|
{
|
2017-03-03 05:42:27 -05:00
|
|
|
if (bd_already_init) {
|
|
|
|
if (bd_already_enable) {
|
2016-12-23 11:28:47 -05:00
|
|
|
return ESP_BLUEDROID_STATUS_ENABLED;
|
|
|
|
} else {
|
|
|
|
return ESP_BLUEDROID_STATUS_INITIALIZED;
|
|
|
|
}
|
2017-01-10 22:04:41 -05:00
|
|
|
} else {
|
2016-12-23 11:28:47 -05:00
|
|
|
return ESP_BLUEDROID_STATUS_UNINITIALIZED;
|
2017-01-10 22:04:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 02:53:06 -05:00
|
|
|
esp_err_t esp_bluedroid_enable(void)
|
2016-11-15 22:28:41 -05:00
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
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");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE);
|
|
|
|
*future_p = future_new();
|
|
|
|
if (*future_p == NULL) {
|
2017-03-03 05:42:27 -05:00
|
|
|
LOG_ERROR("Bluedroid enable failed\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_MAIN_INIT;
|
|
|
|
msg.act = BTC_MAIN_ACT_ENABLE;
|
2017-06-13 05:14:50 -04:00
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
|
|
|
LOG_ERROR("Bluedroid enable failed\n");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2017-03-03 05:42:27 -05:00
|
|
|
LOG_ERROR("Bluedroid enable failed\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
bd_already_enable = true;
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
return ESP_OK;
|
2016-11-15 22:28:41 -05:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:53:06 -05:00
|
|
|
esp_err_t esp_bluedroid_disable(void)
|
2016-11-15 22:28:41 -05:00
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
if (!bd_already_enable) {
|
|
|
|
LOG_ERROR("Bluedroid already disabled\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE);
|
|
|
|
*future_p = future_new();
|
|
|
|
if (*future_p == NULL) {
|
2017-03-03 05:42:27 -05:00
|
|
|
LOG_ERROR("Bluedroid disable failed\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_MAIN_INIT;
|
|
|
|
msg.act = BTC_MAIN_ACT_DISABLE;
|
2017-06-13 05:14:50 -04:00
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
|
|
|
LOG_ERROR("Bluedroid disable failed\n");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2017-03-03 05:42:27 -05:00
|
|
|
LOG_ERROR("Bluedroid disable failed\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
bd_already_enable = false;
|
2016-11-24 13:10:15 -05:00
|
|
|
|
|
|
|
return ESP_OK;
|
2016-11-15 22:28:41 -05:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:53:06 -05:00
|
|
|
esp_err_t esp_bluedroid_init(void)
|
2016-11-15 22:28:41 -05:00
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
2020-05-23 03:11:20 -04:00
|
|
|
bt_status_t ret;
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2017-02-20 12:05:37 -05:00
|
|
|
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
|
2018-12-13 20:23:27 -05:00
|
|
|
LOG_ERROR("Controller not initialised\n");
|
2017-02-20 12:05:37 -05:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
if (bd_already_init) {
|
|
|
|
LOG_ERROR("Bluedroid already initialised\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2018-09-17 04:44:38 -04:00
|
|
|
#if HEAP_MEMORY_DEBUG
|
2017-06-13 08:52:26 -04:00
|
|
|
osi_mem_dbg_init();
|
|
|
|
#endif
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
/*
|
2020-05-23 03:11:20 -04:00
|
|
|
* BTC Init
|
|
|
|
*/
|
|
|
|
ret = btc_init();
|
|
|
|
if (ret != BT_STATUS_SUCCESS) {
|
|
|
|
LOG_ERROR("Bluedroid Initialize Fail");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2018-11-06 02:34:38 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
|
|
|
|
*future_p = future_new();
|
|
|
|
if (*future_p == NULL) {
|
2020-05-23 03:11:20 -04:00
|
|
|
LOG_ERROR("Bluedroid Initialize Fail!");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_MAIN_INIT;
|
|
|
|
msg.act = BTC_MAIN_ACT_INIT;
|
2017-06-13 05:14:50 -04:00
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
2020-05-23 03:11:20 -04:00
|
|
|
LOG_ERROR("Bluedroid Initialize Fail");
|
2017-06-13 05:14:50 -04:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2020-05-23 03:11:20 -04:00
|
|
|
LOG_ERROR("Bluedroid Initialize Fail");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
bd_already_init = true;
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_OK;
|
2016-11-15 22:28:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-03 02:53:06 -05:00
|
|
|
esp_err_t esp_bluedroid_deinit(void)
|
2016-11-15 22:28:41 -05:00
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
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");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
|
|
|
|
*future_p = future_new();
|
|
|
|
if (*future_p == NULL) {
|
2017-03-03 05:42:27 -05:00
|
|
|
LOG_ERROR("Bluedroid de-initialise failed\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_MAIN_INIT;
|
|
|
|
msg.act = BTC_MAIN_ACT_DEINIT;
|
2017-06-13 05:14:50 -04:00
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
|
|
|
LOG_ERROR("Bluedroid de-initialise failed\n");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2017-03-03 05:42:27 -05:00
|
|
|
LOG_ERROR("Bluedroid de-initialise failed\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
btc_deinit();
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2017-03-03 05:42:27 -05:00
|
|
|
bd_already_init = false;
|
2016-11-16 03:26:02 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return ESP_OK;
|
2016-11-15 22:28:41 -05:00
|
|
|
}
|