Skip to content

Commit

Permalink
ESP8266: add SocketAddress-based API for get_ip_address, get_gateway …
Browse files Browse the repository at this point in the history
…and get_netmask
  • Loading branch information
michalpasztamobica committed Nov 25, 2019
1 parent 883295e commit b858fad
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
52 changes: 52 additions & 0 deletions components/wifi/esp8266-driver/ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,26 @@ const char *ESP8266Interface::get_ip_address()
return ip_buff;
}

nsapi_error_t ESP8266Interface::get_ip_address(SocketAddress *address)
{
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
_esp.uart_enable_input(true);
}

const char *ip_buff = _esp.ip_addr();
if (!ip_buff || strcmp(ip_buff, "0.0.0.0") == 0) {
ip_buff = NULL;
}
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
_esp.uart_enable_input(false);
}
if (ip_buff) {
address->set_ip_address(ip_buff);
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}

const char *ESP8266Interface::get_mac_address()
{
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
Expand All @@ -522,11 +542,43 @@ const char *ESP8266Interface::get_mac_address()
return ret;
}

nsapi_error_t ESP8266Interface::get_gateway(SocketAddress *address)
{
if (address == nullptr) {
return NSAPI_ERROR_PARAMETER;
}
if (_conn_stat == NSAPI_STATUS_DISCONNECTED) {
return NSAPI_ERROR_NO_CONNECTION;
}

if (!address->set_ip_address(_esp.gateway())) {
return NSAPI_ERROR_NO_ADDRESS;
}

return NSAPI_ERROR_OK;
}

const char *ESP8266Interface::get_gateway()
{
return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.gateway() : NULL;
}

nsapi_error_t ESP8266Interface::get_netmask(SocketAddress *address)
{
if (address == nullptr) {
return NSAPI_ERROR_PARAMETER;
}
if (_conn_stat == NSAPI_STATUS_DISCONNECTED) {
return NSAPI_ERROR_NO_CONNECTION;
}

if (!address->set_ip_address(_esp.gateway())) {
return NSAPI_ERROR_NO_ADDRESS;
}

return NSAPI_ERROR_OK;
}

const char *ESP8266Interface::get_netmask()
{
return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.netmask() : NULL;
Expand Down
6 changes: 6 additions & 0 deletions components/wifi/esp8266-driver/ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
/** Get the internally stored IP address
* @return IP address of the interface or null if not yet connected
*/
virtual nsapi_error_t get_ip_address(SocketAddress *address);

virtual const char *get_ip_address();

/** Get the internally stored MAC address
Expand All @@ -149,13 +151,17 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
*/
virtual nsapi_error_t get_gateway(SocketAddress *address);

virtual const char *get_gateway();

/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
*/
virtual nsapi_error_t get_netmask(SocketAddress *address);

virtual const char *get_netmask();

/** Get the network interface name
Expand Down

0 comments on commit b858fad

Please sign in to comment.