2016-11-16 08:26:02 +00: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-16 03:28:41 +00:00
|
|
|
|
|
|
|
|
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"
|
2017-12-07 13:48:27 +00:00
|
|
|
#include "esp_bt.h"
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "osi/future.h"
|
|
|
|
#include "osi/allocator.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
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
|
|
|
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
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
|
|
|
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)
|
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-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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
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) {
|
2017-03-03 10:42:27 +00:00
|
|
|
LOG_ERROR("Bluedroid 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
|
|
|
btc_init();
|
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
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
|
|
|
LOG_ERROR("Bluedroid 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 initialise failed\n");
|
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
|
|
|
|
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
|
|
|
|
|
|
|
if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|