Skip to content

Commit 1de97e9

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

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ 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+
142+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
140143
virtual const char *get_ip_address();
141144

142145
/** Get the internally stored MAC address
@@ -149,13 +152,19 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
149152
* @return Null-terminated representation of the local gateway
150153
* or null if no network mask has been recieved
151154
*/
155+
virtual nsapi_error_t get_gateway(SocketAddress *address);
156+
157+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
152158
virtual const char *get_gateway();
153159

154160
/** Get the local network mask
155161
*
156162
* @return Null-terminated representation of the local network mask
157163
* or null if no network mask has been recieved
158164
*/
165+
virtual nsapi_error_t get_netmask(SocketAddress *address);
166+
167+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
159168
virtual const char *get_netmask();
160169

161170
/** Get the network interface name

0 commit comments

Comments
 (0)