Skip to content

Commit

Permalink
Add compatibility check for ::config() method with arduino arg order (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
devyte authored Nov 22, 2017
1 parent 7de58d9 commit 9dcc580
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9dcc580

Please sign in to comment.