diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 1c4e64aeba..52cabe65fd 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -20,10 +20,17 @@ if(CONFIG_BT_ENABLED) list(APPEND priv_include_dirs common/btc/include common/include) + list(APPEND include_dirs + common/api/include/api + common/btc/profile/esp/blufi/include + common/btc/profile/esp/include) list(APPEND srcs "common/btc/core/btc_alarm.c" + "common/api/esp_blufi_api.c" "common/btc/core/btc_manage.c" "common/btc/core/btc_task.c" + "common/btc/profile/esp/blufi/blufi_prf.c" + "common/btc/profile/esp/blufi/blufi_protocol.c" "common/osi/alarm.c" "common/osi/allocator.c" "common/osi/buffer.c" @@ -57,7 +64,6 @@ if(CONFIG_BT_ENABLED) host/bluedroid/external/sbc/decoder/include host/bluedroid/external/sbc/encoder/include host/bluedroid/external/sbc/plc/include - host/bluedroid/btc/profile/esp/blufi/include host/bluedroid/btc/profile/esp/include host/bluedroid/btc/profile/std/a2dp/include host/bluedroid/btc/profile/std/hid/include @@ -81,7 +87,6 @@ if(CONFIG_BT_ENABLED) list(APPEND srcs "host/bluedroid/api/esp_a2dp_api.c" "host/bluedroid/api/esp_avrc_api.c" - "host/bluedroid/api/esp_blufi_api.c" "host/bluedroid/api/esp_bt_device.c" "host/bluedroid/api/esp_bt_main.c" "host/bluedroid/api/esp_gap_ble_api.c" @@ -167,8 +172,6 @@ if(CONFIG_BT_ENABLED) "host/bluedroid/btc/core/btc_sm.c" "host/bluedroid/btc/core/btc_storage.c" "host/bluedroid/btc/core/btc_util.c" - "host/bluedroid/btc/profile/esp/blufi/blufi_prf.c" - "host/bluedroid/btc/profile/esp/blufi/blufi_protocol.c" "host/bluedroid/btc/profile/std/a2dp/bta_av_co.c" "host/bluedroid/btc/profile/std/a2dp/btc_a2dp.c" "host/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c" @@ -321,6 +324,7 @@ if(CONFIG_BT_ENABLED) "host/bluedroid/stack/smp/smp_main.c" "host/bluedroid/stack/smp/smp_utils.c") + list(APPEND srcs "common/btc/profile/esp/blufi/bluedroid_host/esp_blufi.c") if(CONFIG_BLE_MESH) list(APPEND srcs "esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c") endif() @@ -540,6 +544,8 @@ if(CONFIG_BT_ENABLED) "host/nimble/esp-hci/src/esp_nimble_hci.c" "host/nimble/port/src/esp_nimble_mem.c") + list(APPEND srcs + "common/btc/profile/esp/blufi/nimble_host/esp_blufi.c") if(CONFIG_BLE_MESH) list(APPEND srcs "esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c") endif() diff --git a/components/bt/host/bluedroid/api/esp_blufi_api.c b/components/bt/common/api/esp_blufi_api.c similarity index 71% rename from components/bt/host/bluedroid/api/esp_blufi_api.c rename to components/bt/common/api/esp_blufi_api.c index 6a915fa2e8..8a9ac5596b 100644 --- a/components/bt/host/bluedroid/api/esp_blufi_api.c +++ b/components/bt/common/api/esp_blufi_api.c @@ -14,22 +14,14 @@ #include "esp_blufi_api.h" -#include "esp_bt_defs.h" -#include "esp_bt_main.h" #include "btc/btc_task.h" #include "btc_blufi_prf.h" #include "btc/btc_manage.h" -#include "btc/btc_main.h" #include "osi/future.h" -#include "btc_gatts.h" -#include "btc_gatt_util.h" -#include "common/bt_target.h" #if (BLUFI_INCLUDED == TRUE) esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks) { - if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) { - return ESP_ERR_INVALID_STATE; - } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); if (callbacks == NULL) { return ESP_FAIL; @@ -44,9 +36,7 @@ esp_err_t esp_blufi_send_wifi_conn_report(wifi_mode_t opmode, esp_blufi_sta_conn btc_msg_t msg; btc_blufi_args_t arg; - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_BLUFI; @@ -64,9 +54,7 @@ esp_err_t esp_blufi_send_wifi_list(uint16_t apCount, esp_blufi_ap_record_t *list btc_msg_t msg; btc_blufi_args_t arg; - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_BLUFI; @@ -81,9 +69,7 @@ esp_err_t esp_blufi_profile_init(void) { btc_msg_t msg; - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_BLUFI; @@ -96,9 +82,7 @@ esp_err_t esp_blufi_profile_deinit(void) { btc_msg_t msg; - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_BLUFI; @@ -112,29 +96,12 @@ uint16_t esp_blufi_get_version(void) return btc_blufi_get_version(); } -esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id) -{ - btc_msg_t msg; - btc_ble_gatts_args_t arg; - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } - msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_GATTS; - msg.act = BTC_GATTS_ACT_CLOSE; - arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gatts_if, conn_id); - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) - == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); -} - esp_err_t esp_blufi_send_error_info(esp_blufi_error_state_t state) { btc_msg_t msg; btc_blufi_args_t arg; - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_BLUFI; @@ -151,9 +118,7 @@ esp_err_t esp_blufi_send_custom_data(uint8_t *data, uint32_t data_len) if(data == NULL || data_len == 0) { return ESP_ERR_INVALID_ARG; } - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_BLUFI; diff --git a/components/bt/host/bluedroid/api/include/api/esp_blufi_api.h b/components/bt/common/api/include/api/esp_blufi_api.h similarity index 97% rename from components/bt/host/bluedroid/api/include/api/esp_blufi_api.h rename to components/bt/common/api/include/api/esp_blufi_api.h index fbd8adb797..7c99f65820 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_blufi_api.h +++ b/components/bt/common/api/include/api/esp_blufi_api.h @@ -15,8 +15,6 @@ #ifndef __ESP_BLUFI_API_H__ #define __ESP_BLUFI_API_H__ -#include "esp_bt_defs.h" -#include "esp_gatt_defs.h" #include "esp_err.h" #include "esp_wifi_types.h" @@ -115,6 +113,11 @@ typedef struct { int8_t rssi; /**< signal strength of AP */ } esp_blufi_ap_record_t; +/// Bluetooth address length +#define ESP_BD_ADDR_LEN 6 +/// Bluetooth device address +typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN]; + /** * @brief BLUFI callback parameters union */ @@ -404,19 +407,6 @@ esp_err_t esp_blufi_send_wifi_list(uint16_t apCount, esp_blufi_ap_record_t *list */ uint16_t esp_blufi_get_version(void); -/** - * @brief Close a connection a remote device. - * - * @param[in] gatts_if: GATT server access interface - * @param[in] conn_id: connection ID to be closed. - * - * @return - * - ESP_OK : success - * - other : failed - * - */ -esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id); - /** * * @brief This function is called to send blufi error information diff --git a/components/bt/common/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c index e93d1f075e..9579446817 100644 --- a/components/bt/common/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -21,17 +21,17 @@ #include "osi/allocator.h" #include "btc/btc_alarm.h" +#include "btc/btc_manage.h" +#include "btc_blufi_prf.h" +#include "blufi_int.h" #ifdef CONFIG_BT_BLUEDROID_ENABLED #include "common/bt_target.h" #include "btc/btc_main.h" -#include "btc/btc_manage.h" #include "btc/btc_dev.h" #include "btc_gatts.h" #include "btc_gattc.h" #include "btc_gatt_common.h" #include "btc_gap_ble.h" -#include "btc_blufi_prf.h" -#include "blufi_int.h" #include "btc/btc_dm.h" #include "bta/bta_gatt_api.h" #if CLASSIC_BT_INCLUDED @@ -94,11 +94,11 @@ static const btc_func_t profile_tab[BTC_PID_NUM] = { #endif ///BLE_INCLUDED == TRUE [BTC_PID_BLE_HID] = {NULL, NULL}, [BTC_PID_SPPLIKE] = {NULL, NULL}, + [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler }, +#endif #if (BLUFI_INCLUDED == TRUE) [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler }, #endif ///BLUFI_INCLUDED == TRUE - [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler }, -#endif [BTC_PID_ALARM] = {btc_alarm_handler, NULL }, #ifdef CONFIG_BT_BLUEDROID_ENABLED #if CLASSIC_BT_INCLUDED diff --git a/components/bt/common/btc/profile/esp/blufi/bluedroid_host/esp_blufi.c b/components/bt/common/btc/profile/esp/blufi/bluedroid_host/esp_blufi.c new file mode 100644 index 0000000000..071046dce9 --- /dev/null +++ b/components/bt/common/btc/profile/esp/blufi/bluedroid_host/esp_blufi.c @@ -0,0 +1,418 @@ +#include +#include +#include +#include "osi/allocator.h" +#include "btc/btc_task.h" +#include "btc/btc_manage.h" +#include "blufi_int.h" +#include "btc_blufi_prf.h" +#include "esp_log.h" +#include "esp_blufi_api.h" + +#include "common/bt_target.h" +#include "common/bt_trace.h" +#include "stack/bt_types.h" +#include "stack/gatt_api.h" +#include "bta/bta_api.h" +#include "bta/bta_gatt_api.h" +#include "bta_gatts_int.h" +#include "btc_gatt_util.h" +#include "btc_gatts.h" + +#include "esp_bt_defs.h" +#include "esp_gap_ble_api.h" +#include "esp_gatt_common_api.h" +#include "esp_bt_main.h" +#include "esp_bt_device.h" +#include "esp_err.h" +#include "esp_blufi.h" + +#if (BLUFI_INCLUDED == TRUE) + +#if GATT_DYNAMIC_MEMORY == FALSE +tBLUFI_ENV blufi_env; +#else +tBLUFI_ENV *blufi_env_ptr; +#endif + +static uint8_t server_if; +static uint16_t conn_id; +static uint8_t blufi_service_uuid128[32] = { + /* LSB <--------------------------------------------------------------------------------> MSB */ + //first uuid, 16bit, [12],[13] is the value + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, +}; + +static esp_ble_adv_data_t blufi_adv_data = { + .set_scan_rsp = false, + .include_name = true, + .include_txpower = true, + .min_interval = 0x0006, //slave connection min interval, Time = min_interval * 1.25 msec + .max_interval = 0x0010, //slave connection max interval, Time = max_interval * 1.25 msec + .appearance = 0x00, + .manufacturer_len = 0, + .p_manufacturer_data = NULL, + .service_data_len = 0, + .p_service_data = NULL, + .service_uuid_len = 16, + .p_service_uuid = blufi_service_uuid128, + .flag = 0x6, +}; + +static esp_ble_adv_params_t blufi_adv_params = { + .adv_int_min = 0x100, + .adv_int_max = 0x100, + .adv_type = ADV_TYPE_IND, + .own_addr_type = BLE_ADDR_TYPE_PUBLIC, + //.peer_addr = + //.peer_addr_type = + .channel_map = ADV_CHNL_ALL, + .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, +}; + +void esp_blufi_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) +{ + switch (event) { + case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: + esp_ble_gap_start_advertising(&blufi_adv_params); + break; + default: + break; + } +} + +// static functions declare +static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data); + +void blufi_create_service(void) +{ + if (!blufi_env.enabled) { + BTC_TRACE_ERROR("blufi service added error."); + return; + } + + blufi_env.srvc_inst = 0x00; + BTA_GATTS_CreateService(blufi_env.gatt_if, &blufi_srvc_uuid, blufi_env.srvc_inst, BLUFI_HDL_NUM, true); +} + +uint8_t esp_blufi_init(void) +{ + + /* register the BLUFI profile to the BTA_GATTS module*/ + BTA_GATTS_AppRegister(&blufi_app_uuid, blufi_profile_cb); + return GATT_SUCCESS; +} + +static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) +{ + tBTA_GATTS_RSP rsp; + BLUFI_TRACE_DEBUG("blufi profile cb event = %x\n", event); + + switch (event) { + case BTA_GATTS_REG_EVT: + BLUFI_TRACE_DEBUG("REG: status %d, app_uuid %04x, gatt_if %d\n", p_data->reg_oper.status, p_data->reg_oper.uuid.uu.uuid16, p_data->reg_oper.server_if); + + if (p_data->reg_oper.status != BTA_GATT_OK) { + BLUFI_TRACE_ERROR("BLUFI profile register failed\n"); + return; + } + + blufi_env.gatt_if = p_data->reg_oper.server_if; + blufi_env.enabled = true; + + //create the blufi service to the service data base. + if (p_data->reg_oper.uuid.uu.uuid16 == BLUFI_APP_UUID) { + BLUFI_TRACE_DEBUG("%s %d\n", __func__, __LINE__); + blufi_create_service(); + } + break; + case BTA_GATTS_DEREG_EVT: { + esp_blufi_cb_param_t param; + btc_msg_t msg; + + BLUFI_TRACE_DEBUG("DEREG: status %d, gatt_if %d\n", p_data->reg_oper.status, p_data->reg_oper.server_if); + + if (p_data->reg_oper.status != BTA_GATT_OK) { + BLUFI_TRACE_ERROR("BLUFI profile unregister failed\n"); + return; + } + + blufi_env.enabled = false; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_DEINIT_FINISH; + param.deinit_finish.state = ESP_BLUFI_DEINIT_OK; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + + break; + } + case BTA_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 = 1; + rsp.attr_value.value[0] = 0x00; + BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, + p_data->req_data.status, &rsp); + break; + case BTA_GATTS_WRITE_EVT: { + if (p_data->req_data.p_data->write_req.is_prep) { + tBTA_GATT_STATUS status = GATT_SUCCESS; + + if (blufi_env.prepare_buf == NULL) { + blufi_env.prepare_buf = osi_malloc(BLUFI_PREPAIR_BUF_MAX_SIZE); + blufi_env.prepare_len = 0; + if (blufi_env.prepare_buf == NULL) { + BLUFI_TRACE_ERROR("Blufi prep no mem\n"); + status = GATT_NO_RESOURCES; + } + } else { + if (p_data->req_data.p_data->write_req.offset > BLUFI_PREPAIR_BUF_MAX_SIZE) { + status = GATT_INVALID_OFFSET; + } else if ((p_data->req_data.p_data->write_req.offset + p_data->req_data.p_data->write_req.len) > BLUFI_PREPAIR_BUF_MAX_SIZE) { + status = GATT_INVALID_ATTR_LEN; + } + } + + memset(&rsp, 0, sizeof(tGATTS_RSP)); + rsp.attr_value.handle = p_data->req_data.p_data->write_req.handle; + rsp.attr_value.len = p_data->req_data.p_data->write_req.len; + rsp.attr_value.offset = p_data->req_data.p_data->write_req.offset; + memcpy(rsp.attr_value.value, p_data->req_data.p_data->write_req.value, p_data->req_data.p_data->write_req.len); + + BLUFI_TRACE_DEBUG("prep write, len=%d, offset=%d\n", p_data->req_data.p_data->write_req.len, p_data->req_data.p_data->write_req.offset); + + BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, + status, &rsp); + + if (status != GATT_SUCCESS) { + if (blufi_env.prepare_buf) { + osi_free(blufi_env.prepare_buf); + blufi_env.prepare_buf = NULL; + blufi_env.prepare_len = 0; + } + BLUFI_TRACE_ERROR("write data error , error code 0x%x\n", status); + return; + } + memcpy(blufi_env.prepare_buf + p_data->req_data.p_data->write_req.offset, + p_data->req_data.p_data->write_req.value, + p_data->req_data.p_data->write_req.len); + blufi_env.prepare_len += p_data->req_data.p_data->write_req.len; + + return; + } else { + BLUFI_TRACE_DEBUG("norm write, len=%d, offset=%d\n", p_data->req_data.p_data->write_req.len, p_data->req_data.p_data->write_req.offset); + BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, + p_data->req_data.status, NULL); + } + + if (p_data->req_data.p_data->write_req.handle == blufi_env.handle_char_p2e) { + btc_blufi_recv_handler(&p_data->req_data.p_data->write_req.value[0], + p_data->req_data.p_data->write_req.len); + } + break; + } + case BTA_GATTS_EXEC_WRITE_EVT: + BLUFI_TRACE_DEBUG("exec write exec %d\n", p_data->req_data.p_data->exec_write); + + BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, + GATT_SUCCESS, NULL); + + if (blufi_env.prepare_buf && p_data->req_data.p_data->exec_write == GATT_PREP_WRITE_EXEC) { + btc_blufi_recv_handler(blufi_env.prepare_buf, blufi_env.prepare_len); + } + + if (blufi_env.prepare_buf) { + osi_free(blufi_env.prepare_buf); + blufi_env.prepare_buf = NULL; + blufi_env.prepare_len = 0; + } + + break; + case BTA_GATTS_MTU_EVT: + BLUFI_TRACE_DEBUG("MTU size %d\n", p_data->req_data.p_data->mtu); + blufi_env.frag_size = (p_data->req_data.p_data->mtu < BLUFI_MAX_DATA_LEN ? p_data->req_data.p_data->mtu : BLUFI_MAX_DATA_LEN) - BLUFI_MTU_RESERVED_SIZE; + break; + case BTA_GATTS_CONF_EVT: + BLUFI_TRACE_DEBUG("CONFIRM EVT\n"); + /* Nothing */ + break; + case BTA_GATTS_CREATE_EVT: + blufi_env.handle_srvc = p_data->create.service_id; + + //add the frist blufi characteristic --> write characteristic + BTA_GATTS_AddCharacteristic(blufi_env.handle_srvc, &blufi_char_uuid_p2e, + (GATT_PERM_WRITE), + (GATT_CHAR_PROP_BIT_WRITE), + NULL, NULL); + break; + case BTA_GATTS_ADD_CHAR_EVT: + switch (p_data->add_result.char_uuid.uu.uuid16) { + case BLUFI_CHAR_P2E_UUID: /* Phone to ESP32 */ + blufi_env.handle_char_p2e = p_data->add_result.attr_id; + + BTA_GATTS_AddCharacteristic(blufi_env.handle_srvc, &blufi_char_uuid_e2p, + (GATT_PERM_READ), + (GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_NOTIFY), + NULL, NULL); + break; + case BLUFI_CHAR_E2P_UUID: /* ESP32 to Phone */ + blufi_env.handle_char_e2p = p_data->add_result.attr_id; + + BTA_GATTS_AddCharDescriptor (blufi_env.handle_srvc, + (GATT_PERM_READ | GATT_PERM_WRITE), + &blufi_descr_uuid_e2p, + NULL, NULL); + break; + default: + break; + } + break; + case BTA_GATTS_ADD_CHAR_DESCR_EVT: { + /* call init finish */ + esp_blufi_cb_param_t param; + btc_msg_t msg; + + blufi_env.handle_descr_e2p = p_data->add_result.attr_id; + //start the blufi service after created + BTA_GATTS_StartService(blufi_env.handle_srvc, BTA_GATT_TRANSPORT_LE); + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_INIT_FINISH; + param.init_finish.state = ESP_BLUFI_INIT_OK; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + } + case BTA_GATTS_CONNECT_EVT: { + btc_msg_t msg; + esp_blufi_cb_param_t param; + + //set the connection flag to true + BLUFI_TRACE_API("\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); + + memcpy(blufi_env.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); + blufi_env.conn_id = p_data->conn.conn_id; + blufi_env.is_connected = true; + blufi_env.recv_seq = blufi_env.send_seq = 0; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_BLE_CONNECT; + memcpy(param.connect.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); + param.connect.conn_id = BTC_GATT_GET_CONN_ID(p_data->conn.conn_id); + conn_id = param.connect.conn_id; + server_if = p_data->conn.server_if; + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + } + case BTA_GATTS_DISCONNECT_EVT: { + btc_msg_t msg; + esp_blufi_cb_param_t param; + + blufi_env.is_connected = false; + //set the connection flag to true + BLUFI_TRACE_API("\ndevice is disconnected "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); + + memcpy(blufi_env.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); + blufi_env.conn_id = p_data->conn.conn_id; + blufi_env.recv_seq = blufi_env.send_seq = 0; + blufi_env.sec_mode = 0x0; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_BLE_DISCONNECT; + memcpy(param.disconnect.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + } + case BTA_GATTS_OPEN_EVT: + break; + case BTA_GATTS_CLOSE_EVT: + break; + case BTA_GATTS_CONGEST_EVT: + break; + default: + break; + } +} + +void esp_blufi_send_notify(void *arg) +{ + struct pkt_info *pkts = (struct pkt_info *) arg; + uint16_t conn_id = blufi_env.conn_id; + uint16_t attr_id = blufi_env.handle_char_e2p; + bool rsp = false; + BTA_GATTS_HandleValueIndication(conn_id, attr_id, pkts->pkt_len, + pkts->pkt, rsp); + +} + +void esp_blufi_deinit(void) +{ + BTA_GATTS_StopService(blufi_env.handle_srvc); + BTA_GATTS_DeleteService(blufi_env.handle_srvc); + /* register the BLUFI profile to the BTA_GATTS module*/ + BTA_GATTS_AppDeregister(blufi_env.gatt_if); +} + +void esp_blufi_adv_start(void) +{ + esp_ble_gap_set_device_name(BLUFI_DEVICE_NAME); + esp_ble_gap_config_adv_data(&blufi_adv_data); +} + +void esp_blufi_adv_stop(void) +{ + esp_ble_gap_stop_advertising(); +} + +void esp_blufi_send_encap(void *arg) +{ + struct blufi_hdr *hdr = (struct blufi_hdr *)arg; +retry: + if (blufi_env.is_connected == false) { + BTC_TRACE_WARNING("%s ble connection is broken\n", __func__); + osi_free(hdr); + hdr = NULL; + return; + } + if (esp_ble_get_cur_sendable_packets_num(BTC_GATT_GET_CONN_ID(blufi_env.conn_id)) > 0) { + btc_blufi_send_notify((uint8_t *)hdr, + ((hdr->fc & BLUFI_FC_CHECK) ? + hdr->data_len + sizeof(struct blufi_hdr) + 2 : + hdr->data_len + sizeof(struct blufi_hdr))); + } else { + BTC_TRACE_WARNING("%s wait to send blufi custom data\n", __func__); + vTaskDelay(pdMS_TO_TICKS(10)); + goto retry; + } +} + +esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + btc_msg_t msg; + btc_ble_gatts_args_t arg; + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GATTS; + msg.act = BTC_GATTS_ACT_CLOSE; + arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gatts_if, conn_id); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +void esp_blufi_disconnect() +{ + int rc; + rc = esp_blufi_close(server_if, conn_id); + assert (rc == 0); +} + +#endif diff --git a/components/bt/host/bluedroid/btc/profile/esp/blufi/blufi_prf.c b/components/bt/common/btc/profile/esp/blufi/blufi_prf.c similarity index 68% rename from components/bt/host/bluedroid/btc/profile/esp/blufi/blufi_prf.c rename to components/bt/common/btc/profile/esp/blufi/blufi_prf.c index 6150c651cd..d2c7f7ebff 100644 --- a/components/bt/host/bluedroid/btc/profile/esp/blufi/blufi_prf.c +++ b/components/bt/common/btc/profile/esp/blufi/blufi_prf.c @@ -19,61 +19,30 @@ #include -#include "common/bt_target.h" -#include "common/bt_trace.h" #include "osi/allocator.h" -#include "stack/bt_types.h" -#include "stack/gatt_api.h" -#include "bta/bta_api.h" -#include "bta/bta_gatt_api.h" -#include "bta_gatts_int.h" #include "btc_blufi_prf.h" #include "btc/btc_task.h" #include "btc/btc_manage.h" -#include "btc_gatt_util.h" #include "blufi_int.h" +#include "esp_log.h" #include "esp_blufi_api.h" -#include "esp_gatt_common_api.h" +#include "esp_blufi.h" #if (BLUFI_INCLUDED == TRUE) -#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] - -//define the blufi serivce uuid -#define BLUFI_SERVICE_UUID 0xFFFF -//define the blufi Char uuid (PHONE to ESP32) -#define BLUFI_CHAR_P2E_UUID 0xFF01 -//define the blufi Char uuid (ESP32 to PHONE) -#define BLUFI_CHAR_E2P_UUID 0xFF02 -//define the blufi Descriptor uuid (ESP32 to PHONE) -#define BLUFI_DESCR_E2P_UUID GATT_UUID_CHAR_CLIENT_CONFIG -//define the blufi APP ID -#define BLUFI_APP_UUID 0xFFFF - -#define BLUFI_HDL_NUM 6 - #if GATT_DYNAMIC_MEMORY == FALSE tBLUFI_ENV blufi_env; #else tBLUFI_ENV *blufi_env_ptr; #endif -static const tBT_UUID blufi_srvc_uuid = {LEN_UUID_16, {BLUFI_SERVICE_UUID}}; -static const tBT_UUID blufi_char_uuid_p2e = {LEN_UUID_16, {BLUFI_CHAR_P2E_UUID}}; -static const tBT_UUID blufi_char_uuid_e2p = {LEN_UUID_16, {BLUFI_CHAR_E2P_UUID}}; -static const tBT_UUID blufi_descr_uuid_e2p = {LEN_UUID_16, {BLUFI_DESCR_E2P_UUID}}; -static const tBT_UUID blufi_app_uuid = {LEN_UUID_16, {BLUFI_APP_UUID}}; - // static functions declare -static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data); -static void btc_blufi_recv_handler(uint8_t *data, int len); static void btc_blufi_send_ack(uint8_t seq); -static inline void btc_blufi_cb_to_app(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param) +inline void btc_blufi_cb_to_app(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param) { esp_blufi_event_cb_t btc_blufi_cb = (esp_blufi_event_cb_t)btc_profile_cb_get(BTC_PID_BLUFI); if (btc_blufi_cb) { @@ -81,299 +50,44 @@ static inline void btc_blufi_cb_to_app(esp_blufi_cb_event_t event, esp_blufi_cb_ } } -static void blufi_create_service(void) -{ - if (!blufi_env.enabled) { - BTC_TRACE_ERROR("blufi service added error."); - return; - } - - blufi_env.srvc_inst = 0x00; - BTA_GATTS_CreateService(blufi_env.gatt_if, &blufi_srvc_uuid, blufi_env.srvc_inst, BLUFI_HDL_NUM, true); -} - -static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) -{ - tBTA_GATTS_RSP rsp; - - BLUFI_TRACE_DEBUG("blufi profile cb event = %x\n", event); - - switch (event) { - case BTA_GATTS_REG_EVT: - BLUFI_TRACE_DEBUG("REG: status %d, app_uuid %04x, gatt_if %d\n", p_data->reg_oper.status, p_data->reg_oper.uuid.uu.uuid16, p_data->reg_oper.server_if); - - if (p_data->reg_oper.status != BTA_GATT_OK) { - BLUFI_TRACE_ERROR("BLUFI profile register failed\n"); - return; - } - - blufi_env.gatt_if = p_data->reg_oper.server_if; - blufi_env.enabled = true; - - //create the blufi service to the service data base. - if (p_data->reg_oper.uuid.uu.uuid16 == BLUFI_APP_UUID) { - BLUFI_TRACE_DEBUG("%s %d\n", __func__, __LINE__); - blufi_create_service(); - } - break; - case BTA_GATTS_DEREG_EVT: { - esp_blufi_cb_param_t param; - btc_msg_t msg; - - BLUFI_TRACE_DEBUG("DEREG: status %d, gatt_if %d\n", p_data->reg_oper.status, p_data->reg_oper.server_if); - - if (p_data->reg_oper.status != BTA_GATT_OK) { - BLUFI_TRACE_ERROR("BLUFI profile unregister failed\n"); - return; - } - - blufi_env.enabled = false; - - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_BLUFI; - msg.act = ESP_BLUFI_EVENT_DEINIT_FINISH; - param.deinit_finish.state = ESP_BLUFI_DEINIT_OK; - - btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); - - break; - } - case BTA_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 = 1; - rsp.attr_value.value[0] = 0x00; - BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, - p_data->req_data.status, &rsp); - break; - case BTA_GATTS_WRITE_EVT: { - if(p_data->req_data.p_data->write_req.is_prep) { - tBTA_GATT_STATUS status = GATT_SUCCESS; - - if (blufi_env.prepare_buf == NULL) { - blufi_env.prepare_buf = osi_malloc(BLUFI_PREPAIR_BUF_MAX_SIZE); - blufi_env.prepare_len = 0; - if (blufi_env.prepare_buf == NULL) { - BLUFI_TRACE_ERROR("Blufi prep no mem\n"); - status = GATT_NO_RESOURCES; - } - } else { - if(p_data->req_data.p_data->write_req.offset > BLUFI_PREPAIR_BUF_MAX_SIZE) { - status = GATT_INVALID_OFFSET; - } else if ((p_data->req_data.p_data->write_req.offset + p_data->req_data.p_data->write_req.len) > BLUFI_PREPAIR_BUF_MAX_SIZE) { - status = GATT_INVALID_ATTR_LEN; - } - } - - memset(&rsp, 0, sizeof(tGATTS_RSP)); - rsp.attr_value.handle = p_data->req_data.p_data->write_req.handle; - rsp.attr_value.len = p_data->req_data.p_data->write_req.len; - rsp.attr_value.offset = p_data->req_data.p_data->write_req.offset; - memcpy(rsp.attr_value.value, p_data->req_data.p_data->write_req.value, p_data->req_data.p_data->write_req.len); - - BLUFI_TRACE_DEBUG("prep write, len=%d, offset=%d\n", p_data->req_data.p_data->write_req.len, p_data->req_data.p_data->write_req.offset); - - BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, - status, &rsp); - - if(status != GATT_SUCCESS) { - if (blufi_env.prepare_buf) { - osi_free(blufi_env.prepare_buf); - blufi_env.prepare_buf = NULL; - blufi_env.prepare_len = 0; - } - BLUFI_TRACE_ERROR("write data error , error code 0x%x\n", status); - return; - } - memcpy(blufi_env.prepare_buf + p_data->req_data.p_data->write_req.offset, - p_data->req_data.p_data->write_req.value, - p_data->req_data.p_data->write_req.len); - blufi_env.prepare_len += p_data->req_data.p_data->write_req.len; - - return; - } else { - BLUFI_TRACE_DEBUG("norm write, len=%d, offset=%d\n", p_data->req_data.p_data->write_req.len, p_data->req_data.p_data->write_req.offset); - BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, - p_data->req_data.status, NULL); - } - - if (p_data->req_data.p_data->write_req.handle == blufi_env.handle_char_p2e) { - btc_blufi_recv_handler(&p_data->req_data.p_data->write_req.value[0], - p_data->req_data.p_data->write_req.len); - } - break; - } - case BTA_GATTS_EXEC_WRITE_EVT: - BLUFI_TRACE_DEBUG("exec write exec %d\n", p_data->req_data.p_data->exec_write); - - BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, - GATT_SUCCESS, NULL); - - if (blufi_env.prepare_buf && p_data->req_data.p_data->exec_write == GATT_PREP_WRITE_EXEC) { - btc_blufi_recv_handler(blufi_env.prepare_buf, blufi_env.prepare_len); - } - - if (blufi_env.prepare_buf) { - osi_free(blufi_env.prepare_buf); - blufi_env.prepare_buf = NULL; - blufi_env.prepare_len = 0; - } - - break; - case BTA_GATTS_MTU_EVT: - BLUFI_TRACE_DEBUG("MTU size %d\n", p_data->req_data.p_data->mtu); - blufi_env.frag_size = (p_data->req_data.p_data->mtu < BLUFI_MAX_DATA_LEN ? p_data->req_data.p_data->mtu : BLUFI_MAX_DATA_LEN) - BLUFI_MTU_RESERVED_SIZE; - break; - case BTA_GATTS_CONF_EVT: - BLUFI_TRACE_DEBUG("CONFIRM EVT\n"); - /* Nothing */ - break; - case BTA_GATTS_CREATE_EVT: - blufi_env.handle_srvc = p_data->create.service_id; - - //add the frist blufi characteristic --> write characteristic - BTA_GATTS_AddCharacteristic(blufi_env.handle_srvc, &blufi_char_uuid_p2e, - (GATT_PERM_WRITE), - (GATT_CHAR_PROP_BIT_WRITE), - NULL, NULL); - break; - case BTA_GATTS_ADD_CHAR_EVT: - switch (p_data->add_result.char_uuid.uu.uuid16) { - case BLUFI_CHAR_P2E_UUID: /* Phone to ESP32 */ - blufi_env.handle_char_p2e = p_data->add_result.attr_id; - - BTA_GATTS_AddCharacteristic(blufi_env.handle_srvc, &blufi_char_uuid_e2p, - (GATT_PERM_READ), - (GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_NOTIFY), - NULL, NULL); - break; - case BLUFI_CHAR_E2P_UUID: /* ESP32 to Phone */ - blufi_env.handle_char_e2p = p_data->add_result.attr_id; - - BTA_GATTS_AddCharDescriptor (blufi_env.handle_srvc, - (GATT_PERM_READ | GATT_PERM_WRITE), - &blufi_descr_uuid_e2p, - NULL, NULL); - break; - default: - break; - } - break; - case BTA_GATTS_ADD_CHAR_DESCR_EVT: { - /* call init finish */ - esp_blufi_cb_param_t param; - btc_msg_t msg; - - blufi_env.handle_descr_e2p = p_data->add_result.attr_id; - //start the blufi service after created - BTA_GATTS_StartService(blufi_env.handle_srvc, BTA_GATT_TRANSPORT_LE); - - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_BLUFI; - msg.act = ESP_BLUFI_EVENT_INIT_FINISH; - param.init_finish.state = ESP_BLUFI_INIT_OK; - - btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); - break; - } - case BTA_GATTS_CONNECT_EVT: { - btc_msg_t msg; - esp_blufi_cb_param_t param; - - //set the connection flag to true - BLUFI_TRACE_API("\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); - - memcpy(blufi_env.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); - blufi_env.conn_id = p_data->conn.conn_id; - blufi_env.is_connected = true; - blufi_env.recv_seq = blufi_env.send_seq = 0; - - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_BLUFI; - msg.act = ESP_BLUFI_EVENT_BLE_CONNECT; - memcpy(param.connect.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); - param.connect.conn_id=BTC_GATT_GET_CONN_ID(p_data->conn.conn_id); - param.connect.server_if=p_data->conn.server_if; - btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); - break; - } - case BTA_GATTS_DISCONNECT_EVT: { - btc_msg_t msg; - esp_blufi_cb_param_t param; - - blufi_env.is_connected = false; - //set the connection flag to true - BLUFI_TRACE_API("\ndevice is disconnected "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); - - memcpy(blufi_env.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); - blufi_env.conn_id = p_data->conn.conn_id; - blufi_env.recv_seq = blufi_env.send_seq = 0; - blufi_env.sec_mode = 0x0; - - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_BLUFI; - msg.act = ESP_BLUFI_EVENT_BLE_DISCONNECT; - memcpy(param.disconnect.remote_bda, p_data->conn.remote_bda, sizeof(esp_bd_addr_t)); - btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); - break; - } - case BTA_GATTS_OPEN_EVT: - break; - case BTA_GATTS_CLOSE_EVT: - break; - case BTA_GATTS_CONGEST_EVT: - break; - default: - break; - } -} - -static tGATT_STATUS btc_blufi_profile_init(void) +static uint8_t btc_blufi_profile_init(void) { esp_blufi_callbacks_t *store_p = blufi_env.cbs; + uint8_t rc; if (blufi_env.enabled) { BLUFI_TRACE_ERROR("BLUFI already initialized"); - return GATT_ERROR; + return ESP_BLUFI_ERROR; } memset(&blufi_env, 0x0, sizeof(blufi_env)); blufi_env.cbs = store_p; /* if set callback prior, restore the point */ blufi_env.frag_size = BLUFI_FRAG_DATA_DEFAULT_LEN; + rc = esp_blufi_init(); + if(rc != 0 ){ + return rc; + } - /* register the BLUFI profile to the BTA_GATTS module*/ - BTA_GATTS_AppRegister(&blufi_app_uuid, blufi_profile_cb); - - return GATT_SUCCESS; + return ESP_BLUFI_SUCCESS; } -static tGATT_STATUS btc_blufi_profile_deinit(void) +static uint8_t btc_blufi_profile_deinit(void) { if (!blufi_env.enabled) { BTC_TRACE_ERROR("BLUFI already de-initialized"); - return GATT_ERROR; + return ESP_BLUFI_ERROR; } - BTA_GATTS_StopService(blufi_env.handle_srvc); - BTA_GATTS_DeleteService(blufi_env.handle_srvc); - /* register the BLUFI profile to the BTA_GATTS module*/ - BTA_GATTS_AppDeregister(blufi_env.gatt_if); - - return GATT_SUCCESS; + esp_blufi_deinit(); + return ESP_BLUFI_SUCCESS; } -static void btc_blufi_send_notify(uint8_t *pkt, int pkt_len) +void btc_blufi_send_notify(uint8_t *pkt, int pkt_len) { - UINT16 conn_id = blufi_env.conn_id; - UINT16 attr_id = blufi_env.handle_char_e2p; - bool rsp = false; - - BTA_GATTS_HandleValueIndication(conn_id, attr_id, pkt_len, - pkt, rsp); + struct pkt_info pkts; + pkts.pkt = pkt; + pkts.pkt_len = pkt_len; + esp_blufi_send_notify(&pkts); } void btc_blufi_report_error(esp_blufi_error_state_t state) @@ -387,7 +101,7 @@ void btc_blufi_report_error(esp_blufi_error_state_t state) btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); } -static void btc_blufi_recv_handler(uint8_t *data, int len) +void btc_blufi_recv_handler(uint8_t *data, int len) { struct blufi_hdr *hdr = (struct blufi_hdr *)data; uint16_t checksum, checksum_pkt; @@ -536,24 +250,7 @@ void btc_blufi_send_encap(uint8_t type, uint8_t *data, int total_data_len) remain_len -= hdr->data_len; } -retry: - if (blufi_env.is_connected == false) { - BTC_TRACE_WARNING("%s ble connection is broken\n", __func__); - osi_free(hdr); - hdr = NULL; - return; - } - - if (esp_ble_get_cur_sendable_packets_num(BTC_GATT_GET_CONN_ID(blufi_env.conn_id)) > 0) { - btc_blufi_send_notify((uint8_t *)hdr, - ((hdr->fc & BLUFI_FC_CHECK) ? - hdr->data_len + sizeof(struct blufi_hdr) + 2 : - hdr->data_len + sizeof(struct blufi_hdr))); - } else { - BTC_TRACE_WARNING("%s wait to send blufi custom data\n", __func__); - vTaskDelay(pdMS_TO_TICKS(10)); - goto retry; - } + esp_blufi_send_encap(hdr); osi_free(hdr); hdr = NULL; diff --git a/components/bt/host/bluedroid/btc/profile/esp/blufi/blufi_protocol.c b/components/bt/common/btc/profile/esp/blufi/blufi_protocol.c similarity index 98% rename from components/bt/host/bluedroid/btc/profile/esp/blufi/blufi_protocol.c rename to components/bt/common/btc/profile/esp/blufi/blufi_protocol.c index 746b6c82e0..193221e811 100644 --- a/components/bt/host/bluedroid/btc/profile/esp/blufi/blufi_protocol.c +++ b/components/bt/common/btc/profile/esp/blufi/blufi_protocol.c @@ -18,15 +18,6 @@ #include #include - -#include "common/bt_target.h" -#include "common/bt_trace.h" -#include "stack/bt_types.h" -#include "stack/gatt_api.h" -#include "bta/bta_api.h" -#include "bta/bta_gatt_api.h" -#include "bta_gatts_int.h" - #include "btc_blufi_prf.h" #include "btc/btc_task.h" #include "btc/btc_manage.h" diff --git a/components/bt/host/bluedroid/btc/profile/esp/blufi/include/blufi_int.h b/components/bt/common/btc/profile/esp/blufi/include/blufi_int.h similarity index 98% rename from components/bt/host/bluedroid/btc/profile/esp/blufi/include/blufi_int.h rename to components/bt/common/btc/profile/esp/blufi/include/blufi_int.h index 6b7b9fb3f9..996621e048 100644 --- a/components/bt/host/bluedroid/btc/profile/esp/blufi/include/blufi_int.h +++ b/components/bt/common/btc/profile/esp/blufi/include/blufi_int.h @@ -15,11 +15,15 @@ #ifndef __BLUFI_INT_H__ #define __BLUFI_INT_H__ +#include "btc/btc_task.h" +#include "esp_blufi_api.h" #if (BLUFI_INCLUDED == TRUE) + #define BTC_BLUFI_GREAT_VER 0x01 //Version + Subversion #define BTC_BLUFI_SUB_VER 0x02 //Version + Subversion #define BTC_BLUFI_VERSION ((BTC_BLUFI_GREAT_VER<<8)|BTC_BLUFI_SUB_VER) //Version + Subversion +typedef UINT8 tGATT_IF; /* service engine control block */ typedef struct { /* Protocol reference */ @@ -176,6 +180,9 @@ extern tBLUFI_ENV *blufi_env_ptr; #define BLUFI_FC_IS_REQ_ACK(fc) ((fc) & BLUFI_FC_REQ_ACK_MASK) #define BLUFI_FC_IS_FRAG(fc) ((fc) & BLUFI_FC_FRAG_MASK) +/* default GATT MTU size over LE link +*/ +#define GATT_DEF_BLE_MTU_SIZE 23 /* BLUFI HEADER + TOTAL(REMAIN) LENGTH + CRC + L2CAP RESERVED */ #define BLUFI_MTU_RESERVED_SIZE (sizeof(struct blufi_hdr) + 2 + 2 + 3) #define BLUFI_FRAG_DATA_DEFAULT_LEN (GATT_DEF_BLE_MTU_SIZE - BLUFI_MTU_RESERVED_SIZE) diff --git a/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h b/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h new file mode 100644 index 0000000000..060a364d21 --- /dev/null +++ b/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h @@ -0,0 +1,77 @@ +#ifndef __ESP_BLUFI_H__ +#define __ESP_BLUFI_H__ + +#include "esp_blufi_api.h" +#include "esp_err.h" + +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "nimble/ble.h" +#include "modlog/modlog.h" +#endif + +#ifdef CONFIG_BT_BLUEDROID_ENABLED +#include "esp_gap_ble_api.h" +#endif + +#define BLUFI_APP_UUID 0xFFFF +#define BLUFI_DEVICE_NAME "BLUFI_DEVICE" + +#ifdef CONFIG_BT_NIMBLE_ENABLED +struct ble_hs_cfg; +struct ble_gatt_register_ctxt; +struct gatt_value { + struct os_mbuf *buf; + uint16_t val_handle; + uint8_t type; + void *ptr; +}; +#define SERVER_MAX_VALUES 3 +#define MAX_VAL_SIZE 512 +extern struct gatt_value gatt_values[SERVER_MAX_VALUES]; +/* GATT server callback */ +void esp_blufi_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg); + +/* Initialise gatt server */ +int esp_blufi_gatt_svr_init(void); +void esp_blufi_btc_init(void); +#endif + +#ifdef CONFIG_BT_BLUEDROID_ENABLED +/** + * @brief Close a connection a remote device. + * + * @param[in] gatts_if: GATT server access interface + * @param[in] conn_id: connection ID to be closed. + * + * @return + * - ESP_OK : success + * - other : failed + * + */ +esp_err_t esp_blufi_close(uint8_t gatts_if, uint16_t conn_id); +void esp_blufi_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); +#endif + +/* Initialise blufi profile */ +uint8_t esp_blufi_init(void); + +/* start advertising */ +void bleprph_advertise(void); + +/* send notifications */ +void esp_blufi_send_notify(void *arg); + +/* Deinitialise blufi */ +void esp_blufi_deinit(void); +/* disconnect */ +void esp_blufi_disconnect(void); + +/* Stop advertisement */ +void esp_blufi_adv_stop(void); + +/* Start advertisement */ +void esp_blufi_adv_start(void); + +void esp_blufi_send_encap(void *arg); + +#endif/* _ESP_BLUFI_ */ diff --git a/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c new file mode 100644 index 0000000000..5538357b94 --- /dev/null +++ b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c @@ -0,0 +1,457 @@ +#include +#include +#include +#include + +#include "btc_blufi_prf.h" +#include "blufi_int.h" +#include "esp_log.h" +#include "esp_blufi_api.h" +#include "esp_err.h" +#include "btc/btc_task.h" +#include "esp_blufi.h" +#include "osi/allocator.h" +#include "console/console.h" + +/*nimBLE Host*/ +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "host/ble_uuid.h" +#include "host/ble_gatt.h" + +#include "services/gap/ble_svc_gap.h" +#include "services/gatt/ble_svc_gatt.h" + +#if (BLUFI_INCLUDED == TRUE) + +static uint8_t own_addr_type; +static uint16_t conn_handle; + +struct gatt_value gatt_values[SERVER_MAX_VALUES]; +const static char *TAG = "BLUFI_EXAMPLE"; + +enum { + GATT_VALUE_TYPE_CHR, + GATT_VALUE_TYPE_DSC, +}; + +static int gatt_svr_access_cb(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg); + +static const struct ble_gatt_svc_def gatt_svr_svcs[] = { + { + /*** Service: Blufi */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLUFI_SERVICE_UUID), + .characteristics = (struct ble_gatt_chr_def[]) + { { + /*** Characteristic: P2E */ + .uuid = BLE_UUID16_DECLARE(BLUFI_CHAR_P2E_UUID), + .access_cb = gatt_svr_access_cb, + .flags = BLE_GATT_CHR_F_WRITE, + .arg = &gatt_values[0], + .val_handle = &gatt_values[0].val_handle, + }, { + /*** Characteristic: E2P */ + .uuid = BLE_UUID16_DECLARE(BLUFI_CHAR_E2P_UUID), + .access_cb = gatt_svr_access_cb, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, + .arg = &gatt_values[1], + .val_handle = &gatt_values[1].val_handle, + }, { + 0, /* No more characteristics in this service. */ + } + }, + }, + + { + 0, /* No more services. */ + }, +}; + +void esp_blufi_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) +{ + char buf[BLE_UUID_STR_LEN]; + switch (ctxt->op) { + case BLE_GATT_REGISTER_OP_SVC: + ESP_LOGI(TAG, "registered service %s with handle=%d\n", + ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf), + ctxt->svc.handle); + break; + + case BLE_GATT_REGISTER_OP_CHR: + ESP_LOGI(TAG, "registering characteristic %s with " + "def_handle=%d val_handle=%d\n", + ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf), + ctxt->chr.def_handle, + ctxt->chr.val_handle); + break; + + case BLE_GATT_REGISTER_OP_DSC: + ESP_LOGI(TAG, "registering descriptor %s with handle=%d\n", + ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf), + ctxt->dsc.handle); + break; + + default: + assert(0); + break; + } +} + +static size_t write_value(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + struct gatt_value *value = (struct gatt_value *)arg; + uint16_t len; + int rc; + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + if (ctxt->chr->flags & BLE_GATT_CHR_F_WRITE_AUTHOR) { + return BLE_ATT_ERR_INSUFFICIENT_AUTHOR; + } + } else { + if (ctxt->dsc->att_flags & BLE_ATT_F_WRITE_AUTHOR) { + return BLE_ATT_ERR_INSUFFICIENT_AUTHOR; + } + } + + btc_blufi_recv_handler(&ctxt->om->om_data[0], ctxt->om->om_len); + rc = ble_hs_mbuf_to_flat(ctxt->om, value->buf->om_data, + value->buf->om_len, &len); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + /* Maximum attribute value size is 512 bytes */ + assert(value->buf->om_len < MAX_VAL_SIZE); + + return 0; +} + +static size_t read_value(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + const struct gatt_value *value = (const struct gatt_value *) arg; + char str[BLE_UUID_STR_LEN]; + int rc; + + memset(str, '\0', sizeof(str)); + + if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + if (ctxt->chr->flags & BLE_GATT_CHR_F_READ_AUTHOR) { + return BLE_ATT_ERR_INSUFFICIENT_AUTHOR; + } + + ble_uuid_to_str(ctxt->chr->uuid, str); + } else { + if (ctxt->dsc->att_flags & BLE_ATT_F_READ_AUTHOR) { + return BLE_ATT_ERR_INSUFFICIENT_AUTHOR; + } + + ble_uuid_to_str(ctxt->dsc->uuid, str); + } + + rc = os_mbuf_append(ctxt->om, value->buf->om_data, value->buf->om_len); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + +static int gatt_svr_access_cb(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_READ_CHR: + return read_value(conn_handle, attr_handle, + ctxt, arg); + case BLE_GATT_ACCESS_OP_WRITE_CHR: + return write_value(conn_handle, attr_handle, + ctxt, arg); + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } + + /* Unknown characteristic; the nimble stack should not have called this + * function. + */ + assert(0); + return BLE_ATT_ERR_UNLIKELY; +} + +static void init_gatt_values(void) +{ + int i = 0; + const struct ble_gatt_svc_def *svc; + const struct ble_gatt_chr_def *chr; + const struct ble_gatt_dsc_def *dsc; + + for (svc = gatt_svr_svcs; svc && svc->uuid; svc++) { + for (chr = svc->characteristics; chr && chr->uuid; chr++) { + assert(i < SERVER_MAX_VALUES); + gatt_values[i].type = GATT_VALUE_TYPE_CHR; + gatt_values[i].ptr = (void *)chr; + gatt_values[i].buf = os_msys_get(0, 0); + os_mbuf_extend(gatt_values[i].buf, 1); + ++i; + + for (dsc = chr->descriptors; dsc && dsc->uuid; dsc++) { + assert(i < SERVER_MAX_VALUES); + gatt_values[i].type = GATT_VALUE_TYPE_DSC; + gatt_values[i].ptr = (void *)dsc; + gatt_values[i].buf = os_msys_get(0, 0); + os_mbuf_extend(gatt_values[i].buf, 1); + ++i; + } + } + } + +} + +int esp_blufi_gatt_svr_init(void) +{ + int rc; + ble_svc_gap_init(); + ble_svc_gatt_init(); + + rc = ble_gatts_count_cfg(gatt_svr_svcs); + if (rc != 0) { + return rc; + } + + rc = ble_gatts_add_svcs(gatt_svr_svcs); + if (rc != 0) { + return rc; + } + + init_gatt_values(); + return 0; +} + +static int +esp_blufi_gap_event(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_conn_desc desc; + int rc; + + switch (event->type) { + case BLE_GAP_EVENT_CONNECT: + /* A new connection was established or a connection attempt failed. */ + ESP_LOGI(TAG, "connection %s; status=%d\n", + event->connect.status == 0 ? "established" : "failed", + event->connect.status); + if (event->connect.status == 0) { + + blufi_env.is_connected = true; + blufi_env.recv_seq = blufi_env.send_seq = 0; + btc_msg_t msg; + esp_blufi_cb_param_t param; + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_BLE_CONNECT; + + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + memcpy(param.connect.remote_bda, desc.peer_id_addr.val, sizeof(esp_bd_addr_t)); + + param.connect.conn_id = event->connect.conn_handle; + /* save connection handle */ + conn_handle = event->connect.conn_handle; + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + } + if (event->connect.status != 0) { + /* Connection failed; resume advertising. */ + esp_blufi_adv_start(); + } + return 0; + case BLE_GAP_EVENT_DISCONNECT: + ESP_LOGI(TAG, "disconnect; reason=%d\n", event->disconnect.reason); + memcpy(blufi_env.remote_bda, event->disconnect.conn.peer_id_addr.val, sizeof(esp_bd_addr_t)); + blufi_env.is_connected = false; + blufi_env.recv_seq = blufi_env.send_seq = 0; + blufi_env.sec_mode = 0x0; + btc_msg_t msg; + esp_blufi_cb_param_t param; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_BLE_DISCONNECT; + memcpy(param.disconnect.remote_bda, event->disconnect.conn.peer_id_addr.val, sizeof(esp_bd_addr_t)); + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + + return 0; + case BLE_GAP_EVENT_CONN_UPDATE: + /* The central has updated the connection parameters. */ + ESP_LOGI(TAG, "connection updated; status=%d\n", + event->conn_update.status); + return 0; + + case BLE_GAP_EVENT_ADV_COMPLETE: + ESP_LOGI(TAG, "advertise complete; reason=%d", + event->adv_complete.reason); + esp_blufi_adv_start(); + return 0; + + case BLE_GAP_EVENT_SUBSCRIBE: + ESP_LOGI(TAG, "subscribe event; conn_handle=%d attr_handle=%d " + "reason=%d prevn=%d curn=%d previ=%d curi=%d\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate, + event->subscribe.cur_indicate); + return 0; + + case BLE_GAP_EVENT_MTU: + ESP_LOGI(TAG, "mtu update event; conn_handle=%d cid=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.channel_id, + event->mtu.value); + blufi_env.frag_size = (event->mtu.value < BLUFI_MAX_DATA_LEN ? event->mtu.value : BLUFI_MAX_DATA_LEN) - BLUFI_MTU_RESERVED_SIZE; + return 0; + + } + return 0; +} + +void esp_blufi_adv_start(void) +{ + int rc; + + rc = ble_hs_util_ensure_addr(0); + assert(rc == 0); + + /* Figure out address to use while advertising (no privacy for now) */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + ESP_LOGI(TAG, "error determining address type; rc=%d ", rc); + return; + } + + /* Printing ADDR */ + uint8_t addr_val[6] = {0}; + rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL); + + /* Begin advertising. */ + struct ble_gap_adv_params adv_params; + struct ble_hs_adv_fields fields; + const char *name; + + /** + * Set the advertisement data included in our advertisements: + * o Flags (indicates advertisement type and other general info). + * o Advertising tx power. + * o Device name. + * o 16-bit service UUIDs (alert notifications). + */ + + memset(&fields, 0, sizeof fields); + + /* Advertise two flags: + * o Discoverability in forthcoming advertisement (general) + * o BLE-only (BR/EDR unsupported). + */ + fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; + + /* Indicate that the TX power level field should be included; have the + * stack fill this value automatically. This is done by assigning the + * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. + */ + fields.tx_pwr_lvl_is_present = 1; + fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + + name = ble_svc_gap_device_name(); + fields.name = (uint8_t *)name; + fields.name_len = strlen(name); + fields.name_is_complete = 1; + + fields.uuids16 = (ble_uuid16_t[]) { + BLE_UUID16_INIT(BLUFI_APP_UUID) + }; + fields.num_uuids16 = 1; + fields.uuids16_is_complete = 1; + rc = ble_gap_adv_set_fields(&fields); + if (rc != 0) { + ESP_LOGE(TAG, "error setting advertisement data; rc=%d\n", rc); + return; + } + + /* Begin advertising. */ + memset(&adv_params, 0, sizeof adv_params); + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, + &adv_params, esp_blufi_gap_event, NULL); + if (rc != 0) { + ESP_LOGE(TAG, "error enabling advertisement; rc=%d\n", rc); + return; + } +} + +uint8_t esp_blufi_init(void) +{ + blufi_env.enabled = true; + esp_blufi_cb_param_t param; + param.init_finish.state = ESP_BLUFI_INIT_OK; + btc_blufi_cb_to_app(ESP_BLUFI_EVENT_INIT_FINISH, ¶m); + return ESP_BLUFI_ERROR; +} + +void esp_blufi_deinit(void) +{ + blufi_env.enabled = false; + btc_msg_t msg; + esp_blufi_cb_param_t param; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_DEINIT_FINISH; + param.deinit_finish.state = ESP_BLUFI_DEINIT_OK; + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); +} + +void esp_blufi_send_notify(void *arg) +{ + struct pkt_info *pkts = (struct pkt_info *) arg; + struct os_mbuf *om; + om = ble_hs_mbuf_from_flat(pkts->pkt, pkts->pkt_len); + int rc = 0; + rc = ble_gattc_notify_custom(conn_handle, gatt_values[1].val_handle, om); + assert(rc == 0); +} + +void esp_blufi_disconnect(void) +{ + ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM); +} + +void esp_blufi_adv_stop(void) {} + +void esp_blufi_send_encap(void *arg) +{ + struct blufi_hdr *hdr = (struct blufi_hdr *)arg; + if (blufi_env.is_connected == false) { + BTC_TRACE_WARNING("%s ble connection is broken\n", __func__); + osi_free(hdr); + hdr = NULL; + return; + } + btc_blufi_send_notify((uint8_t *)hdr, + ((hdr->fc & BLUFI_FC_CHECK) ? + hdr->data_len + sizeof(struct blufi_hdr) + 2 : + hdr->data_len + sizeof(struct blufi_hdr))); +} + +void esp_blufi_btc_init(void) +{ + int rc; + rc = btc_init(); + assert(rc == 0); +} + +#endif diff --git a/components/bt/host/bluedroid/btc/profile/esp/include/btc_blufi_prf.h b/components/bt/common/btc/profile/esp/include/btc_blufi_prf.h similarity index 56% rename from components/bt/host/bluedroid/btc/profile/esp/include/btc_blufi_prf.h rename to components/bt/common/btc/profile/esp/include/btc_blufi_prf.h index e79b896f88..31bbc380d6 100644 --- a/components/bt/host/bluedroid/btc/profile/esp/include/btc_blufi_prf.h +++ b/components/bt/common/btc/profile/esp/include/btc_blufi_prf.h @@ -15,9 +15,47 @@ #ifndef __BTC_BLUFI_PRF_H__ #define __BTC_BLUFI_PRF_H__ -#include "common/bt_target.h" +#include "blufi_int.h" #include "btc/btc_task.h" #include "esp_blufi_api.h" +#include "esp_err.h" + +#ifdef CONFIG_BT_BLUEDROID_ENABLED +#include "stack/gatt_api.h" +#define ESP_BLUFI_ERROR GATT_ERROR +#define ESP_BLUFI_SUCCESS GATT_SUCCESS +#else +#define ESP_BLUFI_ERROR 0x85 +#define ESP_BLUFI_SUCCESS 0x00 +#endif + +#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] + +#define GATT_UUID_CHAR_CLIENT_CONFIG 0x2902 /* Client Characteristic Configuration */ +//define the blufi serivce uuid +#define BLUFI_SERVICE_UUID 0xFFFF +//define the blufi Char uuid (PHONE to ESP32) +#define BLUFI_CHAR_P2E_UUID 0xFF01 +//define the blufi Char uuid (ESP32 to PHONE) +#define BLUFI_CHAR_E2P_UUID 0xFF02 +//define the blufi Descriptor uuid (ESP32 to PHONE) +#define BLUFI_DESCR_E2P_UUID GATT_UUID_CHAR_CLIENT_CONFIG +//define the blufi APP ID +#define BLUFI_APP_UUID 0xFFFF + +#define BLUFI_HDL_NUM 6 + +struct pkt_info{ + uint8_t *pkt; + int pkt_len; +}; + +static const tBT_UUID blufi_srvc_uuid = {LEN_UUID_16, {BLUFI_SERVICE_UUID}}; +static const tBT_UUID blufi_char_uuid_p2e = {LEN_UUID_16, {BLUFI_CHAR_P2E_UUID}}; +static const tBT_UUID blufi_char_uuid_e2p = {LEN_UUID_16, {BLUFI_CHAR_E2P_UUID}}; +static const tBT_UUID blufi_descr_uuid_e2p = {LEN_UUID_16, {BLUFI_DESCR_E2P_UUID}}; +static const tBT_UUID blufi_app_uuid = {LEN_UUID_16, {BLUFI_APP_UUID}}; typedef enum { BTC_BLUFI_ACT_INIT = 0, @@ -58,10 +96,13 @@ typedef union { } custom_data; } btc_blufi_args_t; +void btc_blufi_cb_to_app(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param); void btc_blufi_cb_handler(btc_msg_t *msg); void btc_blufi_call_handler(btc_msg_t *msg); void btc_blufi_set_callbacks(esp_blufi_callbacks_t *callbacks); +void btc_blufi_recv_handler(uint8_t *data, int len); +void btc_blufi_send_notify(uint8_t *pkt, int pkt_len); void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); void btc_blufi_call_deep_free(btc_msg_t *msg); diff --git a/components/bt/common/include/bt_common.h b/components/bt/common/include/bt_common.h index 15662303e3..6951bbca6f 100644 --- a/components/bt/common/include/bt_common.h +++ b/components/bt/common/include/bt_common.h @@ -27,12 +27,31 @@ #define TRUE true #endif + +#if (UC_BT_BLUFI_ENABLE) +#define BLUFI_INCLUDED TRUE +#else +#define BLUFI_INCLUDED FALSE +#endif + +#ifdef CONFIG_BT_BLUEDROID_ENABLED +#include "esp_bt_defs.h" +#include "esp_bt_main.h" +#include "esp_gatt_defs.h" +#define ESP_BLE_HOST_STATUS_ENABLED ESP_BLUEDROID_STATUS_ENABLED +#define ESP_BLE_HOST_STATUS_CHECK(status) ESP_BLUEDROID_STATUS_CHECK(status) +#else +#define ESP_BLE_HOST_STATUS_ENABLED 0 +#define ESP_BLE_HOST_STATUS_CHECK(status) do {} while (0) +#endif + #ifndef BT_QUEUE_CONGEST_SIZE #define BT_QUEUE_CONGEST_SIZE 40 #endif #define BTC_INITIAL_TRACE_LEVEL UC_BT_LOG_BTC_TRACE_LEVEL #define OSI_INITIAL_TRACE_LEVEL UC_BT_LOG_OSI_TRACE_LEVEL +#define BLUFI_INITIAL_TRACE_LEVEL UC_BT_LOG_BLUFI_TRACE_LEVEL #if UC_BT_BLE_DYNAMIC_ENV_MEMORY #define BT_BLE_DYNAMIC_ENV_MEMORY TRUE @@ -109,6 +128,14 @@ #define OSI_TRACE_DEBUG(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(OSI,DEBUG)) BT_PRINT_D("BT_OSI", fmt, ## args);} #define OSI_TRACE_VERBOSE(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(OSI,VERBOSE)) BT_PRINT_V("BT_OSI", fmt, ## args);} +/* define traces for BLUFI */ +#define BLUFI_TRACE_ERROR(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BLUFI, ERROR)) BT_PRINT_E("BT_BLUFI", fmt, ## args);} +#define BLUFI_TRACE_WARNING(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BLUFI, WARNING)) BT_PRINT_W("BT_BLUFI", fmt, ## args);} +#define BLUFI_TRACE_API(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BLUFI,API)) BT_PRINT_I("BT_BLUFI", fmt, ## args);} +#define BLUFI_TRACE_EVENT(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BLUFI,EVENT)) BT_PRINT_D("BT_BLUFI", fmt, ## args);} +#define BLUFI_TRACE_DEBUG(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BLUFI,DEBUG)) BT_PRINT_D("BT_BLUFI", fmt, ## args);} +#define BLUFI_TRACE_VERBOSE(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BLUFI,VERBOSE)) BT_PRINT_V("BT_BLUFI", fmt, ## args);} + #else /* define traces for BTC */ @@ -127,6 +154,14 @@ #define OSI_TRACE_DEBUG(fmt, args...) #define OSI_TRACE_VERBOSE(fmt, args...) +/* define traces for BLUFI */ +#define BLUFI_TRACE_ERROR(fmt, args...) +#define BLUFI_TRACE_WARNING(fmt, args...) +#define BLUFI_TRACE_API(fmt, args...) +#define BLUFI_TRACE_EVENT(fmt, args...) +#define BLUFI_TRACE_DEBUG(fmt, args...) +#define BLUFI_TRACE_VERBOSE(fmt, args...) + #endif /** Bluetooth Error Status */ @@ -155,4 +190,31 @@ typedef enum { BT_STATUS_EIR_TOO_LARGE, } bt_status_t; +typedef uint8_t UINT8; +typedef uint16_t UINT16; +typedef uint32_t UINT32; +typedef uint64_t UINT64; +typedef bool BOOLEAN; +/* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */ +#define MAX_UUID_SIZE 16 + +typedef struct { +#define LEN_UUID_16 2 +#define LEN_UUID_32 4 +#define LEN_UUID_128 16 + + UINT16 len; + + union { + UINT16 uuid16; + UINT32 uuid32; + UINT8 uuid128[MAX_UUID_SIZE]; + } uu; + +} tBT_UUID; + +/* Common Bluetooth field definitions */ +#define BD_ADDR_LEN 6 /* Device address length */ +typedef UINT8 BD_ADDR[BD_ADDR_LEN]; /* Device address */ + #endif /* _BT_COMMON_H_ */ diff --git a/components/bt/common/include/bt_user_config.h b/components/bt/common/include/bt_user_config.h index d496300603..0ad168d224 100644 --- a/components/bt/common/include/bt_user_config.h +++ b/components/bt/common/include/bt_user_config.h @@ -80,4 +80,17 @@ #define UC_BT_LOG_OSI_TRACE_LEVEL UC_TRACE_LEVEL_WARNING #endif +#ifdef CONFIG_BT_LOG_BLUFI_TRACE_LEVEL +#define UC_BT_LOG_BLUFI_TRACE_LEVEL CONFIG_BT_LOG_BLUFI_TRACE_LEVEL +#else +#define UC_BT_LOG_BLUFI_TRACE_LEVEL UC_TRACE_LEVEL_WARNING +#endif + +//BLUFI +#if defined(CONFIG_BT_BLE_BLUFI_ENABLE) || defined(CONFIG_BT_NIMBLE_BLUFI_ENABLE) +#define UC_BT_BLUFI_ENABLE TRUE +#else +#define UC_BT_BLUFI_ENABLE FALSE +#endif + #endif /* __BT_USER_CONFIG_H__ */ diff --git a/components/bt/component.mk b/components/bt/component.mk index 9f909cef8d..5687f49381 100644 --- a/components/bt/component.mk +++ b/components/bt/component.mk @@ -24,6 +24,18 @@ CFLAGS += -Wno-implicit-fallthrough COMPONENT_ADD_INCLUDEDIRS += include/esp32/include COMPONENT_SRCDIRS += controller/esp32 +COMPONENT_PRIV_INCLUDEDIRS += common/btc/include \ + common/include + +COMPONENT_ADD_INCLUDEDIRS += common/api/include/api \ + common/btc/profile/esp/blufi/include \ + common/btc/profile/esp/include \ + common/osi/include +COMPONENT_SRCDIRS += common/api \ + common/btc/core \ + common/osi \ + common/btc/profile/esp/blufi + ifdef CONFIG_BT_BLUEDROID_ENABLED COMPONENT_PRIV_INCLUDEDIRS += host/bluedroid/bta/include \ @@ -44,7 +56,6 @@ COMPONENT_PRIV_INCLUDEDIRS += host/bluedroid/bta/include \ host/bluedroid/external/sbc/decoder/include \ host/bluedroid/external/sbc/encoder/include \ host/bluedroid/external/sbc/plc/include \ - host/bluedroid/btc/profile/esp/blufi/include \ host/bluedroid/btc/profile/esp/include \ host/bluedroid/btc/profile/std/gatt/include \ host/bluedroid/btc/profile/std/gap/include \ @@ -71,7 +82,6 @@ COMPONENT_PRIV_INCLUDEDIRS += host/bluedroid/bta/include \ host/bluedroid/common/include \ COMPONENT_ADD_INCLUDEDIRS += host/bluedroid/api/include/api \ - common/osi/include COMPONENT_SRCDIRS += host/bluedroid/bta/dm \ host/bluedroid/bta/gatt \ @@ -93,7 +103,6 @@ COMPONENT_SRCDIRS += host/bluedroid/bta/dm \ host/bluedroid/external/sbc/encoder/srce \ host/bluedroid/external/sbc/plc \ host/bluedroid/btc/core \ - host/bluedroid/btc/profile/esp/blufi \ host/bluedroid/btc/profile/std/gap \ host/bluedroid/btc/profile/std/gatt \ host/bluedroid/btc/profile/std/a2dp \ @@ -128,11 +137,7 @@ host/bluedroid/btc/core/btc_config.o: CFLAGS += -Wno-unused-const-variable host/bluedroid/stack/btm/btm_sec.o: CFLAGS += -Wno-unused-const-variable host/bluedroid/stack/smp/smp_keys.o: CFLAGS += -Wno-unused-const-variable -COMPONENT_PRIV_INCLUDEDIRS += common/btc/include \ - common/include - -COMPONENT_SRCDIRS += common/osi \ - common/btc/core \ +COMPONENT_SRCDIRS += common/btc/profile/esp/blufi/bluedroid_host ifdef CONFIG_BLE_MESH @@ -218,14 +223,9 @@ endif COMPONENT_OBJEXCLUDE += host/nimble/nimble/nimble/host/store/config/src/ble_store_config_conf.o ifdef CONFIG_BLE_MESH -COMPONENT_PRIV_INCLUDEDIRS += common/btc/include \ - common/include -COMPONENT_SRCDIRS += common/osi \ - common/btc/core \ - esp_ble_mesh/mesh_core/nimble_host +COMPONENT_SRCDIRS += esp_ble_mesh/mesh_core/nimble_host -COMPONENT_ADD_INCLUDEDIRS += common/osi/include endif ifdef CONFIG_BT_NIMBLE_MESH @@ -234,6 +234,7 @@ COMPONENT_ADD_INCLUDEDIRS += host/nimble/nimble/nimble/host/mesh/include COMPONENT_SRCDIRS += host/nimble/nimble/nimble/host/mesh/src endif +COMPONENT_SRCDIRS += common/btc/profile/esp/blufi/nimble_host endif endif diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h index 4be2189ea6..e056d77ac2 100644 --- a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -22,16 +22,6 @@ #include "proxy_server.h" #include "provisioner_main.h" -#ifdef CONFIG_BT_BLUEDROID_ENABLED -#include "esp_bt_defs.h" -#include "esp_bt_main.h" -#define ESP_BLE_HOST_STATUS_ENABLED ESP_BLUEDROID_STATUS_ENABLED -#define ESP_BLE_HOST_STATUS_CHECK(status) ESP_BLUEDROID_STATUS_CHECK(status) -#else -#define ESP_BLE_HOST_STATUS_ENABLED 0 -#define ESP_BLE_HOST_STATUS_CHECK(status) do {} while (0) -#endif - #ifdef __cplusplus extern "C" { #endif diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index 4edca2bacb..07663e6da2 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -120,13 +120,6 @@ #define UC_BT_GATTC_ENABLE FALSE #endif -//BLUFI -#ifdef CONFIG_BT_BLE_BLUFI_ENABLE -#define UC_BT_BLUFI_ENABLE CONFIG_BT_BLE_BLUFI_ENABLE -#else -#define UC_BT_BLUFI_ENABLE FALSE -#endif - //GATTC CACHE #ifdef CONFIG_BT_GATTC_CACHE_NVS_FLASH #define UC_BT_GATTC_CACHE_NVS_FLASH_ENABLED CONFIG_BT_GATTC_CACHE_NVS_FLASH @@ -402,10 +395,4 @@ #define UC_BT_LOG_BTIF_TRACE_LEVEL UC_TRACE_LEVEL_WARNING #endif -#ifdef CONFIG_BT_LOG_BLUFI_TRACE_LEVEL -#define UC_BT_LOG_BLUFI_TRACE_LEVEL CONFIG_BT_LOG_BLUFI_TRACE_LEVEL -#else -#define UC_BT_LOG_BLUFI_TRACE_LEVEL UC_TRACE_LEVEL_WARNING -#endif - #endif /* __BLUEDROID_USER_CONFIG_H__ */ diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index fa16eea4fa..341190268e 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -190,12 +190,6 @@ #define GATTC_INCLUDED FALSE #endif /* UC_BT_GATTC_ENABLE */ -#if (UC_BT_BLUFI_ENABLE) -#define BLUFI_INCLUDED TRUE -#else -#define BLUFI_INCLUDED FALSE -#endif /* UC_BT_BLUFI_ENABLE */ - #if (UC_BT_GATTC_ENABLE && UC_BT_GATTC_CACHE_NVS_FLASH_ENABLED) #define GATTC_CACHE_NVS TRUE #else diff --git a/components/bt/host/bluedroid/common/include/common/bt_trace.h b/components/bt/host/bluedroid/common/include/common/bt_trace.h index 7afde68815..55babed961 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_trace.h +++ b/components/bt/host/bluedroid/common/include/common/bt_trace.h @@ -203,7 +203,6 @@ static inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t l #define GATT_INITIAL_TRACE_LEVEL UC_BT_LOG_GATT_TRACE_LEVEL #define SMP_INITIAL_TRACE_LEVEL UC_BT_LOG_SMP_TRACE_LEVEL #define BTIF_INITIAL_TRACE_LEVEL UC_BT_LOG_BTIF_TRACE_LEVEL -#define BLUFI_INITIAL_TRACE_LEVEL UC_BT_LOG_BLUFI_TRACE_LEVEL // btla-specific -- @@ -360,14 +359,6 @@ extern UINT8 btif_trace_level; #define HCI_TRACE_EVENT(fmt, args...) {if (HCI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(HCI,EVENT)) BT_PRINT_D("BT_HCI", fmt,## args);} #define HCI_TRACE_DEBUG(fmt, args...) {if (HCI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(HCI,DEBUG)) BT_PRINT_D("BT_HCI", fmt,## args);} -/* define traces for BLUFI */ -#define BLUFI_TRACE_ERROR(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BLUFI, ERROR)) BT_PRINT_E("BT_BLUFI", fmt, ## args);} -#define BLUFI_TRACE_WARNING(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BLUFI, WARNING)) BT_PRINT_W("BT_BLUFI", fmt, ## args);} -#define BLUFI_TRACE_API(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BLUFI,API)) BT_PRINT_I("BT_BLUFI", fmt, ## args);} -#define BLUFI_TRACE_EVENT(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BLUFI,EVENT)) BT_PRINT_D("BT_BLUFI", fmt, ## args);} -#define BLUFI_TRACE_DEBUG(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BLUFI,DEBUG)) BT_PRINT_D("BT_BLUFI", fmt, ## args);} -#define BLUFI_TRACE_VERBOSE(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BLUFI,VERBOSE)) BT_PRINT_V("BT_BLUFI", fmt, ## args);} - #else #define LOG_ERROR(fmt, args...) #define LOG_WARN(fmt, args...) @@ -518,13 +509,6 @@ extern UINT8 btif_trace_level; #define APPL_TRACE_DEBUG(fmt, args...) #define APPL_TRACE_VERBOSE(fmt, args...) -/* define traces for BLUFI */ -#define BLUFI_TRACE_ERROR(fmt, args...) -#define BLUFI_TRACE_WARNING(fmt, args...) -#define BLUFI_TRACE_API(fmt, args...) -#define BLUFI_TRACE_EVENT(fmt, args...) -#define BLUFI_TRACE_DEBUG(fmt, args...) -#define BLUFI_TRACE_VERBOSE(fmt, args...) #endif ///!UC_BT_STACK_NO_LOG diff --git a/components/bt/host/bluedroid/stack/include/stack/bt_types.h b/components/bt/host/bluedroid/stack/include/stack/bt_types.h index 17a94e1b3a..be803d50d2 100644 --- a/components/bt/host/bluedroid/stack/include/stack/bt_types.h +++ b/components/bt/host/bluedroid/stack/include/stack/bt_types.h @@ -24,15 +24,9 @@ #include "bt_common.h" #include "osi/semaphore.h" -typedef uint8_t UINT8; -typedef uint16_t UINT16; -typedef uint32_t UINT32; -typedef uint64_t UINT64; - typedef int8_t INT8; typedef int16_t INT16; typedef int32_t INT32; -typedef bool BOOLEAN; #define PACKED __packed // #define INLINE __inline @@ -304,8 +298,6 @@ typedef struct { /* Common Bluetooth field definitions */ -#define BD_ADDR_LEN 6 /* Device address length */ -typedef UINT8 BD_ADDR[BD_ADDR_LEN]; /* Device address */ typedef UINT8 *BD_ADDR_PTR; /* Pointer to Device Address */ #define AMP_KEY_TYPE_GAMP 0 @@ -422,23 +414,6 @@ typedef UINT8 ACCESS_CODE[ACCESS_CODE_BYTE_LEN]; #define BT_CLK_TO_MICROSECS(x) (((x) * 5000 + 3) / 8) #define BT_MICROSECS_TO_CLK(x) (((x) * 8 + 2499) / 5000) -/* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */ -#define MAX_UUID_SIZE 16 -typedef struct { -#define LEN_UUID_16 2 -#define LEN_UUID_32 4 -#define LEN_UUID_128 16 - - UINT16 len; - - union { - UINT16 uuid16; - UINT32 uuid32; - UINT8 uuid128[MAX_UUID_SIZE]; - } uu; - -} tBT_UUID; - #define BT_EIR_FLAGS_TYPE 0x01 #define BT_EIR_MORE_16BITS_UUID_TYPE 0x02 #define BT_EIR_COMPLETE_16BITS_UUID_TYPE 0x03 diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index b4d69a6d78..cd0b5c9d57 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -499,3 +499,10 @@ config BT_NIMBLE_USE_ESP_TIMER default y help Set this option to use Esp Timer which has higher priority timer instead of FreeRTOS timer + +config BT_NIMBLE_BLUFI_ENABLE + bool "Enable blufi functionality" + depends on BT_NIMBLE_ENABLED + default n + help + Set this option to enable blufi functionality. diff --git a/docs/doxygen/Doxyfile_common b/docs/doxygen/Doxyfile_common index 12314dda1c..97d621acb4 100644 --- a/docs/doxygen/Doxyfile_common +++ b/docs/doxygen/Doxyfile_common @@ -38,6 +38,7 @@ INPUT = \ $(IDF_PATH)/components/esp_event/include/esp_event_base.h \ $(IDF_PATH)/components/esp_event/include/esp_event_legacy.h \ $(IDF_PATH)/components/bt/include/esp32/include/esp_bt.h \ + $(IDF_PATH)/components/bt/common/api/include/api/esp_blufi_api.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_bt_defs.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_bt_main.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_bt_device.h \ @@ -45,7 +46,6 @@ INPUT = \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_gatt_defs.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h \ - $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_blufi_api.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_gap_bt_api.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_a2dp_api.h \ $(IDF_PATH)/components/bt/host/bluedroid/api/include/api/esp_avrc_api.h \ diff --git a/docs/en/api-reference/bluetooth/esp_blufi.rst b/docs/en/api-reference/bluetooth/esp_blufi.rst index 2eb2b2a6d0..13d422f81b 100644 --- a/docs/en/api-reference/bluetooth/esp_blufi.rst +++ b/docs/en/api-reference/bluetooth/esp_blufi.rst @@ -12,9 +12,9 @@ Use should concern these things: Application Example ------------------- -Check :example:`bluetooth/bluedroid/ble` folder in ESP-IDF examples, which contains the following application: +Check :example:`bluetooth` folder in ESP-IDF examples, which contains the following application: -* This is the BLUFI demo. This demo can set ESP32's wifi to softap/station/softap&station mode and config wifi connections - :example:`bluetooth/bluedroid/ble/blufi` +* This is the BLUFI demo. This demo can set ESP32's wifi to softap/station/softap&station mode and config wifi connections - :example:`bluetooth/blufi` API Reference ------------- diff --git a/docs/sphinx-known-warnings.txt b/docs/sphinx-known-warnings.txt index 8a40acadde..e7f9301603 100644 --- a/docs/sphinx-known-warnings.txt +++ b/docs/sphinx-known-warnings.txt @@ -28,7 +28,9 @@ README.rst:line: WARNING: Duplicate declaration, esp_err_t ulp_run(uint32_t entr # This seems like a bug, as the field are ::model_id and ::vnd::model_id esp_ble_mesh_defs.inc:line: WARNING: Duplicate declaration, uint16_t esp_ble_mesh_model::model_id - +WARNING:esp_bt_defs, use :noindex: for one of them +WARNING:esp_blufi, use :noindex: for one of them +esp_bt_defs.inc:line: WARNING: Duplicate declaration, uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN] # # Issue present only when building on msys2 / mingw32 START >>> diff --git a/examples/bluetooth/bluedroid/ble/blufi/CMakeLists.txt b/examples/bluetooth/blufi/CMakeLists.txt similarity index 100% rename from examples/bluetooth/bluedroid/ble/blufi/CMakeLists.txt rename to examples/bluetooth/blufi/CMakeLists.txt diff --git a/examples/bluetooth/bluedroid/ble/blufi/Makefile b/examples/bluetooth/blufi/Makefile similarity index 100% rename from examples/bluetooth/bluedroid/ble/blufi/Makefile rename to examples/bluetooth/blufi/Makefile diff --git a/examples/bluetooth/bluedroid/ble/blufi/README.md b/examples/bluetooth/blufi/README.md similarity index 94% rename from examples/bluetooth/bluedroid/ble/blufi/README.md rename to examples/bluetooth/blufi/README.md index f98d530809..d5416a29ea 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/README.md +++ b/examples/bluetooth/blufi/README.md @@ -10,7 +10,7 @@ To test this demo, you need to prepare a mobile phone with blufi application ins Blufi is completely open source, here is the download link: -* [blufi source code](https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/bluedroid/ble/blufi) +* [blufi source code](https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/blufi) * [BluFi protocol](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/blufi.html?highlight=blufi#the-frame-formats-defined-in-blufi) @@ -18,4 +18,4 @@ Blufi is completely open source, here is the download link: * [Android source code](https://github.com/EspressifApp/EspBlufi) -* [Bluetooth Network User Guide CN](https://www.espressif.com/sites/default/files/documentation/esp32_bluetooth_networking_user_guide_cn.pdf) \ No newline at end of file +* [Bluetooth Network User Guide CN](https://www.espressif.com/sites/default/files/documentation/esp32_bluetooth_networking_user_guide_cn.pdf) diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/CMakeLists.txt b/examples/bluetooth/blufi/main/CMakeLists.txt similarity index 75% rename from examples/bluetooth/bluedroid/ble/blufi/main/CMakeLists.txt rename to examples/bluetooth/blufi/main/CMakeLists.txt index e4bc55caf1..a8b056e73a 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/CMakeLists.txt +++ b/examples/bluetooth/blufi/main/CMakeLists.txt @@ -1,3 +1,4 @@ idf_component_register(SRCS "blufi_example_main.c" "blufi_security.c" + "blufi_init.c" INCLUDE_DIRS ".") diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example.h b/examples/bluetooth/blufi/main/blufi_example.h similarity index 88% rename from examples/bluetooth/bluedroid/ble/blufi/main/blufi_example.h rename to examples/bluetooth/blufi/main/blufi_example.h index 3a1c72a1df..d8b92f5d9e 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example.h +++ b/examples/bluetooth/blufi/main/blufi_example.h @@ -11,3 +11,5 @@ uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len); int blufi_security_init(void); void blufi_security_deinit(void); +int esp_blufi_gap_register_callback(void); +esp_err_t esp_blufi_host_init(void); diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c similarity index 84% rename from examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c rename to examples/bluetooth/blufi/main/blufi_example_main.c index 816fe5e425..67ff42cb29 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/blufi/main/blufi_example_main.c @@ -28,49 +28,12 @@ #include "esp_bt.h" #include "esp_blufi_api.h" -#include "esp_bt_defs.h" -#include "esp_gap_ble_api.h" -#include "esp_bt_main.h" -#include "esp_bt_device.h" #include "blufi_example.h" +#include "esp_blufi.h" + static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param); -#define BLUFI_DEVICE_NAME "BLUFI_DEVICE" -static uint8_t example_service_uuid128[32] = { - /* LSB <--------------------------------------------------------------------------------> MSB */ - //first uuid, 16bit, [12],[13] is the value - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, -}; - -//static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x12, 0x23, 0x45, 0x56}; -static esp_ble_adv_data_t example_adv_data = { - .set_scan_rsp = false, - .include_name = true, - .include_txpower = true, - .min_interval = 0x0006, //slave connection min interval, Time = min_interval * 1.25 msec - .max_interval = 0x0010, //slave connection max interval, Time = max_interval * 1.25 msec - .appearance = 0x00, - .manufacturer_len = 0, - .p_manufacturer_data = NULL, - .service_data_len = 0, - .p_service_data = NULL, - .service_uuid_len = 16, - .p_service_uuid = example_service_uuid128, - .flag = 0x6, -}; - -static esp_ble_adv_params_t example_adv_params = { - .adv_int_min = 0x100, - .adv_int_max = 0x100, - .adv_type = ADV_TYPE_IND, - .own_addr_type = BLE_ADDR_TYPE_PUBLIC, - //.peer_addr = - //.peer_addr_type = - .channel_map = ADV_CHNL_ALL, - .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, -}; - #define WIFI_LIST_NUM 10 static wifi_config_t sta_config; @@ -91,10 +54,6 @@ static uint8_t gl_sta_bssid[6]; static uint8_t gl_sta_ssid[32]; static int gl_sta_ssid_len; -/* connect infor*/ -static uint8_t server_if; -static uint16_t conn_id; - static void ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { @@ -242,8 +201,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para case ESP_BLUFI_EVENT_INIT_FINISH: BLUFI_INFO("BLUFI init finish\n"); - esp_ble_gap_set_device_name(BLUFI_DEVICE_NAME); - esp_ble_gap_config_adv_data(&example_adv_data); + esp_blufi_adv_start(); break; case ESP_BLUFI_EVENT_DEINIT_FINISH: BLUFI_INFO("BLUFI deinit finish\n"); @@ -251,16 +209,14 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para case ESP_BLUFI_EVENT_BLE_CONNECT: BLUFI_INFO("BLUFI ble connect\n"); ble_is_connected = true; - server_if = param->connect.server_if; - conn_id = param->connect.conn_id; - esp_ble_gap_stop_advertising(); + esp_blufi_adv_stop(); blufi_security_init(); break; case ESP_BLUFI_EVENT_BLE_DISCONNECT: BLUFI_INFO("BLUFI ble disconnect\n"); ble_is_connected = false; blufi_security_deinit(); - esp_ble_gap_start_advertising(&example_adv_params); + esp_blufi_adv_start(); break; case ESP_BLUFI_EVENT_SET_WIFI_OPMODE: BLUFI_INFO("BLUFI Set WIFI opmode %d\n", param->wifi_mode.op_mode); @@ -304,7 +260,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para } case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE: BLUFI_INFO("blufi close a gatt connection"); - esp_blufi_close(server_if, conn_id); + esp_blufi_disconnect(); break; case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA: /* TODO */ @@ -401,17 +357,6 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para } } -static void example_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) -{ - switch (event) { - case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: - esp_ble_gap_start_advertising(&example_adv_params); - break; - default: - break; - } -} - void app_main(void) { esp_err_t ret; @@ -440,33 +385,23 @@ void app_main(void) return; } - ret = esp_bluedroid_init(); + ret = esp_blufi_host_init(); if (ret) { - BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); + BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret)); return; } - ret = esp_bluedroid_enable(); - if (ret) { - BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); - return; - } - - BLUFI_INFO("BD ADDR: "ESP_BD_ADDR_STR"\n", ESP_BD_ADDR_HEX(esp_bt_dev_get_address())); - BLUFI_INFO("BLUFI VERSION %04x\n", esp_blufi_get_version()); - ret = esp_ble_gap_register_callback(example_gap_event_handler); - if(ret){ - BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret); - return; - } - ret = esp_blufi_register_callbacks(&example_callbacks); if(ret){ BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret); return; } - esp_blufi_profile_init(); + ret = esp_blufi_gap_register_callback(); + if(ret){ + BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret); + return; + } } diff --git a/examples/bluetooth/blufi/main/blufi_init.c b/examples/bluetooth/blufi/main/blufi_init.c new file mode 100644 index 0000000000..052739b055 --- /dev/null +++ b/examples/bluetooth/blufi/main/blufi_init.c @@ -0,0 +1,128 @@ +#include +#include "esp_err.h" +#include "esp_blufi_api.h" +#include "esp_log.h" +#include "esp_blufi.h" +#ifdef CONFIG_BT_BLUEDROID_ENABLED +#include "esp_bt.h" +#include "esp_bt_main.h" +#include "blufi_example.h" +#include "esp_bt_device.h" +#endif + +#ifdef CONFIG_BT_NIMBLE_ENABLED +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "services/gap/ble_svc_gap.h" +#include "services/gatt/ble_svc_gatt.h" +#include "console/console.h" +#endif + +#ifdef CONFIG_BT_BLUEDROID_ENABLED +esp_err_t esp_blufi_host_init(void) +{ + int ret; + ret = esp_bluedroid_init(); + if (ret) { + BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); + return ESP_FAIL; + } + + ret = esp_bluedroid_enable(); + if (ret) { + BLUFI_ERROR("%s init bluedroid failed: %s\n", __func__, esp_err_to_name(ret)); + return ESP_FAIL; + } + BLUFI_INFO("BD ADDR: "ESP_BD_ADDR_STR"\n", ESP_BD_ADDR_HEX(esp_bt_dev_get_address())); + + return ESP_OK; + +} + +int esp_blufi_gap_register_callback(void) +{ + int rc; + rc = esp_ble_gap_register_callback(esp_blufi_gap_event_handler); + if(rc){ + return rc; + } + esp_blufi_profile_init(); + return 0; +} + +#endif /* CONFIG_BT_BLUEDROID_ENABLED */ + +#ifdef CONFIG_BT_NIMBLE_ENABLED +void ble_store_config_init(void); +static void blufi_on_reset(int reason) +{ + MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason); +} + +static void +blufi_on_sync(void) +{ + esp_blufi_profile_init(); +} + +void bleprph_host_task(void *param) +{ + ESP_LOGI("BLUFI_EXAMPLE", "BLE Host Task Started"); + /* This function will return only when nimble_port_stop() is executed */ + nimble_port_run(); + + nimble_port_freertos_deinit(); +} + +esp_err_t esp_blufi_host_init(void) +{ + ESP_ERROR_CHECK(esp_nimble_hci_init()); + nimble_port_init(); +/* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = blufi_on_reset; + ble_hs_cfg.sync_cb = blufi_on_sync; + ble_hs_cfg.gatts_register_cb = esp_blufi_gatt_svr_register_cb; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + ble_hs_cfg.sm_io_cap = 4; +#ifdef CONFIG_EXAMPLE_BONDING + ble_hs_cfg.sm_bonding = 1; +#endif +#ifdef CONFIG_EXAMPLE_MITM + ble_hs_cfg.sm_mitm = 1; +#endif +#ifdef CONFIG_EXAMPLE_USE_SC + ble_hs_cfg.sm_sc = 1; +#else + ble_hs_cfg.sm_sc = 0; +#ifdef CONFIG_EXAMPLE_BONDING + ble_hs_cfg.sm_our_key_dist = 1; + ble_hs_cfg.sm_their_key_dist = 1; +#endif +#endif + + int rc; + rc = esp_blufi_gatt_svr_init(); + assert(rc == 0); + + /* Set the default device name. */ + rc = ble_svc_gap_device_name_set(BLUFI_DEVICE_NAME); + assert(rc == 0); + + /* XXX Need to have template for store */ + ble_store_config_init(); + + nimble_port_freertos_init(bleprph_host_task); + esp_blufi_btc_init(); + return ESP_OK; +} + +int esp_blufi_gap_register_callback(void) +{ + return 0; +} + +#endif /* CONFIG_BT_NIMBLE_ENABLED */ diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_security.c b/examples/bluetooth/blufi/main/blufi_security.c similarity index 98% rename from examples/bluetooth/bluedroid/ble/blufi/main/blufi_security.c rename to examples/bluetooth/blufi/main/blufi_security.c index 635d5f00a2..c5286ca700 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_security.c +++ b/examples/bluetooth/blufi/main/blufi_security.c @@ -20,9 +20,6 @@ #include "esp_bt.h" #include "esp_blufi_api.h" -#include "esp_bt_defs.h" -#include "esp_gap_ble_api.h" -#include "esp_bt_main.h" #include "blufi_example.h" #include "mbedtls/aes.h" diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/component.mk b/examples/bluetooth/blufi/main/component.mk similarity index 100% rename from examples/bluetooth/bluedroid/ble/blufi/main/component.mk rename to examples/bluetooth/blufi/main/component.mk diff --git a/examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults b/examples/bluetooth/blufi/sdkconfig.defaults similarity index 100% rename from examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults rename to examples/bluetooth/blufi/sdkconfig.defaults diff --git a/examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults.esp32 b/examples/bluetooth/blufi/sdkconfig.defaults.esp32 similarity index 100% rename from examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults.esp32 rename to examples/bluetooth/blufi/sdkconfig.defaults.esp32 diff --git a/examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults.esp32c3 b/examples/bluetooth/blufi/sdkconfig.defaults.esp32c3 similarity index 100% rename from examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults.esp32c3 rename to examples/bluetooth/blufi/sdkconfig.defaults.esp32c3 diff --git a/examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults.esp32s3 b/examples/bluetooth/blufi/sdkconfig.defaults.esp32s3 similarity index 100% rename from examples/bluetooth/bluedroid/ble/blufi/sdkconfig.defaults.esp32s3 rename to examples/bluetooth/blufi/sdkconfig.defaults.esp32s3