Skip to content

Commit c0af462

Browse files
committed
app_wifi: Add an option to reset prov state machine on failure
Earlier, a reset and reboot were required if wrong Wi-Fi credentials were given during provisioning. With this change, based on some configured number of reconnection attempts, the firmware will go back into provisioning mode.
1 parent d796985 commit c0af462

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

examples/common/app_wifi/Kconfig.projbuild

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ menu "ESP RainMaker App Wi-Fi Provisioning"
2525
default 1 if APP_WIFI_PROV_TRANSPORT_SOFTAP
2626
default 2 if APP_WIFI_PROV_TRANSPORT_BLE
2727

28+
config APP_WIFI_RESET_PROV_ON_FAILURE
29+
bool
30+
default y
31+
prompt "Reset provisioned credentials and state machine after session failure"
32+
help
33+
Enable reseting provisioned credentials and state machine after session failure.
34+
This will restart the provisioning service after retries are exhausted.
35+
36+
config APP_WIFI_PROV_MAX_RETRY_CNT
37+
int
38+
default 5
39+
prompt "Max retries before reseting provisioning state machine"
40+
depends on APP_WIFI_RESET_PROV_ON_FAILURE
41+
help
42+
Set the Maximum retry to avoid reconnecting to an inexistent AP or if credentials
43+
are misconfigured. Provisioned credentials are erased and internal state machine
44+
is reset after this threshold is reached.
45+
2846
config APP_WIFI_SHOW_DEMO_INTRO_TEXT
2947
bool "Show intro text for demos"
3048
default n

examples/common/app_wifi/app_wifi.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ static void app_wifi_print_qr(const char *name, const char *pop, const char *tra
128128
static void event_handler(void* arg, esp_event_base_t event_base,
129129
int32_t event_id, void* event_data)
130130
{
131+
#ifdef CONFIG_APP_WIFI_RESET_PROV_ON_FAILURE
132+
static int retries = 0;
133+
#endif
131134
if (event_base == WIFI_PROV_EVENT) {
132135
switch (event_id) {
133136
case WIFI_PROV_START:
@@ -147,10 +150,26 @@ static void event_handler(void* arg, esp_event_base_t event_base,
147150
"\n\tPlease reset to factory and retry provisioning",
148151
(*reason == WIFI_PROV_STA_AUTH_ERROR) ?
149152
"Wi-Fi station authentication failed" : "Wi-Fi access-point not found");
153+
#ifdef CONFIG_APP_WIFI_RESET_PROV_ON_FAILURE
154+
retries++;
155+
if (retries >= CONFIG_APP_WIFI_PROV_MAX_RETRY_CNT) {
156+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 1)
157+
ESP_LOGI(TAG, "Failed to connect with provisioned AP, reseting provisioned credentials");
158+
wifi_prov_mgr_reset_sm_state_on_failure();
159+
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_RESTART, NULL, 0, portMAX_DELAY);
160+
#else
161+
ESP_LOGW(TAG, "Failed to connect with provisioned AP, please reset to provisioning manually");
162+
#endif
163+
retries = 0;
164+
}
165+
#endif
150166
break;
151167
}
152168
case WIFI_PROV_CRED_SUCCESS:
153169
ESP_LOGI(TAG, "Provisioning successful");
170+
#ifdef CONFIG_APP_WIFI_RESET_PROV_ON_FAILURE
171+
retries = 0;
172+
#endif
154173
break;
155174
case WIFI_PROV_END:
156175
if (prov_stop_timer) {

examples/common/app_wifi/app_wifi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef enum {
1818
APP_WIFI_EVENT_QR_DISPLAY = 1,
1919
/** Provisioning timed out */
2020
APP_WIFI_EVENT_PROV_TIMEOUT,
21+
/** Provisioning has restarted due to failures (Invalid SSID/Passphrase) */
22+
APP_WIFI_EVENT_PROV_RESTART,
2123
} app_wifi_event_t;
2224

2325
/** Types of Proof of Possession */

0 commit comments

Comments
 (0)