Merge branch 'feature/btdm_blufi' into 'master'

Feature/btdm blufi

This is new BLUFI for config wifi connection through bluetooth.


See merge request !359
This commit is contained in:
Ivan Grokhotkov 2017-01-05 21:54:45 +08:00
commit ded13ac8e6
38 changed files with 2273 additions and 1094 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

@ -21,7 +21,7 @@
static bool esp_already_enable = false;
static bool esp_already_init = false;
esp_err_t esp_enable_bluetooth(void)
esp_err_t esp_bluedroid_enable(void)
{
btc_msg_t msg;
future_t **future_p;
@ -53,7 +53,7 @@ esp_err_t esp_enable_bluetooth(void)
return ESP_OK;
}
esp_err_t esp_disable_bluetooth(void)
esp_err_t esp_bluedroid_disable(void)
{
btc_msg_t msg;
future_t **future_p;
@ -85,7 +85,7 @@ esp_err_t esp_disable_bluetooth(void)
return ESP_OK;
}
esp_err_t esp_init_bluetooth(void)
esp_err_t esp_bluedroid_init(void)
{
btc_msg_t msg;
future_t **future_p;
@ -120,7 +120,7 @@ esp_err_t esp_init_bluetooth(void)
}
esp_err_t esp_deinit_bluetooth(void)
esp_err_t esp_bluedroid_deinit(void)
{
btc_msg_t msg;
future_t **future_p;

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

@ -22,22 +22,22 @@ extern "C" {
#endif
/**
* @brief Enable bluetooth, must after esp_init_bluetooth()
* @brief Enable bluetooth, must after esp_bluedroid_init()
*
* @return
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_enable_bluetooth(void);
esp_err_t esp_bluedroid_enable(void);
/**
* @brief Disable bluetooth, must prior to esp_deinit_bluetooth()
* @brief Disable bluetooth, must prior to esp_bluedroid_deinit()
*
* @return
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_disable_bluetooth(void);
esp_err_t esp_bluedroid_disable(void);
/**
* @brief Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff
@ -46,7 +46,7 @@ esp_err_t esp_disable_bluetooth(void);
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_init_bluetooth(void);
esp_err_t esp_bluedroid_init(void);
/**
* @brief Deinit and free the resource for bluetooth, must be after every bluetooth stuff
@ -55,7 +55,7 @@ esp_err_t esp_init_bluetooth(void);
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_deinit_bluetooth(void);
esp_err_t esp_bluedroid_deinit(void);
#ifdef __cplusplus
}

View File

@ -25,6 +25,19 @@
extern "C" {
#endif
/**@{
* BLE_ADV_DATA_FLAG data flag bit definition used for advertising data flag
*/
#define ESP_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
#define ESP_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
#define ESP_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
#define ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
#define ESP_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
#define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
/**
* @}
*/
/// GAP BLE callback event type
typedef enum {
ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT = 0, /*!< When advertising data set complete, the event comes */
@ -126,7 +139,7 @@ typedef struct {
uint8_t *p_service_data; /*!< Service data point */
uint16_t service_uuid_len; /*!< Service uuid length */
uint8_t *p_service_uuid; /*!< Service uuid array point */
uint8_t flag; /*!< Advertising flag of discovery mode */
uint8_t flag; /*!< Advertising flag of discovery mode, see BLE_ADV_DATA_FLAG detail */
} esp_ble_adv_data_t;
/// Own BD address source of the device

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

@ -109,6 +109,7 @@ static void btc_to_bta_adv_data(esp_ble_adv_data_t *p_adv_data, tBTA_BLE_ADV_DAT
if (p_adv_data->flag != 0) {
mask = BTM_BLE_AD_BIT_FLAGS;
bta_adv_data->flag = p_adv_data->flag;
}
if (p_adv_data->include_name) {
@ -200,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;
@ -220,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

@ -57,7 +57,7 @@ typedef struct {
static hci_hal_env_t hci_hal_env;
static const hci_hal_t interface;
static const hci_hal_callbacks_t *callbacks;
static const vhci_host_callback_t vhci_host_cb;
static const esp_vhci_host_callback_t vhci_host_cb;
static xTaskHandle xHciH4TaskHandle;
static xQueueHandle xHciH4Queue;
@ -105,7 +105,7 @@ static bool hal_open(const hci_hal_callbacks_t *upper_callbacks)
xTaskCreate(hci_hal_h4_rx_handler, HCI_H4_TASK_NAME, HCI_H4_TASK_STACK_SIZE, NULL, HCI_H4_TASK_PRIO, &xHciH4TaskHandle);
//register vhci host cb
API_vhci_host_register_callback(&vhci_host_cb);
esp_vhci_host_register_callback(&vhci_host_cb);
return true;
@ -148,7 +148,7 @@ static uint16_t transmit_data(serial_data_type_t type,
BTTRC_DUMP_BUFFER("Transmit Pkt", data, length);
// TX Data to target
API_vhci_host_send_packet(data, length);
esp_vhci_host_send_packet(data, length);
// Be nice and restore the old value of that byte
*(data) = previous_byte;
@ -278,7 +278,7 @@ static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
return 0;
}
static const vhci_host_callback_t vhci_host_cb = {
static const esp_vhci_host_callback_t vhci_host_cb = {
.notify_host_send_available = host_send_pkt_available_cb,
.notify_host_recv = host_recv_pkt_cb,
};

View File

@ -228,7 +228,7 @@ static void hci_host_thread_handler(void *arg)
if (pdTRUE == xQueueReceive(xHciHostQueue, &e, (portTickType)portMAX_DELAY)) {
if (e.sig == 0xff) {
if (API_vhci_host_check_send_available()) {
if (esp_vhci_host_check_send_available()) {
/*Now Target only allowed one packet per TX*/
BT_HDR *pkt = packet_fragmenter->fragment_current_packet();
if (pkt != NULL) {

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

@ -35,6 +35,15 @@
extern void btdm_osi_funcs_register(void *osi_funcs);
extern void btdm_controller_init(void);
/* VHCI function interface */
typedef struct vhci_host_callback {
void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
} vhci_host_callback_t;
extern bool API_vhci_host_check_send_available(void);
extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
extern void API_vhci_host_register_callback(const vhci_host_callback_t *callback);
#define BT_DEBUG(...)
#define BT_API_CALL_CHECK(info, api_call, ret) \
@ -118,13 +127,28 @@ static struct osi_funcs_t osi_funcs = {
._read_efuse_mac = esp_efuse_read_mac,
};
bool esp_vhci_host_check_send_available(void)
{
return API_vhci_host_check_send_available();
}
void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
{
API_vhci_host_send_packet(data, len);
}
void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
{
API_vhci_host_register_callback((const vhci_host_callback_t *)callback);
}
static void bt_controller_task(void *pvParam)
{
btdm_osi_funcs_register(&osi_funcs);
btdm_controller_init();
}
void bt_controller_init()
void esp_bt_controller_init()
{
xTaskCreatePinnedToCore(bt_controller_task, "btController",
ESP_TASK_BT_CONTROLLER_STACK, NULL,

View File

@ -29,35 +29,35 @@ extern "C" {
*
* This function should be called only once, before any other BT functions are called.
*/
void bt_controller_init(void);
void esp_bt_controller_init(void);
/** @brief vhci_host_callback
/** @brief esp_vhci_host_callback
* used for vhci call host function to notify what host need to do
*/
typedef struct vhci_host_callback {
typedef struct esp_vhci_host_callback {
void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
} vhci_host_callback_t;
} esp_vhci_host_callback_t;
/** @brief API_vhci_host_check_send_available
/** @brief esp_vhci_host_check_send_available
* used for check actively if the host can send packet to controller or not.
* @return true for ready to send, false means cannot send packet
*/
bool API_vhci_host_check_send_available(void);
bool esp_vhci_host_check_send_available(void);
/** @brief API_vhci_host_send_packet
/** @brief esp_vhci_host_send_packet
* host send packet to controller
* @param data the packet point
*,@param len the packet length
*/
void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
void esp_vhci_host_send_packet(uint8_t *data, uint16_t len);
/** @brief API_vhci_host_register_callback
/** @brief esp_vhci_host_register_callback
* register the vhci referece callback, the call back
* struct defined by vhci_host_callback structure.
* @param callback vhci_host_callback type variable
* @param callback esp_vhci_host_callback type variable
*/
void API_vhci_host_register_callback(const vhci_host_callback_t *callback);
void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback);
#ifdef __cplusplus
}

@ -1 +1 @@
Subproject commit 91657e0c4025f5a694b0a89f449c347b0f2fdf79
Subproject commit 566acfd8c61a4ba0fb6b9026c89488b01af0fff0

View File

@ -229,7 +229,7 @@ esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_da
*
* Applications which don't need to enable PHY on every start up should
* disable this menuconfig option and call esp_phy_init before calling
* esp_wifi_init or bt_controller_init. See do_phy_init function in
* esp_wifi_init or esp_bt_controller_init. See do_phy_init function in
* cpu_start.c for an example of using this function.
*
* @param init_data PHY parameters. Default set of parameters can

View File

@ -4,7 +4,7 @@ Bluetooth
.. toctree::
:caption: Bluetooth APIs
Bluetooth VHCI <vhci>
Bluetooth Controller && VHCI <controller_vhci>
Bluetooth Common <bt_common>
Bluetooth Classic
Bluetooth LE <bt_le>

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>

View File

@ -1,5 +1,5 @@
VHCI
====
Controller && VHCI
==================
Overview
--------
@ -30,12 +30,24 @@ Header Files
Type Definitions
^^^^^^^^^^^^^^^^
.. doxygenstruct:: vhci_host_callback
.. doxygentypedef:: esp_vhci_host_callback_t
Enumerations
^^^^^^^^^^^^
Structures
^^^^^^^^^^
.. doxygenstruct:: esp_vhci_host_callback
:members:
Functions
^^^^^^^^^
.. doxygenfunction:: API_vhci_host_check_send_available
.. doxygenfunction:: API_vhci_host_register_callback
.. doxygenfunction:: API_vhci_host_send_packet
.. doxygenfunction:: bt_controller_init
.. doxygenfunction:: esp_bt_controller_init
.. doxygenfunction:: esp_vhci_host_check_send_available
.. doxygenfunction:: esp_vhci_host_send_packet
.. doxygenfunction:: esp_vhci_host_register_callback

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

@ -42,8 +42,8 @@ Structures
Functions
^^^^^^^^^
.. doxygenfunction:: esp_enable_bluetooth
.. doxygenfunction:: esp_disable_bluetooth
.. doxygenfunction:: esp_init_bluetooth
.. doxygenfunction:: esp_deinit_bluetooth
.. doxygenfunction:: esp_bluedroid_enable
.. doxygenfunction:: esp_bluedroid_disable
.. doxygenfunction:: esp_bluedroid_init
.. doxygenfunction:: esp_bluedroid_deinit

View File

@ -33,6 +33,12 @@ Header Files
Macros
^^^^^^
.. doxygendefine:: ESP_BLE_ADV_FLAG_LIMIT_DISC
.. doxygendefine:: ESP_BLE_ADV_FLAG_GEN_DISC
.. doxygendefine:: ESP_BLE_ADV_FLAG_BREDR_NOT_SPT
.. doxygendefine:: ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT
.. doxygendefine:: ESP_BLE_ADV_FLAG_DMT_HOST_SPT
.. doxygendefine:: ESP_BLE_ADV_FLAG_NON_LIMIT_DISC
.. doxygendefine:: ESP_BLE_ADV_DATA_LEN_MAX
Type Definitions

View File

@ -73,7 +73,7 @@ static int host_rcv_pkt(uint8_t *data, uint16_t len)
return 0;
}
static vhci_host_callback_t vhci_host_cb = {
static esp_vhci_host_callback_t vhci_host_cb = {
controller_rcv_pkt_ready,
host_rcv_pkt
};
@ -139,13 +139,13 @@ static uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_
static void hci_cmd_send_reset(void)
{
uint16_t sz = make_cmd_reset (hci_cmd_buf);
API_vhci_host_send_packet(hci_cmd_buf, sz);
esp_vhci_host_send_packet(hci_cmd_buf, sz);
}
static void hci_cmd_send_ble_adv_start(void)
{
uint16_t sz = make_cmd_ble_set_adv_enable (hci_cmd_buf, 1);
API_vhci_host_send_packet(hci_cmd_buf, sz);
esp_vhci_host_send_packet(hci_cmd_buf, sz);
}
static void hci_cmd_send_ble_set_adv_param(void)
@ -168,7 +168,7 @@ static void hci_cmd_send_ble_set_adv_param(void)
peer_addr,
adv_chn_map,
adv_filter_policy);
API_vhci_host_send_packet(hci_cmd_buf, sz);
esp_vhci_host_send_packet(hci_cmd_buf, sz);
}
static void hci_cmd_send_ble_set_adv_data(void)
@ -185,7 +185,7 @@ static void hci_cmd_send_ble_set_adv_data(void)
adv_data_len = 5 + name_len;
uint16_t sz = make_cmd_ble_set_adv_data(hci_cmd_buf, adv_data_len, (uint8_t *)adv_data);
API_vhci_host_send_packet(hci_cmd_buf, sz);
esp_vhci_host_send_packet(hci_cmd_buf, sz);
}
/*
@ -195,11 +195,11 @@ void bleAdvtTask(void *pvParameters)
{
int cmd_cnt = 0;
bool send_avail = false;
API_vhci_host_register_callback(&vhci_host_cb);
esp_vhci_host_register_callback(&vhci_host_cb);
printf("BLE advt task start\n");
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
send_avail = API_vhci_host_check_send_available();
send_avail = esp_vhci_host_check_send_available();
if (send_avail) {
switch (cmd_cnt) {
case 0: hci_cmd_send_reset(); ++cmd_cnt; break;
@ -214,7 +214,7 @@ void bleAdvtTask(void *pvParameters)
void app_main()
{
bt_controller_init();
esp_bt_controller_init();
xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0);
}

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_enable_bluetooth();
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_disable_bluetooth();
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_disable_bluetooth(); //close bluetooth function
//esp_deinit_bluetooth(); //free bluetooth resource
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
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);
bt_controller_init();
xTaskCreatePinnedToCore(&wifiTestTask, "wifiTestTask", 2048, NULL, 20, NULL, 0);
LOG_ERROR("%s init bluetooth\n", __func__);
ret = esp_init_bluetooth();
if (ret) {
LOG_ERROR("%s init bluetooth failed\n", __func__);
return;
}
blufi_init();
}

View File

@ -2,7 +2,7 @@
//
// 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
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
@ -30,6 +30,8 @@
#include "esp_bt_main.h"
#include "esp_bt_main.h"
#define GATTS_TAG "GATTS_DEMO"
///Declare the static function
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
@ -68,7 +70,7 @@ static esp_ble_adv_data_t test_adv_data = {
.p_service_data = NULL,
.service_uuid_len = 32,
.p_service_uuid = test_service_uuid128,
.flag = 0x2,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
static esp_ble_adv_params_t test_adv_params = {
@ -115,8 +117,6 @@ static struct gatts_profile_inst gl_profile_tab[PROFILE_NUM] = {
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
LOG_ERROR("GAP_EVT, event %d\n", event);
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
esp_ble_gap_start_advertising(&test_adv_params);
@ -129,23 +129,19 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
switch (event) {
case ESP_GATTS_REG_EVT:
LOG_INFO("REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
ESP_LOGI(GATTS_TAG, "REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
gl_profile_tab[PROFILE_A_APP_ID].service_id.is_primary = true;
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.inst_id = 0x00;
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_A;
LOG_INFO("%s %d\n", __func__, __LINE__);
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
LOG_INFO("%s %d\n", __func__, __LINE__);
esp_ble_gap_config_adv_data(&test_adv_data);
LOG_INFO("%s %d\n", __func__, __LINE__);
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_A);
LOG_INFO("%s %d\n", __func__, __LINE__);
break;
case ESP_GATTS_READ_EVT: {
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
esp_gatt_rsp_t rsp;
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
rsp.attr_value.handle = param->read.handle;
@ -159,8 +155,8 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
break;
}
case ESP_GATTS_WRITE_EVT: {
LOG_INFO("GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
LOG_INFO("GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
break;
}
@ -170,7 +166,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
case ESP_GATTS_UNREG_EVT:
break;
case ESP_GATTS_CREATE_EVT:
LOG_INFO("CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
ESP_LOGI(GATTS_TAG, "CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_A;
@ -184,7 +180,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
case ESP_GATTS_ADD_INCL_SRVC_EVT:
break;
case ESP_GATTS_ADD_CHAR_EVT:
LOG_INFO("ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
ESP_LOGI(GATTS_TAG, "ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
gl_profile_tab[PROFILE_A_APP_ID].char_handle = param->add_char.attr_handle;
@ -194,19 +190,19 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
break;
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
LOG_INFO("ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
ESP_LOGI(GATTS_TAG, "ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
break;
case ESP_GATTS_DELETE_EVT:
break;
case ESP_GATTS_START_EVT:
LOG_INFO("SERVICE_START_EVT, status %d, service_handle %d\n",
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, status %d, service_handle %d\n",
param->start.status, param->start.service_handle);
break;
case ESP_GATTS_STOP_EVT:
break;
case ESP_GATTS_CONNECT_EVT:
LOG_INFO("SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
param->connect.conn_id,
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5],
@ -227,23 +223,19 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
switch (event) {
case ESP_GATTS_REG_EVT:
LOG_INFO("REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
ESP_LOGI(GATTS_TAG, "REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
gl_profile_tab[PROFILE_A_APP_ID].service_id.is_primary = true;
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.inst_id = 0x00;
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_B;
LOG_INFO("%s %d\n", __func__, __LINE__);
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
LOG_INFO("%s %d\n", __func__, __LINE__);
esp_ble_gap_config_adv_data(&test_adv_data);
LOG_INFO("%s %d\n", __func__, __LINE__);
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_B);
LOG_INFO("%s %d\n", __func__, __LINE__);
break;
case ESP_GATTS_READ_EVT: {
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
esp_gatt_rsp_t rsp;
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
rsp.attr_value.handle = param->read.handle;
@ -257,8 +249,8 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
break;
}
case ESP_GATTS_WRITE_EVT: {
LOG_INFO("GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
LOG_INFO("GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
break;
}
@ -268,7 +260,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
case ESP_GATTS_UNREG_EVT:
break;
case ESP_GATTS_CREATE_EVT:
LOG_INFO("CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
ESP_LOGI(GATTS_TAG, "CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_B;
@ -282,7 +274,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
case ESP_GATTS_ADD_INCL_SRVC_EVT:
break;
case ESP_GATTS_ADD_CHAR_EVT:
LOG_INFO("ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
ESP_LOGI(GATTS_TAG, "ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
gl_profile_tab[PROFILE_A_APP_ID].char_handle = param->add_char.attr_handle;
@ -292,19 +284,19 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
break;
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
LOG_INFO("ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
ESP_LOGI(GATTS_TAG, "ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
break;
case ESP_GATTS_DELETE_EVT:
break;
case ESP_GATTS_START_EVT:
LOG_INFO("SERVICE_START_EVT, status %d, service_handle %d\n",
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, status %d, service_handle %d\n",
param->start.status, param->start.service_handle);
break;
case ESP_GATTS_STOP_EVT:
break;
case ESP_GATTS_CONNECT_EVT:
LOG_INFO("SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
param->connect.conn_id,
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5],
@ -324,14 +316,12 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
LOG_INFO("EVT %d, gatts if %d\n", event, gatts_if);
/* If event is register event, store the gatts_if for each profile */
if (event == ESP_GATTS_REG_EVT) {
if (param->reg.status == ESP_GATT_OK) {
gl_profile_tab[param->reg.app_id].gatts_if = gatts_if;
} else {
LOG_INFO("Reg app failed, app_id %04x, status %d\n",
ESP_LOGI(GATTS_TAG, "Reg app failed, app_id %04x, status %d\n",
param->reg.app_id,
param->reg.status);
return;
@ -357,16 +347,16 @@ void app_main()
{
esp_err_t ret;
bt_controller_init();
LOG_INFO("%s init bluetooth\n", __func__);
ret = esp_init_bluetooth();
esp_bt_controller_init();
ret = esp_bluedroid_init();
if (ret) {
LOG_ERROR("%s init bluetooth failed\n", __func__);
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
return;
}
ret = esp_enable_bluetooth();
ret = esp_bluedroid_enable();
if (ret) {
LOG_ERROR("%s enable bluetooth failed\n", __func__);
ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
return;
}

View File

@ -37,6 +37,8 @@
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
#define GATTC_TAG "GATTC_DEMO"
///Declare static functions
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
@ -108,16 +110,16 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
switch (event) {
case ESP_GATTC_REG_EVT:
LOG_INFO("REG_EVT\n");
ESP_LOGI(GATTC_TAG, "REG_EVT\n");
esp_ble_gap_set_scan_params(&ble_scan_params);
break;
case ESP_GATTC_OPEN_EVT:
conn_id = p_data->open.conn_id;
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
LOG_INFO("REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
ESP_LOGI(GATTC_TAG, "REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[1],
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[3],
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[5]
@ -128,37 +130,37 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
case ESP_GATTC_SEARCH_RES_EVT: {
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
conn_id = p_data->search_res.conn_id;
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x\n", conn_id);
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
ESP_LOGI(GATTC_TAG, "UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
LOG_INFO("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
ESP_LOGI(GATTC_TAG, "UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
LOG_INFO("UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
} else {
LOG_ERROR("UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
}
break;
}
case ESP_GATTC_SEARCH_CMPL_EVT:
conn_id = p_data->search_cmpl.conn_id;
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
ESP_LOGI(GATTC_TAG, "SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
break;
case ESP_GATTC_GET_CHAR_EVT:
if (p_data->get_char.status != ESP_GATT_OK) {
break;
}
LOG_INFO("GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
LOG_INFO("GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
ESP_LOGI(GATTC_TAG, "GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
ESP_LOGI(GATTC_TAG, "GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
if (p_data->get_char.char_id.uuid.uuid.uuid16 == 0x2a46) {
LOG_INFO("register notify\n");
ESP_LOGI(GATTC_TAG, "register notify\n");
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, &alert_service_id, &p_data->get_char.char_id);
}
@ -166,8 +168,8 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
break;
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
uint16_t notify_en = 1;
LOG_INFO("REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
LOG_INFO("REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
ESP_LOGI(GATTC_TAG, "REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
ESP_LOGI(GATTC_TAG, "REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
esp_ble_gattc_write_char_descr(
gattc_if,
@ -182,10 +184,10 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
break;
}
case ESP_GATTC_NOTIFY_EVT:
LOG_INFO("NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
ESP_LOGI(GATTC_TAG, "NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
break;
case ESP_GATTC_WRITE_DESCR_EVT:
LOG_INFO("WRITE: status %d\n", p_data->write.status);
ESP_LOGI(GATTC_TAG, "WRITE: status %d\n", p_data->write.status);
break;
default:
break;
@ -199,16 +201,15 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
switch (event) {
case ESP_GATTC_REG_EVT:
LOG_INFO("REG_EVT\n");
//esp_ble_gap_set_scan_params(&ble_scan_params);
ESP_LOGI(GATTC_TAG, "REG_EVT\n");
break;
case ESP_GATTC_OPEN_EVT:
conn_id = p_data->open.conn_id;
memcpy(gl_profile_tab[PROFILE_B_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
LOG_INFO("REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
ESP_LOGI(GATTC_TAG, "REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[1],
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[3],
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[5]
@ -219,37 +220,37 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
case ESP_GATTC_SEARCH_RES_EVT: {
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
conn_id = p_data->search_res.conn_id;
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x\n", conn_id);
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
ESP_LOGI(GATTC_TAG, "UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
LOG_INFO("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
ESP_LOGI(GATTC_TAG, "UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
LOG_INFO("UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
} else {
LOG_ERROR("UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
}
break;
}
case ESP_GATTC_SEARCH_CMPL_EVT:
conn_id = p_data->search_cmpl.conn_id;
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
ESP_LOGI(GATTC_TAG, "SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
break;
case ESP_GATTC_GET_CHAR_EVT:
if (p_data->get_char.status != ESP_GATT_OK) {
break;
}
LOG_INFO("GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
LOG_INFO("GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
ESP_LOGI(GATTC_TAG, "GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
ESP_LOGI(GATTC_TAG, "GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
if (p_data->get_char.char_id.uuid.uuid.uuid16 == 0x2a46) {
LOG_INFO("register notify\n");
ESP_LOGI(GATTC_TAG, "register notify\n");
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_B_APP_ID].remote_bda, &alert_service_id, &p_data->get_char.char_id);
}
@ -257,8 +258,8 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
break;
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
uint16_t notify_en = 1;
LOG_INFO("REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
LOG_INFO("REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
ESP_LOGI(GATTC_TAG, "REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
ESP_LOGI(GATTC_TAG, "REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
esp_ble_gattc_write_char_descr(
gattc_if,
@ -273,10 +274,10 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
break;
}
case ESP_GATTC_NOTIFY_EVT:
LOG_INFO("NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
ESP_LOGI(GATTC_TAG, "NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
break;
case ESP_GATTC_WRITE_DESCR_EVT:
LOG_INFO("WRITE: status %d\n", p_data->write.status);
ESP_LOGI(GATTC_TAG, "WRITE: status %d\n", p_data->write.status);
break;
default:
break;
@ -299,27 +300,22 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
switch (scan_result->scan_rst.search_evt) {
case ESP_GAP_SEARCH_INQ_RES_EVT:
for (int i = 0; i < 6; i++) {
LOG_INFO("%x:", scan_result->scan_rst.bda[i]);
ESP_LOGI(GATTC_TAG, "%x:", scan_result->scan_rst.bda[i]);
}
LOG_INFO("\n");
ESP_LOGI(GATTC_TAG, "\n");
adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
LOG_INFO("adv_name_len=%x\n", adv_name_len);
ESP_LOGI(GATTC_TAG, "Searched Device Name Len %d\n", adv_name_len);
for (int j = 0; j < adv_name_len; j++) {
LOG_INFO("%c", adv_name[j]);
ESP_LOGI(GATTC_TAG, "%c", adv_name[j]);
}
LOG_INFO("\n");
for (int j = 0; j < adv_name_len; j++) {
LOG_INFO("%c", device_name[j]);
}
LOG_INFO("\n");
if (adv_name != NULL) {
if (strcmp((char *)adv_name, device_name) == 0) {
LOG_INFO("the name equal to Heart Rate\n");
ESP_LOGI(GATTC_TAG, "Searched device %s\n", device_name);
if (connect == false) {
connect = true;
LOG_INFO("Connect to the remote device.\n");
ESP_LOGI(GATTC_TAG, "Connect to the remote device.\n");
esp_ble_gap_stop_scanning();
esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, true);
esp_ble_gattc_open(gl_profile_tab[PROFILE_B_APP_ID].gattc_if, scan_result->scan_rst.bda, true);
@ -341,14 +337,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
{
LOG_INFO("EVT %d, gattc if %d\n", event, gattc_if);
ESP_LOGI(GATTC_TAG, "EVT %d, gattc if %d\n", event, gattc_if);
/* If event is register event, store the gattc_if for each profile */
if (event == ESP_GATTC_REG_EVT) {
if (param->reg.status == ESP_GATT_OK) {
gl_profile_tab[param->reg.app_id].gattc_if = gattc_if;
} else {
LOG_INFO("Reg app failed, app_id %04x, status %d\n",
ESP_LOGI(GATTC_TAG, "Reg app failed, app_id %04x, status %d\n",
param->reg.app_id,
param->reg.status);
return;
@ -374,17 +370,17 @@ void ble_client_appRegister(void)
{
esp_err_t status;
LOG_INFO("register callback\n");
ESP_LOGI(GATTC_TAG, "register callback\n");
//register the scan callback function to the gap moudule
if ((status = esp_ble_gap_register_callback(esp_gap_cb)) != ESP_OK) {
LOG_ERROR("gap register error, error code = %x\n", status);
ESP_LOGE(GATTC_TAG, "gap register error, error code = %x\n", status);
return;
}
//register the callback function to the gattc module
if ((status = esp_ble_gattc_register_callback(esp_gattc_cb)) != ESP_OK) {
LOG_ERROR("gattc register error, error code = %x\n", status);
ESP_LOGE(GATTC_TAG, "gattc register error, error code = %x\n", status);
return;
}
esp_ble_gattc_app_register(PROFILE_A_APP_ID);
@ -393,14 +389,14 @@ void ble_client_appRegister(void)
void gattc_client_test(void)
{
esp_init_bluetooth();
esp_enable_bluetooth();
esp_bluedroid_init();
esp_bluedroid_enable();
ble_client_appRegister();
}
void app_main()
{
bt_controller_init();
esp_bt_controller_init();
gattc_client_test();
}