Skip to content

Commit

Permalink
LwipIntfDev - config static IP auto gw,mask,dns as in Arduino libraries
Browse files Browse the repository at this point in the history
for 'modern' Ethernet libraries W5100lwIP, W5500lwIP and ENC28J60lwIP
used without EthernetCompat
  • Loading branch information
JAndrassy committed Nov 30, 2023
1 parent 9a4e178 commit 3152948
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
27 changes: 24 additions & 3 deletions cores/esp8266/LwipIntfDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ class LwipIntfDev: public LwipIntf, public RawDev
memset(&_netif, 0, sizeof(_netif));
}

//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
//to detect Arduino arg order, and handle it correctly.
boolean config(const IPAddress& local_ip, const IPAddress& arg1, const IPAddress& arg2,
const IPAddress& arg3 = IPADDR_NONE, const IPAddress& dns2 = IPADDR_NONE);

// two and one parameter version. 2nd parameter is DNS like in Arduino. IPv4 only
boolean config(IPAddress local_ip, IPAddress dns = INADDR_ANY);

// default mac-address is inferred from esp8266's STA interface
boolean begin(const uint8_t* macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU);
void end();
Expand Down Expand Up @@ -209,6 +214,25 @@ boolean LwipIntfDev<RawDev>::config(const IPAddress& localIP, const IPAddress& g
return true;
}

template<class RawDev>
boolean LwipIntfDev<RawDev>::config(IPAddress local_ip, IPAddress dns)
{

if (!local_ip.isSet())
return config(INADDR_ANY, INADDR_ANY, INADDR_ANY);

if (!local_ip.isV4())
return false;

IPAddress gw(local_ip);
gw[3] = 1;
if (!dns.isSet())
{
dns = gw;
}
return config(local_ip, gw, IPAddress(255, 255, 255, 0), dns);
}

template<class RawDev>
boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu)
{
Expand Down Expand Up @@ -336,9 +360,6 @@ template<class RawDev>
void LwipIntfDev<RawDev>::end()
{
netif_remove(&_netif);
ip_addr_copy(_netif.ip_addr, ip_addr_any); // to allow DHCP at next begin
ip_addr_copy(_netif.netmask, ip_addr_any);
ip_addr_copy(_netif.gw, ip_addr_any);
_started = false;
RawDev::end();
}
Expand Down
7 changes: 7 additions & 0 deletions libraries/lwIP_Ethernet/src/EthernetCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class ArduinoEthernet: public LwipIntfDev<RawDev>
return ret;
}

void end()
{
ip_addr_copy(LwipIntfDev<RawDev>::_netif.ip_addr,
ip_addr_any); // to allow DHCP at next begin
LwipIntfDev<RawDev>::end();
}

HardwareStatus hardwareStatus() const
{
return _hardwareStatus;
Expand Down

0 comments on commit 3152948

Please sign in to comment.