wlan_dev.c 25 KB

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