-#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;
}
#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__ */
+#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;
+ }
+}
+
#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"
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 },
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);
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;
}
if (copy_func) {
copy_func(&lmsg, lmsg.arg, arg);
}
+ } else {
+ lmsg.arg = NULL;
}
return btc_task_post(&lmsg);
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 */
+++ /dev/null
-#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__ */
--- /dev/null
+#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__ */
#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
} 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,
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__ */
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)
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;
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();
}