wlan_dev.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. rt_err_t rt_wlan_dev_enter_mgnt_filter(struct rt_wlan_device *device)
  482. {
  483. rt_err_t result = RT_EOK;
  484. int enable = 1;
  485. if (device == RT_NULL)
  486. {
  487. return -RT_EIO;
  488. }
  489. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_MGNT_FILTER, &enable);
  490. return result;
  491. }
  492. rt_err_t rt_wlan_dev_exit_mgnt_filter(struct rt_wlan_device *device)
  493. {
  494. rt_err_t result = RT_EOK;
  495. int enable = 0;
  496. if (device == RT_NULL)
  497. {
  498. return -RT_EIO;
  499. }
  500. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_MGNT_FILTER, &enable);
  501. return result;
  502. }
  503. rt_err_t rt_wlan_dev_set_mgnt_filter_callback(struct rt_wlan_device *device, rt_wlan_mgnt_filter_callback_t callback)
  504. {
  505. if (device == RT_NULL)
  506. {
  507. return -RT_EIO;
  508. }
  509. device->mgnt_filter_callback = callback;
  510. return RT_EOK;
  511. }
  512. void rt_wlan_dev_mgnt_filter_handler(struct rt_wlan_device *device, void *data, int len)
  513. {
  514. rt_wlan_mgnt_filter_callback_t callback;
  515. if (device == RT_NULL)
  516. {
  517. return;
  518. }
  519. callback = device->mgnt_filter_callback;
  520. if (callback != RT_NULL)
  521. {
  522. callback(device, data, len);
  523. }
  524. }
  525. int rt_wlan_dev_send_raw_frame(struct rt_wlan_device *device, void *buff, int len)
  526. {
  527. if (device == RT_NULL)
  528. {
  529. return -RT_EIO;
  530. }
  531. if (device->ops->wlan_send_raw_frame)
  532. {
  533. return device->ops->wlan_send_raw_frame(device, buff, len);
  534. }
  535. return -RT_ERROR;
  536. }
  537. static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
  538. {
  539. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  540. rt_err_t result = RT_EOK;
  541. rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_PRIO);
  542. if (wlan->ops->wlan_init)
  543. result = wlan->ops->wlan_init(wlan);
  544. if (result == RT_EOK)
  545. {
  546. LOG_I("wlan init success");
  547. }
  548. else
  549. {
  550. LOG_I("wlan init failed");
  551. }
  552. return result;
  553. }
  554. static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
  555. {
  556. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  557. rt_err_t err = RT_EOK;
  558. RT_ASSERT(dev != RT_NULL);
  559. WLAN_DEV_LOCK(wlan);
  560. switch (cmd)
  561. {
  562. case RT_WLAN_CMD_MODE:
  563. {
  564. rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
  565. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
  566. if (wlan->ops->wlan_mode)
  567. err = wlan->ops->wlan_mode(wlan, mode);
  568. break;
  569. }
  570. case RT_WLAN_CMD_SCAN:
  571. {
  572. struct rt_scan_info *scan_info = args;
  573. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
  574. if (wlan->ops->wlan_scan)
  575. err = wlan->ops->wlan_scan(wlan, scan_info);
  576. break;
  577. }
  578. case RT_WLAN_CMD_JOIN:
  579. {
  580. struct rt_sta_info *sta_info = args;
  581. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
  582. if (wlan->ops->wlan_join)
  583. err = wlan->ops->wlan_join(wlan, sta_info);
  584. break;
  585. }
  586. case RT_WLAN_CMD_SOFTAP:
  587. {
  588. struct rt_ap_info *ap_info = args;
  589. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
  590. if (wlan->ops->wlan_softap)
  591. err = wlan->ops->wlan_softap(wlan, ap_info);
  592. break;
  593. }
  594. case RT_WLAN_CMD_DISCONNECT:
  595. {
  596. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
  597. if (wlan->ops->wlan_disconnect)
  598. err = wlan->ops->wlan_disconnect(wlan);
  599. break;
  600. }
  601. case RT_WLAN_CMD_AP_STOP:
  602. {
  603. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
  604. if (wlan->ops->wlan_ap_stop)
  605. err = wlan->ops->wlan_ap_stop(wlan);
  606. break;
  607. }
  608. case RT_WLAN_CMD_AP_DEAUTH:
  609. {
  610. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
  611. if (wlan->ops->wlan_ap_deauth)
  612. err = wlan->ops->wlan_ap_deauth(wlan, args);
  613. break;
  614. }
  615. case RT_WLAN_CMD_SCAN_STOP:
  616. {
  617. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
  618. if (wlan->ops->wlan_scan_stop)
  619. err = wlan->ops->wlan_scan_stop(wlan);
  620. break;
  621. }
  622. case RT_WLAN_CMD_GET_RSSI:
  623. {
  624. int *rssi = args;
  625. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
  626. if (wlan->ops->wlan_get_rssi)
  627. *rssi = wlan->ops->wlan_get_rssi(wlan);
  628. break;
  629. }
  630. case RT_WLAN_CMD_SET_POWERSAVE:
  631. {
  632. int level = *((int *)args);
  633. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_POWERSAVE, "RT_WLAN_CMD_SET_POWERSAVE");
  634. if (wlan->ops->wlan_set_powersave)
  635. err = wlan->ops->wlan_set_powersave(wlan, level);
  636. break;
  637. }
  638. case RT_WLAN_CMD_GET_POWERSAVE:
  639. {
  640. int *level = args;
  641. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_POWERSAVE, "RT_WLAN_CMD_GET_POWERSAVE");
  642. if (wlan->ops->wlan_get_powersave)
  643. *level = wlan->ops->wlan_get_powersave(wlan);
  644. break;
  645. }
  646. case RT_WLAN_CMD_CFG_PROMISC:
  647. {
  648. rt_bool_t start = *((rt_bool_t *)args);
  649. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
  650. if (wlan->ops->wlan_cfg_promisc)
  651. err = wlan->ops->wlan_cfg_promisc(wlan, start);
  652. break;
  653. }
  654. case RT_WLAN_CMD_CFG_FILTER:
  655. {
  656. struct rt_wlan_filter *filter = args;
  657. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
  658. if (wlan->ops->wlan_cfg_filter)
  659. err = wlan->ops->wlan_cfg_filter(wlan, filter);
  660. break;
  661. }
  662. case RT_WLAN_CMD_CFG_MGNT_FILTER:
  663. {
  664. rt_bool_t start = *((rt_bool_t *)args);
  665. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_MGNT_FILTER, "RT_WLAN_CMD_CFG_MGNT_FILTER");
  666. if (wlan->ops->wlan_cfg_mgnt_filter)
  667. err = wlan->ops->wlan_cfg_mgnt_filter(wlan, start);
  668. break;
  669. }
  670. case RT_WLAN_CMD_SET_CHANNEL:
  671. {
  672. int channel = *(int *)args;
  673. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
  674. if (wlan->ops->wlan_set_channel)
  675. err = wlan->ops->wlan_set_channel(wlan, channel);
  676. break;
  677. }
  678. case RT_WLAN_CMD_GET_CHANNEL:
  679. {
  680. int *channel = args;
  681. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
  682. if (wlan->ops->wlan_get_channel)
  683. *channel = wlan->ops->wlan_get_channel(wlan);
  684. break;
  685. }
  686. case RT_WLAN_CMD_SET_COUNTRY:
  687. {
  688. rt_country_code_t country = *(rt_country_code_t *)args;
  689. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
  690. if (wlan->ops->wlan_set_country)
  691. err = wlan->ops->wlan_set_country(wlan, country);
  692. break;
  693. }
  694. case RT_WLAN_CMD_GET_COUNTRY:
  695. {
  696. rt_country_code_t *country = args;
  697. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
  698. if (wlan->ops->wlan_get_country)
  699. *country = wlan->ops->wlan_get_country(wlan);
  700. break;
  701. }
  702. case RT_WLAN_CMD_SET_MAC:
  703. {
  704. rt_uint8_t *mac = args;
  705. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
  706. if (wlan->ops->wlan_set_mac)
  707. err = wlan->ops->wlan_set_mac(wlan, mac);
  708. break;
  709. }
  710. case RT_WLAN_CMD_GET_MAC:
  711. {
  712. rt_uint8_t *mac = args;
  713. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
  714. if (wlan->ops->wlan_get_mac)
  715. err = wlan->ops->wlan_get_mac(wlan, mac);
  716. break;
  717. }
  718. default:
  719. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
  720. break;
  721. }
  722. WLAN_DEV_UNLOCK(wlan);
  723. return err;
  724. }
  725. #ifdef RT_USING_DEVICE_OPS
  726. const static struct rt_device_ops wlan_ops =
  727. {
  728. _rt_wlan_dev_init,
  729. RT_NULL,
  730. RT_NULL,
  731. RT_NULL,
  732. RT_NULL,
  733. _rt_wlan_dev_control
  734. };
  735. #endif
  736. 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)
  737. {
  738. rt_err_t err = RT_EOK;
  739. if ((wlan == RT_NULL) || (name == RT_NULL) || (ops == RT_NULL) ||
  740. (flag & RT_WLAN_FLAG_STA_ONLY && flag & RT_WLAN_FLAG_AP_ONLY))
  741. {
  742. LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
  743. return RT_NULL;
  744. }
  745. rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
  746. #ifdef RT_USING_DEVICE_OPS
  747. wlan->device.ops = &wlan_ops;
  748. #else
  749. wlan->device.init = _rt_wlan_dev_init;
  750. wlan->device.open = RT_NULL;
  751. wlan->device.close = RT_NULL;
  752. wlan->device.read = RT_NULL;
  753. wlan->device.write = RT_NULL;
  754. wlan->device.control = _rt_wlan_dev_control;
  755. #endif
  756. wlan->device.user_data = RT_NULL;
  757. wlan->device.type = RT_Device_Class_NetIf;
  758. wlan->ops = ops;
  759. wlan->user_data = user_data;
  760. wlan->flags = flag;
  761. err = rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
  762. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  763. return err;
  764. }
  765. #endif