From 2a46b8fac65d38b5b4effdf25816e892ffe729df Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Tue, 19 Apr 2022 20:05:34 +0300 Subject: [PATCH 01/16] Initialize SoftAP DhcpServer object on demand Remove dependency on global ctor, and just construct the object when someone asks us to do it. Only dependency right now is netif_git, which is expected to be initialized by the lwip code some time before dhcps_start happens. Removing ip_info from begin(), since we never reference later on. Also removing the specific check for netif id and simplify the ctors. Update tests and recover old nonos-sdk dhcps functions that were not implemented. --- cores/esp8266/LwipDhcpServer-NonOS.cpp | 87 +++++++++++++------ cores/esp8266/LwipDhcpServer.cpp | 49 +++-------- cores/esp8266/LwipDhcpServer.h | 35 ++++---- .../RangeExtender-NAPT/RangeExtender-NAPT.ino | 5 +- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 16 ++-- tests/host/common/user_interface.cpp | 12 +-- tools/sdk/include/user_interface.h | 7 +- 7 files changed, 112 insertions(+), 99 deletions(-) diff --git a/cores/esp8266/LwipDhcpServer-NonOS.cpp b/cores/esp8266/LwipDhcpServer-NonOS.cpp index aee22b6d5c..3a9ecb9016 100644 --- a/cores/esp8266/LwipDhcpServer-NonOS.cpp +++ b/cores/esp8266/LwipDhcpServer-NonOS.cpp @@ -1,5 +1,5 @@ /* - lwIPDhcpServer-NonOS.cpp - DHCP server wrapper + lwIPDhcpServer-NonOS - DHCP server wrapper Copyright (c) 2020 esp8266 arduino. All rights reserved. This file is part of the esp8266 core for Arduino environment. @@ -23,41 +23,76 @@ // these functions must exists as-is with "C" interface, // nonos-sdk calls them at boot time and later -#include // LWIP_VERSION +#include "LwipDhcpServer-NonOS.h" #include -#include "LwipDhcpServer.h" -extern netif netif_git[2]; - -// global DHCP instance for softAP interface -DhcpServer dhcpSoftAP(&netif_git[SOFTAP_IF]); +// Global static DHCP instance for softAP interface +// (since the netif object never goes away, even when AP is disabled) +// Initial version fully emulates nonos-sdk api in DhcpServer class, +// before trying to further change it and possibly break legacy behaviour +DhcpServer& dhcpSoftAP() { + extern netif netif_git[2]; + static DhcpServer server(&netif_git[SOFTAP_IF]); + return server; +} extern "C" { - void dhcps_start(struct ip_info* info, netif* apnetif) - { - // apnetif is esp interface, replaced by lwip2's - // netif_git[SOFTAP_IF] interface in constructor - (void)apnetif; - -#if 0 - // can't use C++ now, global ctors are not initialized yet - dhcpSoftAP.begin(info); -#else - (void)info; - // initial version: emulate nonos-sdk in DhcpServer class before - // trying to change legacy behavor - // `fw_has_started_softap_dhcps` will be read in DhcpServer::DhcpServer - // which is called when c++ ctors are initialized, specifically - // dhcpSoftAP initialized with AP interface number above. - fw_has_started_softap_dhcps = 1; -#endif + // `ip_info` is useless, since we get the information from the netif directly + // `netif` would be netif_git[SOFTAP_IF], which we get from the lwip2 glue + void dhcps_start(ip_info *, netif *) + { + auto& server = dhcpSoftAP(); + if (!server.isRunning()) { + server.begin(); + } } void dhcps_stop() { - dhcpSoftAP.end(); + auto& server = dhcpSoftAP(); + if (server.isRunning()) { + server.end(); + } + } + + // providing the rest of the nonos-sdk API, which was originally removed in 3.0.0 + + bool wifi_softap_set_dhcps_lease(dhcps_lease *please) + { + auto& server = dhcpSoftAP(); + return server.set_dhcps_lease(please); + } + + bool wifi_softap_get_dhcps_lease(dhcps_lease *please) + { + auto& server = dhcpSoftAP(); + return server.get_dhcps_lease(please); + } + + uint32 wifi_softap_get_dhcps_lease_time() + { + auto& server = dhcpSoftAP(); + return server.get_dhcps_lease_time(); + } + + bool wifi_softap_set_dhcps_lease_time(uint32 minutes) + { + auto& server = dhcpSoftAP(); + return server.set_dhcps_lease_time(minutes); + } + + bool wifi_softap_reset_dhcps_lease_time() + { + auto& server = dhcpSoftAP(); + return server.reset_dhcps_lease_time(); + } + + bool wifi_softap_add_dhcps_lease(uint8 *macaddr) + { + auto& server = dhcpSoftAP(); + return server.add_dhcps_lease(macaddr); } } // extern "C" diff --git a/cores/esp8266/LwipDhcpServer.cpp b/cores/esp8266/LwipDhcpServer.cpp index 07270bb6c2..9e31152da3 100644 --- a/cores/esp8266/LwipDhcpServer.cpp +++ b/cores/esp8266/LwipDhcpServer.cpp @@ -36,8 +36,6 @@ #include // LWIP_VERSION -#define DHCPS_LEASE_TIME_DEF (120) - #define USE_DNS #include "lwip/inet.h" @@ -166,7 +164,7 @@ const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; int ret = 1, errval = (err); \ if (errval != ERR_OK) \ { \ - os_printf("DHCPS ERROR: %s (lwip:%d)\n", what, errval); \ + os_printf("DHCPS ERROR: %s (lwip:%s(%d))\n", what, lwip_strerr(errval), errval); \ ret = 0; \ } \ ret; \ @@ -177,34 +175,11 @@ const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; const uint32 DhcpServer::magic_cookie = 0x63538263; // https://tools.ietf.org/html/rfc1497 -int fw_has_started_softap_dhcps = 0; - //////////////////////////////////////////////////////////////////////////////////// DhcpServer::DhcpServer(netif* netif) : _netif(netif) { - pcb_dhcps = nullptr; - dns_address.addr = 0; - plist = nullptr; - offer = 0xFF; - renew = false; - dhcps_lease_time = DHCPS_LEASE_TIME_DEF; // minute - - if (netif->num == SOFTAP_IF && fw_has_started_softap_dhcps == 1) - { - // When nonos-sdk starts DHCPS at boot: - // 1. `fw_has_started_softap_dhcps` is already initialized to 1 - // 2. global ctor DhcpServer's `dhcpSoftAP(&netif_git[SOFTAP_IF])` is called - // 3. (that's here) => begin(legacy-values) is called - ip_info ip = { - { 0x0104a8c0 }, // IP 192.168.4.1 - { 0x00ffffff }, // netmask 255.255.255.0 - { 0 } // gateway 0.0.0.0 - }; - begin(&ip); - fw_has_started_softap_dhcps = 2; // not 1, ending initial boot sequence - } -}; +} // wifi_softap_set_station_info is missing in user_interface.h: extern "C" void wifi_softap_set_station_info(uint8_t* mac, struct ipv4_addr*); @@ -1008,7 +983,7 @@ void DhcpServer::init_dhcps_lease(uint32 ip) } /////////////////////////////////////////////////////////////////////////////////// -bool DhcpServer::begin(struct ip_info* info) +bool DhcpServer::begin() { if (pcb_dhcps != nullptr) { @@ -1016,9 +991,11 @@ bool DhcpServer::begin(struct ip_info* info) } pcb_dhcps = udp_new(); - if (pcb_dhcps == nullptr || info == nullptr) + if (pcb_dhcps == nullptr) { +#if DHCPS_DEBUG os_printf("dhcps_start(): could not obtain pcb\n"); +#endif return false; } @@ -1030,7 +1007,7 @@ bool DhcpServer::begin(struct ip_info* info) ip_2_ip4(&broadcast_dhcps)->addr |= ~ip_2_ip4(&_netif->netmask)->addr; // XXXFIXMEIPV6 broadcast address? - server_address = info->ip; + server_address = *ip_2_ip4(&_netif->ip_addr); init_dhcps_lease(server_address.addr); udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT); @@ -1040,12 +1017,6 @@ bool DhcpServer::begin(struct ip_info* info) "pcb_dhcps\n"); #endif - if (_netif->num == SOFTAP_IF) - { - wifi_set_ip_info(SOFTAP_IF, info); // added for lwip-git, not sure whether useful - } - _netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP; // added for lwip-git - return true; } @@ -1091,9 +1062,9 @@ void DhcpServer::end() } } -bool DhcpServer::isRunning() +bool DhcpServer::isRunning() const { - return !!_netif->state; + return pcb_dhcps != nullptr; } /****************************************************************************** @@ -1342,7 +1313,7 @@ bool DhcpServer::reset_dhcps_lease_time(void) { return false; } - dhcps_lease_time = DHCPS_LEASE_TIME_DEF; + dhcps_lease_time = DefaultLeaseTime; return true; } diff --git a/cores/esp8266/LwipDhcpServer.h b/cores/esp8266/LwipDhcpServer.h index d5eb6410ef..4ed08edd49 100644 --- a/cores/esp8266/LwipDhcpServer.h +++ b/cores/esp8266/LwipDhcpServer.h @@ -36,14 +36,23 @@ class DhcpServer { public: - DhcpServer(netif* netif); + static constexpr int DefaultLeaseTime = 120 /* seconds */; + static constexpr ip_info DefaultIpConfig = { + .ip { 0x0104a8c0 }, // 192.168.4.1 + .netmask { 0x00ffffff }, // 255.255.255.0 + .gw { 0 } // 0.0.0.0 + }; + + static const uint32 magic_cookie; + + DhcpServer(netif*); ~DhcpServer(); void setDns(int num, const ipv4_addr_t* dns); - bool begin(ip_info* info); + bool begin(); void end(); - bool isRunning(); + bool isRunning() const; // this is the C interface encapsulated in a class // (originally dhcpserver.c in lwIP-v1.4 in NonOS-SDK) @@ -91,25 +100,19 @@ class DhcpServer void dhcps_client_leave(u8* bssid, struct ipv4_addr* ip, bool force); uint32 dhcps_client_update(u8* bssid, struct ipv4_addr* ip); - netif* _netif; + netif* _netif = nullptr; - struct udp_pcb* pcb_dhcps; - ip_addr_t broadcast_dhcps; - struct ipv4_addr server_address; - struct ipv4_addr client_address; - struct ipv4_addr dns_address; - uint32 dhcps_lease_time; + udp_pcb* pcb_dhcps = nullptr; + ip_addr_t broadcast_dhcps{}; + ipv4_addr server_address{}; + ipv4_addr client_address{}; + ipv4_addr dns_address{}; + uint32 dhcps_lease_time = DefaultLeaseTime; struct dhcps_lease dhcps_lease; list_node* plist; uint8 offer; bool renew; - - static const uint32 magic_cookie; }; -// SoftAP DHCP server always exists and is started on boot -extern DhcpServer dhcpSoftAP; -extern "C" int fw_has_started_softap_dhcps; - #endif // __DHCPS_H__ diff --git a/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino b/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino index 2c62ded038..0f73032285 100644 --- a/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino +++ b/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino @@ -54,8 +54,9 @@ void setup() { Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str()); // give DNS servers to AP side - dhcpSoftAP.dhcps_set_dns(0, WiFi.dnsIP(0)); - dhcpSoftAP.dhcps_set_dns(1, WiFi.dnsIP(1)); + auto& server = dhcpSoftAP(); + server.dhcps_set_dns(0, WiFi.dnsIP(0)); + server.dhcps_set_dns(1, WiFi.dnsIP(1)); WiFi.softAPConfig( // enable AP, with android-compatible google domain IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0)); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index b0a0084853..48300d662d 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -37,7 +37,9 @@ extern "C" { } #include "debug.h" -#include "LwipDhcpServer.h" + +#include +#include // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ @@ -166,7 +168,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, DEBUG_WIFI("[AP] softap config unchanged\n"); } - dhcpSoftAP.end(); + auto& server = dhcpSoftAP(); + server.end(); // check IP config struct ip_info ip; @@ -186,7 +189,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, ret = false; } - dhcpSoftAP.begin(&ip); + server.begin(); return ret; } @@ -244,21 +247,22 @@ 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()); - if(!dhcpSoftAP.set_dhcps_lease(&dhcp_lease)) + auto& server = dhcpSoftAP(); + if(!server.set_dhcps_lease(&dhcp_lease)) { DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); ret = false; } // set lease time to 720min --> 12h - if(!dhcpSoftAP.set_dhcps_lease_time(720)) + if(!server.set_dhcps_lease_time(720)) { DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n"); ret = false; } uint8 mode = info.gw.addr ? 1 : 0; - if(!dhcpSoftAP.set_dhcps_offer_option(OFFER_ROUTER, &mode)) + if(!server.set_dhcps_offer_option(OFFER_ROUTER, &mode)) { DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n"); ret = false; diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 05f90c704a..32890cbfc4 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -64,15 +64,13 @@ bool DhcpServer::set_dhcps_offer_option(uint8 level, void* optarg) void DhcpServer::end() { } -bool DhcpServer::begin(struct ip_info* info) +bool DhcpServer::begin() { - (void)info; return false; } -DhcpServer::DhcpServer(netif* netif) +DhcpServer::DhcpServer(netif*) { - (void)netif; } DhcpServer::~DhcpServer() @@ -80,7 +78,11 @@ DhcpServer::~DhcpServer() end(); } -DhcpServer dhcpSoftAP(nullptr); +DhcpServer& dhcpSoftAP() +{ + static DhcpServer server(nullptr); + return server; +} extern "C" { diff --git a/tools/sdk/include/user_interface.h b/tools/sdk/include/user_interface.h index e69821b372..2fe43994d1 100644 --- a/tools/sdk/include/user_interface.h +++ b/tools/sdk/include/user_interface.h @@ -382,17 +382,14 @@ void wifi_softap_free_station_info(void); bool wifi_softap_dhcps_start(void); bool wifi_softap_dhcps_stop(void); -#if 1 // dhcp server -// these functions are open-source, in dhcp server, -// which is now moved to lwIPDhcpServer.cpp (lwip2) -// (but still there with lwip1) +// these dhcp functions were moved to LwipDhcpServer-NonOS.cpp, and are no longer provided by the lwip lib + bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please); bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please); uint32 wifi_softap_get_dhcps_lease_time(void); bool wifi_softap_set_dhcps_lease_time(uint32 minute); bool wifi_softap_reset_dhcps_lease_time(void); bool wifi_softap_add_dhcps_lease(uint8 *macaddr); // add static lease on the list, this will be the next available @ -#endif // dhcp server enum dhcp_status wifi_softap_dhcps_status(void); bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg); From 953cae8ba26c6e8e7fa9f8c991ac248cd07329ce Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 00:29:02 +0300 Subject: [PATCH 02/16] nonos helpers have a separate header --- cores/esp8266/LwipDhcpServer-NonOS.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cores/esp8266/LwipDhcpServer-NonOS.h diff --git a/cores/esp8266/LwipDhcpServer-NonOS.h b/cores/esp8266/LwipDhcpServer-NonOS.h new file mode 100644 index 0000000000..3e7c3261b4 --- /dev/null +++ b/cores/esp8266/LwipDhcpServer-NonOS.h @@ -0,0 +1,26 @@ +/* + lwIPDhcpServer-NonOS - DHCP server wrapper + + Copyright (c) 2020 esp8266 arduino. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "LwipDhcpServer.h" + +// We create a SoftAP server by default, it should be operational at boot +// (b/c the SDK will attempt to create it even before we fall into our boot code) +DhcpServer& dhcpSoftAP(); From bb5a7d30d77d5328a25f40751fed5025e03fbd3f Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 01:32:47 +0300 Subject: [PATCH 03/16] wifi ap needs this anyway, simplify sketch includes --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 1 - libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 48300d662d..9fa78370a3 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -39,7 +39,6 @@ extern "C" { #include "debug.h" #include -#include // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index b8b3b8fb12..7ed23d563b 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -27,6 +27,7 @@ #include "ESP8266WiFiType.h" #include "ESP8266WiFiGeneric.h" +#include class ESP8266WiFiAPClass { From 666bab49edd9aaebb9deb984de834bd5dc571927 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 01:48:39 +0300 Subject: [PATCH 04/16] missing example --- libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index e4520c4720..3f55c07fb7 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -76,8 +76,9 @@ void setup() { ... any client not listed will use next IP address available from the range (here 192.168.0.102 and more) */ - dhcpSoftAP.add_dhcps_lease(mac_CAM); // always 192.168.0.100 - dhcpSoftAP.add_dhcps_lease(mac_PC); // always 192.168.0.101 + auto& server = dhcpSoftAP(); + server.add_dhcps_lease(mac_CAM); // always 192.168.0.100 + server.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: "); From 92d7cd6a29d75b3694c2e5c9fea062a42e01d353 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 01:53:30 +0300 Subject: [PATCH 05/16] existing name :/ --- libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index 3f55c07fb7..5cab54bf85 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -76,9 +76,9 @@ void setup() { ... any client not listed will use next IP address available from the range (here 192.168.0.102 and more) */ - auto& server = dhcpSoftAP(); - server.add_dhcps_lease(mac_CAM); // always 192.168.0.100 - server.add_dhcps_lease(mac_PC); // always 192.168.0.101 + auto& dhcpServer = dhcpSoftAP(); + dhcpServer.add_dhcps_lease(mac_CAM); // always 192.168.0.100 + dhcpServer.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: "); From f42de0ade45a5f467b7d90a65a4c3ced9ae02f35 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 02:19:43 +0300 Subject: [PATCH 06/16] trying to fix header dependency --- cores/esp8266/LwipDhcpServer-NonOS.h | 2 + cores/esp8266/LwipDhcpServer.h | 5 +- tools/sdk/lwip2/include/dhcpserver.h | 128 +-------------------------- 3 files changed, 4 insertions(+), 131 deletions(-) diff --git a/cores/esp8266/LwipDhcpServer-NonOS.h b/cores/esp8266/LwipDhcpServer-NonOS.h index 3e7c3261b4..1e56c9a357 100644 --- a/cores/esp8266/LwipDhcpServer-NonOS.h +++ b/cores/esp8266/LwipDhcpServer-NonOS.h @@ -19,6 +19,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#pragma once + #include "LwipDhcpServer.h" // We create a SoftAP server by default, it should be operational at boot diff --git a/cores/esp8266/LwipDhcpServer.h b/cores/esp8266/LwipDhcpServer.h index 4ed08edd49..098c53a191 100644 --- a/cores/esp8266/LwipDhcpServer.h +++ b/cores/esp8266/LwipDhcpServer.h @@ -28,8 +28,7 @@ // nearly as-is. This is an initial version to guaranty legacy behavior // with same default values. -#ifndef __DHCPS_H__ -#define __DHCPS_H__ +#pragma once #include // LWIP_VERSION @@ -114,5 +113,3 @@ class DhcpServer uint8 offer; bool renew; }; - -#endif // __DHCPS_H__ diff --git a/tools/sdk/lwip2/include/dhcpserver.h b/tools/sdk/lwip2/include/dhcpserver.h index 4ec907126f..4f00006162 100644 --- a/tools/sdk/lwip2/include/dhcpserver.h +++ b/tools/sdk/lwip2/include/dhcpserver.h @@ -1,130 +1,4 @@ - // adapted from dhcpserver.c distributed in esp8266 sdk 2.0.0 // same license may apply -#ifndef __DHCPS_H__ -#define __DHCPS_H__ - -#include "glue.h" // for UDEBUG - -#define USE_DNS - -typedef struct dhcps_state{ - sint16_t state; -} dhcps_state; - -typedef struct dhcps_msg { - uint8_t op, htype, hlen, hops; - uint8_t xid[4]; - uint16_t secs, flags; - uint8_t ciaddr[4]; - uint8_t yiaddr[4]; - uint8_t siaddr[4]; - uint8_t giaddr[4]; - uint8_t chaddr[16]; - uint8_t sname[64]; - uint8_t file[128]; - uint8_t options[312]; -}dhcps_msg; - -#ifndef LWIP_OPEN_SRC -struct dhcps_lease { - bool enable; - struct ipv4_addr start_ip; - struct ipv4_addr end_ip; -}; - -enum dhcps_offer_option{ - OFFER_START = 0x00, - OFFER_ROUTER = 0x01, - OFFER_END -}; -#endif - -typedef enum { - DHCPS_TYPE_DYNAMIC, - DHCPS_TYPE_STATIC -} dhcps_type_t; - -typedef enum { - DHCPS_STATE_ONLINE, - DHCPS_STATE_OFFLINE -} dhcps_state_t; - -struct dhcps_pool{ - struct ipv4_addr ip; - uint8 mac[6]; - uint32 lease_timer; - dhcps_type_t type; - dhcps_state_t state; - -}; - -typedef struct _list_node{ - void *pnode; - struct _list_node *pnext; -}list_node; - -extern uint32 dhcps_lease_time; -#define DHCPS_LEASE_TIMER dhcps_lease_time //0x05A0 -#define DHCPS_MAX_LEASE 0x64 -#define BOOTP_BROADCAST 0x8000 - -#define DHCP_REQUEST 1 -#define DHCP_REPLY 2 -#define DHCP_HTYPE_ETHERNET 1 -#define DHCP_HLEN_ETHERNET 6 -#define DHCP_MSG_LEN 236 - -#define DHCPS_SERVER_PORT 67 -#define DHCPS_CLIENT_PORT 68 - -#define DHCPDISCOVER 1 -#define DHCPOFFER 2 -#define DHCPREQUEST 3 -#define DHCPDECLINE 4 -#define DHCPACK 5 -#define DHCPNAK 6 -#define DHCPRELEASE 7 - -#define DHCP_OPTION_SUBNET_MASK 1 -#define DHCP_OPTION_ROUTER 3 -#define DHCP_OPTION_DNS_SERVER 6 -#define DHCP_OPTION_REQ_IPADDR 50 -#define DHCP_OPTION_LEASE_TIME 51 -#define DHCP_OPTION_MSG_TYPE 53 -#define DHCP_OPTION_SERVER_ID 54 -#define DHCP_OPTION_INTERFACE_MTU 26 -#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31 -#define DHCP_OPTION_BROADCAST_ADDRESS 28 -#define DHCP_OPTION_REQ_LIST 55 -#define DHCP_OPTION_END 255 - -//#define USE_CLASS_B_NET 1 -#define DHCPS_DEBUG UDEBUG -#define MAX_STATION_NUM 8 - -#define DHCPS_STATE_OFFER 1 -#define DHCPS_STATE_DECLINE 2 -#define DHCPS_STATE_ACK 3 -#define DHCPS_STATE_NAK 4 -#define DHCPS_STATE_IDLE 5 -#define DHCPS_STATE_RELEASE 6 - -#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) - -#ifdef __cplusplus -extern "C" -{ -#endif - -void dhcps_set_dns (int num, const ipv4_addr_t* dns); - -void dhcps_start(struct ip_info *info); -void dhcps_stop(void); - -#ifdef __cplusplus -} -#endif - -#endif +#include From 70b89c5ac3808b0bd677f2e1acbf7e5009d6beb6 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 03:16:35 +0300 Subject: [PATCH 07/16] restyle --- cores/esp8266/LwipDhcpServer-NonOS.cpp | 19 ++++++++++-------- cores/esp8266/LwipDhcpServer.cpp | 4 +--- cores/esp8266/LwipDhcpServer.h | 20 +++++++++---------- .../examples/StaticLease/StaticLease.ino | 2 +- tests/host/common/user_interface.cpp | 4 +--- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/cores/esp8266/LwipDhcpServer-NonOS.cpp b/cores/esp8266/LwipDhcpServer-NonOS.cpp index 3a9ecb9016..84c08337c1 100644 --- a/cores/esp8266/LwipDhcpServer-NonOS.cpp +++ b/cores/esp8266/LwipDhcpServer-NonOS.cpp @@ -31,8 +31,9 @@ // (since the netif object never goes away, even when AP is disabled) // Initial version fully emulates nonos-sdk api in DhcpServer class, // before trying to further change it and possibly break legacy behaviour -DhcpServer& dhcpSoftAP() { - extern netif netif_git[2]; +DhcpServer& dhcpSoftAP() +{ + extern netif netif_git[2]; static DhcpServer server(&netif_git[SOFTAP_IF]); return server; } @@ -41,10 +42,11 @@ extern "C" { // `ip_info` is useless, since we get the information from the netif directly // `netif` would be netif_git[SOFTAP_IF], which we get from the lwip2 glue - void dhcps_start(ip_info *, netif *) + void dhcps_start(ip_info*, netif*) { auto& server = dhcpSoftAP(); - if (!server.isRunning()) { + if (!server.isRunning()) + { server.begin(); } } @@ -52,20 +54,21 @@ extern "C" void dhcps_stop() { auto& server = dhcpSoftAP(); - if (server.isRunning()) { + if (server.isRunning()) + { server.end(); } } // providing the rest of the nonos-sdk API, which was originally removed in 3.0.0 - bool wifi_softap_set_dhcps_lease(dhcps_lease *please) + bool wifi_softap_set_dhcps_lease(dhcps_lease* please) { auto& server = dhcpSoftAP(); return server.set_dhcps_lease(please); } - bool wifi_softap_get_dhcps_lease(dhcps_lease *please) + bool wifi_softap_get_dhcps_lease(dhcps_lease* please) { auto& server = dhcpSoftAP(); return server.get_dhcps_lease(please); @@ -89,7 +92,7 @@ extern "C" return server.reset_dhcps_lease_time(); } - bool wifi_softap_add_dhcps_lease(uint8 *macaddr) + bool wifi_softap_add_dhcps_lease(uint8* macaddr) { auto& server = dhcpSoftAP(); return server.add_dhcps_lease(macaddr); diff --git a/cores/esp8266/LwipDhcpServer.cpp b/cores/esp8266/LwipDhcpServer.cpp index 9e31152da3..ed27d460b6 100644 --- a/cores/esp8266/LwipDhcpServer.cpp +++ b/cores/esp8266/LwipDhcpServer.cpp @@ -177,9 +177,7 @@ const uint32 DhcpServer::magic_cookie = 0x63538263; // https://tools.ietf.org/h //////////////////////////////////////////////////////////////////////////////////// -DhcpServer::DhcpServer(netif* netif) : _netif(netif) -{ -} +DhcpServer::DhcpServer(netif* netif) : _netif(netif) { } // wifi_softap_set_station_info is missing in user_interface.h: extern "C" void wifi_softap_set_station_info(uint8_t* mac, struct ipv4_addr*); diff --git a/cores/esp8266/LwipDhcpServer.h b/cores/esp8266/LwipDhcpServer.h index 098c53a191..71b8fb0403 100644 --- a/cores/esp8266/LwipDhcpServer.h +++ b/cores/esp8266/LwipDhcpServer.h @@ -35,11 +35,11 @@ class DhcpServer { public: - static constexpr int DefaultLeaseTime = 120 /* seconds */; - static constexpr ip_info DefaultIpConfig = { - .ip { 0x0104a8c0 }, // 192.168.4.1 - .netmask { 0x00ffffff }, // 255.255.255.0 - .gw { 0 } // 0.0.0.0 + static constexpr int DefaultLeaseTime = 120 /* seconds */; + static constexpr ip_info DefaultIpConfig = { + .ip { 0x0104a8c0 }, // 192.168.4.1 + .netmask { 0x00ffffff }, // 255.255.255.0 + .gw { 0 } // 0.0.0.0 }; static const uint32 magic_cookie; @@ -102,11 +102,11 @@ class DhcpServer netif* _netif = nullptr; udp_pcb* pcb_dhcps = nullptr; - ip_addr_t broadcast_dhcps{}; - ipv4_addr server_address{}; - ipv4_addr client_address{}; - ipv4_addr dns_address{}; - uint32 dhcps_lease_time = DefaultLeaseTime; + ip_addr_t broadcast_dhcps {}; + ipv4_addr server_address {}; + ipv4_addr client_address {}; + ipv4_addr dns_address {}; + uint32 dhcps_lease_time = DefaultLeaseTime; struct dhcps_lease dhcps_lease; list_node* plist; diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index 5cab54bf85..0b90b852ff 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -76,7 +76,7 @@ void setup() { ... any client not listed will use next IP address available from the range (here 192.168.0.102 and more) */ - auto& dhcpServer = dhcpSoftAP(); + auto &dhcpServer = dhcpSoftAP(); dhcpServer.add_dhcps_lease(mac_CAM); // always 192.168.0.100 dhcpServer.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. */ diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 32890cbfc4..3e9b521318 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -69,9 +69,7 @@ bool DhcpServer::begin() return false; } -DhcpServer::DhcpServer(netif*) -{ -} +DhcpServer::DhcpServer(netif*) { } DhcpServer::~DhcpServer() { From ea92dedf7e777dbd1a1d5d1443810adcc6855a09 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 04:31:11 +0300 Subject: [PATCH 08/16] not a c header --- libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino | 1 - tools/sdk/include/user_interface.h | 5 ++++- tools/sdk/lwip2/include/dhcpserver.h | 4 ---- 3 files changed, 4 insertions(+), 6 deletions(-) delete mode 100644 tools/sdk/lwip2/include/dhcpserver.h diff --git a/libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino b/libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino index f5e93eeb4b..baa499721e 100644 --- a/libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino +++ b/libraries/lwIP_PPP/examples/PPPServer/PPPServer.ino @@ -19,7 +19,6 @@ #include #include #include -#include #include #include diff --git a/tools/sdk/include/user_interface.h b/tools/sdk/include/user_interface.h index 2fe43994d1..f89d172113 100644 --- a/tools/sdk/include/user_interface.h +++ b/tools/sdk/include/user_interface.h @@ -382,7 +382,10 @@ void wifi_softap_free_station_info(void); bool wifi_softap_dhcps_start(void); bool wifi_softap_dhcps_stop(void); -// these dhcp functions were moved to LwipDhcpServer-NonOS.cpp, and are no longer provided by the lwip lib +// esp8266/Arduino notice: +// these dhcp functions are no longer provided by the lwip lib +// only way to include them is to build our NonOS LwipDhcpServer helpers +// (ref. cores/esp8266/LwipDhcpServer-NonOS.cpp) bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please); bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please); diff --git a/tools/sdk/lwip2/include/dhcpserver.h b/tools/sdk/lwip2/include/dhcpserver.h deleted file mode 100644 index 4f00006162..0000000000 --- a/tools/sdk/lwip2/include/dhcpserver.h +++ /dev/null @@ -1,4 +0,0 @@ -// adapted from dhcpserver.c distributed in esp8266 sdk 2.0.0 -// same license may apply - -#include From cc5fc18da5fbbffbabcd380892b372666740d6b8 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Apr 2022 18:23:13 +0300 Subject: [PATCH 09/16] no need to init --- cores/esp8266/LwipDhcpServer.cpp | 6 ++---- cores/esp8266/LwipDhcpServer.h | 10 ++-------- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 7 ++++--- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/cores/esp8266/LwipDhcpServer.cpp b/cores/esp8266/LwipDhcpServer.cpp index ed27d460b6..f5a85f24f7 100644 --- a/cores/esp8266/LwipDhcpServer.cpp +++ b/cores/esp8266/LwipDhcpServer.cpp @@ -173,8 +173,6 @@ const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; #define LWIP_IS_OK(what, err) ((err) == ERR_OK) #endif -const uint32 DhcpServer::magic_cookie = 0x63538263; // https://tools.ietf.org/html/rfc1497 - //////////////////////////////////////////////////////////////////////////////////// DhcpServer::DhcpServer(netif* netif) : _netif(netif) { } @@ -489,7 +487,7 @@ void DhcpServer::create_msg(struct dhcps_msg* m) memset((char*)m->sname, 0, sizeof(m->sname)); memset((char*)m->file, 0, sizeof(m->file)); memset((char*)m->options, 0, sizeof(m->options)); - memcpy((char*)m->options, &magic_cookie, sizeof(magic_cookie)); + memcpy((char*)m->options, &MagicCookie, sizeof(MagicCookie)); } /////////////////////////////////////////////////////////////////////////////////// /* @@ -794,7 +792,7 @@ uint8_t DhcpServer::parse_options(uint8_t* optptr, sint16_t len) /////////////////////////////////////////////////////////////////////////////////// sint16_t DhcpServer::parse_msg(struct dhcps_msg* m, u16_t len) { - if (memcmp((char*)m->options, &magic_cookie, sizeof(magic_cookie)) == 0) + if (memcmp((char*)m->options, &MagicCookie, sizeof(MagicCookie)) == 0) { struct ipv4_addr ip; memcpy(&ip.addr, m->ciaddr, sizeof(ip.addr)); diff --git a/cores/esp8266/LwipDhcpServer.h b/cores/esp8266/LwipDhcpServer.h index 71b8fb0403..b350f0e66b 100644 --- a/cores/esp8266/LwipDhcpServer.h +++ b/cores/esp8266/LwipDhcpServer.h @@ -35,14 +35,8 @@ class DhcpServer { public: - static constexpr int DefaultLeaseTime = 120 /* seconds */; - static constexpr ip_info DefaultIpConfig = { - .ip { 0x0104a8c0 }, // 192.168.4.1 - .netmask { 0x00ffffff }, // 255.255.255.0 - .gw { 0 } // 0.0.0.0 - }; - - static const uint32 magic_cookie; + static constexpr int DefaultLeaseTime = 120; // seconds + static constexpr uint32 MagicCookie = 0x63538263; // https://tools.ietf.org/html/rfc1497 DhcpServer(netif*); ~DhcpServer(); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 9fa78370a3..28fe7b1b5c 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -174,10 +174,11 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, struct ip_info ip; if(wifi_get_ip_info(SOFTAP_IF, &ip)) { if(ip.ip.addr == 0x00000000) { - // Invalid config DEBUG_WIFI("[AP] IP config Invalid resetting...\n"); - //192.168.4.1 , 192.168.4.1 , 255.255.255.0 - ret = softAPConfig(0x0104A8C0, 0x0104A8C0, 0x00FFFFFF); + ret = softAPConfig( + 0x0104A8C0 /* 192.168.4.1 */, + 0x0104A8C0 /* 192.168.4.1 */, + 0x00FFFFFF /* 255.255.255.0 */); if(!ret) { DEBUG_WIFI("[AP] softAPConfig failed!\n"); ret = false; From 955c19a5b8ec1199094de8567b33e083e1a09d37 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 8 May 2022 16:40:24 +0300 Subject: [PATCH 10/16] move dhcp server getter to WiFi more... arduino'ish? we ahve object as namespace, plus everything else related to softAP is there redundant includes, redundant mock impl (out-of-scope here to fix) --- cores/esp8266/LwipDhcpServer-NonOS.h | 28 ------------ .../RangeExtender-NAPT/RangeExtender-NAPT.ino | 3 +- .../examples/StaticLease/StaticLease.ino | 3 +- .../src/ESP8266WiFiAP-DhcpServer.cpp | 43 ++++++++++--------- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 6 +-- libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 8 ++-- tests/host/common/user_interface.cpp | 6 --- tools/sdk/include/user_interface.h | 2 +- 8 files changed, 32 insertions(+), 67 deletions(-) delete mode 100644 cores/esp8266/LwipDhcpServer-NonOS.h rename cores/esp8266/LwipDhcpServer-NonOS.cpp => libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp (77%) diff --git a/cores/esp8266/LwipDhcpServer-NonOS.h b/cores/esp8266/LwipDhcpServer-NonOS.h deleted file mode 100644 index 1e56c9a357..0000000000 --- a/cores/esp8266/LwipDhcpServer-NonOS.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - lwIPDhcpServer-NonOS - DHCP server wrapper - - Copyright (c) 2020 esp8266 arduino. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#pragma once - -#include "LwipDhcpServer.h" - -// We create a SoftAP server by default, it should be operational at boot -// (b/c the SDK will attempt to create it even before we fall into our boot code) -DhcpServer& dhcpSoftAP(); diff --git a/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino b/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino index 0f73032285..3c9caee2fc 100644 --- a/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino +++ b/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino @@ -13,7 +13,6 @@ #include #include #include -#include #define NAPT 1000 #define NAPT_PORT 10 @@ -54,7 +53,7 @@ void setup() { Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str()); // give DNS servers to AP side - auto& server = dhcpSoftAP(); + auto& server = WiFi.softAPDhcpServer(); server.dhcps_set_dns(0, WiFi.dnsIP(0)); server.dhcps_set_dns(1, WiFi.dnsIP(1)); diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index 0b90b852ff..d4ed0cd375 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -4,7 +4,6 @@ #include #include #include -#include /* Set these to your desired credentials. */ const char *ssid = "ESPap"; @@ -76,7 +75,7 @@ void setup() { ... any client not listed will use next IP address available from the range (here 192.168.0.102 and more) */ - auto &dhcpServer = dhcpSoftAP(); + auto &dhcpServer = WiFi.softAPDhcpServer(); dhcpServer.add_dhcps_lease(mac_CAM); // always 192.168.0.100 dhcpServer.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. */ diff --git a/cores/esp8266/LwipDhcpServer-NonOS.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp similarity index 77% rename from cores/esp8266/LwipDhcpServer-NonOS.cpp rename to libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp index 84c08337c1..2cb99c14cf 100644 --- a/cores/esp8266/LwipDhcpServer-NonOS.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp @@ -1,59 +1,58 @@ /* - lwIPDhcpServer-NonOS - DHCP server wrapper + NonOS DHCP server helpers - Copyright (c) 2020 esp8266 arduino. All rights reserved. + Copyright (c) 2020-2022 esp8266 arduino. All rights reserved. This file is part of the esp8266 core for Arduino environment. - This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -// STARTS/STOPS DHCP SERVER ON WIFI AP INTERFACE -// these functions must exists as-is with "C" interface, -// nonos-sdk calls them at boot time and later - -#include "LwipDhcpServer-NonOS.h" +#include "ESP8266WiFi.h" +#include "ESP8266WiFiAP.h" +#include #include // Global static DHCP instance for softAP interface // (since the netif object never goes away, even when AP is disabled) // Initial version fully emulates nonos-sdk api in DhcpServer class, // before trying to further change it and possibly break legacy behaviour -DhcpServer& dhcpSoftAP() +static DhcpServer& getNonOSDhcpServer() { extern netif netif_git[2]; static DhcpServer server(&netif_git[SOFTAP_IF]); return server; } +DhcpServer& ESP8266WiFiAPClass::softAPDhcpServer() +{ + return getNonOSDhcpServer(); +} + extern "C" { // `ip_info` is useless, since we get the information from the netif directly // `netif` would be netif_git[SOFTAP_IF], which we get from the lwip2 glue void dhcps_start(ip_info*, netif*) { - auto& server = dhcpSoftAP(); + auto& server = getNonOSDhcpServer(); if (!server.isRunning()) { server.begin(); } } - void dhcps_stop() - { - auto& server = dhcpSoftAP(); + void dhcps_stop() { + auto& server = getNonOSDhcpServer(); if (server.isRunning()) { server.end(); @@ -64,38 +63,40 @@ extern "C" bool wifi_softap_set_dhcps_lease(dhcps_lease* please) { - auto& server = dhcpSoftAP(); + auto& server = getNonOSDhcpServer(); return server.set_dhcps_lease(please); } bool wifi_softap_get_dhcps_lease(dhcps_lease* please) { - auto& server = dhcpSoftAP(); + auto& server = getNonOSDhcpServer(); return server.get_dhcps_lease(please); } uint32 wifi_softap_get_dhcps_lease_time() { - auto& server = dhcpSoftAP(); + auto& server = getNonOSDhcpServer(); return server.get_dhcps_lease_time(); } bool wifi_softap_set_dhcps_lease_time(uint32 minutes) { - auto& server = dhcpSoftAP(); + auto& server = getNonOSDhcpServer(); return server.set_dhcps_lease_time(minutes); } bool wifi_softap_reset_dhcps_lease_time() { - auto& server = dhcpSoftAP(); + auto& server = getNonOSDhcpServer(); return server.reset_dhcps_lease_time(); } bool wifi_softap_add_dhcps_lease(uint8* macaddr) { - auto& server = dhcpSoftAP(); + auto& server = getNonOSDhcpServer(); return server.add_dhcps_lease(macaddr); } } // extern "C" + + diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 28fe7b1b5c..bc7ee3e24f 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -38,8 +38,6 @@ extern "C" { #include "debug.h" -#include - // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ // ----------------------------------------------------------------------------------------------------------------------- @@ -167,7 +165,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, DEBUG_WIFI("[AP] softap config unchanged\n"); } - auto& server = dhcpSoftAP(); + auto& server = softAPDhcpServer(); server.end(); // check IP config @@ -247,7 +245,7 @@ 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 = dhcpSoftAP(); + auto& server = softAPDhcpServer(); if(!server.set_dhcps_lease(&dhcp_lease)) { DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index 7ed23d563b..73b3218c32 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -27,7 +27,7 @@ #include "ESP8266WiFiType.h" #include "ESP8266WiFiGeneric.h" -#include +#include class ESP8266WiFiAPClass { @@ -49,8 +49,10 @@ class ESP8266WiFiAPClass { uint8_t* softAPmacAddress(uint8_t* mac); String softAPmacAddress(void); - String softAPSSID() const; - String softAPPSK() const; + String softAPSSID() const; + String softAPPSK() const; + + static DhcpServer& softAPDhcpServer(); protected: diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 3e9b521318..cc44355535 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -76,12 +76,6 @@ DhcpServer::~DhcpServer() end(); } -DhcpServer& dhcpSoftAP() -{ - static DhcpServer server(nullptr); - return server; -} - extern "C" { #include diff --git a/tools/sdk/include/user_interface.h b/tools/sdk/include/user_interface.h index f89d172113..696583d2ba 100644 --- a/tools/sdk/include/user_interface.h +++ b/tools/sdk/include/user_interface.h @@ -385,7 +385,7 @@ bool wifi_softap_dhcps_stop(void); // esp8266/Arduino notice: // these dhcp functions are no longer provided by the lwip lib // only way to include them is to build our NonOS LwipDhcpServer helpers -// (ref. cores/esp8266/LwipDhcpServer-NonOS.cpp) +// (ref. libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp) bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please); bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please); From 8a76b20ccb52fe8b01676daec7ddc1702308e7e8 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 19 May 2022 07:07:08 +0300 Subject: [PATCH 11/16] ...move things back, still expose as WiFi method --- .../esp8266/LwipDhcpServer-NonOS.cpp | 12 ++-------- cores/esp8266/LwipDhcpServer-NonOS.h | 24 +++++++++++++++++++ libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 8 +++++++ 3 files changed, 34 insertions(+), 10 deletions(-) rename libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp => cores/esp8266/LwipDhcpServer-NonOS.cpp (94%) create mode 100644 cores/esp8266/LwipDhcpServer-NonOS.h diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp b/cores/esp8266/LwipDhcpServer-NonOS.cpp similarity index 94% rename from libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp rename to cores/esp8266/LwipDhcpServer-NonOS.cpp index 2cb99c14cf..7604988be2 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp +++ b/cores/esp8266/LwipDhcpServer-NonOS.cpp @@ -16,8 +16,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "ESP8266WiFi.h" -#include "ESP8266WiFiAP.h" +#include "LwipDhcpServer-NonOS.h" #include #include @@ -26,18 +25,13 @@ // (since the netif object never goes away, even when AP is disabled) // Initial version fully emulates nonos-sdk api in DhcpServer class, // before trying to further change it and possibly break legacy behaviour -static DhcpServer& getNonOSDhcpServer() +DhcpServer& getNonOSDhcpServer() { extern netif netif_git[2]; static DhcpServer server(&netif_git[SOFTAP_IF]); return server; } -DhcpServer& ESP8266WiFiAPClass::softAPDhcpServer() -{ - return getNonOSDhcpServer(); -} - extern "C" { // `ip_info` is useless, since we get the information from the netif directly @@ -98,5 +92,3 @@ extern "C" } } // extern "C" - - diff --git a/cores/esp8266/LwipDhcpServer-NonOS.h b/cores/esp8266/LwipDhcpServer-NonOS.h new file mode 100644 index 0000000000..4da4eca1b3 --- /dev/null +++ b/cores/esp8266/LwipDhcpServer-NonOS.h @@ -0,0 +1,24 @@ +/* + NonOS DHCP server helpers + + Copyright (c) 2020-2022 esp8266 arduino. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include "LwipDhcpServer.h" + +// Global static DHCP instance for softAP interface +DhcpServer& getNonOSDhcpServer(); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index bc7ee3e24f..ea5562bc91 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -391,3 +391,11 @@ String ESP8266WiFiAPClass::softAPPSK() const { return psk; } + +/** + * Get the static DHCP server instance attached to the softAP interface + * @return DhcpServer instance. + */ +DhcpServer& ESP8266WiFiAPClass::softAPDhcpServer() { + return getNonOSDhcpServer(); +} From a69d87a976748c5a2a3cd9f0a57bc1a43b2a78d1 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 19 May 2022 07:12:35 +0300 Subject: [PATCH 12/16] review fix --- cores/esp8266/LwipDhcpServer.h | 2 +- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/cores/esp8266/LwipDhcpServer.h b/cores/esp8266/LwipDhcpServer.h index b350f0e66b..3ec9263342 100644 --- a/cores/esp8266/LwipDhcpServer.h +++ b/cores/esp8266/LwipDhcpServer.h @@ -35,7 +35,7 @@ class DhcpServer { public: - static constexpr int DefaultLeaseTime = 120; // seconds + static constexpr int DefaultLeaseTime = 720; // minutes static constexpr uint32 MagicCookie = 0x63538263; // https://tools.ietf.org/html/rfc1497 DhcpServer(netif*); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index ea5562bc91..3eaff0ff70 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -252,13 +252,6 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA ret = false; } - // set lease time to 720min --> 12h - if(!server.set_dhcps_lease_time(720)) - { - DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n"); - ret = false; - } - uint8 mode = info.gw.addr ? 1 : 0; if(!server.set_dhcps_offer_option(OFFER_ROUTER, &mode)) { From bfd67da30316c3ec075fc70873c6c79c3351c1a5 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 19 May 2022 07:23:52 +0300 Subject: [PATCH 13/16] include -nonos header in wifi lib though --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 3eaff0ff70..ffd536a0ba 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -26,6 +26,8 @@ #include "ESP8266WiFiGeneric.h" #include "ESP8266WiFiAP.h" +#include + extern "C" { #include "c_types.h" #include "ets_sys.h" From 91ad893c09a3762daebd8546ba09d8e8d53ce882 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 19 May 2022 07:43:13 +0300 Subject: [PATCH 14/16] no more lwip include --- .../DNSServer/examples/NAPTCaptivePortal/NAPTCaptivePortal.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/DNSServer/examples/NAPTCaptivePortal/NAPTCaptivePortal.ino b/libraries/DNSServer/examples/NAPTCaptivePortal/NAPTCaptivePortal.ino index cb65f518ef..1fa46291bb 100644 --- a/libraries/DNSServer/examples/NAPTCaptivePortal/NAPTCaptivePortal.ino +++ b/libraries/DNSServer/examples/NAPTCaptivePortal/NAPTCaptivePortal.ino @@ -74,7 +74,6 @@ #include #include #include -#include #include #include #include From efae1be8aec23f5abf02903c397bce3741e9047c Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 1 Jun 2022 12:43:45 +0300 Subject: [PATCH 15/16] style --- cores/esp8266/LwipDhcpServer-NonOS.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/LwipDhcpServer-NonOS.cpp b/cores/esp8266/LwipDhcpServer-NonOS.cpp index 7604988be2..0a4d1298cd 100644 --- a/cores/esp8266/LwipDhcpServer-NonOS.cpp +++ b/cores/esp8266/LwipDhcpServer-NonOS.cpp @@ -45,7 +45,8 @@ extern "C" } } - void dhcps_stop() { + void dhcps_stop() + { auto& server = getNonOSDhcpServer(); if (server.isRunning()) { From f5a417ae2e7220161a37b0d95c97a38805c9ea82 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 1 Jun 2022 12:44:34 +0300 Subject: [PATCH 16/16] need mock dhcpserver instance --- tests/host/Makefile | 1 + tests/host/common/DhcpServer.cpp | 80 ++++++++++++++++++++++++++++ tests/host/common/user_interface.cpp | 69 ------------------------ 3 files changed, 81 insertions(+), 69 deletions(-) create mode 100644 tests/host/common/DhcpServer.cpp diff --git a/tests/host/Makefile b/tests/host/Makefile index 82222ad299..f819047891 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -143,6 +143,7 @@ MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) \ ArduinoMainUdp.cpp \ ArduinoMainSpiffs.cpp \ ArduinoMainLittlefs.cpp \ + DhcpServer.cpp \ user_interface.cpp \ ) diff --git a/tests/host/common/DhcpServer.cpp b/tests/host/common/DhcpServer.cpp new file mode 100644 index 0000000000..7e88ea88c4 --- /dev/null +++ b/tests/host/common/DhcpServer.cpp @@ -0,0 +1,80 @@ +#include +#include + +DhcpServer& getNonOSDhcpServer() +{ + static DhcpServer instance(nullptr); + return instance; +} + +bool DhcpServer::set_dhcps_lease(struct dhcps_lease* please) +{ + (void)please; + return false; +} + +bool DhcpServer::set_dhcps_lease_time(uint32 minute) +{ + (void)minute; + return false; +} + +bool DhcpServer::set_dhcps_offer_option(uint8 level, void* optarg) +{ + (void)level; + (void)optarg; + return false; +} + +void DhcpServer::end() { } + +bool DhcpServer::begin() +{ + return false; +} + +DhcpServer::DhcpServer(netif*) { } + +DhcpServer::~DhcpServer() +{ + end(); +} + +extern "C" +{ +#include + + bool wifi_softap_dhcps_start(void) + { + return true; + } + + enum dhcp_status wifi_softap_dhcps_status(void) + { + return DHCP_STARTED; + } + + bool wifi_softap_dhcps_stop(void) + { + return true; + } + + bool wifi_softap_set_dhcps_lease(struct dhcps_lease* please) + { + (void)please; + return true; + } + + bool wifi_softap_set_dhcps_lease_time(uint32 minute) + { + (void)minute; + return true; + } + + bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg) + { + (void)level; + (void)optarg; + return true; + } +} diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 4e9c261b52..c35a7cc236 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -41,41 +41,6 @@ #include "MocklwIP.h" -#include - -bool DhcpServer::set_dhcps_lease(struct dhcps_lease* please) -{ - (void)please; - return false; -} - -bool DhcpServer::set_dhcps_lease_time(uint32 minute) -{ - (void)minute; - return false; -} - -bool DhcpServer::set_dhcps_offer_option(uint8 level, void* optarg) -{ - (void)level; - (void)optarg; - return false; -} - -void DhcpServer::end() { } - -bool DhcpServer::begin() -{ - return false; -} - -DhcpServer::DhcpServer(netif*) { } - -DhcpServer::~DhcpServer() -{ - end(); -} - extern "C" { #include @@ -394,21 +359,6 @@ extern "C" (void)max_tpw; } - bool wifi_softap_dhcps_start(void) - { - return true; - } - - enum dhcp_status wifi_softap_dhcps_status(void) - { - return DHCP_STARTED; - } - - bool wifi_softap_dhcps_stop(void) - { - return true; - } - bool wifi_softap_get_config(struct softap_config* config) { strcpy((char*)config->ssid, "apssid"); @@ -444,25 +394,6 @@ extern "C" return true; } - bool wifi_softap_set_dhcps_lease(struct dhcps_lease* please) - { - (void)please; - return true; - } - - bool wifi_softap_set_dhcps_lease_time(uint32 minute) - { - (void)minute; - return true; - } - - bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg) - { - (void)level; - (void)optarg; - return true; - } - bool wifi_station_scan(struct scan_config* config, scan_done_cb_t cb) { (void)config;