xref: /nrf52832-nimble/rt-thread/components/drivers/wlan/wlan_dev.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, 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  */
10 
11 #include <rthw.h>
12 #include <rtthread.h>
13 #include <wlan_dev.h>
14 #include <wlan_prot.h>
15 
16 #define DBG_ENABLE
17 #ifdef RT_WLAN_DEV_DEBUG
18 #define DBG_LEVEL DBG_LOG
19 #else
20 #define DBG_LEVEL DBG_INFO
21 #endif
22 #define DBG_SECTION_NAME  "WLAN.dev"
23 #define DBG_COLOR
24 #include <rtdbg.h>
25 
26 #ifndef RT_DEVICE
27 #define RT_DEVICE(__device) ((rt_device_t)__device)
28 #endif
29 
30 #define WLAN_DEV_LOCK(_wlan)      (rt_mutex_take(&(_wlan)->lock, RT_WAITING_FOREVER))
31 #define WLAN_DEV_UNLOCK(_wlan)    (rt_mutex_release(&(_wlan)->lock))
32 
33 #if RT_WLAN_SSID_MAX_LENGTH < 1
34 #error "SSID length is too short"
35 #endif
36 
37 #if RT_WLAN_BSSID_MAX_LENGTH < 1
38 #error "BSSID length is too short"
39 #endif
40 
41 #if RT_WLAN_PASSWORD_MAX_LENGTH < 1
42 #error "password length is too short"
43 #endif
44 
45 #if RT_WLAN_DEV_EVENT_NUM < 2
46 #error "dev num Too little"
47 #endif
48 
rt_wlan_dev_init(struct rt_wlan_device * device,rt_wlan_mode_t mode)49 rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
50 {
51     rt_err_t result = RT_EOK;
52 
53     /* init wlan device */
54     LOG_D("F:%s L:%d is run device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
55     if ((device == RT_NULL) || (mode >= RT_WLAN_MODE_MAX))
56     {
57         LOG_E("F:%s L:%d Parameter Wrongful device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
58         return -RT_ERROR;
59     }
60 
61     result = rt_device_init(RT_DEVICE(device));
62     if (result != RT_EOK)
63     {
64         LOG_E("L:%d wlan init failed", __LINE__);
65         return -RT_ERROR;
66     }
67     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_MODE, (void *)&mode);
68     if (result != RT_EOK)
69     {
70         LOG_E("L:%d wlan config mode failed", __LINE__);
71         return -RT_ERROR;
72     }
73     device->mode = mode;
74     return result;
75 }
76 
rt_wlan_dev_connect(struct rt_wlan_device * device,struct rt_wlan_info * info,const char * password,int password_len)77 rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
78 {
79     rt_err_t result = RT_EOK;
80     struct rt_sta_info sta_info;
81 
82     if (device == RT_NULL)
83     {
84         return -RT_EIO;
85     }
86     if (info == RT_NULL)
87     {
88         return -RT_ERROR;
89     }
90 
91     if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) ||
92             (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH))
93     {
94         LOG_E("L:%d password or ssid is to long", __LINE__);
95         return -RT_ERROR;
96     }
97     rt_memset(&sta_info, 0, sizeof(struct rt_sta_info));
98     rt_memcpy(&sta_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
99     rt_memcpy(sta_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
100     if (password != RT_NULL)
101     {
102         rt_memcpy(sta_info.key.val, password, password_len);
103         sta_info.key.len = password_len;
104     }
105     sta_info.channel = info->channel;
106     sta_info.security = info->security;
107 
108     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_JOIN, &sta_info);
109     return result;
110 }
111 
rt_wlan_dev_disconnect(struct rt_wlan_device * device)112 rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device)
113 {
114     rt_err_t result = RT_EOK;
115 
116     if (device == RT_NULL)
117     {
118         return -RT_EIO;
119     }
120 
121     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_DISCONNECT, RT_NULL);
122     return result;
123 }
124 
rt_wlan_dev_ap_start(struct rt_wlan_device * device,struct rt_wlan_info * info,const char * password,int password_len)125 rt_err_t rt_wlan_dev_ap_start(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_ap_info ap_info;
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 to long", __LINE__);
143         return -RT_ERROR;
144     }
145 
146     rt_memset(&ap_info, 0, sizeof(struct rt_ap_info));
147     rt_memcpy(&ap_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
148     if (password != RT_NULL)
149     {
150         rt_memcpy(ap_info.key.val, password, password_len);
151     }
152     ap_info.key.len = password_len;
153     ap_info.hidden = info->hidden;
154     ap_info.channel = info->channel;
155     ap_info.security = info->security;
156 
157     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SOFTAP, &ap_info);
158     return result;
159 }
160 
rt_wlan_dev_ap_stop(struct rt_wlan_device * device)161 rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device)
162 {
163     rt_err_t result = RT_EOK;
164 
165     if (device == RT_NULL)
166     {
167         return -RT_EIO;
168     }
169 
170     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_STOP, RT_NULL);
171     return result;
172 }
173 
rt_wlan_dev_ap_deauth(struct rt_wlan_device * device,rt_uint8_t mac[6])174 rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6])
175 {
176     rt_err_t result = RT_EOK;
177 
178     if (device == RT_NULL)
179     {
180         return -RT_EIO;
181     }
182 
183     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_DEAUTH, mac);
184     return result;
185 }
186 
rt_wlan_dev_get_rssi(struct rt_wlan_device * device)187 int rt_wlan_dev_get_rssi(struct rt_wlan_device *device)
188 {
189     int rssi = 0;
190     rt_err_t result = RT_EOK;
191 
192     if (device == RT_NULL)
193     {
194         rt_set_errno(-RT_EIO);
195         return 0;
196     }
197 
198     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_RSSI, &rssi);
199     if (result != RT_EOK)
200     {
201         rt_set_errno(result);
202         return 0;
203     }
204 
205     return rssi;
206 }
207 
rt_wlan_dev_get_mac(struct rt_wlan_device * device,rt_uint8_t mac[6])208 rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
209 {
210     rt_err_t result = RT_EOK;
211 
212     if (device == RT_NULL)
213     {
214         return -RT_EIO;
215     }
216 
217     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_MAC, &mac[0]);
218     return result;
219 }
220 
rt_wlan_dev_set_mac(struct rt_wlan_device * device,rt_uint8_t mac[6])221 rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
222 {
223     rt_err_t result = RT_EOK;
224 
225     if (device == RT_NULL)
226     {
227         return -RT_EIO;
228     }
229 
230     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_MAC, &mac[0]);
231     return result;
232 }
233 
rt_wlan_dev_set_powersave(struct rt_wlan_device * device,int level)234 rt_err_t rt_wlan_dev_set_powersave(struct rt_wlan_device *device, int level)
235 {
236     rt_err_t result = RT_EOK;
237 
238     if (device == RT_NULL)
239     {
240         return -RT_EIO;
241     }
242 
243     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_POWERSAVE, &level);
244     return result;
245 }
246 
rt_wlan_dev_get_powersave(struct rt_wlan_device * device)247 int rt_wlan_dev_get_powersave(struct rt_wlan_device *device)
248 {
249     int level = -1;
250     rt_err_t result = RT_EOK;
251 
252     if (device == RT_NULL)
253     {
254         rt_set_errno(-RT_EIO);
255         return -1;
256     }
257 
258     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_POWERSAVE, &level);
259     if (result != RT_EOK)
260     {
261         rt_set_errno(result);
262     }
263 
264     return level;
265 }
266 
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)267 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)
268 {
269     int i = 0;
270     rt_base_t level;
271 
272     if (device == RT_NULL)
273     {
274         return -RT_EIO;
275     }
276     if (event >= RT_WLAN_DEV_EVT_MAX)
277     {
278         return -RT_EINVAL;
279     }
280 
281     level = rt_hw_interrupt_disable();
282     for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
283     {
284         if (device->handler_table[event][i].handler == RT_NULL)
285         {
286             device->handler_table[event][i].handler = handler;
287             device->handler_table[event][i].parameter = parameter;
288             rt_hw_interrupt_enable(level);
289             return RT_EOK;
290         }
291     }
292     rt_hw_interrupt_enable(level);
293 
294     /* No space found */
295     return -RT_ERROR;
296 }
297 
rt_wlan_dev_unregister_event_handler(struct rt_wlan_device * device,rt_wlan_dev_event_t event,rt_wlan_dev_event_handler handler)298 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)
299 {
300     int i = 0;
301     rt_base_t level;
302 
303     if (device == RT_NULL)
304     {
305         return -RT_EIO;
306     }
307     if (event >= RT_WLAN_DEV_EVT_MAX)
308     {
309         return -RT_EINVAL;
310     }
311 
312     level = rt_hw_interrupt_disable();
313     for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
314     {
315         if (device->handler_table[event][i].handler == handler)
316         {
317             rt_memset(&device->handler_table[event][i], 0, sizeof(struct rt_wlan_dev_event_desc));
318             rt_hw_interrupt_enable(level);
319             return RT_EOK;
320         }
321     }
322     rt_hw_interrupt_enable(level);
323     /* not find iteam */
324     return -RT_ERROR;
325 }
326 
rt_wlan_dev_indicate_event_handle(struct rt_wlan_device * device,rt_wlan_dev_event_t event,struct rt_wlan_buff * buff)327 void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff)
328 {
329     void *parameter[RT_WLAN_DEV_EVENT_NUM];
330     rt_wlan_dev_event_handler handler[RT_WLAN_DEV_EVENT_NUM];
331     int i;
332     rt_base_t level;
333 
334     if (device == RT_NULL)
335     {
336         return;
337     }
338     if (event >= RT_WLAN_DEV_EVT_MAX)
339     {
340         return;
341     }
342 
343     /* get callback handle */
344     level = rt_hw_interrupt_disable();
345     for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
346     {
347         handler[i] = device->handler_table[event][i].handler;
348         parameter[i] = device->handler_table[event][i].parameter;
349     }
350     rt_hw_interrupt_enable(level);
351 
352     /* run callback */
353     for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
354     {
355         if (handler[i] != RT_NULL)
356         {
357             handler[i](device, event, buff, parameter[i]);
358         }
359     }
360 }
361 
rt_wlan_dev_enter_promisc(struct rt_wlan_device * device)362 rt_err_t rt_wlan_dev_enter_promisc(struct rt_wlan_device *device)
363 {
364     rt_err_t result = RT_EOK;
365     int enable = 1;
366 
367     if (device == RT_NULL)
368     {
369         return -RT_EIO;
370     }
371 
372     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
373     return result;
374 }
375 
rt_wlan_dev_exit_promisc(struct rt_wlan_device * device)376 rt_err_t rt_wlan_dev_exit_promisc(struct rt_wlan_device *device)
377 {
378     rt_err_t result = RT_EOK;
379     int enable = 0;
380 
381     if (device == RT_NULL)
382     {
383         return -RT_EIO;
384     }
385 
386     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
387     return result;
388 }
389 
rt_wlan_dev_set_promisc_callback(struct rt_wlan_device * device,rt_wlan_pormisc_callback_t callback)390 rt_err_t rt_wlan_dev_set_promisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback)
391 {
392     if (device == RT_NULL)
393     {
394         return -RT_EIO;
395     }
396     device->pormisc_callback = callback;
397 
398     return RT_EOK;
399 }
400 
rt_wlan_dev_promisc_handler(struct rt_wlan_device * device,void * data,int len)401 void rt_wlan_dev_promisc_handler(struct rt_wlan_device *device, void *data, int len)
402 {
403     rt_wlan_pormisc_callback_t callback;
404 
405     if (device == RT_NULL)
406     {
407         return;
408     }
409 
410     callback = device->pormisc_callback;
411 
412     if (callback != RT_NULL)
413     {
414         callback(device, data, len);
415     }
416 }
417 
rt_wlan_dev_cfg_filter(struct rt_wlan_device * device,struct rt_wlan_filter * filter)418 rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter)
419 {
420     rt_err_t result = RT_EOK;
421 
422     if (device == RT_NULL)
423     {
424         return -RT_EIO;
425     }
426     if (filter == RT_NULL)
427     {
428         return -RT_ERROR;
429     }
430 
431     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_FILTER, filter);
432     return result;
433 }
434 
rt_wlan_dev_set_channel(struct rt_wlan_device * device,int channel)435 rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel)
436 {
437     rt_err_t result = RT_EOK;
438 
439     if (device == RT_NULL)
440     {
441         return -RT_EIO;
442     }
443     if (channel < 0)
444     {
445         return -RT_ERROR;
446     }
447 
448     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_CHANNEL, &channel);
449     return result;
450 }
451 
rt_wlan_dev_get_channel(struct rt_wlan_device * device)452 int rt_wlan_dev_get_channel(struct rt_wlan_device *device)
453 {
454     rt_err_t result = RT_EOK;
455     int channel = -1;
456 
457     if (device == RT_NULL)
458     {
459         rt_set_errno(-RT_EIO);
460         return -1;
461     }
462 
463     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_CHANNEL, &channel);
464     if (result != RT_EOK)
465     {
466         rt_set_errno(result);
467         return -1;
468     }
469 
470     return channel;
471 }
472 
rt_wlan_dev_set_country(struct rt_wlan_device * device,rt_country_code_t country_code)473 rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code)
474 {
475     int result = RT_EOK;
476 
477     if (device == RT_NULL)
478     {
479         return -RT_EIO;
480     }
481 
482     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_COUNTRY, &country_code);
483     return result;
484 }
485 
rt_wlan_dev_get_country(struct rt_wlan_device * device)486 rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device)
487 {
488     int result = RT_EOK;
489     rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
490 
491     if (device == RT_NULL)
492     {
493         rt_set_errno(-RT_EIO);
494         return RT_COUNTRY_UNKNOWN;
495     }
496 
497     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_COUNTRY, &country_code);
498     if (result != RT_EOK)
499     {
500         rt_set_errno(result);
501         return RT_COUNTRY_UNKNOWN;
502     }
503 
504     return country_code;
505 }
506 
rt_wlan_dev_scan(struct rt_wlan_device * device,struct rt_wlan_info * info)507 rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info)
508 {
509     struct rt_scan_info scan_info = { 0 };
510     struct rt_scan_info *p_scan_info = RT_NULL;
511     rt_err_t result = 0;
512 
513     if (device == RT_NULL)
514     {
515         return -RT_EIO;
516     }
517 
518     if (info != RT_NULL)
519     {
520         if (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH)
521         {
522             LOG_E("L:%d ssid is to long", __LINE__);
523             return -RT_EINVAL;
524         }
525         rt_memcpy(&scan_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
526         rt_memcpy(scan_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
527         scan_info.channel_min = -1;
528         scan_info.channel_max = -1;
529         p_scan_info = &scan_info;
530     }
531     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info);
532     return result;
533 }
534 
rt_wlan_dev_scan_stop(struct rt_wlan_device * device)535 rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device)
536 {
537     rt_err_t result = 0;
538 
539     if (device == RT_NULL)
540     {
541         return -RT_EIO;
542     }
543 
544     result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL);
545     return result;
546 }
547 
rt_wlan_dev_report_data(struct rt_wlan_device * device,void * buff,int len)548 rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len)
549 {
550     return rt_wlan_dev_transfer_prot(device, buff, len);
551 }
552 
_rt_wlan_dev_init(rt_device_t dev)553 static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
554 {
555     struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
556     rt_err_t result = RT_EOK;
557 
558     rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_FIFO);
559 
560     if (wlan->ops->wlan_init)
561         result = wlan->ops->wlan_init(wlan);
562 
563     if (result == RT_EOK)
564     {
565         LOG_I("wlan init success");
566     }
567     else
568     {
569         LOG_I("wlan init failed");
570     }
571 
572     return result;
573 }
574 
_rt_wlan_dev_control(rt_device_t dev,int cmd,void * args)575 static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
576 {
577     struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
578     rt_err_t err = RT_EOK;
579 
580     RT_ASSERT(dev != RT_NULL);
581 
582     WLAN_DEV_LOCK(wlan);
583 
584     switch (cmd)
585     {
586     case RT_WLAN_CMD_MODE:
587     {
588         rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
589 
590         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
591         if (wlan->ops->wlan_mode)
592             err = wlan->ops->wlan_mode(wlan, mode);
593         break;
594     }
595     case RT_WLAN_CMD_SCAN:
596     {
597         struct rt_scan_info *scan_info = args;
598 
599         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
600         if (wlan->ops->wlan_scan)
601             err = wlan->ops->wlan_scan(wlan, scan_info);
602         break;
603     }
604     case RT_WLAN_CMD_JOIN:
605     {
606         struct rt_sta_info *sta_info = args;
607 
608         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
609         if (wlan->ops->wlan_join)
610             err = wlan->ops->wlan_join(wlan, sta_info);
611         break;
612     }
613     case RT_WLAN_CMD_SOFTAP:
614     {
615         struct rt_ap_info *ap_info = args;
616 
617         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
618         if (wlan->ops->wlan_softap)
619             err = wlan->ops->wlan_softap(wlan, ap_info);
620         break;
621     }
622     case RT_WLAN_CMD_DISCONNECT:
623     {
624         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
625         if (wlan->ops->wlan_disconnect)
626             err = wlan->ops->wlan_disconnect(wlan);
627         break;
628     }
629     case RT_WLAN_CMD_AP_STOP:
630     {
631         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
632         if (wlan->ops->wlan_ap_stop)
633             err = wlan->ops->wlan_ap_stop(wlan);
634         break;
635     }
636     case RT_WLAN_CMD_AP_DEAUTH:
637     {
638         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
639         if (wlan->ops->wlan_ap_deauth)
640             err = wlan->ops->wlan_ap_deauth(wlan, args);
641         break;
642     }
643     case RT_WLAN_CMD_SCAN_STOP:
644     {
645         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
646         if (wlan->ops->wlan_scan_stop)
647             err = wlan->ops->wlan_scan_stop(wlan);
648         break;
649     }
650     case RT_WLAN_CMD_GET_RSSI:
651     {
652         int *rssi = args;
653 
654         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
655         if (wlan->ops->wlan_get_rssi)
656             *rssi = wlan->ops->wlan_get_rssi(wlan);
657         break;
658     }
659     case RT_WLAN_CMD_SET_POWERSAVE:
660     {
661         int level = *((int *)args);
662 
663         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_POWERSAVE, "RT_WLAN_CMD_SET_POWERSAVE");
664         if (wlan->ops->wlan_set_powersave)
665             err = wlan->ops->wlan_set_powersave(wlan, level);
666         break;
667     }
668     case RT_WLAN_CMD_GET_POWERSAVE:
669     {
670         int *level = args;
671 
672         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_POWERSAVE, "RT_WLAN_CMD_GET_POWERSAVE");
673         if (wlan->ops->wlan_get_powersave)
674             *level = wlan->ops->wlan_get_powersave(wlan);
675         break;
676     }
677     case RT_WLAN_CMD_CFG_PROMISC:
678     {
679         rt_bool_t start = *((rt_bool_t *)args);
680 
681         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
682         if (wlan->ops->wlan_cfg_promisc)
683             err = wlan->ops->wlan_cfg_promisc(wlan, start);
684         break;
685     }
686     case RT_WLAN_CMD_CFG_FILTER:
687     {
688         struct rt_wlan_filter *filter = args;
689 
690         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
691         if (wlan->ops->wlan_cfg_filter)
692             err = wlan->ops->wlan_cfg_filter(wlan, filter);
693         break;
694     }
695     case RT_WLAN_CMD_SET_CHANNEL:
696     {
697         int channel = *(int *)args;
698         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
699         if (wlan->ops->wlan_set_channel)
700             err = wlan->ops->wlan_set_channel(wlan, channel);
701         break;
702     }
703     case RT_WLAN_CMD_GET_CHANNEL:
704     {
705         int *channel = args;
706 
707         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
708         if (wlan->ops->wlan_get_channel)
709             *channel = wlan->ops->wlan_get_channel(wlan);
710         break;
711     }
712     case RT_WLAN_CMD_SET_COUNTRY:
713     {
714         rt_country_code_t country = *(rt_country_code_t *)args;
715 
716         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
717         if (wlan->ops->wlan_set_country)
718             err = wlan->ops->wlan_set_country(wlan, country);
719         break;
720     }
721     case RT_WLAN_CMD_GET_COUNTRY:
722     {
723         rt_country_code_t *country = args;
724         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
725         if (wlan->ops->wlan_get_country)
726             *country = wlan->ops->wlan_get_country(wlan);
727         break;
728     }
729     case RT_WLAN_CMD_SET_MAC:
730     {
731         rt_uint8_t *mac = args;
732 
733         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
734         if (wlan->ops->wlan_set_mac)
735             err = wlan->ops->wlan_set_mac(wlan, mac);
736         break;
737     }
738     case RT_WLAN_CMD_GET_MAC:
739     {
740         rt_uint8_t *mac = args;
741 
742         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
743         if (wlan->ops->wlan_get_mac)
744             err = wlan->ops->wlan_get_mac(wlan, mac);
745         break;
746     }
747     default:
748         LOG_D("%s %d cmd[%d]:%s  run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
749         break;
750     }
751 
752     WLAN_DEV_UNLOCK(wlan);
753 
754     return err;
755 }
756 
757 #ifdef RT_USING_DEVICE_OPS
758 const static struct rt_device_ops wlan_ops =
759 {
760     _rt_wlan_dev_init,
761     RT_NULL,
762     RT_NULL,
763     RT_NULL,
764     RT_NULL,
765     _rt_wlan_dev_control
766 };
767 #endif
768 
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)769 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)
770 {
771     rt_err_t err = RT_EOK;
772 
773     if ((wlan == RT_NULL) || (name == RT_NULL) || (ops == RT_NULL))
774     {
775         LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
776         return RT_NULL;
777     }
778 
779     rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
780 
781 #ifdef RT_USING_DEVICE_OPS
782     wlan->device.ops = &wlan_ops;
783 #else
784     wlan->device.init       = _rt_wlan_dev_init;
785     wlan->device.open       = RT_NULL;
786     wlan->device.close      = RT_NULL;
787     wlan->device.read       = RT_NULL;
788     wlan->device.write      = RT_NULL;
789     wlan->device.control    = _rt_wlan_dev_control;
790 #endif
791 
792     wlan->device.user_data  = RT_NULL;
793 
794     wlan->device.type = RT_Device_Class_NetIf;
795 
796     wlan->ops = ops;
797     wlan->user_data  = user_data;
798 
799     wlan->flags = flag;
800     err = rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
801 
802     LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
803 
804     return err;
805 }
806