Skip to content

Commit 9dcc580

Browse files
authored
Add compatibility check for ::config() method with arduino arg order (#3860)
1 parent 7de58d9 commit 9dcc580

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ wl_status_t ESP8266WiFiSTAClass::begin() {
195195
return status();
196196
}
197197

198+
static void
199+
swap(IPAddress &lhs, IPAddress &rhs)
200+
{
201+
IPAddress tmp = lhs;
202+
lhs = rhs;
203+
rhs = tmp;
204+
}
205+
198206

199207
/**
200208
* Change IP configuration settings disabling the dhcp client
@@ -210,6 +218,22 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres
210218
return false;
211219
}
212220

221+
//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.
222+
if(subnet[0] != 255)
223+
{
224+
//octet is not 255 => interpret as Arduino order
225+
226+
if(dns1[0] == 0)
227+
{
228+
//arg order is arduino and 4th arg not given => assign it arduino default
229+
dns1 = IPAddress(255,255,255,0);
230+
}
231+
232+
//current order is arduino: ip-dns-gway-subnet
233+
swap(gateway, subnet); //after this, order is ip-gway-dns-subnet
234+
swap(subnet, dns1); //after this, order is ip-gway-subnet-dns (correct ESP order)
235+
}
236+
213237
struct ip_info info;
214238
info.ip.addr = static_cast<uint32_t>(local_ip);
215239
info.gw.addr = static_cast<uint32_t>(gateway);

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class ESP8266WiFiSTAClass {
3939
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
4040
wl_status_t begin();
4141

42+
//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
43+
//to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
44+
//work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
4245
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
4346

4447
bool reconnect();

0 commit comments

Comments
 (0)