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