Allow DHCP to be started again by giving a zero IP address to STA config

Fixes: https://github.com/espressif/arduino-esp32/issues/654
This commit is contained in:
me-no-dev 2017-09-22 12:20:53 +08:00
parent 1407654c52
commit eb282131ba

View File

@ -199,6 +199,7 @@ void WiFiSTAClass::_setStatus(wl_status_t status)
*/ */
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
{ {
esp_err_t err = ESP_OK;
if(!WiFi.enableSTA(true)) { if(!WiFi.enableSTA(true)) {
return false; return false;
@ -206,16 +207,40 @@ bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subne
esp_wifi_start(); esp_wifi_start();
tcpip_adapter_ip_info_t info; tcpip_adapter_ip_info_t info;
info.ip.addr = static_cast<uint32_t>(local_ip);
info.gw.addr = static_cast<uint32_t>(gateway);
info.netmask.addr = static_cast<uint32_t>(subnet);
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); if(local_ip != (uint32_t)0x00000000){
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info) == ESP_OK) { info.ip.addr = static_cast<uint32_t>(local_ip);
_useStaticIp = true; info.gw.addr = static_cast<uint32_t>(gateway);
info.netmask.addr = static_cast<uint32_t>(subnet);
} else { } else {
info.ip.addr = 0;
info.gw.addr = 0;
info.netmask.addr = 0;
}
err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){
log_e("DHCP could not be stopped! Error: %d", err);
return false; return false;
} }
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info);
if(err != ERR_OK){
log_e("STA IP could not be configured! Error: %d", err);
return false;
}
if(info.ip.addr){
_useStaticIp = true;
} else {
err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED){
log_w("DHCP could not be started! Error: %d", err);
return false;
}
_useStaticIp = false;
}
ip_addr_t d; ip_addr_t d;
d.type = IPADDR_TYPE_V4; d.type = IPADDR_TYPE_V4;