test

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Aug 31 17:27:04 2017 +0100
Revision:
173:7d866c31b3c5
Parent:
170:e3b6fe271b81
Child:
175:b96e65c34a4d
This updates the lib to the mbed lib v 150

Who changed what in which revision?

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