Rohm / LWIPBP3595Interface

Dependents:   LWIPBP3595Interface_STA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LWIPBP3595Interface.cpp Source File

LWIPBP3595Interface.cpp

00001 /* LWIPBP3595Interface.cpp */
00002 /* Copyright (C) 2016 Grape Systems, Inc. */
00003 /* The base file is LWIPInterface.cpp. */
00004 
00005 /* LWIPInterface.cpp */
00006 /* LWIP implementation of NetworkInterfaceAPI
00007  * Copyright (c) 2015 ARM Limited
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "mbed.h"
00023 #include "LWIPBP3595Interface.h"
00024 #include "LWIPBP3595Interface_BssType.h"
00025 #include "WlanBP3595.h"
00026 
00027 #include "lwip/inet.h"
00028 #include "lwip/netif.h"
00029 #include "lwip/dhcp.h"
00030 #include "lwip/tcpip.h"
00031 #include "lwip/sockets.h"
00032 #include "lwip/netdb.h"
00033 #include "netif/etharp.h"
00034 #include "wifi_arch.h"
00035 #include "lwip/netif.h"
00036 #include "lwip/udp.h"
00037 #include "lwip/tcp.h"
00038 #include "lwip/tcp_impl.h"
00039 #include "lwip/timers.h"
00040 #include "lwip/dns.h"
00041 #include "lwip/def.h"
00042 #include "lwip/ip_addr.h"
00043 
00044 #define LWIP_TIMEOUT 180000
00045 
00046 /* TCP/IP and Network Interface Initialisation */
00047 static struct netif netif;
00048 
00049 static char ip_addr[NSAPI_IP_SIZE] = "\0";
00050 static char mac_addr[NSAPI_MAC_SIZE] = "\0";
00051 
00052 static Semaphore tcpip_inited(0);
00053 static Semaphore netif_linked(0);
00054 static Semaphore netif_up(0);
00055 
00056 static void tcpip_init_irq(void *)
00057 {
00058     tcpip_inited.release();
00059 }
00060 
00061 static void netif_link_irq(struct netif *netif)
00062 {
00063     if (netif_is_link_up(netif)) {
00064         netif_linked.release();
00065     }
00066 }
00067 
00068 static void netif_status_irq(struct netif *netif)
00069 {
00070     if (netif_is_up(netif)) {
00071         strcpy(ip_addr, inet_ntoa(netif->ip_addr));
00072         netif_up.release();
00073     }
00074 }
00075 
00076 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw)
00077 {
00078     tcpip_init(tcpip_init_irq, NULL);
00079     tcpip_inited.wait();
00080 
00081     memset((void*) &netif, 0, sizeof(netif));
00082     netif_add(&netif, ipaddr, netmask, gw, NULL, wifi_arch_enetif_init, tcpip_input);
00083     netif_set_default(&netif);
00084 
00085     netif_set_link_callback  (&netif, netif_link_irq);
00086     netif_set_status_callback(&netif, netif_status_irq);
00087 }
00088 
00089 static void set_mac_address(void)
00090 {
00091     snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1],
00092              netif.hwaddr[2], netif.hwaddr[3], netif.hwaddr[4], netif.hwaddr[5]);
00093 }
00094 
00095 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) {
00096     if ((ucType == 'I') && (usWid == 0x0005)) {
00097         if (pucData[0] == 0x01) {     // CONNECTED
00098             /* Notify the EthernetInterface driver that WLAN was connected */
00099             WlanBP3595_Connected();
00100         } else {
00101             /* Notify the EthernetInterface driver that WLAN was disconnected */
00102             WlanBP3595_Disconnected();
00103         }
00104     }
00105 }
00106 
00107 static int _wlan_init() {
00108     uint32_t status;
00109 
00110     /* Initialize WlanBP3595 */
00111     if (WlanBP3595_Init(&_wlan_inf_callback) != 0) {
00112         return -1;
00113     }
00114 
00115     /* Wait until WLAN_BP3595_START  timeout 60s */
00116     while (1) {
00117         Thread::wait(200);
00118         status = WlanBP3595_GetWlanSts();
00119         if (status == WLAN_BP3595_START) {
00120             break;
00121         }
00122     }
00123 
00124     return 0;
00125 }
00126 
00127 static int _wlan_setting(const char *ssid, const char *pass, nsapi_security_t security)
00128 {
00129     int     ret;
00130     grp_u8  ucWidData8;     // 8bit wid data
00131     grp_wld_byte_array  tBAWidData;     // byte array wid data
00132 
00133     // Set BSS type
00134     ucWidData8 = BSS_TYPE;
00135     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8);
00136     if (ret != 0) {
00137         return -1;
00138     }
00139 
00140     // Set SSID
00141     tBAWidData.pucData = (grp_u8 *)ssid;
00142     tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
00143     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData);
00144     if (ret != 0) {
00145         return -1;
00146     }
00147 
00148     if ((security == NSAPI_SECURITY_WPA) || (security == NSAPI_SECURITY_WPA2)) {
00149         // Set PSK
00150         tBAWidData.pucData = (grp_u8 *)pass;
00151         tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
00152         ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData);
00153         if (ret != 0) {
00154             return -1;
00155         }
00156     }
00157 
00158     // Set 11i mode
00159     switch (security) {
00160         case NSAPI_SECURITY_WEP:
00161             ret = strlen(pass);
00162             if (ret == 5) {
00163                 ucWidData8 = 0x03;  // WEP64
00164             } else if (ret == 13) {
00165                 ucWidData8 = 0x07;  // WEP128
00166             } else {
00167                 return -1;
00168             }
00169             break;
00170         case NSAPI_SECURITY_WPA:
00171         case NSAPI_SECURITY_WPA2:
00172             ucWidData8 = 0x79;  // WPA/WPA2 Mixed
00173             break;
00174         case NSAPI_SECURITY_NONE:
00175         default:
00176             ucWidData8 = 0x00;
00177             break;
00178     }
00179     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8);
00180     if (ret != 0) {
00181         return -1;
00182     }
00183 
00184     if (security == NSAPI_SECURITY_WEP) {
00185         // Set WEP KEY
00186         tBAWidData.pucData = (grp_u8 *)pass;
00187         tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
00188         ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_WEP_KEY, &tBAWidData);
00189         if (ret != 0) {
00190             return -1;
00191         }
00192     }
00193 
00194     return 0;
00195 }
00196 
00197 // LWIPBP3595Interface implementation
00198 int LWIPBP3595Interface::connect(
00199     const char *ssid, 
00200     const char *pass, 
00201     nsapi_security_t security)
00202 {
00203     _wlan_init();
00204 
00205     // Set up network
00206     init_netif(0, 0, 0);
00207     set_mac_address();
00208 
00209     _wlan_setting(ssid, pass, security);
00210 
00211     // Connect to network
00212     wifi_arch_enable_interrupts();
00213 
00214     dhcp_start(&netif);
00215 
00216     // Wait for an IP Address
00217     // -1: error, 0: timeout
00218     if (netif_up.wait(LWIP_TIMEOUT) < 0) {
00219         return NSAPI_ERROR_DHCP_FAILURE;
00220     }
00221 
00222     return 0;
00223 }
00224 
00225 int LWIPBP3595Interface::disconnect()
00226 {
00227     dhcp_release(&netif);
00228     dhcp_stop(&netif);
00229 
00230     wifi_arch_disable_interrupts();
00231 
00232     return 0;
00233 }
00234 
00235 const char *LWIPBP3595Interface::get_ip_address()
00236 {
00237     return ip_addr;
00238 }
00239 
00240 const char *LWIPBP3595Interface::get_mac_address()
00241 {
00242     return mac_addr;
00243 }