dev_wlan.c 26 KB

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