Skip to content

ESP8266: Add built-in hostname resolution handling (disabled by default) #12234

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
Jan 15, 2020
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
6 changes: 5 additions & 1 deletion components/wifi/esp8266-driver/ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,11 @@ nsapi_error_t ESP8266::open_tcp(int id, const char *addr, int port, int keepaliv
bool ESP8266::dns_lookup(const char *name, char *ip)
{
_smutex.lock();
bool done = _parser.send("AT+CIPDOMAIN=\"%s\"", name) && _parser.recv("+CIPDOMAIN:%s%*[\r]%*[\n]", ip);
set_timeout(ESP8266_DNS_TIMEOUT);
bool done = _parser.send("AT+CIPDOMAIN=\"%s\"", name)
&& _parser.recv("+CIPDOMAIN:%15[^\n]\n", ip)
&& _parser.recv("OK\n");
set_timeout();
_smutex.unlock();

return done;
Expand Down
3 changes: 3 additions & 0 deletions components/wifi/esp8266-driver/ESP8266/ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
#ifndef ESP8266_MISC_TIMEOUT
#define ESP8266_MISC_TIMEOUT 2000
#endif
#ifndef ESP8266_DNS_TIMEOUT
#define ESP8266_DNS_TIMEOUT 15000
#endif

#define ESP8266_SCAN_TIME_MIN 0 // [ms]
#define ESP8266_SCAN_TIME_MAX 1500 // [ms]
Expand Down
22 changes: 22 additions & 0 deletions components/wifi/esp8266-driver/ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,28 @@ int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count, scan_mode mode,
return ret;
}

#if MBED_CONF_ESP8266_BUILT_IN_DNS
nsapi_error_t ESP8266Interface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name)
{
char ip[NSAPI_IPv4_SIZE];
memset(ip, 0, NSAPI_IPv4_SIZE);
if (!_esp.dns_lookup(name, ip)) {
return NSAPI_ERROR_DNS_FAILURE;
}
if (!address->set_ip_address(ip)) {
return NSAPI_ERROR_DNS_FAILURE;
}

return NSAPI_ERROR_OK;
}


nsapi_error_t ESP8266Interface::add_dns_server(const SocketAddress &address, const char *interface_name)
{
return NSAPI_ERROR_OK;
}
#endif

bool ESP8266Interface::_get_firmware_ok()
{
ESP8266::fw_at_version at_v = _esp.at_version();
Expand Down
11 changes: 11 additions & 0 deletions components/wifi/esp8266-driver/ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,25 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure
*/
#if MBED_CONF_ESP8266_BUILT_IN_DNS
nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name);
#else
using NetworkInterface::gethostbyname;
#endif

using NetworkInterface::gethostbyname_async;
using NetworkInterface::gethostbyname_async_cancel;

/** Add a domain name server to list of servers to query
*
* @param addr Destination for the host address
* @return 0 on success, negative error code on failure
*/
#if MBED_CONF_ESP8266_BUILT_IN_DNS
nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name);
#else
using NetworkInterface::add_dns_server;
#endif

/** @copydoc NetworkStack::setsockopt
*/
Expand Down
4 changes: 4 additions & 0 deletions components/wifi/esp8266-driver/mbed_lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"channels": {
"help": "channel count, 13 by default",
"value": null
},
"built-in-dns": {
"help": "use built-in CIPDOMAIN AT command to resolve address to IP",
"value": false
}
},
"target_overrides": {
Expand Down