From 5ad6ff9d96f4b947c3c18b49339ad027bc83df8e Mon Sep 17 00:00:00 2001 From: Mehdi Beyk Mohamadi Date: Thu, 15 Aug 2019 02:20:03 +0430 Subject: [PATCH 1/7] Added ability to set start and end ip for softAP Base on #3562 and discussed in #6031 --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 49 ++++++++++++++++++--- libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 1 + 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 692b214fad..440f80ff7b 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -25,6 +25,7 @@ #include "ESP8266WiFi.h" #include "ESP8266WiFiGeneric.h" #include "ESP8266WiFiAP.h" +#include "utility/util.h" extern "C" { #include "c_types.h" @@ -194,8 +195,10 @@ bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, in * @param local_ip access point IP * @param gateway gateway IP (0.0.0.0 to disable) * @param subnet subnet mask + * @param dhcp_start first IP assigned by DHCP + * @param dhcp_end last IP assigned by DHCP */ -bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { +bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end) { DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str()); if(!WiFi.enableAP(true)) { // enable AP failed @@ -214,10 +217,24 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA return false; } struct ip_info info; + info.ip.addr = local_ip.v4(); info.gw.addr = gateway.v4(); info.netmask.addr = subnet.v4(); - + + uint32_t softap_ip = htonl(info.ip.addr); + uint32_t start_ip = htonl(dhcp_start.v4()); + uint32_t end_ip = htonl(dhcp_end.v4()); + softap_ip >>= 8; + if (( start_ip >> 8 != softap_ip ) || ( end_ip >> 8 != softap_ip )) { + DEBUG_WIFI("[APConfig] dhcp_start or dhcp_end isn't in range of local_ip Address!\n"); + DEBUG_WIFI("[APConfig] set dfault vaule\n"); + dhcp_start = local_ip; + dhcp_start[3] += 99; + dhcp_end = dhcp_start; + dhcp_end[3] += 100; + } + if(!wifi_softap_dhcps_stop()) { DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n"); } @@ -228,13 +245,14 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA } struct dhcps_lease dhcp_lease; - IPAddress ip = local_ip; - ip[3] += 99; - dhcp_lease.start_ip.addr = ip.v4(); + dhcp_lease.enable = true; + + IPAddress ip = dhcp_start; + dhcp_lease.start_ip.addr = dhcp_start.v4(); DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str()); - ip[3] += 100; - dhcp_lease.end_ip.addr = ip.v4(); + ip = dhcp_end; + dhcp_lease.end_ip.addr = dhcp_end.v4(); DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { @@ -277,7 +295,24 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA return ret; } +/** + * Configure access point + * @param local_ip access point IP + * @param gateway gateway IP + * @param subnet subnet mask + */ +bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + IPAddress dhcp_start; + IPAddress dhcp_end; + + dhcp_start = local_ip; + dhcp_start[3] += 99; + dhcp_end = dhcp_start; + dhcp_end[3] += 100; + bool ret = softAPConfig(local_ip, gateway, subnet, dhcp_start, dhcp_end); + return ret; +} /** * Disconnect from the network (close AP) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index 599c8e0e11..8781c5c57f 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -38,6 +38,7 @@ class ESP8266WiFiAPClass { bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4); + bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end); bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); bool softAPdisconnect(bool wifioff = false); From 802b68ba7d1e538f0fd72d9fcc757cf9e9234d4f Mon Sep 17 00:00:00 2001 From: Mehdi Beyk Mohamadi Date: Thu, 15 Aug 2019 02:55:31 +0430 Subject: [PATCH 2/7] Update ESP8266WiFiAP.cpp --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 440f80ff7b..7659801e6d 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -25,7 +25,6 @@ #include "ESP8266WiFi.h" #include "ESP8266WiFiGeneric.h" #include "ESP8266WiFiAP.h" -#include "utility/util.h" extern "C" { #include "c_types.h" @@ -38,7 +37,12 @@ extern "C" { #include "debug.h" - +#ifndef htonl +#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ + ((x)<< 8 & 0x00FF0000UL) | \ + ((x)>> 8 & 0x0000FF00UL) | \ + ((x)>>24 & 0x000000FFUL) ) +#endif // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ From 9f1b343b09b3d710bd526c201ee65beb83d0e919 Mon Sep 17 00:00:00 2001 From: Mehdi Beyk Mohamadi Date: Thu, 15 Aug 2019 15:48:58 +0430 Subject: [PATCH 3/7] Fix Static DHCP Lease Fix #6031 --- .../examples/StaticLease/StaticLease.ino | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index fa183d0fbe..38392002dd 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -23,6 +23,10 @@ void loop() { const char *ssid = "ESPap"; const char *password = "thereisnospoon"; +/* List of mac address for static lease */ +uint8 mac_CAM[6] = { 0x00, 0x0C, 0x43, 0x01, 0x60, 0x15 }; +uint8 mac_PC[6] = { 0xb4, 0x52, 0x7e, 0x9a, 0x19, 0xa5 }; + ESP8266WebServer server(80); /* Set the IP Address you want for your AP */ @@ -44,7 +48,6 @@ void handleRoot() { result += String(number_client); result += "
"; while (stat_info != NULL) { - result += "Client "; result += String(i); result += " = "; @@ -53,7 +56,6 @@ void handleRoot() { sprintf(wifiClientMac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(stat_info->bssid)); result += wifiClientMac; result += "
"; - stat_info = STAILQ_NEXT(stat_info, next); i++; } @@ -63,10 +65,6 @@ void handleRoot() { } void setup() { - /* List of mac address for static lease */ - uint8 mac_CAM[6] = { 0x00, 0x0C, 0x43, 0x01, 0x60, 0x15 }; - uint8 mac_PC[6] = { 0xb4, 0x52, 0x7e, 0x9a, 0x19, 0xa5 }; - Serial.begin(115200); Serial.println(); Serial.println("Configuring access point..."); @@ -75,15 +73,18 @@ void setup() { WiFi.persistent(false); WiFi.mode(WIFI_AP); + /* Configure AP with IP = 192.168.0.1 / Gateway = 192.168.0.1 / Subnet = 255.255.255.0 if you specify the ESP8266's IP-address with 192.168.0.1, the function softAPConfig() sets the DHCP-range as 192.168.0.100 - 192.168.0.200 */ WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); - /* Setup your static leases. - As it depend from your first address, and need to be done BEFORE any request from client, - this need to be specified after WiFi.softAPConfig() and before WiFi.softAP(). + /* Start Access Point. You can remove the password parameter if you want the AP to be open. */ + WiFi.softAP(ssid, password); + /* Setup your static leases. + As it depend from your first address, and need to be done BEFORE any request from client, + this need to be specified after WiFi.softAPConfig() and before WiFi.softAP(). first call to wifi_softap_add_dhcps_lease() will setup first IP address of the range second call to wifi_softap_add_dhcps_lease() will setup second IP address of the range ... @@ -91,10 +92,10 @@ void setup() { */ wifi_softap_add_dhcps_lease(mac_CAM); // always 192.168.0.100 wifi_softap_add_dhcps_lease(mac_PC); // always 192.168.0.101 - /* Start Access Point. You can remove the password parameter if you want the AP to be open. */ - WiFi.softAP(ssid, password); + Serial.print("AP IP address: "); Serial.println(WiFi.softAPIP()); + /* Setup HTTP Server */ server.on("/", handleRoot); server.begin(); @@ -105,4 +106,4 @@ void loop() { server.handleClient(); } -#endif // lwIP-v2 +#endif // lwIP-v2 \ No newline at end of file From eeeffbcfa2221ab54e17bd90fee1204a73e95d7a Mon Sep 17 00:00:00 2001 From: Mehdi Beyk Mohamadi Date: Thu, 15 Aug 2019 15:53:34 +0430 Subject: [PATCH 4/7] Update StaticLease.ino --- libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index 38392002dd..361cfe5a70 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -83,8 +83,8 @@ void setup() { WiFi.softAP(ssid, password); /* Setup your static leases. - As it depend from your first address, and need to be done BEFORE any request from client, - this need to be specified after WiFi.softAPConfig() and before WiFi.softAP(). + As it depend from your first address, and need to be done BEFORE any request from client, + this need to be specified after WiFi.softAP(). first call to wifi_softap_add_dhcps_lease() will setup first IP address of the range second call to wifi_softap_add_dhcps_lease() will setup second IP address of the range ... From 5bc4914837304a6dc9ad75a7fd5f46b9428e7026 Mon Sep 17 00:00:00 2001 From: Mehdi Beyk Mohamadi Date: Thu, 15 Aug 2019 15:57:01 +0430 Subject: [PATCH 5/7] Update StaticLease.ino --- libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index 361cfe5a70..baccbbec24 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -106,4 +106,4 @@ void loop() { server.handleClient(); } -#endif // lwIP-v2 \ No newline at end of file +#endif // lwIP-v2 From 47bf960d5d5ada224ffbaa968f71a730dd7562a2 Mon Sep 17 00:00:00 2001 From: Mehdi Beyk Mohamadi Date: Wed, 4 Sep 2019 02:33:46 +0430 Subject: [PATCH 6/7] Add some sainity check --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 44 +++++++++++++-------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 7659801e6d..36e7a77aef 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -33,17 +33,12 @@ extern "C" { #include "osapi.h" #include "mem.h" #include "user_interface.h" +#include "lwip/def.h" +#include "dhcpserver.h" } #include "debug.h" -#ifndef htonl -#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ - ((x)<< 8 & 0x00FF0000UL) | \ - ((x)>> 8 & 0x0000FF00UL) | \ - ((x)>>24 & 0x000000FFUL) ) -#endif - // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ // ----------------------------------------------------------------------------------------------------------------------- @@ -213,6 +208,8 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA if ( !local_ip.isV4() || !subnet.isV4() + || !dhcp_start.isV4() + || !dhcp_end.isV4() #if LWIP_IPV6 // uninitialized gateway is valid || gateway.isV6() @@ -220,6 +217,12 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA ) { return false; } + + if( subnet != IPAddress(255,255,255,0)) { + DEBUG_WIFI("[APConfig] invalid netmask. only netmask \"255.255.255.0\" is valid!\n"); + return false; + } + struct ip_info info; info.ip.addr = local_ip.v4(); @@ -229,16 +232,25 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA uint32_t softap_ip = htonl(info.ip.addr); uint32_t start_ip = htonl(dhcp_start.v4()); uint32_t end_ip = htonl(dhcp_end.v4()); - softap_ip >>= 8; - if (( start_ip >> 8 != softap_ip ) || ( end_ip >> 8 != softap_ip )) { - DEBUG_WIFI("[APConfig] dhcp_start or dhcp_end isn't in range of local_ip Address!\n"); - DEBUG_WIFI("[APConfig] set dfault vaule\n"); - dhcp_start = local_ip; - dhcp_start[3] += 99; - dhcp_end = dhcp_start; - dhcp_end[3] += 100; + + if ((start_ip <= softap_ip) + || ( end_ip <= softap_ip)) { + DEBUG_WIFI("[APConfig] config ip information can't contain local ip!\n"); + return false; } + if ((end_ip - start_ip) > DHCPS_MAX_LEASE) { + DEBUG_WIFI("[APConfig] the value of dhcp lease is greater than \"DHCPS_MAX_LEASE\"!\n"); + return false; + } + + softap_ip >>= 8; + if ((start_ip >> 8 != softap_ip) + || (end_ip >> 8 != softap_ip)) { + DEBUG_WIFI("[APConfig] config ip information must be in the same segment as the local ip!\n"); + return false; + } + if(!wifi_softap_dhcps_stop()) { DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n"); } @@ -310,7 +322,7 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA IPAddress dhcp_end; dhcp_start = local_ip; - dhcp_start[3] += 99; + dhcp_start[3] += 1; dhcp_end = dhcp_start; dhcp_end[3] += 100; From 560460d64bb1e5cd912abe4641b1d0ff925a526b Mon Sep 17 00:00:00 2001 From: Mehdi Beyk Mohamadi Date: Wed, 4 Sep 2019 13:10:01 +0430 Subject: [PATCH 7/7] Fix some includes. --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 36e7a77aef..0a2c393466 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -25,6 +25,13 @@ #include "ESP8266WiFi.h" #include "ESP8266WiFiGeneric.h" #include "ESP8266WiFiAP.h" +#include "lwip/def.h" + +#if LWIP_VERSION_MAJOR == 1 +#include "lwip/app/dhcpserver.h" +#else +#include "dhcpserver.h" +#endif extern "C" { #include "c_types.h" @@ -33,8 +40,6 @@ extern "C" { #include "osapi.h" #include "mem.h" #include "user_interface.h" -#include "lwip/def.h" -#include "dhcpserver.h" } #include "debug.h" @@ -219,8 +224,8 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA } if( subnet != IPAddress(255,255,255,0)) { - DEBUG_WIFI("[APConfig] invalid netmask. only netmask \"255.255.255.0\" is valid!\n"); - return false; + DEBUG_WIFI("[APConfig] invalid netmask. only netmask \"255.255.255.0\" is valid!\n"); + return false; } struct ip_info info;