Skip to content

Commit

Permalink
blufi: update version to 1.3
Browse files Browse the repository at this point in the history
record connecting status, got ip status, maximum retry, connection end info (reason code, rssi) and provide greater information to phone
  • Loading branch information
Espressif-liuuuu authored and jack0c committed Dec 5, 2022
1 parent 14efa6e commit 497b451
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 11 deletions.
9 changes: 9 additions & 0 deletions components/bt/common/api/include/api/esp_blufi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

/**
Expand All @@ -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 */
Expand Down
30 changes: 30 additions & 0 deletions components/bt/common/btc/profile/esp/blufi/blufi_prf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions examples/bluetooth/blufi/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -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
85 changes: 75 additions & 10 deletions examples/bluetooth/blufi/main/blufi_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand All @@ -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");
Expand Down Expand Up @@ -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() );
}

Expand Down Expand Up @@ -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");
Expand All @@ -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");

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 497b451

Please sign in to comment.