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
This commit is contained in:
Tian Hao 2016-12-28 12:02:43 +08:00
parent 2e7748d625
commit 24af07fd13
22 changed files with 2090 additions and 953 deletions

View file

@ -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);
}

View file

@ -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, /*<! When BLUFI init complete, this event happen */
ESP_BLUFI_EVENT_DEINIT_FINISH, /*<! When BLUFI deinit complete, this event happen */
ESP_BLUFI_EVENT_SET_WIFI_OPMODE, /*<! When Phone set ESP32 wifi operation mode(AP/STA/AP_STA), this event happen */
ESP_BLUFI_EVENT_BLE_CONNECT, /*<! When Phone connect to ESP32 with BLE, this event happen */
ESP_BLUFI_EVENT_BLE_DISCONNECT, /*<! When Phone disconnect with BLE, this event happen */
ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP, /*<! When Phone request ESP32's STA connect to AP, this event happen */
ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP, /*<! When Phone request ESP32's STA disconnect from AP, this event happen */
ESP_BLUFI_EVENT_GET_WIFI_STATUS, /*<! When Phone get ESP32 wifi status, this event happen */
ESP_BLUFI_EVENT_DEAUTHENTICATE_STA, /*<! When Phone deauthenticate sta from SOFTAP, this event happen */
/* recv data */
ESP_BLUFI_EVENT_RECV_STA_BSSID, /*<! When Phone send STA BSSID to ESP32 to connect, this event happen */
ESP_BLUFI_EVENT_RECV_STA_SSID, /*<! When Phone send STA SSID to ESP32 to connect, this event happen */
ESP_BLUFI_EVENT_RECV_STA_PASSWD, /*<! When Phone send STA PASSWORD to ESP32 to connect, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_SSID, /*<! When Phone send SOFTAP SSID to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD, /*<! When Phone send SOFTAP PASSWORD to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM, /*<! When Phone send SOFTAP max connection number to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE, /*<! When Phone send SOFTAP authentication mode to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL, /*<! When Phone send SOFTAP channel to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_USERNAME, /*<! When Phone send username to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_CA_CERT, /*<! When Phone send CA certificate to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_CLIENT_CERT, /*<! When Phone send Client certificate to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_SERVER_CERT, /*<! When Phone send Server certificate to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY, /*<! When Phone send Client Private key to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY, /*<! When Phone send Server Private key to ESP32, this event happen */
} esp_blufi_cb_event_t;
/// BLUFI config status
typedef enum {
ESP_BLUFI_CONFIG_OK = 0,
ESP_BLUFI_CONFIG_FAILED,
} esp_blufi_config_state_t;
ESP_BLUFI_STA_CONN_SUCCESS = 0x00,
ESP_BLUFI_STA_CONN_FAIL = 0x01,
} esp_blufi_sta_conn_state_t;
/// BLUFI init status
typedef enum {
@ -49,6 +69,29 @@ typedef enum {
ESP_BLUFI_DEINIT_FAILED = 0,
} esp_blufi_deinit_state_t;
/**
* @brief BLUFI extra information structure
*/
typedef struct {
//station
uint8_t sta_bssid[6]; /*!< BSSID of station interface */
bool sta_bssid_set; /*!< is BSSID of station interface set */
uint8_t *sta_ssid; /*!< SSID of station interface */
int sta_ssid_len; /*!< length of SSID of station interface */
uint8_t *sta_passwd; /*!< password of station interface */
int sta_passwd_len; /*!< length of password of station interface */
uint8_t *softap_ssid; /*!< SSID of softap interface */
int softap_ssid_len; /*!< length of SSID of softap interface */
uint8_t *softap_passwd; /*!< password of station interface */
int softap_passwd_len; /*!< length of password of station interface */
uint8_t softap_authmode; /*!< authentication mode of softap interface */
bool softap_authmode_set; /*!< is authentication mode of softap interface set */
uint8_t softap_max_conn_num; /*!< max connection number of softap interface */
bool softap_max_conn_num_set; /*!< is max connection number of softap interface set */
uint8_t softap_channel; /*!< channel of softap interface */
bool softap_channel_set; /*!< is channel of softap interface set */
} esp_blufi_extra_info_t;
/**
* @brief BLUFI callback parameters union
*/
@ -68,43 +111,207 @@ typedef union {
} deinit_finish; /*!< Blufi callback param of ESP_BLUFI_EVENT_DEINIT_FINISH */
/**
* @brief ESP_BLUFI_EVENT_RECV_DATA
* @brief ESP_BLUFI_EVENT_SET_WIFI_MODE
*/
struct blufi_set_wifi_mode_evt_param {
wifi_mode_t op_mode; /*!< Wifi operation mode */
} wifi_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_INIT_FINISH */
/**
* @brief ESP_BLUFI_EVENT_CONNECT
*/
struct blufi_recv_evt_param {
uint8_t data[ESP_BLUFI_RECV_DATA_LEN_MAX]; /*!< Blufi receive data */
uint8_t data_len; /*!< Blufi receive data length */
} recv_data; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_DATA */
struct blufi_connect_evt_param {
esp_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
} connect; /*!< Blufi callback param of ESP_BLUFI_EVENT_CONNECT */
/**
* @brief ESP_BLUFI_EVENT_DISCONNECT
*/
struct blufi_disconnect_evt_param {
esp_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
} disconnect; /*!< Blufi callback param of ESP_BLUFI_EVENT_DISCONNECT */
/* ESP_BLUFI_EVENT_REQ_WIFI_CONNECT */ /* No callback param */
/* ESP_BLUFI_EVENT_REQ_WIFI_DISCONNECT */ /* No callback param */
/**
* @brief ESP_BLUFI_EVENT_RECV_STA_BSSID
*/
struct blufi_recv_sta_bssid_evt_param {
uint8_t bssid[6]; /*!< BSSID */
} sta_bssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_BSSID */
/**
* @brief ESP_BLUFI_EVENT_RECV_STA_SSID
*/
struct blufi_recv_sta_ssid_evt_param {
uint8_t *ssid; /*!< SSID */
int ssid_len; /*!< SSID length */
} sta_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_SSID */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_STA_PASSWD
*/
struct blufi_recv_sta_passwd_evt_param {
uint8_t *passwd; /*!< Password */
int passwd_len; /*!< Password Length */
} sta_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_PASSWD */
/**
* @brief ESP_BLUFI_EVENT_RECV_SOFTAP_SSID
*/
struct blufi_recv_softap_ssid_evt_param {
uint8_t *ssid; /*!< SSID */
int ssid_len; /*!< SSID length */
} softap_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_SSID */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD
*/
struct blufi_recv_softap_passwd_evt_param {
uint8_t *passwd; /*!< Password */
int passwd_len; /*!< Password Length */
} softap_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD */
/**
* @brief ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM
*/
struct blufi_recv_softap_max_conn_num_evt_param {
int max_conn_num; /*!< SSID */
} softap_max_conn_num; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE
*/
struct blufi_recv_softap_auth_mode_evt_param {
wifi_auth_mode_t auth_mode; /*!< Authentication mode */
} softap_auth_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL
*/
struct blufi_recv_softap_channel_evt_param {
uint8_t channel; /*!< Authentication mode */
} softap_channel; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL */
/**
* @brief ESP_BLUFI_EVENT_RECV_USERNAME
*/
struct blufi_recv_username_evt_param {
uint8_t *name; /*!< Username point */
int name_len; /*!< Username length */
} username; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_USERNAME*/
/**
* @brief ESP_BLUFI_EVENT_RECV_CA_CERT
*/
struct blufi_recv_ca_evt_param {
uint8_t *cert; /*!< CA certificate point */
int cert_len; /*!< CA certificate length */
} ca; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CA_CERT */
/**
* ESP_BLUFI_EVENT_RECV_CLIENT_CERT
*/
struct blufi_recv_client_cert_evt_param {
uint8_t *cert; /*!< Client certificate point */
int cert_len; /*!< Client certificate length */
} client_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_CERT */
/**
* ESP_BLUFI_EVENT_RECV_SERVER_CERT
*/
struct blufi_recv_server_cert_evt_param {
uint8_t *cert; /*!< Client certificate point */
int cert_len; /*!< Client certificate length */
} server_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_CERT */
/**
* ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY
*/
struct blufi_recv_client_pkey_evt_param {
uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
int pkey_len; /*!< Client Private key length */
} client_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY */
/**
* ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY
*/
struct blufi_recv_server_pkey_evt_param {
uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
int pkey_len; /*!< Client Private key length */
} server_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY */
} esp_blufi_cb_param_t;
/**
* @brief BLUFI callback function type
* @brief BLUFI event callback function type
* @param event : Event type
* @param param : Point to callback parameter, currently is union type
*/
typedef void (* esp_blufi_cb_t)(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
typedef void (* esp_blufi_event_cb_t)(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
/* security function declare */
/**
* @brief BLUFI negotiate data handler
* @param data : data from phone
* @param len : length of data from phone
* @param output_data : data want to send to phone
* @param output_len : length of data want to send to phone
*/
typedef void (*esp_blufi_negotiate_data_handler_t)(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
/**
* @brief BLUFI encrypt the data after negotiate a share key
* @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
* @param crypt_data : plain text and encrypted data, the encrypt function must support autochthonous encrypt
* @param crypt_len : length of plain text
* @return Nonnegative number is encrypted length, if error, return negative number;
*/
typedef int (* esp_blufi_encrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int cyprt_len);
/**
* @brief BLUFI decrypt the data after negotiate a share key
* @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
* @param crypt_data : encrypted data and plain text, the encrypt function must support autochthonous decrypt
* @param crypt_len : length of encrypted text
* @return Nonnegative number is decrypted length, if error, return negative number;
*/
typedef int (* esp_blufi_decrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
/**
* @brief BLUFI checksum
* @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
* @param data : data need to checksum
* @param len : length of data
*/
typedef uint16_t (*esp_blufi_checksum_func_t)(uint8_t iv8, uint8_t *data, int len);
/**
* @brief BLUFI callback functions type
*/
typedef struct {
esp_blufi_event_cb_t event_cb; /*!< BLUFI event callback */
esp_blufi_negotiate_data_handler_t negotiate_data_handler; /*!< BLUFI negotiate data function for negotiate share key */
esp_blufi_encrypt_func_t encrypt_func; /*!< BLUFI encrypt data function with share key generated by negotiate_data_handler */
esp_blufi_decrypt_func_t decrypt_func; /*!< BLUFI decrypt data function with share key generated by negotiate_data_handler */
esp_blufi_checksum_func_t checksum_func; /*!< BLUFI check sum function (FCS) */
} esp_blufi_callbacks_t;
/**
*
* @brief This function is called to receive blufi callback event
*
* @param[in] callback: callback function
* @param[in] callbacks: callback functions
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_register_callback(esp_blufi_cb_t callback);
/**
*
* @brief This function is called to send config state to phone
*
* @param[in] state: blufi config OK or not
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_send_config_state(esp_blufi_config_state_t state);
esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks);
/**
*
@ -124,6 +331,19 @@ esp_err_t esp_blufi_profile_init(void);
*/
esp_err_t esp_blufi_profile_deinit(void);
/**
*
* @brief This function is called to send wifi connection report
* @param opmode : wifi opmode
* @param sta_conn_state : station is already in connection or not
* @param softap_conn_num : softap connection number
* @param extra_info : extra information, such as sta_ssid, softap_ssid and etc.
*
* @return ESP_OK - success, other - failed
*
*/
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);
#ifdef __cplusplus
}
#endif

View file

@ -1014,8 +1014,8 @@ void BTA_DmSetBleAdvParamsAll (UINT16 adv_int_min, UINT16 adv_int_max,
#if BLE_INCLUDED == TRUE
tBTA_DM_API_BLE_ADV_PARAMS_ALL *p_msg;
APPL_TRACE_ERROR ("BTA_DmSetBleAdvParamsAll: %d, %d\n", adv_int_min, adv_int_max);
APPL_TRACE_ERROR ("adv_type = %d, addr_type_own = %d, chnl_map = %d, adv_fil_pol = %d\n",
APPL_TRACE_API ("BTA_DmSetBleAdvParamsAll: %d, %d\n", adv_int_min, adv_int_max);
APPL_TRACE_API ("adv_type = %d, addr_type_own = %d, chnl_map = %d, adv_fil_pol = %d\n",
adv_type, addr_type_own, chnl_map, adv_fil_pol);
if ((p_msg = (tBTA_DM_API_BLE_ADV_PARAMS_ALL *) GKI_getbuf(sizeof(tBTA_DM_API_BLE_ADV_PARAMS_ALL)
+ sizeof(tBLE_BD_ADDR))) != NULL) {

View file

@ -1,164 +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 "blufi_adv.h"
/*******************************************************************************
**
** Function BlufiConfigadvData
**
** Description This function is called to override the BTA default ADV parameters.
**
** adv_data: Pointer to User defined ADV data structure. This
** memory space can not be freed until p_adv_data_cback
** is received.
** p_adv_data_cback: set adv data complete callback.
**
** Returns None
**
*******************************************************************************/
void BlufiBleConfigadvData(tBLUFI_BLE_ADV_DATA *adv_data,
tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback)
{
tBTA_BLE_AD_MASK data_mask = 0;
if (adv_data->adv_name != NULL) {
data_mask |= BTM_BLE_AD_BIT_DEV_NAME;
BTA_DmSetDeviceName(adv_data->adv_name);
}
if (adv_data->ble_adv_data.int_range.low != 0 ||
adv_data->ble_adv_data.int_range.hi != 0) {
data_mask |= BTM_BLE_AD_BIT_INT_RANGE;
}
if (adv_data->ble_adv_data.p_manu != NULL) {
data_mask |= BTM_BLE_AD_BIT_MANU;
}
if (adv_data->ble_adv_data.p_services != NULL) {
data_mask |= BTM_BLE_AD_BIT_SERVICE;
}
if (adv_data->ble_adv_data.p_service_32b != NULL) {
data_mask |= BTM_BLE_AD_BIT_SERVICE_32;
}
if (adv_data->ble_adv_data.p_services_128b != NULL) {
data_mask |= BTM_BLE_AD_BIT_SERVICE_128;
}
if (adv_data->ble_adv_data.p_sol_services != NULL) {
data_mask |= BTM_BLE_AD_BIT_SERVICE_SOL;
}
if (adv_data->ble_adv_data.p_sol_service_32b != NULL) {
data_mask |= BTM_BLE_AD_BIT_SERVICE_32SOL;
}
if (adv_data->ble_adv_data.p_sol_service_128b != NULL) {
data_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL;
}
if (adv_data->ble_adv_data.p_service_data != NULL) {
data_mask |= BTM_BLE_AD_BIT_SERVICE_DATA;
}
if (adv_data->ble_adv_data.appearance != 0) {
data_mask |= BTM_BLE_AD_BIT_APPEARANCE;
}
if (adv_data->ble_adv_data.p_proprietary != NULL) {
data_mask |= BTM_BLE_AD_BIT_PROPRIETARY;
}
if (adv_data->ble_adv_data.tx_power != 0) {
data_mask |= BTM_BLE_AD_BIT_TX_PWR;
}
BTA_DmBleSetAdvConfig(data_mask, &(adv_data->ble_adv_data), p_adv_data_cback);
}
/*******************************************************************************
**
** 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);
}

File diff suppressed because it is too large Load diff

View file

@ -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 <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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, &param, 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;
}
}

View file

@ -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__ */

View file

@ -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)<<BLUFI_SUBTYPE_SHIFT))
#define BLUFI_TYPE_CTRL 0x0
#define BLUFI_TYPE_CTRL_SUBTYPE_ACK 0x00
#define BLUFI_TYPE_CTRL_SUBTYPE_SET_SEC_MODE 0x01
#define BLUFI_TYPE_CTRL_SUBTYPE_SET_WIFI_OPMODE 0x02
#define BLUFI_TYPE_CTRL_SUBTYPE_CONN_TO_AP 0x03
#define BLUFI_TYPE_CTRL_SUBTYPE_DISCONN_FROM_AP 0x04
#define BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS 0x05
#define BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA 0x06
#define BLUFI_TYPE_DATA 0x1
#define BLUFI_TYPE_DATA_SUBTYPE_NEG 0x00
#define BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID 0x01
#define BLUFI_TYPE_DATA_SUBTYPE_STA_SSID 0x02
#define BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD 0x03
#define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID 0x04
#define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD 0x05
#define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM 0x06
#define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE 0x07
#define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL 0x08
#define BLUFI_TYPE_DATA_SUBTYPE_USERNAME 0x09
#define BLUFI_TYPE_DATA_SUBTYPE_CA 0x0a
#define BLUFI_TYPE_DATA_SUBTYPE_CLIENT_CERT 0x0b
#define BLUFI_TYPE_DATA_SUBTYPE_SERVER_CERT 0x0c
#define BLUFI_TYPE_DATA_SUBTYPE_CLIENT_PRIV_KEY 0x0d
#define BLUFI_TYPE_DATA_SUBTYPE_SERVER_PRIV_KEY 0x0e
#define BLUFI_TYPE_DATA_SUBTYPE_WIFI_REP 0x0f
#define BLUFI_TYPE_IS_CTRL(type) (BLUFI_GET_TYPE((type)) == BLUFI_TYPE_CTRL)
#define BLUFI_TYPE_IS_DATA(type) (BLUFI_GET_TYPE((type)) == BLUFI_TYPE_DATA)
#define BLUFI_TYPE_IS_CTRL_ACK(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_ACK)
#define BLUFI_TYPE_IS_CTRL_START_NEG(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_START_NEG)
#define BLUFI_TYPE_IS_CTRL_STOP_NEG(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_STOP_NEG)
#define BLUFI_TYPE_IS_CTRL_SET_WIFI_OPMODE(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_SET_WIFI_OPMODE)
#define BLUFI_TYPE_IS_CTRL_CONN_WIFI(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_CONN_TO_AP)
#define BLUFI_TYPE_IS_CTRL_DISCONN_WIFI(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_DISCONN_FROM_AP)
#define BLUFI_TYPE_IS_CTRL_GET_WIFI_STATUS(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS)
#define BLUFI_TYPE_IS_CTRL_DEAUTHENTICATE_STA(type) (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA)
#define BLUFI_TYPE_IS_DATA_NEG(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_NEG)
#define BLUFI_TYPE_IS_DATA_STA_BSSID(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID)
#define BLUFI_TYPE_IS_DATA_STA_SSID(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_STA_SSID)
#define BLUFI_TYPE_IS_DATA_STA_PASSWD(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD)
#define BLUFI_TYPE_IS_DATA_SOFTAP_SSID(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID)
#define BLUFI_TYPE_IS_DATA_SOFTAP_PASSWD(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD)
#define BLUFI_TYPE_IS_DATA_SOFTAP_MAX_CONN_NUM(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM)
#define BLUFI_TYPE_IS_DATA_SOFTAP_AUTH_MODE(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE)
#define BLUFI_TYPE_IS_DATA_SOFTAP_CHANNEL(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL)
#define BLUFI_TYPE_IS_DATA_USERNAME(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_USERNAME)
#define BLUFI_TYPE_IS_DATA_CA(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_CA)
#define BLUFI_TYPE_IS_DATA_CLEINT_CERT(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_CLIENT_CERT)
#define BLUFI_TYPE_IS_DATA_SERVER_CERT(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SERVER_CERT)
#define BLUFI_TYPE_IS_DATA_CLIENT_PRIV_KEY(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_CLIENT_PRIV_KEY)
#define BLUFI_TYPE_IS_DATA_SERVER_PRIV_KEY(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SERVER_PRIV_KEY)
// packet frame control
#define BLUFI_FC_ENC_MASK 0x01
#define BLUFI_FC_CHECK_MASK 0x02
#define BLUFI_FC_DIR_MASK 0x04
#define BLUFI_FC_REQ_ACK_MASK 0x08
#define BLUFI_FC_FRAG_MASK 0x10
#define BLUFI_FC_ENC 0x01
#define BLUFI_FC_CHECK 0x02
#define BLUFI_FC_DIR_P2E 0x00
#define BLUFI_FC_DIR_E2P 0x04
#define BLUFI_FC_REQ_ACK 0x08
#define BLUFI_FC_FRAG 0x10
#define BLUFI_FC_IS_ENC(fc) ((fc) & BLUFI_FC_ENC_MASK)
#define BLUFI_FC_IS_CHECK(fc) ((fc) & BLUFI_FC_CHECK_MASK)
#define BLUFI_FC_IS_DIR_P2E(fc) ((fc) & BLUFI_FC_DIR_P2E_MASK)
#define BLUFI_FC_IS_DIR_E2P(fc) (!((fc) & BLUFI_DIR_P2E_MASK))
#define BLUFI_FC_IS_REQ_ACK(fc) ((fc) & BLUFI_FC_REQ_ACK_MASK)
#define BLUFI_FC_IS_FRAG(fc) ((fc) & BLUFI_FC_FRAG_MASK)
#define BLUFI_FRAG_DATA_MAX_LEN 50
//function declare
void btc_blufi_protocol_handler(uint8_t type, uint8_t *data, int len);
void btc_blufi_send_encap(uint8_t type, uint8_t *data, int data_len);
void btc_blufi_set_callbacks(esp_blufi_callbacks_t *callbacks);
void btc_blufi_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
void btc_blufi_cb_deep_free(btc_msg_t *msg);
#endif /* __BLUFI_INT_H__ */

View file

@ -22,31 +22,23 @@
typedef enum {
BTC_BLUFI_ACT_INIT = 0,
BTC_BLUFI_ACT_DEINIT,
BTC_BLUFI_ACT_SEND_CFG_STATE,
BTC_BLUFI_ACT_SEND_CFG_REPORT,
} btc_blufi_act_t;
typedef enum {
BTC_BLUFI_CB_ACT_INIT_FINISH = 0,
BTC_BLUFI_CB_ACT_DEINIT_FINISH,
BTC_BLUFI_CB_ACT_RECV_DATA,
} btc_blufi_cb_act_t;
typedef union {
#if 0
//BTC_BLUFI_ACT_INIT = 0,
struct blufi_init_param {
} init;
//BTC_BLUFI_ACT_DEINIT,
struct blufi_deinit_param {
} deinit;
#endif
//BTC_BLUFI_ACT_SEND_CFG_STATE,
struct blufi_send_cfg_state_pram {
esp_blufi_config_state_t state;
} cfg_state;
struct blufi_cfg_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;
int extra_info_len;
} wifi_conn_report;
} btc_blufi_args_t;
void btc_blufi_cb_handler(btc_msg_t *msg);
void btc_blufi_call_handler(btc_msg_t *msg);
void btc_blufi_set_callbacks(esp_blufi_callbacks_t *callbacks);
void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
void btc_blufi_call_deep_free(btc_msg_t *msg);
#endif /* __BTC_BLUFI_PRF_H__ */

View file

@ -201,7 +201,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_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;

View file

@ -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;

View file

@ -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");
}

View file

@ -8,3 +8,4 @@ BT COMMON
BLE GATT DEFINE <esp_gatt_defs>
BLE GATT SERVER <esp_gatts>
BLE GATT CLIENT <esp_gattc>
BLE BLUFI <esp_blufi>

128
docs/api/esp_blufi.rst Normal file
View file

@ -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 <https://github.com/espressif/esp-idf/tree/master/examples>`_ folder of `espressif/esp-idf <https://github.com/espressif/esp-idf/>`_ repository, that contains the following example:
`12_blufi <https://github.com/espressif/esp-idf/blob/master/examples/12_blufi/main/>`_
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 <https://github.com/espressif/esp-idf/blob/master/components/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

View file

@ -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 <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#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;
}

View file

@ -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 <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#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);
}

View file

@ -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

View file

@ -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 <stdint.h>
#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__ */

View file

@ -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__ */

View file

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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();
}

View file

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View file

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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();
}