wlan_dev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <wlan_dev.h>
  13. #include <wlan_prot.h>
  14. #define DBG_ENABLE
  15. #define DBG_LEVEL DBG_INFO
  16. #define DBG_SECTION_NAME "WLAN.dev"
  17. #define DBG_COLOR
  18. #include <rtdbg.h>
  19. #ifndef RT_DEVICE
  20. #define RT_DEVICE(__device) ((rt_device_t)__device)
  21. #endif
  22. #define WLAN_DEV_LOCK(_wlan) (rt_mutex_take(&(_wlan)->lock, RT_WAITING_FOREVER))
  23. #define WLAN_DEV_UNLOCK(_wlan) (rt_mutex_release(&(_wlan)->lock))
  24. rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
  25. {
  26. rt_err_t result = RT_EOK;
  27. /* init wlan device */
  28. LOG_D("F:%s L:%d is run device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
  29. if ((device == RT_NULL) || (mode >= RT_WLAN_MODE_MAX))
  30. {
  31. LOG_E("F:%s L:%d Parameter Wrongful device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
  32. return -RT_ERROR;
  33. }
  34. result = rt_device_init(RT_DEVICE(device));
  35. if (result != RT_EOK)
  36. {
  37. LOG_E("L:%d wlan init failed", __LINE__);
  38. return -RT_ERROR;
  39. }
  40. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_MODE, (void *)&mode);
  41. if (result != RT_EOK)
  42. {
  43. LOG_E("L:%d wlan config mode failed", __LINE__);
  44. return -RT_ERROR;
  45. }
  46. device->mode = mode;
  47. return result;
  48. }
  49. rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
  50. {
  51. rt_err_t result = RT_EOK;
  52. struct rt_sta_info sta_info;
  53. if (device == RT_NULL)
  54. {
  55. return -RT_EIO;
  56. }
  57. if (info == RT_NULL)
  58. {
  59. return -RT_ERROR;
  60. }
  61. if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) ||
  62. (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH))
  63. {
  64. LOG_E("L:%d password or ssid is to long", __LINE__);
  65. return -RT_ERROR;
  66. }
  67. rt_memset(&sta_info, 0, sizeof(struct rt_sta_info));
  68. rt_memcpy(&sta_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
  69. rt_memcpy(sta_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
  70. if (password != RT_NULL)
  71. {
  72. rt_memcpy(sta_info.key.val, password, password_len);
  73. sta_info.key.len = password_len;
  74. }
  75. sta_info.channel = info->channel;
  76. sta_info.security = info->security;
  77. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_JOIN, &sta_info);
  78. return result;
  79. }
  80. rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device)
  81. {
  82. rt_err_t result = RT_EOK;
  83. if (device == RT_NULL)
  84. {
  85. return -RT_EIO;
  86. }
  87. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_DISCONNECT, RT_NULL);
  88. return result;
  89. }
  90. rt_err_t rt_wlan_dev_ap_start(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_ap_info ap_info;
  94. if (device == RT_NULL)
  95. {
  96. return -RT_EIO;
  97. }
  98. if (info == RT_NULL)
  99. {
  100. return -RT_ERROR;
  101. }
  102. if ((password_len >= RT_WLAN_PASSWORD_MAX_LENGTH) ||
  103. (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH))
  104. {
  105. LOG_E("L:%d password or ssid is to long", __LINE__);
  106. return -RT_ERROR;
  107. }
  108. rt_memset(&ap_info, 0, sizeof(struct rt_ap_info));
  109. rt_memcpy(&ap_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
  110. rt_memcpy(ap_info.key.val, password, password_len);
  111. ap_info.key.len = password_len;
  112. ap_info.hidden = info->hidden;
  113. ap_info.channel = info->channel;
  114. ap_info.security = info->security;
  115. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SOFTAP, &ap_info);
  116. return result;
  117. }
  118. rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device)
  119. {
  120. rt_err_t result = 0;
  121. if (device == RT_NULL)
  122. {
  123. return -RT_EIO;
  124. }
  125. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_STOP, RT_NULL);
  126. return result;
  127. }
  128. rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6])
  129. {
  130. rt_err_t result = 0;
  131. if (device == RT_NULL)
  132. {
  133. return -RT_EIO;
  134. }
  135. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_DEAUTH, mac);
  136. return result;
  137. }
  138. int rt_wlan_dev_get_rssi(struct rt_wlan_device *device)
  139. {
  140. int rssi = 0;
  141. if (device == RT_NULL)
  142. {
  143. return -RT_EIO;
  144. }
  145. rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_RSSI, &rssi);
  146. return rssi;
  147. }
  148. rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
  149. {
  150. rt_err_t result = 0;
  151. if (device == RT_NULL)
  152. {
  153. return -RT_EIO;
  154. }
  155. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_MAC, &mac[0]);
  156. return result;
  157. }
  158. rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
  159. {
  160. rt_err_t result = RT_EOK;
  161. if (device == RT_NULL)
  162. {
  163. return -RT_EIO;
  164. }
  165. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_MAC, &mac[0]);
  166. return result;
  167. }
  168. rt_err_t rt_wlan_dev_enable_powersave(struct rt_wlan_device *device)
  169. {
  170. rt_err_t result = RT_EOK;
  171. int enable = 1;
  172. if (device == RT_NULL)
  173. {
  174. return -RT_EIO;
  175. }
  176. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_POWERSAVE, &enable);
  177. return result;
  178. }
  179. rt_err_t rt_wlan_dev_disable_powersave(struct rt_wlan_device *device)
  180. {
  181. rt_err_t result = RT_EOK;
  182. int enable = 0;
  183. if (device == RT_NULL)
  184. {
  185. return -RT_EIO;
  186. }
  187. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_POWERSAVE, &enable);
  188. return result;
  189. }
  190. 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)
  191. {
  192. int i = 0;
  193. rt_base_t level;
  194. if (device == RT_NULL)
  195. {
  196. return -RT_EIO;
  197. }
  198. if (event >= RT_WLAN_DEV_EVT_MAX)
  199. {
  200. return -RT_EINVAL;
  201. }
  202. level = rt_hw_interrupt_disable();
  203. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  204. {
  205. if (device->handler_table[event][i].handler == RT_NULL)
  206. {
  207. device->handler_table[event][i].handler = handler;
  208. device->handler_table[event][i].parameter = parameter;
  209. rt_hw_interrupt_enable(level);
  210. return RT_EOK;
  211. }
  212. }
  213. rt_hw_interrupt_enable(level);
  214. /* No space found */
  215. return -RT_ERROR;
  216. }
  217. 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)
  218. {
  219. int i = 0;
  220. rt_base_t level;
  221. if (device == RT_NULL)
  222. {
  223. return -RT_EIO;
  224. }
  225. if (event >= RT_WLAN_DEV_EVT_MAX)
  226. {
  227. return -RT_EINVAL;
  228. }
  229. level = rt_hw_interrupt_disable();
  230. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  231. {
  232. if (device->handler_table[event][i].handler == handler)
  233. {
  234. rt_memset(&device->handler_table[event][i], 0, sizeof(struct rt_wlan_dev_event_desc));
  235. rt_exit_critical();
  236. return RT_EOK;
  237. }
  238. }
  239. rt_hw_interrupt_enable(level);
  240. /* not find iteam */
  241. return -RT_ERROR;
  242. }
  243. void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff)
  244. {
  245. void *parameter[RT_WLAN_DEV_EVENT_NUM];
  246. rt_wlan_dev_event_handler handler[RT_WLAN_DEV_EVENT_NUM];
  247. int i;
  248. rt_base_t level;
  249. if (device == RT_NULL)
  250. {
  251. return;
  252. }
  253. if (event >= RT_WLAN_DEV_EVT_MAX)
  254. {
  255. return;
  256. }
  257. /* get callback handle */
  258. level = rt_hw_interrupt_disable();
  259. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  260. {
  261. handler[i] = device->handler_table[event][i].handler;
  262. parameter[i] = device->handler_table[event][i].parameter;
  263. }
  264. rt_hw_interrupt_enable(level);
  265. /* run callback */
  266. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  267. {
  268. if (handler[i] != RT_NULL)
  269. {
  270. handler[i](device, event, buff, parameter[i]);
  271. }
  272. }
  273. }
  274. rt_err_t rt_wlan_dev_enter_pormisc(struct rt_wlan_device *device)
  275. {
  276. rt_err_t result = RT_EOK;
  277. int enable = 1;
  278. if (device == RT_NULL)
  279. {
  280. return -RT_EIO;
  281. }
  282. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
  283. return result;
  284. }
  285. rt_err_t rt_wlan_dev_exit_pormisc(struct rt_wlan_device *device)
  286. {
  287. rt_err_t result = RT_EOK;
  288. int enable = 0;
  289. if (device == RT_NULL)
  290. {
  291. return -RT_EIO;
  292. }
  293. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
  294. return result;
  295. }
  296. rt_err_t rt_wlan_dev_set_pormisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback)
  297. {
  298. if (device == RT_NULL)
  299. {
  300. return -RT_EIO;
  301. }
  302. device->pormisc_callback = callback;
  303. return RT_EOK;
  304. }
  305. void rt_wlan_dev_pormisc_handler(struct rt_wlan_device *device, void *data, int len)
  306. {
  307. rt_wlan_pormisc_callback_t callback;
  308. if (device == RT_NULL)
  309. {
  310. return;
  311. }
  312. callback = device->pormisc_callback;
  313. if (callback != RT_NULL)
  314. {
  315. callback(device, data, len);
  316. }
  317. }
  318. rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter)
  319. {
  320. rt_err_t result = RT_EOK;
  321. if (device == RT_NULL)
  322. {
  323. return -RT_EIO;
  324. }
  325. if (filter == RT_NULL)
  326. {
  327. return -RT_ERROR;
  328. }
  329. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_FILTER, filter);
  330. return result;
  331. }
  332. rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel)
  333. {
  334. rt_err_t result = RT_EOK;
  335. if (device == RT_NULL)
  336. {
  337. return -RT_EIO;
  338. }
  339. if (channel < 0)
  340. {
  341. return -RT_ERROR;
  342. }
  343. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_CHANNEL, &channel);
  344. return result;
  345. }
  346. rt_err_t rt_wlan_dev_get_channel(struct rt_wlan_device *device)
  347. {
  348. rt_err_t result = RT_EOK;
  349. int channel;
  350. if (device == RT_NULL)
  351. {
  352. return -RT_EIO;
  353. }
  354. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_CHANNEL, &channel);
  355. if (result != RT_EOK)
  356. {
  357. return -1;
  358. }
  359. return channel;
  360. }
  361. rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code)
  362. {
  363. int result = RT_EOK;
  364. if (device == RT_NULL)
  365. {
  366. return -RT_EIO;
  367. }
  368. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_COUNTRY, &country_code);
  369. return result;
  370. }
  371. rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device)
  372. {
  373. int result = 0;
  374. rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
  375. if (device == RT_NULL)
  376. {
  377. return country_code;
  378. }
  379. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_COUNTRY, &country_code);
  380. if (result != RT_EOK)
  381. {
  382. return RT_COUNTRY_UNKNOWN;
  383. }
  384. return country_code;
  385. }
  386. rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info)
  387. {
  388. struct rt_scan_info scan_info = { 0 };
  389. struct rt_scan_info *p_scan_info = RT_NULL;
  390. rt_err_t result = 0;
  391. if (device == RT_NULL)
  392. {
  393. return -RT_EIO;
  394. }
  395. if (info != RT_NULL)
  396. {
  397. if (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH)
  398. {
  399. LOG_E("L:%d ssid is to long", __LINE__);
  400. return -RT_EINVAL;
  401. }
  402. rt_memcpy(&scan_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
  403. rt_memcpy(scan_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
  404. scan_info.channel_min = -1;
  405. scan_info.channel_max = -1;
  406. p_scan_info = &scan_info;
  407. }
  408. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info);
  409. return result;
  410. }
  411. rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device)
  412. {
  413. rt_err_t result = 0;
  414. if (device == RT_NULL)
  415. {
  416. return -RT_EIO;
  417. }
  418. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL);
  419. return result;
  420. }
  421. rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len)
  422. {
  423. return rt_wlan_dev_transfer_prot(device, buff, len);
  424. }
  425. static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
  426. {
  427. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  428. rt_err_t result = RT_EOK;
  429. rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_FIFO);
  430. if (wlan->ops->wlan_init)
  431. result = wlan->ops->wlan_init(wlan);
  432. if (result == RT_EOK)
  433. {
  434. LOG_I("wlan init success");
  435. }
  436. else
  437. {
  438. LOG_I("wlan init failed");
  439. }
  440. return result;
  441. }
  442. static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
  443. {
  444. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  445. rt_err_t err = RT_EOK;
  446. RT_ASSERT(dev != RT_NULL);
  447. WLAN_DEV_LOCK(wlan);
  448. switch (cmd)
  449. {
  450. case RT_WLAN_CMD_MODE:
  451. {
  452. rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
  453. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
  454. if (wlan->ops->wlan_mode)
  455. err = wlan->ops->wlan_mode(wlan, mode);
  456. break;
  457. }
  458. case RT_WLAN_CMD_SCAN:
  459. {
  460. struct rt_scan_info *scan_info = args;
  461. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
  462. if (wlan->ops->wlan_scan)
  463. err = wlan->ops->wlan_scan(wlan, scan_info);
  464. break;
  465. }
  466. case RT_WLAN_CMD_JOIN:
  467. {
  468. struct rt_sta_info *sta_info = args;
  469. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
  470. if (wlan->ops->wlan_join)
  471. err = wlan->ops->wlan_join(wlan, sta_info);
  472. break;
  473. }
  474. case RT_WLAN_CMD_SOFTAP:
  475. {
  476. struct rt_ap_info *ap_info = args;
  477. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
  478. if (wlan->ops->wlan_softap)
  479. err = wlan->ops->wlan_softap(wlan, ap_info);
  480. break;
  481. }
  482. case RT_WLAN_CMD_DISCONNECT:
  483. {
  484. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
  485. if (wlan->ops->wlan_disconnect)
  486. err = wlan->ops->wlan_disconnect(wlan);
  487. break;
  488. }
  489. case RT_WLAN_CMD_AP_STOP:
  490. {
  491. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
  492. if (wlan->ops->wlan_ap_stop)
  493. err = wlan->ops->wlan_ap_stop(wlan);
  494. break;
  495. }
  496. case RT_WLAN_CMD_AP_DEAUTH:
  497. {
  498. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
  499. if (wlan->ops->wlan_ap_deauth)
  500. err = wlan->ops->wlan_ap_deauth(wlan, args);
  501. break;
  502. }
  503. case RT_WLAN_CMD_SCAN_STOP:
  504. {
  505. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
  506. if (wlan->ops->wlan_scan_stop)
  507. err = wlan->ops->wlan_scan_stop(wlan);
  508. break;
  509. }
  510. case RT_WLAN_CMD_GET_RSSI:
  511. {
  512. int *rssi = args;
  513. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
  514. if (wlan->ops->wlan_get_rssi)
  515. *rssi = wlan->ops->wlan_get_rssi(wlan);
  516. break;
  517. }
  518. case RT_WLAN_CMD_POWERSAVE:
  519. {
  520. rt_bool_t enable = *((rt_bool_t *)args);
  521. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_POWERSAVE, "RT_WLAN_CMD_POWERSAVE");
  522. if (wlan->ops->wlan_powersave)
  523. wlan->ops->wlan_powersave(wlan, enable);
  524. break;
  525. }
  526. case RT_WLAN_CMD_CFG_PROMISC:
  527. {
  528. rt_bool_t start = *((rt_bool_t *)args);
  529. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
  530. if (wlan->ops->wlan_cfg_promisc)
  531. wlan->ops->wlan_cfg_promisc(wlan, start);
  532. break;
  533. }
  534. case RT_WLAN_CMD_CFG_FILTER:
  535. {
  536. struct rt_wlan_filter *filter = args;
  537. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
  538. if (wlan->ops->wlan_cfg_filter)
  539. wlan->ops->wlan_cfg_filter(wlan, filter);
  540. break;
  541. }
  542. case RT_WLAN_CMD_SET_CHANNEL:
  543. {
  544. int channel = *(int *)args;
  545. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
  546. if (wlan->ops->wlan_set_channel)
  547. wlan->ops->wlan_set_channel(wlan, channel);
  548. break;
  549. }
  550. case RT_WLAN_CMD_GET_CHANNEL:
  551. {
  552. int *channel = args;
  553. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
  554. if (wlan->ops->wlan_get_channel)
  555. *channel = wlan->ops->wlan_get_channel(wlan);
  556. break;
  557. }
  558. case RT_WLAN_CMD_SET_COUNTRY:
  559. {
  560. rt_country_code_t country = *(rt_country_code_t *)args;
  561. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
  562. if (wlan->ops->wlan_set_country)
  563. wlan->ops->wlan_set_country(wlan, country);
  564. break;
  565. }
  566. case RT_WLAN_CMD_GET_COUNTRY:
  567. {
  568. rt_country_code_t *country = args;
  569. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
  570. if (wlan->ops->wlan_get_country)
  571. *country = wlan->ops->wlan_get_country(wlan);
  572. break;
  573. }
  574. case RT_WLAN_CMD_SET_MAC:
  575. {
  576. rt_uint8_t *mac = args;
  577. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
  578. if (wlan->ops->wlan_set_mac)
  579. wlan->ops->wlan_set_mac(wlan, mac);
  580. break;
  581. }
  582. case RT_WLAN_CMD_GET_MAC:
  583. {
  584. rt_uint8_t *mac = args;
  585. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
  586. if (wlan->ops->wlan_get_mac)
  587. wlan->ops->wlan_get_mac(wlan, mac);
  588. break;
  589. }
  590. default:
  591. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
  592. break;
  593. }
  594. WLAN_DEV_UNLOCK(wlan);
  595. return err;
  596. }
  597. struct rt_wlan_device *rt_wlan_dev_register(const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data)
  598. {
  599. struct rt_wlan_device *wlan;
  600. if (name == RT_NULL || ops == RT_NULL)
  601. {
  602. LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
  603. return RT_NULL;
  604. }
  605. wlan = rt_malloc(sizeof(struct rt_wlan_device));
  606. if (wlan == RT_NULL)
  607. {
  608. LOG_E("F:%s L:%d", __FUNCTION__, __LINE__);
  609. return RT_NULL;
  610. }
  611. rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
  612. wlan->device.init = _rt_wlan_dev_init;
  613. wlan->device.open = RT_NULL;
  614. wlan->device.close = RT_NULL;
  615. wlan->device.read = RT_NULL;
  616. wlan->device.write = RT_NULL;
  617. wlan->device.control = _rt_wlan_dev_control;
  618. wlan->device.user_data = RT_NULL;
  619. wlan->device.type = RT_Device_Class_NetIf;
  620. wlan->ops = ops;
  621. wlan->user_data = user_data;
  622. wlan->flags = flag;
  623. rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
  624. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  625. return wlan;
  626. }