2021-12-06 06:59:55 +00:00
|
|
|
/*
|
2024-04-08 12:16:28 +00:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-12-06 06:59:55 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-11-16 03:28:41 +00:00
|
|
|
|
|
|
|
|
2023-09-01 01:29:40 +00:00
|
|
|
#include "common/bt_target.h"
|
2016-11-16 08:26:02 +00:00
|
|
|
#include "esp_bt_main.h"
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "btc/btc_task.h"
|
|
|
|
#include "btc/btc_main.h"
|
2023-09-01 01:29:40 +00:00
|
|
|
#if (BT_CONTROLLER_INCLUDED == TRUE)
|
2017-12-07 13:48:27 +00:00
|
|
|
#include "esp_bt.h"
|
2023-09-01 01:29:40 +00:00
|
|
|
#endif
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "osi/future.h"
|
|
|
|
#include "osi/allocator.h"
|
2023-08-29 08:49:49 +00:00
|
|
|
#include "config/stack_config.h"
|
2024-04-08 12:16:28 +00:00
|
|
|
#include "hci_log/bt_hci_log.h"
|
|
|
|
#include "bt_common.h"
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2017-03-03 10:42:27 +00:00
|
|
|
static bool bd_already_enable = false;
|
|
|
|
static bool bd_already_init = false;
|
2016-11-16 03:28:41 +00:00
|
|
|
|
2017-01-11 03:04:41 +00:00
|
|
|
esp_bluedroid_status_t esp_bluedroid_get_status(void)
|
|
|
|
{
|
2017-03-03 10:42:27 +00:00
|
|
|
if (bd_already_init) {
|
|
|
|
if (bd_already_enable) {
|
2016-12-23 16:28:47 +00:00
|
|
|
return ESP_BLUEDROID_STATUS_ENABLED;
|
|
|
|
} else {
|
|
|
|
return ESP_BLUEDROID_STATUS_INITIALIZED;
|
|
|
|
}
|
2017-01-11 03:04:41 +00:00
|
|
|
} else {
|
2016-12-23 16:28:47 +00:00
|
|
|
return ESP_BLUEDROID_STATUS_UNINITIALIZED;
|
2017-01-11 03:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 07:53:06 +00:00
|
|
|
esp_err_t esp_bluedroid_enable(void)
|
2016-11-16 03:28:41 +00:00
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
|
|
|
|
2017-03-03 10:42:27 +00: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 18:10:15 +00: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 10:42:27 +00:00
|
|
|
LOG_ERROR("Bluedroid enable failed\n");
|
2016-11-24 18:10:15 +00: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 09:14:50 +00:00
|
|
|
|
2022-08-09 06:16:30 +00:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
|
2017-06-13 09:14:50 +00:00
|
|
|
LOG_ERROR("Bluedroid enable failed\n");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-24 18:10:15 +00:00
|
|
|
|
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2017-03-03 10:42:27 +00:00
|
|
|
LOG_ERROR("Bluedroid enable failed\n");
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2017-03-03 10:42:27 +00:00
|
|
|
bd_already_enable = true;
|
2016-11-24 18:10:15 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
2016-11-16 03:28:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 07:53:06 +00:00
|
|
|
esp_err_t esp_bluedroid_disable(void)
|
2016-11-16 03:28:41 +00:00
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
|
|
|
|
2017-03-03 10:42:27 +00:00
|
|
|
if (!bd_already_enable) {
|
|
|
|
LOG_ERROR("Bluedroid already disabled\n");
|
2016-11-24 18:10:15 +00: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 10:42:27 +00:00
|
|
|
LOG_ERROR("Bluedroid disable failed\n");
|
2016-11-24 18:10:15 +00: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 09:14:50 +00:00
|
|
|
|
2022-08-09 06:16:30 +00:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
|
2017-06-13 09:14:50 +00:00
|
|
|
LOG_ERROR("Bluedroid disable failed\n");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-24 18:10:15 +00:00
|
|
|
|
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2017-03-03 10:42:27 +00:00
|
|
|
LOG_ERROR("Bluedroid disable failed\n");
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2017-03-03 10:42:27 +00:00
|
|
|
bd_already_enable = false;
|
2016-11-24 18:10:15 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
2016-11-16 03:28:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 07:53:06 +00:00
|
|
|
esp_err_t esp_bluedroid_init(void)
|
2023-08-29 08:49:49 +00: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-16 03:28:41 +00:00
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
2020-05-23 07:11:20 +00:00
|
|
|
bt_status_t ret;
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2023-08-29 08:49:49 +00:00
|
|
|
if (!cfg) {
|
|
|
|
LOG_ERROR("%s cfg is NULL", __func__);
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2023-09-01 01:29:40 +00:00
|
|
|
#if (BT_CONTROLLER_INCLUDED == TRUE)
|
2017-02-20 17:05:37 +00:00
|
|
|
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
|
2018-12-14 01:23:27 +00:00
|
|
|
LOG_ERROR("Controller not initialised\n");
|
2017-02-20 17:05:37 +00:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2023-09-01 01:29:40 +00:00
|
|
|
#endif
|
2017-02-20 17:05:37 +00:00
|
|
|
|
2017-03-03 10:42:27 +00:00
|
|
|
if (bd_already_init) {
|
|
|
|
LOG_ERROR("Bluedroid already initialised\n");
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_MEMORY_DEBUG
|
2017-06-13 12:52:26 +00:00
|
|
|
osi_mem_dbg_init();
|
|
|
|
#endif
|
|
|
|
|
2023-08-29 08:49:49 +00: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 07:40:01 +00:00
|
|
|
/*
|
2023-08-29 08:49:49 +00:00
|
|
|
* BTC Init
|
|
|
|
*/
|
2020-05-23 07:11:20 +00:00
|
|
|
ret = btc_init();
|
|
|
|
if (ret != BT_STATUS_SUCCESS) {
|
|
|
|
LOG_ERROR("Bluedroid Initialize Fail");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2018-11-06 07:34:38 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE);
|
|
|
|
*future_p = future_new();
|
|
|
|
if (*future_p == NULL) {
|
2020-05-23 07:11:20 +00:00
|
|
|
LOG_ERROR("Bluedroid Initialize Fail!");
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_MAIN_INIT;
|
|
|
|
msg.act = BTC_MAIN_ACT_INIT;
|
2017-06-13 09:14:50 +00:00
|
|
|
|
2022-08-09 06:16:30 +00:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
|
2020-05-23 07:11:20 +00:00
|
|
|
LOG_ERROR("Bluedroid Initialize Fail");
|
2017-06-13 09:14:50 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2020-05-23 07:11:20 +00:00
|
|
|
LOG_ERROR("Bluedroid Initialize Fail");
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2017-03-03 10:42:27 +00:00
|
|
|
bd_already_init = true;
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2024-04-08 12:16:28 +00:00
|
|
|
#if (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
bt_hci_log_init();
|
|
|
|
#endif // (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_OK;
|
2016-11-16 03:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-03 07:53:06 +00:00
|
|
|
esp_err_t esp_bluedroid_deinit(void)
|
2016-11-16 03:28:41 +00:00
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
btc_msg_t msg;
|
|
|
|
future_t **future_p;
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2017-03-03 10:42:27 +00: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 18:10:15 +00:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE);
|
|
|
|
*future_p = future_new();
|
|
|
|
if (*future_p == NULL) {
|
2017-03-03 10:42:27 +00:00
|
|
|
LOG_ERROR("Bluedroid de-initialise failed\n");
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_MAIN_INIT;
|
|
|
|
msg.act = BTC_MAIN_ACT_DEINIT;
|
2017-06-13 09:14:50 +00:00
|
|
|
|
2022-08-09 06:16:30 +00:00
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL, NULL) != BT_STATUS_SUCCESS) {
|
2017-06-13 09:14:50 +00:00
|
|
|
LOG_ERROR("Bluedroid de-initialise failed\n");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (future_await(*future_p) == FUTURE_FAIL) {
|
2017-03-03 10:42:27 +00:00
|
|
|
LOG_ERROR("Bluedroid de-initialise failed\n");
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
btc_deinit();
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2023-08-29 08:49:49 +00:00
|
|
|
bluedriod_config_deinit();
|
|
|
|
|
2024-04-08 12:16:28 +00:00
|
|
|
#if (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
bt_hci_log_deinit();
|
|
|
|
#endif // (BT_HCI_LOG_INCLUDED == TRUE)
|
|
|
|
|
2017-03-03 10:42:27 +00:00
|
|
|
bd_already_init = false;
|
2016-11-16 08:26:02 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_OK;
|
2016-11-16 03:28:41 +00:00
|
|
|
}
|