Skip to content

Commit

Permalink
Merge branch 'master' into intfdev_end_rebegin
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v authored Nov 12, 2023
2 parents 3594be9 + 7fc2caa commit d7a9610
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
32 changes: 32 additions & 0 deletions cores/esp8266/LwipIntfDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class LwipIntfDev: public LwipIntf, public RawDev
return &_netif;
}

uint8_t* macAddress(uint8_t* mac) // WiFi lib way
{
memcpy(mac, &_netif.hwaddr, 6);
return mac;
}
void MACAddress(uint8_t* mac) // Ethernet lib way
{
macAddress(mac);
}
IPAddress localIP() const
{
return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.ip_addr)));
Expand All @@ -87,6 +96,29 @@ class LwipIntfDev: public LwipIntf, public RawDev
{
return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.gw)));
}
IPAddress dnsIP(int n) const // WiFi lib way
{
return IPAddress(dns_getserver(n));
}
IPAddress dnsServerIP() const // Ethernet lib way
{
return dnsIP(0);
}
void setDNS(IPAddress dns1, IPAddress dns2 = INADDR_ANY) // WiFi lib way
{
if (dns1.isSet())
{
dns_setserver(0, dns1);
}
if (dns2.isSet())
{
dns_setserver(1, dns2);
}
}
void setDnsServerIP(const IPAddress dnsIP) // Ethernet lib way
{
setDNS(dnsIP);
}

// 1. Currently when no default is set, esp8266-Arduino uses the first
// DHCP client interface receiving a valid address and gateway to
Expand Down
25 changes: 25 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,29 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress a
return true;
}

/**
* Change DNS for static IP configuration
* @param dns1 Static DNS server 1
* @param dns2 Static DNS server 2 (optional)
*/
bool ESP8266WiFiSTAClass::setDNS(IPAddress dns1, IPAddress dns2) {

if((WiFi.getMode() & WIFI_STA) == 0)
return false;

if(dns1.isSet()) {
// Set DNS1-Server
dns_setserver(0, dns1);
}

if(dns2.isSet()) {
// Set DNS2-Server
dns_setserver(1, dns2);
}

return true;
}

/**
* will force a disconnect an then start reconnecting to AP
* @return ok
Expand Down Expand Up @@ -400,6 +423,8 @@ bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseCredentials) {
WiFi.enableSTA(false);
}

_useStaticIp = false;

return ret;
}

Expand Down
3 changes: 2 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class ESP8266WiFiSTAClass: public LwipIntf {
//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. Be aware that the Arduino default value handling doesn't
//work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = INADDR_ANY, IPAddress dns2 = INADDR_ANY);
bool setDNS(IPAddress dns1, IPAddress dns2 = INADDR_ANY);

bool reconnect();

Expand Down
25 changes: 22 additions & 3 deletions libraries/lwIP_Ethernet/src/EthernetCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,29 @@ class ArduinoEthernet: public LwipIntfDev<RawDev>
// Arduino-Ethernet API compatibility, order can be either:
// mac, ip, gateway, netmask, dns (esp8266 or natural order)
// mac, ip, dns, gateway, netmask (Arduino legacy)
boolean begin(const uint8_t* macAddress, const IPAddress& local_ip = IPADDR_NONE,
const IPAddress& arg1 = IPADDR_NONE, const IPAddress& arg2 = IPADDR_NONE,
const IPAddress& arg3 = IPADDR_NONE)
boolean begin(const uint8_t* macAddress, IPAddress local_ip = IPADDR_NONE,
IPAddress arg1 = IPADDR_NONE, IPAddress arg2 = IPADDR_NONE,
IPAddress arg3 = IPADDR_NONE)
{
if (local_ip.isSet() && local_ip.isV4())
{
// setting auto values using arduino ordering of parameters
if (arg1 == IPADDR_NONE) // else dns or gw
{
arg1 = local_ip;
arg1[3] = 1;
}
if (arg2 == IPADDR_NONE) // else gw or mask
{
arg2 = local_ip;
arg2[3] = 1;
}
// if arg2 is mask (esp ordering), let DNS IP unconfigured
if (arg3 == IPADDR_NONE && arg2[0] != 255) // else mask or dns
{
arg3 = IPAddress(255, 255, 255, 0);
}
}
SPI4EthInit(); // Arduino Ethernet self-initializes SPI
bool ret = true;
if (local_ip.isSet())
Expand Down

0 comments on commit d7a9610

Please sign in to comment.