dev_wlan.c 25 KB

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