LWIPBP3595Interface library for mbed-os.
Dependents: LWIPBP3595Interface_STA_for_mbed-os
Fork of LWIPBP3595Interface by
LWIPBP3595Interface.cpp
- Committer:
- dkato
- Date:
- 2016-10-27
- Revision:
- 3:2ff2514e4fca
- Parent:
- 2:c7e325599570
- Child:
- 4:b49b4b36aaa4
File content as of revision 3:2ff2514e4fca:
/* 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 "LWIPBP3595Interface.h" #include "LWIPBP3595Interface_BssType.h" #include "lwip_wifi_stack.h" #include "WlanBP3595.h" 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 LWIPBP3595Interface driver that WLAN was connected */ WlanBP3595_Connected(); } else { /* Notify the LWIPBP3595Interface 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; } /* Interface implementation */ LWIPBP3595Interface::LWIPBP3595Interface() : _dhcp(true), _ip_address(), _netmask(), _gateway(), _ssid(NULL), _pass(NULL), _security() { } LWIPBP3595Interface::~LWIPBP3595Interface() { if (_ssid != NULL) { delete []_ssid; } if (_pass != NULL) { delete []_pass; } } int LWIPBP3595Interface::set_network(const char *ip_address, const char *netmask, const char *gateway) { _dhcp = false; strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address)); strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask)); strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway)); return 0; } int LWIPBP3595Interface::set_dhcp(bool dhcp) { _dhcp = dhcp; return 0; } int LWIPBP3595Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) { if (_ssid != NULL) { delete []_ssid; } _ssid = new char[strlen(ssid) + 1]; if (_ssid != NULL) { strcpy(_ssid, ssid); } if (_pass != NULL) { delete []_pass; } _pass = new char[strlen(pass) + 1]; if (_pass != NULL) { strcpy(_pass, pass); } _security = security; if ((_ssid == NULL) || (_pass == NULL)) { return -1; } return 0; } int LWIPBP3595Interface::set_channel(uint8_t channel) { return 0; } int8_t LWIPBP3595Interface::get_rssi() { return 0; } int LWIPBP3595Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel) { set_credentials(ssid, pass, security); set_channel(channel); return connect(); } int LWIPBP3595Interface::connect() { _wlan_init(); if (mbed_lwip_wifi_init(NULL) == 0) { _wlan_setting(_ssid, _pass, _security); } return mbed_lwip_wifi_bringup(_dhcp, _ip_address[0] ? _ip_address : 0, _netmask[0] ? _netmask : 0, _gateway[0] ? _gateway : 0); } int LWIPBP3595Interface::disconnect() { return mbed_lwip_wifi_bringdown(); } int LWIPBP3595Interface::scan(WiFiAccessPoint *res, unsigned count) { return NSAPI_ERROR_UNSUPPORTED; } const char *LWIPBP3595Interface::get_mac_address() { return mbed_lwip_wifi_get_mac_address(); } const char *LWIPBP3595Interface::get_ip_address() { if (mbed_lwip_wifi_get_ip_address(_ip_address, sizeof _ip_address)) { return _ip_address; } return 0; } const char *LWIPBP3595Interface::get_netmask() { if (mbed_lwip_wifi_get_netmask(_netmask, sizeof _netmask)) { return _netmask; } return 0; } const char *LWIPBP3595Interface::get_gateway() { if (mbed_lwip_wifi_get_gateway(_gateway, sizeof _gateway)) { return _gateway; } return 0; } NetworkStack *LWIPBP3595Interface::get_stack() { return nsapi_create_stack(&lwip_wifi_stack); }