Skip to content

Commit 19c9761

Browse files
ESP8266: add SocketAddress-based API for get_ip_address, get_gateway and get_netmask
1 parent c7b07a7 commit 19c9761

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,26 @@ const char *ESP8266Interface::get_ip_address()
509509
return ip_buff;
510510
}
511511

512+
nsapi_error_t ESP8266Interface::get_ip_address(SocketAddress *address)
513+
{
514+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
515+
_esp.uart_enable_input(true);
516+
}
517+
518+
const char *ip_buff = _esp.ip_addr();
519+
if (!ip_buff || strcmp(ip_buff, "0.0.0.0") == 0) {
520+
ip_buff = NULL;
521+
}
522+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
523+
_esp.uart_enable_input(false);
524+
}
525+
if (ip_buff) {
526+
address->set_ip_address(ip_buff);
527+
return NSAPI_ERROR_OK;
528+
}
529+
return NSAPI_ERROR_NO_ADDRESS;
530+
}
531+
512532
const char *ESP8266Interface::get_mac_address()
513533
{
514534
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
@@ -522,11 +542,43 @@ const char *ESP8266Interface::get_mac_address()
522542
return ret;
523543
}
524544

545+
nsapi_error_t ESP8266Interface::get_gateway(SocketAddress *address)
546+
{
547+
if (address == nullptr) {
548+
return NSAPI_ERROR_PARAMETER;
549+
}
550+
if (_conn_stat == NSAPI_STATUS_DISCONNECTED) {
551+
return NSAPI_ERROR_NO_CONNECTION;
552+
}
553+
554+
if (!address->set_ip_address(_esp.gateway())) {
555+
return NSAPI_ERROR_NO_ADDRESS;
556+
}
557+
558+
return NSAPI_ERROR_OK;
559+
}
560+
525561
const char *ESP8266Interface::get_gateway()
526562
{
527563
return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.gateway() : NULL;
528564
}
529565

566+
nsapi_error_t ESP8266Interface::get_netmask(SocketAddress *address)
567+
{
568+
if (address == nullptr) {
569+
return NSAPI_ERROR_PARAMETER;
570+
}
571+
if (_conn_stat == NSAPI_STATUS_DISCONNECTED) {
572+
return NSAPI_ERROR_NO_CONNECTION;
573+
}
574+
575+
if (!address->set_ip_address(_esp.gateway())) {
576+
return NSAPI_ERROR_NO_ADDRESS;
577+
}
578+
579+
return NSAPI_ERROR_OK;
580+
}
581+
530582
const char *ESP8266Interface::get_netmask()
531583
{
532584
return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.netmask() : NULL;

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
137137
/** Get the internally stored IP address
138138
* @return IP address of the interface or null if not yet connected
139139
*/
140+
virtual nsapi_error_t get_ip_address(SocketAddress *address);
141+
140142
virtual const char *get_ip_address();
141143

142144
/** Get the internally stored MAC address
@@ -149,13 +151,17 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
149151
* @return Null-terminated representation of the local gateway
150152
* or null if no network mask has been recieved
151153
*/
154+
virtual nsapi_error_t get_gateway(SocketAddress *address);
155+
152156
virtual const char *get_gateway();
153157

154158
/** Get the local network mask
155159
*
156160
* @return Null-terminated representation of the local network mask
157161
* or null if no network mask has been recieved
158162
*/
163+
virtual nsapi_error_t get_netmask(SocketAddress *address);
164+
159165
virtual const char *get_netmask();
160166

161167
/** Get the network interface name

0 commit comments

Comments
 (0)