Merge branch 'feature/btdm_add_get_error_enent_for_blufi' into 'master'

component/bt: add  error report event for blufi

See merge request !1702
This commit is contained in:
Jiang Jiang Jian 2018-01-22 19:48:29 +08:00
commit 84b92a4ead
7 changed files with 122 additions and 4 deletions

View file

@ -125,3 +125,20 @@ esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id)
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL)
== BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
esp_err_t esp_blufi_send_error_info(esp_blufi_error_state_t state)
{
btc_msg_t msg;
btc_blufi_args_t arg;
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
return ESP_ERR_INVALID_STATE;
}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_BLUFI;
msg.act = BTC_BLUFI_ACT_SEND_ERR_INFO;
arg.blufi_err_infor.state = state;
return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

View file

@ -51,6 +51,7 @@ typedef enum {
ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY, /*<! When Phone send Server Private key to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE, /*<! When Phone send Disconnect key to ESP32, this event happen */
ESP_BLUFI_EVENT_GET_WIFI_LIST, /*<! When Phone send get wifi list command to ESP32, this event happen */
ESP_BLUFI_EVENT_REPORT_ERROR, /*<! When Blufi report error, this event happen */
} esp_blufi_cb_event_t;
/// BLUFI config status
@ -71,6 +72,18 @@ typedef enum {
ESP_BLUFI_DEINIT_FAILED,
} esp_blufi_deinit_state_t;
typedef enum {
ESP_BLUFI_SEQUENCE_ERROR = 0,
ESP_BLUFI_CHECKSUM_ERROR,
ESP_BLUFI_DECRYPT_ERROR,
ESP_BLUFI_ENCRYPT_ERROR,
ESP_BLUFI_INIT_SECURITY_ERROR,
ESP_BLUFI_DH_MALLOC_ERROR,
ESP_BLUFI_DH_PARAM_ERROR,
ESP_BLUFI_READ_PARAM_ERROR,
ESP_BLUFI_MAKE_PUBLIC_ERROR,
} esp_blufi_error_state_t;
/**
* @brief BLUFI extra information structure
*/
@ -254,6 +267,13 @@ typedef union {
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 */
/**
* @brief
* ESP_BLUFI_EVENT_REPORT_ERROR
*/
struct blufi_get_error_evt_param {
esp_blufi_error_state_t state; /*!< Blufi error state */
} report_error; /*!< Blufi callback param of ESP_BLUFI_EVENT_REPORT_ERROR */
} esp_blufi_cb_param_t;
@ -386,6 +406,16 @@ uint16_t esp_blufi_get_version(void);
*
*/
esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id);
/**
*
* @brief This function is called to send blufi error information
* @param state : error state
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_send_error_info(esp_blufi_error_state_t state);
#ifdef __cplusplus
}
#endif

View file

@ -363,6 +363,17 @@ static void btc_blufi_send_notify(uint8_t *pkt, int pkt_len)
pkt, rsp);
}
void btc_blufi_report_error(esp_blufi_error_state_t state)
{
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_BLUFI;
msg.act = ESP_BLUFI_EVENT_REPORT_ERROR;
esp_blufi_cb_param_t param;
param.report_error.state = state;
btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), NULL);
}
static void btc_blufi_recv_handler(uint8_t *data, int len)
{
struct blufi_hdr *hdr = (struct blufi_hdr *)data;
@ -371,6 +382,7 @@ static void btc_blufi_recv_handler(uint8_t *data, int len)
if (hdr->seq != blufi_env.recv_seq) {
LOG_ERROR("%s seq %d is not expect %d\n", __func__, hdr->seq, blufi_env.recv_seq + 1);
btc_blufi_report_error(ESP_BLUFI_SEQUENCE_ERROR);
return;
}
@ -382,6 +394,7 @@ static void btc_blufi_recv_handler(uint8_t *data, int len)
ret = blufi_env.cbs->decrypt_func(hdr->seq, hdr->data, hdr->data_len);
if (ret != hdr->data_len) { /* enc must be success and enc len must equal to plain len */
LOG_ERROR("%s decrypt error %d\n", __func__, ret);
btc_blufi_report_error(ESP_BLUFI_DECRYPT_ERROR);
return;
}
}
@ -393,6 +406,7 @@ static void btc_blufi_recv_handler(uint8_t *data, int len)
checksum_pkt = hdr->data[hdr->data_len] | (((uint16_t) hdr->data[hdr->data_len + 1]) << 8);
if (checksum != checksum_pkt) {
LOG_ERROR("%s checksum error %04x, pkt %04x\n", __func__, checksum, checksum_pkt);
btc_blufi_report_error(ESP_BLUFI_CHECKSUM_ERROR);
return;
}
}
@ -468,7 +482,7 @@ void btc_blufi_send_encap(uint8_t type, uint8_t *data, int total_data_len)
checksum = blufi_env.cbs->checksum_func(hdr->seq, &hdr->seq, hdr->data_len + 2);
memcpy(&hdr->data[hdr->data_len], &checksum, 2);
}
} else if (!BLUFI_TYPE_IS_DATA_NEG(hdr->type)) {
} else if (!BLUFI_TYPE_IS_DATA_NEG(hdr->type) && !BLUFI_TYPE_IS_DATA_ERROR_INFO(hdr->type)) {
if ((blufi_env.sec_mode & BLUFI_DATA_SEC_MODE_CHECK_MASK)
&& (blufi_env.cbs && blufi_env.cbs->checksum_func)) {
hdr->fc |= BLUFI_FC_CHECK;
@ -483,6 +497,7 @@ void btc_blufi_send_encap(uint8_t type, uint8_t *data, int total_data_len)
hdr->fc |= BLUFI_FC_ENC;
} else {
LOG_ERROR("%s encrypt error %d\n", __func__, ret);
btc_blufi_report_error(ESP_BLUFI_ENCRYPT_ERROR);
osi_free(hdr);
return;
}
@ -622,6 +637,28 @@ static void btc_blufi_send_ack(uint8_t seq)
btc_blufi_send_encap(type, &data, 1);
}
static void btc_blufi_send_error_info(uint8_t state)
{
uint8_t type;
uint8_t *data;
int data_len;
uint8_t *p;
data_len = 1;
p = data = osi_malloc(data_len);
if (data == NULL) {
return;
}
type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_ERROR_INFO);
*p++ = state;
if (p - data > data_len) {
LOG_ERROR("%s len error %d %d\n", __func__, (int)(p - data), data_len);
}
btc_blufi_send_encap(type, data, data_len);
osi_free(data);
}
void btc_blufi_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
@ -826,6 +863,9 @@ void btc_blufi_cb_handler(btc_msg_t *msg)
case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE:
btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE, param);
break;
case ESP_BLUFI_EVENT_REPORT_ERROR:
btc_blufi_cb_to_app(ESP_BLUFI_EVENT_REPORT_ERROR, param);
break;
default:
LOG_ERROR("%s UNKNOWN %d\n", __func__, msg->act);
break;
@ -986,6 +1026,9 @@ void btc_blufi_call_handler(btc_msg_t *msg)
btc_blufi_send_wifi_list(arg->wifi_list.apCount, arg->wifi_list.list);
break;
}
case BTC_BLUFI_ACT_SEND_ERR_INFO:
btc_blufi_send_error_info(arg->blufi_err_infor.state);
break;
default:
LOG_ERROR("%s UNKNOWN %d\n", __func__, msg->act);
break;

View file

@ -114,6 +114,7 @@ typedef struct blufi_frag_hdr blufi_frag_hdr_t;
#define BLUFI_TYPE_DATA_SUBTYPE_WIFI_REP 0x0f
#define BLUFI_TYPE_DATA_SUBTYPE_REPLY_VERSION 0x10
#define BLUFI_TYPE_DATA_SUBTYPE_WIFI_LIST 0x11
#define BLUFI_TYPE_DATA_SUBTYPE_ERROR_INFO 0x12
#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)
@ -142,6 +143,7 @@ typedef struct blufi_frag_hdr blufi_frag_hdr_t;
#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)
#define BLUFI_TYPE_IS_DATA_ERROR_INFO(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_ERROR_INFO)
// packet frame control
#define BLUFI_FC_ENC_MASK 0x01

View file

@ -24,6 +24,7 @@ typedef enum {
BTC_BLUFI_ACT_DEINIT,
BTC_BLUFI_ACT_SEND_CFG_REPORT,
BTC_BLUFI_ACT_SEND_WIFI_LIST,
BTC_BLUFI_ACT_SEND_ERR_INFO,
} btc_blufi_act_t;
typedef union {
@ -41,6 +42,12 @@ typedef union {
uint16_t apCount;
esp_blufi_ap_record_t *list;
} wifi_list;
/*
BTC_BLUFI_ACT_SEND_ERR_INFO
*/
struct blufi_error_infor {
esp_blufi_error_state_t state;
} blufi_err_infor;
} btc_blufi_args_t;
void btc_blufi_cb_handler(btc_msg_t *msg);

View file

@ -229,6 +229,10 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
BLUFI_INFO("BLUFI requset wifi disconnect from AP\n");
esp_wifi_disconnect();
break;
case ESP_BLUFI_EVENT_REPORT_ERROR:
BLUFI_ERROR("BLUFI report error, error code %d\n", param->report_error.state);
esp_blufi_send_error_info(param->report_error.state);
break;
case ESP_BLUFI_EVENT_GET_WIFI_STATUS: {
wifi_mode_t mode;
esp_blufi_extra_info_t info;
@ -397,9 +401,17 @@ void app_main()
BLUFI_INFO("BLUFI VERSION %04x\n", esp_blufi_get_version());
blufi_security_init();
esp_ble_gap_register_callback(example_gap_event_handler);
ret = esp_ble_gap_register_callback(example_gap_event_handler);
if(ret){
BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret);
return;
}
ret = esp_blufi_register_callbacks(&example_callbacks);
if(ret){
BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret);
return;
}
esp_blufi_register_callbacks(&example_callbacks);
esp_blufi_profile_init();
}

View file

@ -69,6 +69,8 @@ static int myrand( void *rng_state, unsigned char *output, size_t len )
return( 0 );
}
extern void btc_blufi_report_error(esp_blufi_error_state_t state);
void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free)
{
int ret;
@ -76,6 +78,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
if (blufi_sec == NULL) {
BLUFI_ERROR("BLUFI Security is not initialized");
btc_blufi_report_error(ESP_BLUFI_INIT_SECURITY_ERROR);
return;
}
@ -88,6 +91,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
}
blufi_sec->dh_param = (uint8_t *)malloc(blufi_sec->dh_param_len);
if (blufi_sec->dh_param == NULL) {
btc_blufi_report_error(ESP_BLUFI_DH_MALLOC_ERROR);
BLUFI_ERROR("%s, malloc failed\n", __func__);
return;
}
@ -95,6 +99,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
case SEC_TYPE_DH_PARAM_DATA:{
if (blufi_sec->dh_param == NULL) {
BLUFI_ERROR("%s, blufi_sec->dh_param == NULL\n", __func__);
btc_blufi_report_error(ESP_BLUFI_DH_PARAM_ERROR);
return;
}
uint8_t *param = blufi_sec->dh_param;
@ -102,6 +107,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
ret = mbedtls_dhm_read_params(&blufi_sec->dhm, &param, &param[blufi_sec->dh_param_len]);
if (ret) {
BLUFI_ERROR("%s read param failed %d\n", __func__, ret);
btc_blufi_report_error(ESP_BLUFI_READ_PARAM_ERROR);
return;
}
free(blufi_sec->dh_param);
@ -109,6 +115,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
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);
btc_blufi_report_error(ESP_BLUFI_MAKE_PUBLIC_ERROR);
return;
}