Skip to content

Commit 2aeac91

Browse files
committed
ESP8266WiFi: support timeout for WiFi.hostByName
1 parent 85078f4 commit 2aeac91

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,23 @@ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback
426426
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg);
427427
#endif
428428

429+
static bool _dns_lookup_pending = false;
430+
429431
/**
430432
* Resolve the given hostname to an IP address.
431433
* @param aHostname Name to be resolved
432434
* @param aResult IPAddress structure to store the returned IP address
433435
* @return 1 if aIPAddrString was successfully converted to an IP address,
434436
* else error code
435437
*/
436-
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) {
438+
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
439+
{
440+
return hostByName(aHostname, aResult, 10000);
441+
}
442+
443+
444+
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms)
445+
{
437446
ip_addr_t addr;
438447
aResult = static_cast<uint32_t>(0);
439448

@@ -448,7 +457,9 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
448457
if(err == ERR_OK) {
449458
aResult = addr.addr;
450459
} else if(err == ERR_INPROGRESS) {
451-
esp_yield();
460+
_dns_lookup_pending = true;
461+
delay(timeout_ms);
462+
_dns_lookup_pending = false;
452463
// will return here when dns_found_callback fires
453464
if(aResult != 0) {
454465
err = ERR_OK;
@@ -477,6 +488,9 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca
477488
#endif
478489
{
479490
(void) name;
491+
if (!_dns_lookup_pending) {
492+
return;
493+
}
480494
if(ipaddr) {
481495
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->addr;
482496
}

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ESP8266WiFiGenericClass {
9898
public:
9999

100100
int hostByName(const char* aHostname, IPAddress& aResult);
101+
int hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms);
101102

102103
protected:
103104

0 commit comments

Comments
 (0)