This is LWIPBP3595Interface class library. The base library is LWIPInterface.

Dependents:   LWIPBP3595Interface_STA

Files at this revision

API Documentation at this revision

Comitter:
tousaki
Date:
Tue May 24 10:34:31 2016 +0000
Child:
1:abb17eced903
Commit message:
Created 1st version.

Changed in this revision

LWIPBP3595Interface.cpp Show annotated file Show diff for this revision Revisions of this file
LWIPBP3595Interface.h Show annotated file Show diff for this revision Revisions of this file
lwip-wifi/arch/rza1_bp3595_emac.c Show annotated file Show diff for this revision Revisions of this file
lwip-wifi/arch/rza1_bp3595_emac.h Show annotated file Show diff for this revision Revisions of this file
wifi_arch.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LWIPBP3595Interface.cpp	Tue May 24 10:34:31 2016 +0000
@@ -0,0 +1,243 @@
+/* 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
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "LWIPBP3595Interface.h"
+#include "LWIPBP3595Interface_BssType.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
+            /* Notify the EthernetInterface driver that WLAN was connected */
+            WlanBP3595_Connected();
+        } else {
+            /* Notify the EthernetInterface driver that WLAN was disconnected */
+            WlanBP3595_Disconnected();
+        }
+    }
+}
+
+static int _wlan_init() {
+    uint32_t status;
+
+    /* Initialize WlanBP3595 */
+    if (WlanBP3595_Init(&_wlan_inf_callback) != 0) {
+        return -1;
+    }
+
+    /* Wait until WLAN_BP3595_START  timeout 60s */
+    while (1) {
+        Thread::wait(200);
+        status = WlanBP3595_GetWlanSts();
+        if (status == WLAN_BP3595_START) {
+            break;
+        }
+    }
+
+    return 0;
+}
+
+static int _wlan_setting(const char *ssid, const char *pass, nsapi_security_t security)
+{
+    int     ret;
+    grp_u8  ucWidData8;     // 8bit wid data
+    grp_wld_byte_array  tBAWidData;     // byte array wid data
+
+    // Set BSS type
+    ucWidData8 = BSS_TYPE;
+    ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8);
+    if (ret != 0) {
+        return -1;
+    }
+
+    // Set SSID
+    tBAWidData.pucData = (grp_u8 *)ssid;
+    tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
+    ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData);
+    if (ret != 0) {
+        return -1;
+    }
+
+    if ((security == NSAPI_SECURITY_WPA) || (security == NSAPI_SECURITY_WPA2)) {
+        // Set PSK
+        tBAWidData.pucData = (grp_u8 *)pass;
+        tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
+        ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData);
+        if (ret != 0) {
+            return -1;
+        }
+    }
+
+    // Set 11i mode
+    switch (security) {
+        case NSAPI_SECURITY_WEP:
+            ret = strlen(pass);
+            if (ret == 5) {
+                ucWidData8 = 0x03;  // WEP64
+            } else if (ret == 13) {
+                ucWidData8 = 0x07;  // WEP128
+            } else {
+                return -1;
+            }
+            break;
+        case NSAPI_SECURITY_WPA:
+        case NSAPI_SECURITY_WPA2:
+            ucWidData8 = 0x79;  // WPA/WPA2 Mixed
+            break;
+        case NSAPI_SECURITY_NONE:
+        default:
+            ucWidData8 = 0x00;
+            break;
+    }
+    ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8);
+    if (ret != 0) {
+        return -1;
+    }
+
+    if (security == NSAPI_SECURITY_WEP) {
+        // Set WEP KEY
+        tBAWidData.pucData = (grp_u8 *)pass;
+        tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
+        ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_WEP_KEY, &tBAWidData);
+        if (ret != 0) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+// LWIPBP3595Interface 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;
+    }
+
+    return 0;
+}
+
+int LWIPBP3595Interface::disconnect()
+{
+    dhcp_release(&netif);
+    dhcp_stop(&netif);
+
+    wifi_arch_disable_interrupts();
+
+    return 0;
+}
+
+const char *LWIPBP3595Interface::get_ip_address()
+{
+    return ip_addr;
+}
+
+const char *LWIPBP3595Interface::get_mac_address()
+{
+    return mac_addr;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LWIPBP3595Interface.h	Tue May 24 10:34:31 2016 +0000
@@ -0,0 +1,66 @@
+/* LWIPBP3595Interface.h */
+/* Copyright (C) 2016 Grape Systems, Inc. */
+/* The base file is LWIPInterface.h. */
+
+/* LWIPInterface.h */
+/* LWIP implementation of NetworkInterfaceAPI
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LWIP_BP3595_INTERFACE_H
+#define LWIP_BP3595_INTERFACE_H
+
+#include "LWIPInterface.h"
+#include "WiFiInterface.h"
+
+
+/** LWIPBP3595Interface class
+ *  Implementation of the NetworkInterface for LWIP
+ */
+class LWIPBP3595Interface : public LWIPInterface
+{
+public:
+
+    /** Start the interface
+     *  @param ssid Name of the network to connect to
+     *  @param pass Security passphrase to connect to the network
+     *  @param security Type of encryption to connect with
+     *  @return 0 on success
+     */
+    virtual int connect(
+        const char *ssid,
+        const char *pass,
+        nsapi_security_t security = NSAPI_SECURITY_WPA2);
+
+    virtual int disconnect();
+
+    /** Get the internally stored IP address
+     *  @return             IP address of the interface or null if not yet connected
+     */
+    virtual const char *get_ip_address();
+ 
+    /** Get the internally stored MAC address
+     *  @return             MAC address of the interface
+     */
+    virtual const char *get_mac_address();
+
+private:
+    // Implementation of EthernetInterface
+    virtual int32_t connect() {return 0;}
+
+};
+
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwip-wifi/arch/rza1_bp3595_emac.c	Tue May 24 10:34:31 2016 +0000
@@ -0,0 +1,155 @@
+#include "lwip/opt.h"
+#include "lwip/def.h"
+#include "lwip/mem.h"
+#include "lwip/pbuf.h"
+#include "lwip/sys.h"
+#include "lwip/stats.h"
+#include "lwip/snmp.h"
+#include "lwip/tcpip.h"
+#include "netif/etharp.h"
+#include "netif/ppp_oe.h"
+#include "mbed_interface.h"
+
+#include <string.h>
+#include "rza1_bp3595_emac.h"
+#include "WlanBP3595.h"
+
+/* Static variable */
+static struct netif * volatile target_netif = NULL;
+static volatile int init_sts = 0;           /* 0: not initialized, 1:initialized */
+static int          connect_sts = 0;        /* 0: disconnected, 1:connected */
+
+/* Static function  */
+static err_t rza1_bp3595_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr);
+static err_t rza1_bp3595_low_level_output(struct netif *netif, struct pbuf *p);
+
+/* This function is called from the receiving thread of WlanBP3595 library. */
+void rza1_bp3595_input(void *buff, u16_t recv_size) {
+    struct eth_hdr  *ethhdr;
+    struct pbuf     *p;
+
+    if (recv_size != 0) {
+        p = pbuf_alloc(PBUF_RAW, recv_size, PBUF_RAM);
+        if (p != NULL) {
+            /* Copy data */
+            memcpy(p->payload, buff, recv_size);
+            /* Check Ethernet frame type */
+            ethhdr = p->payload;
+            switch (htons(ethhdr->type)) {
+                case ETHTYPE_IP:
+                case ETHTYPE_ARP:
+#if PPPOE_SUPPORT
+                case ETHTYPE_PPPOEDISC:
+                case ETHTYPE_PPPOE:
+#endif /* PPPOE_SUPPORT */
+                    /* full packet send to tcpip_thread to process */
+                    if (target_netif->input(p, target_netif) != ERR_OK) {
+                        /* Free buffer */
+                        pbuf_free(p);
+                    }
+                    break;
+                default:
+                    /* Free buffer */
+                    pbuf_free(p);
+                    break;
+            }
+        }
+    }
+}
+
+void rza1_bp3595_connected(void) {
+    /* 0: not initialized, 1:initialized */
+    if (init_sts == 1) {
+        /* 0: disconnected, 1:connected */
+        if (connect_sts == 0) {
+            tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, (void*)target_netif, 1);
+            connect_sts = 1;
+        }
+    }
+}
+
+void rza1_bp3595_disconnected(void) {
+    /* 0: not initialized, 1:initialized */
+    if (init_sts == 1) {
+        /* 0: disconnected, 1:connected */
+        if (connect_sts == 1) {
+            tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, (void*)target_netif, 1);
+            connect_sts = 0;
+        }
+    }
+}
+
+static err_t rza1_bp3595_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) {
+    if (netif->flags & NETIF_FLAG_LINK_UP) {
+        return etharp_output(netif, q, ipaddr);
+    }
+
+    return ERR_CONN;
+}
+
+static err_t rza1_bp3595_low_level_output(struct netif *netif, struct pbuf *p) {
+    err_t   err = ERR_MEM;
+    int     ret;
+
+    ret = WlanBP3595_Output(p);
+    if (ret == 0) {
+        err = ERR_OK;
+    }
+
+    return err;
+}
+
+err_t wifi_arch_enetif_init(struct netif *netif) {
+    grp_wld_byte_array  tBAWidData;     /* byte array wid data */
+    int                 ret;
+
+    /* Set MAC hardware address */
+    tBAWidData.pucData = (grp_u8 *)netif->hwaddr;
+    tBAWidData.ulSize  = 6;
+
+    ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_MAC_ADDRESS, &tBAWidData);
+    if (ret != 0) {
+        /* error(return a default MAC hardware address) */
+        netif->hwaddr[0] = 0x00;
+        netif->hwaddr[1] = 0x02;
+        netif->hwaddr[2] = 0xF7;
+        netif->hwaddr[3] = 0xF0;
+        netif->hwaddr[4] = 0x00;
+        netif->hwaddr[5] = 0x00;
+    }
+
+    /* Set MAC hardware address length */
+    netif->hwaddr_len = ETHARP_HWADDR_LEN;
+
+    /* Set maximum transfer unit */
+    netif->mtu = 1500;
+
+    /* Set device capabilities */
+    netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
+
+#if LWIP_NETIF_HOSTNAME
+    /* Initialize interface hostname */
+    netif->hostname = "lwiprza1";
+#endif /* LWIP_NETIF_HOSTNAME */
+
+    netif->name[0] = 'e';
+    netif->name[1] = 'n';
+
+    netif->output     = rza1_bp3595_etharp_output;
+    netif->linkoutput = rza1_bp3595_low_level_output;
+
+    target_netif = netif;
+
+    init_sts = 1;   /* 0: not initialized, 1:initialized */
+
+    return ERR_OK;
+}
+
+void wifi_arch_enable_interrupts(void) {
+    WlanBP3595_RecvEnable();
+}
+
+void wifi_arch_disable_interrupts(void) {
+    WlanBP3595_RecvDisable();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwip-wifi/arch/rza1_bp3595_emac.h	Tue May 24 10:34:31 2016 +0000
@@ -0,0 +1,27 @@
+#ifndef _RZA1_BP3595_EMAC_H_
+#define _RZA1_BP3595_EMAC_H_
+
+#include "lwip/opt.h"
+#include "lwip/def.h"
+#include "lwip/mem.h"
+#include "lwip/pbuf.h"
+#include "lwip/sys.h"
+#include "lwip/stats.h"
+#include "lwip/snmp.h"
+#include "netif/etharp.h"
+#include "netif/ppp_oe.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Function */
+void rza1_bp3595_input(void *buff, u16_t recv_size);
+void rza1_bp3595_connected(void);
+void rza1_bp3595_disconnected(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RZA1_BP3595_EMAC_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wifi_arch.h	Tue May 24 10:34:31 2016 +0000
@@ -0,0 +1,44 @@
+/* wifi_arch.h */
+/* Copyright (C) 2016 Grape Systems, Inc. */
+/* The base file is eth_arch.h. */
+
+/* eth_arch.h */
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+// Architecture specific WiFi interface
+// Must be implemented by each target
+
+#ifndef WIFIARCH_H_
+#define WIFIARCH_H_
+
+#include "lwip/netif.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+void wifi_arch_enable_interrupts(void);
+void wifi_arch_disable_interrupts(void);
+err_t wifi_arch_enetif_init(struct netif *netif);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // #ifndef WIFIARCH_H_
+