1 /*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2018-08-03 tyx the first version
9 * 2024-12-25 Evlers add get_info api for more new sta information
10 * 2025-01-04 Evlers add ap_get_info api for more ap information
11 */
12
13 #include <rthw.h>
14 #include <rtthread.h>
15 #include <dev_wlan.h>
16 #include <dev_wlan_prot.h>
17
18 #define DBG_TAG "WLAN.dev"
19 #ifdef RT_WLAN_DEV_DEBUG
20 #define DBG_LVL DBG_LOG
21 #else
22 #define DBG_LVL DBG_INFO
23 #endif /* RT_WLAN_DEV_DEBUG */
24 #include <rtdbg.h>
25
26 #if defined(RT_USING_WIFI) || defined(RT_USING_WLAN)
27
28 #ifndef RT_DEVICE
29 #define RT_DEVICE(__device) ((rt_device_t)__device)
30 #endif
31
32 #define WLAN_DEV_LOCK(_wlan) (rt_mutex_take(&(_wlan)->lock, RT_WAITING_FOREVER))
33 #define WLAN_DEV_UNLOCK(_wlan) (rt_mutex_release(&(_wlan)->lock))
34
35 #if RT_WLAN_SSID_MAX_LENGTH < 1
36 #error "SSID length is too short"
37 #endif
38
39 #if RT_WLAN_BSSID_MAX_LENGTH < 1
40 #error "BSSID length is too short"
41 #endif
42
43 #if RT_WLAN_PASSWORD_MAX_LENGTH < 1
44 #error "password length is too short"
45 #endif
46
47 #if RT_WLAN_DEV_EVENT_NUM < 2
48 #error "dev num Too little"
49 #endif
50
rt_wlan_dev_init(struct rt_wlan_device * device,rt_wlan_mode_t mode)51 rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
52 {
53 rt_err_t result = RT_EOK;
54
55 /* init wlan device */
56 LOG_D("F:%s L:%d is run device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
57 if ((device == RT_NULL) || (mode >= RT_WLAN_MODE_MAX))
58 {
59 LOG_E("F:%s L:%d Parameter Wrongful device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
60 return -RT_ERROR;
61 }
62
63 if (mode == RT_WLAN_AP && device->flags & RT_WLAN_FLAG_STA_ONLY)
64 {
65 LOG_E("F:%s L:%d This wlan device can only be set to sta mode!", __FUNCTION__, __LINE__);
66 return -RT_ERROR;
67 }
68 else if (mode == RT_WLAN_STATION && device->flags & RT_WLAN_FLAG_AP_ONLY)
69 {
70 LOG_E("F:%s L:%d This wlan device can only be set to ap mode!", __FUNCTION__, __LINE__);
71 return -RT_ERROR;
72 }
73
74 result = rt_device_init(RT_DEVICE(device));
75 if (result != RT_EOK)
76 {
77 LOG_E("L:%d wlan init failed", __LINE__);
78 return -RT_ERROR;
79 }
80 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_MODE, (void *)&mode);
81 if (result != RT_EOK)
82 {
83 LOG_E("L:%d wlan config mode failed", __LINE__);
84 return -RT_ERROR;
85 }
86 device->mode = mode;
87 return result;
88 }
89
rt_wlan_dev_connect(struct rt_wlan_device * device,struct rt_wlan_info * info,const char * password,int password_len)90 rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
91 {
92 rt_err_t result = RT_EOK;
93 struct rt_sta_info sta_info;
94
95 if (device == RT_NULL)
96 {
97 return -RT_EIO;
98 }
99 if (info == RT_NULL)
100 {
101 return -RT_ERROR;
102 }
103
104 if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) ||
105 (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH))
106 {
107 LOG_E("L:%d password or ssid is too long", __LINE__);
108 return -RT_ERROR;
109 }
110 rt_memset(&sta_info, 0, sizeof(struct rt_sta_info));
111 rt_memcpy(&sta_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
112 rt_memcpy(sta_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
113 if (password != RT_NULL)
114 {
115 rt_memcpy(sta_info.key.val, password, password_len);
116 sta_info.key.len = password_len;
117 }
118 sta_info.channel = info->channel;
119 sta_info.security = info->security;
120
121 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_JOIN, &sta_info);
122 return result;
123 }
124
rt_wlan_dev_fast_connect(struct rt_wlan_device * device,struct rt_wlan_info * info,const char * password,int password_len)125 rt_err_t rt_wlan_dev_fast_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
126 {
127 rt_err_t result = RT_EOK;
128 struct rt_wlan_buff buff = {0};
129
130 if (device == RT_NULL)
131 {
132 return -RT_EIO;
133 }
134 if (info == RT_NULL)
135 {
136 return -RT_ERROR;
137 }
138
139 if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) ||
140 (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH))
141 {
142 LOG_E("L:%d password or ssid is too long", __LINE__);
143 return -RT_ERROR;
144 }
145
146 buff.len = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_FAST_CONNECT_INFO, buff.data);
147 if(buff.len < 0)
148 {
149 LOG_D("L:%d Can't get fast connect info", __LINE__);
150 return buff.len;
151 }
152
153 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_FAST_CONNECT, &buff);
154
155 return result;
156 }
157
rt_wlan_dev_disconnect(struct rt_wlan_device * device)158 rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device)
159 {
160 rt_err_t result = RT_EOK;
161
162 if (device == RT_NULL)
163 {
164 return -RT_EIO;
165 }
166
167 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_DISCONNECT, RT_NULL);
168 return result;
169 }
170
rt_wlan_dev_ap_start(struct rt_wlan_device * device,struct rt_wlan_info * info,const char * password,int password_len)171 rt_err_t rt_wlan_dev_ap_start(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
172 {
173 rt_err_t result = RT_EOK;
174 struct rt_ap_info ap_info;
175
176 if (device == RT_NULL)
177 {
178 return -RT_EIO;
179 }
180 if (info == RT_NULL)
181 {
182 return -RT_ERROR;
183 }
184
185 if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) ||
186 (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH))
187 {
188 LOG_E("L:%d password or ssid is too long", __LINE__);
189 return -RT_ERROR;
190 }
191
192 rt_memset(&ap_info, 0, sizeof(struct rt_ap_info));
193 rt_memcpy(&ap_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
194 if (password != RT_NULL)
195 {
196 rt_memcpy(ap_info.key.val, password, password_len);
197 }
198 ap_info.key.len = password_len;
199 ap_info.hidden = info->hidden;
200 ap_info.channel = info->channel;
201 ap_info.security = info->security;
202
203 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SOFTAP, &ap_info);
204 return result;
205 }
206
rt_wlan_dev_ap_stop(struct rt_wlan_device * device)207 rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device)
208 {
209 rt_err_t result = RT_EOK;
210
211 if (device == RT_NULL)
212 {
213 return -RT_EIO;
214 }
215
216 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_STOP, RT_NULL);
217 return result;
218 }
219
rt_wlan_dev_ap_deauth(struct rt_wlan_device * device,rt_uint8_t mac[6])220 rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6])
221 {
222 rt_err_t result = RT_EOK;
223
224 if (device == RT_NULL)
225 {
226 return -RT_EIO;
227 }
228
229 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_DEAUTH, mac);
230 return result;
231 }
232
rt_wlan_dev_get_rssi(struct rt_wlan_device * device)233 int rt_wlan_dev_get_rssi(struct rt_wlan_device *device)
234 {
235 int rssi = 0;
236 rt_err_t result = RT_EOK;
237
238 if (device == RT_NULL)
239 {
240 rt_set_errno(-RT_EIO);
241 return 0;
242 }
243
244 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_RSSI, &rssi);
245 if (result != RT_EOK)
246 {
247 rt_set_errno(result);
248 return 0;
249 }
250
251 return rssi;
252 }
253
rt_wlan_dev_get_info(struct rt_wlan_device * device,struct rt_wlan_info * info)254 rt_err_t rt_wlan_dev_get_info(struct rt_wlan_device *device, struct rt_wlan_info *info)
255 {
256 rt_err_t result = RT_EOK;
257
258 if (device == RT_NULL)
259 {
260 return -RT_EIO;
261 }
262
263 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_INFO, info);
264 if (result != RT_EOK)
265 {
266 rt_set_errno(result);
267 return 0;
268 }
269
270 return result;
271 }
272
rt_wlan_dev_ap_get_info(struct rt_wlan_device * device,struct rt_wlan_info * info)273 rt_err_t rt_wlan_dev_ap_get_info(struct rt_wlan_device *device, struct rt_wlan_info *info)
274 {
275 rt_err_t result = RT_EOK;
276
277 if (device == RT_NULL)
278 {
279 return -RT_EIO;
280 }
281
282 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_GET_INFO, info);
283 if (result != RT_EOK)
284 {
285 rt_set_errno(result);
286 return 0;
287 }
288
289 return result;
290 }
291
rt_wlan_dev_get_mac(struct rt_wlan_device * device,rt_uint8_t mac[6])292 rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
293 {
294 rt_err_t result = RT_EOK;
295
296 if (device == RT_NULL)
297 {
298 return -RT_EIO;
299 }
300
301 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_MAC, &mac[0]);
302 return result;
303 }
304
rt_wlan_dev_set_mac(struct rt_wlan_device * device,rt_uint8_t mac[6])305 rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
306 {
307 rt_err_t result = RT_EOK;
308
309 if (device == RT_NULL)
310 {
311 return -RT_EIO;
312 }
313
314 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_MAC, &mac[0]);
315 return result;
316 }
317
rt_wlan_dev_set_powersave(struct rt_wlan_device * device,int level)318 rt_err_t rt_wlan_dev_set_powersave(struct rt_wlan_device *device, int level)
319 {
320 rt_err_t result = RT_EOK;
321
322 if (device == RT_NULL)
323 {
324 return -RT_EIO;
325 }
326
327 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_POWERSAVE, &level);
328 return result;
329 }
330
rt_wlan_dev_get_powersave(struct rt_wlan_device * device)331 int rt_wlan_dev_get_powersave(struct rt_wlan_device *device)
332 {
333 int level = -1;
334 rt_err_t result = RT_EOK;
335
336 if (device == RT_NULL)
337 {
338 rt_set_errno(-RT_EIO);
339 return -1;
340 }
341
342 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_POWERSAVE, &level);
343 if (result != RT_EOK)
344 {
345 rt_set_errno(result);
346 }
347
348 return level;
349 }
350
rt_wlan_dev_register_event_handler(struct rt_wlan_device * device,rt_wlan_dev_event_t event,rt_wlan_dev_event_handler handler,void * parameter)351 rt_err_t rt_wlan_dev_register_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler, void *parameter)
352 {
353 int i = 0;
354 rt_base_t level;
355
356 if (device == RT_NULL)
357 {
358 return -RT_EIO;
359 }
360 if (event >= RT_WLAN_DEV_EVT_MAX)
361 {
362 return -RT_EINVAL;
363 }
364
365 level = rt_hw_interrupt_disable();
366 for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
367 {
368 if (device->handler_table[event][i].handler == RT_NULL)
369 {
370 device->handler_table[event][i].handler = handler;
371 device->handler_table[event][i].parameter = parameter;
372 rt_hw_interrupt_enable(level);
373 return RT_EOK;
374 }
375 }
376 rt_hw_interrupt_enable(level);
377
378 /* No space found */
379 return -RT_ERROR;
380 }
381
rt_wlan_dev_unregister_event_handler(struct rt_wlan_device * device,rt_wlan_dev_event_t event,rt_wlan_dev_event_handler handler)382 rt_err_t rt_wlan_dev_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler)
383 {
384 int i = 0;
385 rt_base_t level;
386
387 if (device == RT_NULL)
388 {
389 return -RT_EIO;
390 }
391 if (event >= RT_WLAN_DEV_EVT_MAX)
392 {
393 return -RT_EINVAL;
394 }
395
396 level = rt_hw_interrupt_disable();
397 for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
398 {
399 if (device->handler_table[event][i].handler == handler)
400 {
401 rt_memset(&device->handler_table[event][i], 0, sizeof(struct rt_wlan_dev_event_desc));
402 rt_hw_interrupt_enable(level);
403 return RT_EOK;
404 }
405 }
406 rt_hw_interrupt_enable(level);
407 /* not find iteam */
408 return -RT_ERROR;
409 }
410
rt_wlan_dev_indicate_event_handle(struct rt_wlan_device * device,rt_wlan_dev_event_t event,struct rt_wlan_buff * buff)411 void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff)
412 {
413 void *parameter[RT_WLAN_DEV_EVENT_NUM] = {0};
414 rt_wlan_dev_event_handler handler[RT_WLAN_DEV_EVENT_NUM] = {0};
415 int i;
416 rt_base_t level;
417
418 if (device == RT_NULL)
419 {
420 return;
421 }
422 if (event >= RT_WLAN_DEV_EVT_MAX)
423 {
424 return;
425 }
426
427 /* get callback handle */
428 level = rt_hw_interrupt_disable();
429 for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
430 {
431 handler[i] = device->handler_table[event][i].handler;
432 parameter[i] = device->handler_table[event][i].parameter;
433 }
434 rt_hw_interrupt_enable(level);
435
436 /* run callback */
437 for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
438 {
439 if (handler[i] != RT_NULL)
440 {
441 handler[i](device, event, buff, parameter[i]);
442 }
443 }
444 }
445
rt_wlan_dev_enter_promisc(struct rt_wlan_device * device)446 rt_err_t rt_wlan_dev_enter_promisc(struct rt_wlan_device *device)
447 {
448 rt_err_t result = RT_EOK;
449 int enable = 1;
450
451 if (device == RT_NULL)
452 {
453 return -RT_EIO;
454 }
455
456 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
457 return result;
458 }
459
rt_wlan_dev_exit_promisc(struct rt_wlan_device * device)460 rt_err_t rt_wlan_dev_exit_promisc(struct rt_wlan_device *device)
461 {
462 rt_err_t result = RT_EOK;
463 int enable = 0;
464
465 if (device == RT_NULL)
466 {
467 return -RT_EIO;
468 }
469
470 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
471 return result;
472 }
473
rt_wlan_dev_set_promisc_callback(struct rt_wlan_device * device,rt_wlan_pormisc_callback_t callback)474 rt_err_t rt_wlan_dev_set_promisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback)
475 {
476 if (device == RT_NULL)
477 {
478 return -RT_EIO;
479 }
480 device->pormisc_callback = callback;
481
482 return RT_EOK;
483 }
484
rt_wlan_dev_promisc_handler(struct rt_wlan_device * device,void * data,int len)485 void rt_wlan_dev_promisc_handler(struct rt_wlan_device *device, void *data, int len)
486 {
487 rt_wlan_pormisc_callback_t callback;
488
489 if (device == RT_NULL)
490 {
491 return;
492 }
493
494 callback = device->pormisc_callback;
495
496 if (callback != RT_NULL)
497 {
498 callback(device, data, len);
499 }
500 }
501
rt_wlan_dev_cfg_filter(struct rt_wlan_device * device,struct rt_wlan_filter * filter)502 rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter)
503 {
504 rt_err_t result = RT_EOK;
505
506 if (device == RT_NULL)
507 {
508 return -RT_EIO;
509 }
510 if (filter == RT_NULL)
511 {
512 return -RT_ERROR;
513 }
514
515 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_FILTER, filter);
516 return result;
517 }
518
rt_wlan_dev_set_channel(struct rt_wlan_device * device,int channel)519 rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel)
520 {
521 rt_err_t result = RT_EOK;
522
523 if (device == RT_NULL)
524 {
525 return -RT_EIO;
526 }
527 if (channel < 0)
528 {
529 return -RT_ERROR;
530 }
531
532 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_CHANNEL, &channel);
533 return result;
534 }
535
rt_wlan_dev_get_channel(struct rt_wlan_device * device)536 int rt_wlan_dev_get_channel(struct rt_wlan_device *device)
537 {
538 rt_err_t result = RT_EOK;
539 int channel = -1;
540
541 if (device == RT_NULL)
542 {
543 rt_set_errno(-RT_EIO);
544 return -1;
545 }
546
547 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_CHANNEL, &channel);
548 if (result != RT_EOK)
549 {
550 rt_set_errno(result);
551 return -1;
552 }
553
554 return channel;
555 }
556
rt_wlan_dev_set_country(struct rt_wlan_device * device,rt_country_code_t country_code)557 rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code)
558 {
559 int result = RT_EOK;
560
561 if (device == RT_NULL)
562 {
563 return -RT_EIO;
564 }
565
566 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_COUNTRY, &country_code);
567 return result;
568 }
569
rt_wlan_dev_get_country(struct rt_wlan_device * device)570 rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device)
571 {
572 int result = RT_EOK;
573 rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
574
575 if (device == RT_NULL)
576 {
577 rt_set_errno(-RT_EIO);
578 return RT_COUNTRY_UNKNOWN;
579 }
580
581 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_COUNTRY, &country_code);
582 if (result != RT_EOK)
583 {
584 rt_set_errno(result);
585 return RT_COUNTRY_UNKNOWN;
586 }
587
588 return country_code;
589 }
590
rt_wlan_dev_scan(struct rt_wlan_device * device,struct rt_wlan_info * info)591 rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info)
592 {
593 struct rt_scan_info scan_info = { 0 };
594 struct rt_scan_info *p_scan_info = RT_NULL;
595 rt_err_t result = 0;
596
597 if (device == RT_NULL)
598 {
599 return -RT_EIO;
600 }
601
602 if (info != RT_NULL)
603 {
604 if (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
605 {
606 LOG_E("L:%d ssid is too long", __LINE__);
607 return -RT_EINVAL;
608 }
609 rt_memcpy(&scan_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
610 rt_memcpy(scan_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
611 if (info->channel > 0)
612 {
613 scan_info.channel_min = info->channel;
614 scan_info.channel_max = info->channel;
615 }
616 else
617 {
618 scan_info.channel_min = -1;
619 scan_info.channel_max = -1;
620 }
621 scan_info.passive = info->hidden ? RT_TRUE : RT_FALSE;
622 p_scan_info = &scan_info;
623 }
624 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info);
625 return result;
626 }
627
rt_wlan_dev_scan_stop(struct rt_wlan_device * device)628 rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device)
629 {
630 rt_err_t result = 0;
631
632 if (device == RT_NULL)
633 {
634 return -RT_EIO;
635 }
636
637 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL);
638 return result;
639 }
640
rt_wlan_dev_report_data(struct rt_wlan_device * device,void * buff,int len)641 rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len)
642 {
643 #ifdef RT_WLAN_PROT_ENABLE
644 return rt_wlan_dev_transfer_prot(device, buff, len);
645 #else
646 return -RT_ERROR;
647 #endif
648 }
649
rt_wlan_dev_enter_mgnt_filter(struct rt_wlan_device * device)650 rt_err_t rt_wlan_dev_enter_mgnt_filter(struct rt_wlan_device *device)
651 {
652 rt_err_t result = RT_EOK;
653 int enable = 1;
654
655 if (device == RT_NULL)
656 {
657 return -RT_EIO;
658 }
659
660 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_MGNT_FILTER, &enable);
661 return result;
662 }
663
rt_wlan_dev_exit_mgnt_filter(struct rt_wlan_device * device)664 rt_err_t rt_wlan_dev_exit_mgnt_filter(struct rt_wlan_device *device)
665 {
666 rt_err_t result = RT_EOK;
667 int enable = 0;
668
669 if (device == RT_NULL)
670 {
671 return -RT_EIO;
672 }
673
674 result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_MGNT_FILTER, &enable);
675 return result;
676 }
677
rt_wlan_dev_set_mgnt_filter_callback(struct rt_wlan_device * device,rt_wlan_mgnt_filter_callback_t callback)678 rt_err_t rt_wlan_dev_set_mgnt_filter_callback(struct rt_wlan_device *device, rt_wlan_mgnt_filter_callback_t callback)
679 {
680 if (device == RT_NULL)
681 {
682 return -RT_EIO;
683 }
684 device->mgnt_filter_callback = callback;
685
686 return RT_EOK;
687 }
688
rt_wlan_dev_mgnt_filter_handler(struct rt_wlan_device * device,void * data,int len)689 void rt_wlan_dev_mgnt_filter_handler(struct rt_wlan_device *device, void *data, int len)
690 {
691 rt_wlan_mgnt_filter_callback_t callback;
692
693 if (device == RT_NULL)
694 {
695 return;
696 }
697
698 callback = device->mgnt_filter_callback;
699
700 if (callback != RT_NULL)
701 {
702 callback(device, data, len);
703 }
704 }
705
rt_wlan_dev_send_raw_frame(struct rt_wlan_device * device,void * buff,int len)706 int rt_wlan_dev_send_raw_frame(struct rt_wlan_device *device, void *buff, int len)
707 {
708 if (device == RT_NULL)
709 {
710 return -RT_EIO;
711 }
712
713 if (device->ops->wlan_send_raw_frame)
714 {
715 return device->ops->wlan_send_raw_frame(device, buff, len);
716 }
717
718 return -RT_ERROR;
719 }
720
_rt_wlan_dev_init(rt_device_t dev)721 static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
722 {
723 struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
724 rt_err_t result = RT_EOK;
725
726 rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_PRIO);
727
728 if (wlan->ops->wlan_init)
729 result = wlan->ops->wlan_init(wlan);
730
731 if (result == RT_EOK)
732 {
733 LOG_I("wlan init success");
734 }
735 else
736 {
737 LOG_I("wlan init failed");
738 }
739
740 return result;
741 }
742
_rt_wlan_dev_control(rt_device_t dev,int cmd,void * args)743 static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
744 {
745 struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
746 rt_err_t err = RT_EOK;
747
748 RT_ASSERT(dev != RT_NULL);
749
750 WLAN_DEV_LOCK(wlan);
751
752 switch (cmd)
753 {
754 case RT_WLAN_CMD_MODE:
755 {
756 rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
757
758 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
759 if (wlan->ops->wlan_mode)
760 err = wlan->ops->wlan_mode(wlan, mode);
761 break;
762 }
763 case RT_WLAN_CMD_SCAN:
764 {
765 struct rt_scan_info *scan_info = args;
766
767 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
768 if (wlan->ops->wlan_scan)
769 err = wlan->ops->wlan_scan(wlan, scan_info);
770 break;
771 }
772 case RT_WLAN_CMD_JOIN:
773 {
774 struct rt_sta_info *sta_info = args;
775
776 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
777 if (wlan->ops->wlan_join)
778 err = wlan->ops->wlan_join(wlan, sta_info);
779 break;
780 }
781 case RT_WLAN_CMD_SOFTAP:
782 {
783 struct rt_ap_info *ap_info = args;
784
785 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
786 if (wlan->ops->wlan_softap)
787 err = wlan->ops->wlan_softap(wlan, ap_info);
788 break;
789 }
790 case RT_WLAN_CMD_DISCONNECT:
791 {
792 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
793 if (wlan->ops->wlan_disconnect)
794 err = wlan->ops->wlan_disconnect(wlan);
795 break;
796 }
797 case RT_WLAN_CMD_AP_STOP:
798 {
799 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
800 if (wlan->ops->wlan_ap_stop)
801 err = wlan->ops->wlan_ap_stop(wlan);
802 break;
803 }
804 case RT_WLAN_CMD_AP_DEAUTH:
805 {
806 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
807 if (wlan->ops->wlan_ap_deauth)
808 err = wlan->ops->wlan_ap_deauth(wlan, args);
809 break;
810 }
811 case RT_WLAN_CMD_SCAN_STOP:
812 {
813 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
814 if (wlan->ops->wlan_scan_stop)
815 err = wlan->ops->wlan_scan_stop(wlan);
816 break;
817 }
818 case RT_WLAN_CMD_GET_RSSI:
819 {
820 int *rssi = args;
821
822 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
823 if (wlan->ops->wlan_get_rssi)
824 *rssi = wlan->ops->wlan_get_rssi(wlan);
825 break;
826 }
827 case RT_WLAN_CMD_GET_INFO:
828 {
829 struct rt_wlan_info *info = args;
830
831 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_INFO, "RT_WLAN_CMD_GET_INFO");
832 if (wlan->ops->wlan_get_info)
833 err = wlan->ops->wlan_get_info(wlan, info);
834 else
835 err = -RT_ERROR;
836 break;
837 }
838 case RT_WLAN_CMD_AP_GET_INFO:
839 {
840 struct rt_wlan_info *info = args;
841
842 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_GET_INFO, "RT_WLAN_CMD_AP_GET_INFO");
843 if (wlan->ops->wlan_ap_get_info)
844 err = wlan->ops->wlan_ap_get_info(wlan, info);
845 break;
846 }
847 case RT_WLAN_CMD_SET_POWERSAVE:
848 {
849 int level = *((int *)args);
850
851 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_POWERSAVE, "RT_WLAN_CMD_SET_POWERSAVE");
852 if (wlan->ops->wlan_set_powersave)
853 err = wlan->ops->wlan_set_powersave(wlan, level);
854 break;
855 }
856 case RT_WLAN_CMD_GET_POWERSAVE:
857 {
858 int *level = args;
859
860 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_POWERSAVE, "RT_WLAN_CMD_GET_POWERSAVE");
861 if (wlan->ops->wlan_get_powersave)
862 *level = wlan->ops->wlan_get_powersave(wlan);
863 break;
864 }
865 case RT_WLAN_CMD_CFG_PROMISC:
866 {
867 rt_bool_t start = *((rt_bool_t *)args);
868
869 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
870 if (wlan->ops->wlan_cfg_promisc)
871 err = wlan->ops->wlan_cfg_promisc(wlan, start);
872 break;
873 }
874 case RT_WLAN_CMD_CFG_FILTER:
875 {
876 struct rt_wlan_filter *filter = args;
877
878 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
879 if (wlan->ops->wlan_cfg_filter)
880 err = wlan->ops->wlan_cfg_filter(wlan, filter);
881 break;
882 }
883 case RT_WLAN_CMD_CFG_MGNT_FILTER:
884 {
885 rt_bool_t start = *((rt_bool_t *)args);
886
887 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_MGNT_FILTER, "RT_WLAN_CMD_CFG_MGNT_FILTER");
888 if (wlan->ops->wlan_cfg_mgnt_filter)
889 err = wlan->ops->wlan_cfg_mgnt_filter(wlan, start);
890 break;
891 }
892 case RT_WLAN_CMD_SET_CHANNEL:
893 {
894 int channel = *(int *)args;
895 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
896 if (wlan->ops->wlan_set_channel)
897 err = wlan->ops->wlan_set_channel(wlan, channel);
898 break;
899 }
900 case RT_WLAN_CMD_GET_CHANNEL:
901 {
902 int *channel = args;
903
904 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
905 if (wlan->ops->wlan_get_channel)
906 *channel = wlan->ops->wlan_get_channel(wlan);
907 break;
908 }
909 case RT_WLAN_CMD_SET_COUNTRY:
910 {
911 rt_country_code_t country = *(rt_country_code_t *)args;
912
913 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
914 if (wlan->ops->wlan_set_country)
915 err = wlan->ops->wlan_set_country(wlan, country);
916 break;
917 }
918 case RT_WLAN_CMD_GET_COUNTRY:
919 {
920 rt_country_code_t *country = args;
921 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
922 if (wlan->ops->wlan_get_country)
923 *country = wlan->ops->wlan_get_country(wlan);
924 break;
925 }
926 case RT_WLAN_CMD_SET_MAC:
927 {
928 rt_uint8_t *mac = args;
929
930 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
931 if (wlan->ops->wlan_set_mac)
932 err = wlan->ops->wlan_set_mac(wlan, mac);
933 break;
934 }
935 case RT_WLAN_CMD_GET_MAC:
936 {
937 rt_uint8_t *mac = args;
938
939 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
940 if (wlan->ops->wlan_get_mac)
941 err = wlan->ops->wlan_get_mac(wlan, mac);
942 break;
943 }
944 case RT_WLAN_CMD_GET_FAST_CONNECT_INFO:
945 {
946
947 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_FAST_INFO, "RT_WLAN_CMD_GET_FAST_INFO");
948 if (wlan->ops->wlan_get_fast_info)
949 {
950 err = wlan->ops->wlan_get_fast_info(args);
951 }
952 else
953 {
954 err = -RT_EEMPTY;
955 }
956 break;
957 }
958 case RT_WLAN_CMD_FAST_CONNECT:
959 {
960 struct rt_wlan_buff *buff = (struct rt_wlan_buff *)args;
961 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_FAST_CONNECT, "RT_WLAN_CMD_FAST_CONNECT");
962 if (wlan->ops->wlan_get_fast_info)
963 {
964 err = wlan->ops->wlan_fast_connect(buff->data,buff->len);
965 }
966 else
967 {
968 err = -RT_EEMPTY;
969 }
970 break;
971 }
972
973 default:
974 LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
975 break;
976 }
977
978 WLAN_DEV_UNLOCK(wlan);
979
980 return err;
981 }
982
983 #ifdef RT_USING_DEVICE_OPS
984 const static struct rt_device_ops wlan_ops =
985 {
986 _rt_wlan_dev_init,
987 RT_NULL,
988 RT_NULL,
989 RT_NULL,
990 RT_NULL,
991 _rt_wlan_dev_control
992 };
993 #endif
994
rt_wlan_dev_register(struct rt_wlan_device * wlan,const char * name,const struct rt_wlan_dev_ops * ops,rt_uint32_t flag,void * user_data)995 rt_err_t rt_wlan_dev_register(struct rt_wlan_device *wlan, const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data)
996 {
997 rt_err_t err = RT_EOK;
998
999 if ((wlan == RT_NULL) || (name == RT_NULL) || (ops == RT_NULL) ||
1000 (flag & RT_WLAN_FLAG_STA_ONLY && flag & RT_WLAN_FLAG_AP_ONLY))
1001 {
1002 LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
1003 return RT_NULL;
1004 }
1005
1006 rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
1007
1008 #ifdef RT_USING_DEVICE_OPS
1009 wlan->device.ops = &wlan_ops;
1010 #else
1011 wlan->device.init = _rt_wlan_dev_init;
1012 wlan->device.open = RT_NULL;
1013 wlan->device.close = RT_NULL;
1014 wlan->device.read = RT_NULL;
1015 wlan->device.write = RT_NULL;
1016 wlan->device.control = _rt_wlan_dev_control;
1017 #endif
1018
1019 wlan->device.user_data = RT_NULL;
1020
1021 wlan->device.type = RT_Device_Class_NetIf;
1022
1023 wlan->ops = ops;
1024 wlan->user_data = user_data;
1025
1026 wlan->flags = flag;
1027 err = rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
1028
1029 LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
1030
1031 return err;
1032 }
1033
1034 #endif
1035