From aafaebefb43a473ad1329ccddee29e61b45793ba Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Wed, 16 Nov 2016 22:11:59 +0800 Subject: [PATCH] component/bt : move blufi to new architecture btc task 1. move blufi to new architecture 2. delete some log trace --- components/bt/bluedroid/api/esp_blufi_api.c | 66 +++++ .../bt/bluedroid/api/include/esp_blufi_api.h | 97 +++++++ .../bt/bluedroid/bta/gatt/bta_gatts_act.c | 4 +- .../bt/bluedroid/bta/gatt/bta_gatts_main.c | 1 - components/bt/bluedroid/btc/core/btc_main.c | 2 +- components/bt/bluedroid/btc/core/btc_task.c | 3 +- .../btc/profile/esp/blufi/blufi_prf.c | 260 +++++++++--------- .../btc/profile/esp/blufi/include/blufi_int.h | 40 +++ .../btc/profile/esp/include/blufi_prf.h | 76 ----- .../btc/profile/esp/include/btc_blufi_prf.h | 38 +++ .../bt/bluedroid/stack/btm/btm_ble_gap.c | 4 +- examples/07_blufi/components/blufi/blufi.c | 89 +++--- .../07_blufi/components/blufi/blufi_task.c | 4 +- examples/07_blufi/main/demo_main.c | 9 +- 14 files changed, 415 insertions(+), 278 deletions(-) create mode 100644 components/bt/bluedroid/api/esp_blufi_api.c create mode 100644 components/bt/bluedroid/api/include/esp_blufi_api.h create mode 100644 components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h delete mode 100644 components/bt/bluedroid/btc/profile/esp/include/blufi_prf.h create mode 100644 components/bt/bluedroid/btc/profile/esp/include/btc_blufi_prf.h diff --git a/components/bt/bluedroid/api/esp_blufi_api.c b/components/bt/bluedroid/api/esp_blufi_api.c new file mode 100644 index 0000000000..e00ab290aa --- /dev/null +++ b/components/bt/bluedroid/api/esp_blufi_api.c @@ -0,0 +1,66 @@ +// 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_blufi_api.h" +#include "esp_bt_defs.h" +#include "esp_bt_main.h" +#include "btc_task.h" +#include "btc_blufi_prf.h" +#include "btc_manage.h" +#include "btc_main.h" +#include "future.h" + +esp_err_t esp_blufi_register_callback(esp_profile_cb_t callback) +{ + return (btc_profile_cb_set(BTC_PID_BLUFI, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_blufi_send_config_state(esp_blufi_config_state_t state) +{ + btc_msg_t msg; + btc_blufi_args_t arg; + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_BLUFI; + msg.act = BTC_BLUFI_ACT_SEND_CFG_STATE; + arg.cfg_state.state = state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + + +esp_err_t esp_blufi_profile_init(void) +{ + btc_msg_t msg; + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_BLUFI; + msg.act = BTC_BLUFI_ACT_INIT; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_blufi_profile_deinit(void) +{ + btc_msg_t msg; + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_BLUFI; + msg.act = BTC_BLUFI_ACT_DEINIT; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + + diff --git a/components/bt/bluedroid/api/include/esp_blufi_api.h b/components/bt/bluedroid/api/include/esp_blufi_api.h new file mode 100644 index 0000000000..cfebca96ae --- /dev/null +++ b/components/bt/bluedroid/api/include/esp_blufi_api.h @@ -0,0 +1,97 @@ +#ifndef __ESP_BLUFI_API_H__ +#define __ESP_BLUFI_API_H__ + +#include "bt_types.h" +#include "esp_bt_defs.h" +#include "esp_gatt_defs.h" +#include "bta_gatt_api.h" +#include "esp_err.h" + +#define ESP_BLUFI_RECV_DATA_LEN_MAX 128 + +#define ESP_BLUFI_EVENT_INIT_FINISH 0 +#define ESP_BLUFI_EVENT_DEINIT_FINISH 1 +#define ESP_BLUFI_EVENT_RECV_DATA 2 + +typedef enum { + ESP_BLUFI_CONFIG_OK = 0, + ESP_BLUFI_CONFIG_FAILED, +} esp_blufi_config_state_t; + +typedef enum { + ESP_BLUFI_INIT_OK = 0, + ESP_BLUFI_INIT_FAILED = 0, +} esp_blufi_init_state_t; + +typedef enum { + ESP_BLUFI_DEINIT_OK = 0, + ESP_BLUFI_DEINIT_FAILED = 0, +} esp_blufi_deinit_state_t; + +typedef union { + //ESP_BLUFI_EVENT_INIT_FINISH + struct blufi_init_finish_evt_param { + esp_blufi_init_state_t state; + } init_finish; + //ESP_BLUFI_EVENT_DEINIT_FINISH + struct blufi_deinit_finish_evt_param { + esp_blufi_deinit_state_t state; + } deinit_finish; + //ESP_BLUFI_EVENT_RECV_DATA + struct blufi_recv_evt_param { + uint8_t data[ESP_BLUFI_RECV_DATA_LEN_MAX]; + uint8_t data_len; + } recv_data; +} esp_blufi_cb_param_t; + +/******************************************************************************* +** +** @function esp_blufi_register_callback +** +** @brief This function is called to receive blufi callback event +** +** @param[in] callback: callback function +** +** @return ESP_OK - success, other - failed +** +*******************************************************************************/ +esp_err_t esp_blufi_register_callback(esp_profile_cb_t callback); + +/******************************************************************************* +** +** @function esp_blufi_send_config_state +** +** @brief This function is called to send config state to phone +** +** @param[in] state: blufi config ok or not +** +** @return ESP_OK - success, other - failed +** +*******************************************************************************/ +esp_err_t esp_blufi_send_config_state(esp_blufi_config_state_t state); + +/******************************************************************************* +** +** @function esp_blufi_profile_init +** +** @brief This function is called to init blufi_profile +** +** @return ESP_OK - success, other - failed +** +*******************************************************************************/ +esp_err_t esp_blufi_profile_init(void); + +/******************************************************************************* +** +** @function esp_blufi_profile_deinit +** +** @brief This function is called to init blufi_profile +** +** @return ESP_OK - success, other - failed +** +*******************************************************************************/ +esp_err_t esp_blufi_profile_deinit(void); + + + +#endif /* _ESP_BLUFI_API_ */ diff --git a/components/bt/bluedroid/bta/gatt/bta_gatts_act.c b/components/bt/bluedroid/bta/gatt/bta_gatts_act.c index 6e1e46fa9e..5ff5062739 100755 --- a/components/bt/bluedroid/bta/gatt/bta_gatts_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gatts_act.c @@ -222,7 +222,7 @@ void bta_gatts_register(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg) // btla-specific -- if (first_unuse != 0xff) { - APPL_TRACE_ERROR("register application first_unuse rcb_idx = %d", first_unuse); + APPL_TRACE_VERBOSE("register application first_unuse rcb_idx = %d", first_unuse); p_cb->rcb[first_unuse].in_use = TRUE; p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback; @@ -353,7 +353,7 @@ void bta_gatts_create_srvc(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg) rcb_idx = bta_gatts_find_app_rcb_idx_by_app_if(p_cb, p_msg->api_create_svc.server_if); - APPL_TRACE_ERROR("create service rcb_idx = %d", rcb_idx); + APPL_TRACE_DEBUG("create service rcb_idx = %d", rcb_idx); if (rcb_idx != BTA_GATTS_INVALID_APP) { diff --git a/components/bt/bluedroid/bta/gatt/bta_gatts_main.c b/components/bt/bluedroid/bta/gatt/bta_gatts_main.c index 716c7c94ea..c682a85394 100755 --- a/components/bt/bluedroid/bta/gatt/bta_gatts_main.c +++ b/components/bt/bluedroid/bta/gatt/bta_gatts_main.c @@ -72,7 +72,6 @@ BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg) break; case BTA_GATTS_API_REG_EVT: - LOG_ERROR("bta_gatts_register\n"); bta_gatts_register(p_cb, (tBTA_GATTS_DATA *) p_msg); break; diff --git a/components/bt/bluedroid/btc/core/btc_main.c b/components/bt/bluedroid/btc/core/btc_main.c index 865aa099e2..7b349890c6 100644 --- a/components/bt/bluedroid/btc/core/btc_main.c +++ b/components/bt/bluedroid/btc/core/btc_main.c @@ -54,7 +54,7 @@ static void btc_deinit_bluetooth(void) void btc_main_call_handler(btc_msg_t *msg) { - LOG_ERROR("%s act %d\n", __func__, msg->act); + LOG_DEBUG("%s act %d\n", __func__, msg->act); switch (msg->act) { case BTC_MAIN_ACT_INIT: diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index cdc982bf7e..64129d2724 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -23,6 +23,7 @@ #include "btc_gatts.h" #include "btc_gattc.h" #include "btc_gap_ble.h" +#include "btc_blufi_prf.h" static xTaskHandle xBtcTaskHandle = NULL; static xQueueHandle xBtcQueue = 0; @@ -38,7 +39,7 @@ static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_BT_HID] = {NULL, NULL}, [BTC_PID_SPP] = {NULL, NULL}, [BTC_PID_SPPLIKE] = {NULL, NULL}, - [BTC_PID_BLUFI] = {NULL, NULL}, + [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler }, }; /***************************************************************************** diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c index 70237a6e74..b039208d69 100644 --- a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c +++ b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c @@ -27,14 +27,21 @@ #include "bta_gatt_api.h" #include "bta_gatts_int.h" -#include "blufi_prf.h" +#include "btc_blufi_prf.h" +#include "btc_task.h" +#include "btc_manage.h" + #include "blufi_adv.h" +#include "blufi_int.h" + +static uint8_t *success_msg = "BLUFI_CONFIG_OK"; +static uint8_t *failed_msg = "BLUFI_CONFIG_FAILED"; + +#define BTC_BLUFI_CB_TO_APP(_event, _param) ((esp_profile_cb_t)btc_profile_cb_get(BTC_PID_BLUFI))(_event, _param) #define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x" #define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] -void blufi_config_success(void); -void blufi_config_failed(void); UINT16 esp32_uuid = SVC_BLUFI_UUID; UINT8 esp32_manu[17] = {0xff,0x20,0x14,0x07,0x22,0x00,0x02,0x5B,0x00,0x33,0x49,0x31,0x30,0x4a,0x30,0x30,0x31}; @@ -99,6 +106,33 @@ static tBLUFI_CB_ENV blufi_cb_env; static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data); +/******************************************************************************* +** +** Function blufi_create_service +** +** Description Create a Service for the blufi profile +** +** Returns NULL +** +*******************************************************************************/ +static void blufi_create_service(void) +{ + tBTA_GATTS_IF server_if ; + tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; + UINT16 num_handle = BLUFI_HDL_NUM; + UINT8 inst = 0x00; + server_if = blufi_cb_env.gatt_if; + blufi_cb_env.inst_id = inst; + if(!blufi_cb_env.enabled) + { + LOG_ERROR("blufi service added error."); + return; + } + BTA_GATTS_CreateService(server_if, &uuid, inst, num_handle, true); + +} + + /******************************************************************************* ** ** Function blufi_profile_cb @@ -112,8 +146,6 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) { tBTA_GATTS_RSP rsp; tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; - tBLUFI_INST *p_inst = &blufi_cb_env.blufi_inst; - UINT8 net_event = 0xff; UINT8 len = 0; UINT8 *p_rec_data = NULL; tBTA_GATT_STATUS status; @@ -169,10 +201,20 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) LOG_DEBUG("\n"); if (p_data->req_data.p_data->write_req.handle == blufi_cb_env.blufi_inst.blufi_hdl) { + btc_msg_t msg; + struct blufi_recv_evt_param recv_data; + + memset(&recv_data, 0x00, sizeof(struct blufi_recv_evt_param)); p_rec_data = &p_data->req_data.p_data->write_req.value[0]; - if (p_inst && p_inst->blufi_cback) - p_inst->blufi_cback(blufi_cb_env.blufi_inst.app_id, net_event, len, p_rec_data); + recv_data.data_len = p_data->req_data.p_data->write_req.len; + memcpy(recv_data.data, p_rec_data, recv_data.data_len); + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = BTC_BLUFI_CB_ACT_RECV_DATA; + + btc_transfer_context(&msg, &recv_data, sizeof(struct blufi_recv_evt_param), NULL); } break; case BTA_GATTS_CONF_EVT: @@ -180,12 +222,12 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) break; case BTA_GATTS_CREATE_EVT: uuid.uu.uuid16 = CHAR_BLUFI_UUID; - blufi_cb_env.clcb.cur_srvc_id = p_data->create.service_id; + blufi_cb_env.cur_srvc_id = p_data->create.service_id; blufi_cb_env.is_primery = p_data->create.is_primary; //start the blufi service after created BTA_GATTS_StartService(p_data->create.service_id, BTA_GATT_TRANSPORT_LE); //add the frist blufi characteristic --> write characteristic - BTA_GATTS_AddCharacteristic(blufi_cb_env.clcb.cur_srvc_id, &uuid, + BTA_GATTS_AddCharacteristic(blufi_cb_env.cur_srvc_id, &uuid, (GATT_PERM_WRITE | GATT_PERM_READ), (GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE | GATT_CHAR_PROP_BIT_NOTIFY)); break; @@ -196,20 +238,29 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) blufi_cb_env.blufi_inst.blufi_hdl = p_data->add_result.attr_id; uuid.uu.uuid16 = GATT_UUID_CHAR_CLIENT_CONFIG; - BTA_GATTS_AddCharDescriptor (blufi_cb_env.clcb.cur_srvc_id, + BTA_GATTS_AddCharDescriptor (blufi_cb_env.cur_srvc_id, (GATT_PERM_WRITE|GATT_PERM_WRITE), &uuid); } break; - case BTA_GATTS_ADD_CHAR_DESCR_EVT: - /* Nothing */ + case BTA_GATTS_ADD_CHAR_DESCR_EVT: { + /* call init finish */ + btc_msg_t msg; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = BTC_BLUFI_CB_ACT_INIT_FINISH; + btc_transfer_context(&msg, NULL, 0, NULL); break; + } case BTA_GATTS_CONNECT_EVT: //set the connection flag to true - blufi_env_clcb_alloc(p_data->conn.conn_id, p_data->conn.remote_bda); - LOG_DEBUG("\ndevice is connected "BT_BD_ADDR_STR", server_if=%d,reason=0x%x,connect_id=%d\n", + LOG_ERROR("\ndevice is connected "BT_BD_ADDR_STR", server_if=%d,reason=0x%x,connect_id=%d\n", BT_BD_ADDR_HEX(p_data->conn.remote_bda), p_data->conn.server_if, p_data->conn.reason, p_data->conn.conn_id); + + blufi_cb_env.conn_id = p_data->conn.conn_id; + /*return whether the remote device is currently connected*/ int is_connected = BTA_DmGetConnectionState(p_data->conn.remote_bda); LOG_DEBUG("is_connected=%d\n",is_connected); @@ -217,15 +268,15 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) break; case BTA_GATTS_DISCONNECT_EVT: //set the connection flag to true - blufi_cb_env.clcb.connected = false; + blufi_cb_env.connected = false; break; case BTA_GATTS_OPEN_EVT: break; case BTA_GATTS_CLOSE_EVT: - if(blufi_cb_env.clcb.connected && (blufi_cb_env.clcb.conn_id == p_data->conn.conn_id)) + if(blufi_cb_env.connected && (blufi_cb_env.conn_id == p_data->conn.conn_id)) { //set the connection channal congested flag to true - blufi_cb_env.clcb.congest = p_data->congest.congested; + blufi_cb_env.congest = p_data->congest.congested; } break; case BTA_GATTS_LISTEN_EVT: @@ -237,111 +288,7 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) } } - -/******************************************************************************* -** -** Function blufi_create_service -** -** Description Create a Service for the blufi profile -** -** Returns NULL -** -*******************************************************************************/ -void blufi_create_service(void) -{ - tBTA_GATTS_IF server_if ; - tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; - UINT16 num_handle = BLUFI_HDL_NUM; - UINT8 inst = 0x00; - server_if = blufi_cb_env.gatt_if; - blufi_cb_env.inst_id = inst; - if(!blufi_cb_env.enabled) - { - LOG_ERROR("blufi service added error."); - return; - } - BTA_GATTS_CreateService(server_if, &uuid, inst, num_handle, true); - -} - -/******************************************************************************* -** -** Function blufi_env_clcb_alloc -** -** Description The function allocates a GATT profile connection link control block -** -** Returns NULL if not found. Otherwise pointer to the connection link block. -** -*******************************************************************************/ -tBLUFI_CLCB *blufi_env_clcb_alloc (UINT16 conn_id, BD_ADDR remote_bda) -{ - tBLUFI_CLCB *p_clcb = NULL; - p_clcb = &blufi_cb_env.clcb; - - if(!p_clcb->in_use) - { - p_clcb->in_use = TRUE; - p_clcb->conn_id = conn_id; - p_clcb->connected = TRUE; - memcpy(p_clcb->remote_bda, remote_bda, BD_ADDR_LEN); - LOG_ERROR("p_clcb->conn_id = %x\n", conn_id); - } - - return p_clcb; -} - -/******************************************************************************* -** -** Function blufi_env_find_conn_id_by_bd_adddr -** -** Description The function searches all LCB with macthing bd address -** -** Returns total number of clcb found. -** -*******************************************************************************/ -/* -UINT16 blufi_env_find_conn_id_by_bd_adddr(BD_ADDR remote_bda) -{ - UINT8 i_clcb; - tBLUFI_CLCB *p_clcb = NULL; - - for(i_clcb = 0, p_clcb = &blufi_cb_env.clcb; i_clcb < BLUFIT_MAX_APPS; i_clcb++, p_clcb++) - { - if(p_clcb->in_use && p_clcb->connected && memcmp(p_clcb->remote_bda, remote_bda, BD_ADDR_LEN) == 0) - { - return p_clcb->conn_id; - } - } - - return GATT_INVALID_CONN_ID; -} -*/ -/******************************************************************************* -** -** Function blufi_env_clcb_dealloc -** -** Description The function deallocates a GATT profile connection link control block -** -** Returns True the deallocation is successful -** -*******************************************************************************/ -BOOLEAN blufi_env_clcb_dealloc(UINT16 conn_id) -{ -// UINT8 i_clcb = 0; -// tBLUFI_CLCB *p_clcb = NULL; - - return FALSE; -} - - -/******************************************************************************* -** -** Function blufi_init -** -** Description Initializa the GATT Service for blufi profiles. -** -*******************************************************************************/ -tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back) +static tGATT_STATUS btc_blufi_profile_init(void) { tBT_UUID app_uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; @@ -356,12 +303,6 @@ tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back) memset(&blufi_cb_env,0,sizeof(tBLUFI_CB_ENV)); } - - if(call_back != NULL) - { - blufi_cb_env.blufi_inst.blufi_cback = call_back; - } - /* register the blufi profile to the BTA_GATTS module*/ BTA_GATTS_AppRegister(&app_uuid, blufi_profile_cb); @@ -369,14 +310,14 @@ tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back) return GATT_SUCCESS; } -void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len) +static void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len) { - BOOLEAN conn_status = blufi_cb_env.clcb.connected; - UINT16 conn_id = blufi_cb_env.clcb.conn_id; + BOOLEAN conn_status = blufi_cb_env.connected; + UINT16 conn_id = blufi_cb_env.conn_id; UINT16 attr_id = blufi_cb_env.blufi_inst.blufi_hdl; //notify rsp==false; indicate rsp==true. BOOLEAN rsp = false; - if(!conn_status && blufi_cb_env.clcb.congest) + if(!conn_status && blufi_cb_env.congest) { LOG_ERROR("the conneciton for blufi profile has been loss"); return; @@ -386,14 +327,61 @@ void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len) blufi_msg, rsp); } -void blufi_config_success(void) +static void btc_blufi_config_success(void) { - uint8_t *success_msg = "BLUFI_CONFIG_OK"; + LOG_DEBUG("config success\n"); blufi_msg_notify(success_msg, strlen(success_msg)); } -void blufi_config_failed(void) +static void btc_blufi_config_failed(void) { - uint8_t *failed_msg = "BLUFI_CONFIG_FAILED"; + LOG_DEBUG("config faield\n"); blufi_msg_notify(failed_msg, strlen(failed_msg)); } + +void btc_blufi_cb_handler(btc_msg_t *msg) +{ + esp_blufi_cb_param_t param; + + switch (msg->act) { + case BTC_BLUFI_CB_ACT_INIT_FINISH: + param.init_finish.state = ESP_BLUFI_INIT_OK; + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_INIT_FINISH, ¶m); + break; + case BTC_BLUFI_CB_ACT_DEINIT_FINISH: + /* TODO: but now nothing */ + break; + case BTC_BLUFI_CB_ACT_RECV_DATA: + memcpy(¶m.recv_data, msg->arg, sizeof(struct blufi_recv_evt_param)); + + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_DATA, ¶m); + break; + default: + LOG_ERROR("%s UNKNOWN %d\n", msg->act); + break; + } +} + +void btc_blufi_call_handler(btc_msg_t *msg) +{ + btc_blufi_args_t *arg = (btc_blufi_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLUFI_ACT_INIT: + btc_blufi_profile_init(); + break; + case BTC_BLUFI_ACT_DEINIT: + /* TODO: but now nothing */ + break; + case BTC_BLUFI_ACT_SEND_CFG_STATE: + if (arg->cfg_state.state == ESP_BLUFI_CONFIG_OK) { + btc_blufi_config_success(); + } else { + btc_blufi_config_failed(); + } + break; + default: + LOG_ERROR("%s UNKNOWN %d\n", msg->act); + break; + } +} diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h b/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h new file mode 100644 index 0000000000..80710bf7b5 --- /dev/null +++ b/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h @@ -0,0 +1,40 @@ +#ifndef __BLUFI_INT_H__ +#define __BLUFI_INT_H__ + +//define the blufi serivce uuid +#define SVC_BLUFI_UUID 0xFFFF +//define the blufi Char uuid +#define CHAR_BLUFI_UUID 0xFF01 + +#define BLUFI_HDL_NUM 4 + +#define BLUFI_VAL_MAX_LEN (128) + +#define BLUFI_MAX_STRING_DATA 128 + + +typedef struct +{ + UINT8 app_id; + UINT16 blufi_hdl; +}tBLUFI_INST; + + +/* service engine control block */ +typedef struct +{ + BOOLEAN enabled; + BOOLEAN is_primery; + UINT8 inst_id; + tGATT_IF gatt_if; + tBLUFI_INST blufi_inst; + BOOLEAN in_use; + BOOLEAN congest; + UINT16 conn_id; + BOOLEAN connected; + BD_ADDR remote_bda; + UINT32 trans_id; + UINT8 cur_srvc_id; +} tBLUFI_CB_ENV; + +#endif /* __BLUFI_INT_H__ */ diff --git a/components/bt/bluedroid/btc/profile/esp/include/blufi_prf.h b/components/bt/bluedroid/btc/profile/esp/include/blufi_prf.h deleted file mode 100644 index 9fb53e8768..0000000000 --- a/components/bt/bluedroid/btc/profile/esp/include/blufi_prf.h +++ /dev/null @@ -1,76 +0,0 @@ -#include "bt_target.h" -#include "gatt_api.h" -#include "gattdefs.h" - -//define the blufi serivce uuid -#define SVC_BLUFI_UUID 0xFFFF -//define the blufi Char uuid -#define CHAR_BLUFI_UUID 0xFF01 - -#define BLUFI_HDL_NUM 4 - -#define BLUFI_VAL_MAX_LEN (20) - -#define BLUFI_MAX_STRING_DATA 33 - -typedef void (tBLUFI_CBACK)(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *data); - -typedef enum -{ - RECV_ACT_PASSWD = 0, - RECV_ACT_SSID, - RECV_ACT_MAX -} RECV_ACTION; - -typedef struct -{ - BD_ADDR remote_bda; - BOOLEAN need_rsp; - UINT16 clt_cfg; -} tBLUFI_WRITE_DATA; - -typedef struct -{ - BOOLEAN in_use; - BOOLEAN congest; - UINT16 conn_id; - BOOLEAN connected; - BD_ADDR remote_bda; - UINT32 trans_id; - UINT8 cur_srvc_id; - -} tBLUFI_CLCB; - - -typedef struct -{ - UINT8 app_id; - UINT16 blufi_hdl; - tBLUFI_CBACK* blufi_cback; - -}tBLUFI_INST; - - -/* service engine control block */ -typedef struct -{ - BOOLEAN enabled; - BOOLEAN is_primery; - UINT8 inst_id; - tGATT_IF gatt_if; - tBLUFI_CLCB clcb; /* connection link*/ - tBLUFI_INST blufi_inst; -} tBLUFI_CB_ENV; - -void blufi_create_service(void); - -tBLUFI_CLCB *blufi_env_clcb_alloc(UINT16 conn_id, BD_ADDR bda); - -UINT16 blufi_env_find_conn_id_by_bd_adddr(BD_ADDR bda); - -BOOLEAN blufi_env_clcb_dealloc(UINT16 conn_id); - -tGATT_STATUS blufi_profile_init(tBLUFI_CBACK *call_back); - -void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len); - diff --git a/components/bt/bluedroid/btc/profile/esp/include/btc_blufi_prf.h b/components/bt/bluedroid/btc/profile/esp/include/btc_blufi_prf.h new file mode 100644 index 0000000000..050e91c4ff --- /dev/null +++ b/components/bt/bluedroid/btc/profile/esp/include/btc_blufi_prf.h @@ -0,0 +1,38 @@ +#ifndef __BTC_BLUFI_PRF_H__ +#define __BTC_BLUFI_PRF_H__ + +#include "bt_target.h" +#include "btc_task.h" +#include "esp_blufi_api.h" + +typedef enum { + BTC_BLUFI_ACT_INIT = 0, + BTC_BLUFI_ACT_DEINIT, + BTC_BLUFI_ACT_SEND_CFG_STATE, +} btc_blufi_act_t; + +typedef enum { + BTC_BLUFI_CB_ACT_INIT_FINISH = 0, + BTC_BLUFI_CB_ACT_DEINIT_FINISH, + BTC_BLUFI_CB_ACT_RECV_DATA, +} btc_blufi_cb_act_t; + +typedef union { +#if 0 + //BTC_BLUFI_ACT_INIT = 0, + struct blufi_init_param { + } init; + //BTC_BLUFI_ACT_DEINIT, + struct blufi_deinit_param { + } deinit; +#endif + //BTC_BLUFI_ACT_SEND_CFG_STATE, + struct blufi_send_cfg_state_pram { + esp_blufi_config_state_t state; + } cfg_state; +} btc_blufi_args_t; + +void btc_blufi_cb_handler(btc_msg_t *msg); +void btc_blufi_call_handler(btc_msg_t *msg); + +#endif /* __BTC_BLUFI_PRF_H__ */ diff --git a/components/bt/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/bluedroid/stack/btm/btm_ble_gap.c index f665998ace..fee4842219 100755 --- a/components/bt/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_gap.c @@ -1548,7 +1548,7 @@ UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst, cp_len = len - MIN_ADV_LENGTH; else cp_len = p_data->p_manu->len; - LOG_ERROR("cp_len = %d\n,p_data->p_manu->len=%d\n",cp_len,p_data->p_manu->len); + LOG_DEBUG("cp_len = %d\n,p_data->p_manu->len=%d\n",cp_len,p_data->p_manu->len); for(int i = 0; i < p_data->p_manu->len; i++) { LOG_DEBUG("p_data->p_manu->p_val[%d] = %x\n",i,p_data->p_manu->p_val[i]); @@ -1556,7 +1556,7 @@ UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst, *p++ = cp_len + 1; *p++ = BTM_BLE_AD_TYPE_MANU; ARRAY_TO_STREAM(p, p_data->p_manu->p_val, cp_len); - LOG_ERROR("p_addr = %p\n,p_data->p_manu->p_val = %p\n",p,p_data->p_manu->p_val); + LOG_DEBUG("p_addr = %p\n,p_data->p_manu->p_val = %p\n",p,p_data->p_manu->p_val); len -= (cp_len + MIN_ADV_LENGTH); data_mask &= ~BTM_BLE_AD_BIT_MANU; } diff --git a/examples/07_blufi/components/blufi/blufi.c b/examples/07_blufi/components/blufi/blufi.c index 414d12803c..1cc0220588 100644 --- a/examples/07_blufi/components/blufi/blufi.c +++ b/examples/07_blufi/components/blufi/blufi.c @@ -26,65 +26,42 @@ #include #include -#include "bta_api.h" -#include "bta_gatt_api.h" #include "controller.h" -#include "gatt_int.h" #include "bt_trace.h" -#include "btm_api.h" #include "bt_types.h" -#include "blufi_prf.h" - #include "blufi.h" -#include "blufi_adv.h" #include "esp_bt_defs.h" #include "esp_bt_main.h" +#include "esp_blufi_api.h" -static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_data); - -struct dm_evt { - tBTA_DM_SEC_EVT event; - tBTA_DM_SEC* p_data; -}; +extern void wifi_set_blue_config(char *ssid, char *passwd); #define HEADER_SSID "ssid" #define HEADER_PASSWD "passwd" #define HEADER_CONFIRM "confirm" -extern void wifi_set_blue_config(char *ssid, char *passwd); -static char tmp_ssid[33]; -static char tmp_passwd[33]; -static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_data) +static char tmp_ssid[32 + 1]; +static char tmp_passwd[64 + 1]; + +static void blufi_data_recv(uint8_t *data, int len) { char *p = NULL; - LOG_DEBUG("the data is:%s\n", p_data); -#if 0 - switch(event) - { - case RECEIVE_NET_PASSWD_EVT: - LOG_ERROR("Received the network passwork"); - break; - case RECEIVE_NET_SSD_EVT: - LOG_ERROR("Received the network SSID"); - break; - default: - break; - } -#endif - p = strstr(p_data, HEADER_SSID); + LOG_DEBUG("the data is:%s\n", data); + + p = strstr(data, HEADER_SSID); if (p) { LOG_ERROR("SSID: %s\n", p+strlen(HEADER_SSID)+1); strcpy(tmp_ssid, p+strlen(HEADER_SSID)+1); } - p = strstr(p_data, HEADER_PASSWD); + p = strstr(data, HEADER_PASSWD); if (p) { LOG_ERROR("PASSWORD: %s\n", p+strlen(HEADER_PASSWD)+1); strcpy(tmp_passwd, p+strlen(HEADER_PASSWD)+1); } - p = strstr(p_data, HEADER_CONFIRM); + p = strstr(data, HEADER_CONFIRM); if (p) { LOG_ERROR("CONFIRM\n"); wifi_set_blue_config(tmp_ssid, tmp_passwd); @@ -92,32 +69,42 @@ static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_dat } -static esp_err_t blufi_dm_upstreams_evt(void *arg) +static void blufi_callback(uint32_t event, void *param) +{ + /* actually, should post to blufi_task handle the procedure, + * now, as a demo, we do simplely */ + switch (event) { + case ESP_BLUFI_EVENT_INIT_FINISH: + LOG_ERROR("blufi init finish\n"); + break; + case ESP_BLUFI_EVENT_RECV_DATA: { + LOG_DEBUG("blufi recv data\n"); + esp_blufi_cb_param_t *blufi_param = (esp_blufi_cb_param_t *)param; + blufi_data_recv(blufi_param->recv_data.data, blufi_param->recv_data.data_len); + break; + } + default: + break; + } +} + +static esp_err_t blufi_startup_in_blufi_task(void *arg) { /*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); -#endif - blufi_profile_init(BlufiDataCallBack); + BTA_DmSetVisibility(disc_mode, conn_mode, (uint8_t)BTA_DM_NON_PAIRABLE, (uint8_t)BTA_DM_CONN_ALL); + + esp_blufi_register_callback(blufi_callback); + esp_blufi_profile_init(); return ESP_OK; } -void blufi_bte_dm_evt(void) +static void blufi_startup(void) { - blufi_transfer_context(blufi_dm_upstreams_evt, NULL); + blufi_transfer_context(blufi_startup_in_blufi_task, NULL); } esp_err_t blufi_enable(void *arg) @@ -131,7 +118,7 @@ esp_err_t blufi_enable(void *arg) LOG_ERROR("%s failed\n", __func__); return err; } - blufi_bte_dm_evt(); + blufi_startup(); vTaskDelay(1000 / portTICK_PERIOD_MS); return err; diff --git a/examples/07_blufi/components/blufi/blufi_task.c b/examples/07_blufi/components/blufi/blufi_task.c index f02a8143fd..e4d4875c0d 100644 --- a/examples/07_blufi/components/blufi/blufi_task.c +++ b/examples/07_blufi/components/blufi/blufi_task.c @@ -93,8 +93,8 @@ static void blufi_task_deinit(void) static void blufi_task_init(void) { - xBlufiTaskQueue = xQueueCreate(10, sizeof(BtTaskEvt_t)); - xTaskCreate(blufi_task, "BlUFI", 8192, NULL, configMAX_PRIORITIES - 3, xBlufiTaskHandle); + xBlufiTaskQueue = xQueueCreate(5, sizeof(BtTaskEvt_t)); + xTaskCreate(blufi_task, "BlUFI", 4096, NULL, configMAX_PRIORITIES - 3, xBlufiTaskHandle); } void blufi_init(void) { diff --git a/examples/07_blufi/main/demo_main.c b/examples/07_blufi/main/demo_main.c index b399e973c3..3445a9aa73 100644 --- a/examples/07_blufi/main/demo_main.c +++ b/examples/07_blufi/main/demo_main.c @@ -26,6 +26,7 @@ #include "bt.h" #include "bta_api.h" +#include "esp_blufi_api.h" #include "esp_bt_defs.h" #include "esp_bt_main.h" #include "blufi.h" @@ -42,7 +43,6 @@ const int CONNECTED_BIT = BIT0; -static wifi_scan_config_t scan_config; static wifi_config_t sta_config; static char tmp_ssid[33]; @@ -59,8 +59,6 @@ void wifi_set_blue_config(char *ssid, char *passwd) LOG_DEBUG("confirm true\n"); } -extern void blufi_config_failed(void); -extern void blufi_config_success(void); static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { @@ -69,7 +67,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) break; case SYSTEM_EVENT_STA_GOT_IP: xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - blufi_config_success(); + esp_blufi_send_config_state(ESP_BLUFI_CONFIG_OK); esp_disable_bluetooth(); //close bluetooth function //esp_deinit_bluetooth(); //free bluetooth resource break; @@ -98,7 +96,6 @@ static void initialise_wifi(void) } -static int loop = 0; void wifiTestTask(void *pvParameters) { esp_err_t ret; @@ -119,7 +116,7 @@ void wifiTestTask(void *pvParameters) ret = esp_wifi_connect(); if (ret != ESP_OK) { LOG_ERROR("esp_wifi connect failed\n"); - blufi_config_failed(); + esp_blufi_send_config_state(ESP_BLUFI_CONFIG_FAILED); } } } -- 2.40.0