From: Tian Hao Date: Sun, 6 Nov 2016 18:16:00 +0000 (+0800) Subject: component/bt : blufi use api X-Git-Tag: v1.0~2^2~53 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=118984283aec37fbdf16f28fe3bdbe881377c5f4;p=esp-idf component/bt : blufi use api 1. use api 2. bluetooth init/deinit/enable/disable wrap api(1st version) 3. bluetooth deinit(free resource) still some problem --- diff --git a/components/bt/bluedroid/api/esp_bt_common.c b/components/bt/bluedroid/api/esp_bt_common.c new file mode 100644 index 0000000000..868b6a7a99 --- /dev/null +++ b/components/bt/bluedroid/api/esp_bt_common.c @@ -0,0 +1,30 @@ +#include "esp_bt_defs.h" +#include "esp_bt_common.h" +#include "esp_err.h" + +extern int bte_main_boot_entry(void *cb); +extern int bte_main_shutdown(void); + +// tBTA_STATUS BTA_EnableBluetooth(tBTA_DM_SEC_CBACK *p_cback) +esp_err_t esp_enable_bluetooth(esp_bt_sec_cb_t *p_cback) +{ + return BTA_EnableBluetooth(p_cback) == BTA_SUCCESS ? ESP_OK : ESP_FAIL; +} + +esp_err_t esp_disable_bluetooth(void) +{ + return BTA_DisableBluetooth() == BTA_SUCCESS ? ESP_OK : ESP_FAIL; +} + +esp_err_t esp_init_bluetooth(bluetooth_init_cb_t cb) +{ + return bte_main_boot_entry(cb) == 0 ? ESP_OK : ESP_FAIL; +} + + +void esp_deinit_bluetooth(void) +{ + bte_main_shutdown(); +} + + diff --git a/components/bt/bluedroid/api/include/esp_bt_common.h b/components/bt/bluedroid/api/include/esp_bt_common.h index ea9404ff1d..4886b12bb0 100644 --- a/components/bt/bluedroid/api/include/esp_bt_common.h +++ b/components/bt/bluedroid/api/include/esp_bt_common.h @@ -4,10 +4,81 @@ #include #include "bt_types.h" - +#include "bta_api.h" +#include "esp_err.h" typedef tBT_UUID esp_bt_uuid_t; /* tBT_UUID in "bt_types.h" */ typedef BD_ADDR esp_bd_addr_t; /* BD_ADDR in bt_types.h */ +typedef tBTA_DM_SEC_CBACK esp_bt_sec_cb_t; + +typedef void (*bluetooth_init_cb_t)(void); + +/******************************************************************************* +** +** @function esp_enable_bluetooth +** +** @brief This function is called to enable bluetooth host. This +** function must be called before any other functions in the +** API (except esp_bluetooth_init) are called. +** +** @param[in] p_cback: +** security call back function +** @param[out] None +** +** @return ESP_OK - Success; Other - Failed +** +*******************************************************************************/ +esp_err_t esp_enable_bluetooth(esp_bt_sec_cb_t p_cback); + + +/******************************************************************************* +** +** @function esp_disable_bluetooth +** +** @brief This function is called to disable bluetooth host +** +** @param[in] None +** +** @param[out] None +** +** @return ESP_OK - Success; Other - Failed +** +*******************************************************************************/ +esp_err_t esp_disable_bluetooth(void); + +/******************************************************************************* +** +** @function esp_init_bluetooth +** +** @brief This function is called to init bluetooth host and alloc the +** resource. This function must be called before all othor API +** are called. +** +** @param[in] cb: When this function called success, the callback will be called +** +** @param[out] None +** +** @return ESP_OK - Success; Other - Failed +** +*******************************************************************************/ +esp_err_t esp_init_bluetooth(bluetooth_init_cb_t cb); + +/******************************************************************************* +** +** @function esp_deinit_bluetooth +** +** @brief This function is called to deinit bluetooth host and free the +** resource. +** +** @param[in] None +** +** @param[out] None +** +** @return ESP_OK - Success; Other - Failed +** +*******************************************************************************/ +void esp_deinit_bluetooth(void); + #endif /* __ESP_BT_COMMON_H__ */ diff --git a/components/bt/bluedroid/main/bte_main.c b/components/bt/bluedroid/main/bte_main.c index 0ad3cee605..bbe509a50a 100755 --- a/components/bt/bluedroid/main/bte_main.c +++ b/components/bt/bluedroid/main/bte_main.c @@ -89,10 +89,12 @@ ** Static variables *******************************************************************************/ static const hci_t *hci; -static void bte_main_enable(void); + /******************************************************************************* ** Static functions *******************************************************************************/ +static void bte_main_disable(void); +static void bte_main_enable(void); /******************************************************************************* ** Externs @@ -111,19 +113,23 @@ bluedroid_init_done_cb_t bluedroid_init_done_cb; ** Returns None ** ******************************************************************************/ -void bte_main_boot_entry(bluedroid_init_done_cb_t cb) +int bte_main_boot_entry(bluedroid_init_done_cb_t cb) { - if (gki_init()) + if (gki_init()) { LOG_ERROR("%s: Init GKI Module Failure.\n", __func__); + return -1; + } hci = hci_layer_get_interface(); - if (!hci) + if (!hci) { LOG_ERROR("%s could not get hci layer interface.\n", __func__); + return -2; + } btu_hci_msg_queue = fixed_queue_new(SIZE_MAX); if (btu_hci_msg_queue == NULL) { LOG_ERROR("%s unable to allocate hci message queue.\n", __func__); - return; + return -3; } bluedroid_init_done_cb = cb; @@ -140,6 +146,8 @@ void bte_main_boot_entry(bluedroid_init_done_cb_t cb) //Enbale HCI bte_main_enable(); + + return 0; } /****************************************************************************** @@ -151,7 +159,7 @@ void bte_main_boot_entry(bluedroid_init_done_cb_t cb) ** Returns None ** ******************************************************************************/ -void bte_main_shutdown() +void bte_main_shutdown(void) { //data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL); hci->set_data_queue(NULL); @@ -167,6 +175,8 @@ void bte_main_shutdown() */ gki_clean_up(); + + bte_main_disable(); } /****************************************************************************** @@ -202,7 +212,7 @@ static void bte_main_enable(void) ** Returns None ** ******************************************************************************/ -void bte_main_disable(void) +static void bte_main_disable(void) { /* APPL_TRACE_DEBUG("%s", __FUNCTION__); diff --git a/components/bt/bluedroid/profiles/esp/blufi/blufi_adv.c b/components/bt/bluedroid/profiles/esp/blufi/blufi_adv.c index ee95d89b53..2944177f53 100644 --- a/components/bt/bluedroid/profiles/esp/blufi/blufi_adv.c +++ b/components/bt/bluedroid/profiles/esp/blufi/blufi_adv.c @@ -13,7 +13,7 @@ // limitations under the License. #include "blufi_adv.h" - +#include "esp_adv_api.h" /******************************************************************************* ** @@ -29,75 +29,10 @@ ** Returns None ** *******************************************************************************/ - void BlufiBleConfigadvData(tBLUFI_BLE_ADV_DATA *adv_data, - tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback) + void BlufiBleConfigadvData(esp_ble_adv_data_cfg_t *adv_data, + esp_ble_set_adv_data_cmpl_cb_t *p_adv_data_cback) { - tBTA_BLE_AD_MASK data_mask = 0; - if(adv_data->adv_name != NULL) - { - data_mask |= BTM_BLE_AD_BIT_DEV_NAME; - BTA_DmSetDeviceName(adv_data->adv_name); - } - if(adv_data->ble_adv_data.int_range.low != 0 || - adv_data->ble_adv_data.int_range.hi != 0) - data_mask |= BTM_BLE_AD_BIT_INT_RANGE; - - if(adv_data->ble_adv_data.p_manu != NULL) - { - data_mask |= BTM_BLE_AD_BIT_MANU; - } - - if(adv_data->ble_adv_data.p_services != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE; - } - - if(adv_data->ble_adv_data.p_service_32b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_32; - } - - if(adv_data->ble_adv_data.p_services_128b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_128; - } - - if(adv_data->ble_adv_data.p_sol_services != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_SOL; - } - - if(adv_data->ble_adv_data.p_sol_service_32b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_32SOL; - } - - if(adv_data->ble_adv_data.p_sol_service_128b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL; - } - - if(adv_data->ble_adv_data.p_service_data != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_DATA; - } - - if(adv_data->ble_adv_data.appearance != 0) - { - data_mask |= BTM_BLE_AD_BIT_APPEARANCE; - } - - if(adv_data->ble_adv_data.p_proprietary != NULL) - { - data_mask |= BTM_BLE_AD_BIT_PROPRIETARY; - } - - if(adv_data->ble_adv_data.tx_power != 0) - { - data_mask |= BTM_BLE_AD_BIT_TX_PWR; - } - - BTA_DmBleSetAdvConfig(data_mask, &(adv_data->ble_adv_data), p_adv_data_cback); + esp_ble_config_adv_data(adv_data, p_adv_data_cback); } @@ -112,75 +47,10 @@ ** Returns None ** *******************************************************************************/ -void BlufiBleSetScanRsp(tBLUFI_BLE_ADV_DATA *scan_rsp_data, - tBTA_SET_ADV_DATA_CMPL_CBACK *p_scan_rsp_data_cback) +void BlufiBleSetScanRsp(esp_ble_adv_data_cfg_t *scan_rsp_data, + esp_ble_set_adv_data_cmpl_cb_t *p_scan_rsp_data_cback) { - tBTA_BLE_AD_MASK data_mask = 0; - if(scan_rsp_data->adv_name != NULL) - { - data_mask |= BTM_BLE_AD_BIT_DEV_NAME; - BTA_DmSetDeviceName(scan_rsp_data->adv_name); - } - if(scan_rsp_data->ble_adv_data.int_range.low != 0 || - scan_rsp_data->ble_adv_data.int_range.hi != 0) - data_mask |= BTM_BLE_AD_BIT_INT_RANGE; - - if(scan_rsp_data->ble_adv_data.p_manu != NULL) - { - data_mask |= BTM_BLE_AD_BIT_MANU; - } - - if(scan_rsp_data->ble_adv_data.p_services != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE; - } - - if(scan_rsp_data->ble_adv_data.p_service_32b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_32; - } - - if(scan_rsp_data->ble_adv_data.p_services_128b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_128; - } - - if(scan_rsp_data->ble_adv_data.p_sol_services != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_SOL; - } - - if(scan_rsp_data->ble_adv_data.p_sol_service_32b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_32SOL; - } - - if(scan_rsp_data->ble_adv_data.p_sol_service_128b != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL; - } - - if(scan_rsp_data->ble_adv_data.p_service_data != NULL) - { - data_mask |= BTM_BLE_AD_BIT_SERVICE_DATA; - } - - if(scan_rsp_data->ble_adv_data.appearance != 0) - { - data_mask |= BTM_BLE_AD_BIT_APPEARANCE; - } - - if(scan_rsp_data->ble_adv_data.p_proprietary != NULL) - { - data_mask |= BTM_BLE_AD_BIT_PROPRIETARY; - } - - if(scan_rsp_data->ble_adv_data.tx_power != 0) - { - data_mask |= BTM_BLE_AD_BIT_TX_PWR; - } - - BTA_DmBleSetScanRsp(data_mask, &(scan_rsp_data->ble_adv_data), p_scan_rsp_data_cback); + esp_ble_set_scan_rsp(scan_rsp_data, p_scan_rsp_data_cback); } diff --git a/components/bt/bluedroid/profiles/esp/blufi/blufi_prf.c b/components/bt/bluedroid/profiles/esp/blufi/blufi_prf.c index 70237a6e74..7b1fb9c505 100644 --- a/components/bt/bluedroid/profiles/esp/blufi/blufi_prf.c +++ b/components/bt/bluedroid/profiles/esp/blufi/blufi_prf.c @@ -30,6 +30,10 @@ #include "blufi_prf.h" #include "blufi_adv.h" +#include "esp_adv_api.h" +#include "esp_bt_defs.h" +#include "esp_gatt_api.h" + #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] @@ -41,13 +45,13 @@ UINT8 esp32_manu[17] = {0xff,0x20,0x14,0x07,0x22,0x00,0x02,0x5B,0x00,0x33,0x49,0 tBTA_BLE_MANU p_esp32_manu = {sizeof(esp32_manu),esp32_manu}; /* manufacturer data */ tBTA_BLE_SERVICE esp32_service = { - 0x01, //only one service in the ijiazu button profile + 0x01, //only one service in the blufi profile false, &esp32_uuid }; /* 16 bits services */ -tBLUFI_BLE_ADV_DATA esp32_adv_data[ADV_SCAN_IDX_MAX] = +esp_ble_adv_data_cfg_t esp32_adv_data[ADV_SCAN_IDX_MAX] = { [BLE_ADV_DATA_IDX] = { .adv_name = "Espressif_008", @@ -87,7 +91,21 @@ tBLUFI_BLE_ADV_DATA esp32_adv_data[ADV_SCAN_IDX_MAX] = } }; +tBLE_BD_ADDR peer_bda = { + .type = API_PUBLIC_ADDR, + .bda = {0} +}; +esp_ble_adv_params_all_t adv_params = +{ + .adv_int_min = BTM_BLE_ADV_INT_MIN + 0x100, + .adv_int_max = BTM_BLE_ADV_INT_MIN + 0x100, + .adv_type = API_NON_DISCOVERABLE, + .addr_type_own = API_PUBLIC_ADDR, + .channel_map = ESP_BLE_ADV_CHNL_MAP, + .adv_filter_policy = ADV_ALLOW_SCAN_ANY_CON_ANY, + .p_dir_bda = &peer_bda +}; static tBLUFI_CB_ENV blufi_cb_env; @@ -96,7 +114,7 @@ static tBLUFI_CB_ENV blufi_cb_env; /***************************************************************************** ** Constants *****************************************************************************/ -static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data); +static void blufi_profile_cb(esp_gatts_evt_t event, esp_gatts_t *p_data); /******************************************************************************* @@ -108,24 +126,24 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data); ** Returns NULL ** *******************************************************************************/ -static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) +static void blufi_profile_cb(esp_gatts_evt_t event, esp_gatts_t *p_data) { - tBTA_GATTS_RSP rsp; - tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; + esp_gatts_rsp_t rsp; + esp_bt_uuid_t 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; + esp_gatt_status_t status; LOG_DEBUG("blufi profile cb event = %x\n",event); switch(event) { - case BTA_GATTS_REG_EVT: + case ESP_GATTS_REG_EVT: status = p_data->reg_oper.status; LOG_DEBUG("p_data->reg_oper.status = %x\n",p_data->reg_oper.status); LOG_DEBUG("(p_data->reg_oper.uuid.uu.uuid16=%x\n",p_data->reg_oper.uuid.uu.uuid16); - if(p_data->reg_oper.status != BTA_GATT_OK) { + if(p_data->reg_oper.status != ESP_GATT_OK) { LOG_ERROR("blufi profile register failed\n"); return; } @@ -140,14 +158,13 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) BlufiBleConfigadvData(&esp32_adv_data[BLE_ADV_DATA_IDX], NULL); //set the adversting data to the btm layer BlufiBleSetScanRsp(&esp32_adv_data[BLE_SCAN_RSP_DATA_IDX],NULL); - BTA_GATTS_Listen(blufi_cb_env.gatt_if, true, NULL); - + esp_ble_start_advertising(&adv_params); //create the blufi service to the service data base. if(p_data->reg_oper.uuid.uu.uuid16 == SVC_BLUFI_UUID) { blufi_create_service(); } break; - case BTA_GATTS_READ_EVT: + case ESP_GATTS_READ_EVT: memset(&rsp, 0, sizeof(tBTA_GATTS_API_RSP)); rsp.attr_value.handle = p_data->req_data.p_data->read_req.handle; rsp.attr_value.len = 2; @@ -155,11 +172,11 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) //rsp.attr_value.value[1] = 0xed; //rsp.attr_value.value[2] = 0xbe; //rsp.attr_value.value[3] = 0xef; - BTA_GATTS_SendRsp(p_data->req_data.conn_id,p_data->req_data.trans_id, + esp_ble_gatts_send_rsp(p_data->req_data.conn_id,p_data->req_data.trans_id, p_data->req_data.status,&rsp); break; - case BTA_GATTS_WRITE_EVT: - BTA_GATTS_SendRsp(p_data->req_data.conn_id,p_data->req_data.trans_id, + case ESP_GATTS_WRITE_EVT: + esp_ble_gatts_send_rsp(p_data->req_data.conn_id,p_data->req_data.trans_id, p_data->req_data.status,NULL); LOG_DEBUG("Received blufi data:"); @@ -175,36 +192,36 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) p_inst->blufi_cback(blufi_cb_env.blufi_inst.app_id, net_event, len, p_rec_data); } break; - case BTA_GATTS_CONF_EVT: + case ESP_GATTS_CFM_EVT: /* Nothing */ break; - case BTA_GATTS_CREATE_EVT: + case ESP_GATTS_CREATE_EVT: uuid.uu.uuid16 = CHAR_BLUFI_UUID; blufi_cb_env.clcb.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); + esp_ble_gatts_start_srvc(p_data->create.service_id); //add the frist blufi characteristic --> write characteristic - BTA_GATTS_AddCharacteristic(blufi_cb_env.clcb.cur_srvc_id, &uuid, + esp_ble_gatts_add_char(blufi_cb_env.clcb.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; - case BTA_GATTS_ADD_CHAR_EVT: + case ESP_GATTS_ADD_CHAR_EVT: if(p_data->add_result.char_uuid.uu.uuid16 == CHAR_BLUFI_UUID) { //save the att handle to the env 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, + esp_ble_gatts_add_char_descr(blufi_cb_env.clcb.cur_srvc_id, (GATT_PERM_WRITE|GATT_PERM_WRITE), &uuid); } break; - case BTA_GATTS_ADD_CHAR_DESCR_EVT: + case ESP_GATTS_ADD_CHAR_DESCR_EVT: /* Nothing */ break; - case BTA_GATTS_CONNECT_EVT: + case ESP_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", @@ -213,24 +230,23 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) /*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); - BTA_DmBleBroadcast(0); //stop adv + esp_ble_stop_advertising(); //stop adv break; - case BTA_GATTS_DISCONNECT_EVT: + case ESP_GATTS_DISCONNECT_EVT: //set the connection flag to true blufi_cb_env.clcb.connected = false; + esp_ble_start_advertising(&adv_params); break; - case BTA_GATTS_OPEN_EVT: + case ESP_GATTS_OPEN_EVT: break; - case BTA_GATTS_CLOSE_EVT: + case ESP_GATTS_CLOSE_EVT: if(blufi_cb_env.clcb.connected && (blufi_cb_env.clcb.conn_id == p_data->conn.conn_id)) { //set the connection channal congested flag to true blufi_cb_env.clcb.congest = p_data->congest.congested; } break; - case BTA_GATTS_LISTEN_EVT: - break; - case BTA_GATTS_CONGEST_EVT: + case ESP_GATTS_CONGEST_EVT: break; default: break; @@ -249,8 +265,8 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) *******************************************************************************/ void blufi_create_service(void) { - tBTA_GATTS_IF server_if ; - tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; + esp_gatts_if_t server_if ; + esp_bt_uuid_t uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; UINT16 num_handle = BLUFI_HDL_NUM; UINT8 inst = 0x00; server_if = blufi_cb_env.gatt_if; @@ -260,7 +276,7 @@ void blufi_create_service(void) LOG_ERROR("blufi service added error."); return; } - BTA_GATTS_CreateService(server_if, &uuid, inst, num_handle, true); + esp_ble_gatts_create_srvc(server_if, &uuid, inst, num_handle, true); } @@ -341,9 +357,9 @@ BOOLEAN blufi_env_clcb_dealloc(UINT16 conn_id) ** Description Initializa the GATT Service for blufi profiles. ** *******************************************************************************/ -tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back) +esp_gatt_status_t blufi_profile_init (tBLUFI_CBACK *call_back) { - tBT_UUID app_uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; + esp_bt_uuid_t app_uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; if(blufi_cb_env.enabled) @@ -363,8 +379,8 @@ tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back) } - /* register the blufi profile to the BTA_GATTS module*/ - BTA_GATTS_AppRegister(&app_uuid, blufi_profile_cb); + /* register the blufi profile to the ESP_GATTS module*/ + esp_ble_gatts_app_register(&app_uuid, blufi_profile_cb); return GATT_SUCCESS; } @@ -382,7 +398,7 @@ void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len) return; } - BTA_GATTS_HandleValueIndication (conn_id, attr_id, len, + esp_ble_gatts_hdl_val_indica(conn_id, attr_id, len, blufi_msg, rsp); } diff --git a/components/bt/bluedroid/profiles/esp/blufi/include/blufi_adv.h b/components/bt/bluedroid/profiles/esp/blufi/include/blufi_adv.h index a01b288c49..172586cdc2 100644 --- a/components/bt/bluedroid/profiles/esp/blufi/include/blufi_adv.h +++ b/components/bt/bluedroid/profiles/esp/blufi/include/blufi_adv.h @@ -5,16 +5,10 @@ #include "btm_ble_api.h" #include "esp_bt_defs.h" -typedef struct -{ - char *adv_name; //set the device name to be sent on the advertising - tBTA_BLE_ADV_DATA ble_adv_data; -}tBLUFI_BLE_ADV_DATA; +extern void BlufiBleConfigadvData(esp_ble_adv_data_cfg_t *adv_data, + esp_ble_set_adv_data_cmpl_cb_t *p_adv_data_cback); -extern void BlufiBleConfigadvData(tBLUFI_BLE_ADV_DATA *adv_data, - tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback); - -extern void BlufiBleSetScanRsp(tBLUFI_BLE_ADV_DATA *scan_rsp_data, - tBTA_SET_ADV_DATA_CMPL_CBACK *p_scan_rsp_data_cback); +extern void BlufiBleSetScanRsp(esp_ble_adv_data_cfg_t *scan_rsp_data, + esp_ble_set_adv_data_cmpl_cb_t *p_scan_rsp_data_cback); #endif /* __BLUFI_ADV_H__ */ diff --git a/examples/07_blufi/components/blufi/blufi.c b/examples/07_blufi/components/blufi/blufi.c index 304fcf80c0..057a5c4136 100644 --- a/examples/07_blufi/components/blufi/blufi.c +++ b/examples/07_blufi/components/blufi/blufi.c @@ -44,6 +44,8 @@ #include "blufi.h" #include "blufi_adv.h" +#include "esp_bt_common.h" + static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_data); struct dm_evt { @@ -93,7 +95,7 @@ static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_dat } -static bt_status_t blufi_dm_upstreams_evt(void *arg) +static esp_err_t blufi_dm_upstreams_evt(void *arg) { struct dm_evt *evt = (struct dm_evt *)arg; @@ -124,7 +126,7 @@ static bt_status_t blufi_dm_upstreams_evt(void *arg) GKI_freebuf(evt); - return BT_STATUS_SUCCESS; + return ESP_OK; } @@ -144,10 +146,27 @@ void blufi_bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data) blufi_transfer_context(blufi_dm_upstreams_evt, evt); } -bt_status_t blufi_enable(void *arg) +esp_err_t blufi_enable(void *arg) { + esp_err_t err; + BTM_SetTraceLevel(BT_TRACE_LEVEL_ERROR); - BTA_EnableBluetooth(blufi_bte_dm_evt); + err = esp_enable_bluetooth(blufi_bte_dm_evt); vTaskDelay(1000 / portTICK_PERIOD_MS); + + return err; +} + +esp_err_t blufi_disable(void *arg) +{ + esp_err_t err; + + err = esp_disable_bluetooth(); + + if (arg) { + ((void (*)(void))arg)(); + } + + return err; } diff --git a/examples/07_blufi/components/blufi/blufi_task.c b/examples/07_blufi/components/blufi/blufi_task.c index a1d0f0a2f5..f02a8143fd 100644 --- a/examples/07_blufi/components/blufi/blufi_task.c +++ b/examples/07_blufi/components/blufi/blufi_task.c @@ -60,7 +60,7 @@ static void blufi_task(void *arg) } } -static int blufi_task_post(uint32_t sig, void *par, void *cb, void *arg) +static esp_err_t blufi_task_post(uint32_t sig, void *par, void *cb, void *arg) { BtTaskEvt_t evt; @@ -71,13 +71,13 @@ static int blufi_task_post(uint32_t sig, void *par, void *cb, void *arg) if (xQueueSend(xBlufiTaskQueue, &evt, 10/portTICK_RATE_MS) != pdTRUE) { LOG_ERROR("Blufi Post failed\n"); - return -1; + return ESP_FAIL; } - return 0; + return ESP_OK; } -bt_status_t blufi_transfer_context(BtTaskCb_t cb, void *arg) +esp_err_t blufi_transfer_context(blufi_task_cb_t cb, void *arg) { LOG_DEBUG("%s cb %08x, arg %u\n", __func__, cb, arg); @@ -102,3 +102,7 @@ void blufi_init(void) { blufi_transfer_context(blufi_enable, NULL); } +void blufi_deinit(void) { + blufi_transfer_context(blufi_disable, blufi_task_deinit); +} + diff --git a/examples/07_blufi/components/blufi/include/blufi.h b/examples/07_blufi/components/blufi/include/blufi.h index 61a1c1c242..abccc144a4 100644 --- a/examples/07_blufi/components/blufi/include/blufi.h +++ b/examples/07_blufi/components/blufi/include/blufi.h @@ -4,6 +4,7 @@ #include #include "osi.h" #include "bt_common_types.h" +#include "esp_err.h" enum BLUFI_SIG { BLUFI_SIG_SWITCH_CONTEXT = 0, @@ -11,8 +12,14 @@ enum BLUFI_SIG { BLUFI_SIG_DISABLE, }; +typedef esp_err_t (*blufi_task_cb_t)(void *arg); + void blufi_init(void); -bt_status_t blufi_enable(void *arg); -bt_status_t blufi_transfer_context(BtTaskCb_t cb, void *arg); +void blufi_deinit(void); + +esp_err_t blufi_enable(void *arg); +esp_err_t blufi_disable(void *arg); + +esp_err_t blufi_transfer_context(blufi_task_cb_t cb, void *arg); #endif /* __BT_APP_COMMON_H__ */ diff --git a/examples/07_blufi/main/demo_main.c b/examples/07_blufi/main/demo_main.c index ac52c8ab34..3aeea617a9 100644 --- a/examples/07_blufi/main/demo_main.c +++ b/examples/07_blufi/main/demo_main.c @@ -26,10 +26,8 @@ #include "bt.h" #include "bta_api.h" -extern tBTA_STATUS BTA_DisableBluetooth(void); -extern void phy_set_wifi_mode_only(bool wifi_only); -extern void bte_main_boot_entry(void *); -extern void blufi_init(void); +#include "esp_bt_common.h" +#include "blufi.h" #define WIFI_LIST_NUM 10 @@ -71,7 +69,8 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) case SYSTEM_EVENT_STA_GOT_IP: xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); blufi_config_success(); - BTA_DisableBluetooth(); + esp_disable_bluetooth(); //close bluetooth function + //esp_deinit_bluetooth(); //free bluetooth resource break; case SYSTEM_EVENT_STA_DISCONNECTED: /* This is a workaround as ESP32 WiFi libs don't currently @@ -107,7 +106,6 @@ void wifiTestTask(void *pvParameters) vTaskDelay(1000 / portTICK_PERIOD_MS); if (confirm) { confirm = false; - //BTA_DisableBluetooth(); strcpy(sta_config.sta.ssid, tmp_ssid); strcpy(sta_config.sta.password, tmp_passwd); @@ -138,6 +136,6 @@ void app_main() bt_controller_init(); xTaskCreatePinnedToCore(&wifiTestTask, "wifiTestTask", 2048, NULL, 20, NULL, 0); - // bt_app_task_start_up(); - bte_main_boot_entry(blufi_init); + + esp_init_bluetooth(blufi_init); }