LWIPBP3595Interface library for mbed-os.

Dependents:   LWIPBP3595Interface_STA_for_mbed-os

Fork of LWIPBP3595Interface by Rohm

Revision:
2:c7e325599570
Parent:
0:a933851e5d22
Child:
3:2ff2514e4fca
--- a/LWIPBP3595Interface.cpp	Tue May 31 05:56:27 2016 +0000
+++ b/LWIPBP3595Interface.cpp	Tue Sep 13 09:31:41 2016 +0000
@@ -1,8 +1,3 @@
-/* LWIPBP3595Interface.cpp */
-/* Copyright (C) 2016 Grape Systems, Inc. */
-/* The base file is LWIPInterface.cpp. */
-
-/* LWIPInterface.cpp */
 /* LWIP implementation of NetworkInterfaceAPI
  * Copyright (c) 2015 ARM Limited
  *
@@ -19,79 +14,11 @@
  * limitations under the License.
  */
 
-#include "mbed.h"
 #include "LWIPBP3595Interface.h"
 #include "LWIPBP3595Interface_BssType.h"
+#include "lwip_wifi_stack.h"
 #include "WlanBP3595.h"
 
-#include "lwip/inet.h"
-#include "lwip/netif.h"
-#include "lwip/dhcp.h"
-#include "lwip/tcpip.h"
-#include "lwip/sockets.h"
-#include "lwip/netdb.h"
-#include "netif/etharp.h"
-#include "wifi_arch.h"
-#include "lwip/netif.h"
-#include "lwip/udp.h"
-#include "lwip/tcp.h"
-#include "lwip/tcp_impl.h"
-#include "lwip/timers.h"
-#include "lwip/dns.h"
-#include "lwip/def.h"
-#include "lwip/ip_addr.h"
-
-#define LWIP_TIMEOUT 180000
-
-/* TCP/IP and Network Interface Initialisation */
-static struct netif netif;
-
-static char ip_addr[NSAPI_IP_SIZE] = "\0";
-static char mac_addr[NSAPI_MAC_SIZE] = "\0";
-
-static Semaphore tcpip_inited(0);
-static Semaphore netif_linked(0);
-static Semaphore netif_up(0);
-
-static void tcpip_init_irq(void *)
-{
-    tcpip_inited.release();
-}
-
-static void netif_link_irq(struct netif *netif)
-{
-    if (netif_is_link_up(netif)) {
-        netif_linked.release();
-    }
-}
-
-static void netif_status_irq(struct netif *netif)
-{
-    if (netif_is_up(netif)) {
-        strcpy(ip_addr, inet_ntoa(netif->ip_addr));
-        netif_up.release();
-    }
-}
-
-static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw)
-{
-    tcpip_init(tcpip_init_irq, NULL);
-    tcpip_inited.wait();
-
-    memset((void*) &netif, 0, sizeof(netif));
-    netif_add(&netif, ipaddr, netmask, gw, NULL, wifi_arch_enetif_init, tcpip_input);
-    netif_set_default(&netif);
-
-    netif_set_link_callback  (&netif, netif_link_irq);
-    netif_set_status_callback(&netif, netif_status_irq);
-}
-
-static void set_mac_address(void)
-{
-    snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1],
-             netif.hwaddr[2], netif.hwaddr[3], netif.hwaddr[4], netif.hwaddr[5]);
-}
-
 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) {
     if ((ucType == 'I') && (usWid == 0x0005)) {
         if (pucData[0] == 0x01) {     // CONNECTED
@@ -194,50 +121,34 @@
     return 0;
 }
 
-// LWIPBP3595Interface implementation
-int LWIPBP3595Interface::connect(
-    const char *ssid, 
-    const char *pass, 
-    nsapi_security_t security)
+/* Interface implementation */
+int LWIPBP3595Interface::connect(const char *ssid, const char *pass, nsapi_security_t security)
 {
     _wlan_init();
-
-    // Set up network
-    init_netif(0, 0, 0);
-    set_mac_address();
-
-    _wlan_setting(ssid, pass, security);
-
-    // Connect to network
-    wifi_arch_enable_interrupts();
-
-    dhcp_start(&netif);
-
-    // Wait for an IP Address
-    // -1: error, 0: timeout
-    if (netif_up.wait(LWIP_TIMEOUT) < 0) {
-        return NSAPI_ERROR_DHCP_FAILURE;
+    if (lwip_wifi_init() == 0) {
+        _wlan_setting(ssid, pass, security);
     }
 
-    return 0;
+    return lwip_wifi_bringup();
 }
 
 int LWIPBP3595Interface::disconnect()
 {
-    dhcp_release(&netif);
-    dhcp_stop(&netif);
-
-    wifi_arch_disable_interrupts();
-
+    lwip_wifi_bringdown();
     return 0;
 }
 
 const char *LWIPBP3595Interface::get_ip_address()
 {
-    return ip_addr;
+    return lwip_wifi_get_ip_address();
 }
 
 const char *LWIPBP3595Interface::get_mac_address()
 {
-    return mac_addr;
+    return lwip_wifi_get_mac_address();
 }
+
+NetworkStack *LWIPBP3595Interface::get_stack()
+{
+    return nsapi_create_stack(&lwip_wifi_stack);
+}