19
19
20
20
#include " RTWInterface.h"
21
21
#include " mbed_interface.h"
22
- #include " rtw_emac.h"
23
22
23
+ #include " rtw_emac.h"
24
+ #include " EMAC.h"
24
25
#include " wifi_constants.h"
25
26
#include " wifi_conf.h"
26
- #include " lwip_stack.h"
27
27
28
+ #include " OnboardNetworkStack.h"
29
+ #include " EMACMemoryManager.h"
28
30
#include " osdep_service.h"
29
31
30
32
typedef struct _wifi_scan_hdl {
@@ -42,14 +44,14 @@ static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_sca
42
44
{
43
45
wifi_scan_hdl *scan_handler = (wifi_scan_hdl *)malloced_scan_result->user_data ;
44
46
if (malloced_scan_result->scan_complete != RTW_TRUE) {
45
- if (scan_handler->ap_details && scan_handler->scan_num > scan_handler->ap_num ){
47
+ if (scan_handler->ap_details && scan_handler->scan_num > scan_handler->ap_num ) {
46
48
nsapi_wifi_ap_t ap;
47
49
rtw_scan_result_t * record = &malloced_scan_result->ap_details ;
48
50
record->SSID .val [record->SSID .len ] = 0 ; /* Ensure the SSID is null terminated */
49
51
memset ((void *)&ap, 0x00 , sizeof (nsapi_wifi_ap_t ));
50
52
memcpy (ap.ssid , record->SSID .val , record->SSID .len );
51
53
memcpy (ap.bssid , record->BSSID .octet , 6 );
52
- switch (record->security ){
54
+ switch (record->security ) {
53
55
case RTW_SECURITY_OPEN:
54
56
ap.security = NSAPI_SECURITY_NONE;
55
57
break ;
@@ -78,41 +80,30 @@ static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_sca
78
80
scan_handler->ap_details [scan_handler->ap_num ] = WiFiAccessPoint (ap);
79
81
}
80
82
scan_handler->ap_num ++;
81
- } else {
83
+ } else {
82
84
// scan done
83
85
rtw_up_sema (&scan_handler->scan_sema );
84
86
}
85
87
return RTW_SUCCESS;
86
88
}
87
89
88
- RTWInterface::RTWInterface (bool debug)
89
- : _dhcp(true ), _ssid(), _pass(), _ip_address(), _netmask(), _gateway()
90
+ RTWInterface::RTWInterface (RTW_EMAC &get_rtw_emac, OnboardNetworkStack &get_rtw_obn_stack) :
91
+ rtw_emac(get_rtw_emac),
92
+ rtw_obn_stack(get_rtw_obn_stack),
93
+ rtw_interface(NULL ),
94
+ _dhcp(true ),
95
+ _ip_address(),
96
+ _netmask(),
97
+ _gateway(),
98
+ _mac_address()
90
99
{
91
- emac_interface_t *emac;
92
- int ret;
93
- extern u32 GlobalDebugEnable;
94
-
95
- GlobalDebugEnable = debug?1 :0 ;
96
- emac = wlan_emac_init_interface ();
97
- if (!emac) {
98
- printf (" Error init RTWInterface!\r\n " );
99
- return ;
100
- }
101
- emac->ops .power_up (emac);
102
- if (_inited == false ) {
103
- ret = mbed_lwip_init (emac);
104
- if (ret != 0 ) {
105
- printf (" Error init RTWInterface!(%d)\r\n " , ret);
106
- return ;
107
- }
108
- _inited = true ;
109
- }
100
+ rtw_emac.power_up ();
110
101
}
111
102
112
103
RTWInterface::~RTWInterface ()
113
104
{
114
- wlan_emac_link_change (false );
115
- mbed_lwip_bringdown ();
105
+ rtw_emac. wlan_emac_link_change (false );
106
+ rtw_interface-> bringdown ();
116
107
}
117
108
118
109
nsapi_error_t RTWInterface::set_network (const char *ip_address, const char *netmask, const char *gateway)
@@ -135,7 +126,7 @@ nsapi_error_t RTWInterface::set_dhcp(bool dhcp)
135
126
*/
136
127
nsapi_error_t RTWInterface::set_credentials (const char *ssid, const char *pass, nsapi_security_t security)
137
128
{
138
- if (!ssid || ( strlen (ssid) == 0 ) ) {
129
+ if (!ssid) {
139
130
return NSAPI_ERROR_PARAMETER;
140
131
}
141
132
@@ -144,14 +135,11 @@ nsapi_error_t RTWInterface::set_credentials(const char *ssid, const char *pass,
144
135
case NSAPI_SECURITY_WPA2:
145
136
case NSAPI_SECURITY_WPA_WPA2:
146
137
case NSAPI_SECURITY_WEP:
147
- if ((strlen (pass) < 8 ) || (strlen (pass) > 63 )) { // 802.11 password 8-63 characters
138
+ if ((strlen (pass) < 8 ) || (strlen (pass) > 63 )) { // 802.11 password 8-63 characters
148
139
return NSAPI_ERROR_PARAMETER;
149
140
}
150
141
break ;
151
142
case NSAPI_SECURITY_NONE:
152
- if (pass && strlen (pass) > 0 ) {
153
- return NSAPI_ERROR_PARAMETER;
154
- }
155
143
break ;
156
144
default :
157
145
return NSAPI_ERROR_PARAMETER;
@@ -169,8 +157,7 @@ nsapi_error_t RTWInterface::connect()
169
157
int ret;
170
158
rtw_security_t sec;
171
159
172
- if (!_ssid || (strlen (_ssid) == 0 ) ||
173
- (!_pass && _security != NSAPI_SECURITY_NONE)) {
160
+ if (!_ssid || (!_pass && _security != NSAPI_SECURITY_NONE)) {
174
161
printf (" Invalid credentials\r\n " );
175
162
return NSAPI_ERROR_PARAMETER;
176
163
}
@@ -191,85 +178,94 @@ nsapi_error_t RTWInterface::connect()
191
178
return NSAPI_ERROR_PARAMETER;
192
179
}
193
180
181
+ if (_channel > 0 && _channel < 14 ) {
182
+ uint8_t pscan_config = PSCAN_ENABLE;
183
+ wifi_set_pscan_chan (&_channel, &pscan_config, 1 );
184
+ }
185
+
194
186
ret = wifi_connect (_ssid, sec, _pass, strlen (_ssid), strlen (_pass), 0 , (void *)NULL );
195
187
if (ret != RTW_SUCCESS) {
196
188
printf (" failed: %d\r\n " , ret);
197
189
return NSAPI_ERROR_NO_CONNECTION;
198
190
}
199
191
200
- wlan_emac_link_change (true );
201
- return mbed_lwip_bringup (_dhcp,
202
- _ip_address[0 ] ? _ip_address : 0 ,
203
- _netmask[0 ] ? _netmask : 0 ,
204
- _gateway[0 ] ? _gateway : 0 );
192
+ rtw_emac.wlan_emac_link_change (true );
193
+ if (!rtw_interface) {
194
+ nsapi_error_t err = rtw_obn_stack.add_ethernet_interface (rtw_emac, true , &rtw_interface);
195
+ if (err != NSAPI_ERROR_OK) {
196
+ rtw_interface = NULL ;
197
+ return err;
198
+ }
199
+ }
200
+
201
+ int rtw_if_bringup = rtw_interface->bringup (_dhcp,
202
+ _ip_address[0 ] ? _ip_address : 0 ,
203
+ _netmask[0 ] ? _netmask : 0 ,
204
+ _gateway[0 ] ? _gateway : 0 ,
205
+ DEFAULT_STACK);
206
+ return rtw_if_bringup;
205
207
}
206
208
207
209
nsapi_error_t RTWInterface::scan (WiFiAccessPoint *res, unsigned count)
208
210
{
209
211
static wifi_scan_hdl scan_handler;
210
212
scan_handler.ap_num = 0 ;
211
- if (!scan_handler.scan_sema )
213
+ if (!scan_handler.scan_sema ) {
212
214
rtw_init_sema (&scan_handler.scan_sema , 0 );
215
+ }
213
216
scan_handler.scan_num = count;
214
217
scan_handler.ap_details = res;
215
- if (wifi_scan_networks (scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS){
218
+ if (wifi_scan_networks (scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS) {
216
219
printf (" wifi scan failed\n\r " );
217
220
return NSAPI_ERROR_DEVICE_ERROR;
218
221
}
219
- if (rtw_down_timeout_sema ( &scan_handler.scan_sema , MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
222
+ if (rtw_down_timeout_sema ( &scan_handler.scan_sema , MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
220
223
printf (" wifi scan timeout\r\n " );
221
224
return NSAPI_ERROR_DEVICE_ERROR;
222
225
}
223
- if (count <= 0 || count > scan_handler.ap_num )
226
+ if (count <= 0 || count > scan_handler.ap_num ) {
224
227
count = scan_handler.ap_num ;
225
-
228
+ }
226
229
return count;
227
230
}
228
231
229
232
nsapi_error_t RTWInterface::set_channel (uint8_t channel)
230
233
{
231
- // Not supported for STA mode wifi driver
232
- if (channel != 0 )
233
- return NSAPI_ERROR_UNSUPPORTED;
234
-
234
+ _channel = channel;
235
235
return NSAPI_ERROR_OK;
236
236
}
237
237
238
238
int8_t RTWInterface::get_rssi ()
239
239
{
240
240
int rssi = 0 ;
241
- if (wifi_get_rssi (&rssi) == 0 )
241
+ if (wifi_get_rssi (&rssi) == 0 ) {
242
242
return (int8_t )rssi;
243
+ }
243
244
return NSAPI_ERROR_OK;
244
245
}
245
246
246
247
nsapi_error_t RTWInterface::connect (const char *ssid, const char *pass,
247
248
nsapi_security_t security, uint8_t channel)
248
249
{
249
- nsapi_error_t ret;
250
-
251
- ret = set_credentials (ssid, pass, security);
252
- if (ret != NSAPI_ERROR_OK) return ret;
253
-
254
- ret = set_channel (channel);
255
- if (ret != NSAPI_ERROR_OK) return ret;
256
-
250
+ set_credentials (ssid, pass, security);
251
+ set_channel (channel);
257
252
return connect ();
258
253
}
259
254
260
255
nsapi_error_t RTWInterface::disconnect ()
261
256
{
262
257
char essid[33 ];
263
258
264
- wlan_emac_link_change (false );
265
- mbed_lwip_bringdown ();
266
- if (wifi_is_connected_to_ap () != RTW_SUCCESS)
259
+ rtw_emac. wlan_emac_link_change (false );
260
+ rtw_interface-> bringdown ();
261
+ if (wifi_is_connected_to_ap () != RTW_SUCCESS) {
267
262
return NSAPI_ERROR_NO_CONNECTION;
268
- if (wifi_disconnect ()<0 ){
263
+ }
264
+ if (wifi_disconnect ()<0 ) {
269
265
return NSAPI_ERROR_DEVICE_ERROR;
270
266
}
271
- while (1 ){
272
- if (wext_get_ssid (WLAN0_NAME, (unsigned char *) essid) < 0 ) {
267
+ while (1 ) {
268
+ if (wext_get_ssid (WLAN0_NAME, (unsigned char *) essid) < 0 ) {
273
269
break ;
274
270
}
275
271
}
@@ -278,40 +274,42 @@ nsapi_error_t RTWInterface::disconnect()
278
274
279
275
int RTWInterface::is_connected ()
280
276
{
281
- // wifi_is_connected_to_ap return 0 on connected
282
277
return !wifi_is_connected_to_ap ();
283
278
}
284
279
285
280
const char *RTWInterface::get_mac_address ()
286
281
{
287
- return mbed_lwip_get_mac_address ();
282
+ if (rtw_interface->get_mac_address (_mac_address, sizeof _mac_address)) {
283
+ return _mac_address;
284
+ }
285
+ return 0 ;
288
286
}
289
287
290
288
const char *RTWInterface::get_ip_address ()
291
289
{
292
- if (mbed_lwip_get_ip_address (_ip_address, sizeof _ip_address)) {
290
+ if (rtw_interface-> get_ip_address (_ip_address, sizeof _ip_address)) {
293
291
return _ip_address;
294
292
}
295
293
return 0 ;
296
294
}
297
295
298
296
const char *RTWInterface::get_netmask ()
299
297
{
300
- if (mbed_lwip_get_netmask (_netmask, sizeof _netmask)) {
298
+ if (rtw_interface-> get_netmask (_netmask, sizeof _netmask)) {
301
299
return _netmask;
302
300
}
303
301
return 0 ;
304
302
}
305
303
306
304
const char *RTWInterface::get_gateway ()
307
305
{
308
- if (mbed_lwip_get_gateway (_gateway, sizeof _gateway)) {
306
+ if (rtw_interface-> get_gateway (_gateway, sizeof _gateway)) {
309
307
return _gateway;
310
308
}
311
309
return 0 ;
312
310
}
313
311
314
312
NetworkStack *RTWInterface::get_stack ()
315
313
{
316
- return nsapi_create_stack (&lwip_stack) ;
314
+ return &rtw_obn_stack ;
317
315
}
0 commit comments