From 24af07fd13e69f1e82620b5608feb14dc50283f9 Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Wed, 28 Dec 2016 12:02:43 +0800 Subject: [PATCH] component/bt : new blufi 1. new blufi protocol 2. new blufi demo 3. support security 4. support sta/ap/sta_ap 5. support wpa-enterprise --- components/bt/bluedroid/api/esp_blufi_api.c | 20 +- .../bt/bluedroid/api/include/esp_blufi_api.h | 276 ++++- components/bt/bluedroid/bta/dm/bta_dm_api.c | 4 +- .../btc/profile/esp/blufi/blufi_adv.c | 164 --- .../btc/profile/esp/blufi/blufi_prf.c | 940 ++++++++++++++---- .../btc/profile/esp/blufi/blufi_protocol.c | 240 +++++ .../btc/profile/esp/blufi/include/blufi_adv.h | 39 - .../btc/profile/esp/blufi/include/blufi_int.h | 172 +++- .../btc/profile/esp/include/btc_blufi_prf.h | 30 +- .../btc/profile/std/gap/btc_gap_ble.c | 4 +- .../bt/bluedroid/hci/packet_fragmenter.c | 16 +- components/bt/bluedroid/include/bt_trace.h | 13 +- docs/api/bt_le.rst | 1 + docs/api/esp_blufi.rst | 128 +++ examples/12_blufi/components/blufi/blufi.c | 132 --- .../12_blufi/components/blufi/blufi_task.c | 110 -- .../12_blufi/components/blufi/component.mk | 13 - .../12_blufi/components/blufi/include/blufi.h | 39 - examples/12_blufi/main/blufi_demo.h | 17 + examples/12_blufi/main/blufi_main.c | 336 +++++++ examples/12_blufi/main/blufi_security.c | 205 ++++ examples/12_blufi/main/demo_main.c | 144 --- 22 files changed, 2090 insertions(+), 953 deletions(-) delete mode 100644 components/bt/bluedroid/btc/profile/esp/blufi/blufi_adv.c create mode 100644 components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c delete mode 100644 components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_adv.h create mode 100644 docs/api/esp_blufi.rst delete mode 100644 examples/12_blufi/components/blufi/blufi.c delete mode 100644 examples/12_blufi/components/blufi/blufi_task.c delete mode 100644 examples/12_blufi/components/blufi/component.mk delete mode 100644 examples/12_blufi/components/blufi/include/blufi.h create mode 100644 examples/12_blufi/main/blufi_demo.h create mode 100644 examples/12_blufi/main/blufi_main.c create mode 100644 examples/12_blufi/main/blufi_security.c delete mode 100644 examples/12_blufi/main/demo_main.c diff --git a/components/bt/bluedroid/api/esp_blufi_api.c b/components/bt/bluedroid/api/esp_blufi_api.c index 2697f2cbf..5493824d2 100644 --- a/components/bt/bluedroid/api/esp_blufi_api.c +++ b/components/bt/bluedroid/api/esp_blufi_api.c @@ -22,22 +22,30 @@ #include "btc_main.h" #include "future.h" -esp_err_t esp_blufi_register_callback(esp_blufi_cb_t callback) +esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks) { - return (btc_profile_cb_set(BTC_PID_BLUFI, callback) == 0 ? ESP_OK : ESP_FAIL); + if (callbacks == NULL) { + return ESP_FAIL; + } + + btc_blufi_set_callbacks(callbacks); + return (btc_profile_cb_set(BTC_PID_BLUFI, callbacks->event_cb) == 0 ? ESP_OK : ESP_FAIL); } -esp_err_t esp_blufi_send_config_state(esp_blufi_config_state_t state) +esp_err_t esp_blufi_send_wifi_conn_report(wifi_mode_t opmode, esp_blufi_sta_conn_state_t sta_conn_state, uint8_t softap_conn_num, esp_blufi_extra_info_t *extra_info) { btc_msg_t msg; btc_blufi_args_t arg; msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_BLUFI; - msg.act = BTC_BLUFI_ACT_SEND_CFG_STATE; - arg.cfg_state.state = state; + msg.act = BTC_BLUFI_ACT_SEND_CFG_REPORT; + arg.wifi_conn_report.opmode = opmode; + arg.wifi_conn_report.sta_conn_state = sta_conn_state; + arg.wifi_conn_report.softap_conn_num = softap_conn_num; + arg.wifi_conn_report.extra_info = extra_info; - return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); + return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), btc_blufi_call_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } diff --git a/components/bt/bluedroid/api/include/esp_blufi_api.h b/components/bt/bluedroid/api/include/esp_blufi_api.h index 2644bd524..5490fec05 100644 --- a/components/bt/bluedroid/api/include/esp_blufi_api.h +++ b/components/bt/bluedroid/api/include/esp_blufi_api.h @@ -18,24 +18,44 @@ #include "esp_bt_defs.h" #include "esp_gatt_defs.h" #include "esp_err.h" +#include "esp_wifi_types.h" #ifdef __cplusplus extern "C" { #endif -#define ESP_BLUFI_RECV_DATA_LEN_MAX (64+1) - typedef enum { - ESP_BLUFI_EVENT_INIT_FINISH = 0, - ESP_BLUFI_EVENT_DEINIT_FINISH = 1, - ESP_BLUFI_EVENT_RECV_DATA = 2, + ESP_BLUFI_EVENT_INIT_FINISH = 0, /*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); -} - - -/******************************************************************************* -** -** Function BLUFI_BleSetScanRsp -** -** Description This function is called to override the app scan response. -** -** Parameters Pointer to User defined ADV data structure -** -** Returns None -** -*******************************************************************************/ -void BlufiBleSetScanRsp(tBLUFI_BLE_ADV_DATA *scan_rsp_data, - tBTA_SET_ADV_DATA_CMPL_CBACK *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); -} - - diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c index d6e23e63a..d48035045 100644 --- a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c +++ b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c @@ -31,255 +31,240 @@ #include "btc_task.h" #include "btc_manage.h" -#include "blufi_adv.h" #include "blufi_int.h" -const char success_msg[] = "BLUFI_CONFIG_OK"; -const char failed_msg[] = "BLUFI_CONFIG_FAILED"; +#include "esp_blufi_api.h" -#define BTC_BLUFI_CB_TO_APP(event, param) ((esp_blufi_cb_t)btc_profile_cb_get(BTC_PID_BLUFI))((event), (param)) +#define BTC_BLUFI_CB_TO_APP(event, param) ((esp_blufi_event_cb_t)btc_profile_cb_get(BTC_PID_BLUFI))((event), (param)) #define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x" #define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] +//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 -UINT16 esp32_uuid = SVC_BLUFI_UUID; -UINT8 esp32_manu[17] = {0xff, 0x20, 0x14, 0x07, 0x22, 0x00, 0x02, 0x5B, 0x00, 0x33, 0x49, 0x31, 0x30, 0x4a, 0x30, 0x30, 0x31}; -tBTA_BLE_MANU p_esp32_manu = {sizeof(esp32_manu), esp32_manu}; /* manufacturer data */ +#define BLUFI_HDL_NUM 6 -tBTA_BLE_SERVICE esp32_service = { - 0x01, //only one service in the ijiazu button profile - false, - &esp32_uuid -}; /* 16 bits services */ +tBLUFI_ENV blufi_env; +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}}; -tBLUFI_BLE_ADV_DATA esp32_adv_data[ADV_SCAN_IDX_MAX] = { - [BLE_ADV_DATA_IDX] = { - .adv_name = "Espressif_008", - { - {0, 0}, - NULL, //no manufature data to be setting in the esp32 adervetisiing datas - &esp32_service, - NULL, //the 128 bits service uuid set to null(not used) - NULL, //the 32 bits Service UUID set to null(not used) - NULL, //16 bits services Solicitation UUIDs set to null(not used) - NULL, //List of 32 bit Service Solicitation UUIDs set to null(not used) - NULL, //List of 128 bit Service Solicitation UUIDs set to null(not used) - NULL, //proprietary data set to null(not used) - NULL, //service data set not null(no service data to be sent) - 0x0200, //device type : generic display - BTA_DM_GENERAL_DISC, // General discoverable. - 0xFE //the tx power value,defult value is 0 - }, - }, - - [BLE_SCAN_RSP_DATA_IDX] = { - .adv_name = NULL, - { - {0, 0}, - &p_esp32_manu, - NULL, - NULL, //the 128 bits service uuid set to null(not used) - NULL, //the 32 bits Service UUID set to null(not used) - NULL, //16 bits services Solicitation UUIDs set to null(not used) - NULL, //List of 32 bit Service Solicitation UUIDs set to null(not used) - NULL, //List of 128 bit Service Solicitation UUIDs set to null(not used) - NULL, //proprietary data set to null(not used) - NULL, //service data set not null(no service data to be sent) - 0x0000, //device type : generic display - 0x00, // General discoverable. - 0x00 - }, //the tx power value,defult value is 0 - } -}; - - - -static tBLUFI_CB_ENV blufi_cb_env; - - - -/***************************************************************************** -** Constants -*****************************************************************************/ +// 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); - -/******************************************************************************* -** -** Function blufi_create_service -** -** Description Create a Service for the blufi profile -** -** Returns NULL -** -*******************************************************************************/ static void blufi_create_service(void) { - tBTA_GATTS_IF server_if ; - tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; - UINT16 num_handle = BLUFI_HDL_NUM; - UINT8 inst = 0x00; - server_if = blufi_cb_env.gatt_if; - blufi_cb_env.inst_id = inst; - if (!blufi_cb_env.enabled) { + if (!blufi_env.enabled) { LOG_ERROR("blufi service added error."); return; } - BTA_GATTS_CreateService(server_if, &uuid, inst, num_handle, true); + blufi_env.srvc_inst = 0x00; + BTA_GATTS_CreateService(blufi_env.gatt_if, &blufi_srvc_uuid, blufi_env.srvc_inst, BLUFI_HDL_NUM, true); } - -/******************************************************************************* -** -** Function blufi_profile_cb -** -** Description the callback function after the profile has been register to the BTA manager module -** -** Returns NULL -** -*******************************************************************************/ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) { tBTA_GATTS_RSP rsp; - tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; - UINT8 *p_rec_data = NULL; - tBTA_GATT_STATUS status; - tBTA_DM_DISC disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE; - tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE; LOG_DEBUG("blufi profile cb event = %x\n", event); switch (event) { case BTA_GATTS_REG_EVT: - status = p_data->reg_oper.status; + LOG_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); - 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) { - LOG_ERROR("blufi profile register failed\n"); + LOG_ERROR("BLUFI profile register failed\n"); return; } - blufi_cb_env.gatt_if = p_data->reg_oper.server_if; - blufi_cb_env.enabled = true; - LOG_DEBUG("register complete: event=%d, status=%d, server_if=%d\n", - event, status, blufi_cb_env.gatt_if); - - LOG_DEBUG("set advertising parameters\n"); - //set connectable,discoverable, pairable and paired only modes of local device - BTA_DmSetVisibility(disc_mode, conn_mode, (uint8_t)BTA_DM_NON_PAIRABLE, (uint8_t)BTA_DM_CONN_ALL); - //set the advertising data to the btm layer - 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); + 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 == SVC_BLUFI_UUID) { + if (p_data->reg_oper.uuid.uu.uuid16 == BLUFI_APP_UUID) { + LOG_DEBUG("%s %d\n", __func__, __LINE__); blufi_create_service(); } 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 = 2; - //rsp.attr_value.value[0] = 0xde; - //rsp.attr_value.value[1] = 0xed; - //rsp.attr_value.value[2] = 0xbe; - //rsp.attr_value.value[3] = 0xef; + 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: - BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, + 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 = GKI_getbuf(BLUFI_PREPAIR_BUF_MAX_SIZE); + if (blufi_env.prepare_buf == NULL) { + LOG_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); + + LOG_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); + + 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 { + LOG_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); - - LOG_DEBUG("Received blufi data:"); - for (int i = 0; i < p_data->req_data.p_data->write_req.len; i++) { - LOG_DEBUG("%x", p_data->req_data.p_data->write_req.value[i]); } - LOG_DEBUG("\n"); - - if (p_data->req_data.p_data->write_req.handle == blufi_cb_env.blufi_inst.blufi_hdl) { - btc_msg_t msg; - struct blufi_recv_evt_param recv_data; - - memset(&recv_data, 0x00, sizeof(struct blufi_recv_evt_param)); - - p_rec_data = &p_data->req_data.p_data->write_req.value[0]; - recv_data.data_len = p_data->req_data.p_data->write_req.len; - memcpy(recv_data.data, p_rec_data, recv_data.data_len); - - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_BLUFI; - msg.act = BTC_BLUFI_CB_ACT_RECV_DATA; - - btc_transfer_context(&msg, &recv_data, sizeof(struct blufi_recv_evt_param), NULL); + + 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: + LOG_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 (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) { + GKI_freebuf(blufi_env.prepare_buf); + blufi_env.prepare_buf = NULL; + } + break; case BTA_GATTS_CONF_EVT: + LOG_DEBUG("CONIRM EVT\n"); /* Nothing */ break; case BTA_GATTS_CREATE_EVT: - uuid.uu.uuid16 = CHAR_BLUFI_UUID; - blufi_cb_env.cur_srvc_id = p_data->create.service_id; - blufi_cb_env.is_primery = p_data->create.is_primary; - //start the blufi service after created - BTA_GATTS_StartService(p_data->create.service_id, BTA_GATT_TRANSPORT_LE); + blufi_env.handle_srvc = p_data->create.service_id; + //add the frist blufi characteristic --> write characteristic - BTA_GATTS_AddCharacteristic(blufi_cb_env.cur_srvc_id, &uuid, - (GATT_PERM_WRITE | GATT_PERM_READ), - (GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE | GATT_CHAR_PROP_BIT_NOTIFY)); + BTA_GATTS_AddCharacteristic(blufi_env.handle_srvc, &blufi_char_uuid_p2e, + (GATT_PERM_WRITE), + (GATT_CHAR_PROP_BIT_WRITE)); break; case BTA_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; + 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; - uuid.uu.uuid16 = GATT_UUID_CHAR_CLIENT_CONFIG; - BTA_GATTS_AddCharDescriptor (blufi_cb_env.cur_srvc_id, - (GATT_PERM_WRITE | GATT_PERM_WRITE), - &uuid); + BTA_GATTS_AddCharacteristic(blufi_env.handle_srvc, &blufi_char_uuid_e2p, + (GATT_PERM_READ), + (GATT_PERM_READ | GATT_CHAR_PROP_BIT_NOTIFY)); + 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); + 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 = BTC_BLUFI_CB_ACT_INIT_FINISH; - btc_transfer_context(&msg, NULL, 0, NULL); + 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: + case BTA_GATTS_CONNECT_EVT: { + btc_msg_t msg; + esp_blufi_cb_param_t param; + //set the connection flag to true - LOG_ERROR("\ndevice is connected "BT_BD_ADDR_STR", server_if=%d,reason=0x%x,connect_id=%d\n", + LOG_INFO("\ndevice is connected "BT_BD_ADDR_STR", server_if=%d,reason=0x%x,connect_id=%d\n", BT_BD_ADDR_HEX(p_data->conn.remote_bda), p_data->conn.server_if, p_data->conn.reason, p_data->conn.conn_id); - blufi_cb_env.conn_id = p_data->conn.conn_id; + 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; - /*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 + 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)); + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); break; - case BTA_GATTS_DISCONNECT_EVT: + } + 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_cb_env.connected = false; + LOG_INFO("\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.is_connected = false; + 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_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: - if (blufi_cb_env.connected && (blufi_cb_env.conn_id == p_data->conn.conn_id)) { - //set the connection channal congested flag to true - blufi_cb_env.congest = p_data->congest.congested; - } - break; - case BTA_GATTS_LISTEN_EVT: break; case BTA_GATTS_CONGEST_EVT: break; @@ -290,72 +275,594 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) static tGATT_STATUS btc_blufi_profile_init(void) { - tBT_UUID app_uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}}; + esp_blufi_callbacks_t *store_p = blufi_env.cbs; - - if (blufi_cb_env.enabled) { - LOG_ERROR("blufi svc already initaliezd"); + if (blufi_env.enabled) { + LOG_ERROR("BLUFI already initialized"); return GATT_ERROR; - } else { - memset(&blufi_cb_env, 0, sizeof(tBLUFI_CB_ENV)); } + memset(&blufi_env, 0x0, sizeof(blufi_env)); + blufi_env.cbs = store_p; /* if set callback prior, restore the point */ - /* register the blufi profile to the BTA_GATTS module*/ - BTA_GATTS_AppRegister(&app_uuid, blufi_profile_cb); + /* register the BLUFI profile to the BTA_GATTS module*/ + BTA_GATTS_AppRegister(&blufi_app_uuid, blufi_profile_cb); return GATT_SUCCESS; } -static void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len) +static tGATT_STATUS btc_blufi_profile_deinit(void) { - BOOLEAN conn_status = blufi_cb_env.connected; - UINT16 conn_id = blufi_cb_env.conn_id; - UINT16 attr_id = blufi_cb_env.blufi_inst.blufi_hdl; - //notify rsp==false; indicate rsp==true. - BOOLEAN rsp = false; - if (!conn_status && blufi_cb_env.congest) { - LOG_ERROR("the conneciton for blufi profile has been loss"); + esp_blufi_cb_param_t param; + btc_msg_t msg; + + if (!blufi_env.enabled) { + LOG_ERROR("BLUFI already initialized"); + return GATT_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); + + 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); + + return GATT_SUCCESS; +} + +static 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); +} + +static void btc_blufi_recv_handler(uint8_t *data, int len) +{ + struct blufi_hdr *hdr = (struct blufi_hdr *)data; + uint16_t checksum; + int ret; + + if (hdr->seq != blufi_env.recv_seq) { + LOG_ERROR("%s seq %d is not expect %d\n", __func__, hdr->seq, blufi_env.recv_seq + 1); return; } - BTA_GATTS_HandleValueIndication (conn_id, attr_id, len, - blufi_msg, rsp); + blufi_env.recv_seq++; + + // first step, decrypt + if (BLUFI_FC_IS_ENC(hdr->fc) + && (blufi_env.cbs && blufi_env.cbs->decrypt_func)) { + ret = blufi_env.cbs->decrypt_func(hdr->seq, hdr->data, hdr->data_len); + if (ret != hdr->data_len) { /* enc must be success and enc len must equal to plain len */ + LOG_ERROR("%s decrypt error %d\n", __func__, ret); + return; + } + } + + // second step, check sum + if (BLUFI_FC_IS_CHECK(hdr->fc) + && (blufi_env.cbs && blufi_env.cbs->checksum_func)) { + checksum = blufi_env.cbs->checksum_func(hdr->seq, &hdr->seq, hdr->data_len + 2); + if (memcmp(&checksum, &hdr->data[hdr->data_len], 2) != 0) { + LOG_ERROR("%s checksum error %04x, pkt %04x\n", __func__, checksum, *(uint16_t *)&hdr->data[hdr->data_len]); + return; + } + } + + if (BLUFI_FC_IS_REQ_ACK(hdr->fc)) { + btc_blufi_send_ack(hdr->seq); + } + + if (BLUFI_FC_IS_FRAG(hdr->fc)) { + if (blufi_env.offset == 0) { + blufi_env.total_len = *(uint16_t *)(hdr->data); + blufi_env.aggr_buf = GKI_getbuf(blufi_env.total_len); + if (blufi_env.aggr_buf == NULL) { + LOG_ERROR("%s no mem, len %d\n", __func__, blufi_env.total_len); + return; + } + } + memcpy(blufi_env.aggr_buf + blufi_env.offset, hdr->data + 2, hdr->data_len - 2); + blufi_env.offset += (hdr->data_len - 2); + } else { + if (blufi_env.offset > 0) { /* if previous pkt is frag */ + memcpy(blufi_env.aggr_buf + blufi_env.offset, hdr->data, hdr->data_len); + + btc_blufi_protocol_handler(hdr->type, blufi_env.aggr_buf, blufi_env.total_len); + blufi_env.offset = 0; + GKI_freebuf(blufi_env.aggr_buf); + blufi_env.aggr_buf = NULL; + } else { + btc_blufi_protocol_handler(hdr->type, hdr->data, hdr->data_len); + blufi_env.offset = 0; + } + } +} +void btc_blufi_send_encap(uint8_t type, uint8_t *data, int total_data_len) +{ + struct blufi_hdr *hdr = NULL; + int remain_len = total_data_len; + uint16_t checksum; + int ret; + + while (remain_len > 0) { + if (remain_len > BLUFI_FRAG_DATA_MAX_LEN) { + hdr = GKI_getbuf(sizeof(struct blufi_hdr) + 2 + BLUFI_FRAG_DATA_MAX_LEN + 2); + if (hdr == NULL) { + LOG_ERROR("%s no mem\n", __func__); + return; + } + hdr->fc = 0x0; + hdr->data_len = BLUFI_FRAG_DATA_MAX_LEN + 2; + *(uint16_t *)hdr->data = remain_len; + memcpy(hdr->data + 2, &data[total_data_len - remain_len], BLUFI_FRAG_DATA_MAX_LEN); //copy first, easy for check sum + hdr->fc |= BLUFI_FC_FRAG; + } else { + hdr = GKI_getbuf(sizeof(struct blufi_hdr) + remain_len + 2); + if (hdr == NULL) { + LOG_ERROR("%s no mem\n", __func__); + return; + } + hdr->fc = 0x0; + hdr->data_len = remain_len; + memcpy(hdr->data, &data[total_data_len - remain_len], hdr->data_len); //copy first, easy for check sum + } + + hdr->type = type; + hdr->fc |= BLUFI_FC_DIR_E2P; + hdr->seq = blufi_env.send_seq++; + + if (BLUFI_TYPE_IS_CTRL(hdr->type)) { + if ((blufi_env.sec_mode & BLUFI_CTRL_SEC_MODE_CHECK_MASK) + && (blufi_env.cbs && blufi_env.cbs->checksum_func)) { + hdr->fc |= BLUFI_FC_CHECK; + checksum = blufi_env.cbs->checksum_func(hdr->seq, &hdr->seq, hdr->data_len + 2); + memcpy(&hdr->data[hdr->data_len], &checksum, 2); + } + } else if (!BLUFI_TYPE_IS_DATA_NEG(hdr->type)) { + if ((blufi_env.sec_mode & BLUFI_DATA_SEC_MODE_CHECK_MASK) + && (blufi_env.cbs && blufi_env.cbs->checksum_func)) { + hdr->fc |= BLUFI_FC_CHECK; + checksum = blufi_env.cbs->checksum_func(hdr->seq, &hdr->seq, hdr->data_len + 2); + memcpy(&hdr->data[hdr->data_len], &checksum, 2); + } + + if ((blufi_env.sec_mode & BLUFI_DATA_SEC_MODE_ENC_MASK) + && (blufi_env.cbs && blufi_env.cbs->encrypt_func)) { + ret = blufi_env.cbs->encrypt_func(hdr->seq, hdr->data, hdr->data_len); + if (ret == hdr->data_len) { /* enc must be success and enc len must equal to plain len */ + hdr->fc |= BLUFI_FC_ENC; + } else { + LOG_ERROR("%s encrypt error %d\n", __func__, ret); + GKI_freebuf(hdr); + return; + } + } + } + + if (hdr->fc & BLUFI_FC_FRAG) { + remain_len -= (hdr->data_len - 2); + } else { + remain_len -= hdr->data_len; + } + + 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))); + + GKI_freebuf(hdr); + hdr = NULL; + } } -static void btc_blufi_config_success(void) +static void btc_blufi_wifi_conn_report(uint8_t opmode, uint8_t sta_conn_state, uint8_t softap_conn_num, esp_blufi_extra_info_t *info, int info_len) { - LOG_DEBUG("config success\n"); - blufi_msg_notify((uint8_t *)success_msg, strlen(success_msg)); + uint8_t type; + uint8_t *data; + int data_len; + uint8_t *p; + + data_len = info_len + 3; + p = data = GKI_getbuf(data_len); + if (data == NULL) { + return; + } + + type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_WIFI_REP); + *p++ = opmode; + *p++ = sta_conn_state; + *p++ = softap_conn_num; + + if (info) { + if (info->sta_bssid_set) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID; + *p++ = 6; + memcpy(p, info->sta_bssid, 6); + p += 6; + } + if (info->sta_ssid) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_SSID; + *p++ = info->sta_ssid_len; + memcpy(p, info->sta_ssid, info->sta_ssid_len); + p += info->sta_ssid_len; + } + if (info->sta_passwd) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD; + *p++ = info->sta_passwd_len; + memcpy(p, info->sta_passwd, info->sta_passwd_len); + p += info->sta_passwd_len; + } + if (info->softap_ssid) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID; + *p++ = info->softap_ssid_len; + memcpy(p, info->softap_ssid, info->softap_ssid_len); + p += info->softap_ssid_len; + } + if (info->softap_passwd) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD; + *p++ = info->softap_passwd_len; + memcpy(p, info->softap_passwd, info->softap_passwd_len); + p += info->softap_passwd_len; + } + if (info->softap_authmode_set) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE; + *p++ = 1; + *p++ = info->softap_authmode; + } + if (info->softap_max_conn_num_set) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM; + *p++ = 1; + *p++ = info->softap_max_conn_num; + } + if (info->softap_channel_set) { + *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM; + *p++ = 1; + *p++ = info->softap_channel; + } + } + if (p - data > data_len) { + LOG_ERROR("%s len error %d %d\n", __func__, (int)(p - data), data_len); + } + + btc_blufi_send_encap(type, data, data_len); + GKI_freebuf(data); } -static void btc_blufi_config_failed(void) +static void btc_blufi_send_ack(uint8_t seq) { - LOG_DEBUG("config faield\n"); - blufi_msg_notify((uint8_t *)failed_msg, strlen(failed_msg)); + uint8_t type; + uint8_t data; + + type = BLUFI_BUILD_TYPE(BLUFI_TYPE_CTRL, BLUFI_TYPE_CTRL_SUBTYPE_ACK); + data = seq; + + btc_blufi_send_encap(type, &data, 1); +} + +void btc_blufi_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_blufi_cb_param_t *dst = (esp_blufi_cb_param_t *) p_dest; + esp_blufi_cb_param_t *src = (esp_blufi_cb_param_t *) p_src; + + switch (msg->act) { + case ESP_BLUFI_EVENT_RECV_STA_SSID: + dst->sta_ssid.ssid = GKI_getbuf(src->sta_ssid.ssid_len); + if (dst->sta_ssid.ssid == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->sta_ssid.ssid, src->sta_ssid.ssid, src->sta_ssid.ssid_len); + break; + case ESP_BLUFI_EVENT_RECV_STA_PASSWD: + dst->sta_passwd.passwd = GKI_getbuf(src->sta_passwd.passwd_len); + if (dst->sta_passwd.passwd == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->sta_passwd.passwd, src->sta_passwd.passwd, src->sta_passwd.passwd_len); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID: + dst->softap_ssid.ssid = GKI_getbuf(src->softap_ssid.ssid_len); + if (dst->softap_ssid.ssid == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->softap_ssid.ssid, src->softap_ssid.ssid, src->softap_ssid.ssid_len); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD: + dst->softap_passwd.passwd = GKI_getbuf(src->softap_passwd.passwd_len); + if (dst->softap_passwd.passwd == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->softap_passwd.passwd, src->softap_passwd.passwd, src->softap_passwd.passwd_len); + break; + case ESP_BLUFI_EVENT_RECV_USERNAME: + dst->username.name = GKI_getbuf(src->username.name_len); + if (dst->username.name == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->username.name, src->username.name, src->username.name_len); + break; + case ESP_BLUFI_EVENT_RECV_CA_CERT: + dst->ca.cert = GKI_getbuf(src->ca.cert_len); + if (dst->ca.cert == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->ca.cert, src->ca.cert, src->ca.cert_len); + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_CERT: + dst->client_cert.cert = GKI_getbuf(src->client_cert.cert_len); + if (dst->client_cert.cert == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->client_cert.cert, src->client_cert.cert, src->client_cert.cert_len); + break; + case ESP_BLUFI_EVENT_RECV_SERVER_CERT: + dst->server_cert.cert = GKI_getbuf(src->server_cert.cert_len); + if (dst->server_cert.cert == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->server_cert.cert, src->server_cert.cert, src->server_cert.cert_len); + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY: + dst->client_pkey.pkey = GKI_getbuf(src->client_pkey.pkey_len); + if (dst->client_pkey.pkey == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->client_pkey.pkey, src->client_pkey.pkey, src->client_pkey.pkey_len); + break; + case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY: + dst->server_pkey.pkey = GKI_getbuf(src->server_pkey.pkey_len); + if (dst->server_pkey.pkey == NULL) { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + memcpy(dst->server_pkey.pkey, src->server_pkey.pkey, src->server_pkey.pkey_len); + break; + default: + break; + } +} + +void btc_blufi_cb_deep_free(btc_msg_t *msg) +{ + esp_blufi_cb_param_t *param = (esp_blufi_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLUFI_EVENT_RECV_STA_SSID: + GKI_freebuf(param->sta_ssid.ssid); + break; + case ESP_BLUFI_EVENT_RECV_STA_PASSWD: + GKI_freebuf(param->sta_passwd.passwd); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID: + GKI_freebuf(param->softap_ssid.ssid); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD: + GKI_freebuf(param->softap_passwd.passwd); + break; + case ESP_BLUFI_EVENT_RECV_USERNAME: + GKI_freebuf(param->username.name); + break; + case ESP_BLUFI_EVENT_RECV_CA_CERT: + GKI_freebuf(param->ca.cert); + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_CERT: + GKI_freebuf(param->client_cert.cert); + break; + case ESP_BLUFI_EVENT_RECV_SERVER_CERT: + GKI_freebuf(param->server_cert.cert); + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY: + GKI_freebuf(param->client_pkey.pkey); + break; + case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY: + GKI_freebuf(param->server_pkey.pkey); + break; + default: + break; + } } void btc_blufi_cb_handler(btc_msg_t *msg) { - esp_blufi_cb_param_t param; + esp_blufi_cb_param_t *param = (esp_blufi_cb_param_t *)msg->arg; switch (msg->act) { - case BTC_BLUFI_CB_ACT_INIT_FINISH: - param.init_finish.state = ESP_BLUFI_INIT_OK; - BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_INIT_FINISH, ¶m); + case ESP_BLUFI_EVENT_INIT_FINISH: { + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_INIT_FINISH, param); break; - case BTC_BLUFI_CB_ACT_DEINIT_FINISH: - /* TODO: but now nothing */ + } + case ESP_BLUFI_EVENT_DEINIT_FINISH: { + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_DEINIT_FINISH, param); break; - case BTC_BLUFI_CB_ACT_RECV_DATA: - memcpy(¶m.recv_data, msg->arg, sizeof(struct blufi_recv_evt_param)); - - BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_DATA, ¶m); + } + case ESP_BLUFI_EVENT_BLE_CONNECT: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_BLE_CONNECT, param); + break; + case ESP_BLUFI_EVENT_BLE_DISCONNECT: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_BLE_DISCONNECT, param); + break; + case ESP_BLUFI_EVENT_SET_WIFI_OPMODE: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_SET_WIFI_OPMODE, param); + break; + case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP, NULL); + break; + case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP, NULL); + break; + case ESP_BLUFI_EVENT_GET_WIFI_STATUS: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_GET_WIFI_STATUS, NULL); + break; + case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_DEAUTHENTICATE_STA, NULL); + break; + case ESP_BLUFI_EVENT_RECV_STA_BSSID: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_STA_BSSID, param); + break; + case ESP_BLUFI_EVENT_RECV_STA_SSID: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_STA_SSID, param); + break; + case ESP_BLUFI_EVENT_RECV_STA_PASSWD: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_STA_PASSWD, param); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_SOFTAP_SSID, param); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD, param); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM, param); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE, param); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL, param); + break; + case ESP_BLUFI_EVENT_RECV_USERNAME: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_USERNAME, param); + break; + case ESP_BLUFI_EVENT_RECV_CA_CERT: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_CA_CERT, param); + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_CERT: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_CLIENT_CERT, param); + break; + case ESP_BLUFI_EVENT_RECV_SERVER_CERT: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_SERVER_CERT, param); + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY, param); + break; + case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY: + BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY, param); break; default: LOG_ERROR("%s UNKNOWN %d\n", __func__, msg->act); break; } + + btc_blufi_cb_deep_free(msg); +} + +void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_blufi_args_t *dst = (btc_blufi_args_t *) p_dest; + btc_blufi_args_t *src = (btc_blufi_args_t *) p_src; + + switch (msg->act) { + case BTC_BLUFI_ACT_SEND_CFG_REPORT: { + esp_blufi_extra_info_t *src_info = src->wifi_conn_report.extra_info; + dst->wifi_conn_report.extra_info_len = 0; + dst->wifi_conn_report.extra_info = NULL; + + if (src_info == NULL) { + return; + } + + dst->wifi_conn_report.extra_info = GKI_getbuf(sizeof(esp_blufi_extra_info_t)); + if (dst->wifi_conn_report.extra_info == NULL) { + return; + } + + if (src_info->sta_bssid_set) { + memcpy(dst->wifi_conn_report.extra_info->sta_bssid, src_info->sta_bssid, 6); + dst->wifi_conn_report.extra_info->sta_bssid_set = src_info->sta_bssid_set; + dst->wifi_conn_report.extra_info_len += (6 + 2); + } + if (src_info->sta_ssid) { + dst->wifi_conn_report.extra_info->sta_ssid = GKI_getbuf(src_info->sta_ssid_len); + if (dst->wifi_conn_report.extra_info->sta_ssid) { + memcpy(dst->wifi_conn_report.extra_info->sta_ssid, src_info->sta_ssid, src_info->sta_ssid_len); + dst->wifi_conn_report.extra_info->sta_ssid_len = src_info->sta_ssid_len; + dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_ssid_len + 2); + } + } + if (src_info->sta_passwd) { + dst->wifi_conn_report.extra_info->sta_passwd = GKI_getbuf(src_info->sta_passwd_len); + if (dst->wifi_conn_report.extra_info->sta_passwd) { + memcpy(dst->wifi_conn_report.extra_info->sta_passwd, src_info->sta_passwd, src_info->sta_passwd_len); + dst->wifi_conn_report.extra_info->sta_passwd_len = src_info->sta_passwd_len; + dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_passwd_len + 2); + } + } + if (src_info->softap_ssid) { + dst->wifi_conn_report.extra_info->softap_ssid = GKI_getbuf(src_info->softap_ssid_len); + if (dst->wifi_conn_report.extra_info->softap_ssid) { + memcpy(dst->wifi_conn_report.extra_info->softap_ssid, src_info->softap_ssid, src_info->softap_ssid_len); + dst->wifi_conn_report.extra_info->softap_ssid_len = src_info->softap_ssid_len; + dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_ssid_len + 2); + } + } + if (src_info->softap_passwd) { + dst->wifi_conn_report.extra_info->softap_passwd = GKI_getbuf(src_info->softap_passwd_len); + if (dst->wifi_conn_report.extra_info->softap_passwd) { + memcpy(dst->wifi_conn_report.extra_info->softap_passwd, src_info->softap_passwd, src_info->softap_passwd_len); + dst->wifi_conn_report.extra_info->softap_passwd_len = src_info->softap_passwd_len; + dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_passwd_len + 2); + } + } + if (src_info->softap_authmode_set) { + dst->wifi_conn_report.extra_info->softap_authmode_set = src_info->softap_authmode_set; + dst->wifi_conn_report.extra_info->softap_authmode = src_info->softap_authmode; + dst->wifi_conn_report.extra_info_len += (1 + 2); + } + if (src_info->softap_max_conn_num_set) { + dst->wifi_conn_report.extra_info->softap_max_conn_num_set = src_info->softap_max_conn_num_set; + dst->wifi_conn_report.extra_info->softap_max_conn_num = src_info->softap_max_conn_num; + dst->wifi_conn_report.extra_info_len += (1 + 2); + } + if (src_info->softap_channel_set) { + dst->wifi_conn_report.extra_info->softap_channel_set = src_info->softap_channel_set; + dst->wifi_conn_report.extra_info->softap_channel = src_info->softap_channel; + dst->wifi_conn_report.extra_info_len += (1 + 2); + } + break; + } + default: + break; + } +} + +void btc_blufi_call_deep_free(btc_msg_t *msg) +{ + btc_blufi_args_t *arg = (btc_blufi_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLUFI_ACT_SEND_CFG_REPORT: { + esp_blufi_extra_info_t *info = (esp_blufi_extra_info_t *)arg->wifi_conn_report.extra_info; + + if (info == NULL) { + return; + } + if (info->sta_ssid) { + GKI_freebuf(info->sta_ssid); + } + if (info->sta_passwd) { + GKI_freebuf(info->sta_passwd); + } + if (info->softap_ssid) { + GKI_freebuf(info->softap_ssid); + } + if (info->softap_passwd) { + GKI_freebuf(info->softap_passwd); + } + GKI_freebuf(info); + break; + } + default: + break; + } } void btc_blufi_call_handler(btc_msg_t *msg) @@ -367,17 +874,24 @@ void btc_blufi_call_handler(btc_msg_t *msg) btc_blufi_profile_init(); break; case BTC_BLUFI_ACT_DEINIT: - /* TODO: but now nothing */ + btc_blufi_profile_deinit(); break; - case BTC_BLUFI_ACT_SEND_CFG_STATE: - if (arg->cfg_state.state == ESP_BLUFI_CONFIG_OK) { - btc_blufi_config_success(); - } else { - btc_blufi_config_failed(); - } + case BTC_BLUFI_ACT_SEND_CFG_REPORT: + btc_blufi_wifi_conn_report(arg->wifi_conn_report.opmode, + arg->wifi_conn_report.sta_conn_state, + arg->wifi_conn_report.softap_conn_num, + arg->wifi_conn_report.extra_info, + arg->wifi_conn_report.extra_info_len); break; default: LOG_ERROR("%s UNKNOWN %d\n", __func__, msg->act); break; } + btc_blufi_call_deep_free(msg); } + +void btc_blufi_set_callbacks(esp_blufi_callbacks_t *callbacks) +{ + blufi_env.cbs = callbacks; +} + diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c new file mode 100644 index 000000000..2f92a4380 --- /dev/null +++ b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c @@ -0,0 +1,240 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + + +#include "bt_target.h" +#include "bt_trace.h" +#include "bt_types.h" +#include "gatt_api.h" +#include "bta_api.h" +#include "bta_gatt_api.h" +#include "bta_gatts_int.h" + +#include "btc_blufi_prf.h" +#include "btc_task.h" +#include "btc_manage.h" + +#include "blufi_int.h" + +#include "esp_wifi.h" + +extern tBLUFI_ENV blufi_env; + +void btc_blufi_protocol_handler(uint8_t type, uint8_t *data, int len) +{ + btc_msg_t msg; + esp_blufi_cb_param_t param; + uint8_t *output_data = NULL; + int output_len = 0; + bool need_free = false; + + switch (BLUFI_GET_TYPE(type)) { + case BLUFI_TYPE_CTRL: + switch (BLUFI_GET_SUBTYPE(type)) { + case BLUFI_TYPE_CTRL_SUBTYPE_ACK: + /* TODO: check sequence */ + break; + case BLUFI_TYPE_CTRL_SUBTYPE_SET_SEC_MODE: + blufi_env.sec_mode = data[0]; + break; + case BLUFI_TYPE_CTRL_SUBTYPE_SET_WIFI_OPMODE: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_SET_WIFI_OPMODE; + param.wifi_mode.op_mode = data[0]; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + case BLUFI_TYPE_CTRL_SUBTYPE_CONN_TO_AP: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP; + + btc_transfer_context(&msg, NULL, 0, NULL); + break; + case BLUFI_TYPE_CTRL_SUBTYPE_DISCONN_FROM_AP: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP; + + btc_transfer_context(&msg, NULL, 0, NULL); + break; + case BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_GET_WIFI_STATUS; + + btc_transfer_context(&msg, NULL, 0, NULL); + break; + case BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_DEAUTHENTICATE_STA; + + btc_transfer_context(&msg, NULL, 0, NULL); + break; + default: + LOG_ERROR("%s Unkown Ctrl pkt %02x\n", __func__, type); + break; + } + break; + case BLUFI_TYPE_DATA: + switch (BLUFI_GET_SUBTYPE(type)) { + case BLUFI_TYPE_DATA_SUBTYPE_NEG: + if (blufi_env.cbs && blufi_env.cbs->negotiate_data_handler) { + blufi_env.cbs->negotiate_data_handler(data, len, &output_data, &output_len, &need_free); + } + + if (output_data && output_len > 0) { + btc_blufi_send_encap(BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_NEG), + output_data, output_len); + } + break; + case BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_STA_BSSID; + memcpy(param.sta_bssid.bssid, &data[0], 6); + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + case BLUFI_TYPE_DATA_SUBTYPE_STA_SSID: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_STA_SSID; + param.sta_ssid.ssid = &data[0]; + param.sta_ssid.ssid_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_STA_PASSWD; + param.sta_passwd.passwd = &data[0]; + param.sta_passwd.passwd_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_SSID; + param.softap_ssid.ssid = &data[0]; + param.softap_ssid.ssid_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD; + param.softap_passwd.passwd = &data[0]; + param.softap_passwd.passwd_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM; + param.softap_max_conn_num.max_conn_num = data[0]; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE; + param.softap_auth_mode.auth_mode = data[0]; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL; + param.softap_channel.channel = data[0]; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + break; + case BLUFI_TYPE_DATA_SUBTYPE_USERNAME: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_USERNAME; + param.username.name = &data[0]; + param.username.name_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_CA: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_CA_CERT; + param.ca.cert = &data[0]; + param.ca.cert_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_CLIENT_CERT: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_CLIENT_CERT; + param.client_cert.cert = &data[0]; + param.client_cert.cert_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_SERVER_CERT: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SERVER_CERT; + param.client_cert.cert = &data[0]; + param.client_cert.cert_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_CLIENT_PRIV_KEY: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY; + param.client_pkey.pkey = &data[0]; + param.client_pkey.pkey_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + case BLUFI_TYPE_DATA_SUBTYPE_SERVER_PRIV_KEY: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY; + param.client_pkey.pkey = &data[0]; + param.client_pkey.pkey_len = len; + + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy); + break; + default: + LOG_ERROR("%s Unkown Ctrl pkt %02x\n", __func__, type); + break; + } + break; + default: + break; + } +} diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_adv.h b/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_adv.h deleted file mode 100644 index 39869ec9e..000000000 --- a/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_adv.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef __BLUFI_ADV_H__ -#define __BLUFI_ADV_H__ - -#include "bta_api.h" -#include "btm_ble_api.h" -#include "esp_bt_defs.h" - -typedef enum { - BLE_ADV_DATA_IDX = 0, - BLE_SCAN_RSP_DATA_IDX = 1, - ADV_SCAN_IDX_MAX, -} ADV_SCAN_IDX_t; - -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(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); - -#endif /* __BLUFI_ADV_H__ */ diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h b/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h index f46a41a2e..0de66b8e7 100644 --- a/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h +++ b/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h @@ -15,38 +15,156 @@ #ifndef __BLUFI_INT_H__ #define __BLUFI_INT_H__ -//define the blufi serivce uuid -#define SVC_BLUFI_UUID 0xFFFF -//define the blufi Char uuid -#define CHAR_BLUFI_UUID 0xFF01 - -#define BLUFI_HDL_NUM 4 - -#define BLUFI_VAL_MAX_LEN (128) - -#define BLUFI_MAX_STRING_DATA 128 - - -typedef struct { - UINT8 app_id; - UINT16 blufi_hdl; -} tBLUFI_INST; - - /* service engine control block */ typedef struct { - BOOLEAN enabled; - BOOLEAN is_primery; - UINT8 inst_id; + /* Protocol reference */ tGATT_IF gatt_if; - tBLUFI_INST blufi_inst; - BOOLEAN in_use; - BOOLEAN congest; + UINT8 srvc_inst; + UINT16 handle_srvc; + UINT16 handle_char_p2e; + UINT16 handle_char_e2p; + UINT16 handle_descr_e2p; UINT16 conn_id; - BOOLEAN connected; + BOOLEAN is_connected; BD_ADDR remote_bda; UINT32 trans_id; - UINT8 cur_srvc_id; -} tBLUFI_CB_ENV; + UINT8 congest; +#define BLUFI_PREPAIR_BUF_MAX_SIZE 1024 + uint8_t *prepare_buf; + int prepare_len; + /* Control reference */ + esp_blufi_callbacks_t *cbs; + BOOLEAN enabled; + uint8_t send_seq; + uint8_t recv_seq; + uint8_t sec_mode; + uint8_t *aggr_buf; + uint16_t total_len; + uint16_t offset; +} tBLUFI_ENV; + +/* BLUFI protocol */ +struct blufi_hdr{ + uint8_t type; + uint8_t fc; + uint8_t seq; + uint8_t data_len; + uint8_t data[0]; +}; +typedef struct blufi_hdr blufi_hd_t; + +struct blufi_frag_hdr { + uint8_t type; + uint8_t fc; + uint8_t seq; + uint8_t data_len; + uint16_t total_len; + uint8_t content[0]; +}; +typedef struct blufi_frag_hdr blufi_frag_hdr_t; + +#define BLUFI_DATA_SEC_MODE_CHECK_MASK 0x01 +#define BLUFI_DATA_SEC_MODE_ENC_MASK 0x02 +#define BLUFI_CTRL_SEC_MODE_CHECK_MASK 0x10 +#define BLUFI_CTRL_SEC_MODE_ENC_MASK 0x20 + +// packet type +#define BLUFI_TYPE_MASK 0x03 +#define BLUFI_TYPE_SHIFT 0 +#define BLUFI_SUBTYPE_MASK 0xFC +#define BLUFI_SUBTYPE_SHIFT 2 + +#define BLUFI_GET_TYPE(type) ((type) & BLUFI_TYPE_MASK) +#define BLUFI_GET_SUBTYPE(type) (((type) & BLUFI_SUBTYPE_MASK) >>BLUFI_SUBTYPE_SHIFT) +#define BLUFI_BUILD_TYPE(type, subtype) (((type) & BLUFI_TYPE_MASK) | ((subtype)<p_services->p_uuid) { - LOG_ERROR("%s - In 16-UUID_data", __FUNCTION__); + LOG_DEBUG("%s - In 16-UUID_data", __FUNCTION__); mask |= BTM_BLE_AD_BIT_SERVICE; ++bta_adv_data->p_services->num_service; *p_uuid_out16++ = bt_uuid.uu.uuid16; @@ -221,7 +221,7 @@ static void btc_to_bta_adv_data(esp_ble_adv_data_t *p_adv_data, tBTA_BLE_ADV_DAT } if (NULL != bta_adv_data->p_service_32b->p_uuid) { - LOG_ERROR("%s - In 32-UUID_data", __FUNCTION__); + LOG_DEBUG("%s - In 32-UUID_data", __FUNCTION__); mask |= BTM_BLE_AD_BIT_SERVICE_32; ++bta_adv_data->p_service_32b->num_service; *p_uuid_out32++ = bt_uuid.uu.uuid32; diff --git a/components/bt/bluedroid/hci/packet_fragmenter.c b/components/bt/bluedroid/hci/packet_fragmenter.c index 42663a249..bd46041c3 100644 --- a/components/bt/bluedroid/hci/packet_fragmenter.c +++ b/components/bt/bluedroid/hci/packet_fragmenter.c @@ -147,16 +147,16 @@ static void reassemble_and_dispatch(BT_HDR *packet) if (boundary_flag == START_PACKET_BOUNDARY) { if (partial_packet) { - LOG_ERROR("%s found unfinished packet for handle with start packet. Dropping old.\n", __func__); - LOG_ERROR("partial_packet->len = %x, offset = %x\n", partial_packet->len, partial_packet->len); + LOG_DEBUG("%s found unfinished packet for handle with start packet. Dropping old.\n", __func__); + LOG_DEBUG("partial_packet->len = %x, offset = %x\n", partial_packet->len, partial_packet->len); - for (int i = 0; i < partial_packet->len; i++) { - LOG_ERROR("%x", partial_packet->data[i]); - } - LOG_ERROR("\n"); + //for (int i = 0; i < partial_packet->len; i++) { + // LOG_ERROR("%x", partial_packet->data[i]); + //} + //LOG_ERROR("\n"); hash_map_erase(partial_packets, (void *)(uintptr_t)handle); //buffer_allocator->free(partial_packet); - LOG_ERROR("+++++++++++++++++++\n"); + //LOG_ERROR("+++++++++++++++++++\n"); } uint16_t full_length = l2cap_length + L2CAP_HEADER_SIZE + HCI_ACL_PREAMBLE_SIZE; @@ -214,7 +214,7 @@ static void reassemble_and_dispatch(BT_HDR *packet) STREAM_TO_UINT16(handle, stream); STREAM_TO_UINT16(acl_length, stream); STREAM_TO_UINT16(l2cap_length, stream); - LOG_ERROR("partial_packet->offset = %x\n", partial_packet->offset); + LOG_DEBUG("partial_packet->offset = %x\n", partial_packet->offset); hash_map_erase(partial_packets, (void *)(uintptr_t)handle); partial_packet->offset = 0; diff --git a/components/bt/bluedroid/include/bt_trace.h b/components/bt/bluedroid/include/bt_trace.h index fd92f6e8c..787c4c8ae 100644 --- a/components/bt/bluedroid/include/bt_trace.h +++ b/components/bt/bluedroid/include/bt_trace.h @@ -32,7 +32,7 @@ #define assert(x) do { if (!(x)) BT_PRINTF("bt host error %s %u\n", __FILE__, __LINE__); } while (0) #endif -inline void trc_dump_buffer(uint8_t *prefix, uint8_t *data, uint16_t len) +inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t len) { uint16_t i; @@ -41,14 +41,13 @@ inline void trc_dump_buffer(uint8_t *prefix, uint8_t *data, uint16_t len) } if (prefix) { - BT_PRINTF("%s:\t", prefix); + BT_PRINTF("%s: len %d\n", prefix, len); } - for (i = 0; i < len; i++) { - BT_PRINTF(" %02x", *(data + i)); - if (!((i + 1) & 0xf)) { - BT_PRINTF("\n"); - } + for (i = 0; i < len; i+=16) { + BT_PRINTF("%02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n", + *(data + i), *(data + i + 1), *(data + i + 2), *(data + i + 3), *(data + i + 4), *(data + i + 5), *(data + i + 6), *(data + i + 7), + *(data + i + 8), *(data + i + 9), *(data + i + 10), *(data + i + 11), *(data + i + 12), *(data + i + 13), *(data + i + 14), *(data + i + 15)); } BT_PRINTF("\n"); } diff --git a/docs/api/bt_le.rst b/docs/api/bt_le.rst index 6ae486937..11d09809f 100644 --- a/docs/api/bt_le.rst +++ b/docs/api/bt_le.rst @@ -8,3 +8,4 @@ BT COMMON BLE GATT DEFINE BLE GATT SERVER BLE GATT CLIENT + BLE BLUFI diff --git a/docs/api/esp_blufi.rst b/docs/api/esp_blufi.rst new file mode 100644 index 000000000..1ffb67bec --- /dev/null +++ b/docs/api/esp_blufi.rst @@ -0,0 +1,128 @@ +BLUFI API +========= + +Overview +-------- +BLUFI is a profile based GATT to config ESP32 WIFI to connect/disconnect AP or setup a softap and etc. +Use should concern these things: + 1. The event sent from profile. Then you need to do something as the event indicate. + 2. Security reference. You can write your own Security functions such as symmetrical encryption/decryption and checksum functions. + Even you can define the "Key Exchange/Negotiation" procedure. + +Application Example +------------------- + +Check `/examples `_ folder of `espressif/esp-idf `_ repository, that contains the following example: + +`12_blufi `_ + +This is a BLUFI demo. This demo can set ESP32's wifi to softap/station/softap&station mode and config wifi connections. + + +API Reference +------------- + +Header Files +^^^^^^^^^^^^ + + * `bt/bluedroid/api/include/esp_blufi_api.h `_ + +Macros +^^^^^^ + + +Type Definitions +^^^^^^^^^^^^^^^^ + +.. doxygentypedef:: esp_blufi_event_cb_t +.. doxygentypedef:: esp_blufi_negotiate_data_handler_t +.. doxygentypedef:: esp_blufi_encrypt_func_t +.. doxygentypedef:: esp_blufi_decrypt_func_t +.. doxygentypedef:: esp_blufi_checksum_func_t + +Enumerations +^^^^^^^^^^^^ + +.. doxygenenum:: esp_blufi_cb_event_t +.. doxygenenum:: esp_blufi_sta_conn_state_t +.. doxygenenum:: esp_blufi_init_state_t +.. doxygenenum:: esp_blufi_deinit_state_t + +Structures +^^^^^^^^^^ + +.. doxygenstruct:: esp_blufi_extra_info_t + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_init_finish_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_deinit_finish_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_set_wifi_mode_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_connect_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_disconnect_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_sta_bssid_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_sta_ssid_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_sta_passwd_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_softap_ssid_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_softap_passwd_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_softap_max_conn_num_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_softap_auth_mode_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_softap_channel_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_username_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_ca_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_client_cert_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_server_cert_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_client_pkey_evt_param + :members: + +.. doxygenstruct:: esp_blufi_cb_param_t::blufi_recv_server_pkey_evt_param + :members: + +.. doxygenstruct:: esp_blufi_callbacks_t + :members: + + +Functions +^^^^^^^^^ + +.. doxygenfunction:: esp_blufi_register_callbacks +.. doxygenfunction:: esp_blufi_profile_init +.. doxygenfunction:: esp_blufi_profile_deinit +.. doxygenfunction:: esp_blufi_send_wifi_conn_report + diff --git a/examples/12_blufi/components/blufi/blufi.c b/examples/12_blufi/components/blufi/blufi.c deleted file mode 100644 index 8e2b5d1f3..000000000 --- a/examples/12_blufi/components/blufi/blufi.c +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/*************************************************************** -* -* This file is for gatt server device. It instantiates BATTERY -* sevice. It can be scanned and connected by central device, -* and the client will get the BAS value. It calls the API bta -* layer provides. -* -****************************************************************/ - -#include -#include -#include -#include - -#include "controller.h" - -#include "bt_trace.h" -#include "bt_types.h" -#include "bta_api.h" - -#include "blufi.h" - -#include "esp_bt_defs.h" -#include "esp_bt_main.h" -#include "esp_blufi_api.h" - -extern void wifi_set_blue_config(char *ssid, char *passwd); - -#define HEADER_SSID "ssid" -#define HEADER_PASSWD "passwd" -#define HEADER_CONFIRM "confirm" - -static char tmp_ssid[32 + 1]; -static char tmp_passwd[64 + 1]; - -static void blufi_data_recv(uint8_t *data, int len) -{ - char *p = NULL; - LOG_DEBUG("the data is:%s\n", data); - - p = strstr((char *)data, HEADER_SSID); - if (p) { - LOG_ERROR("SSID: %s\n", p + strlen(HEADER_SSID) + 1); - strcpy(tmp_ssid, p + strlen(HEADER_SSID) + 1); - } - p = strstr((char *)data, HEADER_PASSWD); - if (p) { - LOG_ERROR("PASSWORD: %s\n", p + strlen(HEADER_PASSWD) + 1); - strcpy(tmp_passwd, p + strlen(HEADER_PASSWD) + 1); - } - p = strstr((char *)data, HEADER_CONFIRM); - if (p) { - LOG_ERROR("CONFIRM\n"); - wifi_set_blue_config(tmp_ssid, tmp_passwd); - } - -} - -static void blufi_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param) -{ - /* actually, should post to blufi_task handle the procedure, - * now, as a demo, we do simplely */ - switch (event) { - case ESP_BLUFI_EVENT_INIT_FINISH: - LOG_ERROR("blufi init finish\n"); - break; - case ESP_BLUFI_EVENT_RECV_DATA: { - LOG_DEBUG("blufi recv data\n"); - esp_blufi_cb_param_t *blufi_param = (esp_blufi_cb_param_t *)param; - blufi_data_recv(blufi_param->recv_data.data, blufi_param->recv_data.data_len); - break; - } - default: - break; - } -} - -static esp_err_t blufi_startup_in_blufi_task(void *arg) -{ - esp_blufi_register_callback(blufi_callback); - esp_blufi_profile_init(); - - return ESP_OK; -} - - -static void blufi_startup(void) -{ - blufi_transfer_context(blufi_startup_in_blufi_task, NULL); -} - -esp_err_t blufi_enable(void *arg) -{ - esp_err_t err; - - err = esp_bluedroid_enable(); - if (err) { - LOG_ERROR("%s failed\n", __func__); - return err; - } - blufi_startup(); - vTaskDelay(1000 / portTICK_PERIOD_MS); - - return err; -} - -esp_err_t blufi_disable(void *arg) -{ - esp_err_t err; - - err = esp_bluedroid_disable(); - - if (arg) { - ((void (*)(void))arg)(); - } - - return err; -} diff --git a/examples/12_blufi/components/blufi/blufi_task.c b/examples/12_blufi/components/blufi/blufi_task.c deleted file mode 100644 index 75547c643..000000000 --- a/examples/12_blufi/components/blufi/blufi_task.c +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include - -#include "gki.h" -#include "bt_defs.h" -#include "bt_trace.h" -#include "bt_types.h" -#include "allocator.h" - -#include "bta_api.h" -#include "bta_gatt_api.h" - -#include "controller.h" - -#include "hash_map.h" -#include "hash_functions.h" -#include "alarm.h" -#include "thread.h" - -#include "blufi.h" -#include "blufi_adv.h" - -xQueueHandle xBlufiTaskQueue; -xTaskHandle xBlufiTaskHandle; - -extern void ble_server_test(void); - -static void blufi_task(void *arg) -{ - BtTaskEvt_t e; - - for (;;) { - if (pdTRUE == xQueueReceive(xBlufiTaskQueue, &e, (portTickType)portMAX_DELAY)) { - switch (e.sig) { - case BLUFI_SIG_SWITCH_CONTEXT: - if (e.cb) { - ((BtTaskCb_t)e.cb)(e.arg); - } - break; - default: - break; - } - } - } -} - -static esp_err_t blufi_task_post(uint32_t sig, void *par, void *cb, void *arg) -{ - BtTaskEvt_t evt; - - evt.sig = sig; - evt.par = par; - evt.cb = cb; - evt.arg = arg; - - if (xQueueSend(xBlufiTaskQueue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) { - LOG_ERROR("Blufi Post failed\n"); - return ESP_FAIL; - } - - return ESP_OK; -} - -esp_err_t blufi_transfer_context(blufi_task_cb_t cb, void *arg) -{ - LOG_DEBUG("%s cb %08x, arg %u\n", __func__, (uint32_t)cb, (uint32_t)arg); - - return blufi_task_post(BLUFI_SIG_SWITCH_CONTEXT, 0, cb, arg); -} - -static void blufi_task_deinit(void) -{ - vTaskDelete(xBlufiTaskHandle); - vQueueDelete(xBlufiTaskQueue); -} - - -static void blufi_task_init(void) -{ - xBlufiTaskQueue = xQueueCreate(5, sizeof(BtTaskEvt_t)); - xTaskCreate(blufi_task, "BlUFI", 4096, NULL, configMAX_PRIORITIES - 3, xBlufiTaskHandle); -} - -void blufi_init(void) -{ - blufi_task_init(); - blufi_transfer_context(blufi_enable, NULL); -} - -void blufi_deinit(void) -{ - blufi_transfer_context(blufi_disable, blufi_task_deinit); -} - diff --git a/examples/12_blufi/components/blufi/component.mk b/examples/12_blufi/components/blufi/component.mk deleted file mode 100644 index 297e63f91..000000000 --- a/examples/12_blufi/components/blufi/component.mk +++ /dev/null @@ -1,13 +0,0 @@ -# -# Main Makefile. This is basically the same as a component makefile. -# -# This Makefile should, at the very least, just include $(SDK_PATH)/make/component_common.mk. By default, -# this will take the sources in the src/ directory, compile them and link them into -# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable, -# please read the ESP-IDF documents if you need to do this. -# - -COMPONENT_SRCDIRS := . - -CFLAGS += -Wno-error=unused-label -Wno-error=return-type -Wno-error=missing-braces -Wno-error=pointer-sign -Wno-error=parentheses -I./include - diff --git a/examples/12_blufi/components/blufi/include/blufi.h b/examples/12_blufi/components/blufi/include/blufi.h deleted file mode 100644 index c79695edc..000000000 --- a/examples/12_blufi/components/blufi/include/blufi.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef __BT_APP_COMMON_H__ -#define __BT_APP_COMMON_H__ - -#include -#include "osi.h" -#include "bt_common_types.h" -#include "esp_err.h" - -enum BLUFI_SIG { - BLUFI_SIG_SWITCH_CONTEXT = 0, - BLUFI_SIG_ENABLE, - BLUFI_SIG_DISABLE, -}; - -typedef esp_err_t (*blufi_task_cb_t)(void *arg); - -void blufi_init(void); -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/12_blufi/main/blufi_demo.h b/examples/12_blufi/main/blufi_demo.h new file mode 100644 index 000000000..c5bf55c7d --- /dev/null +++ b/examples/12_blufi/main/blufi_demo.h @@ -0,0 +1,17 @@ +#ifndef __BLUFI_DEMO_H__ +#define __BLUFI_DEMO_H__ + + +#define BLUFI_DEMO_TAG "BLUFI_DEMO" +#define BLUFI_INFO(fmt, ...) ESP_LOGI(BLUFI_DEMO_TAG, fmt, ##__VA_ARGS__) +#define BLUFI_ERROR(fmt, ...) ESP_LOGE(BLUFI_DEMO_TAG, fmt, ##__VA_ARGS__) + +void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free); +int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len); +int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len); +uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len); + +int blufi_security_init(void); +void blufi_security_deinit(void); + +#endif /* __BLUFI_DEMO_H__ */ diff --git a/examples/12_blufi/main/blufi_main.c b/examples/12_blufi/main/blufi_main.c new file mode 100644 index 000000000..3f5cd3dcc --- /dev/null +++ b/examples/12_blufi/main/blufi_main.c @@ -0,0 +1,336 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "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_demo.h" + +static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param); + +#define BLUFI_DEVICE_NAME "BLUFI_DEVICE" +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 uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x12, 0x23, 0x45, 0x56}; +static esp_ble_adv_data_t blufi_adv_data = { + .set_scan_rsp = false, + .include_name = true, + .include_txpower = true, + .min_interval = 0x100, + .max_interval = 0x100, + .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, +}; + +#define WIFI_LIST_NUM 10 + +static wifi_config_t sta_config; +static wifi_config_t ap_config; + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/* The event group allows multiple bits for each event, + but we only care about one event - are we connected + to the AP with an IP? */ +const int CONNECTED_BIT = BIT0; + +/* store the station info for send back to phone */ +static bool gl_sta_connected = false; +static uint8_t gl_sta_bssid[6]; +static uint8_t gl_sta_ssid[32]; +static int gl_sta_ssid_len; + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + wifi_mode_t mode; + + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: { + esp_blufi_extra_info_t info; + + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + esp_wifi_get_mode(&mode); + + memset(&info, 0, sizeof(esp_blufi_extra_info_t)); + memcpy(info.sta_bssid, gl_sta_bssid, 6); + info.sta_bssid_set = true; + info.sta_ssid = gl_sta_ssid; + info.sta_ssid_len = gl_sta_ssid_len; + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); + break; + } + case SYSTEM_EVENT_STA_CONNECTED: + gl_sta_connected = true; + memcpy(gl_sta_bssid, event->event_info.connected.bssid, 6); + memcpy(gl_sta_ssid, event->event_info.connected.ssid, event->event_info.connected.ssid_len); + gl_sta_ssid_len = event->event_info.connected.ssid_len; + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + /* This is a workaround as ESP32 WiFi libs don't currently + auto-reassociate. */ + gl_sta_connected = false; + memset(gl_sta_ssid, 0, 32); + memset(gl_sta_bssid, 0, 6); + gl_sta_ssid_len = 0; + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + case SYSTEM_EVENT_AP_START: + esp_wifi_get_mode(&mode); + + /* TODO: get config or information of softap, then set to report extra_info */ + if (gl_sta_connected) { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, NULL); + } else { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); + } + break; + default: + break; + } + return ESP_OK; +} + +static void initialise_wifi(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); + ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_start() ); +} + +static esp_blufi_callbacks_t blufi_callbacks = { + .event_cb = blufi_event_callback, + .negotiate_data_handler = blufi_dh_negotiate_data_handler, + .encrypt_func = blufi_aes_encrypt, + .decrypt_func = blufi_aes_decrypt, + .checksum_func = blufi_crc_checksum, +}; + +static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param) +{ + /* actually, should post to blufi_task handle the procedure, + * now, as a demo, we do simplely */ + switch (event) { + case ESP_BLUFI_EVENT_INIT_FINISH: + BLUFI_INFO("BLUFI init finish\n"); + + esp_ble_gap_set_device_name(BLUFI_DEVICE_NAME); + esp_ble_gap_config_adv_data(&blufi_adv_data); + break; + case ESP_BLUFI_EVENT_DEINIT_FINISH: + BLUFI_INFO("BLUFI init finish\n"); + break; + case ESP_BLUFI_EVENT_BLE_CONNECT: + BLUFI_INFO("BLUFI ble connect\n"); + esp_ble_gap_stop_advertising(); + blufi_security_deinit(); + blufi_security_init(); + break; + case ESP_BLUFI_EVENT_BLE_DISCONNECT: + BLUFI_INFO("BLUFI ble disconnect\n"); + esp_ble_gap_start_advertising(&blufi_adv_params); + break; + case ESP_BLUFI_EVENT_SET_WIFI_OPMODE: + BLUFI_INFO("BLUFI Set WIFI opmode %d\n", param->wifi_mode.op_mode); + ESP_ERROR_CHECK( esp_wifi_set_mode(param->wifi_mode.op_mode) ); + break; + case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP: + BLUFI_INFO("BLUFI requset wifi connect to AP\n"); + esp_wifi_connect(); + break; + case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP: + BLUFI_INFO("BLUFI requset wifi disconnect from AP\n"); + esp_wifi_disconnect(); + break; + case ESP_BLUFI_EVENT_GET_WIFI_STATUS: { + wifi_mode_t mode; + esp_blufi_extra_info_t info; + + esp_wifi_get_mode(&mode); + + if (gl_sta_connected ) { + memset(&info, 0, sizeof(esp_blufi_extra_info_t)); + memcpy(info.sta_bssid, gl_sta_bssid, 6); + info.sta_bssid_set = true; + info.sta_ssid = gl_sta_ssid; + info.sta_ssid_len = gl_sta_ssid_len; + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); + } else { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); + } + BLUFI_INFO("BLUFI get wifi status from AP\n"); + + break; + } + case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA: + /* TODO */ + break; + case ESP_BLUFI_EVENT_RECV_STA_BSSID: + memcpy(sta_config.sta.bssid, param->sta_bssid.bssid, 6); + sta_config.sta.bssid_set = 1; + esp_wifi_set_config(WIFI_IF_STA, &sta_config); + BLUFI_INFO("Recv STA BSSID %s\n", sta_config.sta.ssid); + break; + case ESP_BLUFI_EVENT_RECV_STA_SSID: + strncpy((char *)sta_config.sta.ssid, (char *)param->sta_ssid.ssid, param->sta_ssid.ssid_len); + sta_config.sta.ssid[param->sta_ssid.ssid_len] = '\0'; + esp_wifi_set_config(WIFI_IF_STA, &sta_config); + BLUFI_INFO("Recv STA SSID %s\n", sta_config.sta.ssid); + break; + case ESP_BLUFI_EVENT_RECV_STA_PASSWD: + strncpy((char *)sta_config.sta.password, (char *)param->sta_passwd.passwd, param->sta_passwd.passwd_len); + sta_config.sta.password[param->sta_passwd.passwd_len] = '\0'; + esp_wifi_set_config(WIFI_IF_STA, &sta_config); + BLUFI_INFO("Recv STA PASSWORD %s\n", sta_config.sta.password); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID: + strncpy((char *)ap_config.ap.ssid, (char *)param->softap_ssid.ssid, param->softap_ssid.ssid_len); + ap_config.ap.ssid_len = param->softap_ssid.ssid_len; + esp_wifi_set_config(WIFI_IF_AP, &ap_config); + BLUFI_INFO("Recv SOFTAP SSID %s, ssid len %d\n", ap_config.ap.ssid, ap_config.ap.ssid_len); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD: + strncpy((char *)ap_config.ap.password, (char *)param->softap_passwd.passwd, param->softap_passwd.passwd_len); + esp_wifi_set_config(WIFI_IF_AP, &ap_config); + BLUFI_INFO("Recv SOFTAP PASSWORD %s\n", ap_config.ap.password); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM: + if (param->softap_max_conn_num.max_conn_num > 4) { + return; + } + ap_config.ap.max_connection = param->softap_max_conn_num.max_conn_num; + esp_wifi_set_config(WIFI_IF_AP, &ap_config); + BLUFI_INFO("Recv SOFTAP MAX CONN NUM %d\n", ap_config.ap.max_connection); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE: + if (param->softap_auth_mode.auth_mode >= WIFI_AUTH_MAX) { + return; + } + ap_config.ap.authmode = param->softap_auth_mode.auth_mode; + esp_wifi_set_config(WIFI_IF_AP, &ap_config); + BLUFI_INFO("Recv SOFTAP AUTH MODE %d\n", ap_config.ap.authmode); + break; + case ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL: + if (param->softap_channel.channel > 13) { + return; + } + ap_config.ap.channel = param->softap_channel.channel; + esp_wifi_set_config(WIFI_IF_AP, &ap_config); + BLUFI_INFO("Recv SOFTAP CHANNEL %d\n", ap_config.ap.channel); + break; + case ESP_BLUFI_EVENT_RECV_USERNAME: + /* Not handle currently */ + break; + case ESP_BLUFI_EVENT_RECV_CA_CERT: + /* Not handle currently */ + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_CERT: + /* Not handle currently */ + break; + case ESP_BLUFI_EVENT_RECV_SERVER_CERT: + /* Not handle currently */ + break; + case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY: + /* Not handle currently */ + break;; + case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY: + /* Not handle currently */ + break; + default: + break; + } +} + +static void 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; + } +} + +void app_main() +{ + esp_err_t ret; + + nvs_flash_init(); + initialise_wifi(); + + esp_bt_controller_init(); + + ret = esp_bluedroid_init(); + if (ret) { + BLUFI_ERROR("%s init bluedroid failed\n", __func__); + return; + } + + ret = esp_bluedroid_enable(); + if (ret) { + BLUFI_ERROR("%s init bluedroid failed\n", __func__); + return; + } + + blufi_security_init(); + esp_ble_gap_register_callback(gap_event_handler); + + esp_blufi_register_callbacks(&blufi_callbacks); + esp_blufi_profile_init(); +} diff --git a/examples/12_blufi/main/blufi_security.c b/examples/12_blufi/main/blufi_security.c new file mode 100644 index 000000000..94755eaae --- /dev/null +++ b/examples/12_blufi/main/blufi_security.c @@ -0,0 +1,205 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "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_demo.h" + +#include "mbedtls/aes.h" +#include "mbedtls/dhm.h" +#include "mbedtls/md5.h" +#include "rom/crc.h" + +/* + The SEC_TYPE_xxx is for self-defined packet data type in the procedure of "BLUFI negotiate key" + If user use other negotiation procedure to exchange(or generate) key, should redefine the type by yourself. + */ +#define SEC_TYPE_DH_PARAM_LEN 0x00 +#define SEC_TYPE_DH_PARAM_DATA 0x01 +#define SEC_TYPE_DH_P 0x02 +#define SEC_TYPE_DH_G 0x03 +#define SEC_TYPE_DH_PUBLIC 0x04 + + +struct blufi_security { +#define DH_SELF_PUB_KEY_LEN 128 +#define DH_SELF_PUB_KEY_BIT_LEN (DH_SELF_PUB_KEY_LEN * 8) + uint8_t self_public_key[DH_SELF_PUB_KEY_LEN]; +#define SHARE_KEY_LEN 128 +#define SHARE_KEY_BIT_LEN (SHARE_KEY_LEN * 8) + uint8_t share_key[SHARE_KEY_LEN]; + size_t share_len; +#define PSK_LEN 16 + uint8_t psk[PSK_LEN]; + uint8_t *dh_param; + int dh_param_len; + uint8_t iv[16]; + mbedtls_dhm_context dhm; + mbedtls_aes_context aes; +}; +static struct blufi_security *blufi_sec; + +static int myrand( void *rng_state, unsigned char *output, size_t len ) +{ + size_t i; + + for( i = 0; i < len; ++i ) + output[i] = esp_random(); + + return( 0 ); +} + +void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free) +{ + int ret; + uint8_t type = data[0]; + + if (blufi_sec == NULL) { + BLUFI_ERROR("BLUFI Security is not initialized"); + return; + } + + switch (type) { + case SEC_TYPE_DH_PARAM_LEN: + blufi_sec->dh_param_len = ((data[1]<<8)|data[2]); + if (blufi_sec->dh_param) { + free(blufi_sec->dh_param); + } + blufi_sec->dh_param = (uint8_t *)malloc(blufi_sec->dh_param_len); + if (blufi_sec->dh_param == NULL) { + return; + } + break; + case SEC_TYPE_DH_PARAM_DATA: + + memcpy(blufi_sec->dh_param, &data[1], blufi_sec->dh_param_len); + + ret = mbedtls_dhm_read_params(&blufi_sec->dhm, &blufi_sec->dh_param, &blufi_sec->dh_param[blufi_sec->dh_param_len]); + if (ret) { + BLUFI_ERROR("%s read param failed %d\n", __func__, ret); + return; + } + + ret = mbedtls_dhm_make_public(&blufi_sec->dhm, (int) mbedtls_mpi_size( &blufi_sec->dhm.P ), blufi_sec->self_public_key, blufi_sec->dhm.len, myrand, NULL); + if (ret) { + BLUFI_ERROR("%s make public failed %d\n", __func__, ret); + return; + } + + mbedtls_dhm_calc_secret( &blufi_sec->dhm, + blufi_sec->share_key, + SHARE_KEY_BIT_LEN, + &blufi_sec->share_len, + NULL, NULL); + + mbedtls_md5(blufi_sec->share_key, blufi_sec->share_len, blufi_sec->psk); + + mbedtls_aes_setkey_enc(&blufi_sec->aes, blufi_sec->psk, 128); + mbedtls_aes_setkey_dec(&blufi_sec->aes, blufi_sec->psk, 128); + + /* alloc output data */ + *output_data = &blufi_sec->self_public_key[0]; + *output_len = blufi_sec->dhm.len; + *need_free = false; + break; + case SEC_TYPE_DH_P: + break; + case SEC_TYPE_DH_G: + break; + case SEC_TYPE_DH_PUBLIC: + break; + } +} + +int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len) +{ + int ret; + size_t iv_offset = 0; + uint8_t iv0[16]; + + memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv)); + iv0[0] = iv8; /* set iv8 as the iv0[0] */ + + ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_ENCRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data); + if (ret) { + return -1; + } + + return crypt_len; +} + +int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len) +{ + int ret; + size_t iv_offset = 0; + uint8_t iv0[16]; + + memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv)); + iv0[0] = iv8; /* set iv8 as the iv0[0] */ + + ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_DECRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data); + if (ret) { + return -1; + } + + return crypt_len; +} + +uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len) +{ + /* This iv8 ignore, not used */ + return crc16_be(0, data, len); +} + +esp_err_t blufi_security_init(void) +{ + blufi_sec = (struct blufi_security *)malloc(sizeof(struct blufi_security)); + if (blufi_sec == NULL) { + return ESP_FAIL; + } + + memset(&blufi_sec, 0x0, sizeof(struct blufi_security)); + + mbedtls_dhm_init(&blufi_sec->dhm); + mbedtls_aes_init(&blufi_sec->aes); + + memset(blufi_sec->iv, 0x0, 16); + return 0; +} + +void blufi_security_deinit(void) +{ + mbedtls_dhm_free(&blufi_sec->dhm); + mbedtls_aes_free(&blufi_sec->aes); + + memset(&blufi_sec, 0x0, sizeof(struct blufi_security)); + + free(blufi_sec); + blufi_sec = NULL; +} diff --git a/examples/12_blufi/main/demo_main.c b/examples/12_blufi/main/demo_main.c deleted file mode 100644 index abb47aedb..000000000 --- a/examples/12_blufi/main/demo_main.c +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "bt.h" -#include "bta_api.h" - -#include "esp_blufi_api.h" -#include "esp_bt_defs.h" -#include "esp_bt_main.h" -#include "blufi.h" - -#define WIFI_LIST_NUM 10 - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const int CONNECTED_BIT = BIT0; - - - -static wifi_config_t sta_config; - -static char tmp_ssid[33]; -static char tmp_passwd[65]; -static bool confirm = false; - -void wifi_set_blue_config(char *ssid, char *passwd) -{ - memset(tmp_ssid, 0, sizeof(tmp_ssid)); - memset(tmp_passwd, 0, sizeof(tmp_passwd)); - strlcpy(tmp_ssid, ssid, sizeof(tmp_ssid)); - strlcpy(tmp_passwd, passwd, sizeof(tmp_passwd)); - confirm = true; - LOG_DEBUG("confirm true\n"); -} - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - esp_blufi_send_config_state(ESP_BLUFI_CONFIG_OK); - esp_bluedroid_disable(); //close bluetooth function - //esp_bluedroid_deinit(); //free bluetooth resource - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - /* This is a workaround as ESP32 WiFi libs don't currently - auto-reassociate. */ - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void initialise_wifi(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - - -void wifiTestTask(void *pvParameters) -{ - esp_err_t ret; - - while (1) { - vTaskDelay(1000 / portTICK_PERIOD_MS); - if (confirm) { - confirm = false; - - memcpy(sta_config.sta.ssid, tmp_ssid, sizeof(sta_config.sta.ssid)); - memcpy(sta_config.sta.password, tmp_passwd, sizeof(sta_config.sta.password)); - sta_config.sta.bssid_set = 0; - - ret = esp_wifi_disconnect(); - LOG_INFO("esp_wifi config\n"); - esp_wifi_set_config(WIFI_IF_STA, &sta_config); - LOG_INFO("esp_wifi connect\n"); - ret = esp_wifi_connect(); - if (ret != ESP_OK) { - LOG_ERROR("esp_wifi connect failed\n"); - esp_blufi_send_config_state(ESP_BLUFI_CONFIG_FAILED); - } - } - } -} - -void app_main() -{ - esp_err_t ret; - - nvs_flash_init(); - initialise_wifi(); - - //vTaskDelay(3000 / portTICK_PERIOD_MS); - - esp_bt_controller_init(); - xTaskCreatePinnedToCore(&wifiTestTask, "wifiTestTask", 2048, NULL, 20, NULL, 0); - - LOG_ERROR("%s init bluetooth\n", __func__); - ret = esp_bluedroid_init(); - if (ret) { - LOG_ERROR("%s init bluetooth failed\n", __func__); - return; - } - blufi_init(); -}