LWIPBP3595Interface library for mbed-os.
Dependents: LWIPBP3595Interface_STA_for_mbed-os
Fork of LWIPBP3595Interface by
Diff: LWIPBP3595Interface.cpp
- Revision:
- 4:b49b4b36aaa4
- Parent:
- 3:2ff2514e4fca
- Child:
- 5:d03489ec5033
--- a/LWIPBP3595Interface.cpp Thu Oct 27 09:30:15 2016 +0000 +++ b/LWIPBP3595Interface.cpp Fri Oct 28 06:27:14 2016 +0000 @@ -19,6 +19,9 @@ #include "lwip_wifi_stack.h" #include "WlanBP3595.h" +static bool _init_end = false; +static grp_wld_site_survey_result_array survey_result; + 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 @@ -33,19 +36,32 @@ static int _wlan_init() { uint32_t status; + grp_u8 ucWidData8; // 8bit wid data - /* Initialize WlanBP3595 */ - if (WlanBP3595_Init(&_wlan_inf_callback) != 0) { - return -1; - } + if (_init_end == false) { + // 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; + // Wait until WLAN_BP3595_START timeout 60s + while (1) { + Thread::wait(200); + status = WlanBP3595_GetWlanSts(); + if (status == WLAN_BP3595_START) { + break; + } } + + // Set BSS type + ucWidData8 = BSS_TYPE; + if (WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8) != 0) { + return -1; + } + + ucWidData8 = 0x02; // Diversity + WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SEL_ANTENNA, &ucWidData8); + _init_end = true; } return 0; @@ -57,11 +73,8 @@ 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; + if (_wlan_init() != 0) { + return NSAPI_ERROR_DEVICE_ERROR; } // Set SSID @@ -69,7 +82,7 @@ tBAWidData.ulSize = strlen((char *)tBAWidData.pucData); ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData); if (ret != 0) { - return -1; + return NSAPI_ERROR_AUTH_FAILURE; } if ((security == NSAPI_SECURITY_WPA) || (security == NSAPI_SECURITY_WPA2)) { @@ -78,7 +91,7 @@ tBAWidData.ulSize = strlen((char *)tBAWidData.pucData); ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData); if (ret != 0) { - return -1; + return NSAPI_ERROR_AUTH_FAILURE; } } @@ -91,11 +104,16 @@ } else if (ret == 13) { ucWidData8 = 0x07; // WEP128 } else { - return -1; + return NSAPI_ERROR_PARAMETER; } break; case NSAPI_SECURITY_WPA: + ucWidData8 = 0x69; // WPA-TKIP/AES(PSK) + break; case NSAPI_SECURITY_WPA2: + ucWidData8 = 0x71; // WPA2-TKIP/AES(PSK) + break; + case NSAPI_SECURITY_WPA_WPA2: ucWidData8 = 0x79; // WPA/WPA2 Mixed break; case NSAPI_SECURITY_NONE: @@ -105,7 +123,7 @@ } ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8); if (ret != 0) { - return -1; + return NSAPI_ERROR_AUTH_FAILURE; } if (security == NSAPI_SECURITY_WEP) { @@ -114,7 +132,7 @@ tBAWidData.ulSize = strlen((char *)tBAWidData.pucData); ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_WEP_KEY, &tBAWidData); if (ret != 0) { - return -1; + return NSAPI_ERROR_AUTH_FAILURE; } } @@ -123,19 +141,10 @@ /* Interface implementation */ LWIPBP3595Interface::LWIPBP3595Interface() - : _dhcp(true), _ip_address(), _netmask(), _gateway(), _ssid(NULL), _pass(NULL), _security() + : _dhcp(true), _ip_address(), _netmask(), _gateway() { } -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; @@ -153,37 +162,50 @@ 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); - } + memset(_ssid, 0, sizeof(_ssid)); + strncpy(_ssid, ssid, sizeof(_ssid)); - if (_pass != NULL) { - delete []_pass; - } - _pass = new char[strlen(pass) + 1]; - if (_pass != NULL) { - strcpy(_pass, pass); - } + memset(_pass, 0, sizeof(_pass)); + strncpy(_pass, pass, sizeof(_pass)); _security = security; - if ((_ssid == NULL) || (_pass == NULL)) { - return -1; - } return 0; } int LWIPBP3595Interface::set_channel(uint8_t channel) { + int ret; + grp_u8 ucWidData8; // 8bit wid data + + if (_wlan_init() != 0) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + if (channel != 0) { + ucWidData8 = channel; + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_CHANNEL, &ucWidData8); + if (ret != 0) { + return NSAPI_ERROR_PARAMETER; + } + } + return 0; } int8_t LWIPBP3595Interface::get_rssi() { + int ret; + grp_u8 ucWidData8; // 8bit wid data + + if (_init_end) { + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_RSSI, &ucWidData8); + if (ret != 0) { + return 0; + } + + return ucWidData8; + } return 0; } @@ -196,9 +218,11 @@ int LWIPBP3595Interface::connect() { - _wlan_init(); - if (mbed_lwip_wifi_init(NULL) == 0) { - _wlan_setting(_ssid, _pass, _security); + int ret; + + ret = _wlan_setting(_ssid, _pass, _security); + if (ret != 0) { + return ret; } return mbed_lwip_wifi_bringup(_dhcp, @@ -214,7 +238,80 @@ int LWIPBP3595Interface::scan(WiFiAccessPoint *res, unsigned count) { - return NSAPI_ERROR_UNSUPPORTED; + int cnt; + int ret; + grp_u8 ucWidData8; // 8bit wid data + nsapi_wifi_ap_t ap; + + if (_wlan_init() != 0) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + ucWidData8 = 0x01; // All channel + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SITE_SURVEY, &ucWidData8); + if (ret != 0) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + ucWidData8 = 0x01; + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_START_SCAN_REQ, &ucWidData8); + if (ret != 0) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + while (1) { + ucWidData8 = 0x00; + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_CHECK_SCAN_END, &ucWidData8); + if (ret != 0) { + return NSAPI_ERROR_DEVICE_ERROR; + } + if (ucWidData8 == 0) { + break; + } + Thread::wait(100); + } + + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_SITE_SURVEY_RESULT, &survey_result); + if (ret != 0) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + for (cnt = 0; (cnt < count) && (cnt < survey_result.iCnt); cnt++) { + grp_wld_site_survey_result * wk_result = &survey_result.atResult[cnt]; + + memcpy(ap.ssid, wk_result->aucSsid, 33); + memcpy(ap.bssid, wk_result->aucBssid, 6); + switch (wk_result->ucSecurity) { + case 0x00: + ap.security = NSAPI_SECURITY_NONE; + break; + case 0x01: + ap.security = NSAPI_SECURITY_WEP; + break; + case 0x29: + case 0x49: + case 0x69: + ap.security = NSAPI_SECURITY_WPA; + break; + case 0x31: + case 0x51: + case 0x71: + ap.security = NSAPI_SECURITY_WPA2; + break; + case 0x79: + ap.security = NSAPI_SECURITY_WPA_WPA2; + break; + default: + ap.security = NSAPI_SECURITY_UNKNOWN; + break; + } + ap.rssi = wk_result->ucRxPower; + ap.channel = wk_result->ucChannel; + + res[cnt] = WiFiAccessPoint(ap); + } + + return cnt; } const char *LWIPBP3595Interface::get_mac_address() @@ -253,3 +350,4 @@ { return nsapi_create_stack(&lwip_wifi_stack); } +