27
27
28
28
#include " osdep_service.h"
29
29
30
- struct netif *xnetif[2 ];
31
- static struct netif lwap_netif;
32
- extern struct netif *netif_default;
30
+ typedef struct _wifi_scan_hdl {
31
+ int scan_num;
32
+ WiFiAccessPoint *ap_details;
33
+ } wifi_scan_hdl;
33
34
34
- RTWInterface::RTWInterface ()
35
- : _dhcp(true ), _ip_address(), _netmask(), _gateway()
36
- {
37
- nsapi_error_t ret;
38
- ret = init ();
39
- if (ret != NSAPI_ERROR_OK){
40
- printf (" Error init RTWInterface!(%d)\r\n " , ret);
41
- }
42
- }
43
-
44
- static void *scan_sema;
45
- static const signed int maxApNum = 4 ;
46
- static signed int ApNum;
47
- static WiFiAccessPoint *SCANED_AP[maxApNum]; /* maximum store 15 APs*/
48
-
49
- nsapi_error_t RTWInterface::set_network (const char *ip_address, const char *netmask, const char *gateway)
50
- {
51
- _dhcp = false ;
52
- strncpy (_ip_address, ip_address ? ip_address : " " , sizeof (_ip_address));
53
- strncpy (_netmask, netmask ? netmask : " " , sizeof (_netmask));
54
- strncpy (_gateway, gateway ? gateway : " " , sizeof (_gateway));
55
- return NSAPI_ERROR_OK;
56
- }
35
+ #define MAX_SCAN_TIMEOUT (15000 )
36
+ static void *scan_sema = NULL ;
37
+ static signed int ApNum = 0 ;
57
38
58
- nsapi_error_t RTWInterface::set_dhcp ( bool dhcp )
39
+ static rtw_result_t scan_result_handler ( rtw_scan_handler_result_t * malloced_scan_result )
59
40
{
60
- _dhcp = dhcp;
61
- return NSAPI_ERROR_OK;
41
+ if (malloced_scan_result->scan_complete != RTW_TRUE) {
42
+ wifi_scan_hdl *scan_handler = (wifi_scan_hdl *)malloced_scan_result->user_data ;
43
+ if (scan_handler->ap_details && scan_handler->scan_num > ApNum){
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 [ApNum], accesspoint, sizeof (WiFiAccessPoint));
78
+ delete[] accesspoint;
79
+ }
80
+ ApNum++;
81
+ } else {
82
+ // scan done
83
+ rtw_up_sema (&scan_sema);
84
+ }
85
+ return RTW_SUCCESS;
62
86
}
63
87
64
- nsapi_error_t RTWInterface::init ()
88
+ RTWInterface::RTWInterface ()
89
+ : _dhcp(true ), _ip_address(), _netmask(), _gateway()
65
90
{
66
91
emac_interface_t *emac;
67
92
int ret;
68
93
69
- // printf("\r\nInitializing emac ...\r\n");
70
94
emac = wlan_emac_init_interface ();
71
95
if (!emac) {
72
- return NSAPI_ERROR_DEVICE_ERROR ;
96
+ printf ( " Error init RTWInterface! \r\n " ) ;
73
97
}
74
98
emac->ops .power_up (emac);
75
- // printf("Initializing lwip ...\r\n");
76
99
ret = mbed_lwip_init (emac);
77
100
if (ret != 0 ) {
78
- return ret;
101
+ printf ( " Error init RTWInterface!(%d) \r\n " , ret) ;
79
102
}
80
- xnetif[0 ] = netif_default;
103
+ }
104
+
105
+ RTWInterface::~RTWInterface ()
106
+ {
107
+ wlan_emac_link_change (false );
108
+ mbed_lwip_bringdown ();
109
+ }
110
+
111
+ nsapi_error_t RTWInterface::set_network (const char *ip_address, const char *netmask, const char *gateway)
112
+ {
113
+ _dhcp = false ;
114
+ strncpy (_ip_address, ip_address ? ip_address : " " , sizeof (_ip_address));
115
+ strncpy (_netmask, netmask ? netmask : " " , sizeof (_netmask));
116
+ strncpy (_gateway, gateway ? gateway : " " , sizeof (_gateway));
81
117
return NSAPI_ERROR_OK;
82
118
}
83
119
84
- nsapi_error_t RTWInterface::deinit ( )
120
+ nsapi_error_t RTWInterface::set_dhcp ( bool dhcp )
85
121
{
86
- return mbed_lwip_bringdown ();
122
+ _dhcp = dhcp;
123
+ return NSAPI_ERROR_OK;
87
124
}
88
125
89
126
/*
@@ -109,134 +146,102 @@ nsapi_error_t RTWInterface::connect()
109
146
}
110
147
111
148
switch (_security) {
112
- case NSAPI_SECURITY_WPA2:
113
- sec = RTW_SECURITY_WPA2_MIXED_PSK;
114
- break ;
115
- case NSAPI_SECURITY_NONE:
116
- default :
117
- sec = RTW_SECURITY_OPEN;
118
- break ;
149
+ case NSAPI_SECURITY_WPA:
150
+ case NSAPI_SECURITY_WPA2:
151
+ case NSAPI_SECURITY_WPA_WPA2:
152
+ sec = RTW_SECURITY_WPA_WPA2_MIXED;
153
+ break ;
154
+ case NSAPI_SECURITY_WEP:
155
+ sec = RTW_SECURITY_WEP_PSK;
156
+ break ;
157
+ case NSAPI_SECURITY_NONE:
158
+ sec = RTW_SECURITY_OPEN;
159
+ break ;
160
+ default :
161
+ return NSAPI_ERROR_PARAMETER;
119
162
}
120
163
121
- printf (" Connecting to: %s ... \r\n " , _ssid);
164
+ if (_channel > 0 && _channel < 14 ){
165
+ uint8_t pscan_config = PSCAN_ENABLE;
166
+ wifi_set_pscan_chan (&_channel, &pscan_config, 1 );
167
+ }
168
+
169
+ // printf("Connecting to: %s ... \r\n", _ssid);
122
170
ret = wifi_connect (_ssid, sec, _pass, strlen (_ssid), strlen (_pass), 0 , (void *)NULL );
123
171
if (ret != RTW_SUCCESS) {
124
172
printf (" failed: %d\r\n " , ret);
125
173
return NSAPI_ERROR_NO_CONNECTION;
126
174
}
127
175
// printf("connected\r\n");
128
176
129
- wlan_emac_link_up ( 0 );
177
+ wlan_emac_link_change ( true );
130
178
return mbed_lwip_bringup (_dhcp,
131
179
_ip_address[0 ] ? _ip_address : 0 ,
132
180
_netmask[0 ] ? _netmask : 0 ,
133
181
_gateway[0 ] ? _gateway : 0 );
134
182
}
135
183
136
- static rtw_result_t scan_result_handler ( rtw_scan_handler_result_t * malloced_scan_result )
137
- {
138
- if (malloced_scan_result->scan_complete != RTW_TRUE) {
139
- rtw_scan_result_t * record = &malloced_scan_result->ap_details ;
140
- record->SSID .val [record->SSID .len ] = 0 ; /* Ensure the SSID is null terminated */
141
- if (ApNum>maxApNum)
142
- return RTW_SUCCESS;
143
- nsapi_wifi_ap_t ap;
144
-
145
- memset ((void *)&ap, 0x00 , sizeof (nsapi_wifi_ap_t ));
146
- memcpy (ap.ssid , record->SSID .val , record->SSID .len );
147
- memcpy (ap.bssid , record->BSSID .octet , 6 );
148
- ap.security = (record->security == RTW_SECURITY_OPEN)?NSAPI_SECURITY_NONE :
149
- (record->security == RTW_SECURITY_WEP_PSK)?NSAPI_SECURITY_WEP:
150
- (record->security == RTW_SECURITY_WPA_TKIP_PSK || record->security == RTW_SECURITY_WPA_AES_PSK)? NSAPI_SECURITY_WPA:
151
- (record->security == RTW_SECURITY_WPA2_AES_PSK || \
152
- record->security == RTW_SECURITY_WPA2_TKIP_PSK || \
153
- record->security == RTW_SECURITY_WPA2_MIXED_PSK)?NSAPI_SECURITY_WPA2:
154
- (record->security == RTW_SECURITY_WPA_WPA2_MIXED)?NSAPI_SECURITY_WPA_WPA2:NSAPI_SECURITY_UNKNOWN;
155
- ap.rssi = record->signal_strength ;
156
- ap.channel = record->channel ;
157
-
158
-
159
- SCANED_AP[ApNum++] = new WiFiAccessPoint (ap);
160
-
161
- } else {
162
- // scan done
163
-
164
- rtw_up_sema (&scan_sema);
165
- }
166
- return RTW_SUCCESS;
167
- }
168
-
169
184
nsapi_error_t RTWInterface::scan (WiFiAccessPoint *res, unsigned count)
170
185
{
171
- // blocked
172
- if (count == 0 ){
173
- ApNum = 0 ;
174
-
175
- rtw_init_sema (&scan_sema, 0 );
176
- if (wifi_scan_networks (scan_result_handler, NULL ) != RTW_SUCCESS){
177
- printf (" wifi scan failed\n\r " );
178
- // return NSAPI_ERROR_DEVICE_ERROR;
179
- goto error;
180
- }
181
-
182
- if (rtw_down_timeout_sema ( &scan_sema, 15000 ) == RTW_FALSE) {
183
- printf (" wifi scan timeout\r\n " );
184
- // return NSAPI_ERROR_DEVICE_ERROR;
185
- goto error;
186
- }
187
- rtw_free_sema (&scan_sema);
188
- return ApNum;
189
- }else if (count > 0 && res != NULL ){
190
- count = count < maxApNum ? count : maxApNum;
191
- for (int i = 0 ; i < count; i++){
192
- memcpy (&res[i], SCANED_AP[i], sizeof (WiFiAccessPoint));
193
- delete[] SCANED_AP[i];
194
- }
195
- return (signed int )count;
196
- }
197
- return NSAPI_ERROR_OK;
198
- error:
199
- rtw_free_sema (&scan_sema);
200
- return NSAPI_ERROR_DEVICE_ERROR;
186
+ static wifi_scan_hdl scan_handler;
187
+ ApNum = 0 ;
188
+ if (!scan_sema)
189
+ rtw_init_sema (&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_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 > ApNum)
201
+ count = ApNum;
202
+
203
+ return count;
201
204
}
202
205
203
206
nsapi_error_t RTWInterface::set_channel (uint8_t channel)
204
207
{
205
- if (wifi_set_channel (channel) == 0 )
206
- return NSAPI_ERROR_OK;
207
- return NSAPI_ERROR_DEVICE_ERROR;
208
+ _channel = channel;
209
+ return NSAPI_ERROR_OK;
208
210
}
209
211
210
212
int8_t RTWInterface::get_rssi ()
211
213
{
212
- return NSAPI_ERROR_UNSUPPORTED;
214
+ int rssi = 0 ;
215
+ if (wifi_get_rssi (&rssi) == 0 )
216
+ return (int8_t )rssi;
217
+ return NSAPI_ERROR_OK;
213
218
}
214
219
215
220
nsapi_error_t RTWInterface::connect (const char *ssid, const char *pass,
216
221
nsapi_security_t security, uint8_t channel)
217
222
{
218
223
set_credentials (ssid, pass, security);
224
+ set_channel (channel);
219
225
return connect ();
220
226
}
221
227
222
228
nsapi_error_t RTWInterface::disconnect ()
223
229
{
224
- char essid[33 ];
230
+ char essid[33 ];
225
231
232
+ wlan_emac_link_change (false );
226
233
if (wifi_is_connected_to_ap () != RTW_SUCCESS)
227
- return NSAPI_ERROR_NO_CONNECTION;
228
- printf (" Deassociating AP ...\r\n " );
229
- if (wifi_disconnect ()<0 ){
230
- return NSAPI_ERROR_DEVICE_ERROR;
231
- }
232
-
233
- while (1 ){
234
- if (wext_get_ssid (WLAN0_NAME, (unsigned char *) essid) < 0 ) {
235
- printf (" WIFI disconnected\n\r " );
236
- break ;
237
- }
238
- }
239
-
234
+ return NSAPI_ERROR_NO_CONNECTION;
235
+ // printf("Deassociating AP ...\r\n");
236
+ if (wifi_disconnect ()<0 ){
237
+ return NSAPI_ERROR_DEVICE_ERROR;
238
+ }
239
+ while (1 ){
240
+ if (wext_get_ssid (WLAN0_NAME, (unsigned char *) essid) < 0 ) {
241
+ // printf("WIFI disconnected\n\r");
242
+ break ;
243
+ }
244
+ }
240
245
return NSAPI_ERROR_OK;
241
246
}
242
247
0 commit comments