From 9691fe9daefe0242c7f899f0f26bb0b26d4f16db Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Wed, 16 Nov 2016 16:26:02 +0800 Subject: [PATCH] component/bt : init api 1. use future to redefine init api --- components/bt/bluedroid/api/esp_bt_main.c | 147 +++++++++++++++++- .../bt/bluedroid/api/include/esp_bt_main.h | 6 +- components/bt/bluedroid/btc/core/btc_main.c | 65 +++++++- components/bt/bluedroid/btc/core/btc_sec.c | 0 components/bt/bluedroid/btc/core/btc_task.c | 11 +- .../bt/bluedroid/btc/core/include/btc_main.h | 17 -- .../bt/bluedroid/btc/include/btc_main.h | 50 ++++++ .../bt/bluedroid/btc/include/btc_task.h | 7 +- examples/07_blufi/components/blufi/blufi.c | 62 +++----- examples/07_blufi/main/demo_main.c | 10 +- 10 files changed, 294 insertions(+), 81 deletions(-) create mode 100644 components/bt/bluedroid/btc/core/btc_sec.c delete mode 100644 components/bt/bluedroid/btc/core/include/btc_main.h create mode 100644 components/bt/bluedroid/btc/include/btc_main.h diff --git a/components/bt/bluedroid/api/esp_bt_main.c b/components/bt/bluedroid/api/esp_bt_main.c index 9e2fbe545b..3601b4c48c 100644 --- a/components/bt/bluedroid/api/esp_bt_main.c +++ b/components/bt/bluedroid/api/esp_bt_main.c @@ -1,26 +1,157 @@ -#include "esp_bt_main.h" +// 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. +#include "esp_bt_main.h" +#include "btc_task.h" +#include "btc_main.h" +#include "future.h" + +static bool esp_already_enable = false; +static bool esp_already_init = false; -esp_err_t esp_enable_bluetooth(esp_bt_sec_cb_t *p_cback) +esp_err_t esp_enable_bluetooth(void) { - return btc_enable_bluetooth(p_cback) == BT_STATUS_SUCCESS ? ESP_OK: ESP_FAIL; + btc_msg_t msg; + future_t **future_p; + + if (esp_already_enable) { + LOG_ERROR("%s already enable\n"); + return ESP_ERR_INVALID_STATE; + } + + future_p = btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE); + *future_p = future_new(); + if (*future_p == NULL) { + LOG_ERROR("%s failed\n"); + return ESP_ERR_NO_MEM; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_MAIN_INIT; + msg.act = BTC_MAIN_ACT_ENABLE; + btc_transfer_context(&msg, NULL, 0, NULL); + + if (future_await(*future_p) == FUTURE_FAIL) { + LOG_ERROR("%s failed\n"); + return ESP_FAIL; + } + + esp_already_enable = true; + + return ESP_OK; } esp_err_t esp_disable_bluetooth(void) { - return btc_disable_bluetooth() == BT_STATUS_SUCCESS ? ESP_OK: ESP_FAIL; + btc_msg_t msg; + future_t **future_p; + + if (!esp_already_enable) { + LOG_ERROR("%s already disable\n"); + return ESP_ERR_INVALID_STATE; + } + + future_p = btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE); + *future_p = future_new(); + if (*future_p == NULL) { + LOG_ERROR("%s failed\n"); + return ESP_ERR_NO_MEM; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_MAIN_INIT; + msg.act = BTC_MAIN_ACT_DISABLE; + btc_transfer_context(&msg, NULL, 0, NULL); + + if (future_await(*future_p) == FUTURE_FAIL) { + LOG_ERROR("%s failed\n"); + return ESP_FAIL; + } + + esp_already_enable = false; + + return ESP_OK; } -esp_err_t esp_init_bluetooth(bluetooth_init_cb_t cb) +esp_err_t esp_init_bluetooth(void) { - return btc_init_bluetooth(cb) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL; + btc_msg_t msg; + future_t **future_p; + + if (esp_already_init) { + LOG_ERROR("%s already init\n"); + return ESP_ERR_INVALID_STATE; + } + + future_p = btc_main_get_future_p(BTC_MAIN_INIT_FUTURE); + *future_p = future_new(); + if (*future_p == NULL) { + LOG_ERROR("%s failed\n"); + return ESP_ERR_NO_MEM; + } + + btc_init(); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_MAIN_INIT; + msg.act = BTC_MAIN_ACT_INIT; + btc_transfer_context(&msg, NULL, 0, NULL); + + if (future_await(*future_p) == FUTURE_FAIL) { + LOG_ERROR("%s failed\n"); + return ESP_FAIL; + } + + esp_already_init = true;; + + return ESP_OK; } -void esp_deinit_bluetooth(void) +esp_err_t esp_deinit_bluetooth(void) { - btc_deinit_bluetooth(); + btc_msg_t msg; + future_t **future_p; + + if (!esp_already_init) { + LOG_ERROR("%s already deinit\n"); + return ESP_ERR_INVALID_STATE; + } + + future_p = btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE); + *future_p = future_new(); + if (*future_p == NULL) { + LOG_ERROR("%s failed\n"); + return ESP_ERR_NO_MEM; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_MAIN_INIT; + msg.act = BTC_MAIN_ACT_DEINIT; + btc_transfer_context(&msg, NULL, 0, NULL); + + if (future_await(*future_p) == FUTURE_FAIL) { + LOG_ERROR("%s failed\n"); + return ESP_FAIL; + } + + btc_deinit(); + + esp_already_init = false; + + return ESP_OK; } diff --git a/components/bt/bluedroid/api/include/esp_bt_main.h b/components/bt/bluedroid/api/include/esp_bt_main.h index 247fbaa301..149ce3b3dc 100644 --- a/components/bt/bluedroid/api/include/esp_bt_main.h +++ b/components/bt/bluedroid/api/include/esp_bt_main.h @@ -4,13 +4,13 @@ #include "btc_main.h" #include "esp_err.h" -esp_err_t esp_enable_bluetooth(esp_bt_sec_cb_t *p_cback); +esp_err_t esp_enable_bluetooth(void); esp_err_t esp_disable_bluetooth(void); -esp_err_t esp_init_bluetooth(bluetooth_init_cb_t cb); +esp_err_t esp_init_bluetooth(void); -void esp_deinit_bluetooth(void); +esp_err_t esp_deinit_bluetooth(void); #endif /* __ESP_BT_MAIN_H__ */ diff --git a/components/bt/bluedroid/btc/core/btc_main.c b/components/bt/bluedroid/btc/core/btc_main.c index e2c6d57653..865aa099e2 100644 --- a/components/bt/bluedroid/btc/core/btc_main.c +++ b/components/bt/bluedroid/btc/core/btc_main.c @@ -1,26 +1,77 @@ +#include "btc_task.h" #include "btc_main.h" +#include "future.h" #include "esp_err.h" +static future_t *main_future[BTC_MAIN_FUTURE_NUM]; + extern int bte_main_boot_entry(void *cb); extern int bte_main_shutdown(void); -bt_status_t btc_enable_bluetooth(esp_bt_sec_cb_t *p_cback) +future_t **btc_main_get_future_p(btc_main_future_type_t type) +{ + return &main_future[type]; +} + +static void btc_sec_callback(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data) { - return BTA_EnableBluetooth(p_cback) == BTA_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + switch (event) { + case BTA_DM_ENABLE_EVT: + future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_SUCCESS); + break; + case BTA_DM_DISABLE_EVT: + future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_SUCCESS); + break; + } } -bt_status_t btc_disable_bluetooth(void) +static bt_status_t btc_enable_bluetooth(void) { - return BTA_DisableBluetooth() == BTA_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + BTA_EnableBluetooth(btc_sec_callback); } -bt_status_t btc_init_bluetooth(bluetooth_init_cb_t cb) +static bt_status_t btc_disable_bluetooth(void) { - return bte_main_boot_entry(cb) == 0 ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + BTA_DisableBluetooth(); } +void btc_init_callback(void) +{ + future_ready(*btc_main_get_future_p(BTC_MAIN_INIT_FUTURE), FUTURE_SUCCESS); +} -void btc_deinit_bluetooth(void) +static bt_status_t btc_init_bluetooth(void) +{ + bte_main_boot_entry(btc_init_callback); +} + + +static void btc_deinit_bluetooth(void) { bte_main_shutdown(); + future_ready(*btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE), FUTURE_SUCCESS); } + +void btc_main_call_handler(btc_msg_t *msg) +{ + LOG_ERROR("%s act %d\n", __func__, msg->act); + + switch (msg->act) { + case BTC_MAIN_ACT_INIT: + btc_init_bluetooth(); + break; + case BTC_MAIN_ACT_DEINIT: + btc_deinit_bluetooth(); + break; + case BTC_MAIN_ACT_ENABLE: + btc_enable_bluetooth(); + break; + case BTC_MAIN_ACT_DISABLE: + btc_disable_bluetooth(); + break; + default: + LOG_ERROR("%s UNKNOWN ACT %d\n", __func__, msg->act); + break; + } +} + diff --git a/components/bt/bluedroid/btc/core/btc_sec.c b/components/bt/bluedroid/btc/core/btc_sec.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index b5d21d471e..cdc982bf7e 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -19,6 +19,7 @@ #include "thread.h" #include "gki.h" #include "bt_defs.h" +#include "btc_main.h" #include "btc_gatts.h" #include "btc_gattc.h" #include "btc_gap_ble.h" @@ -27,6 +28,7 @@ static xTaskHandle xBtcTaskHandle = NULL; static xQueueHandle xBtcQueue = 0; static btc_func_t profile_tab[BTC_PID_NUM] = { + [BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL }, [BTC_PID_GATTS] = {btc_gatts_call_handler, btc_gatts_cb_handler }, [BTC_PID_GATTC] = {btc_gattc_call_handler, btc_gattc_cb_handler }, [BTC_PID_GAP_BLE] = {btc_gap_ble_call_handler, btc_gap_ble_cb_handler }, @@ -51,6 +53,7 @@ static void btc_task(void *arg) for (;;) { if (pdTRUE == xQueueReceive(xBtcQueue, &msg, (portTickType)portMAX_DELAY)) { + LOG_DEBUG("%s msg %u %u %u %08x\n", __func__, msg.sig, msg.pid, msg.act, msg.arg); switch (msg.sig) { case BTC_SIG_API_CALL: profile_tab[msg.pid].btc_call(&msg); @@ -74,7 +77,7 @@ static bt_status_t btc_task_post(btc_msg_t *msg) return BT_STATUS_PARM_INVALID; } - if (xQueueSend(xBtcQueue, &msg, 10/portTICK_RATE_MS) != pdTRUE) { + if (xQueueSend(xBtcQueue, msg, 10/portTICK_RATE_MS) != pdTRUE) { LOG_ERROR("Btc Post failed\n"); return BT_STATUS_BUSY; } @@ -102,6 +105,8 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg if (copy_func) { copy_func(&lmsg, lmsg.arg, arg); } + } else { + lmsg.arg = NULL; } return btc_task_post(&lmsg); @@ -110,8 +115,8 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg int btc_init(void) { - xBtcQueue = xQueueCreate(60, sizeof(btc_msg_t)); - xTaskCreate(btc_task, "Bt_prf", 4096, NULL, configMAX_PRIORITIES - 1, &xBtcTaskHandle); + xBtcQueue = xQueueCreate(BTC_TASK_QUEUE_NUM, sizeof(btc_msg_t)); + xTaskCreate(btc_task, "Btc_task", BTC_TASK_STACK_SIZE, NULL, BTC_TASK_PRIO, &xBtcTaskHandle); /* TODO: initial the profile_tab */ diff --git a/components/bt/bluedroid/btc/core/include/btc_main.h b/components/bt/bluedroid/btc/core/include/btc_main.h deleted file mode 100644 index d684e775a0..0000000000 --- a/components/bt/bluedroid/btc/core/include/btc_main.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __BTC_BT_COMMON_H__ -#define __BTC_BT_COMMON_H__ - -#include "bt_types.h" -#include "bta_api.h" - -typedef tBTA_DM_SEC_CBACK esp_bt_sec_cb_t; - -typedef void (*bluetooth_init_cb_t)(void); - - -bt_status_t btc_enable_bluetooth(esp_bt_sec_cb_t *p_cback); -bt_status_t btc_disable_bluetooth(void); -bt_status_t btc_init_bluetooth(bluetooth_init_cb_t cb); -void btc_deinit_bluetooth(void); - -#endif /* __BTC_BT_COMMON_H__ */ diff --git a/components/bt/bluedroid/btc/include/btc_main.h b/components/bt/bluedroid/btc/include/btc_main.h new file mode 100644 index 0000000000..6652d0cca0 --- /dev/null +++ b/components/bt/bluedroid/btc/include/btc_main.h @@ -0,0 +1,50 @@ +#ifndef __BTC_BT_MAIN_H__ +#define __BTC_BT_MAIN_H__ + +#include "future.h" +#include "bt_types.h" +#include "bta_api.h" +#include "btc_main.h" +#include "btc_task.h" + +typedef enum { + BTC_MAIN_ACT_INIT = 0, + BTC_MAIN_ACT_DEINIT, + BTC_MAIN_ACT_ENABLE, + BTC_MAIN_ACT_DISABLE, +} btc_main_act_t; + +typedef enum { + BTC_MAIN_INIT_FUTURE = 0, + BTC_MAIN_DEINIT_FUTURE, + BTC_MAIN_ENABLE_FUTURE, + BTC_MAIN_DISABLE_FUTURE, + BTC_MAIN_FUTURE_NUM, +} btc_main_future_type_t; + +future_t **btc_main_get_future_p(btc_main_future_type_t type); + +#if 0 +typedef union { + struct btc_main_init_args { + future_t *future; + } init; + struct btc_main_deinit_args { + future_t *future; + } deinit; + struct btc_main_init_args { + future_t *future; + } enable; + struct btc_main_init_args { + future_t *future; + } disable; +} btc_main_args_t; + +bt_status_t btc_enable_bluetooth(future_t *future); +void btc_disable_bluetooth(future_t *future); +bt_status_t btc_init_bluetooth(future_t *future); +void btc_deinit_bluetooth(future_t *future); +#endif + +void btc_main_call_handler(btc_msg_t *msg); +#endif /* __BTC_BT_MAIN_H__ */ diff --git a/components/bt/bluedroid/btc/include/btc_task.h b/components/bt/bluedroid/btc/include/btc_task.h index 5be6761f47..fdfa8b4057 100644 --- a/components/bt/bluedroid/btc/include/btc_task.h +++ b/components/bt/bluedroid/btc/include/btc_task.h @@ -7,6 +7,7 @@ #define BTC_TASK_QUEUE_NUM 20 #define BTC_TASK_STACK_SIZE 4096 #define BTC_TASK_NAME "btcT" +#define BTC_TASK_PRIO (configMAX_PRIORITIES - 5) typedef struct btc_msg { uint8_t sig; //event signal @@ -23,7 +24,8 @@ typedef enum { } btc_sig_t; //btc message type typedef enum { - BTC_PID_GATTS = 0, + BTC_PID_MAIN_INIT = 0, + BTC_PID_GATTS, BTC_PID_GATTC, BTC_PID_GAP_BLE, BTC_PID_GAP_BT, @@ -45,4 +47,7 @@ typedef void (* btc_arg_deep_copy_t)(btc_msg_t *msg, void *dst, void *src); bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func); +int btc_init(void); +void btc_deinit(void); + #endif /* __BTC_TASK_H__ */ diff --git a/examples/07_blufi/components/blufi/blufi.c b/examples/07_blufi/components/blufi/blufi.c index 2ebe8bccda..414d12803c 100644 --- a/examples/07_blufi/components/blufi/blufi.c +++ b/examples/07_blufi/components/blufi/blufi.c @@ -94,53 +94,30 @@ static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_dat static esp_err_t blufi_dm_upstreams_evt(void *arg) { - struct dm_evt *evt = (struct dm_evt *)arg; - - tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)evt->p_data; - switch (evt->event) { - case BTA_DM_ENABLE_EVT: { - /*set connectable,discoverable, pairable and paired only modes of local device*/ - tBTA_DM_DISC disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE; - tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE; - BTA_DmSetVisibility(disc_mode, conn_mode, (uint8_t)BTA_DM_NON_PAIRABLE, (uint8_t)BTA_DM_CONN_ALL ); - + /*set connectable,discoverable, pairable and paired only modes of local device*/ + tBTA_DM_DISC disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE; + tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE; + BTA_DmSetVisibility(disc_mode, conn_mode, (uint8_t)BTA_DM_NON_PAIRABLE, (uint8_t)BTA_DM_CONN_ALL ); + #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) - /* Enable local privacy */ - //BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED); - do { - const controller_t *controller = controller_get_interface(); - char bdstr[18]; - bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr)); - LOG_DEBUG("BDA is: %s\n", bdstr); - } while (0); + /* Enable local privacy */ + //BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED); + do { + const controller_t *controller = controller_get_interface(); + char bdstr[18]; + bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr)); + LOG_DEBUG("BDA is: %s\n", bdstr); + } while (0); #endif - blufi_profile_init(BlufiDataCallBack); - break; - default: - break; - } - } - - GKI_freebuf(evt); + blufi_profile_init(BlufiDataCallBack); return ESP_OK; } -void blufi_bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data) +void blufi_bte_dm_evt(void) { - struct dm_evt *evt; - - LOG_DEBUG("%s: %d\n", __func__, (uint16_t)event); - - evt = (struct dm_evt *)GKI_getbuf(sizeof(struct dm_evt)); - if (evt == NULL) - return; - - evt->event = event; - evt->p_data = p_data; - - blufi_transfer_context(blufi_dm_upstreams_evt, evt); + blufi_transfer_context(blufi_dm_upstreams_evt, NULL); } esp_err_t blufi_enable(void *arg) @@ -149,7 +126,12 @@ esp_err_t blufi_enable(void *arg) BTM_SetTraceLevel(BT_TRACE_LEVEL_ERROR); - err = esp_enable_bluetooth(blufi_bte_dm_evt); + err = esp_enable_bluetooth(); + if (err) { + LOG_ERROR("%s failed\n", __func__); + return err; + } + blufi_bte_dm_evt(); vTaskDelay(1000 / portTICK_PERIOD_MS); return err; diff --git a/examples/07_blufi/main/demo_main.c b/examples/07_blufi/main/demo_main.c index 6f49ac5ca7..b399e973c3 100644 --- a/examples/07_blufi/main/demo_main.c +++ b/examples/07_blufi/main/demo_main.c @@ -133,10 +133,16 @@ void app_main() system_init(); initialise_wifi(); - vTaskDelay(3000 / portTICK_PERIOD_MS); + //vTaskDelay(3000 / portTICK_PERIOD_MS); bt_controller_init(); xTaskCreatePinnedToCore(&wifiTestTask, "wifiTestTask", 2048, NULL, 20, NULL, 0); - esp_init_bluetooth(blufi_init); + LOG_ERROR("%s init bluetooth\n", __func__); + ret = esp_init_bluetooth(); + if (ret) { + LOG_ERROR("%s init bluetooth failed\n", __func__); + return; + } + blufi_init(); } -- 2.40.0