Merge pull request #2561 from geky/nsapi-network-state

lwip - Add checks for invalid state of network
pull/2734/head
Sam Grove 2016-09-16 17:20:34 -05:00 committed by GitHub
commit 1a91c1cc1d
3 changed files with 21 additions and 4 deletions

View File

@ -26,8 +26,7 @@ int EthernetInterface::connect()
int EthernetInterface::disconnect()
{
lwip_bringdown();
return 0;
return lwip_bringdown();
}
const char *EthernetInterface::get_ip_address()

View File

@ -145,6 +145,11 @@ const char *lwip_get_ip_address(void)
int lwip_bringup(void)
{
// Check if we've already connected
if (lwip_get_ip_address()) {
return NSAPI_ERROR_PARAMETER;
}
// Check if we've already brought up lwip
if (!lwip_get_mac_address()) {
// Set up network
lwip_set_mac_address();
@ -181,12 +186,19 @@ int lwip_bringup(void)
return 0;
}
void lwip_bringdown(void)
int lwip_bringdown(void)
{
// Check if we've connected
if (!lwip_get_ip_address()) {
return NSAPI_ERROR_PARAMETER;
}
// Disconnect from the network
dhcp_release(&lwip_netif);
dhcp_stop(&lwip_netif);
lwip_ip_addr[0] = '\0';
return 0;
}
@ -242,6 +254,12 @@ static int lwip_gethostbyname(nsapi_stack_t *stack, nsapi_addr_t *addr, const ch
static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
{
// check if network is connected
if (!lwip_get_ip_address()) {
return NSAPI_ERROR_NO_CONNECTION;
}
// allocate a socket
struct lwip_socket *s = lwip_arena_alloc();
if (!s) {
return NSAPI_ERROR_NO_SOCKET;

View File

@ -26,7 +26,7 @@ extern "C" {
// Access to lwip through the nsapi
int lwip_bringup(void);
void lwip_bringdown(void);
int lwip_bringdown(void);
extern nsapi_stack_t lwip_stack;