2021-12-06 01:59:55 -05:00
|
|
|
/*
|
2024-04-08 08:16:28 -04:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-12-06 01:59:55 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-11-15 22:28:41 -05:00
|
|
|
|
|
|
|
|
2023-08-31 21:29:40 -04:00
|
|
|
#include "common/bt_target.h"
|
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"
|
2023-08-31 21:29:40 -04:00
|
|
|
#if (BT_CONTROLLER_INCLUDED == TRUE)
|
2017-12-07 08:48:27 -05:00
|
|
|
#include "esp_bt.h"
|
2023-08-31 21:29:40 -04:00
|
|
|
#endif
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "osi/future.h"
|
|
|
|
#include "osi/allocator.h"
|
2023-08-29 04:49:49 -04:00
|
|
|
#include "config/stack_config.h"
|
2024-04-08 08:16:28 -04:00
|
|
|
#include "hci_log/bt_hci_log.h"
|
|
|
|
#include "bt_common.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
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
|
2017-06-13 05:14:50 -04:00
|
|
|
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
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
|
2017-06-13 05:14:50 -04:00
|
|
|
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)
|
2023-08-29 04:49:49 -04:00
|
|
|
{
|
|
|
|
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)
|
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
|
|
|
|
2023-08-29 04:49:49 -04:00
|
|
|
if (!cfg) {
|
|
|
|
LOG_ERROR("%s cfg is NULL", __func__);
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2023-08-31 21:29:40 -04:00
|
|
|
#if (BT_CONTROLLER_INCLUDED == TRUE)
|
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;
|
|
|
|
}
|
2023-08-31 21:29:40 -04:00
|
|
|
#endif
|
2017-02-20 12:05:37 -05:00
|
|
|
|
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
|
|
|
|
|
2023-08-29 04:49:49 -04:00
|
|
|
ret = bluedriod_config_init(cfg);
|
|
|
|
if (ret != BT_STATUS_SUCCESS) {
|
|
|
|
LOG_ERROR("Bluedroid stack initialize fail, ret:%d", ret);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
/*
|
2023-08-29 04:49:49 -04:00
|
|
|
* BTC Init
|
|
|
|
*/
|
2020-05-23 03:11:20 -04:00
|
|
|
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
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, 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
|
|
|
|
2024-04-08 08:16:28 -04:00
|
|
|
#if (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
bt_hci_log_init();
|
|
|
|
#endif // (BT_HCI_LOG_INCLUDED == 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_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
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
|
2017-06-13 05:14:50 -04:00
|
|
|
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
|
|
|
|
2023-08-29 04:49:49 -04:00
|
|
|
bluedriod_config_deinit();
|
|
|
|
|
2024-04-08 08:16:28 -04:00
|
|
|
#if (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
bt_hci_log_deinit();
|
|
|
|
#endif // (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
|
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
|
|
|
}
|