From 25f02378321808eaf8c8fecff2d6b066eebfee3e Mon Sep 17 00:00:00 2001 From: ruggi99 Date: Mon, 7 Jan 2019 15:47:08 +0100 Subject: [PATCH 1/6] Update ESP8266WiFiGeneric.cpp --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 75 +++++++++++-------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index a6239decef..51056d54f4 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -510,55 +510,68 @@ bool ESP8266WiFiGenericClass::isSleepLevelMax () { // ------------------------------------------------ Generic Network function --------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- -void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *callback_arg); +void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg); -static bool _dns_lookup_pending = false; +struct arg_callback { + IPAddress addr; + bool called = false; +}; /** * Resolve the given hostname to an IP address. * @param aHostname Name to be resolved * @param aResult IPAddress structure to store the returned IP address - * @return 1 if aIPAddrString was successfully converted to an IP address, - * else 0 + * @return 1 if a String was successfully converted to an IP address, + * else error code */ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) { return hostByName(aHostname, aResult, 10000); } - int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms) { ip_addr_t addr; - aResult = static_cast(0); + static struct arg_callback addr2; + static bool pending = false; // If waiting for the callback function call if(aResult.fromString(aHostname)) { - // Host name is a IP address use it! - DEBUG_WIFI_GENERIC("[hostByName] Host: %s is a IP!\n", aHostname); + // Host name is a IP address, use it! return 1; } - - DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname); - err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); - if(err == ERR_OK) { - aResult = IPAddress(&addr); - } else if(err == ERR_INPROGRESS) { - _dns_lookup_pending = true; - delay(timeout_ms); - _dns_lookup_pending = false; - // will return here when dns_found_callback fires - if(aResult.isSet()) { - err = ERR_OK; + if(addr2.called) { // If the callback function has been called + addr2.called = false; + pending = false; + if(addr2.addr != IPADDR_ANY) { // If IP found + DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP found %s\n", aHostname, addr2.addr.toString().c_str()); + aResult = addr2.addr; + addr2.addr = (uint32_t)IPADDR_ANY; + return 1; + } else { + DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP NOT found. Please re-try\n", aHostname); + return 0; } } - - if(err != 0) { - DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err); + if(pending) { + DEBUG_WIFI_GENERIC("[hostByName] Host %s: Waiting for callback call\n", aHostname); + return 0; + } + err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &addr2); + DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP requested\n", aHostname); + if(err == ERR_OK) { + aResult = addr.addr; + pending = false; + addr2.called = false; + DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP found %s\n", aHostname, aResult.toString().c_str()); + return 1; + } + if(err == ERR_INPROGRESS) { + DEBUG_WIFI_GENERIC("[hostByName] Host %s: Query registered\n", aHostname); + pending = true; } else { - DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str()); + DEBUG_WIFI_GENERIC("[hostByName] Host %s: Lookup error: %d!\n", aHostname, err); } - - return (err == ERR_OK) ? 1 : 0; + return 0; } /** @@ -567,15 +580,13 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul * @param ipaddr * @param callback_arg */ -void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *callback_arg) +void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg) { - (void) name; - if (!_dns_lookup_pending) { - return; - } + (reinterpret_cast(callback_arg))->called = true; if(ipaddr) { - (*reinterpret_cast(callback_arg)) = IPAddress(ipaddr); + (reinterpret_cast(callback_arg))->addr = ipaddr->addr; } + DEBUG_WIFI_GENERIC("[hostByName] Host %s: Callback function called", name); esp_schedule(); // resume the hostByName function } From 64563bf1165c3955b7677be6f20378a9e7f07c8f Mon Sep 17 00:00:00 2001 From: ruggi99 Date: Thu, 11 Jul 2019 13:40:40 +0200 Subject: [PATCH 2/6] Update ESP8266WiFiGeneric.cpp --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 106 ++++++++++-------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 51056d54f4..31daf370a0 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -272,20 +272,24 @@ bool ESP8266WiFiGenericClass::setSleepMode(WiFiSleepType_t type, uint8_t listenI wifi_set_listen_interval(): Set listen interval of maximum sleep level for modem sleep and light sleep It only works when sleep level is set as MAX_SLEEP_T - It should be called following the order: - wifi_set_sleep_level(MAX_SLEEP_T) - wifi_set_listen_interval - wifi_set_sleep_type forum: https://github.com/espressif/ESP8266_NONOS_SDK/issues/165#issuecomment-416121920 default value seems to be 3 (as recommended by https://routerguide.net/dtim-interval-period-best-setting/) + + call order: + wifi_set_sleep_level(MAX_SLEEP_T) (SDK3) + wifi_set_listen_interval (SDK3) + wifi_set_sleep_type (all SDKs) + */ +#ifdef NONOSDK3V0 + #ifdef DEBUG_ESP_WIFI if (listenInterval && type == WIFI_NONE_SLEEP) DEBUG_WIFI_GENERIC("listenInterval not usable with WIFI_NONE_SLEEP\n"); #endif - if (type == WIFI_LIGHT_SLEEP || type == WIFI_MODEM_SLEEP) { + if (type == WIFI_LIGHT_SLEEP || type == WIFI_MODEM_SLEEP) { if (listenInterval) { if (!wifi_set_sleep_level(MAX_SLEEP_T)) { DEBUG_WIFI_GENERIC("wifi_set_sleep_level(MAX_SLEEP_T): error\n"); @@ -309,6 +313,10 @@ bool ESP8266WiFiGenericClass::setSleepMode(WiFiSleepType_t type, uint8_t listenI } } } +#else // !defined(NONOSDK3V0) + (void)listenInterval; +#endif // !defined(NONOSDK3V0) + bool ret = wifi_set_sleep_type((sleep_type_t) type); if (!ret) { DEBUG_WIFI_GENERIC("wifi_set_sleep_type(%d): error\n", (int)type); @@ -389,6 +397,11 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { bool ret = false; + if (m != WIFI_STA && m != WIFI_AP_STA) + // calls lwIP's dhcp_stop(), + // safe to call even if not started + wifi_station_dhcpc_stop(); + ETS_UART_INTR_DISABLE(); if(_persistent) { ret = wifi_set_opmode(m); @@ -494,7 +507,11 @@ bool ESP8266WiFiGenericClass::forceSleepWake() { * @return interval */ uint8_t ESP8266WiFiGenericClass::getListenInterval () { +#ifndef NONOSDK3V0 + return 0; +#else return wifi_get_listen_interval(); +#endif } /** @@ -502,7 +519,11 @@ uint8_t ESP8266WiFiGenericClass::getListenInterval () { * @return true if max level */ bool ESP8266WiFiGenericClass::isSleepLevelMax () { +#ifndef NONOSDK3V0 + return false; +#else return wifi_get_sleep_level() == MAX_SLEEP_T; +#endif } @@ -510,68 +531,55 @@ bool ESP8266WiFiGenericClass::isSleepLevelMax () { // ------------------------------------------------ Generic Network function --------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- -void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg); +void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *callback_arg); -struct arg_callback { - IPAddress addr; - bool called = false; -}; +static bool _dns_lookup_pending = false; /** * Resolve the given hostname to an IP address. * @param aHostname Name to be resolved * @param aResult IPAddress structure to store the returned IP address - * @return 1 if a String was successfully converted to an IP address, - * else error code + * @return 1 if aIPAddrString was successfully converted to an IP address, + * else 0 */ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) { return hostByName(aHostname, aResult, 10000); } + int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms) { ip_addr_t addr; - static struct arg_callback addr2; - static bool pending = false; // If waiting for the callback function call + aResult = static_cast(0); if(aResult.fromString(aHostname)) { - // Host name is a IP address, use it! + // Host name is a IP address use it! + DEBUG_WIFI_GENERIC("[hostByName] Host: %s is a IP!\n", aHostname); return 1; } - if(addr2.called) { // If the callback function has been called - addr2.called = false; - pending = false; - if(addr2.addr != IPADDR_ANY) { // If IP found - DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP found %s\n", aHostname, addr2.addr.toString().c_str()); - aResult = addr2.addr; - addr2.addr = (uint32_t)IPADDR_ANY; - return 1; - } else { - DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP NOT found. Please re-try\n", aHostname); - return 0; - } - } - if(pending) { - DEBUG_WIFI_GENERIC("[hostByName] Host %s: Waiting for callback call\n", aHostname); - return 0; - } - err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &addr2); - DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP requested\n", aHostname); + + DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname); + err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); if(err == ERR_OK) { - aResult = addr.addr; - pending = false; - addr2.called = false; - DEBUG_WIFI_GENERIC("[hostByName] Host %s: IP found %s\n", aHostname, aResult.toString().c_str()); - return 1; + aResult = IPAddress(&addr); + } else if(err == ERR_INPROGRESS) { + _dns_lookup_pending = true; + delay(timeout_ms); + _dns_lookup_pending = false; + // will return here when dns_found_callback fires + if(aResult.isSet()) { + err = ERR_OK; + } } - if(err == ERR_INPROGRESS) { - DEBUG_WIFI_GENERIC("[hostByName] Host %s: Query registered\n", aHostname); - pending = true; + + if(err != 0) { + DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err); } else { - DEBUG_WIFI_GENERIC("[hostByName] Host %s: Lookup error: %d!\n", aHostname, err); + DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str()); } - return 0; + + return (err == ERR_OK) ? 1 : 0; } /** @@ -580,13 +588,15 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul * @param ipaddr * @param callback_arg */ -void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg) +void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *callback_arg) { - (reinterpret_cast(callback_arg))->called = true; + (void) name; + if (!_dns_lookup_pending) { + return; + } if(ipaddr) { - (reinterpret_cast(callback_arg))->addr = ipaddr->addr; + (*reinterpret_cast(callback_arg)) = IPAddress(ipaddr); } - DEBUG_WIFI_GENERIC("[hostByName] Host %s: Callback function called", name); esp_schedule(); // resume the hostByName function } From 55d93211294e1aca2bf3ad9d02cf5c6388fcc698 Mon Sep 17 00:00:00 2001 From: ruggi99 Date: Fri, 12 Jul 2019 21:20:53 +0200 Subject: [PATCH 3/6] Created empty method --- cores/esp8266/WString.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 48b96f08f8..b484bd90ac 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -82,6 +82,9 @@ class String { return 0; } } + inline bool empty(void) const { + return length() == 0; + } // creates a copy of the assigned value. if the value is null or // invalid, or if the memory allocation fails, the string will be From d946d749f5603f8bb845925ea94280e72d4bb368 Mon Sep 17 00:00:00 2001 From: ruggi99 Date: Mon, 15 Jul 2019 13:13:43 +0200 Subject: [PATCH 4/6] Update WString.h Changed method name from "empty" to "isEmpty". Created a new method to empty a string --- cores/esp8266/WString.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index b484bd90ac..c146c92490 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -82,9 +82,12 @@ class String { return 0; } } - inline bool empty(void) const { - return length() == 0; - } + inline void empty(void) const { + setLen(0); + } + inline bool isEmpty(void) const { + return length() == 0; + } // creates a copy of the assigned value. if the value is null or // invalid, or if the memory allocation fails, the string will be From b19070a858ff0a554414ecb853c34a82f314dc5e Mon Sep 17 00:00:00 2001 From: ruggi99 Date: Mon, 15 Jul 2019 13:38:28 +0200 Subject: [PATCH 5/6] Update WString.h --- cores/esp8266/WString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index c146c92490..0406fc58a0 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -82,7 +82,7 @@ class String { return 0; } } - inline void empty(void) const { + inline void empty(void) { setLen(0); } inline bool isEmpty(void) const { From dae03a79224db4235951ab9a134afa54008c57cc Mon Sep 17 00:00:00 2001 From: ruggi99 Date: Mon, 15 Jul 2019 14:03:36 +0200 Subject: [PATCH 6/6] Changed from "empty" to "clear" Changed method name from "empty" to "clear". --- cores/esp8266/WString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 0406fc58a0..558d1da858 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -82,7 +82,7 @@ class String { return 0; } } - inline void empty(void) { + inline void clear(void) { setLen(0); } inline bool isEmpty(void) const {