wlan_dev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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_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 to 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 to 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 to 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. p_scan_info = &scan_info;
  458. }
  459. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info);
  460. return result;
  461. }
  462. rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device)
  463. {
  464. rt_err_t result = 0;
  465. if (device == RT_NULL)
  466. {
  467. return -RT_EIO;
  468. }
  469. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL);
  470. return result;
  471. }
  472. rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len)
  473. {
  474. #ifdef RT_WLAN_PROT_ENABLE
  475. return rt_wlan_dev_transfer_prot(device, buff, len);
  476. #else
  477. return -RT_ERROR;
  478. #endif
  479. }
  480. static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
  481. {
  482. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  483. rt_err_t result = RT_EOK;
  484. rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_FIFO);
  485. if (wlan->ops->wlan_init)
  486. result = wlan->ops->wlan_init(wlan);
  487. if (result == RT_EOK)
  488. {
  489. LOG_I("wlan init success");
  490. }
  491. else
  492. {
  493. LOG_I("wlan init failed");
  494. }
  495. return result;
  496. }
  497. static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
  498. {
  499. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  500. rt_err_t err = RT_EOK;
  501. RT_ASSERT(dev != RT_NULL);
  502. WLAN_DEV_LOCK(wlan);
  503. switch (cmd)
  504. {
  505. case RT_WLAN_CMD_MODE:
  506. {
  507. rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
  508. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
  509. if (wlan->ops->wlan_mode)
  510. err = wlan->ops->wlan_mode(wlan, mode);
  511. break;
  512. }
  513. case RT_WLAN_CMD_SCAN:
  514. {
  515. struct rt_scan_info *scan_info = args;
  516. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
  517. if (wlan->ops->wlan_scan)
  518. err = wlan->ops->wlan_scan(wlan, scan_info);
  519. break;
  520. }
  521. case RT_WLAN_CMD_JOIN:
  522. {
  523. struct rt_sta_info *sta_info = args;
  524. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
  525. if (wlan->ops->wlan_join)
  526. err = wlan->ops->wlan_join(wlan, sta_info);
  527. break;
  528. }
  529. case RT_WLAN_CMD_SOFTAP:
  530. {
  531. struct rt_ap_info *ap_info = args;
  532. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
  533. if (wlan->ops->wlan_softap)
  534. err = wlan->ops->wlan_softap(wlan, ap_info);
  535. break;
  536. }
  537. case RT_WLAN_CMD_DISCONNECT:
  538. {
  539. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
  540. if (wlan->ops->wlan_disconnect)
  541. err = wlan->ops->wlan_disconnect(wlan);
  542. break;
  543. }
  544. case RT_WLAN_CMD_AP_STOP:
  545. {
  546. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
  547. if (wlan->ops->wlan_ap_stop)
  548. err = wlan->ops->wlan_ap_stop(wlan);
  549. break;
  550. }
  551. case RT_WLAN_CMD_AP_DEAUTH:
  552. {
  553. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
  554. if (wlan->ops->wlan_ap_deauth)
  555. err = wlan->ops->wlan_ap_deauth(wlan, args);
  556. break;
  557. }
  558. case RT_WLAN_CMD_SCAN_STOP:
  559. {
  560. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
  561. if (wlan->ops->wlan_scan_stop)
  562. err = wlan->ops->wlan_scan_stop(wlan);
  563. break;
  564. }
  565. case RT_WLAN_CMD_GET_RSSI:
  566. {
  567. int *rssi = args;
  568. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
  569. if (wlan->ops->wlan_get_rssi)
  570. *rssi = wlan->ops->wlan_get_rssi(wlan);
  571. break;
  572. }
  573. case RT_WLAN_CMD_SET_POWERSAVE:
  574. {
  575. int level = *((int *)args);
  576. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_POWERSAVE, "RT_WLAN_CMD_SET_POWERSAVE");
  577. if (wlan->ops->wlan_set_powersave)
  578. err = wlan->ops->wlan_set_powersave(wlan, level);
  579. break;
  580. }
  581. case RT_WLAN_CMD_GET_POWERSAVE:
  582. {
  583. int *level = args;
  584. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_POWERSAVE, "RT_WLAN_CMD_GET_POWERSAVE");
  585. if (wlan->ops->wlan_get_powersave)
  586. *level = wlan->ops->wlan_get_powersave(wlan);
  587. break;
  588. }
  589. case RT_WLAN_CMD_CFG_PROMISC:
  590. {
  591. rt_bool_t start = *((rt_bool_t *)args);
  592. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
  593. if (wlan->ops->wlan_cfg_promisc)
  594. err = wlan->ops->wlan_cfg_promisc(wlan, start);
  595. break;
  596. }
  597. case RT_WLAN_CMD_CFG_FILTER:
  598. {
  599. struct rt_wlan_filter *filter = args;
  600. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
  601. if (wlan->ops->wlan_cfg_filter)
  602. err = wlan->ops->wlan_cfg_filter(wlan, filter);
  603. break;
  604. }
  605. case RT_WLAN_CMD_SET_CHANNEL:
  606. {
  607. int channel = *(int *)args;
  608. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
  609. if (wlan->ops->wlan_set_channel)
  610. err = wlan->ops->wlan_set_channel(wlan, channel);
  611. break;
  612. }
  613. case RT_WLAN_CMD_GET_CHANNEL:
  614. {
  615. int *channel = args;
  616. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
  617. if (wlan->ops->wlan_get_channel)
  618. *channel = wlan->ops->wlan_get_channel(wlan);
  619. break;
  620. }
  621. case RT_WLAN_CMD_SET_COUNTRY:
  622. {
  623. rt_country_code_t country = *(rt_country_code_t *)args;
  624. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
  625. if (wlan->ops->wlan_set_country)
  626. err = wlan->ops->wlan_set_country(wlan, country);
  627. break;
  628. }
  629. case RT_WLAN_CMD_GET_COUNTRY:
  630. {
  631. rt_country_code_t *country = args;
  632. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
  633. if (wlan->ops->wlan_get_country)
  634. *country = wlan->ops->wlan_get_country(wlan);
  635. break;
  636. }
  637. case RT_WLAN_CMD_SET_MAC:
  638. {
  639. rt_uint8_t *mac = args;
  640. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
  641. if (wlan->ops->wlan_set_mac)
  642. err = wlan->ops->wlan_set_mac(wlan, mac);
  643. break;
  644. }
  645. case RT_WLAN_CMD_GET_MAC:
  646. {
  647. rt_uint8_t *mac = args;
  648. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
  649. if (wlan->ops->wlan_get_mac)
  650. err = wlan->ops->wlan_get_mac(wlan, mac);
  651. break;
  652. }
  653. default:
  654. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
  655. break;
  656. }
  657. WLAN_DEV_UNLOCK(wlan);
  658. return err;
  659. }
  660. #ifdef RT_USING_DEVICE_OPS
  661. const static struct rt_device_ops wlan_ops =
  662. {
  663. _rt_wlan_dev_init,
  664. RT_NULL,
  665. RT_NULL,
  666. RT_NULL,
  667. RT_NULL,
  668. _rt_wlan_dev_control
  669. };
  670. #endif
  671. 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)
  672. {
  673. rt_err_t err = RT_EOK;
  674. if ((wlan == RT_NULL) || (name == RT_NULL) || (ops == RT_NULL) ||
  675. (flag & RT_WLAN_FLAG_STA_ONLY && flag & RT_WLAN_FLAG_AP_ONLY))
  676. {
  677. LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
  678. return RT_NULL;
  679. }
  680. rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
  681. #ifdef RT_USING_DEVICE_OPS
  682. wlan->device.ops = &wlan_ops;
  683. #else
  684. wlan->device.init = _rt_wlan_dev_init;
  685. wlan->device.open = RT_NULL;
  686. wlan->device.close = RT_NULL;
  687. wlan->device.read = RT_NULL;
  688. wlan->device.write = RT_NULL;
  689. wlan->device.control = _rt_wlan_dev_control;
  690. #endif
  691. wlan->device.user_data = RT_NULL;
  692. wlan->device.type = RT_Device_Class_NetIf;
  693. wlan->ops = ops;
  694. wlan->user_data = user_data;
  695. wlan->flags = flag;
  696. err = rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
  697. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  698. return err;
  699. }
  700. #endif