Skip to content

Commit 1a91c1c

Browse files
authored
Merge pull request #2561 from geky/nsapi-network-state
lwip - Add checks for invalid state of network
2 parents c940d0e + 98ec80c commit 1a91c1c

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ int EthernetInterface::connect()
2626

2727
int EthernetInterface::disconnect()
2828
{
29-
lwip_bringdown();
30-
return 0;
29+
return lwip_bringdown();
3130
}
3231

3332
const char *EthernetInterface::get_ip_address()

features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ const char *lwip_get_ip_address(void)
145145
int lwip_bringup(void)
146146
{
147147
// Check if we've already connected
148+
if (lwip_get_ip_address()) {
149+
return NSAPI_ERROR_PARAMETER;
150+
}
151+
152+
// Check if we've already brought up lwip
148153
if (!lwip_get_mac_address()) {
149154
// Set up network
150155
lwip_set_mac_address();
@@ -181,12 +186,19 @@ int lwip_bringup(void)
181186
return 0;
182187
}
183188

184-
void lwip_bringdown(void)
189+
int lwip_bringdown(void)
185190
{
191+
// Check if we've connected
192+
if (!lwip_get_ip_address()) {
193+
return NSAPI_ERROR_PARAMETER;
194+
}
195+
186196
// Disconnect from the network
187197
dhcp_release(&lwip_netif);
188198
dhcp_stop(&lwip_netif);
189199
lwip_ip_addr[0] = '\0';
200+
201+
return 0;
190202
}
191203

192204

@@ -242,6 +254,12 @@ static int lwip_gethostbyname(nsapi_stack_t *stack, nsapi_addr_t *addr, const ch
242254

243255
static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
244256
{
257+
// check if network is connected
258+
if (!lwip_get_ip_address()) {
259+
return NSAPI_ERROR_NO_CONNECTION;
260+
}
261+
262+
// allocate a socket
245263
struct lwip_socket *s = lwip_arena_alloc();
246264
if (!s) {
247265
return NSAPI_ERROR_NO_SOCKET;

features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern "C" {
2626

2727
// Access to lwip through the nsapi
2828
int lwip_bringup(void);
29-
void lwip_bringdown(void);
29+
int lwip_bringdown(void);
3030

3131
extern nsapi_stack_t lwip_stack;
3232

0 commit comments

Comments
 (0)