1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-7-10 tyx the first version
*/
#include "wifi_structures.h"
#include "wifi_constants.h"
#include <wifi/wifi_util.h>
#include <wifi/wifi_conf.h>
#include "board.h"
#include "drv_wlan.h"
// #define SCAN_WAIT_TIME (10000)
struct scan_user_data
{
struct rt_completion done;
scan_callback_fn fun;
void *data;
};
extern rthw_mode_t wifi_mode;
rthw_mode_t rthw_wifi_mode_get(void)
{
return wifi_mode;
}
int rthw_wifi_stop(void)
{
return wifi_off();
}
int rthw_wifi_start(rthw_mode_t mode)
{
if(wifi_on(mode) < 0)
{
rt_kprintf("ERROR: wifi_on failed\n");
return -1;
}
return 0;
}
int rthw_wifi_connect(char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type)
{
int mode;
rtw_wifi_setting_t setting;
mode = rthw_wifi_mode_get();
if ((mode != RTHW_MODE_STA) && (mode != RTHW_MODE_STA_AP))
{
return -1;
}
if(wext_get_mode(WLAN0_NAME, &mode) < 0)
{
rt_kprintf("L:%d wifi get mode err\n", __LINE__);
return -1;
}
if(wifi_connect(ssid, security_type, password, ssid_len, pass_len, -1, NULL) != RTW_SUCCESS)
{
rt_kprintf("wifi connect fail\n");
return -1;
}
rt_kprintf("wifi connect success\n");
rt_kprintf("Show Wi-Fi information\n");
wifi_get_setting(WLAN0_NAME,&setting);
wifi_show_setting(WLAN0_NAME,&setting);
return 0;
}
int rthw_wifi_connect_bssid(char *bssid, char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type)
{
int mode;
rtw_wifi_setting_t setting;
mode = rthw_wifi_mode_get();
if ((mode != RTHW_MODE_STA) && (mode != RTHW_MODE_STA_AP))
{
return -1;
}
if(wext_get_mode(WLAN0_NAME, &mode) < 0)
{
rt_kprintf("L:%d wifi get mode err\n", __LINE__);
return -1;
}
if(wifi_connect_bssid(bssid, ssid, security_type, password, 6, ssid_len, pass_len, -1, NULL) != RTW_SUCCESS)
{
rt_kprintf("wifi connect fail\n");
return -1;
}
rt_kprintf("wifi connect success\n");
rt_kprintf("Show Wi-Fi information\n");
wifi_get_setting(WLAN0_NAME,&setting);
wifi_show_setting(WLAN0_NAME,&setting);
return 0;
}
int rthw_wifi_ap_start(char *ssid, char *password, int channel)
{
int mode = 0, timeout = 20;
rtw_security_t security_type = RTW_SECURITY_WPA2_AES_PSK;
char *name = RT_NULL;
mode = rthw_wifi_mode_get();
if (mode == RTHW_MODE_AP)
{
name = WLAN0_NAME;
}
else if (mode == RTHW_MODE_STA_AP)
{
name = WLAN1_NAME;
}
else
{
return -1;
}
if(wext_get_mode(name, &mode) < 0)
{
rt_kprintf("L:%d wifi get mode err\n", __LINE__);
return -1;
}
if (password == RT_NULL)
{
security_type = RTW_SECURITY_OPEN;
}
if(wifi_start_ap(ssid, security_type, password, rt_strlen(ssid), rt_strlen(password), 6) != 0)
{
rt_kprintf("ERROR: wifi_start_ap failed\n");
return -1;
}
while(1)
{
char essid[33];
if(wext_get_ssid(name, (unsigned char *) essid) > 0)
{
if(strcmp((const char *) essid, (const char *)ssid) == 0)
{
rt_kprintf("%s started\n", ssid);
break;
}
}
if(timeout == 0)
{
rt_kprintf("Start AP timeout\n");
return -1;
}
rt_thread_delay(1 * RT_TICK_PER_SECOND);
timeout --;
}
return 0;
}
static int rthw_wifi_disconnect(char *name)
{
char essid[33];
int timeout = 20;
const rt_uint8_t null_bssid[ETH_ALEN + 2] = {0, 0, 0, 0, 0, 1, 0, 0};
if (name == RT_NULL)
return -1;
if (wext_get_ssid(name, (unsigned char *) essid) < 0)
{
rt_kprintf("\nWIFI disconnected!\n");
return -1;
}
if (wext_set_bssid(name, null_bssid) < 0)
{
rt_kprintf("Failed to set bogus BSSID to disconnect");
return -1;
}
while (1)
{
if(wext_get_ssid(name, (unsigned char *) essid) < 0)
{
rt_kprintf("WIFI disconnected!\n");
break;
}
if(timeout == 0)
{
rt_kprintf("ERROR: Deassoc timeout!\n");
return -1;
}
rt_thread_delay(10);
timeout --;
}
return 0;
}
int rthw_wifi_sta_disconnect(void)
{
int mode = 0;
char *name = RT_NULL;
mode = rthw_wifi_mode_get();
if (mode == RTHW_MODE_STA)
{
name = WLAN0_NAME;
}
else if (mode == RTHW_MODE_STA_AP)
{
name = WLAN0_NAME;
}
else
{
return -1;
}
return rthw_wifi_disconnect(name);
}
int rthw_wifi_ap_disconnect(void)
{
int mode = 0;
char *name = RT_NULL;
mode = rthw_wifi_mode_get();
if (mode == RTHW_MODE_AP)
{
name = WLAN0_NAME;
}
else if (mode == RTHW_MODE_STA_AP)
{
name = WLAN1_NAME;
}
else
{
return -1;
}
return rthw_wifi_disconnect(name);
}
int rthw_wifi_rssi_get(void)
{
int rssi = 0;
wifi_get_rssi(&rssi);
return rssi;
}
void rthw_wifi_channel_set(int channel)
{
wifi_set_channel(channel);
}
int rthw_wifi_channel_get(void)
{
int channel;
wifi_get_channel(&channel);
return channel;
}
static rtw_result_t rthw_wifi_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result)
{
struct scan_user_data* user_data = malloced_scan_result->user_data;
struct rthw_wlan_info info = { 0 };
if (malloced_scan_result->scan_complete != RTW_TRUE)
{
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
if (user_data->fun != RT_NULL)
{
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
info.ssid = record->SSID.val;
info.bssid = record->BSSID.octet;
info.band = record->band;
info.datarate = 0;
info.channel = record->channel;
info.rssi = record->signal_strength;
info.security = record->security;
user_data->fun(&info, user_data->data);
}
}
else
{
user_data->fun(RT_NULL, user_data->data);
rt_free(user_data);
rt_kprintf("scan ap down\n");
}
return RTW_SUCCESS;
}
int rthw_wifi_scan(scan_callback_fn fun, void *data)
{
struct scan_user_data *user_data;
if (fun == RT_NULL)
{
rt_kprintf("scan callback fun is null\n");
return -1;
}
user_data = rt_malloc(sizeof(struct scan_user_data));
if (user_data == RT_NULL)
{
rt_kprintf("wifi scan malloc fail\n");
return -1;
}
user_data->fun = fun;
user_data->data = data;
if (wifi_scan_networks(rthw_wifi_scan_result_handler, user_data) != RTW_SUCCESS)
{
rt_kprintf("ERROR: wifi scan failed\n\r");
return -1;
}
return 0;
}
static rthw_wlan_monitor_callback_t monitor_callback;
static void rthw_wifi_promisc_callback(unsigned char *buf, unsigned int len, void* userdata)
{
if (monitor_callback)
{
monitor_callback(userdata, len, RT_NULL);
}
}
void rthw_wifi_monitor_callback_set(rthw_wlan_monitor_callback_t callback)
{
monitor_callback = callback;
}
void rthw_wifi_monitor_enable(int enable)
{
if (enable)
{
wifi_enter_promisc_mode();
wifi_set_promisc(RTW_PROMISC_ENABLE, rthw_wifi_promisc_callback, 1);
rt_kprintf("%s run \n", __FUNCTION__);
}
else
{
wifi_set_promisc(RTW_PROMISC_DISABLE, RT_NULL, 0);
rthw_wifi_stop();
}
}
|