1
0

wlan_dev.c 25 KB

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