|
| 1 | +/* Copyright (c) 2019 Unisoc Communications Inc. |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "WiFiInterface.h" |
| 18 | +#include "RdaWiFiInterface.h" |
| 19 | +#include "rda5991h_wland.h" |
| 20 | +#include "nsapi_types.h" |
| 21 | +#include "wland_types.h" |
| 22 | +#include "rda_sys_wrapper.h" |
| 23 | + |
| 24 | +nsapi_error_t RDAWiFiInterface::set_channel(uint8_t channel) |
| 25 | +{ |
| 26 | + int ret= 0; |
| 27 | + init(); |
| 28 | + |
| 29 | + if (channel > 13) |
| 30 | + return NSAPI_ERROR_PARAMETER; |
| 31 | + |
| 32 | + if (channel == 0) { |
| 33 | + _channel = 0; |
| 34 | + return NSAPI_ERROR_OK; |
| 35 | + } |
| 36 | + |
| 37 | + ret = rda5981_set_channel(channel); |
| 38 | + if (ret == 0) { |
| 39 | + _channel = channel; |
| 40 | + return NSAPI_ERROR_OK; |
| 41 | + } else { |
| 42 | + return NSAPI_ERROR_TIMEOUT; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +int8_t RDAWiFiInterface::get_rssi() |
| 47 | +{ |
| 48 | + return rda5981_get_rssi(); |
| 49 | +} |
| 50 | + |
| 51 | +nsapi_error_t RDAWiFiInterface::init() |
| 52 | +{ |
| 53 | + if (!_interface) { |
| 54 | + if (!_emac.power_up()) { |
| 55 | + LWIP_DEBUGF(NETIF_DEBUG,"power up failed!\n"); |
| 56 | + } |
| 57 | + nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface); |
| 58 | + if (err != NSAPI_ERROR_OK) { |
| 59 | + _interface = NULL; |
| 60 | + return err; |
| 61 | + } |
| 62 | + _interface->attach(_connection_status_cb); |
| 63 | + } |
| 64 | + return NSAPI_ERROR_OK; |
| 65 | +} |
| 66 | + |
| 67 | +nsapi_error_t RDAWiFiInterface::set_credentials(const char *ssid, const char *pass, |
| 68 | + nsapi_security_t security) |
| 69 | +{ |
| 70 | + if (ssid == 0 || strlen(ssid) == 0) { |
| 71 | + return NSAPI_ERROR_PARAMETER; |
| 72 | + } |
| 73 | + if (security != NSAPI_SECURITY_NONE && (pass == 0 || strlen(pass) == 0)) { |
| 74 | + return NSAPI_ERROR_PARAMETER; |
| 75 | + } |
| 76 | + if (strlen(ssid) > 32 || strlen(pass) > 63) { |
| 77 | + return NSAPI_ERROR_PARAMETER; |
| 78 | + } |
| 79 | + memcpy((void*)_ssid, (void*)ssid, strlen(ssid)); |
| 80 | + _ssid[strlen(ssid)] = '\0'; |
| 81 | + memcpy((void*)_pass, (void*)pass, strlen(pass)); |
| 82 | + _pass[strlen(pass)] = '\0'; |
| 83 | + _security = security; |
| 84 | + return NSAPI_ERROR_OK; |
| 85 | +} |
| 86 | + |
| 87 | +nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass, |
| 88 | + nsapi_security_t security, uint8_t channel) |
| 89 | +{ |
| 90 | + rda_msg msg; |
| 91 | + bool find = false; |
| 92 | + int i = 0; |
| 93 | + rda5981_scan_result bss; |
| 94 | + int ret = 0; |
| 95 | + |
| 96 | + if (ssid == NULL || ssid[0] == 0) { |
| 97 | + return NSAPI_ERROR_PARAMETER; |
| 98 | + } |
| 99 | + |
| 100 | + init(); |
| 101 | + |
| 102 | + if(rda5981_check_scan_result_name(ssid) != 0) { |
| 103 | + for (i = 0; i< 5; i++) { |
| 104 | + rda5981_scan(NULL, 0, 0); |
| 105 | + if(rda5981_check_scan_result_name(ssid) == 0) { |
| 106 | + find = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + } else { |
| 111 | + find = true; |
| 112 | + } |
| 113 | + |
| 114 | + if (find == false) { |
| 115 | + LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n"); |
| 116 | + return NSAPI_ERROR_CONNECTION_TIMEOUT; |
| 117 | + } |
| 118 | + bss.channel = 15; |
| 119 | + rda5981_get_scan_result_name(&bss, ssid); |
| 120 | + if ((channel !=0) && (bss.channel != channel)) { |
| 121 | + LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n"); |
| 122 | + return NSAPI_ERROR_CONNECTION_TIMEOUT; |
| 123 | + } |
| 124 | + |
| 125 | + memcpy(gssid, ssid, strlen(ssid)); |
| 126 | + if (pass[0] != 0) { |
| 127 | + memcpy(gpass, pass, strlen(pass)); |
| 128 | + } |
| 129 | + memset(gbssid, 0, NSAPI_MAC_BYTES); |
| 130 | + gssid[strlen(ssid)] = gpass[strlen(pass)] = '\0'; |
| 131 | + |
| 132 | + msg.type = WLAND_CONNECT; |
| 133 | + rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever); |
| 134 | + ret = rda_sem_wait(wifi_auth_sem, 10000); |
| 135 | + if (ret) { |
| 136 | + return NSAPI_ERROR_CONNECTION_TIMEOUT; |
| 137 | + } |
| 138 | + |
| 139 | + ret = _interface->bringup(_dhcp, |
| 140 | + _ip_address[0] ? _ip_address : 0, |
| 141 | + _netmask[0] ? _netmask : 0, |
| 142 | + _gateway[0] ? _gateway : 0, |
| 143 | + DEFAULT_STACK, |
| 144 | + _blocking); |
| 145 | + |
| 146 | + return ret; |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +nsapi_error_t RDAWiFiInterface::connect() |
| 151 | +{ |
| 152 | + return connect(_ssid, _pass, _security, _channel); |
| 153 | +} |
| 154 | + |
| 155 | +nsapi_error_t RDAWiFiInterface::disconnect() |
| 156 | +{ |
| 157 | + rda_msg msg; |
| 158 | + |
| 159 | + if(sta_state < 2) { |
| 160 | + return NSAPI_ERROR_NO_CONNECTION; |
| 161 | + } |
| 162 | + msg.type = WLAND_DISCONNECT; |
| 163 | + rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever); |
| 164 | + if (_interface) { |
| 165 | + return _interface->bringdown(); |
| 166 | + } |
| 167 | + |
| 168 | + return NSAPI_ERROR_NO_CONNECTION; |
| 169 | +} |
| 170 | + |
| 171 | +nsapi_size_or_error_t RDAWiFiInterface::scan(WiFiAccessPoint *res, nsapi_size_t count) |
| 172 | +{ |
| 173 | + int bss_num = 0, i; |
| 174 | + rda5981_scan_result *bss; |
| 175 | + nsapi_wifi_ap_t ap; |
| 176 | + |
| 177 | + init(); |
| 178 | + |
| 179 | + rda5981_scan(NULL, 0, 0); |
| 180 | + bss_num = rda5981_get_scan_num(); |
| 181 | + if (count != 0) { |
| 182 | + bss_num = (bss_num < count) ? bss_num : count; |
| 183 | + } |
| 184 | + if (res) { |
| 185 | + bss = (rda5981_scan_result *)malloc(bss_num * sizeof(rda5981_scan_result)); |
| 186 | + rda5981_get_scan_result(bss, bss_num); |
| 187 | + for (i=0; i<bss_num; i++) { |
| 188 | + memset(&ap, 0, sizeof(nsapi_wifi_ap_t)); |
| 189 | + memcpy(ap.bssid, bss[i].BSSID, 6); |
| 190 | + memcpy(ap.ssid, bss[i].SSID, bss[i].SSID_len); |
| 191 | + ap.channel = bss[i].channel; |
| 192 | + ap.rssi = bss[i].RSSI; |
| 193 | + if (bss[i].secure_type == ENCRYPT_NONE) { |
| 194 | + ap.security = NSAPI_SECURITY_NONE; |
| 195 | + } else if(bss[i].secure_type & ENCRYPT_WEP) { |
| 196 | + ap.security = NSAPI_SECURITY_WEP; |
| 197 | + } else if((bss[i].secure_type & (ENCRYPT_WPA_TKIP | ENCRYPT_WPA_CCMP)) && \ |
| 198 | + (bss[i].secure_type & (ENCRYPT_WPA2_TKIP | ENCRYPT_WPA2_CCMP))) { |
| 199 | + ap.security = NSAPI_SECURITY_WPA_WPA2; |
| 200 | + } else if((bss[i].secure_type & (ENCRYPT_WPA_TKIP | ENCRYPT_WPA_CCMP))) { |
| 201 | + ap.security = NSAPI_SECURITY_WPA; |
| 202 | + } else { |
| 203 | + ap.security = NSAPI_SECURITY_WPA2; |
| 204 | + } |
| 205 | + WiFiAccessPoint ap_temp(ap); |
| 206 | + memcpy(&res[i], &ap_temp, sizeof(WiFiAccessPoint)); |
| 207 | + } |
| 208 | + free(bss); |
| 209 | + } |
| 210 | + return bss_num; |
| 211 | + |
| 212 | +} |
| 213 | + |
| 214 | +WiFiInterface *WiFiInterface::get_default_instance() { |
| 215 | + static RDAWiFiInterface wifinet; |
| 216 | + return &wifinet; |
| 217 | +} |
0 commit comments