Fix DNS servers not sticking when static IP address is used

This commit is contained in:
Sandeep Mistry
2018-10-22 11:59:37 +02:00
committed by Sandeep Mistry
parent 2aed17fc1e
commit bd477c1d6b
2 changed files with 12 additions and 0 deletions

View File

@@ -43,6 +43,7 @@ WiFiClass::WiFiClass() :
_eventGroup = xEventGroupCreate(); _eventGroup = xEventGroupCreate();
memset(&_apRecord, 0x00, sizeof(_apRecord)); memset(&_apRecord, 0x00, sizeof(_apRecord));
memset(&_ipInfo, 0x00, sizeof(_ipInfo)); memset(&_ipInfo, 0x00, sizeof(_ipInfo));
memset(&_dnsServers, 0x00, sizeof(_dnsServers));
} }
uint8_t WiFiClass::status() uint8_t WiFiClass::status()
@@ -303,6 +304,9 @@ void WiFiClass::setDNS(/*IPAddress*/uint32_t dns_server1, /*IPAddress*/uint32_t
ip_addr_t d; ip_addr_t d;
d.type = IPADDR_TYPE_V4; d.type = IPADDR_TYPE_V4;
_dnsServers[0] = dns_server1;
_dnsServers[1] = dns_server2;
if (dns_server1) { if (dns_server1) {
d.u_addr.ip4.addr = static_cast<uint32_t>(dns_server1); d.u_addr.ip4.addr = static_cast<uint32_t>(dns_server1);
dns_setserver(0, &d); dns_setserver(0, &d);
@@ -622,6 +626,9 @@ void WiFiClass::handleSystemEvent(system_event_t* event)
esp_wifi_sta_get_ap_info(&_apRecord); esp_wifi_sta_get_ap_info(&_apRecord);
if (_ipInfo.ip.addr) { if (_ipInfo.ip.addr) {
// re-apply the custom DNS settings
setDNS(_dnsServers[0], _dnsServers[1]);
// static IP // static IP
_status = WL_CONNECTED; _status = WL_CONNECTED;
} }
@@ -647,6 +654,7 @@ void WiFiClass::handleSystemEvent(system_event_t* event)
case SYSTEM_EVENT_STA_LOST_IP: case SYSTEM_EVENT_STA_LOST_IP:
memset(&_ipInfo, 0x00, sizeof(_ipInfo)); memset(&_ipInfo, 0x00, sizeof(_ipInfo));
memset(&_dnsServers, 0x00, sizeof(_dnsServers));
_status = WL_CONNECTION_LOST; _status = WL_CONNECTION_LOST;
break; break;
@@ -672,6 +680,9 @@ void WiFiClass::handleSystemEvent(system_event_t* event)
tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &_ipInfo); tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &_ipInfo);
tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP); tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
// re-apply the custom DNS settings
setDNS(_dnsServers[0], _dnsServers[1]);
} else { } else {
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &_ipInfo); tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &_ipInfo);
} }

View File

@@ -117,6 +117,7 @@ private:
wifi_ap_record_t _scanResults[MAX_SCAN_RESULTS]; wifi_ap_record_t _scanResults[MAX_SCAN_RESULTS];
wifi_ap_record_t _apRecord; wifi_ap_record_t _apRecord;
tcpip_adapter_ip_info_t _ipInfo; tcpip_adapter_ip_info_t _ipInfo;
uint32_t _dnsServers[2];
netif_input_fn _staNetifInput; netif_input_fn _staNetifInput;
netif_input_fn _apNetifInput; netif_input_fn _apNetifInput;