3636#include " lwip/dns.h"
3737#include " lwip/udp.h"
3838
39- #include " ppp_lwip.h"
40-
4139#include " LWIPStack.h"
4240
4341LWIP::Interface *LWIP::Interface::list;
@@ -352,7 +350,7 @@ char *LWIP::Interface::get_gateway(char *buf, nsapi_size_t buflen)
352350LWIP::Interface::Interface () :
353351 hw(NULL ), has_addr_state(0 ),
354352 connected(NSAPI_STATUS_DISCONNECTED),
355- dhcp_started(false ), dhcp_has_to_be_set(false ), blocking(true ), ppp (false )
353+ dhcp_started(false ), dhcp_has_to_be_set(false ), blocking(true ), ppp_enabled (false )
356354{
357355 memset (&netif, 0 , sizeof netif);
358356
@@ -395,7 +393,7 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
395393 }
396394 interface->emac = &emac;
397395 interface->memory_manager = &memory_manager;
398- interface->ppp = false ;
396+ interface->ppp_enabled = false ;
399397
400398#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
401399 netif->interface .hwaddr [0 ] = MBED_MAC_ADDR_0;
@@ -452,7 +450,7 @@ nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetwo
452450 }
453451 interface->l3ip = &l3ip;
454452 interface->memory_manager = &memory_manager;
455- interface->ppp = false ;
453+ interface->ppp_enabled = false ;
456454
457455
458456
@@ -462,7 +460,7 @@ nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetwo
462460#if LWIP_IPV4
463461 0 , 0 , 0 ,
464462#endif
465- interface, &LWIP::Interface::l3ip_if_init, tcpip_input )) {
463+ interface, &LWIP::Interface::l3ip_if_init, ip_input )) {
466464 return NSAPI_ERROR_DEVICE_ERROR;
467465 }
468466
@@ -521,21 +519,27 @@ nsapi_error_t LWIP::remove_l3ip_interface(OnboardNetworkStack::Interface **inter
521519
522520#endif // LWIP_L3IP
523521}
524- /* Internal API to preserve existing PPP functionality - revise to better match mbed_ipstak_add_ethernet_interface later */
525- nsapi_error_t LWIP::_add_ppp_interface (void *hw, bool default_if, nsapi_ip_stack_t stack, LWIP::Interface **interface_out)
522+
523+
524+ nsapi_error_t LWIP::add_ppp_interface (PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out)
526525{
527- #if LWIP_PPP_API
526+ #if PPP_SUPPORT
528527 Interface *interface = new (std::nothrow) Interface ();
529528 if (!interface) {
530529 return NSAPI_ERROR_NO_MEMORY;
531530 }
532- interface->hw = hw;
533- interface->ppp = true ;
531+ interface->ppp = &ppp;
532+ interface->memory_manager = &memory_manager;
533+ interface->ppp_enabled = true ;
534534
535- nsapi_error_t ret = ppp_lwip_if_init (hw, &interface->netif , stack);
536- if (ret != NSAPI_ERROR_OK) {
537- free (interface);
538- return ret;
535+ // interface->netif.hwaddr_len = 0; should we set?
536+
537+ if (!netif_add (&interface->netif ,
538+ #if LWIP_IPV4
539+ 0 , 0 , 0 ,
540+ #endif
541+ interface, &LWIP::Interface::ppp_if_init, tcpip_input)) {
542+ return NSAPI_ERROR_DEVICE_ERROR;
539543 }
540544
541545 if (default_if) {
@@ -548,11 +552,62 @@ nsapi_error_t LWIP::_add_ppp_interface(void *hw, bool default_if, nsapi_ip_stack
548552
549553 *interface_out = interface;
550554
555+ // lwip_add_random_seed(seed); to do?
556+
557+ return NSAPI_ERROR_OK;
558+
559+ #else
560+ return NSAPI_ERROR_UNSUPPORTED;
561+
562+ #endif // PPP_SUPPORT
563+ }
564+
565+ nsapi_error_t LWIP::remove_ppp_interface (OnboardNetworkStack::Interface **interface_out)
566+ {
567+ #if PPP_SUPPORT
568+ if ((interface_out != NULL ) && (*interface_out != NULL )) {
569+
570+ Interface *lwip = static_cast <Interface *>(*interface_out);
571+ Interface *node = lwip->list ;
572+
573+ if (lwip->list != NULL ) {
574+ if (lwip->list == lwip) {
575+ // Power down PPP service
576+ lwip->ppp ->power_down ();
577+ if (netif_is_link_up (&lwip->netif )) {
578+ // Wait PPP service to report link down
579+ osSemaphoreAcquire (lwip->unlinked , osWaitForever);
580+ }
581+ netif_remove (&node->netif );
582+ lwip->list = lwip->list ->next ;
583+ delete node;
584+ } else {
585+ while (node->next != NULL && node->next != lwip) {
586+ node = node->next ;
587+ }
588+ if (node->next != NULL && node->next == lwip) {
589+ Interface *remove = node->next ;
590+ // Power down PPP service
591+ remove->ppp ->power_down ();
592+ if (netif_is_link_up (&lwip->netif )) {
593+ // Wait PPP service to report link down
594+ osSemaphoreAcquire (lwip->unlinked , osWaitForever);
595+ }
596+ netif_remove (&remove->netif );
597+ node->next = node->next ->next ;
598+ delete remove;
599+ }
600+ }
601+ }
602+ }
603+
551604 return NSAPI_ERROR_OK;
552605#else
553606 return NSAPI_ERROR_UNSUPPORTED;
554- #endif // LWIP_PPP_API
607+
608+ #endif // PPP_SUPPORT
555609}
610+
556611void LWIP::set_default_interface (OnboardNetworkStack::Interface *interface)
557612{
558613 if (interface) {
@@ -609,7 +664,7 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
609664
610665#if LWIP_IPV4
611666 if (stack != IPV6_STACK) {
612- if (!dhcp && !ppp ) {
667+ if (!dhcp && !ppp_enabled ) {
613668 ip4_addr_t ip_addr;
614669 ip4_addr_t netmask_addr;
615670 ip4_addr_t gw_addr;
@@ -629,23 +684,10 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
629684 client_callback (NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_CONNECTING);
630685 }
631686
632- if (ppp) {
633- err_t err = ppp_lwip_connect (hw);
634- if (err) {
635- connected = NSAPI_STATUS_DISCONNECTED;
636- if (client_callback) {
637- client_callback (NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED);
638- }
639- return err_remap (err);
640- }
641- }
642687
643688 if (!netif_is_link_up (&netif)) {
644689 if (blocking) {
645690 if (osSemaphoreAcquire (linked, LINK_TIMEOUT * 1000 ) != osOK) {
646- if (ppp) {
647- (void ) ppp_lwip_disconnect (hw);
648- }
649691 return NSAPI_ERROR_NO_CONNECTION;
650692 }
651693 }
@@ -665,9 +707,6 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
665707 // If doesn't have address
666708 if (!LWIP::get_ip_addr (true , &netif)) {
667709 if (osSemaphoreAcquire (has_any_addr, DHCP_TIMEOUT * 1000 ) != osOK) {
668- if (ppp) {
669- (void ) ppp_lwip_disconnect (hw);
670- }
671710 return NSAPI_ERROR_DHCP_FAILURE;
672711 }
673712 }
@@ -713,21 +752,7 @@ nsapi_error_t LWIP::Interface::bringdown()
713752 }
714753#endif
715754
716- if (ppp) {
717- /* this is a blocking call, returns when PPP is properly closed */
718- err_t err = ppp_lwip_disconnect (hw);
719- if (err) {
720- return err_remap (err);
721- }
722- MBED_ASSERT (!netif_is_link_up (&netif));
723- /* if (netif_is_link_up(&netif)) {
724- if (sys_arch_sem_wait(&unlinked, 15000) == SYS_ARCH_TIMEOUT) {
725- return NSAPI_ERROR_DEVICE_ERROR;
726- }
727- }*/
728- } else {
729- netif_set_down (&netif);
730- }
755+ netif_set_down (&netif);
731756
732757#if LWIP_IPV6
733758 mbed_lwip_clear_ipv6_addresses (&netif);
0 commit comments