1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

LwipIntfDev - config static IP auto gw,mask,dns as in Arduino libraries (#9040)

for 'modern' Ethernet libraries W5100lwIP, W5500lwIP and ENC28J60lwIP
used without EthernetCompat
This commit is contained in:
Juraj Andrássy 2023-12-01 10:36:35 +01:00 committed by GitHub
parent 1efe5ee3fc
commit 9e73cf0021
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -63,9 +63,14 @@ public:
memset(&_netif, 0, sizeof(_netif)); 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, boolean config(const IPAddress& local_ip, const IPAddress& arg1, const IPAddress& arg2,
const IPAddress& arg3 = IPADDR_NONE, const IPAddress& dns2 = IPADDR_NONE); 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 // default mac-address is inferred from esp8266's STA interface
boolean begin(const uint8_t* macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU); boolean begin(const uint8_t* macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU);
void end(); void end();
@ -209,6 +214,24 @@ boolean LwipIntfDev<RawDev>::config(const IPAddress& localIP, const IPAddress& g
return true; 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> template<class RawDev>
boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu) boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu)
{ {
@ -336,9 +359,6 @@ template<class RawDev>
void LwipIntfDev<RawDev>::end() void LwipIntfDev<RawDev>::end()
{ {
netif_remove(&_netif); 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; _started = false;
RawDev::end(); RawDev::end();
} }

View File

@ -86,6 +86,13 @@ public:
return ret; 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 HardwareStatus hardwareStatus() const
{ {
return _hardwareStatus; return _hardwareStatus;