Skip to content

Commit

Permalink
Make sure to call SDK functions to start and stop DHCP server
Browse files Browse the repository at this point in the history
As noticed in esp8266#8582 (comment)
Can't really use `server.begin()` and `server.end()` directly, only
default static IP is applied to the interface since DHCP server is
deemed 'running' (see `wifi_softap_dhcps_status()` return value)
  • Loading branch information
mcspr committed Jun 14, 2022
1 parent 98c2837 commit 13b8ff0
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
DEBUG_WIFI("[AP] softap config unchanged\n");
}

auto& server = softAPDhcpServer();
server.end();
wifi_softap_dhcps_stop();

// check IP config
struct ip_info ip;
Expand All @@ -179,17 +178,13 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
IPAddress(192, 168, 4, 1),
IPAddress(192, 168, 4, 1),
IPAddress(255, 255, 255, 0));
if(!ret) {
DEBUG_WIFI("[AP] softAPConfig failed!\n");
ret = false;
}
}
} else {
DEBUG_WIFI("[AP] wifi_get_ip_info failed!\n");
ret = false;
}

server.begin();
wifi_softap_dhcps_start();

return ret;
}
Expand Down Expand Up @@ -227,9 +222,10 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
info.gw.addr = gateway.v4();
info.netmask.addr = subnet.v4();

auto& server = softAPDhcpServer();
server.end();

// use SDK function for dhcps, not just server.begin()
// setting info with static IPs will fail otherwise
// (TODO: dhcps_flag seems to store 'SDK' DHCPs status)
wifi_softap_dhcps_stop();
if(!wifi_set_ip_info(SOFTAP_IF, &info)) {
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
ret = false;
Expand All @@ -246,14 +242,17 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
dhcp_lease.end_ip.addr = ip.v4();
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());

auto& server = softAPDhcpServer();
if(!server.set_dhcps_lease(&dhcp_lease))
{
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
DEBUG_WIFI("[APConfig] server set_dhcps_lease failed!\n");
ret = false;
}

server.setRouter(true); // send ROUTER option with netif's gateway IP
server.begin();
// send ROUTER option with netif's gateway IP
server.setRouter(true);

wifi_softap_dhcps_start();

// check config
if(wifi_get_ip_info(SOFTAP_IF, &info)) {
Expand Down

0 comments on commit 13b8ff0

Please sign in to comment.