Skip to content

Commit f2e8a72

Browse files
committed
LwipIntfDev - config static IP auto gw,mask,dns as in Arduino libraries
for 'modern' Ethernet libraries W5100lwIP, W5500lwIP and ENC28J60lwIP used without EthernetCompat
1 parent 9a4e178 commit f2e8a72

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

cores/esp8266/LwipIntfDev.h

+23-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ class LwipIntfDev: public LwipIntf, public RawDev
6363
memset(&_netif, 0, sizeof(_netif));
6464
}
6565

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

71+
// two and one parameter version. 2nd parameter is DNS like in Arduino. IPv4 only
72+
boolean config(IPAddress local_ip, IPAddress dns = INADDR_ANY);
73+
6974
// default mac-address is inferred from esp8266's STA interface
7075
boolean begin(const uint8_t* macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU);
7176
void end();
@@ -209,6 +214,24 @@ boolean LwipIntfDev<RawDev>::config(const IPAddress& localIP, const IPAddress& g
209214
return true;
210215
}
211216

217+
template<class RawDev>
218+
boolean LwipIntfDev<RawDev>::config(IPAddress local_ip, IPAddress dns)
219+
{
220+
if (!local_ip.isSet())
221+
return config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
222+
223+
if (!local_ip.isV4())
224+
return false;
225+
226+
IPAddress gw(local_ip);
227+
gw[3] = 1;
228+
if (!dns.isSet())
229+
{
230+
dns = gw;
231+
}
232+
return config(local_ip, gw, IPAddress(255, 255, 255, 0), dns);
233+
}
234+
212235
template<class RawDev>
213236
boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu)
214237
{
@@ -336,9 +359,6 @@ template<class RawDev>
336359
void LwipIntfDev<RawDev>::end()
337360
{
338361
netif_remove(&_netif);
339-
ip_addr_copy(_netif.ip_addr, ip_addr_any); // to allow DHCP at next begin
340-
ip_addr_copy(_netif.netmask, ip_addr_any);
341-
ip_addr_copy(_netif.gw, ip_addr_any);
342362
_started = false;
343363
RawDev::end();
344364
}

libraries/lwIP_Ethernet/src/EthernetCompat.h

+7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ class ArduinoEthernet: public LwipIntfDev<RawDev>
8686
return ret;
8787
}
8888

89+
void end()
90+
{
91+
ip_addr_copy(LwipIntfDev<RawDev>::_netif.ip_addr,
92+
ip_addr_any); // to allow DHCP at next begin
93+
LwipIntfDev<RawDev>::end();
94+
}
95+
8996
HardwareStatus hardwareStatus() const
9097
{
9198
return _hardwareStatus;

0 commit comments

Comments
 (0)