Fix lwIP PPP glue

EMAC changes broke PPP glue - fix it up. In particular, PPP steals
the netif->state pointer, so we can't use it as a back pointer to
our LWIP::Interface.
pull/7030/head
Kevin Bracey 2018-05-28 10:11:04 +03:00
parent 9c62ea311d
commit cedbf72a3b
3 changed files with 34 additions and 13 deletions

View File

@ -40,6 +40,19 @@
#include "LWIPStack.h" #include "LWIPStack.h"
LWIP::Interface *LWIP::Interface::list;
LWIP::Interface *LWIP::Interface::our_if_from_netif(struct netif *netif)
{
for (Interface *interface = list; interface; interface = interface->next) {
if (netif == &interface->netif) {
return interface;
}
}
return NULL;
}
static void add_dns_addr_to_dns_list_index(const u8_t addr_type, const u8_t index) static void add_dns_addr_to_dns_list_index(const u8_t addr_type, const u8_t index)
{ {
#if LWIP_IPV6 #if LWIP_IPV6
@ -152,7 +165,7 @@ nsapi_error_t LWIP::Interface::set_dhcp()
void LWIP::Interface::netif_link_irq(struct netif *netif) void LWIP::Interface::netif_link_irq(struct netif *netif)
{ {
LWIP::Interface *interface = static_cast<LWIP::Interface *>(netif->state); LWIP::Interface *interface = our_if_from_netif(netif);
if (netif_is_link_up(&interface->netif)) { if (netif_is_link_up(&interface->netif)) {
nsapi_error_t dhcp_status = interface->set_dhcp(); nsapi_error_t dhcp_status = interface->set_dhcp();
@ -170,7 +183,7 @@ void LWIP::Interface::netif_link_irq(struct netif *netif)
void LWIP::Interface::netif_status_irq(struct netif *netif) void LWIP::Interface::netif_status_irq(struct netif *netif)
{ {
LWIP::Interface *interface = static_cast<LWIP::Interface *>(netif->state); LWIP::Interface *interface = our_if_from_netif(netif);
if (netif_is_up(&interface->netif)) { if (netif_is_up(&interface->netif)) {
bool dns_addr_has_to_be_added = false; bool dns_addr_has_to_be_added = false;
@ -325,7 +338,8 @@ LWIP::Interface::Interface() :
has_both_addr = osSemaphoreNew(UINT16_MAX, 0, &attr); has_both_addr = osSemaphoreNew(UINT16_MAX, 0, &attr);
#endif #endif
netif.state = this; next = list;
list = this;
} }
nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out) nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out)
@ -385,33 +399,36 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
} }
/* Internal API to preserve existing PPP functionality - revise to better match mbed_ipstak_add_ethernet_interface later */ /* Internal API to preserve existing PPP functionality - revise to better match mbed_ipstak_add_ethernet_interface later */
nsapi_error_t LWIP::_add_ppp_interface(void *hw, bool default_if, LWIP::Interface **interface_out) nsapi_error_t LWIP::_add_ppp_interface(void *hw, bool default_if, nsapi_ip_stack_t stack, LWIP::Interface **interface_out)
{ {
#if LWIP_PPP #if LWIP_PPP_API
Interface *interface = new (nothrow) Interface(); Interface *interface = new (std::nothrow) Interface();
if (!interface) { if (!interface) {
return NSAPI_ERROR_NO_MEMORY; return NSAPI_ERROR_NO_MEMORY;
} }
interface->hw = hw; interface->hw = hw;
interface->ppp = true; interface->ppp = true;
ret = ppp_lwip_if_init(hw, &interface->netif); nsapi_error_t ret = ppp_lwip_if_init(hw, &interface->netif, stack);
if (ret != NSAPI_ERROR_OK) { if (ret != NSAPI_ERROR_OK) {
free(interface); free(interface);
return ret; return ret;
} }
if (default_if) if (default_if) {
netif_set_default(&interface->netif); netif_set_default(&interface->netif);
default_interface = interface;
}
netif_set_link_callback(&interface->netif, mbed_lwip_netif_link_irq); netif_set_link_callback(&interface->netif, &LWIP::Interface::netif_link_irq);
netif_set_status_callback(&interface->netif, mbed_lwip_netif_status_irq); netif_set_status_callback(&interface->netif, &LWIP::Interface::netif_status_irq);
*interface_out = interface; *interface_out = interface;
return NSAPI_ERROR_OK;
#else #else
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
#endif //LWIP_PPP #endif //LWIP_PPP_API
} }

View File

@ -118,6 +118,7 @@ public:
nsapi_error_t set_dhcp(); nsapi_error_t set_dhcp();
static void netif_link_irq(struct netif *netif); static void netif_link_irq(struct netif *netif);
static void netif_status_irq(struct netif *netif); static void netif_status_irq(struct netif *netif);
static Interface *our_if_from_netif(struct netif *netif);
#if LWIP_ETHERNET #if LWIP_ETHERNET
static err_t emac_low_level_output(struct netif *netif, struct pbuf *p); static err_t emac_low_level_output(struct netif *netif, struct pbuf *p);
@ -165,6 +166,8 @@ public:
bool ppp; bool ppp;
mbed::Callback<void(nsapi_event_t, intptr_t)> client_callback; mbed::Callback<void(nsapi_event_t, intptr_t)> client_callback;
struct netif netif; struct netif netif;
static Interface *list;
Interface *next;
LWIPMemoryManager *memory_manager; LWIPMemoryManager *memory_manager;
}; };
@ -193,10 +196,11 @@ public:
* *
* @param pcb PPP implementation specific user data; will be passed to PPP callbacks * @param pcb PPP implementation specific user data; will be passed to PPP callbacks
* @param default_if true if the interface should be treated as the default one * @param default_if true if the interface should be treated as the default one
* @param stack Allow manual selection of IPv4 and/or IPv6
* @param[out] interface_out set to interface handle that must be passed to subsequent mbed_stack calls * @param[out] interface_out set to interface handle that must be passed to subsequent mbed_stack calls
* @return NSAPI_ERROR_OK on success, or error code * @return NSAPI_ERROR_OK on success, or error code
*/ */
nsapi_error_t _add_ppp_interface(void *pcb, bool default_if, LWIP::Interface **interface_out); nsapi_error_t _add_ppp_interface(void *pcb, bool default_if, nsapi_ip_stack_t stack, LWIP::Interface **interface_out);
/** Get a domain name server from a list of servers to query /** Get a domain name server from a list of servers to query
* *

View File

@ -370,7 +370,7 @@ nsapi_error_t nsapi_ppp_connect(FileHandle *stream, Callback<void(nsapi_event_t,
if (!my_interface) { if (!my_interface) {
LWIP &lwip = LWIP::get_instance(); LWIP &lwip = LWIP::get_instance();
retcode = lwip._add_ppp_interface(stream, true, &my_interface); retcode = lwip._add_ppp_interface(stream, true, stack, &my_interface);
if (retcode != NSAPI_ERROR_OK) { if (retcode != NSAPI_ERROR_OK) {
my_interface = NULL; my_interface = NULL;
return retcode; return retcode;