Skip to content

Commit 60f803c

Browse files
Add SocketAddress-based API and deprecate string-based one
As of mbed-os-5.15 the string-based APIs are deprecated. This commit adds the deprecation warning and introduces the new SocketAddress-based API.
1 parent 0321d3c commit 60f803c

File tree

4 files changed

+149
-36
lines changed

4 files changed

+149
-36
lines changed

ESP32Interface.cpp

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,24 @@ ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) :
6868
_esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb));
6969
}
7070

71+
nsapi_error_t ESP32Interface::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
72+
{
73+
_dhcp = false;
74+
_ip_address = ip_address;
75+
_netmask = netmask;
76+
_gateway = gateway;
77+
78+
return NSAPI_ERROR_OK;
79+
}
80+
7181
nsapi_error_t ESP32Interface::set_network(const char *ip_address, const char *netmask, const char *gateway)
7282
{
7383
_dhcp = false;
7484

75-
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
76-
_ip_address[sizeof(_ip_address) - 1] = '\0';
77-
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
78-
_netmask[sizeof(_netmask) - 1] = '\0';
79-
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
80-
_gateway[sizeof(_gateway) - 1] = '\0';
85+
// Don't check return values, so user can clear the addresses by passing empty strings.
86+
_ip_address.set_ip_address(ip_address);
87+
_netmask.set_ip_address(netmask);
88+
_gateway.set_ip_address(gateway);
8189

8290
return NSAPI_ERROR_OK;
8391
}
@@ -114,7 +122,7 @@ int ESP32Interface::connect()
114122
}
115123

116124
if (!_dhcp) {
117-
if (!_esp->set_network(_ip_address, _netmask, _gateway)) {
125+
if (!_esp->set_network(_ip_address.get_ip_address(), _netmask.get_ip_address(), _gateway.get_ip_address())) {
118126
return NSAPI_ERROR_DEVICE_ERROR;
119127
}
120128
}
@@ -204,6 +212,14 @@ int ESP32Interface::disconnect()
204212
return NSAPI_ERROR_OK;
205213
}
206214

215+
nsapi_error_t ESP32Interface::get_ip_address(SocketAddress* sockAddr)
216+
{
217+
if (sockAddr->set_ip_address(_esp->getIPAddress())) {
218+
return NSAPI_ERROR_OK;
219+
}
220+
return NSAPI_ERROR_NO_ADDRESS;
221+
}
222+
207223
const char *ESP32Interface::get_ip_address()
208224
{
209225
return _esp->getIPAddress();
@@ -214,11 +230,27 @@ const char *ESP32Interface::get_mac_address()
214230
return _esp->getMACAddress();
215231
}
216232

233+
nsapi_error_t ESP32Interface::get_gateway(SocketAddress* sockAddr)
234+
{
235+
if (sockAddr->set_ip_address(_esp->getGateway())) {
236+
return NSAPI_ERROR_OK;
237+
}
238+
return NSAPI_ERROR_NO_ADDRESS;
239+
}
240+
217241
const char *ESP32Interface::get_gateway()
218242
{
219243
return _esp->getGateway();
220244
}
221245

246+
nsapi_error_t ESP32Interface::get_netmask(SocketAddress* sockAddr)
247+
{
248+
if (sockAddr->set_ip_address(_esp->getNetmask())) {
249+
return NSAPI_ERROR_OK;
250+
}
251+
return NSAPI_ERROR_NO_ADDRESS;
252+
}
253+
222254
const char *ESP32Interface::get_netmask()
223255
{
224256
return _esp->getNetmask();

ESP32Interface.h

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
5757
* Implicitly disables DHCP, which can be enabled in set_dhcp.
5858
* Requires that the network is disconnected.
5959
*
60-
* @param ip_address Null-terminated representation of the local IP address
61-
* @param netmask Null-terminated representation of the local network mask
62-
* @param gateway Null-terminated representation of the local gateway
60+
* @param ip_address SocketAddress representation of the local IP address
61+
* @param netmask SocketAddress representation of the local network mask
62+
* @param gateway SocketAddress representation of the local gateway
6363
* @return 0 on success, negative error code on failure
6464
*/
65+
virtual nsapi_error_t set_network(
66+
const SocketAddress &ip_address, const SocketAddress &netmask,
67+
const SocketAddress &gateway) override;
68+
69+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
6570
virtual nsapi_error_t set_network(
6671
const char *ip_address, const char *netmask, const char *gateway);
6772

@@ -123,8 +128,15 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
123128
virtual int disconnect();
124129

125130
/** Get the internally stored IP address
126-
* @return IP address of the interface or null if not yet connected
131+
* @param sockAddr SocketAddress pointer to store the local IP address
132+
* @retval NSAPI_ERROR_OK on success
133+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
134+
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
135+
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
127136
*/
137+
virtual nsapi_error_t get_ip_address(SocketAddress *sockAddr);
138+
139+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
128140
virtual const char *get_ip_address();
129141

130142
/** Get the internally stored MAC address
@@ -134,16 +146,28 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
134146

135147
/** Get the local gateway
136148
*
137-
* @return Null-terminated representation of the local gateway
138-
* or null if no network mask has been recieved
149+
* @param sockAddr SocketAddress representation of gateway address
150+
* @retval NSAPI_ERROR_OK on success
151+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
152+
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
153+
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
139154
*/
155+
virtual nsapi_error_t get_gateway(SocketAddress *sockAddr);
156+
157+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
140158
virtual const char *get_gateway();
141159

142160
/** Get the local network mask
143161
*
144-
* @return Null-terminated representation of the local network mask
145-
* or null if no network mask has been recieved
162+
* @param sockAddr SocketAddress representation of netmask
163+
* @retval NSAPI_ERROR_OK on success
164+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
165+
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
166+
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
146167
*/
168+
virtual nsapi_error_t get_netmask(SocketAddress *sockAddr);
169+
170+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
147171
virtual const char *get_netmask();
148172

149173
/** Gets the current radio signal strength for active connection
@@ -221,9 +245,9 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
221245
char _ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
222246
char _ap_pass[64]; /* The longest allowed passphrase */
223247
nsapi_security_t _ap_sec;
224-
char _ip_address[NSAPI_IPv6_SIZE];
225-
char _netmask[NSAPI_IPv4_SIZE];
226-
char _gateway[NSAPI_IPv4_SIZE];
248+
SocketAddress _ip_address;
249+
SocketAddress _netmask;
250+
SocketAddress _gateway;
227251
nsapi_connection_status_t _connection_status;
228252
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
229253

ESP32InterfaceAP.cpp

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,23 @@ ESP32InterfaceAP::ESP32InterfaceAP(PinName tx, PinName rx, bool debug) :
6565
{
6666
}
6767

68+
nsapi_error_t ESP32InterfaceAP::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
69+
{
70+
_dhcp = false;
71+
_ip_address = ip_address;
72+
_netmask = netmask;
73+
_gateway = gateway;
74+
75+
return NSAPI_ERROR_OK;
76+
}
77+
6878
nsapi_error_t ESP32InterfaceAP::set_network(const char *ip_address, const char *netmask, const char *gateway)
6979
{
7080
_dhcp = false;
7181

72-
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
73-
_ip_address[sizeof(_ip_address) - 1] = '\0';
74-
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
75-
_netmask[sizeof(_netmask) - 1] = '\0';
76-
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
77-
_gateway[sizeof(_gateway) - 1] = '\0';
82+
_ip_address.set_ip_address(ip_address);
83+
_netmask.set_ip_address(netmask);
84+
_gateway.set_ip_address(gateway);
7885

7986
return NSAPI_ERROR_OK;
8087
}
@@ -115,7 +122,7 @@ int ESP32InterfaceAP::connect()
115122
}
116123

117124
if (!_dhcp) {
118-
if (!_esp->set_network_ap(_ip_address, _netmask, _gateway)) {
125+
if (!_esp->set_network_ap(_ip_address.get_ip_address(), _netmask.get_ip_address(), _gateway.get_ip_address())) {
119126
return NSAPI_ERROR_DEVICE_ERROR;
120127
}
121128
}
@@ -179,6 +186,14 @@ int ESP32InterfaceAP::disconnect()
179186
return NSAPI_ERROR_OK;
180187
}
181188

189+
nsapi_error_t ESP32InterfaceAP::get_ip_address(SocketAddress* sockAddr)
190+
{
191+
if (sockAddr->set_ip_address(_esp->getIPAddress_ap())) {
192+
return NSAPI_ERROR_OK;
193+
}
194+
return NSAPI_ERROR_NO_ADDRESS;
195+
}
196+
182197
const char *ESP32InterfaceAP::get_ip_address()
183198
{
184199
return _esp->getIPAddress_ap();
@@ -189,11 +204,28 @@ const char *ESP32InterfaceAP::get_mac_address()
189204
return _esp->getMACAddress_ap();
190205
}
191206

207+
nsapi_error_t ESP32InterfaceAP::get_gateway(SocketAddress* sockAddr)
208+
{
209+
if (sockAddr->set_ip_address(_esp->getGateway_ap())) {
210+
return NSAPI_ERROR_OK;
211+
}
212+
return NSAPI_ERROR_NO_ADDRESS;
213+
}
214+
192215
const char *ESP32InterfaceAP::get_gateway()
193216
{
194217
return _esp->getGateway_ap();
195218
}
196219

220+
nsapi_error_t ESP32InterfaceAP::get_netmask(SocketAddress* sockAddr)
221+
{
222+
if (sockAddr->set_ip_address(_esp->getNetmask_ap())) {
223+
return NSAPI_ERROR_OK;
224+
}
225+
return NSAPI_ERROR_NO_ADDRESS;
226+
}
227+
228+
197229
const char *ESP32InterfaceAP::get_netmask()
198230
{
199231
return _esp->getNetmask_ap();

ESP32InterfaceAP.h

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
5858
* Implicitly disables DHCP, which can be enabled in set_dhcp.
5959
* Requires that the network is disconnected.
6060
*
61-
* @param ip_address Null-terminated representation of the local IP address
62-
* @param netmask Null-terminated representation of the local network mask
63-
* @param gateway Null-terminated representation of the local gateway
61+
* @param ip_address SocketAddress representation of the local IP address
62+
* @param netmask SocketAddress representation of the local network mask
63+
* @param gateway SocketAddress representation of the local gateway
6464
* @return 0 on success, negative error code on failure
6565
*/
66+
virtual nsapi_error_t set_network(
67+
const SocketAddress &ip_address, const SocketAddress &netmask,
68+
const SocketAddress &gateway) override;
69+
70+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
6671
virtual nsapi_error_t set_network(
6772
const char *ip_address, const char *netmask, const char *gateway);
6873

@@ -124,8 +129,16 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
124129
virtual int disconnect();
125130

126131
/** Get the internally stored IP address
127-
* @return IP address of the interface or null if not yet connected
132+
*
133+
* @param address SocketAddress pointer to store the local IP address
134+
* @retval NSAPI_ERROR_OK on success
135+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
136+
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
137+
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
128138
*/
139+
virtual nsapi_error_t get_ip_address(SocketAddress *address);
140+
141+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
129142
virtual const char *get_ip_address();
130143

131144
/** Get the internally stored MAC address
@@ -135,16 +148,28 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
135148

136149
/** Get the local gateway
137150
*
138-
* @return Null-terminated representation of the local gateway
139-
* or null if no network mask has been recieved
151+
* @param address SocketAddress representation of gateway address
152+
* @retval NSAPI_ERROR_OK on success
153+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
154+
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
155+
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
140156
*/
157+
virtual nsapi_error_t get_gateway(SocketAddress *address);
158+
159+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
141160
virtual const char *get_gateway();
142161

143162
/** Get the local network mask
144163
*
145-
* @return Null-terminated representation of the local network mask
146-
* or null if no network mask has been recieved
164+
* @param address SocketAddress representation of netmask
165+
* @retval NSAPI_ERROR_OK on success
166+
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
167+
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
168+
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
147169
*/
170+
virtual nsapi_error_t get_netmask(SocketAddress *address);
171+
172+
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
148173
virtual const char *get_netmask();
149174

150175
/** Gets the current radio signal strength for active connection
@@ -224,9 +249,9 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
224249
char _own_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
225250
char _own_pass[64]; /* The longest allowed passphrase */
226251
nsapi_security_t _own_sec;
227-
char _ip_address[NSAPI_IPv6_SIZE];
228-
char _netmask[NSAPI_IPv4_SIZE];
229-
char _gateway[NSAPI_IPv4_SIZE];
252+
SocketAddress _ip_address;
253+
SocketAddress _netmask;
254+
SocketAddress _gateway;
230255
nsapi_connection_status_t _connection_status;
231256
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
232257
};

0 commit comments

Comments
 (0)