Skip to content

Commit

Permalink
ESP8266WiFi: support timeout for WiFi.hostByName
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed May 19, 2017
1 parent 85078f4 commit 2aeac91
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
18 changes: 16 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,23 @@ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg);
#endif

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 aIPAddrString was successfully converted to an IP address,
* else error code
*/
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) {
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<uint32_t>(0);

Expand All @@ -448,7 +457,9 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
if(err == ERR_OK) {
aResult = addr.addr;
} else if(err == ERR_INPROGRESS) {
esp_yield();
_dns_lookup_pending = true;
delay(timeout_ms);
_dns_lookup_pending = false;
// will return here when dns_found_callback fires
if(aResult != 0) {
err = ERR_OK;
Expand Down Expand Up @@ -477,6 +488,9 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca
#endif
{
(void) name;
if (!_dns_lookup_pending) {
return;
}
if(ipaddr) {
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->addr;
}
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ESP8266WiFiGenericClass {
public:

int hostByName(const char* aHostname, IPAddress& aResult);
int hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms);

protected:

Expand Down

0 comments on commit 2aeac91

Please sign in to comment.