mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
181:57724642e740
Parent:
180:96ed750bd169
Child:
184:08ed48f1de7f
mbed-dev library. Release version 159.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /* Ameba implementation of NetworkInterfaceAPI
AnnaBridge 167:e84263d55307 2 * Copyright (c) 2015 ARM Limited
AnnaBridge 167:e84263d55307 3 *
AnnaBridge 167:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 167:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 167:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 167:e84263d55307 7 *
AnnaBridge 167:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 167:e84263d55307 9 *
AnnaBridge 167:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 167:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 167:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 167:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 167:e84263d55307 14 * limitations under the License.
AnnaBridge 167:e84263d55307 15 */
AnnaBridge 167:e84263d55307 16
AnnaBridge 167:e84263d55307 17 #include "mbed.h"
AnnaBridge 167:e84263d55307 18 #include "rtos.h"
AnnaBridge 167:e84263d55307 19
AnnaBridge 167:e84263d55307 20 #include "RTWInterface.h"
AnnaBridge 167:e84263d55307 21 #include "mbed_interface.h"
AnnaBridge 167:e84263d55307 22 #include "rtw_emac.h"
AnnaBridge 167:e84263d55307 23
AnnaBridge 167:e84263d55307 24 #include "wifi_constants.h"
AnnaBridge 167:e84263d55307 25 #include "wifi_conf.h"
AnnaBridge 167:e84263d55307 26 #include "lwip_stack.h"
AnnaBridge 167:e84263d55307 27
AnnaBridge 167:e84263d55307 28 #include "osdep_service.h"
AnnaBridge 167:e84263d55307 29
AnnaBridge 167:e84263d55307 30 typedef struct _wifi_scan_hdl {
AnnaBridge 174:b96e65c34a4d 31 _sema scan_sema;
AnnaBridge 167:e84263d55307 32 nsapi_size_t ap_num;
AnnaBridge 167:e84263d55307 33 nsapi_size_t scan_num;
AnnaBridge 167:e84263d55307 34 WiFiAccessPoint *ap_details;
AnnaBridge 167:e84263d55307 35 } wifi_scan_hdl;
AnnaBridge 167:e84263d55307 36
AnnaBridge 167:e84263d55307 37 #define MAX_SCAN_TIMEOUT (15000)
AnnaBridge 167:e84263d55307 38
Anna Bridge 180:96ed750bd169 39 static bool _inited = false;
Anna Bridge 180:96ed750bd169 40
AnnaBridge 167:e84263d55307 41 static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
AnnaBridge 167:e84263d55307 42 {
AnnaBridge 167:e84263d55307 43 wifi_scan_hdl *scan_handler = (wifi_scan_hdl *)malloced_scan_result->user_data;
AnnaBridge 167:e84263d55307 44 if (malloced_scan_result->scan_complete != RTW_TRUE) {
AnnaBridge 167:e84263d55307 45 if(scan_handler->ap_details && scan_handler->scan_num > scan_handler->ap_num){
Anna Bridge 180:96ed750bd169 46 nsapi_wifi_ap_t ap;
AnnaBridge 167:e84263d55307 47 rtw_scan_result_t* record = &malloced_scan_result->ap_details;
Anna Bridge 180:96ed750bd169 48 record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
AnnaBridge 167:e84263d55307 49 memset((void*)&ap, 0x00, sizeof(nsapi_wifi_ap_t));
AnnaBridge 167:e84263d55307 50 memcpy(ap.ssid, record->SSID.val, record->SSID.len);
AnnaBridge 167:e84263d55307 51 memcpy(ap.bssid, record->BSSID.octet, 6);
AnnaBridge 167:e84263d55307 52 switch (record->security){
AnnaBridge 167:e84263d55307 53 case RTW_SECURITY_OPEN:
AnnaBridge 167:e84263d55307 54 ap.security = NSAPI_SECURITY_NONE;
AnnaBridge 167:e84263d55307 55 break;
AnnaBridge 167:e84263d55307 56 case RTW_SECURITY_WEP_PSK:
AnnaBridge 167:e84263d55307 57 case RTW_SECURITY_WEP_SHARED:
AnnaBridge 167:e84263d55307 58 ap.security = NSAPI_SECURITY_WEP;
AnnaBridge 167:e84263d55307 59 break;
AnnaBridge 167:e84263d55307 60 case RTW_SECURITY_WPA_TKIP_PSK:
AnnaBridge 167:e84263d55307 61 case RTW_SECURITY_WPA_AES_PSK:
AnnaBridge 167:e84263d55307 62 ap.security = NSAPI_SECURITY_WPA;
AnnaBridge 167:e84263d55307 63 break;
AnnaBridge 167:e84263d55307 64 case RTW_SECURITY_WPA2_AES_PSK:
AnnaBridge 167:e84263d55307 65 case RTW_SECURITY_WPA2_TKIP_PSK:
AnnaBridge 167:e84263d55307 66 case RTW_SECURITY_WPA2_MIXED_PSK:
AnnaBridge 167:e84263d55307 67 ap.security = NSAPI_SECURITY_WPA2;
AnnaBridge 167:e84263d55307 68 break;
AnnaBridge 167:e84263d55307 69 case RTW_SECURITY_WPA_WPA2_MIXED:
AnnaBridge 167:e84263d55307 70 ap.security = NSAPI_SECURITY_WPA_WPA2;
AnnaBridge 167:e84263d55307 71 break;
AnnaBridge 167:e84263d55307 72 default:
AnnaBridge 167:e84263d55307 73 ap.security = NSAPI_SECURITY_UNKNOWN;
AnnaBridge 167:e84263d55307 74 break;
AnnaBridge 167:e84263d55307 75 }
AnnaBridge 167:e84263d55307 76 ap.rssi = record->signal_strength;
AnnaBridge 167:e84263d55307 77 ap.channel = record->channel;
AnnaBridge 172:7d866c31b3c5 78 scan_handler->ap_details[scan_handler->ap_num] = WiFiAccessPoint(ap);
AnnaBridge 167:e84263d55307 79 }
AnnaBridge 167:e84263d55307 80 scan_handler->ap_num++;
AnnaBridge 167:e84263d55307 81 } else{
AnnaBridge 167:e84263d55307 82 // scan done
AnnaBridge 167:e84263d55307 83 rtw_up_sema(&scan_handler->scan_sema);
AnnaBridge 167:e84263d55307 84 }
AnnaBridge 167:e84263d55307 85 return RTW_SUCCESS;
AnnaBridge 167:e84263d55307 86 }
AnnaBridge 167:e84263d55307 87
Kojto 169:e3b6fe271b81 88 RTWInterface::RTWInterface(bool debug)
AnnaBridge 167:e84263d55307 89 : _dhcp(true), _ip_address(), _netmask(), _gateway()
AnnaBridge 167:e84263d55307 90 {
AnnaBridge 167:e84263d55307 91 emac_interface_t *emac;
AnnaBridge 167:e84263d55307 92 int ret;
Anna Bridge 180:96ed750bd169 93 extern u32 GlobalDebugEnable;
AnnaBridge 167:e84263d55307 94
Kojto 169:e3b6fe271b81 95 GlobalDebugEnable = debug?1:0;
AnnaBridge 167:e84263d55307 96 emac = wlan_emac_init_interface();
AnnaBridge 167:e84263d55307 97 if (!emac) {
AnnaBridge 167:e84263d55307 98 printf("Error init RTWInterface!\r\n");
AnnaBridge 167:e84263d55307 99 return;
AnnaBridge 167:e84263d55307 100 }
AnnaBridge 167:e84263d55307 101 emac->ops.power_up(emac);
Anna Bridge 180:96ed750bd169 102 if (_inited == false) {
Anna Bridge 180:96ed750bd169 103 ret = mbed_lwip_init(emac);
Anna Bridge 180:96ed750bd169 104 if (ret != 0) {
Anna Bridge 180:96ed750bd169 105 printf("Error init RTWInterface!(%d)\r\n", ret);
Anna Bridge 180:96ed750bd169 106 return;
Anna Bridge 180:96ed750bd169 107 }
Anna Bridge 180:96ed750bd169 108 _inited = true;
AnnaBridge 167:e84263d55307 109 }
AnnaBridge 167:e84263d55307 110 }
AnnaBridge 167:e84263d55307 111
AnnaBridge 167:e84263d55307 112 RTWInterface::~RTWInterface()
AnnaBridge 167:e84263d55307 113 {
AnnaBridge 167:e84263d55307 114 wlan_emac_link_change(false);
AnnaBridge 167:e84263d55307 115 mbed_lwip_bringdown();
AnnaBridge 167:e84263d55307 116 }
AnnaBridge 167:e84263d55307 117
AnnaBridge 167:e84263d55307 118 nsapi_error_t RTWInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
AnnaBridge 167:e84263d55307 119 {
AnnaBridge 167:e84263d55307 120 _dhcp = false;
AnnaBridge 167:e84263d55307 121 strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
AnnaBridge 167:e84263d55307 122 strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
AnnaBridge 167:e84263d55307 123 strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
AnnaBridge 167:e84263d55307 124 return NSAPI_ERROR_OK;
AnnaBridge 167:e84263d55307 125 }
AnnaBridge 167:e84263d55307 126
AnnaBridge 167:e84263d55307 127 nsapi_error_t RTWInterface::set_dhcp(bool dhcp)
AnnaBridge 167:e84263d55307 128 {
AnnaBridge 167:e84263d55307 129 _dhcp = dhcp;
AnnaBridge 167:e84263d55307 130 return NSAPI_ERROR_OK;
AnnaBridge 167:e84263d55307 131 }
AnnaBridge 167:e84263d55307 132
AnnaBridge 167:e84263d55307 133 /*
AnnaBridge 167:e84263d55307 134 * we may call connect multiple times
AnnaBridge 167:e84263d55307 135 */
AnnaBridge 167:e84263d55307 136 nsapi_error_t RTWInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
AnnaBridge 167:e84263d55307 137 {
Anna Bridge 180:96ed750bd169 138 if(!ssid) {
Anna Bridge 180:96ed750bd169 139 return NSAPI_ERROR_PARAMETER;
Anna Bridge 180:96ed750bd169 140 }
Anna Bridge 180:96ed750bd169 141
Anna Bridge 180:96ed750bd169 142 switch (security) {
Anna Bridge 180:96ed750bd169 143 case NSAPI_SECURITY_WPA:
Anna Bridge 180:96ed750bd169 144 case NSAPI_SECURITY_WPA2:
Anna Bridge 180:96ed750bd169 145 case NSAPI_SECURITY_WPA_WPA2:
Anna Bridge 180:96ed750bd169 146 case NSAPI_SECURITY_WEP:
Anna Bridge 180:96ed750bd169 147 if((strlen(pass) < 8) || (strlen(pass) > 63)) { // 802.11 password 8-63 characters
Anna Bridge 180:96ed750bd169 148 return NSAPI_ERROR_PARAMETER;
Anna Bridge 180:96ed750bd169 149 }
Anna Bridge 180:96ed750bd169 150 break;
Anna Bridge 180:96ed750bd169 151 case NSAPI_SECURITY_NONE:
Anna Bridge 180:96ed750bd169 152 break;
Anna Bridge 180:96ed750bd169 153 default:
Anna Bridge 180:96ed750bd169 154 return NSAPI_ERROR_PARAMETER;
Anna Bridge 180:96ed750bd169 155 }
Anna Bridge 180:96ed750bd169 156
AnnaBridge 167:e84263d55307 157 strncpy(_ssid, ssid, 255);
AnnaBridge 167:e84263d55307 158 strncpy(_pass, pass, 255);
AnnaBridge 167:e84263d55307 159 _security = security;
AnnaBridge 167:e84263d55307 160
AnnaBridge 167:e84263d55307 161 return NSAPI_ERROR_OK;
AnnaBridge 167:e84263d55307 162 }
AnnaBridge 167:e84263d55307 163
AnnaBridge 167:e84263d55307 164 nsapi_error_t RTWInterface::connect()
AnnaBridge 167:e84263d55307 165 {
AnnaBridge 167:e84263d55307 166 int ret;
AnnaBridge 167:e84263d55307 167 rtw_security_t sec;
AnnaBridge 167:e84263d55307 168
AnnaBridge 167:e84263d55307 169 if (!_ssid || (!_pass && _security != NSAPI_SECURITY_NONE)) {
AnnaBridge 167:e84263d55307 170 printf("Invalid credentials\r\n");
AnnaBridge 167:e84263d55307 171 return NSAPI_ERROR_PARAMETER;
AnnaBridge 167:e84263d55307 172 }
AnnaBridge 167:e84263d55307 173
AnnaBridge 167:e84263d55307 174 switch (_security) {
AnnaBridge 167:e84263d55307 175 case NSAPI_SECURITY_WPA:
AnnaBridge 167:e84263d55307 176 case NSAPI_SECURITY_WPA2:
AnnaBridge 167:e84263d55307 177 case NSAPI_SECURITY_WPA_WPA2:
AnnaBridge 167:e84263d55307 178 sec = RTW_SECURITY_WPA_WPA2_MIXED;
AnnaBridge 167:e84263d55307 179 break;
AnnaBridge 167:e84263d55307 180 case NSAPI_SECURITY_WEP:
AnnaBridge 167:e84263d55307 181 sec = RTW_SECURITY_WEP_PSK;
AnnaBridge 167:e84263d55307 182 break;
AnnaBridge 167:e84263d55307 183 case NSAPI_SECURITY_NONE:
AnnaBridge 167:e84263d55307 184 sec = RTW_SECURITY_OPEN;
Anna Bridge 180:96ed750bd169 185 break;
AnnaBridge 167:e84263d55307 186 default:
AnnaBridge 167:e84263d55307 187 return NSAPI_ERROR_PARAMETER;
AnnaBridge 167:e84263d55307 188 }
AnnaBridge 167:e84263d55307 189
AnnaBridge 167:e84263d55307 190 if(_channel > 0 && _channel < 14){
AnnaBridge 167:e84263d55307 191 uint8_t pscan_config = PSCAN_ENABLE;
AnnaBridge 167:e84263d55307 192 wifi_set_pscan_chan(&_channel, &pscan_config, 1);
AnnaBridge 167:e84263d55307 193 }
Anna Bridge 180:96ed750bd169 194
AnnaBridge 167:e84263d55307 195 ret = wifi_connect(_ssid, sec, _pass, strlen(_ssid), strlen(_pass), 0, (void *)NULL);
AnnaBridge 167:e84263d55307 196 if (ret != RTW_SUCCESS) {
AnnaBridge 167:e84263d55307 197 printf("failed: %d\r\n", ret);
AnnaBridge 167:e84263d55307 198 return NSAPI_ERROR_NO_CONNECTION;
AnnaBridge 167:e84263d55307 199 }
AnnaBridge 167:e84263d55307 200
AnnaBridge 167:e84263d55307 201 wlan_emac_link_change(true);
AnnaBridge 167:e84263d55307 202 return mbed_lwip_bringup(_dhcp,
AnnaBridge 167:e84263d55307 203 _ip_address[0] ? _ip_address : 0,
AnnaBridge 167:e84263d55307 204 _netmask[0] ? _netmask : 0,
AnnaBridge 167:e84263d55307 205 _gateway[0] ? _gateway : 0);
AnnaBridge 167:e84263d55307 206 }
AnnaBridge 167:e84263d55307 207
AnnaBridge 167:e84263d55307 208 nsapi_error_t RTWInterface::scan(WiFiAccessPoint *res, unsigned count)
AnnaBridge 167:e84263d55307 209 {
AnnaBridge 167:e84263d55307 210 static wifi_scan_hdl scan_handler;
AnnaBridge 167:e84263d55307 211 scan_handler.ap_num = 0;
AnnaBridge 167:e84263d55307 212 if(!scan_handler.scan_sema)
AnnaBridge 167:e84263d55307 213 rtw_init_sema(&scan_handler.scan_sema, 0);
AnnaBridge 167:e84263d55307 214 scan_handler.scan_num = count;
AnnaBridge 167:e84263d55307 215 scan_handler.ap_details = res;
AnnaBridge 167:e84263d55307 216 if(wifi_scan_networks(scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS){
AnnaBridge 167:e84263d55307 217 printf("wifi scan failed\n\r");
AnnaBridge 167:e84263d55307 218 return NSAPI_ERROR_DEVICE_ERROR;
AnnaBridge 167:e84263d55307 219 }
AnnaBridge 167:e84263d55307 220 if(rtw_down_timeout_sema( &scan_handler.scan_sema, MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
AnnaBridge 167:e84263d55307 221 printf("wifi scan timeout\r\n");
AnnaBridge 167:e84263d55307 222 return NSAPI_ERROR_DEVICE_ERROR;
AnnaBridge 167:e84263d55307 223 }
AnnaBridge 167:e84263d55307 224 if(count <= 0 || count > scan_handler.ap_num)
AnnaBridge 167:e84263d55307 225 count = scan_handler.ap_num;
AnnaBridge 167:e84263d55307 226
AnnaBridge 167:e84263d55307 227 return count;
AnnaBridge 167:e84263d55307 228 }
AnnaBridge 167:e84263d55307 229
AnnaBridge 167:e84263d55307 230 nsapi_error_t RTWInterface::set_channel(uint8_t channel)
AnnaBridge 167:e84263d55307 231 {
AnnaBridge 167:e84263d55307 232 _channel = channel;
AnnaBridge 167:e84263d55307 233 return NSAPI_ERROR_OK;
AnnaBridge 167:e84263d55307 234 }
AnnaBridge 167:e84263d55307 235
AnnaBridge 167:e84263d55307 236 int8_t RTWInterface::get_rssi()
AnnaBridge 167:e84263d55307 237 {
AnnaBridge 167:e84263d55307 238 int rssi = 0;
AnnaBridge 167:e84263d55307 239 if(wifi_get_rssi(&rssi) == 0)
AnnaBridge 167:e84263d55307 240 return (int8_t)rssi;
AnnaBridge 167:e84263d55307 241 return NSAPI_ERROR_OK;
AnnaBridge 167:e84263d55307 242 }
AnnaBridge 167:e84263d55307 243
AnnaBridge 167:e84263d55307 244 nsapi_error_t RTWInterface::connect(const char *ssid, const char *pass,
Anna Bridge 180:96ed750bd169 245 nsapi_security_t security, uint8_t channel)
AnnaBridge 167:e84263d55307 246 {
AnnaBridge 167:e84263d55307 247 set_credentials(ssid, pass, security);
AnnaBridge 167:e84263d55307 248 set_channel(channel);
AnnaBridge 167:e84263d55307 249 return connect();
AnnaBridge 167:e84263d55307 250 }
AnnaBridge 167:e84263d55307 251
AnnaBridge 167:e84263d55307 252 nsapi_error_t RTWInterface::disconnect()
AnnaBridge 167:e84263d55307 253 {
AnnaBridge 167:e84263d55307 254 char essid[33];
AnnaBridge 167:e84263d55307 255
AnnaBridge 167:e84263d55307 256 wlan_emac_link_change(false);
Anna Bridge 180:96ed750bd169 257 mbed_lwip_bringdown();
AnnaBridge 167:e84263d55307 258 if(wifi_is_connected_to_ap() != RTW_SUCCESS)
AnnaBridge 167:e84263d55307 259 return NSAPI_ERROR_NO_CONNECTION;
Anna Bridge 180:96ed750bd169 260 if(wifi_disconnect()<0){
AnnaBridge 167:e84263d55307 261 return NSAPI_ERROR_DEVICE_ERROR;
AnnaBridge 167:e84263d55307 262 }
AnnaBridge 167:e84263d55307 263 while(1){
AnnaBridge 167:e84263d55307 264 if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
AnnaBridge 167:e84263d55307 265 break;
AnnaBridge 167:e84263d55307 266 }
AnnaBridge 167:e84263d55307 267 }
AnnaBridge 167:e84263d55307 268 return NSAPI_ERROR_OK;
AnnaBridge 167:e84263d55307 269 }
AnnaBridge 167:e84263d55307 270
AnnaBridge 167:e84263d55307 271 int RTWInterface::is_connected()
AnnaBridge 167:e84263d55307 272 {
AnnaBridge 167:e84263d55307 273 // wifi_is_connected_to_ap return 0 on connected
AnnaBridge 167:e84263d55307 274 return !wifi_is_connected_to_ap();
AnnaBridge 167:e84263d55307 275 }
AnnaBridge 167:e84263d55307 276
AnnaBridge 167:e84263d55307 277 const char *RTWInterface::get_mac_address()
AnnaBridge 167:e84263d55307 278 {
AnnaBridge 167:e84263d55307 279 return mbed_lwip_get_mac_address();
AnnaBridge 167:e84263d55307 280 }
AnnaBridge 167:e84263d55307 281
AnnaBridge 167:e84263d55307 282 const char *RTWInterface::get_ip_address()
AnnaBridge 167:e84263d55307 283 {
AnnaBridge 167:e84263d55307 284 if (mbed_lwip_get_ip_address(_ip_address, sizeof _ip_address)) {
AnnaBridge 167:e84263d55307 285 return _ip_address;
AnnaBridge 167:e84263d55307 286 }
AnnaBridge 167:e84263d55307 287 return 0;
AnnaBridge 167:e84263d55307 288 }
AnnaBridge 167:e84263d55307 289
AnnaBridge 167:e84263d55307 290 const char *RTWInterface::get_netmask()
AnnaBridge 167:e84263d55307 291 {
AnnaBridge 167:e84263d55307 292 if (mbed_lwip_get_netmask(_netmask, sizeof _netmask)) {
AnnaBridge 167:e84263d55307 293 return _netmask;
AnnaBridge 167:e84263d55307 294 }
AnnaBridge 167:e84263d55307 295 return 0;
AnnaBridge 167:e84263d55307 296 }
AnnaBridge 167:e84263d55307 297
AnnaBridge 167:e84263d55307 298 const char *RTWInterface::get_gateway()
AnnaBridge 167:e84263d55307 299 {
AnnaBridge 167:e84263d55307 300 if (mbed_lwip_get_gateway(_gateway, sizeof _gateway)) {
AnnaBridge 167:e84263d55307 301 return _gateway;
AnnaBridge 167:e84263d55307 302 }
AnnaBridge 167:e84263d55307 303 return 0;
AnnaBridge 167:e84263d55307 304 }
AnnaBridge 167:e84263d55307 305
AnnaBridge 167:e84263d55307 306 NetworkStack *RTWInterface::get_stack()
AnnaBridge 167:e84263d55307 307 {
AnnaBridge 167:e84263d55307 308 return nsapi_create_stack(&lwip_stack);
Anna Bridge 180:96ed750bd169 309 }