mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +03:00
Initialize SoftAP DhcpServer object on demand (#8546)
* 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. * nonos helpers have a separate header * wifi ap needs this anyway, simplify sketch includes * missing example * existing name :/ * trying to fix header dependency * restyle * not a c header * no need to init * 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) * ...move things back, still expose as WiFi method * review fix * include -nonos header in wifi lib though * no more lwip include * style * need mock dhcpserver instance
This commit is contained in:
@ -13,7 +13,6 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <lwip/napt.h>
|
||||
#include <lwip/dns.h>
|
||||
#include <LwipDhcpServer.h>
|
||||
|
||||
#define NAPT 1000
|
||||
#define NAPT_PORT 10
|
||||
@ -54,8 +53,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 = WiFi.softAPDhcpServer();
|
||||
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));
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <LwipDhcpServer.h>
|
||||
|
||||
/* Set these to your desired credentials. */
|
||||
const char *ssid = "ESPap";
|
||||
@ -76,8 +75,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 &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. */
|
||||
WiFi.softAP(ssid, password);
|
||||
Serial.print("AP IP address: ");
|
||||
|
@ -26,6 +26,8 @@
|
||||
#include "ESP8266WiFiGeneric.h"
|
||||
#include "ESP8266WiFiAP.h"
|
||||
|
||||
#include <LwipDhcpServer-NonOS.h>
|
||||
|
||||
extern "C" {
|
||||
#include "c_types.h"
|
||||
#include "ets_sys.h"
|
||||
@ -37,7 +39,6 @@ extern "C" {
|
||||
}
|
||||
|
||||
#include "debug.h"
|
||||
#include "LwipDhcpServer.h"
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ---------------------------------------------------- Private functions ------------------------------------------------
|
||||
@ -166,16 +167,18 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
|
||||
DEBUG_WIFI("[AP] softap config unchanged\n");
|
||||
}
|
||||
|
||||
dhcpSoftAP.end();
|
||||
auto& server = softAPDhcpServer();
|
||||
server.end();
|
||||
|
||||
// check IP config
|
||||
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;
|
||||
@ -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,15 @@ 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 = softAPDhcpServer();
|
||||
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))
|
||||
{
|
||||
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;
|
||||
@ -389,3 +386,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();
|
||||
}
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "ESP8266WiFiType.h"
|
||||
#include "ESP8266WiFiGeneric.h"
|
||||
|
||||
#include <LwipDhcpServer.h>
|
||||
|
||||
class ESP8266WiFiAPClass {
|
||||
|
||||
@ -48,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:
|
||||
|
||||
|
Reference in New Issue
Block a user