Daiki Kato / LWIPBP3595Interface_for_mbed-os

Dependents:   LWIPBP3595Interface_STA_for_mbed-os

Fork of LWIPBP3595Interface by Rohm

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LWIPBP3595Interface.cpp Source File

LWIPBP3595Interface.cpp

00001 /* LWIP implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "LWIPBP3595Interface.h"
00018 #include "LWIPBP3595Interface_BssType.h"
00019 #include "lwip_wifi_stack.h"
00020 #include "WlanBP3595.h"
00021 
00022 static bool _init_end = false;
00023 static grp_wld_site_survey_result_array survey_result;
00024 
00025 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) {
00026     if ((ucType == 'I') && (usWid == 0x0005)) {
00027         if (pucData[0] == 0x01) {     // CONNECTED
00028             /* Notify the LWIPBP3595Interface driver that WLAN was connected */
00029             WlanBP3595_Connected();
00030         } else {
00031             /* Notify the LWIPBP3595Interface driver that WLAN was disconnected */
00032             WlanBP3595_Disconnected();
00033         }
00034     }
00035 }
00036 
00037 static int _wlan_init() {
00038     uint32_t status;
00039     grp_u8  ucWidData8;     // 8bit wid data
00040 
00041     if (_init_end == false) {
00042         // Initialize WlanBP3595
00043         if (WlanBP3595_Init(&_wlan_inf_callback) != 0) {
00044             return -1;
00045         }
00046 
00047         // Wait until WLAN_BP3595_START  timeout 60s
00048         while (1) {
00049             Thread::wait(200);
00050             status = WlanBP3595_GetWlanSts();
00051             if (status == WLAN_BP3595_START) {
00052                 break;
00053             }
00054         }
00055 
00056         // Set BSS type
00057         ucWidData8 = BSS_TYPE;
00058         if (WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8) != 0) {
00059             return -1;
00060         }
00061 
00062         ucWidData8 = 0x02;  // Diversity
00063         WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SEL_ANTENNA, &ucWidData8);
00064         _init_end = true;
00065     }
00066 
00067     return 0;
00068 }
00069 
00070 static int _wlan_setting(const char *ssid, const char *pass, nsapi_security_t security)
00071 {
00072     int     ret;
00073     grp_u8  ucWidData8;     // 8bit wid data
00074     grp_wld_byte_array  tBAWidData;     // byte array wid data
00075 
00076     // Set SSID
00077     tBAWidData.pucData = (grp_u8 *)ssid;
00078     tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
00079     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData);
00080     if (ret != 0) {
00081         return NSAPI_ERROR_AUTH_FAILURE;
00082     }
00083 
00084     if ((security == NSAPI_SECURITY_WPA)
00085      || (security == NSAPI_SECURITY_WPA2)
00086      || (security == NSAPI_SECURITY_WPA_WPA2)) {
00087         // Set PSK
00088         tBAWidData.pucData = (grp_u8 *)pass;
00089         tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
00090         ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData);
00091         if (ret != 0) {
00092             return NSAPI_ERROR_AUTH_FAILURE;
00093         }
00094     }
00095 
00096     // Set 11i mode
00097     switch (security) {
00098         case NSAPI_SECURITY_WEP:
00099             ret = strlen(pass);
00100             if (ret == 5) {
00101                 ucWidData8 = 0x03;  // WEP64
00102             } else if (ret == 13) {
00103                 ucWidData8 = 0x07;  // WEP128
00104             } else {
00105                 return NSAPI_ERROR_PARAMETER;
00106             }
00107             break;
00108         case NSAPI_SECURITY_WPA:
00109             ucWidData8 = 0x69;  // WPA-TKIP/AES(PSK)
00110             break;
00111         case NSAPI_SECURITY_WPA2:
00112             ucWidData8 = 0x71;  // WPA2-TKIP/AES(PSK)
00113             break;
00114         case NSAPI_SECURITY_WPA_WPA2:
00115             ucWidData8 = 0x79;  // WPA/WPA2 Mixed
00116             break;
00117         case NSAPI_SECURITY_NONE:
00118         default:
00119             ucWidData8 = 0x00;
00120             break;
00121     }
00122     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8);
00123     if (ret != 0) {
00124         return NSAPI_ERROR_AUTH_FAILURE;
00125     }
00126 
00127     if (security == NSAPI_SECURITY_WEP) {
00128         // Set WEP KEY
00129         tBAWidData.pucData = (grp_u8 *)pass;
00130         tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
00131         ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_WEP_KEY, &tBAWidData);
00132         if (ret != 0) {
00133             return NSAPI_ERROR_AUTH_FAILURE;
00134         }
00135     }
00136 
00137     return 0;
00138 }
00139 
00140 /* Interface implementation */
00141 LWIPBP3595Interface::LWIPBP3595Interface()
00142     : _dhcp(true), _ip_address(), _netmask(), _gateway()
00143 {
00144 }
00145 
00146 nsapi_error_t LWIPBP3595Interface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00147 {
00148     _dhcp = false;
00149     strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
00150     strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
00151     strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
00152     return NSAPI_ERROR_OK;
00153 }
00154 
00155 nsapi_error_t LWIPBP3595Interface::set_dhcp(bool dhcp)
00156 {
00157     _dhcp = dhcp;
00158     return NSAPI_ERROR_OK;
00159 }
00160 
00161 nsapi_error_t LWIPBP3595Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
00162 {
00163     memset(_ssid, 0, sizeof(_ssid));
00164     strncpy(_ssid, ssid, sizeof(_ssid));
00165 
00166     memset(_pass, 0, sizeof(_pass));
00167     strncpy(_pass, pass, sizeof(_pass));
00168 
00169     _security = security;
00170 
00171     return 0;
00172 }
00173 
00174 nsapi_error_t LWIPBP3595Interface::set_channel(uint8_t channel)
00175 {
00176     int     ret;
00177     grp_u8  ucWidData8;     // 8bit wid data
00178 
00179     if (_wlan_init() != 0) {
00180         return NSAPI_ERROR_DEVICE_ERROR;
00181     }
00182 
00183     if (channel != 0) {
00184         ucWidData8 = channel;
00185         ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_CHANNEL, &ucWidData8);
00186         if (ret != 0) {
00187             return NSAPI_ERROR_PARAMETER;
00188         }
00189     }
00190 
00191     return 0;
00192 }
00193 
00194 int8_t LWIPBP3595Interface::get_rssi()
00195 {
00196     int     ret;
00197     grp_u8  ucWidData8;     // 8bit wid data
00198 
00199     if (_init_end) {
00200         ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_RSSI, &ucWidData8);
00201         if (ret != 0) {
00202             return 0;
00203         }
00204 
00205         return ucWidData8;
00206     }
00207     return 0;
00208 }
00209 
00210 nsapi_error_t LWIPBP3595Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel)
00211 {
00212     set_credentials(ssid, pass, security);
00213     set_channel(channel);
00214     return connect();
00215 }
00216 
00217 nsapi_error_t LWIPBP3595Interface::connect()
00218 {
00219     int ret;
00220 
00221     if (_wlan_init() != 0) {
00222         return NSAPI_ERROR_DEVICE_ERROR;
00223     }
00224 
00225     if (mbed_lwip_wifi_init(NULL) != NSAPI_ERROR_OK) {
00226         return NSAPI_ERROR_DEVICE_ERROR;
00227     }
00228 
00229     ret = _wlan_setting(_ssid, _pass, _security);
00230     if (ret != 0) {
00231         return ret;
00232     }
00233 
00234     return mbed_lwip_wifi_bringup(_dhcp,
00235             _ip_address[0] ? _ip_address : 0,
00236             _netmask[0] ? _netmask : 0,
00237             _gateway[0] ? _gateway : 0);
00238 }
00239 
00240 nsapi_error_t LWIPBP3595Interface::disconnect()
00241 {
00242     return mbed_lwip_wifi_bringdown();
00243 }
00244 
00245 nsapi_size_or_error_t LWIPBP3595Interface::scan(WiFiAccessPoint *res, unsigned count)
00246 {
00247     int     cnt;
00248     int     ret;
00249     grp_u8  ucWidData8;     // 8bit wid data
00250     nsapi_wifi_ap_t ap;
00251 
00252     if (_wlan_init() != 0) {
00253         return NSAPI_ERROR_DEVICE_ERROR;
00254     }
00255 
00256     ucWidData8 = 0x01;      // All channel
00257     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SITE_SURVEY, &ucWidData8);
00258     if (ret != 0) {
00259         return NSAPI_ERROR_DEVICE_ERROR;
00260     }
00261 
00262     ucWidData8 = 0x01;
00263     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_START_SCAN_REQ, &ucWidData8);
00264     if (ret != 0) {
00265         return NSAPI_ERROR_DEVICE_ERROR;
00266     }
00267 
00268     while (1) {
00269         ucWidData8 = 0x00;
00270         ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_CHECK_SCAN_END, &ucWidData8);
00271         if (ret != 0) {
00272             return NSAPI_ERROR_DEVICE_ERROR;
00273         }
00274         if (ucWidData8 == 0) {
00275             break;
00276         }
00277         Thread::wait(100);
00278     }
00279 
00280     ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_SITE_SURVEY_RESULT, &survey_result);
00281     if (ret != 0) {
00282         return NSAPI_ERROR_DEVICE_ERROR;
00283     }
00284 
00285     for (cnt = 0; (cnt < count) && (cnt < survey_result.iCnt); cnt++) {
00286         grp_wld_site_survey_result * wk_result = &survey_result.atResult[cnt];
00287 
00288         memcpy(ap.ssid, wk_result->aucSsid, 33);
00289         memcpy(ap.bssid, wk_result->aucBssid, 6);
00290         switch (wk_result->ucSecurity) {
00291             case 0x00:
00292                 ap.security = NSAPI_SECURITY_NONE;
00293                 break;
00294             case 0x01:
00295                 ap.security = NSAPI_SECURITY_WEP;
00296                 break;
00297             case 0x29:
00298             case 0x49:
00299             case 0x69:
00300                 ap.security = NSAPI_SECURITY_WPA;
00301                 break;
00302             case 0x31:
00303             case 0x51:
00304             case 0x71:
00305                 ap.security = NSAPI_SECURITY_WPA2;
00306                 break;
00307             case 0x79:
00308                 ap.security = NSAPI_SECURITY_WPA_WPA2;
00309                 break;
00310             default:
00311                 ap.security = NSAPI_SECURITY_UNKNOWN;
00312                 break;
00313         }
00314         ap.rssi     = wk_result->ucRxPower;
00315         ap.channel  = wk_result->ucChannel;
00316 
00317         res[cnt] = WiFiAccessPoint(ap);
00318     }
00319 
00320     return cnt;
00321 }
00322 
00323 const char *LWIPBP3595Interface::get_mac_address()
00324 {
00325     return mbed_lwip_wifi_get_mac_address();
00326 }
00327 
00328 const char *LWIPBP3595Interface::get_ip_address()
00329 {
00330     if (mbed_lwip_wifi_get_ip_address(_ip_address, sizeof _ip_address)) {
00331         return _ip_address;
00332     }
00333 
00334     return NULL;
00335 }
00336 
00337 const char *LWIPBP3595Interface::get_netmask()
00338 {
00339     if (mbed_lwip_wifi_get_netmask(_netmask, sizeof _netmask)) {
00340         return _netmask;
00341     }
00342 
00343     return NULL;
00344 }
00345 
00346 const char *LWIPBP3595Interface::get_gateway()
00347 {
00348     if (mbed_lwip_wifi_get_gateway(_gateway, sizeof _gateway)) {
00349         return _gateway;
00350     }
00351 
00352     return NULL;
00353 }
00354 
00355 NetworkStack *LWIPBP3595Interface::get_stack()
00356 {
00357     return nsapi_create_stack(&lwip_wifi_stack);
00358 }
00359