Merge branch 'feature/optimize_blufi_example' into 'master'

blufi: optimize example to send more information

Closes IDFGH-6842 and IDFGH-6809

See merge request espressif/esp-idf!19853
This commit is contained in:
Jiang Jiang Jian 2022-11-07 15:09:58 +08:00
commit 0c8272b2e0
11 changed files with 168 additions and 20 deletions

View File

@ -49,6 +49,8 @@ typedef enum {
typedef enum {
ESP_BLUFI_STA_CONN_SUCCESS = 0x00,
ESP_BLUFI_STA_CONN_FAIL = 0x01,
ESP_BLUFI_STA_CONNECTING = 0x02,
ESP_BLUFI_STA_NO_IP = 0x03,
} esp_blufi_sta_conn_state_t;
/// BLUFI init status
@ -75,6 +77,7 @@ typedef enum {
ESP_BLUFI_MAKE_PUBLIC_ERROR,
ESP_BLUFI_DATA_FORMAT_ERROR,
ESP_BLUFI_CALC_MD5_ERROR,
ESP_BLUFI_WIFI_SCAN_FAIL,
} esp_blufi_error_state_t;
/**
@ -98,6 +101,12 @@ typedef struct {
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 */
uint8_t sta_max_conn_retry; /*!< max retry of sta establish connection */
bool sta_max_conn_retry_set; /*!< is max retry of sta establish connection set */
uint8_t sta_conn_end_reason; /*!< reason of sta connection end */
bool sta_conn_end_reason_set; /*!< is reason of sta connection end set */
int8_t sta_conn_rssi; /*!< rssi of sta connection */
bool sta_conn_rssi_set; /*!< is rssi of sta connection set */
} esp_blufi_extra_info_t;
/** @brief Description of an WiFi AP */

View File

@ -313,6 +313,21 @@ static void btc_blufi_wifi_conn_report(uint8_t opmode, uint8_t sta_conn_state, u
*p++ = 1;
*p++ = info->softap_channel;
}
if (info->sta_max_conn_retry_set) {
*p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_MAX_CONN_RETRY;
*p++ = 1;
*p++ = info->sta_max_conn_retry;
}
if (info->sta_conn_end_reason_set) {
*p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_CONN_END_REASON;
*p++ = 1;
*p++ = info->sta_conn_end_reason;
}
if (info->sta_conn_rssi_set) {
*p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_CONN_RSSI;
*p++ = 1;
*p++ = info->sta_conn_rssi;
}
}
if (p - data > data_len) {
BTC_TRACE_ERROR("%s len error %d %d\n", __func__, (int)(p - data), data_len);
@ -706,6 +721,21 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
dst->wifi_conn_report.extra_info->softap_channel = src_info->softap_channel;
dst->wifi_conn_report.extra_info_len += (1 + 2);
}
if (src_info->sta_max_conn_retry_set) {
dst->wifi_conn_report.extra_info->sta_max_conn_retry_set = src_info->sta_max_conn_retry_set;
dst->wifi_conn_report.extra_info->sta_max_conn_retry = src_info->sta_max_conn_retry;
dst->wifi_conn_report.extra_info_len += (1 + 2);
}
if (src_info->sta_conn_end_reason_set) {
dst->wifi_conn_report.extra_info->sta_conn_end_reason_set = src_info->sta_conn_end_reason_set;
dst->wifi_conn_report.extra_info->sta_conn_end_reason = src_info->sta_conn_end_reason;
dst->wifi_conn_report.extra_info_len += (1 + 2);
}
if (src_info->sta_conn_rssi_set) {
dst->wifi_conn_report.extra_info->sta_conn_rssi_set = src_info->sta_conn_rssi_set;
dst->wifi_conn_report.extra_info->sta_conn_rssi = src_info->sta_conn_rssi;
dst->wifi_conn_report.extra_info_len += (1 + 2);
}
break;
}
case BTC_BLUFI_ACT_SEND_WIFI_LIST:{

View File

@ -12,7 +12,7 @@
#if (BLUFI_INCLUDED == TRUE)
#define BTC_BLUFI_GREAT_VER 0x01 //Version + Subversion
#define BTC_BLUFI_SUB_VER 0x02 //Version + Subversion
#define BTC_BLUFI_SUB_VER 0x03 //Version + Subversion
#define BTC_BLUFI_VERSION ((BTC_BLUFI_GREAT_VER<<8)|BTC_BLUFI_SUB_VER) //Version + Subversion
typedef UINT8 tGATT_IF;
@ -121,6 +121,9 @@ extern tBLUFI_ENV *blufi_env_ptr;
#define BLUFI_TYPE_DATA_SUBTYPE_WIFI_LIST 0x11
#define BLUFI_TYPE_DATA_SUBTYPE_ERROR_INFO 0x12
#define BLUFI_TYPE_DATA_SUBTYPE_CUSTOM_DATA 0x13
#define BLUFI_TYPE_DATA_SUBTYPE_STA_MAX_CONN_RETRY 0x14
#define BLUFI_TYPE_DATA_SUBTYPE_STA_CONN_END_REASON 0x15
#define BLUFI_TYPE_DATA_SUBTYPE_STA_CONN_RSSI 0x16
#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)

View File

@ -654,6 +654,7 @@ typedef struct {
uint8_t ssid_len; /**< SSID length of disconnected AP */
uint8_t bssid[6]; /**< BSSID of disconnected AP */
uint8_t reason; /**< reason of disconnection */
int8_t rssi; /**< rssi of disconnection */
} wifi_event_sta_disconnected_t;
/** Argument structure for WIFI_EVENT_STA_AUTHMODE_CHANGE event */

@ -1 +1 @@
Subproject commit 84272dd4079277a05db7b6c4b22a72e9935ebc38
Subproject commit 774353036b5d4bc9055d7961e7011b676c858be2

View File

@ -321,11 +321,11 @@ The format of ACK Frame:
* 0x02: SoftAP
* 0x03: SoftAP & STA
data[1]: connection state of the STA device. 0x0 indicates a connection state, and others represent a disconnected state;
data[1]: connection state of the STA device. 0x0 indicates a connection state with IP address, 0x1 represent a disconnected state, 0x2 indicates a connecting state, and 0x3 indicates a connection state but no IP address.
data[2]: connection state of SoftAP. That is, how many STA devices have been connected.
data[3] and the subsequent is in accordance with the format of SSID/BSSID information.
data[3] and the subsequent is in accordance with the format of SSID/BSSID information. If device is in connecting state, maximum Wi-Fi reconnecting time would be included here. If device is in disconnected state, Wi-Fi connection end reason and RSSI would be included here.
* - 0x10 (b010000)
- Version
-
@ -347,10 +347,25 @@ The format of ACK Frame:
* 0x06: dh param error
* 0x07: read param error
* 0x08: make public error
* 0x09: data format error
* 0x0a: calculate MD5 error
* 0x0b: Wi-Fi scan error
* - 0x13 (b010011)
- Custom Data
- To send or receive custom data.
- The data frame supports to be sent into fragments if the data length is too long.
* - 0x14 (b010100)
- Set the maximum Wi-Fi reconnecting time.
-
- data[0] represents the maximum Wi-Fi reconnecting time.
* - 0x15 (b010101)
- Set the Wi-Fi connection end reason.
-
- data[0] represents the Wi-Fi connection end reason, whose type shall be same with struct `wifi_err_reason_t`.
* - 0x16 (b010110)
- Set the RSSI at Wi-Fi connection end.
-
- data[0] represents the RSSI at Wi-Fi connection end. If there is no meaningful RSSI in the connection end, this value shall be the meaningless one, which is `-128`.
.. note::

View File

@ -321,11 +321,11 @@ ACK 帧格式8 bit
* 0x02: SoftAP
* 0x03: SoftAP & STA
data[1]STA 设备的连接状态。0x0 表示处于连接状态,其他表示处于非连接状态;
data[1]STA 设备的连接状态。0x0 表示处于连接状态且获得 IP 地址0x1 表示处于非连接状态, 0x2 表示处于正在连接状态0x3 表示处于连接状态但未获得 IP 地址。
data[2]SoftAP 的连接状态,即表示有多少 STA 设备已经连接。
data[3]及后面的数据是按照 SSID/BSSID 格式提供的信息。
data[3]及后面的数据是按照 SSID/BSSID 格式提供的信息。 如果 Wi-Fi 处于正在连接状态,这里将会包含最大重连次数;如果 Wi-Fi 处于非连接状态,这里将会包含 Wi-Fi 断开连接原因和 RSSI 信息。
* - 0x10 (b010000)
- 版本
-
@ -347,10 +347,25 @@ ACK 帧格式8 bit
* 0x06: dh param error
* 0x07: read param error
* 0x08: make public error
* 0x09: data format error
* 0x0a: calculate MD5 error
* 0x0b: Wi-Fi scan error
* - 0x13 (b010011)
- 自定义数据
- 用户发送或者接收自定义数据。
- 数据较长时可分片发送。
* - 0x14 (b010100)
- 设置最大 Wi-Fi 重连次数。
-
- data[0] 表示 Wi-Fi 最大重连次数。
* - 0x15 (b010101)
- 设置 Wi-Fi 连接失败原因。
-
- data[0] 表示 Wi-Fi 连接失败原因,它的类型应该和 `wifi_err_reason_t` 一致。
* - 0x16 (b010110)
- 设置 Wi-Fi 连接失败的 RSSI 。
-
- data[0] 表示在 Wi-Fi 连接失败时的 RSSI。 如果在连接结束时没有有意义的 RSSI 这个值应该为一个无意义值 `-128`
.. note::

View File

@ -38,7 +38,7 @@ examples/bluetooth/bluedroid/coex/gattc_gatts_coex:
examples/bluetooth/blufi:
enable:
- if: IDF_TARGET in ["esp32", "esp32c3"]
- if: IDF_TARGET in ["esp32", "esp32c2", "esp32c3", "esp32s3"]
temporary: true
reason: the other targets are not tested yet

View File

@ -1,5 +1,5 @@
| Supported Targets | ESP32 | ESP32-C3 |
| ----------------- | ----- | -------- |
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- |
# ESP-IDF Blufi Example
@ -27,7 +27,7 @@ Blufi is completely open source, here is the download link:
### Hardware Required
* A development board with ESP32/ESP32-C3 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
* A development board with ESP32/ESP32-C3/ESP32-S3/ESP32-C2 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
* A USB cable for Power supply and programming
See [Development Boards](https://www.espressif.com/en/products/devkits) for more information about it.

View File

@ -0,0 +1,10 @@
menu "Example Configuration"
config EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY
int "WiFi connection maximum retry"
range 0 255
default 2
help
WiFi connection maximum retry, from 0 to 255.
endmenu

View File

@ -31,6 +31,10 @@
#include "esp_blufi.h"
#define EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY CONFIG_EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY
#define EXAMPLE_INVALID_REASON 255
#define EXAMPLE_INVALID_RSSI -128
static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
#define WIFI_LIST_NUM 10
@ -46,13 +50,53 @@ static EventGroupHandle_t wifi_event_group;
to the AP with an IP? */
const int CONNECTED_BIT = BIT0;
static uint8_t example_wifi_retry = 0;
/* store the station info for send back to phone */
static bool gl_sta_connected = false;
static bool gl_sta_got_ip = false;
static bool ble_is_connected = false;
static uint8_t gl_sta_bssid[6];
static uint8_t gl_sta_ssid[32];
static int gl_sta_ssid_len;
static wifi_sta_list_t gl_sta_list;
static bool gl_sta_is_connecting = false;
static esp_blufi_extra_info_t gl_sta_conn_info;
static void example_record_wifi_conn_info(int rssi, uint8_t reason)
{
memset(&gl_sta_conn_info, 0, sizeof(esp_blufi_extra_info_t));
if (gl_sta_is_connecting) {
gl_sta_conn_info.sta_max_conn_retry_set = true;
gl_sta_conn_info.sta_max_conn_retry = EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY;
} else {
gl_sta_conn_info.sta_conn_rssi_set = true;
gl_sta_conn_info.sta_conn_rssi = rssi;
gl_sta_conn_info.sta_conn_end_reason_set = true;
gl_sta_conn_info.sta_conn_end_reason = reason;
}
}
static void example_wifi_connect(void)
{
example_wifi_retry = 0;
gl_sta_is_connecting = (esp_wifi_connect() == ESP_OK);
example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON);
}
static bool example_wifi_reconnect(void)
{
bool ret;
if (gl_sta_is_connecting && example_wifi_retry++ < EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY) {
BLUFI_INFO("BLUFI WiFi starts reconnection\n");
gl_sta_is_connecting = (esp_wifi_connect() == ESP_OK);
example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON);
ret = true;
} else {
ret = false;
}
return ret;
}
static int softap_get_current_connection_number(void)
{
@ -83,6 +127,7 @@ static void ip_event_handler(void* arg, esp_event_base_t event_base,
info.sta_bssid_set = true;
info.sta_ssid = gl_sta_ssid;
info.sta_ssid_len = gl_sta_ssid_len;
gl_sta_got_ip = true;
if (ble_is_connected == true) {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, softap_get_current_connection_number(), &info);
} else {
@ -100,27 +145,35 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
wifi_event_sta_connected_t *event;
wifi_event_sta_disconnected_t *disconnected_event;
wifi_mode_t mode;
switch (event_id) {
case WIFI_EVENT_STA_START:
esp_wifi_connect();
example_wifi_connect();
break;
case WIFI_EVENT_STA_CONNECTED:
gl_sta_connected = true;
gl_sta_is_connecting = false;
event = (wifi_event_sta_connected_t*) event_data;
memcpy(gl_sta_bssid, event->bssid, 6);
memcpy(gl_sta_ssid, event->ssid, event->ssid_len);
gl_sta_ssid_len = event->ssid_len;
break;
case WIFI_EVENT_STA_DISCONNECTED:
/* Only handle reconnection during connecting */
if (gl_sta_connected == false && example_wifi_reconnect() == false) {
gl_sta_is_connecting = false;
disconnected_event = (wifi_event_sta_disconnected_t*) event_data;
example_record_wifi_conn_info(disconnected_event->rssi, disconnected_event->reason);
}
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
gl_sta_connected = false;
gl_sta_got_ip = 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 WIFI_EVENT_AP_START:
@ -129,9 +182,17 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
/* TODO: get config or information of softap, then set to report extra_info */
if (ble_is_connected == true) {
if (gl_sta_connected) {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, softap_get_current_connection_number(), NULL);
esp_blufi_extra_info_t info;
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, gl_sta_got_ip ? ESP_BLUFI_STA_CONN_SUCCESS : ESP_BLUFI_STA_NO_IP, softap_get_current_connection_number(), &info);
} else if (gl_sta_is_connecting) {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONNECTING, softap_get_current_connection_number(), &gl_sta_conn_info);
} else {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), NULL);
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), &gl_sta_conn_info);
}
} else {
BLUFI_INFO("BLUFI BLE is not connected yet\n");
@ -207,6 +268,7 @@ static void initialise_wifi(void)
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON);
ESP_ERROR_CHECK( esp_wifi_start() );
}
@ -253,7 +315,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
so disconnect wifi before connection.
*/
esp_wifi_disconnect();
esp_wifi_connect();
example_wifi_connect();
break;
case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP:
BLUFI_INFO("BLUFI requset wifi disconnect from AP\n");
@ -269,17 +331,17 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
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, softap_get_current_connection_number(), &info);
esp_blufi_send_wifi_conn_report(mode, gl_sta_got_ip ? ESP_BLUFI_STA_CONN_SUCCESS : ESP_BLUFI_STA_NO_IP, softap_get_current_connection_number(), &info);
} else if (gl_sta_is_connecting) {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONNECTING, softap_get_current_connection_number(), &gl_sta_conn_info);
} else {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), NULL);
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), &gl_sta_conn_info);
}
BLUFI_INFO("BLUFI get wifi status from AP\n");
@ -354,7 +416,10 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
.channel = 0,
.show_hidden = false
};
esp_wifi_scan_start(&scanConf, true);
esp_err_t ret = esp_wifi_scan_start(&scanConf, true);
if (ret != ESP_OK) {
esp_blufi_send_error_info(ESP_BLUFI_WIFI_SCAN_FAIL);
}
break;
}
case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA: