Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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