wlan_dev.c 21 KB

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