8000 Initialize SoftAP DhcpServer object on demand by mcspr · Pull Request #8546 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Initialize SoftAP DhcpServer object on demand #8546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
mcspr committed Apr 19, 2022
commit 2a46b8fac65d38b5b4effdf25816e892ffe729df
87 changes: 61 additions & 26 deletions cores/esp8266/LwipDhcpServer-NonOS.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,41 +23,76 @@
// these functions must exists as-is with "C" interface,
// nonos-sdk calls them at boot time and later

#include <lwip/init.h> // LWIP_VERSION
#include "LwipDhcpServer-NonOS.h"

#include <lwip/netif.h>
#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"
49 changes: 10 additions & 39 deletions cores/esp8266/LwipDhcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

#include <lwip/init.h> // LWIP_VERSION

#define DHCPS_LEASE_TIME_DEF (120)

#define USE_DNS

#include "lwip/inet.h"
Expand Down Expand Up @@ -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; \
Expand All @@ -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
8000 // 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*);
Expand Down Expand Up @@ -1008,17 +983,19 @@ void DhcpServer::init_dhcps_lease(uint32 ip)
}
///////////////////////////////////////////////////////////////////////////////////

bool DhcpServer::begin(struct ip_info* info)
bool DhcpServer::begin()
{
if (pcb_dhcps != nullptr)
{
udp_remove(pcb_dhcps);
}

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;
}

Expand All @@ -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); 6D40
Expand All @@ -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;
}

Expand Down Expand Up @@ -1091,9 +1062,9 @@ void DhcpServer::end()
}
}

bool DhcpServer::isRunning()
bool DhcpServer::isRunning() const
{
return !!_netif->state;
return pcb_dhcps != nullptr;
}

/******************************************************************************
Expand Down Expand Up @@ -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;
}

Expand Down
35 changes: 19 additions & 16 deletions cores/esp8266/LwipDhcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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__
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
16 changes: 10 additions & 6 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ extern "C" {
}

#include "debug.h"
#include "LwipDhcpServer.h"

#include <LwipDhcpServer.h>
#include <LwipDhcpServer-NonOS.h>

// -----------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------- Private functions ------------------------------------------------
Expand Down Expand Up @@ -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;
Expand All @@ -186,7 +189,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
ret = false;
}

dhcpSoftAP.begin(&ip);
server.begin();

return ret;
}
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions tests/host/common/user_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,25 @@ 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()
{
end();
}

DhcpServer dhcpSoftAP(nullptr);
DhcpServer& dhcpSoftAP()
{
static DhcpServer server(nullptr);
return server;
}

extern "C"
{
Expand Down
Loading
0