wlan_mgnt.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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-06 tyx the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <wlan_dev.h>
  13. #include <wlan_cfg.h>
  14. #include <wlan_mgnt.h>
  15. #include <wlan_prot.h>
  16. #include <wlan_workqueue.h>
  17. // #define RT_WLAN_MGNT_DEBUG
  18. #define DBG_TAG "WLAN.mgnt"
  19. #ifdef RT_WLAN_MGNT_DEBUG
  20. #define DBG_LVL DBG_LOG
  21. #else
  22. #define DBG_LVL DBG_INFO
  23. #endif /* RT_WLAN_MGNT_DEBUG */
  24. #include <rtdbg.h>
  25. #ifdef RT_WLAN_MANAGE_ENABLE
  26. #ifndef RT_WLAN_DEVICE
  27. #define RT_WLAN_DEVICE(__device) ((struct rt_wlan_device *)__device)
  28. #endif
  29. #define RT_WLAN_LOG_D(_fmt, ...) LOG_D("L:%d "_fmt"", __LINE__, ##__VA_ARGS__)
  30. #define RT_WLAN_LOG_I(...) LOG_I(__VA_ARGS__)
  31. #define RT_WLAN_LOG_W(_fmt, ...) LOG_W("F:%s L:%d "_fmt"", __FUNCTION__, __LINE__, ##__VA_ARGS__)
  32. #define RT_WLAN_LOG_E(_fmt, ...) LOG_E("F:%s L:%d "_fmt"", __FUNCTION__, __LINE__, ##__VA_ARGS__)
  33. #define STA_DEVICE() (_sta_mgnt.device)
  34. #define AP_DEVICE() (_ap_mgnt.device)
  35. #define STAINFO_LOCK() (rt_mutex_take(&sta_info_mutex, RT_WAITING_FOREVER))
  36. #define STAINFO_UNLOCK() (rt_mutex_release(&sta_info_mutex))
  37. #define MGNT_LOCK() (rt_mutex_take(&mgnt_mutex, RT_WAITING_FOREVER))
  38. #define MGNT_UNLOCK() (rt_mutex_release(&mgnt_mutex))
  39. #define COMPLETE_LOCK() (rt_mutex_take(&complete_mutex, RT_WAITING_FOREVER))
  40. #define COMPLETE_UNLOCK() (rt_mutex_release(&complete_mutex))
  41. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  42. #define TIME_STOP() (rt_timer_stop(&reconnect_time))
  43. #define TIME_START() (rt_timer_start(&reconnect_time))
  44. static rt_uint32_t id = 0;
  45. #else
  46. #define TIME_STOP()
  47. #define TIME_START()
  48. #endif
  49. #if RT_WLAN_EBOX_NUM < 1
  50. #error "event box num Too few"
  51. #endif
  52. struct rt_wlan_mgnt_des
  53. {
  54. struct rt_wlan_device *device;
  55. struct rt_wlan_info info;
  56. struct rt_wlan_key key;
  57. rt_uint8_t state;
  58. rt_uint8_t flags;
  59. };
  60. struct rt_wlan_event_desc
  61. {
  62. rt_wlan_event_handler handler;
  63. void *parameter;
  64. };
  65. struct rt_wlan_sta_list
  66. {
  67. struct rt_wlan_sta_list *next;
  68. struct rt_wlan_info info;
  69. };
  70. struct rt_wlan_sta_des
  71. {
  72. int num;
  73. struct rt_wlan_sta_list *node;
  74. };
  75. struct rt_wlan_msg
  76. {
  77. rt_int32_t event;
  78. rt_int32_t len;
  79. void *buff;
  80. };
  81. struct rt_wlan_complete_des
  82. {
  83. struct rt_event complete;
  84. rt_uint32_t event_flag;
  85. int index;
  86. };
  87. static struct rt_mutex mgnt_mutex;
  88. static struct rt_wlan_mgnt_des _sta_mgnt;
  89. static struct rt_wlan_mgnt_des _ap_mgnt;
  90. static struct rt_wlan_sta_des sta_info;
  91. static struct rt_mutex sta_info_mutex;
  92. static struct rt_wlan_event_desc event_tab[RT_WLAN_EVT_MAX];
  93. static struct rt_wlan_complete_des *complete_tab[5];
  94. static struct rt_mutex complete_mutex;
  95. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  96. static struct rt_timer reconnect_time;
  97. #endif
  98. rt_inline int _sta_is_null(void)
  99. {
  100. if (_sta_mgnt.device == RT_NULL)
  101. {
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. rt_inline int _ap_is_null(void)
  107. {
  108. if (_ap_mgnt.device == RT_NULL)
  109. {
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. rt_inline rt_bool_t _is_do_connect(void)
  115. {
  116. if ((rt_wlan_get_autoreconnect_mode() == RT_FALSE) ||
  117. (rt_wlan_is_connected() == RT_TRUE) ||
  118. (_sta_mgnt.state & RT_WLAN_STATE_CONNECTING))
  119. {
  120. return RT_FALSE;
  121. }
  122. return RT_TRUE;
  123. }
  124. #ifdef RT_WLAN_WORK_THREAD_ENABLE
  125. static void rt_wlan_mgnt_work(void *parameter)
  126. {
  127. struct rt_wlan_msg *msg = parameter;
  128. void *user_parameter;
  129. rt_wlan_event_handler handler = RT_NULL;
  130. struct rt_wlan_buff user_buff = { 0 };
  131. rt_base_t level;
  132. /* Get user callback */
  133. if (msg->event < RT_WLAN_EVT_MAX)
  134. {
  135. level = rt_hw_interrupt_disable();
  136. handler = event_tab[msg->event].handler;
  137. user_parameter = event_tab[msg->event].parameter;
  138. rt_hw_interrupt_enable(level);
  139. }
  140. /* run user callback fun */
  141. if (handler)
  142. {
  143. user_buff.data = msg->buff;
  144. user_buff.len = msg->len;
  145. RT_WLAN_LOG_D("wlan work thread run user callback, event:%d", msg->event);
  146. handler(msg->event, &user_buff, user_parameter);
  147. }
  148. switch (msg->event)
  149. {
  150. case RT_WLAN_EVT_STA_CONNECTED:
  151. {
  152. struct rt_wlan_cfg_info cfg_info;
  153. rt_memset(&cfg_info, 0, sizeof(cfg_info));
  154. /* save config */
  155. if (rt_wlan_is_connected() == RT_TRUE)
  156. {
  157. rt_enter_critical();
  158. cfg_info.info = _sta_mgnt.info;
  159. cfg_info.key = _sta_mgnt.key;
  160. rt_exit_critical();
  161. RT_WLAN_LOG_D("run save config! ssid:%s len%d", _sta_mgnt.info.ssid.val, _sta_mgnt.info.ssid.len);
  162. #ifdef RT_WLAN_CFG_ENABLE
  163. rt_wlan_cfg_save(&cfg_info);
  164. #endif
  165. }
  166. break;
  167. }
  168. default :
  169. break;
  170. }
  171. rt_free(msg);
  172. }
  173. static rt_err_t rt_wlan_send_to_thread(rt_wlan_event_t event, void *buff, int len)
  174. {
  175. struct rt_wlan_msg *msg;
  176. RT_WLAN_LOG_D("F:%s is run event:%d", __FUNCTION__, event);
  177. /* Event packing */
  178. msg = rt_malloc(sizeof(struct rt_wlan_msg) + len);
  179. if (msg == RT_NULL)
  180. {
  181. RT_WLAN_LOG_E("wlan mgnt send msg err! No memory");
  182. return -RT_ENOMEM;
  183. }
  184. rt_memset(msg, 0, sizeof(struct rt_wlan_msg) + len);
  185. msg->event = event;
  186. if (len != 0)
  187. {
  188. msg->buff = (void *)&msg[1];
  189. rt_memcpy(msg->buff, buff, len);
  190. msg->len = len;
  191. }
  192. /* send event to wlan thread */
  193. if (rt_wlan_workqueue_dowork(rt_wlan_mgnt_work, msg) != RT_EOK)
  194. {
  195. rt_free(msg);
  196. RT_WLAN_LOG_E("wlan mgnt do work fail");
  197. return -RT_ERROR;
  198. }
  199. return RT_EOK;
  200. }
  201. #endif
  202. static rt_err_t rt_wlan_sta_info_add(struct rt_wlan_info *info, int timeout)
  203. {
  204. struct rt_wlan_sta_list *sta_list;
  205. rt_err_t err = RT_EOK;
  206. if (_ap_is_null() || (info == RT_NULL)) return RT_EOK;
  207. err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout));
  208. if (err == RT_EOK)
  209. {
  210. /* malloc memory */
  211. sta_list = rt_malloc(sizeof(struct rt_wlan_sta_list));
  212. if (sta_list == RT_NULL)
  213. {
  214. rt_mutex_release(&sta_info_mutex);
  215. RT_WLAN_LOG_E("sta list malloc failed!");
  216. return -RT_ENOMEM;
  217. }
  218. sta_list->next = RT_NULL;
  219. sta_list->info = *info;
  220. /* Append sta info */
  221. sta_list->next = sta_info.node;
  222. sta_info.node = sta_list;
  223. /* num++ */
  224. sta_info.num ++;
  225. rt_mutex_release(&sta_info_mutex);
  226. RT_WLAN_LOG_I("sta associated mac:%02x:%02x:%02x:%02x:%02x:%02x",
  227. info->bssid[0], info->bssid[1], info->bssid[2],
  228. info->bssid[3], info->bssid[4], info->bssid[5]);
  229. }
  230. return err;
  231. }
  232. static rt_err_t rt_wlan_sta_info_del(struct rt_wlan_info *info, int timeout)
  233. {
  234. struct rt_wlan_sta_list *sta_list, *sta_prve;
  235. rt_err_t err = RT_EOK;
  236. if (_ap_is_null() || (info == RT_NULL)) return RT_EOK;
  237. err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout));
  238. if (err == RT_EOK)
  239. {
  240. /* traversing the list */
  241. for (sta_list = sta_info.node, sta_prve = RT_NULL; sta_list != RT_NULL;
  242. sta_prve = sta_list, sta_list = sta_list->next)
  243. {
  244. /* find mac addr */
  245. if (rt_memcmp(&sta_list->info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)
  246. {
  247. if (sta_prve == RT_NULL)
  248. {
  249. sta_info.node = sta_list->next;
  250. }
  251. else
  252. {
  253. sta_prve->next = sta_list->next;
  254. }
  255. sta_info.num --;
  256. rt_free(sta_list);
  257. break;
  258. }
  259. }
  260. rt_mutex_release(&sta_info_mutex);
  261. RT_WLAN_LOG_I("sta exit mac:%02x:%02x:%02x:%02x:%02x:%02x",
  262. info->bssid[0], info->bssid[1], info->bssid[2],
  263. info->bssid[3], info->bssid[4], info->bssid[5]);
  264. }
  265. return err;
  266. }
  267. static rt_err_t rt_wlan_sta_info_del_all(int timeout)
  268. {
  269. struct rt_wlan_sta_list *sta_list, *sta_next;
  270. rt_err_t err = RT_EOK;
  271. err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout));
  272. if (err == RT_EOK)
  273. {
  274. /* traversing the list */
  275. for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_next)
  276. {
  277. sta_next = sta_list->next;
  278. sta_info.num --;
  279. rt_free(sta_list);
  280. }
  281. rt_mutex_release(&sta_info_mutex);
  282. }
  283. if (sta_info.num != 0)
  284. {
  285. RT_WLAN_LOG_W("\n\n!!!Program runing exception!!!\n\n");
  286. }
  287. sta_info.num = 0;
  288. sta_info.node = RT_NULL;
  289. return err;
  290. }
  291. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  292. static void rt_wlan_auto_connect_run(struct rt_work *work, void *parameter)
  293. {
  294. struct rt_wlan_cfg_info cfg_info;
  295. char *password = RT_NULL;
  296. rt_base_t level;
  297. RT_WLAN_LOG_D("F:%s is run", __FUNCTION__);
  298. if (rt_mutex_take(&mgnt_mutex, 0) != RT_EOK)
  299. goto exit;
  300. /* auto connect status is disable or wifi is connect or connecting, exit */
  301. if (_is_do_connect() == RT_FALSE)
  302. {
  303. id = 0;
  304. RT_WLAN_LOG_D("not connection");
  305. goto exit;
  306. }
  307. /* Read the next configuration */
  308. rt_memset(&cfg_info, 0, sizeof(struct rt_wlan_cfg_info));
  309. if (rt_wlan_cfg_read_index(&cfg_info, id ++) == 0)
  310. {
  311. RT_WLAN_LOG_D("read cfg fail");
  312. id = 0;
  313. goto exit;
  314. }
  315. if (id >= rt_wlan_cfg_get_num()) id = 0;
  316. if ((cfg_info.key.len > 0) && (cfg_info.key.len <= RT_WLAN_PASSWORD_MAX_LENGTH))
  317. {
  318. cfg_info.key.val[cfg_info.key.len] = '\0';
  319. password = (char *)(&cfg_info.key.val[0]);
  320. }
  321. rt_wlan_connect((char *)cfg_info.info.ssid.val, password);
  322. exit:
  323. rt_mutex_release(&mgnt_mutex);
  324. level = rt_hw_interrupt_disable();
  325. rt_memset(work, 0, sizeof(struct rt_work));
  326. rt_hw_interrupt_enable(level);
  327. }
  328. static void rt_wlan_cyclic_check(void *parameter)
  329. {
  330. static struct rt_work work;
  331. rt_base_t level;
  332. if ((_is_do_connect() == RT_TRUE) && (work.work_func == RT_NULL))
  333. {
  334. level = rt_hw_interrupt_disable();
  335. rt_work_init(&work, rt_wlan_auto_connect_run, RT_NULL);
  336. rt_hw_interrupt_enable(level);
  337. if(rt_work_submit(&work,RT_TICK_PER_SECOND) != RT_EOK)
  338. {
  339. level = rt_hw_interrupt_disable();
  340. rt_memset(&work, 0, sizeof(struct rt_work));
  341. rt_hw_interrupt_enable(level);
  342. }
  343. }
  344. }
  345. #endif
  346. static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter)
  347. {
  348. rt_err_t err = RT_NULL;
  349. rt_wlan_event_t user_event = RT_WLAN_EVT_MAX;
  350. int i;
  351. struct rt_wlan_buff user_buff = { 0 };
  352. if (buff)
  353. {
  354. user_buff = *buff;
  355. }
  356. /* Event Handle */
  357. switch (event)
  358. {
  359. case RT_WLAN_DEV_EVT_CONNECT:
  360. {
  361. RT_WLAN_LOG_D("event: CONNECT");
  362. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  363. id = 0;
  364. #endif
  365. _sta_mgnt.state |= RT_WLAN_STATE_CONNECT;
  366. _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
  367. user_event = RT_WLAN_EVT_STA_CONNECTED;
  368. TIME_STOP();
  369. user_buff.data = &_sta_mgnt.info;
  370. user_buff.len = sizeof(struct rt_wlan_info);
  371. RT_WLAN_LOG_I("wifi connect success ssid:%s", &_sta_mgnt.info.ssid.val[0]);
  372. #ifdef RT_WLAN_CFG_ENABLE
  373. {
  374. struct rt_wlan_cfg_info cfg_info;
  375. rt_memset(&cfg_info, 0, sizeof(cfg_info));
  376. /* save config */
  377. if (rt_wlan_is_connected() == RT_TRUE)
  378. {
  379. rt_enter_critical();
  380. cfg_info.info = _sta_mgnt.info;
  381. cfg_info.key = _sta_mgnt.key;
  382. rt_exit_critical();
  383. RT_WLAN_LOG_D("run save config! ssid:%s len%d", _sta_mgnt.info.ssid.val, _sta_mgnt.info.ssid.len);
  384. rt_wlan_cfg_save(&cfg_info);
  385. }
  386. }
  387. #endif
  388. break;
  389. }
  390. case RT_WLAN_DEV_EVT_CONNECT_FAIL:
  391. {
  392. RT_WLAN_LOG_D("event: CONNECT_FAIL");
  393. _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECT;
  394. _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
  395. _sta_mgnt.state &= ~RT_WLAN_STATE_READY;
  396. user_event = RT_WLAN_EVT_STA_CONNECTED_FAIL;
  397. user_buff.data = &_sta_mgnt.info;
  398. user_buff.len = sizeof(struct rt_wlan_info);
  399. if (rt_wlan_get_autoreconnect_mode())
  400. {
  401. TIME_START();
  402. }
  403. break;
  404. }
  405. case RT_WLAN_DEV_EVT_DISCONNECT:
  406. {
  407. RT_WLAN_LOG_D("event: DISCONNECT");
  408. _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECT;
  409. _sta_mgnt.state &= ~RT_WLAN_STATE_READY;
  410. user_event = RT_WLAN_EVT_STA_DISCONNECTED;
  411. user_buff.data = &_sta_mgnt.info;
  412. user_buff.len = sizeof(struct rt_wlan_info);
  413. if (rt_wlan_get_autoreconnect_mode())
  414. {
  415. TIME_START();
  416. }
  417. break;
  418. }
  419. case RT_WLAN_DEV_EVT_AP_START:
  420. {
  421. RT_WLAN_LOG_D("event: AP_START");
  422. _ap_mgnt.state |= RT_WLAN_STATE_ACTIVE;
  423. user_event = RT_WLAN_EVT_AP_START;
  424. user_buff.data = &_ap_mgnt.info;
  425. user_buff.len = sizeof(struct rt_wlan_info);
  426. break;
  427. }
  428. case RT_WLAN_DEV_EVT_AP_STOP:
  429. {
  430. RT_WLAN_LOG_D("event: AP_STOP");
  431. _ap_mgnt.state &= ~RT_WLAN_STATE_ACTIVE;
  432. user_event = RT_WLAN_EVT_AP_STOP;
  433. err = rt_wlan_sta_info_del_all(RT_WAITING_FOREVER);
  434. if (err != RT_NULL)
  435. {
  436. RT_WLAN_LOG_W("AP_STOP event handle fail");
  437. }
  438. user_buff.data = &_ap_mgnt.info;
  439. user_buff.len = sizeof(struct rt_wlan_info);
  440. break;
  441. }
  442. case RT_WLAN_DEV_EVT_AP_ASSOCIATED:
  443. {
  444. RT_WLAN_LOG_D("event: ASSOCIATED");
  445. user_event = RT_WLAN_EVT_AP_ASSOCIATED;
  446. if (user_buff.len != sizeof(struct rt_wlan_info))
  447. break;
  448. err = rt_wlan_sta_info_add(user_buff.data, RT_WAITING_FOREVER);
  449. if (err != RT_EOK)
  450. {
  451. RT_WLAN_LOG_W("AP_ASSOCIATED event handle fail");
  452. }
  453. break;
  454. }
  455. case RT_WLAN_DEV_EVT_AP_DISASSOCIATED:
  456. {
  457. RT_WLAN_LOG_D("event: DISASSOCIATED");
  458. user_event = RT_WLAN_EVT_AP_DISASSOCIATED;
  459. if (user_buff.len != sizeof(struct rt_wlan_info))
  460. break;
  461. err = rt_wlan_sta_info_del(user_buff.data, RT_WAITING_FOREVER);
  462. if (err != RT_EOK)
  463. {
  464. RT_WLAN_LOG_W("AP_DISASSOCIATED event handle fail");
  465. }
  466. break;
  467. }
  468. case RT_WLAN_DEV_EVT_AP_ASSOCIATE_FAILED:
  469. {
  470. RT_WLAN_LOG_D("event: AP_ASSOCIATE_FAILED");
  471. break;
  472. }
  473. case RT_WLAN_DEV_EVT_SCAN_REPORT:
  474. {
  475. RT_WLAN_LOG_D("event: SCAN_REPORT");
  476. user_event = RT_WLAN_EVT_SCAN_REPORT;
  477. break;
  478. }
  479. case RT_WLAN_DEV_EVT_SCAN_DONE:
  480. {
  481. RT_WLAN_LOG_D("event: SCAN_DONE");
  482. user_event = RT_WLAN_EVT_SCAN_DONE;
  483. break;
  484. }
  485. default :
  486. {
  487. RT_WLAN_LOG_D("event: UNKNOWN");
  488. return;
  489. }
  490. }
  491. /* send event */
  492. COMPLETE_LOCK();
  493. for (i = 0; i < sizeof(complete_tab) / sizeof(complete_tab[0]); i++)
  494. {
  495. if ((complete_tab[i] != RT_NULL))
  496. {
  497. complete_tab[i]->event_flag |= 0x1 << event;
  498. rt_event_send(&complete_tab[i]->complete, 0x1 << event);
  499. RT_WLAN_LOG_D("&complete_tab[i]->complete:0x%08x", &complete_tab[i]->complete);
  500. }
  501. }
  502. COMPLETE_UNLOCK();
  503. #ifdef RT_WLAN_WORK_THREAD_ENABLE
  504. rt_wlan_send_to_thread(user_event, user_buff.data, user_buff.len);
  505. #else
  506. {
  507. void *user_parameter;
  508. rt_wlan_event_handler handler = RT_NULL;
  509. rt_base_t level;
  510. /* Get user callback */
  511. if (user_event < RT_WLAN_EVT_MAX)
  512. {
  513. level = rt_hw_interrupt_disable();
  514. handler = event_tab[user_event].handler;
  515. user_parameter = event_tab[user_event].parameter;
  516. rt_hw_interrupt_enable(level);
  517. }
  518. /* run user callback fun */
  519. if (handler)
  520. {
  521. RT_WLAN_LOG_D("unknown thread run user callback, event:%d", user_event);
  522. handler(user_event, &user_buff, user_parameter);
  523. }
  524. }
  525. #endif
  526. }
  527. static struct rt_wlan_complete_des *rt_wlan_complete_create(const char *name)
  528. {
  529. struct rt_wlan_complete_des *complete;
  530. int i;
  531. complete = rt_malloc(sizeof(struct rt_wlan_complete_des));
  532. if (complete == RT_NULL)
  533. {
  534. RT_WLAN_LOG_E("complete event create failed");
  535. MGNT_UNLOCK();
  536. return complete;
  537. }
  538. rt_event_init(&complete->complete, name, RT_IPC_FLAG_FIFO);
  539. complete->event_flag = 0;
  540. //protect
  541. COMPLETE_LOCK();
  542. for (i = 0; i < sizeof(complete_tab) / sizeof(complete_tab[0]); i++)
  543. {
  544. if (complete_tab[i] == RT_NULL)
  545. {
  546. complete->index = i;
  547. complete_tab[i] = complete;
  548. break;
  549. }
  550. }
  551. COMPLETE_UNLOCK();
  552. if (i >= sizeof(complete_tab) / sizeof(complete_tab[0]))
  553. {
  554. rt_event_detach(&complete->complete);
  555. rt_free(complete);
  556. complete = RT_NULL;
  557. }
  558. return complete;
  559. }
  560. static rt_err_t rt_wlan_complete_wait(struct rt_wlan_complete_des *complete, rt_uint32_t event,
  561. rt_uint32_t timeout, rt_uint32_t *recved)
  562. {
  563. if (complete == RT_NULL)
  564. {
  565. return -RT_ERROR;
  566. }
  567. /* Check whether there is a waiting event */
  568. if (complete->event_flag & event)
  569. {
  570. *recved = complete->event_flag;
  571. return RT_EOK;
  572. }
  573. else
  574. {
  575. return rt_event_recv(&complete->complete, event, RT_EVENT_FLAG_OR,
  576. rt_tick_from_millisecond(timeout), recved);
  577. }
  578. }
  579. static void rt_wlan_complete_delete(struct rt_wlan_complete_des *complete)
  580. {
  581. if (complete == RT_NULL)
  582. {
  583. return;
  584. }
  585. COMPLETE_LOCK();
  586. complete_tab[complete->index] = RT_NULL;
  587. COMPLETE_UNLOCK();
  588. rt_event_detach(&complete->complete);
  589. rt_free(complete);
  590. }
  591. rt_err_t rt_wlan_set_mode(const char *dev_name, rt_wlan_mode_t mode)
  592. {
  593. rt_device_t device = RT_NULL;
  594. rt_err_t err;
  595. rt_int8_t up_event_flag = 0;
  596. rt_wlan_dev_event_handler handler = RT_NULL;
  597. if ((dev_name == RT_NULL) || (mode >= RT_WLAN_MODE_MAX))
  598. {
  599. RT_WLAN_LOG_E("Parameter Wrongful name:%s mode:%d", dev_name, mode);
  600. return -RT_EINVAL;
  601. }
  602. RT_WLAN_LOG_D("%s is run dev_name:%s mode:%s%s%s", __FUNCTION__, dev_name,
  603. mode == RT_WLAN_NONE ? "NONE" : "",
  604. mode == RT_WLAN_STATION ? "STA" : "",
  605. mode == RT_WLAN_AP ? "AP" : ""
  606. );
  607. /* find device */
  608. device = rt_device_find(dev_name);
  609. if (device == RT_NULL)
  610. {
  611. RT_WLAN_LOG_E("not find device, set mode failed! name:%s", dev_name);
  612. return -RT_EIO;
  613. }
  614. MGNT_LOCK();
  615. if (RT_WLAN_DEVICE(device)->mode == mode)
  616. {
  617. RT_WLAN_LOG_D("L:%d this device mode is set");
  618. MGNT_UNLOCK();
  619. return RT_EOK;
  620. }
  621. if ((mode == RT_WLAN_STATION) &&
  622. (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_AP_ONLY))
  623. {
  624. RT_WLAN_LOG_I("this device ap mode only");
  625. MGNT_UNLOCK();
  626. return -RT_ERROR;
  627. }
  628. else if ((mode == RT_WLAN_AP) &&
  629. (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_STA_ONLY))
  630. {
  631. RT_WLAN_LOG_I("this device sta mode only");
  632. MGNT_UNLOCK();
  633. return -RT_ERROR;
  634. }
  635. /*
  636. * device == sta and change to ap, should deinit
  637. * device == ap and change to sta, should deinit
  638. */
  639. if (((mode == RT_WLAN_STATION) && (RT_WLAN_DEVICE(device) == AP_DEVICE())) ||
  640. ((mode == RT_WLAN_AP) && (RT_WLAN_DEVICE(device) == STA_DEVICE())))
  641. {
  642. err = rt_wlan_set_mode(dev_name, RT_WLAN_NONE);
  643. if (err != RT_EOK)
  644. {
  645. RT_WLAN_LOG_E("change mode failed!");
  646. MGNT_UNLOCK();
  647. return err;
  648. }
  649. }
  650. /* init device */
  651. err = rt_wlan_dev_init(RT_WLAN_DEVICE(device), mode);
  652. if (err != RT_EOK)
  653. {
  654. RT_WLAN_LOG_E("F:%s L:%d wlan init failed", __FUNCTION__, __LINE__);
  655. MGNT_UNLOCK();
  656. return err;
  657. }
  658. /* the mode is none */
  659. if (mode == RT_WLAN_NONE)
  660. {
  661. if (_sta_mgnt.device == RT_WLAN_DEVICE(device))
  662. {
  663. _sta_mgnt.device = RT_NULL;
  664. _sta_mgnt.state = 0;
  665. up_event_flag = 1;
  666. handler = RT_NULL;
  667. }
  668. else if (_ap_mgnt.device == RT_WLAN_DEVICE(device))
  669. {
  670. _ap_mgnt.state = 0;
  671. _ap_mgnt.device = RT_NULL;
  672. up_event_flag = 1;
  673. handler = RT_NULL;
  674. }
  675. }
  676. /* save sta device */
  677. else if (mode == RT_WLAN_STATION)
  678. {
  679. up_event_flag = 1;
  680. handler = rt_wlan_event_dispatch;
  681. _sta_mgnt.device = RT_WLAN_DEVICE(device);
  682. }
  683. /* save ap device */
  684. else if (mode == RT_WLAN_AP)
  685. {
  686. up_event_flag = 1;
  687. handler = rt_wlan_event_dispatch;
  688. _ap_mgnt.device = RT_WLAN_DEVICE(device);
  689. }
  690. /* update dev event handle */
  691. if (up_event_flag == 1)
  692. {
  693. if (handler)
  694. {
  695. if (mode == RT_WLAN_STATION)
  696. {
  697. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_CONNECT, handler, RT_NULL);
  698. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_CONNECT_FAIL, handler, RT_NULL);
  699. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_DISCONNECT, handler, RT_NULL);
  700. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_SCAN_REPORT, handler, RT_NULL);
  701. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_SCAN_DONE, handler, RT_NULL);
  702. }
  703. else if (mode == RT_WLAN_AP)
  704. {
  705. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_START, handler, RT_NULL);
  706. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_STOP, handler, RT_NULL);
  707. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_ASSOCIATED, handler, RT_NULL);
  708. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_DISASSOCIATED, handler, RT_NULL);
  709. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), RT_WLAN_DEV_EVT_AP_ASSOCIATE_FAILED, handler, RT_NULL);
  710. }
  711. }
  712. else
  713. {
  714. rt_wlan_dev_event_t event;
  715. handler = rt_wlan_event_dispatch;
  716. for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event++)
  717. {
  718. rt_wlan_dev_unregister_event_handler(RT_WLAN_DEVICE(device), event, handler);
  719. }
  720. }
  721. }
  722. MGNT_UNLOCK();
  723. /* Mount protocol */
  724. #if defined(RT_WLAN_PROT_ENABLE) && defined(RT_WLAN_DEFAULT_PROT)
  725. if (err == RT_EOK)
  726. {
  727. rt_wlan_prot_attach(dev_name, RT_WLAN_DEFAULT_PROT);
  728. }
  729. #endif
  730. return err;
  731. }
  732. rt_wlan_mode_t rt_wlan_get_mode(const char *dev_name)
  733. {
  734. rt_device_t device = RT_NULL;
  735. rt_wlan_mode_t mode;
  736. if (dev_name == RT_NULL)
  737. {
  738. RT_WLAN_LOG_E("name is null");
  739. return RT_WLAN_NONE;
  740. }
  741. /* find device */
  742. device = rt_device_find(dev_name);
  743. if (device == RT_NULL)
  744. {
  745. RT_WLAN_LOG_E("device not find! name:%s", dev_name);
  746. return RT_WLAN_NONE;
  747. }
  748. /* get mode */
  749. mode = RT_WLAN_DEVICE(device)->mode;
  750. RT_WLAN_LOG_D("%s is run dev_name:%s mode:%s%s%s", __FUNCTION__, dev_name,
  751. mode == RT_WLAN_NONE ? "NONE" : "",
  752. mode == RT_WLAN_STATION ? "STA" : "",
  753. mode == RT_WLAN_AP ? "AP" : "");
  754. return mode;
  755. }
  756. #ifdef RT_WLAN_JOIN_SCAN_BY_MGNT
  757. static void rt_wlan_join_scan_callback(int event, struct rt_wlan_buff *buff, void *parameter)
  758. {
  759. struct rt_wlan_info *info = RT_NULL;
  760. struct rt_wlan_info *tgt_info = RT_NULL;
  761. RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT);
  762. RT_ASSERT(buff != RT_NULL);
  763. RT_ASSERT(parameter != RT_NULL);
  764. info = (struct rt_wlan_info *)buff->data;
  765. tgt_info = (struct rt_wlan_info *)parameter;
  766. RT_WLAN_LOG_D("%s info len:%d tgt info len:%d", __FUNCTION__,info->ssid.len,tgt_info->ssid.len);
  767. RT_WLAN_LOG_D("%s info ssid:%s tgt info ssid:%s", __FUNCTION__,&info->ssid.val[0],&tgt_info->ssid.val[0]);
  768. if(rt_memcmp(&info->ssid.val[0], &tgt_info->ssid.val[0], info->ssid.len) == 0 &&
  769. info->ssid.len == tgt_info->ssid.len)
  770. {
  771. /*Get the rssi the max ap*/
  772. if(info->rssi > tgt_info->rssi)
  773. {
  774. tgt_info->security = info->security;
  775. tgt_info->band = info->band;
  776. tgt_info->datarate = info->datarate;
  777. tgt_info->channel = info->channel;
  778. tgt_info->rssi = info->rssi;
  779. tgt_info->hidden = info->hidden;
  780. /* hwaddr */
  781. rt_memcpy(tgt_info->bssid,info->bssid,RT_WLAN_BSSID_MAX_LENGTH);
  782. }
  783. }
  784. }
  785. #endif
  786. rt_err_t rt_wlan_connect(const char *ssid, const char *password)
  787. {
  788. rt_err_t err = RT_EOK;
  789. int ssid_len = 0;
  790. struct rt_wlan_info info;
  791. struct rt_wlan_complete_des *complete;
  792. rt_uint32_t set = 0, recved = 0;
  793. /* sta dev Can't be NULL */
  794. if (_sta_is_null())
  795. {
  796. return -RT_EIO;
  797. }
  798. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
  799. if (ssid == RT_NULL)
  800. {
  801. RT_WLAN_LOG_E("ssid is null!");
  802. return -RT_EINVAL;
  803. }
  804. ssid_len = rt_strlen(ssid);
  805. if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
  806. {
  807. RT_WLAN_LOG_E("ssid is to long! ssid:%s len:%d", ssid, ssid_len);
  808. return -RT_EINVAL;
  809. }
  810. if ((rt_wlan_is_connected() == RT_TRUE) &&
  811. (rt_strcmp((char *)&_sta_mgnt.info.ssid.val[0], ssid) == 0))
  812. {
  813. RT_WLAN_LOG_I("wifi is connect ssid:%s", ssid);
  814. return RT_EOK;
  815. }
  816. /* get info from cache */
  817. INVALID_INFO(&info);
  818. MGNT_LOCK();
  819. rt_memcpy(&info.ssid.val[0],ssid,rt_strlen(ssid));
  820. info.ssid.len = rt_strlen(ssid);
  821. #ifdef RT_WLAN_JOIN_SCAN_BY_MGNT
  822. err = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,rt_wlan_join_scan_callback,&info);
  823. if(err != RT_EOK)
  824. {
  825. LOG_E("Scan register user callback error:%d!\n",err);
  826. return err;
  827. }
  828. err = rt_wlan_scan_with_info(&info);
  829. if(err != RT_EOK)
  830. {
  831. LOG_E("Scan with info error:%d!\n",err);
  832. return err;
  833. }
  834. if (info.channel <= 0)
  835. {
  836. RT_WLAN_LOG_W("not find ap! ssid:%s,info.ssid.len=%d", ssid,info.ssid.len);
  837. MGNT_UNLOCK();
  838. return -RT_ERROR;
  839. }
  840. RT_WLAN_LOG_D("find best info ssid:%s mac: %02x %02x %02x %02x %02x %02x",
  841. info.ssid.val, info.bssid[0], info.bssid[1], info.bssid[2], info.bssid[3], info.bssid[4], info.bssid[5]);
  842. #endif
  843. /* create event wait complete */
  844. complete = rt_wlan_complete_create("join");
  845. if (complete == RT_NULL)
  846. {
  847. MGNT_UNLOCK();
  848. return -RT_ENOMEM;
  849. }
  850. /* run connect adv */
  851. err = rt_wlan_connect_adv(&info, password);
  852. if (err != RT_EOK)
  853. {
  854. rt_wlan_complete_delete(complete);
  855. MGNT_UNLOCK();
  856. return err;
  857. }
  858. /* Initializing events that need to wait */
  859. set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT;
  860. set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT_FAIL;
  861. /* Check whether there is a waiting event */
  862. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  863. rt_wlan_complete_delete(complete);
  864. /* check event */
  865. set = 0x1 << RT_WLAN_DEV_EVT_CONNECT;
  866. if (!(recved & set))
  867. {
  868. RT_WLAN_LOG_I("wifi connect failed!");
  869. MGNT_UNLOCK();
  870. return -RT_ERROR;
  871. }
  872. MGNT_UNLOCK();
  873. return err;
  874. }
  875. rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password)
  876. {
  877. int password_len = 0;
  878. rt_err_t err = RT_EOK;
  879. if (_sta_is_null())
  880. {
  881. return -RT_EIO;
  882. }
  883. if (info == RT_NULL)
  884. {
  885. RT_WLAN_LOG_E("info is null!");
  886. return -RT_EINVAL;
  887. }
  888. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, info->ssid.val, password);
  889. /* Parameter checking */
  890. if (password != RT_NULL)
  891. {
  892. password_len = rt_strlen(password);
  893. if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
  894. {
  895. RT_WLAN_LOG_E("password is to long! password:%s len:%d", password, password_len);
  896. return -RT_EINVAL;
  897. }
  898. }
  899. if (info->ssid.len == 0 || info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
  900. {
  901. RT_WLAN_LOG_E("ssid is zero or to long! ssid:%s len:%d", info->ssid.val, info->ssid.len);
  902. return -RT_EINVAL;
  903. }
  904. /* is connect ? */
  905. MGNT_LOCK();
  906. if (rt_wlan_is_connected())
  907. {
  908. if ((_sta_mgnt.info.ssid.len == info->ssid.len) &&
  909. (_sta_mgnt.key.len == password_len) &&
  910. (rt_memcmp(&_sta_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
  911. (rt_memcmp(&_sta_mgnt.info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0) &&
  912. (rt_memcmp(&_sta_mgnt.key.val[0], password, password_len) == 0))
  913. {
  914. RT_WLAN_LOG_I("wifi Already Connected");
  915. MGNT_UNLOCK();
  916. return RT_EOK;
  917. }
  918. err = rt_wlan_disconnect();
  919. if (err != RT_EOK)
  920. {
  921. MGNT_UNLOCK();
  922. return err;
  923. }
  924. }
  925. /* save info */
  926. rt_enter_critical();
  927. _sta_mgnt.info = *info;
  928. rt_memcpy(&_sta_mgnt.key.val, password, password_len);
  929. _sta_mgnt.key.len = password_len;
  930. _sta_mgnt.key.val[password_len] = '\0';
  931. rt_exit_critical();
  932. /* run wifi connect */
  933. _sta_mgnt.state |= RT_WLAN_STATE_CONNECTING;
  934. err = rt_wlan_dev_fast_connect(_sta_mgnt.device, info, password, password_len);
  935. if(err != RT_EOK)
  936. {
  937. err = rt_wlan_dev_connect(_sta_mgnt.device, info, password, password_len);
  938. if (err != RT_EOK)
  939. {
  940. rt_enter_critical();
  941. rt_memset(&_sta_mgnt.info, 0, sizeof(struct rt_wlan_ssid));
  942. rt_memset(&_sta_mgnt.key, 0, sizeof(struct rt_wlan_key));
  943. rt_exit_critical();
  944. _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
  945. MGNT_UNLOCK();
  946. return err;
  947. }
  948. }
  949. MGNT_UNLOCK();
  950. return err;
  951. }
  952. rt_err_t rt_wlan_disconnect(void)
  953. {
  954. rt_err_t err;
  955. struct rt_wlan_complete_des *complete;
  956. rt_uint32_t recved = 0, set = 0;
  957. /* ap dev Can't be empty */
  958. if (_sta_is_null())
  959. {
  960. return -RT_EIO;
  961. }
  962. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  963. /* run disconnect */
  964. MGNT_LOCK();
  965. /* create event wait complete */
  966. complete = rt_wlan_complete_create("disc");
  967. if (complete == RT_NULL)
  968. {
  969. MGNT_UNLOCK();
  970. return -RT_ENOMEM;
  971. }
  972. err = rt_wlan_dev_disconnect(_sta_mgnt.device);
  973. if (err != RT_EOK)
  974. {
  975. RT_WLAN_LOG_E("wifi disconnect fail");
  976. rt_wlan_complete_delete(complete);
  977. MGNT_UNLOCK();
  978. return err;
  979. }
  980. /* Initializing events that need to wait */
  981. set |= 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
  982. /* Check whether there is a waiting event */
  983. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  984. rt_wlan_complete_delete(complete);
  985. /* check event */
  986. set = 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
  987. if (!(recved & set))
  988. {
  989. RT_WLAN_LOG_E("disconnect failed!");
  990. MGNT_UNLOCK();
  991. return -RT_ERROR;
  992. }
  993. RT_WLAN_LOG_I("disconnect success!");
  994. MGNT_UNLOCK();
  995. return err;
  996. }
  997. rt_bool_t rt_wlan_is_connected(void)
  998. {
  999. rt_bool_t _connect;
  1000. if (_sta_is_null())
  1001. {
  1002. return RT_FALSE;
  1003. }
  1004. _connect = _sta_mgnt.state & RT_WLAN_STATE_CONNECT ? RT_TRUE : RT_FALSE;
  1005. RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _connect ? "connect" : "disconnect");
  1006. return _connect;
  1007. }
  1008. rt_bool_t rt_wlan_is_ready(void)
  1009. {
  1010. rt_bool_t _ready;
  1011. if (_sta_is_null())
  1012. {
  1013. return RT_FALSE;
  1014. }
  1015. _ready = _sta_mgnt.state & RT_WLAN_STATE_READY ? RT_TRUE : RT_FALSE;
  1016. RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _ready ? "ready" : "not ready");
  1017. return _ready;
  1018. }
  1019. rt_err_t rt_wlan_set_mac(rt_uint8_t mac[6])
  1020. {
  1021. rt_err_t err = RT_EOK;
  1022. if (_sta_is_null())
  1023. {
  1024. return -RT_EIO;
  1025. }
  1026. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
  1027. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1028. MGNT_LOCK();
  1029. err = rt_wlan_dev_set_mac(STA_DEVICE(), mac);
  1030. if (err != RT_EOK)
  1031. {
  1032. RT_WLAN_LOG_E("set sta mac addr fail");
  1033. MGNT_UNLOCK();
  1034. return err;
  1035. }
  1036. MGNT_UNLOCK();
  1037. return err;
  1038. }
  1039. rt_err_t rt_wlan_get_mac(rt_uint8_t mac[6])
  1040. {
  1041. rt_err_t err = RT_EOK;
  1042. if (_sta_is_null())
  1043. {
  1044. return -RT_EIO;
  1045. }
  1046. MGNT_LOCK();
  1047. err = rt_wlan_dev_get_mac(STA_DEVICE(), mac);
  1048. if (err != RT_EOK)
  1049. {
  1050. RT_WLAN_LOG_E("get sta mac addr fail");
  1051. MGNT_UNLOCK();
  1052. return err;
  1053. }
  1054. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
  1055. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1056. MGNT_UNLOCK();
  1057. return err;
  1058. }
  1059. rt_err_t rt_wlan_get_info(struct rt_wlan_info *info)
  1060. {
  1061. if (_sta_is_null())
  1062. {
  1063. return -RT_EIO;
  1064. }
  1065. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1066. if (rt_wlan_is_connected() == RT_TRUE)
  1067. {
  1068. *info = _sta_mgnt.info;
  1069. info->rssi = rt_wlan_get_rssi();
  1070. return RT_EOK;
  1071. }
  1072. return -RT_ERROR;
  1073. }
  1074. int rt_wlan_get_rssi(void)
  1075. {
  1076. int rssi = 0;
  1077. if (_sta_is_null())
  1078. {
  1079. return -RT_EIO;
  1080. }
  1081. MGNT_LOCK();
  1082. rssi = rt_wlan_dev_get_rssi(STA_DEVICE());
  1083. RT_WLAN_LOG_D("%s is run rssi:%d", __FUNCTION__, rssi);
  1084. MGNT_UNLOCK();
  1085. return rssi;
  1086. }
  1087. rt_err_t rt_wlan_start_ap(const char *ssid, const char *password)
  1088. {
  1089. rt_err_t err = RT_EOK;
  1090. int ssid_len = 0;
  1091. struct rt_wlan_info info;
  1092. struct rt_wlan_complete_des *complete;
  1093. rt_uint32_t set = 0, recved = 0;
  1094. if (_ap_is_null())
  1095. {
  1096. return -RT_EIO;
  1097. }
  1098. if (ssid == RT_NULL) return -RT_EINVAL;
  1099. rt_memset(&info, 0, sizeof(struct rt_wlan_info));
  1100. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
  1101. if (password)
  1102. {
  1103. info.security = SECURITY_WPA2_AES_PSK;
  1104. }
  1105. ssid_len = rt_strlen(ssid);
  1106. if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
  1107. {
  1108. RT_WLAN_LOG_E("ssid is to long! len:%d", ssid_len);
  1109. }
  1110. /* copy info */
  1111. rt_memcpy(&info.ssid.val, ssid, ssid_len);
  1112. info.ssid.len = ssid_len;
  1113. info.channel = 6;
  1114. info.band = RT_802_11_BAND_2_4GHZ;
  1115. /* Initializing events that need to wait */
  1116. MGNT_LOCK();
  1117. /* create event wait complete */
  1118. complete = rt_wlan_complete_create("start_ap");
  1119. if (complete == RT_NULL)
  1120. {
  1121. MGNT_UNLOCK();
  1122. return -RT_ENOMEM;
  1123. }
  1124. /* start ap */
  1125. err = rt_wlan_start_ap_adv(&info, password);
  1126. if (err != RT_EOK)
  1127. {
  1128. rt_wlan_complete_delete(complete);
  1129. RT_WLAN_LOG_I("start ap failed!");
  1130. MGNT_UNLOCK();
  1131. return err;
  1132. }
  1133. /* Initializing events that need to wait */
  1134. set |= 0x1 << RT_WLAN_DEV_EVT_AP_START;
  1135. set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1136. /* Check whether there is a waiting event */
  1137. rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
  1138. rt_wlan_complete_delete(complete);
  1139. /* check event */
  1140. set = 0x1 << RT_WLAN_DEV_EVT_AP_START;
  1141. if (!(recved & set))
  1142. {
  1143. RT_WLAN_LOG_I("start ap failed!");
  1144. MGNT_UNLOCK();
  1145. return -RT_ERROR;
  1146. }
  1147. RT_WLAN_LOG_I("start ap successs!");
  1148. MGNT_UNLOCK();
  1149. return err;
  1150. }
  1151. rt_err_t rt_wlan_start_ap_adv(struct rt_wlan_info *info, const char *password)
  1152. {
  1153. rt_err_t err = RT_EOK;
  1154. int password_len = 0;
  1155. if (_ap_is_null())
  1156. {
  1157. return -RT_EIO;
  1158. }
  1159. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1160. if (password != RT_NULL)
  1161. {
  1162. password_len = rt_strlen(password);
  1163. }
  1164. if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
  1165. {
  1166. RT_WLAN_LOG_E("key is to long! len:%d", password_len);
  1167. return -RT_EINVAL;
  1168. }
  1169. /* is start up ? */
  1170. MGNT_LOCK();
  1171. if (rt_wlan_ap_is_active())
  1172. {
  1173. if ((_ap_mgnt.info.ssid.len == info->ssid.len) &&
  1174. (_ap_mgnt.info.security == info->security) &&
  1175. (_ap_mgnt.info.channel == info->channel) &&
  1176. (_ap_mgnt.info.hidden == info->hidden) &&
  1177. (_ap_mgnt.key.len == password_len) &&
  1178. (rt_memcmp(&_ap_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
  1179. (rt_memcmp(&_ap_mgnt.key.val[0], password, password_len)))
  1180. {
  1181. RT_WLAN_LOG_D("wifi Already Start");
  1182. MGNT_UNLOCK();
  1183. return RT_EOK;
  1184. }
  1185. }
  1186. err = rt_wlan_dev_ap_start(AP_DEVICE(), info, password, password_len);
  1187. if (err != RT_EOK)
  1188. {
  1189. MGNT_UNLOCK();
  1190. return err;
  1191. }
  1192. rt_memcpy(&_ap_mgnt.info, info, sizeof(struct rt_wlan_info));
  1193. rt_memcpy(&_ap_mgnt.key.val, password, password_len);
  1194. _ap_mgnt.key.len = password_len;
  1195. MGNT_UNLOCK();
  1196. return err;
  1197. }
  1198. rt_bool_t rt_wlan_ap_is_active(void)
  1199. {
  1200. rt_bool_t _active = RT_FALSE;
  1201. if (_ap_is_null())
  1202. {
  1203. return RT_FALSE;
  1204. }
  1205. _active = _ap_mgnt.state & RT_WLAN_STATE_ACTIVE ? RT_TRUE : RT_FALSE;
  1206. RT_WLAN_LOG_D("%s is run active:%s", __FUNCTION__, _active ? "Active" : "Inactive");
  1207. return _active;
  1208. }
  1209. rt_err_t rt_wlan_ap_stop(void)
  1210. {
  1211. rt_err_t err = RT_EOK;
  1212. struct rt_wlan_complete_des *complete;
  1213. rt_uint32_t set = 0, recved = 0;
  1214. if (_ap_is_null())
  1215. {
  1216. return -RT_EIO;
  1217. }
  1218. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1219. MGNT_LOCK();
  1220. /* create event wait complete */
  1221. complete = rt_wlan_complete_create("stop_ap");
  1222. if (complete == RT_NULL)
  1223. {
  1224. MGNT_UNLOCK();
  1225. return -RT_ENOMEM;
  1226. }
  1227. err = rt_wlan_dev_ap_stop(AP_DEVICE());
  1228. if (err != RT_EOK)
  1229. {
  1230. RT_WLAN_LOG_E("ap stop fail");
  1231. rt_wlan_complete_delete(complete);
  1232. MGNT_UNLOCK();
  1233. return err;
  1234. }
  1235. /* Initializing events that need to wait */
  1236. set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1237. /* Check whether there is a waiting event */
  1238. rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
  1239. rt_wlan_complete_delete(complete);
  1240. /* check event */
  1241. set = 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1242. if (!(recved & set))
  1243. {
  1244. RT_WLAN_LOG_I("ap stop failed!");
  1245. MGNT_UNLOCK();
  1246. return -RT_ERROR;
  1247. }
  1248. RT_WLAN_LOG_I("ap stop success!");
  1249. MGNT_UNLOCK();
  1250. return err;
  1251. }
  1252. rt_err_t rt_wlan_ap_get_info(struct rt_wlan_info *info)
  1253. {
  1254. if (_ap_is_null())
  1255. {
  1256. return -RT_EIO;
  1257. }
  1258. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1259. if (rt_wlan_ap_is_active() == RT_TRUE)
  1260. {
  1261. *info = _ap_mgnt.info;
  1262. return RT_EOK;
  1263. }
  1264. return -RT_ERROR;
  1265. }
  1266. /* get sta number */
  1267. int rt_wlan_ap_get_sta_num(void)
  1268. {
  1269. int sta_num = 0;
  1270. STAINFO_LOCK();
  1271. sta_num = sta_info.num;
  1272. STAINFO_UNLOCK();
  1273. RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, sta_num);
  1274. return sta_num;
  1275. }
  1276. /* get sta info */
  1277. int rt_wlan_ap_get_sta_info(struct rt_wlan_info *info, int num)
  1278. {
  1279. int sta_num = 0, i = 0;
  1280. struct rt_wlan_sta_list *sta_list;
  1281. STAINFO_LOCK();
  1282. /* sta_num = min(sta_info.num, num) */
  1283. sta_num = sta_info.num > num ? num : sta_info.num;
  1284. for (sta_list = sta_info.node; sta_list != RT_NULL && i < sta_num; sta_list = sta_list->next)
  1285. {
  1286. info[i] = sta_list->info;
  1287. i ++;
  1288. }
  1289. STAINFO_UNLOCK();
  1290. RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, i);
  1291. return i;
  1292. }
  1293. /* deauth sta */
  1294. rt_err_t rt_wlan_ap_deauth_sta(rt_uint8_t *mac)
  1295. {
  1296. rt_err_t err = RT_EOK;
  1297. struct rt_wlan_sta_list *sta_list;
  1298. rt_bool_t find_flag = RT_FALSE;
  1299. if (_ap_is_null())
  1300. {
  1301. return -RT_EIO;
  1302. }
  1303. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x:%d",
  1304. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1305. if (mac == RT_NULL)
  1306. {
  1307. RT_WLAN_LOG_E("mac addr is null");
  1308. return -RT_EINVAL;
  1309. }
  1310. MGNT_LOCK();
  1311. if (sta_info.node == RT_NULL || sta_info.num == 0)
  1312. {
  1313. RT_WLAN_LOG_E("No AP");
  1314. MGNT_UNLOCK();
  1315. return -RT_ERROR;
  1316. }
  1317. STAINFO_LOCK();
  1318. /* Search for MAC address from sta list */
  1319. for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_list->next)
  1320. {
  1321. if (rt_memcmp(&sta_list->info.bssid[0], &mac[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)
  1322. {
  1323. find_flag = RT_TRUE;
  1324. break;
  1325. }
  1326. }
  1327. STAINFO_UNLOCK();
  1328. /* No MAC address was found. return */
  1329. if (find_flag != RT_TRUE)
  1330. {
  1331. RT_WLAN_LOG_E("Not find mac addr");
  1332. MGNT_UNLOCK();
  1333. return -RT_ERROR;
  1334. }
  1335. /* Kill STA */
  1336. err = rt_wlan_dev_ap_deauth(AP_DEVICE(), mac);
  1337. if (err != RT_NULL)
  1338. {
  1339. RT_WLAN_LOG_E("deauth sta failed");
  1340. MGNT_UNLOCK();
  1341. return err;
  1342. }
  1343. MGNT_UNLOCK();
  1344. return err;
  1345. }
  1346. rt_err_t rt_wlan_ap_set_country(rt_country_code_t country_code)
  1347. {
  1348. rt_err_t err = RT_EOK;
  1349. if (_ap_is_null())
  1350. {
  1351. return -RT_EIO;
  1352. }
  1353. RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
  1354. MGNT_LOCK();
  1355. err = rt_wlan_dev_set_country(AP_DEVICE(), country_code);
  1356. MGNT_UNLOCK();
  1357. return err;
  1358. }
  1359. rt_country_code_t rt_wlan_ap_get_country(void)
  1360. {
  1361. rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
  1362. if (_ap_is_null())
  1363. {
  1364. return country_code;
  1365. }
  1366. MGNT_LOCK();
  1367. country_code = rt_wlan_dev_get_country(AP_DEVICE());
  1368. RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
  1369. MGNT_UNLOCK();
  1370. return country_code;
  1371. }
  1372. void rt_wlan_config_autoreconnect(rt_bool_t enable)
  1373. {
  1374. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  1375. RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
  1376. MGNT_LOCK();
  1377. if (enable)
  1378. {
  1379. TIME_START();
  1380. _sta_mgnt.flags |= RT_WLAN_STATE_AUTOEN;
  1381. }
  1382. else
  1383. {
  1384. TIME_STOP();
  1385. _sta_mgnt.flags &= ~RT_WLAN_STATE_AUTOEN;
  1386. }
  1387. MGNT_UNLOCK();
  1388. #endif
  1389. }
  1390. rt_bool_t rt_wlan_get_autoreconnect_mode(void)
  1391. {
  1392. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  1393. rt_bool_t enable = 0;
  1394. enable = _sta_mgnt.flags & RT_WLAN_STATE_AUTOEN ? 1 : 0;
  1395. RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
  1396. return enable;
  1397. #else
  1398. return RT_FALSE;
  1399. #endif
  1400. }
  1401. /* Call the underlying scan function, which is asynchronous.
  1402. The hotspots scanned are returned by callbacks */
  1403. rt_err_t rt_wlan_scan(void)
  1404. {
  1405. rt_err_t err = RT_EOK;
  1406. if (_sta_is_null())
  1407. {
  1408. return -RT_EIO;
  1409. }
  1410. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1411. MGNT_LOCK();
  1412. err = rt_wlan_dev_scan(STA_DEVICE(), RT_NULL);
  1413. MGNT_UNLOCK();
  1414. return err;
  1415. }
  1416. rt_err_t rt_wlan_scan_with_info(struct rt_wlan_info *info)
  1417. {
  1418. rt_err_t err = RT_EOK;
  1419. struct rt_wlan_complete_des *complete;
  1420. rt_uint32_t set = 0, recved = 0;
  1421. if (_sta_is_null())
  1422. {
  1423. return -RT_EINVAL;
  1424. }
  1425. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1426. if (info != RT_NULL && info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
  1427. {
  1428. RT_WLAN_LOG_E("ssid is to long!");
  1429. return -RT_EINVAL;
  1430. }
  1431. /* Create an event that needs to wait. */
  1432. MGNT_LOCK();
  1433. complete = rt_wlan_complete_create("scan");
  1434. if (complete == RT_NULL)
  1435. {
  1436. MGNT_UNLOCK();
  1437. return -RT_EIO;
  1438. }
  1439. /* run scan */
  1440. err = rt_wlan_dev_scan(STA_DEVICE(), info);
  1441. if (err != RT_EOK)
  1442. {
  1443. rt_wlan_complete_delete(complete);
  1444. MGNT_UNLOCK();
  1445. RT_WLAN_LOG_E("scan sync fail");
  1446. return -RT_ERROR;
  1447. }
  1448. /* Initializing events that need to wait */
  1449. set |= 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1450. /* Check whether there is a waiting event */
  1451. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  1452. rt_wlan_complete_delete(complete);
  1453. /* check event */
  1454. set = 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1455. if (!(recved & set))
  1456. {
  1457. MGNT_UNLOCK();
  1458. RT_WLAN_LOG_E("scan wait timeout!");
  1459. return -RT_ETIMEOUT;
  1460. }
  1461. MGNT_UNLOCK();
  1462. return RT_EOK;
  1463. }
  1464. rt_err_t rt_wlan_set_powersave(int level)
  1465. {
  1466. rt_err_t err = RT_EOK;
  1467. if (_sta_is_null())
  1468. {
  1469. return -RT_EIO;
  1470. }
  1471. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1472. MGNT_LOCK();
  1473. err = rt_wlan_dev_set_powersave(STA_DEVICE(), level);
  1474. MGNT_UNLOCK();
  1475. return err;
  1476. }
  1477. int rt_wlan_get_powersave(void)
  1478. {
  1479. int level;
  1480. if (_sta_is_null())
  1481. {
  1482. return -1;
  1483. }
  1484. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1485. MGNT_LOCK();
  1486. level = rt_wlan_dev_get_powersave(STA_DEVICE());
  1487. MGNT_UNLOCK();
  1488. return level;
  1489. }
  1490. rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter)
  1491. {
  1492. rt_base_t level;
  1493. if (event >= RT_WLAN_EVT_MAX)
  1494. {
  1495. return -RT_EINVAL;
  1496. }
  1497. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1498. MGNT_LOCK();
  1499. /* Registering Callbacks */
  1500. level = rt_hw_interrupt_disable();
  1501. event_tab[event].handler = handler;
  1502. event_tab[event].parameter = parameter;
  1503. rt_hw_interrupt_enable(level);
  1504. MGNT_UNLOCK();
  1505. return RT_EOK;
  1506. }
  1507. rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event)
  1508. {
  1509. rt_base_t level;
  1510. if (event >= RT_WLAN_EVT_MAX)
  1511. {
  1512. return -RT_EINVAL;
  1513. }
  1514. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1515. MGNT_LOCK();
  1516. /* unregister*/
  1517. level = rt_hw_interrupt_disable();
  1518. event_tab[event].handler = RT_NULL;
  1519. event_tab[event].parameter = RT_NULL;
  1520. rt_hw_interrupt_enable(level);
  1521. MGNT_UNLOCK();
  1522. return RT_EOK;
  1523. }
  1524. void rt_wlan_mgnt_lock(void)
  1525. {
  1526. MGNT_LOCK();
  1527. }
  1528. void rt_wlan_mgnt_unlock(void)
  1529. {
  1530. MGNT_UNLOCK();
  1531. }
  1532. int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff)
  1533. {
  1534. rt_base_t level;
  1535. if ((wlan == RT_NULL) || (_sta_mgnt.device != wlan) ||
  1536. (!(_sta_mgnt.state & RT_WLAN_STATE_CONNECT)))
  1537. {
  1538. return -1;
  1539. }
  1540. if (_sta_mgnt.state & RT_WLAN_STATE_READY)
  1541. {
  1542. return 0;
  1543. }
  1544. level = rt_hw_interrupt_disable();
  1545. _sta_mgnt.state |= RT_WLAN_STATE_READY;
  1546. rt_hw_interrupt_enable(level);
  1547. #ifdef RT_WLAN_WORK_THREAD_ENABLE
  1548. rt_wlan_send_to_thread(RT_WLAN_EVT_READY, buff->data, buff->len);
  1549. #else
  1550. {
  1551. void *user_parameter;
  1552. rt_wlan_event_handler handler = RT_NULL;
  1553. level = rt_hw_interrupt_disable();
  1554. handler = event_tab[RT_WLAN_EVT_READY].handler;
  1555. user_parameter = event_tab[RT_WLAN_EVT_READY].parameter;
  1556. rt_hw_interrupt_enable(level);
  1557. if (handler)
  1558. {
  1559. handler(RT_WLAN_EVT_READY, buff, user_parameter);
  1560. }
  1561. }
  1562. #endif
  1563. return 0;
  1564. }
  1565. int rt_wlan_init(void)
  1566. {
  1567. static rt_int8_t _init_flag = 0;
  1568. /* Execute only once */
  1569. if (_init_flag == 0)
  1570. {
  1571. rt_memset(&_sta_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1572. rt_memset(&_ap_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1573. rt_memset(&sta_info, 0, sizeof(struct rt_wlan_sta_des));
  1574. rt_mutex_init(&mgnt_mutex, "mgnt", RT_IPC_FLAG_FIFO);
  1575. rt_mutex_init(&sta_info_mutex, "sta", RT_IPC_FLAG_FIFO);
  1576. rt_mutex_init(&complete_mutex, "complete", RT_IPC_FLAG_FIFO);
  1577. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  1578. rt_timer_init(&reconnect_time, "wifi_tim", rt_wlan_cyclic_check, RT_NULL,
  1579. rt_tick_from_millisecond(AUTO_CONNECTION_PERIOD_MS),
  1580. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  1581. #endif
  1582. _init_flag = 1;
  1583. }
  1584. return 0;
  1585. }
  1586. INIT_PREV_EXPORT(rt_wlan_init);
  1587. #endif