Skip to content

Commit 813d9ca

Browse files
committed
Merge branch 'bugfix/add_multiple_scan_mode' into 'master'
Bugfix: Connect example to add scan mode config Closes IDFGH-4793 and IDFGH-4812 See merge request espressif/esp-idf!12525
2 parents 793771e + fddaaef commit 813d9ca

File tree

4 files changed

+150
-8
lines changed

4 files changed

+150
-8
lines changed

components/esp_wifi/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,16 @@ menu "PHY"
430430

431431
If unsure, choose 'n'.
432432

433+
config ESP32_PHY_DEFAULT_INIT_IF_INVALID
434+
bool "Reset default PHY init data if invalid"
435+
default n
436+
depends on ESP32_PHY_INIT_DATA_IN_PARTITION
437+
help
438+
If enabled, PHY init data will be restored to default if
439+
it cannot be verified successfully to avoid endless bootloops.
440+
441+
If unsure, choose 'n'.
442+
433443
if ESP32_PHY_INIT_DATA_IN_PARTITION
434444
config ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
435445
bool "Support multiple PHY init data bin"

components/esp_wifi/src/phy_init.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,43 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
339339
ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
340340
return NULL;
341341
}
342+
// read phy data from flash
342343
esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
343344
if (err != ESP_OK) {
344345
ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err);
346+
free(init_data_store);
345347
return NULL;
346348
}
349+
// verify data
347350
if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
348351
memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
349352
PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
353+
#ifndef CONFIG_ESP32_PHY_DEFAULT_INIT_IF_INVALID
350354
ESP_LOGE(TAG, "failed to validate PHY data partition");
355+
free(init_data_store);
351356
return NULL;
357+
#else
358+
ESP_LOGE(TAG, "failed to validate PHY data partition, restoring default data into flash...");
359+
360+
memcpy(init_data_store,
361+
PHY_INIT_MAGIC, sizeof(phy_init_magic_pre));
362+
memcpy(init_data_store + sizeof(phy_init_magic_pre),
363+
&phy_init_data, sizeof(phy_init_data));
364+
memcpy(init_data_store + sizeof(phy_init_magic_pre) + sizeof(phy_init_data),
365+
PHY_INIT_MAGIC, sizeof(phy_init_magic_post));
366+
367+
assert(memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) == 0);
368+
assert(memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
369+
PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) == 0);
370+
371+
// write default data
372+
err = esp_partition_write(partition, 0, init_data_store, init_data_store_length);
373+
if (err != ESP_OK) {
374+
ESP_LOGE(TAG, "failed to write default PHY data partition (0x%x)", err);
375+
free(init_data_store);
376+
return NULL;
377+
}
378+
#endif // CONFIG_ESP32_PHY_DEFAULT_INIT_IF_INVALID
352379
}
353380
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
354381
if ((*(init_data_store + (sizeof(phy_init_magic_pre) + PHY_SUPPORT_MULTIPLE_BIN_OFFSET)))) {
@@ -751,8 +778,8 @@ static esp_err_t phy_get_multiple_init_data(const esp_partition_t* partition,
751778

752779
err = phy_find_bin_data_according_type(init_data_store, init_data_control_info, init_data_multiple, init_data_type);
753780
if (err != ESP_OK) {
754-
ESP_LOGW(TAG, "%s has not been certified, use DEFAULT PHY init data", s_phy_type[init_data_type]);
755-
s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
781+
ESP_LOGW(TAG, "%s has not been certified, use DEFAULT PHY init data", s_phy_type[init_data_type]);
782+
s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
756783
} else {
757784
s_phy_init_data_type = init_data_type;
758785
}
@@ -829,18 +856,19 @@ esp_err_t esp_phy_update_country_info(const char *country)
829856
{
830857
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
831858
uint8_t phy_init_data_type_map = 0;
832-
//if country equal s_phy_current_country, return;
833-
if (!memcmp(country, s_phy_current_country, sizeof(s_phy_current_country))) {
834-
return ESP_OK;
835-
}
836-
837-
memcpy(s_phy_current_country, country, sizeof(s_phy_current_country));
838859

839860
if (!s_multiple_phy_init_data_bin) {
840861
ESP_LOGD(TAG, "Does not support multiple PHY init data bins");
841862
return ESP_FAIL;
842863
}
843864

865+
//if country equal s_phy_current_country, return;
866+
if (!memcmp(country, s_phy_current_country, sizeof(s_phy_current_country))) {
867+
return ESP_OK;
868+
}
869+
870+
memcpy(s_phy_current_country, country, sizeof(s_phy_current_country));
871+
844872
phy_init_data_type_map = phy_find_bin_type_according_country(country);
845873
if (phy_init_data_type_map == s_phy_init_data_type) {
846874
return ESP_OK;

examples/common_components/protocol_examples_common/Kconfig.projbuild

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,74 @@ menu "Example Connection Configuration"
2020
help
2121
WiFi password (WPA or WPA2) for the example to use.
2222
Can be left blank if the network has no security set.
23+
24+
choice EXAMPLE_WIFI_SCAN_METHOD
25+
prompt "WiFi Scan Method"
26+
default EXAMPLE_WIFI_SCAN_METHOD_FAST
27+
help
28+
WiFi scan method:
29+
30+
If "Fast" is selected, scan will end after find SSID match AP.
31+
32+
If "All Channel" is selected, scan will end after scan all the channel.
33+
34+
config EXAMPLE_WIFI_SCAN_METHOD_FAST
35+
bool "Fast"
36+
config EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
37+
bool "All Channel"
38+
endchoice
39+
40+
menu "WiFi Scan threshold"
41+
config EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD
42+
int "WiFi minimum rssi"
43+
range -127 0
44+
45+
default -127
46+
help
47+
The minimum rssi to accept in the scan mode.
48+
49+
choice EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD
50+
prompt "WiFi Scan auth mode threshold"
51+
default EXAMPLE_WIFI_AUTH_OPEN
52+
help
53+
The weakest authmode to accept in the scan mode.
54+
55+
config EXAMPLE_WIFI_AUTH_OPEN
56+
bool "OPEN"
57+
config EXAMPLE_WIFI_AUTH_WEP
58+
bool "WEP"
59+
config EXAMPLE_WIFI_AUTH_WPA_PSK
60+
bool "WPA PSK"
61+
config EXAMPLE_WIFI_AUTH_WPA2_PSK
62+
bool "WPA2 PSK"
63+
config EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
64+
bool "WPA WPA2 PSK"
65+
config EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
66+
bool "WPA2 ENTERPRISE"
67+
config EXAMPLE_WIFI_AUTH_WPA3_PSK
68+
bool "WPA3 PSK"
69+
config EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
70+
bool "WPA2 WPA3 PSK"
71+
config EXAMPLE_WIFI_AUTH_WAPI_PSK
72+
bool "WAPI PSK"
73+
endchoice
74+
endmenu
75+
76+
choice EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD
77+
prompt "WiFi Connect AP Sort Method"
78+
default EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
79+
help
80+
WiFi connect AP sort method:
81+
82+
If "Signal" is selected, Sort matched APs in scan list by RSSI.
83+
84+
If "Security" is selected, Sort matched APs in scan list by security mode.
85+
86+
config EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
87+
bool "Signal"
88+
config EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
89+
bool "Security"
90+
endchoice
2391
endif
2492

2593
config EXAMPLE_CONNECT_ETHERNET

examples/common_components/protocol_examples_common/connect.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@
4848

4949
#define EXAMPLE_DO_CONNECT CONFIG_EXAMPLE_CONNECT_WIFI || CONFIG_EXAMPLE_CONNECT_ETHERNET
5050

51+
#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
52+
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
53+
#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
54+
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
55+
#endif
56+
57+
#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
58+
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
59+
#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
60+
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
61+
#endif
62+
63+
#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN
64+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
65+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP
66+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
67+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK
68+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
69+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK
70+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
71+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
72+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
73+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
74+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE
75+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK
76+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
77+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
78+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
79+
#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK
80+
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
81+
#endif
82+
5183
static int s_active_interfaces = 0;
5284
static xSemaphoreHandle s_semph_get_ip_addrs;
5385
static esp_netif_t *s_example_esp_netif = NULL;
@@ -265,6 +297,10 @@ static esp_netif_t *wifi_start(void)
265297
.sta = {
266298
.ssid = CONFIG_EXAMPLE_WIFI_SSID,
267299
.password = CONFIG_EXAMPLE_WIFI_PASSWORD,
300+
.scan_method = EXAMPLE_WIFI_SCAN_METHOD,
301+
.sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD,
302+
.threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD,
303+
.threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD,
268304
},
269305
};
270306
ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);

0 commit comments

Comments
 (0)