1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-06 05:21:22 +03:00

Add compatibility check for ::config() method with arduino arg order (#3860)

This commit is contained in:
Develo 2017-11-22 10:22:11 -03:00 committed by GitHub
parent 7de58d9671
commit 9dcc580aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -195,6 +195,14 @@ wl_status_t ESP8266WiFiSTAClass::begin() {
return status();
}
static void
swap(IPAddress &lhs, IPAddress &rhs)
{
IPAddress tmp = lhs;
lhs = rhs;
rhs = tmp;
}
/**
* Change IP configuration settings disabling the dhcp client
@ -210,6 +218,22 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres
return false;
}
//Arduino has a different arg order: ip, dns, gateway, subnet. To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order.
if(subnet[0] != 255)
{
//octet is not 255 => interpret as Arduino order
if(dns1[0] == 0)
{
//arg order is arduino and 4th arg not given => assign it arduino default
dns1 = IPAddress(255,255,255,0);
}
//current order is arduino: ip-dns-gway-subnet
swap(gateway, subnet); //after this, order is ip-gway-dns-subnet
swap(subnet, dns1); //after this, order is ip-gway-subnet-dns (correct ESP order)
}
struct ip_info info;
info.ip.addr = static_cast<uint32_t>(local_ip);
info.gw.addr = static_cast<uint32_t>(gateway);

View File

@ -39,6 +39,9 @@ class ESP8266WiFiSTAClass {
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin();
//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 reconnect();