mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-18 23:03:34 +03:00
Better handling of wifi disconnect (#231)
When network interface is down, some nasty things happen, for instance tcp_connect returns without ever calling error callback. This change adds some workarounds for that: before doing a tcp connect and DNS resolve we check if there is a route available. Also added a listener for wifi events which stops (aborts) all the WiFiClients and WiFiUDPs when wifi is disconnected. This should help libraries detect disconnect properly.
This commit is contained in:
@ -33,6 +33,8 @@ extern "C" {
|
||||
#include "lwip/dns.h"
|
||||
}
|
||||
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiUdp.h"
|
||||
|
||||
extern "C" void esp_schedule();
|
||||
extern "C" void esp_yield();
|
||||
@ -42,6 +44,7 @@ ESP8266WiFiClass::ESP8266WiFiClass()
|
||||
, _useClientMode(false)
|
||||
, _useStaticIp(false)
|
||||
{
|
||||
wifi_set_event_handler_cb((wifi_event_handler_cb_t)&ESP8266WiFiClass::_eventCallback);
|
||||
}
|
||||
|
||||
void ESP8266WiFiClass::mode(WiFiMode m)
|
||||
@ -104,8 +107,8 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
|
||||
wifi_set_channel(channel);
|
||||
}
|
||||
|
||||
if(!_useStaticIp)
|
||||
wifi_station_dhcpc_start();
|
||||
if(!_useStaticIp)
|
||||
wifi_station_dhcpc_start();
|
||||
return status();
|
||||
}
|
||||
|
||||
@ -128,8 +131,8 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s
|
||||
|
||||
wifi_station_dhcpc_stop();
|
||||
wifi_set_ip_info(STATION_IF, &info);
|
||||
|
||||
_useStaticIp = true;
|
||||
|
||||
_useStaticIp = true;
|
||||
}
|
||||
|
||||
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns)
|
||||
@ -146,8 +149,8 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s
|
||||
ip_addr_t d;
|
||||
d.addr = static_cast<uint32_t>(dns);
|
||||
dns_setserver(0,&d);
|
||||
|
||||
_useStaticIp = true;
|
||||
|
||||
_useStaticIp = true;
|
||||
}
|
||||
|
||||
int ESP8266WiFiClass::disconnect()
|
||||
@ -588,6 +591,17 @@ void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result)
|
||||
}
|
||||
}
|
||||
|
||||
void ESP8266WiFiClass::_eventCallback(void* arg)
|
||||
{
|
||||
System_Event_t* event = reinterpret_cast<System_Event_t*>(arg);
|
||||
DEBUGV("wifi evt: %d\r\n", event->event);
|
||||
|
||||
if (event->event == EVENT_STAMODE_DISCONNECTED) {
|
||||
WiFiClient::stopAll();
|
||||
WiFiUDP::stopAll();
|
||||
}
|
||||
}
|
||||
|
||||
void ESP8266WiFiClass::printDiag(Print& p)
|
||||
{
|
||||
const char* modes[] = {"NULL", "STA", "AP", "STA+AP"};
|
||||
|
Reference in New Issue
Block a user