-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WiFi error when processing STA_DISCONNECTED event with reason 0 #7406
Comments
Just to clarify, you are getting this STA_DISCONNECTED at boot, before the device has ever connected to an AP? I don't think this is coming from IDF, AFAICT that event code is an arduino thing. Since the station has not actually disconnected, I think this is probably an erroneous pathway (hence the reason 0), and tightening the logic there should help out a lot of connection issues. |
@Kuenlun Excellent analysis. Accessing array element system_event_reasons[-1] would indeed cause a crash.
I would change
To
Why IDF returns 0 is an issue for someone to look into. As you noted that is not a valid reason in "esp_wifi_types.h". Be careful with xTimerCreate(). That is an ESP-IDF native call. The Arduino core uses 1 of the system timers. It's possible to break that timer and cause odd behaviors. See the "Ticker" class for a safe way to fire timers in Arduino. Possibly related: #7344 |
@mrengineer7777 Thanks for the feedback. I understand that the proper way of fixing this involves modifiying:
And also understanding why this reason is returned in the first place. But all of this is very unsafe as the fix could unchain many unexpected errors. I think the changes you propose are ok.
I would like to add a minor change, replacing
The From my code style point of view, I would do the following.
But I understand it will break the code style of the file and the project itself. I will propose a pull request adding the second fix. With respect to "xTimerCreate" function I'll have a look to what you say. I have implemented timers in this way because of Shawn Hymel (Digi-Key) FreeRTOS tutorials from YouTube, but I don't have much experience with it. |
Fixed #7406 . The "reason2str" macro in WiFiGeneric.cpp tries to read memory from index "-1" in "system_event_reasons" array when handling STA_DISCONNECTED event with reason 0. Dealing with reason 0 as a reason 1 (WIFI_REASON_UNSPECIFIED) will solve the problem (the reason for this event to arrive with reason 0 is unknown). #7406
Board
esp32-s3-devkitc-1
Device Description
Bare esp32-s3-devkitc-1 board connected through micro-USB cable (on the UART port) to see serial monitor.
Hardware Configuration
No other hardware is attached to the development board.
Version
v2.0.4
IDE Name
PlatformIO
Operating System
Microsoft Windows 10 Home (10.0.19045 compilation 19045)
Flash frequency
40MHz
PSRAM enabled
no
Upload speed
460800
Description
I've been developing a WiFi keep alive FreeRTOS task implemented with events for maximum eficiency (as much as I can get with the Arduino framework).
Each time I get "ARDUINO_EVENT_WIFI_STA_DISCONNECTED" event, I try to reconnect in two different ways depending on the reason:
[Extra info]
As my router won't let me connect for 60 seconds and the purpose of the task im developing is to keep the WiFi alive, I have disabled WiFi reconnection: "WiFi.setAutoReconnect(false);"
If I don't do this I get lots of warnings and errors each time WiFi tries to reconnect (each 100 ms). With WiFi reconnection disabled I only get it twice (because even with WiFi reconnection disabled, the ESP tries to reconnect once).
The problem arises when very rarely (sometimes I have to reboot the board 50 times) Arduino gets the "ARDUINO_EVENT_WIFI_STA_DISCONNECTED" event with reason "0" (it isn't even listed in "arduino-esp32/blob/master/tools/sdk/esp32s3/include/esp_wifi/include/esp_wifi_types.h"):
typedef enum {
WIFI_REASON_UNSPECIFIED = 1,
WIFI_REASON_AUTH_EXPIRE = 2,
...
WIFI_REASON_ASSOC_FAIL = 203,
WIFI_REASON_HANDSHAKE_TIMEOUT = 204,
WIFI_REASON_CONNECTION_FAIL = 205,
WIFI_REASON_AP_TSF_RESET = 206,
WIFI_REASON_ROAMING = 207,
} wifi_err_reason_t;
At that moment I get the following:
I have traced the error and found that inside "WiFiGeneric.cpp" file line 950, there is a call to the "reason2str" macro.
928:
if(event->event_id < ARDUINO_EVENT_MAX) {
929:
log_d("Arduino Event: %d - %s", event->event_id, arduino_event_names[event->event_id]);
930:
}
...
948:
} else if(event->event_id == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
949:
uint8_t reason = event->event_info.wifi_sta_disconnected.reason;
950:
log_w("Reason: %u - %s", reason, reason2str(reason));
...
As you can see, when the reason is 0, the program tries to read memory from "system_event_reasons[-1]".
#define reason2str(r) ((r>176)?system_event_reasons[r-176]:system_event_reasons[r-1])
This problem can be easily fixed adding the reason 0 to the "system_event_reasons" array, also to the "esp_wifi_types.h" "wifi_err_reason_t" enum, and changing the "reason2str" macro to.
As the reason 0 is suposed not to exists maybe the problem is from ESP-IDF. What do you think?
Thanks in advanced.
Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: