diff --git a/features/lwipstack/LWIPInterface.cpp b/features/lwipstack/LWIPInterface.cpp index fb63a76253..18a5671774 100644 --- a/features/lwipstack/LWIPInterface.cpp +++ b/features/lwipstack/LWIPInterface.cpp @@ -465,6 +465,41 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN #endif //LWIP_ETHERNET } +nsapi_error_t LWIP::remove_ethernet_interface(OnboardNetworkStack::Interface **interface_out) +{ +#if LWIP_ETHERNET + + if ((interface_out != NULL) && (*interface_out != NULL)) { + + Interface *lwip = static_cast(*interface_out); + Interface *node = lwip->list; + + if (lwip->list != NULL) { + if (lwip->list == lwip) { + lwip->list = lwip->list->next; + netif_remove(&node->netif); + *interface_out = NULL; + delete node; + } else { + while (node->next != NULL && node->next != lwip) { + node = node->next; + } + if (node->next != NULL && node->next == lwip) { + Interface *remove = node->next; + node->next = node->next->next; + netif_remove(&remove->netif); + *interface_out = NULL; + delete remove; + } + } + } + } + + return NSAPI_ERROR_OK; +#else + return NSAPI_ERROR_UNSUPPORTED; +#endif //LWIP_ETHERNET +} nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetworkStack::Interface **interface_out) { diff --git a/features/lwipstack/LWIPStack.h b/features/lwipstack/LWIPStack.h index a5756645a9..0f5ec2cb25 100644 --- a/features/lwipstack/LWIPStack.h +++ b/features/lwipstack/LWIPStack.h @@ -281,6 +281,14 @@ public: * @param[out] interface_out pointer to stack interface object controlling the L3IP * @return NSAPI_ERROR_OK on success, or error code */ + virtual nsapi_error_t remove_ethernet_interface(OnboardNetworkStack::Interface **interface_out); + + /** Remove a network interface from IP stack + * + * Removes PPP objects,network interface from stack list, and shutdown device driver. + * @param[out] interface_out pointer to stack interface object controlling the PPP + * @return NSAPI_ERROR_OK on success, or error code + */ virtual nsapi_error_t remove_l3ip_interface(OnboardNetworkStack::Interface **interface_out); /** Remove a network interface from IP stack diff --git a/features/netsocket/OnboardNetworkStack.h b/features/netsocket/OnboardNetworkStack.h index 0a0c8430e5..8a26a880c7 100644 --- a/features/netsocket/OnboardNetworkStack.h +++ b/features/netsocket/OnboardNetworkStack.h @@ -181,6 +181,11 @@ public: return NSAPI_ERROR_UNSUPPORTED; }; + virtual nsapi_error_t remove_ethernet_interface(Interface **interface_out) + { + return NSAPI_ERROR_OK; + }; + virtual nsapi_error_t remove_l3ip_interface(Interface **interface_out) { return NSAPI_ERROR_OK; diff --git a/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSTAInterface.cpp b/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSTAInterface.cpp index b7d46ad176..a7659abca2 100644 --- a/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSTAInterface.cpp +++ b/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSTAInterface.cpp @@ -355,6 +355,15 @@ nsapi_error_t WhdSTAInterface::disconnect() } whd_emac_wifi_link_state_changed(_whd_emac.ifp, WHD_FALSE); + // remove the interface added in connect + if (_interface) { + nsapi_error_t err = _stack.remove_ethernet_interface(&_interface); + if (err != NSAPI_ERROR_OK) { + return err; + } + _iface_shared.iface_sta = NULL; + } + res = whd_wifi_deregister_event_handler(_whd_emac.ifp, sta_link_update_entry); if (res != WHD_SUCCESS) { return whd_toerror(res); diff --git a/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSoftAPInterface.cpp b/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSoftAPInterface.cpp index 6a764750ee..3446bf2bc5 100644 --- a/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSoftAPInterface.cpp +++ b/features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSoftAPInterface.cpp @@ -201,6 +201,16 @@ int WhdSoftAPInterface::stop(void) if (res != WHD_SUCCESS) { return whd_toerror(res); } + + // remove the interface added in start + if (_interface) { + nsapi_error_t err = _stack.remove_ethernet_interface(&_interface); + if (err != NSAPI_ERROR_OK) { + return err; + } + _iface_shared.iface_softap = NULL; + } + return NSAPI_ERROR_OK; }