wlan_mgnt.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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. static void rt_wlan_join_scan_callback(int event, struct rt_wlan_buff *buff, void *parameter)
  757. {
  758. struct rt_wlan_info *info = RT_NULL;
  759. struct rt_wlan_info *tgt_info = RT_NULL;
  760. RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT);
  761. RT_ASSERT(buff != RT_NULL);
  762. RT_ASSERT(parameter != RT_NULL);
  763. info = (struct rt_wlan_info *)buff->data;
  764. tgt_info = (struct rt_wlan_info *)parameter;
  765. RT_WLAN_LOG_D("%s info len:%d tgt info len:%d", __FUNCTION__,info->ssid.len,tgt_info->ssid.len);
  766. RT_WLAN_LOG_D("%s info ssid:%s tgt info ssid:%s", __FUNCTION__,&info->ssid.val[0],&tgt_info->ssid.val[0]);
  767. if(rt_memcmp(&info->ssid.val[0], &tgt_info->ssid.val[0], info->ssid.len) == 0 &&
  768. info->ssid.len == tgt_info->ssid.len)
  769. {
  770. /*Get the rssi the max ap*/
  771. if(info->rssi > tgt_info->rssi)
  772. {
  773. tgt_info->security = info->security;
  774. tgt_info->band = info->band;
  775. tgt_info->datarate = info->datarate;
  776. tgt_info->channel = info->channel;
  777. tgt_info->rssi = info->rssi;
  778. tgt_info->hidden = info->hidden;
  779. /* hwaddr */
  780. rt_memcpy(tgt_info->bssid,info->bssid,RT_WLAN_BSSID_MAX_LENGTH);
  781. }
  782. }
  783. }
  784. rt_err_t rt_wlan_connect(const char *ssid, const char *password)
  785. {
  786. rt_err_t err = RT_EOK;
  787. int ssid_len = 0;
  788. struct rt_wlan_info info;
  789. struct rt_wlan_complete_des *complete;
  790. rt_uint32_t set = 0, recved = 0;
  791. /* sta dev Can't be NULL */
  792. if (_sta_is_null())
  793. {
  794. return -RT_EIO;
  795. }
  796. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
  797. if (ssid == RT_NULL)
  798. {
  799. RT_WLAN_LOG_E("ssid is null!");
  800. return -RT_EINVAL;
  801. }
  802. ssid_len = rt_strlen(ssid);
  803. if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
  804. {
  805. RT_WLAN_LOG_E("ssid is to long! ssid:%s len:%d", ssid, ssid_len);
  806. return -RT_EINVAL;
  807. }
  808. if ((rt_wlan_is_connected() == RT_TRUE) &&
  809. (rt_strcmp((char *)&_sta_mgnt.info.ssid.val[0], ssid) == 0))
  810. {
  811. RT_WLAN_LOG_I("wifi is connect ssid:%s", ssid);
  812. return RT_EOK;
  813. }
  814. /* get info from cache */
  815. INVALID_INFO(&info);
  816. MGNT_LOCK();
  817. rt_memcpy(&info.ssid.val[0],ssid,rt_strlen(ssid));
  818. info.ssid.len = rt_strlen(ssid);
  819. #ifdef RT_WLAN_JOIN_SCAN_BY_MGNT
  820. err = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,rt_wlan_join_scan_callback,&info);
  821. if(err != RT_EOK)
  822. {
  823. LOG_E("Scan register user callback error:%d!\n",err);
  824. return err;
  825. }
  826. err = rt_wlan_scan_with_info(&info);
  827. if(err != RT_EOK)
  828. {
  829. LOG_E("Scan with info error:%d!\n",err);
  830. return err;
  831. }
  832. if (info.channel <= 0)
  833. {
  834. RT_WLAN_LOG_W("not find ap! ssid:%s,info.ssid.len=%d", ssid,info.ssid.len);
  835. MGNT_UNLOCK();
  836. return -RT_ERROR;
  837. }
  838. RT_WLAN_LOG_D("find best info ssid:%s mac: %02x %02x %02x %02x %02x %02x",
  839. info.ssid.val, info.bssid[0], info.bssid[1], info.bssid[2], info.bssid[3], info.bssid[4], info.bssid[5]);
  840. #endif
  841. /* create event wait complete */
  842. complete = rt_wlan_complete_create("join");
  843. if (complete == RT_NULL)
  844. {
  845. MGNT_UNLOCK();
  846. return -RT_ENOMEM;
  847. }
  848. /* run connect adv */
  849. err = rt_wlan_connect_adv(&info, password);
  850. if (err != RT_EOK)
  851. {
  852. rt_wlan_complete_delete(complete);
  853. MGNT_UNLOCK();
  854. return err;
  855. }
  856. /* Initializing events that need to wait */
  857. set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT;
  858. set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT_FAIL;
  859. /* Check whether there is a waiting event */
  860. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  861. rt_wlan_complete_delete(complete);
  862. /* check event */
  863. set = 0x1 << RT_WLAN_DEV_EVT_CONNECT;
  864. if (!(recved & set))
  865. {
  866. RT_WLAN_LOG_I("wifi connect failed!");
  867. MGNT_UNLOCK();
  868. return -RT_ERROR;
  869. }
  870. MGNT_UNLOCK();
  871. return err;
  872. }
  873. rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password)
  874. {
  875. int password_len = 0;
  876. rt_err_t err = RT_EOK;
  877. if (_sta_is_null())
  878. {
  879. return -RT_EIO;
  880. }
  881. if (info == RT_NULL)
  882. {
  883. RT_WLAN_LOG_E("info is null!");
  884. return -RT_EINVAL;
  885. }
  886. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, info->ssid.val, password);
  887. /* Parameter checking */
  888. if (password != RT_NULL)
  889. {
  890. password_len = rt_strlen(password);
  891. if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
  892. {
  893. RT_WLAN_LOG_E("password is to long! password:%s len:%d", password, password_len);
  894. return -RT_EINVAL;
  895. }
  896. }
  897. if (info->ssid.len == 0 || info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
  898. {
  899. RT_WLAN_LOG_E("ssid is zero or to long! ssid:%s len:%d", info->ssid.val, info->ssid.len);
  900. return -RT_EINVAL;
  901. }
  902. /* is connect ? */
  903. MGNT_LOCK();
  904. if (rt_wlan_is_connected())
  905. {
  906. if ((_sta_mgnt.info.ssid.len == info->ssid.len) &&
  907. (_sta_mgnt.key.len == password_len) &&
  908. (rt_memcmp(&_sta_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
  909. (rt_memcmp(&_sta_mgnt.info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0) &&
  910. (rt_memcmp(&_sta_mgnt.key.val[0], password, password_len) == 0))
  911. {
  912. RT_WLAN_LOG_I("wifi Already Connected");
  913. MGNT_UNLOCK();
  914. return RT_EOK;
  915. }
  916. err = rt_wlan_disconnect();
  917. if (err != RT_EOK)
  918. {
  919. MGNT_UNLOCK();
  920. return err;
  921. }
  922. }
  923. /* save info */
  924. rt_enter_critical();
  925. _sta_mgnt.info = *info;
  926. rt_memcpy(&_sta_mgnt.key.val, password, password_len);
  927. _sta_mgnt.key.len = password_len;
  928. _sta_mgnt.key.val[password_len] = '\0';
  929. rt_exit_critical();
  930. /* run wifi connect */
  931. _sta_mgnt.state |= RT_WLAN_STATE_CONNECTING;
  932. err = rt_wlan_dev_fast_connect(_sta_mgnt.device, info, password, password_len);
  933. if(err != RT_EOK)
  934. {
  935. err = rt_wlan_dev_connect(_sta_mgnt.device, info, password, password_len);
  936. if (err != RT_EOK)
  937. {
  938. rt_enter_critical();
  939. rt_memset(&_sta_mgnt.info, 0, sizeof(struct rt_wlan_ssid));
  940. rt_memset(&_sta_mgnt.key, 0, sizeof(struct rt_wlan_key));
  941. rt_exit_critical();
  942. _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
  943. MGNT_UNLOCK();
  944. return err;
  945. }
  946. }
  947. MGNT_UNLOCK();
  948. return err;
  949. }
  950. rt_err_t rt_wlan_disconnect(void)
  951. {
  952. rt_err_t err;
  953. struct rt_wlan_complete_des *complete;
  954. rt_uint32_t recved = 0, set = 0;
  955. /* ap dev Can't be empty */
  956. if (_sta_is_null())
  957. {
  958. return -RT_EIO;
  959. }
  960. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  961. /* run disconnect */
  962. MGNT_LOCK();
  963. /* create event wait complete */
  964. complete = rt_wlan_complete_create("disc");
  965. if (complete == RT_NULL)
  966. {
  967. MGNT_UNLOCK();
  968. return -RT_ENOMEM;
  969. }
  970. err = rt_wlan_dev_disconnect(_sta_mgnt.device);
  971. if (err != RT_EOK)
  972. {
  973. RT_WLAN_LOG_E("wifi disconnect fail");
  974. rt_wlan_complete_delete(complete);
  975. MGNT_UNLOCK();
  976. return err;
  977. }
  978. /* Initializing events that need to wait */
  979. set |= 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
  980. /* Check whether there is a waiting event */
  981. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  982. rt_wlan_complete_delete(complete);
  983. /* check event */
  984. set = 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
  985. if (!(recved & set))
  986. {
  987. RT_WLAN_LOG_E("disconnect failed!");
  988. MGNT_UNLOCK();
  989. return -RT_ERROR;
  990. }
  991. RT_WLAN_LOG_I("disconnect success!");
  992. MGNT_UNLOCK();
  993. return err;
  994. }
  995. rt_bool_t rt_wlan_is_connected(void)
  996. {
  997. rt_bool_t _connect;
  998. if (_sta_is_null())
  999. {
  1000. return RT_FALSE;
  1001. }
  1002. _connect = _sta_mgnt.state & RT_WLAN_STATE_CONNECT ? RT_TRUE : RT_FALSE;
  1003. RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _connect ? "connect" : "disconnect");
  1004. return _connect;
  1005. }
  1006. rt_bool_t rt_wlan_is_ready(void)
  1007. {
  1008. rt_bool_t _ready;
  1009. if (_sta_is_null())
  1010. {
  1011. return RT_FALSE;
  1012. }
  1013. _ready = _sta_mgnt.state & RT_WLAN_STATE_READY ? RT_TRUE : RT_FALSE;
  1014. RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _ready ? "ready" : "not ready");
  1015. return _ready;
  1016. }
  1017. rt_err_t rt_wlan_set_mac(rt_uint8_t mac[6])
  1018. {
  1019. rt_err_t err = RT_EOK;
  1020. if (_sta_is_null())
  1021. {
  1022. return -RT_EIO;
  1023. }
  1024. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
  1025. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1026. MGNT_LOCK();
  1027. err = rt_wlan_dev_set_mac(STA_DEVICE(), mac);
  1028. if (err != RT_EOK)
  1029. {
  1030. RT_WLAN_LOG_E("set sta mac addr fail");
  1031. MGNT_UNLOCK();
  1032. return err;
  1033. }
  1034. MGNT_UNLOCK();
  1035. return err;
  1036. }
  1037. rt_err_t rt_wlan_get_mac(rt_uint8_t mac[6])
  1038. {
  1039. rt_err_t err = RT_EOK;
  1040. if (_sta_is_null())
  1041. {
  1042. return -RT_EIO;
  1043. }
  1044. MGNT_LOCK();
  1045. err = rt_wlan_dev_get_mac(STA_DEVICE(), mac);
  1046. if (err != RT_EOK)
  1047. {
  1048. RT_WLAN_LOG_E("get sta mac addr fail");
  1049. MGNT_UNLOCK();
  1050. return err;
  1051. }
  1052. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
  1053. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1054. MGNT_UNLOCK();
  1055. return err;
  1056. }
  1057. rt_err_t rt_wlan_get_info(struct rt_wlan_info *info)
  1058. {
  1059. if (_sta_is_null())
  1060. {
  1061. return -RT_EIO;
  1062. }
  1063. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1064. if (rt_wlan_is_connected() == RT_TRUE)
  1065. {
  1066. *info = _sta_mgnt.info;
  1067. info->rssi = rt_wlan_get_rssi();
  1068. return RT_EOK;
  1069. }
  1070. return -RT_ERROR;
  1071. }
  1072. int rt_wlan_get_rssi(void)
  1073. {
  1074. int rssi = 0;
  1075. if (_sta_is_null())
  1076. {
  1077. return -RT_EIO;
  1078. }
  1079. MGNT_LOCK();
  1080. rssi = rt_wlan_dev_get_rssi(STA_DEVICE());
  1081. RT_WLAN_LOG_D("%s is run rssi:%d", __FUNCTION__, rssi);
  1082. MGNT_UNLOCK();
  1083. return rssi;
  1084. }
  1085. rt_err_t rt_wlan_start_ap(const char *ssid, const char *password)
  1086. {
  1087. rt_err_t err = RT_EOK;
  1088. int ssid_len = 0;
  1089. struct rt_wlan_info info;
  1090. struct rt_wlan_complete_des *complete;
  1091. rt_uint32_t set = 0, recved = 0;
  1092. if (_ap_is_null())
  1093. {
  1094. return -RT_EIO;
  1095. }
  1096. if (ssid == RT_NULL) return -RT_EINVAL;
  1097. rt_memset(&info, 0, sizeof(struct rt_wlan_info));
  1098. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
  1099. if (password)
  1100. {
  1101. info.security = SECURITY_WPA2_AES_PSK;
  1102. }
  1103. ssid_len = rt_strlen(ssid);
  1104. if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
  1105. {
  1106. RT_WLAN_LOG_E("ssid is to long! len:%d", ssid_len);
  1107. }
  1108. /* copy info */
  1109. rt_memcpy(&info.ssid.val, ssid, ssid_len);
  1110. info.ssid.len = ssid_len;
  1111. info.channel = 6;
  1112. info.band = RT_802_11_BAND_2_4GHZ;
  1113. /* Initializing events that need to wait */
  1114. MGNT_LOCK();
  1115. /* create event wait complete */
  1116. complete = rt_wlan_complete_create("start_ap");
  1117. if (complete == RT_NULL)
  1118. {
  1119. MGNT_UNLOCK();
  1120. return -RT_ENOMEM;
  1121. }
  1122. /* start ap */
  1123. err = rt_wlan_start_ap_adv(&info, password);
  1124. if (err != RT_EOK)
  1125. {
  1126. rt_wlan_complete_delete(complete);
  1127. RT_WLAN_LOG_I("start ap failed!");
  1128. MGNT_UNLOCK();
  1129. return err;
  1130. }
  1131. /* Initializing events that need to wait */
  1132. set |= 0x1 << RT_WLAN_DEV_EVT_AP_START;
  1133. set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1134. /* Check whether there is a waiting event */
  1135. rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
  1136. rt_wlan_complete_delete(complete);
  1137. /* check event */
  1138. set = 0x1 << RT_WLAN_DEV_EVT_AP_START;
  1139. if (!(recved & set))
  1140. {
  1141. RT_WLAN_LOG_I("start ap failed!");
  1142. MGNT_UNLOCK();
  1143. return -RT_ERROR;
  1144. }
  1145. RT_WLAN_LOG_I("start ap successs!");
  1146. MGNT_UNLOCK();
  1147. return err;
  1148. }
  1149. rt_err_t rt_wlan_start_ap_adv(struct rt_wlan_info *info, const char *password)
  1150. {
  1151. rt_err_t err = RT_EOK;
  1152. int password_len = 0;
  1153. if (_ap_is_null())
  1154. {
  1155. return -RT_EIO;
  1156. }
  1157. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1158. if (password != RT_NULL)
  1159. {
  1160. password_len = rt_strlen(password);
  1161. }
  1162. if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
  1163. {
  1164. RT_WLAN_LOG_E("key is to long! len:%d", password_len);
  1165. return -RT_EINVAL;
  1166. }
  1167. /* is start up ? */
  1168. MGNT_LOCK();
  1169. if (rt_wlan_ap_is_active())
  1170. {
  1171. if ((_ap_mgnt.info.ssid.len == info->ssid.len) &&
  1172. (_ap_mgnt.info.security == info->security) &&
  1173. (_ap_mgnt.info.channel == info->channel) &&
  1174. (_ap_mgnt.info.hidden == info->hidden) &&
  1175. (_ap_mgnt.key.len == password_len) &&
  1176. (rt_memcmp(&_ap_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
  1177. (rt_memcmp(&_ap_mgnt.key.val[0], password, password_len)))
  1178. {
  1179. RT_WLAN_LOG_D("wifi Already Start");
  1180. MGNT_UNLOCK();
  1181. return RT_EOK;
  1182. }
  1183. }
  1184. err = rt_wlan_dev_ap_start(AP_DEVICE(), info, password, password_len);
  1185. if (err != RT_EOK)
  1186. {
  1187. MGNT_UNLOCK();
  1188. return err;
  1189. }
  1190. rt_memcpy(&_ap_mgnt.info, info, sizeof(struct rt_wlan_info));
  1191. rt_memcpy(&_ap_mgnt.key.val, password, password_len);
  1192. _ap_mgnt.key.len = password_len;
  1193. MGNT_UNLOCK();
  1194. return err;
  1195. }
  1196. rt_bool_t rt_wlan_ap_is_active(void)
  1197. {
  1198. rt_bool_t _active = RT_FALSE;
  1199. if (_ap_is_null())
  1200. {
  1201. return RT_FALSE;
  1202. }
  1203. _active = _ap_mgnt.state & RT_WLAN_STATE_ACTIVE ? RT_TRUE : RT_FALSE;
  1204. RT_WLAN_LOG_D("%s is run active:%s", __FUNCTION__, _active ? "Active" : "Inactive");
  1205. return _active;
  1206. }
  1207. rt_err_t rt_wlan_ap_stop(void)
  1208. {
  1209. rt_err_t err = RT_EOK;
  1210. struct rt_wlan_complete_des *complete;
  1211. rt_uint32_t set = 0, recved = 0;
  1212. if (_ap_is_null())
  1213. {
  1214. return -RT_EIO;
  1215. }
  1216. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1217. MGNT_LOCK();
  1218. /* create event wait complete */
  1219. complete = rt_wlan_complete_create("stop_ap");
  1220. if (complete == RT_NULL)
  1221. {
  1222. MGNT_UNLOCK();
  1223. return -RT_ENOMEM;
  1224. }
  1225. err = rt_wlan_dev_ap_stop(AP_DEVICE());
  1226. if (err != RT_EOK)
  1227. {
  1228. RT_WLAN_LOG_E("ap stop fail");
  1229. rt_wlan_complete_delete(complete);
  1230. MGNT_UNLOCK();
  1231. return err;
  1232. }
  1233. /* Initializing events that need to wait */
  1234. set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1235. /* Check whether there is a waiting event */
  1236. rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
  1237. rt_wlan_complete_delete(complete);
  1238. /* check event */
  1239. set = 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1240. if (!(recved & set))
  1241. {
  1242. RT_WLAN_LOG_I("ap stop failed!");
  1243. MGNT_UNLOCK();
  1244. return -RT_ERROR;
  1245. }
  1246. RT_WLAN_LOG_I("ap stop success!");
  1247. MGNT_UNLOCK();
  1248. return err;
  1249. }
  1250. rt_err_t rt_wlan_ap_get_info(struct rt_wlan_info *info)
  1251. {
  1252. if (_ap_is_null())
  1253. {
  1254. return -RT_EIO;
  1255. }
  1256. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1257. if (rt_wlan_ap_is_active() == RT_TRUE)
  1258. {
  1259. *info = _ap_mgnt.info;
  1260. return RT_EOK;
  1261. }
  1262. return -RT_ERROR;
  1263. }
  1264. /* get sta number */
  1265. int rt_wlan_ap_get_sta_num(void)
  1266. {
  1267. int sta_num = 0;
  1268. STAINFO_LOCK();
  1269. sta_num = sta_info.num;
  1270. STAINFO_UNLOCK();
  1271. RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, sta_num);
  1272. return sta_num;
  1273. }
  1274. /* get sta info */
  1275. int rt_wlan_ap_get_sta_info(struct rt_wlan_info *info, int num)
  1276. {
  1277. int sta_num = 0, i = 0;
  1278. struct rt_wlan_sta_list *sta_list;
  1279. STAINFO_LOCK();
  1280. /* sta_num = min(sta_info.num, num) */
  1281. sta_num = sta_info.num > num ? num : sta_info.num;
  1282. for (sta_list = sta_info.node; sta_list != RT_NULL && i < sta_num; sta_list = sta_list->next)
  1283. {
  1284. info[i] = sta_list->info;
  1285. i ++;
  1286. }
  1287. STAINFO_UNLOCK();
  1288. RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, i);
  1289. return i;
  1290. }
  1291. /* deauth sta */
  1292. rt_err_t rt_wlan_ap_deauth_sta(rt_uint8_t *mac)
  1293. {
  1294. rt_err_t err = RT_EOK;
  1295. struct rt_wlan_sta_list *sta_list;
  1296. rt_bool_t find_flag = RT_FALSE;
  1297. if (_ap_is_null())
  1298. {
  1299. return -RT_EIO;
  1300. }
  1301. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x:%d",
  1302. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1303. if (mac == RT_NULL)
  1304. {
  1305. RT_WLAN_LOG_E("mac addr is null");
  1306. return -RT_EINVAL;
  1307. }
  1308. MGNT_LOCK();
  1309. if (sta_info.node == RT_NULL || sta_info.num == 0)
  1310. {
  1311. RT_WLAN_LOG_E("No AP");
  1312. MGNT_UNLOCK();
  1313. return -RT_ERROR;
  1314. }
  1315. STAINFO_LOCK();
  1316. /* Search for MAC address from sta list */
  1317. for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_list->next)
  1318. {
  1319. if (rt_memcmp(&sta_list->info.bssid[0], &mac[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)
  1320. {
  1321. find_flag = RT_TRUE;
  1322. break;
  1323. }
  1324. }
  1325. STAINFO_UNLOCK();
  1326. /* No MAC address was found. return */
  1327. if (find_flag != RT_TRUE)
  1328. {
  1329. RT_WLAN_LOG_E("Not find mac addr");
  1330. MGNT_UNLOCK();
  1331. return -RT_ERROR;
  1332. }
  1333. /* Kill STA */
  1334. err = rt_wlan_dev_ap_deauth(AP_DEVICE(), mac);
  1335. if (err != RT_NULL)
  1336. {
  1337. RT_WLAN_LOG_E("deauth sta failed");
  1338. MGNT_UNLOCK();
  1339. return err;
  1340. }
  1341. MGNT_UNLOCK();
  1342. return err;
  1343. }
  1344. rt_err_t rt_wlan_ap_set_country(rt_country_code_t country_code)
  1345. {
  1346. rt_err_t err = RT_EOK;
  1347. if (_ap_is_null())
  1348. {
  1349. return -RT_EIO;
  1350. }
  1351. RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
  1352. MGNT_LOCK();
  1353. err = rt_wlan_dev_set_country(AP_DEVICE(), country_code);
  1354. MGNT_UNLOCK();
  1355. return err;
  1356. }
  1357. rt_country_code_t rt_wlan_ap_get_country(void)
  1358. {
  1359. rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
  1360. if (_ap_is_null())
  1361. {
  1362. return country_code;
  1363. }
  1364. MGNT_LOCK();
  1365. country_code = rt_wlan_dev_get_country(AP_DEVICE());
  1366. RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
  1367. MGNT_UNLOCK();
  1368. return country_code;
  1369. }
  1370. void rt_wlan_config_autoreconnect(rt_bool_t enable)
  1371. {
  1372. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  1373. RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
  1374. MGNT_LOCK();
  1375. if (enable)
  1376. {
  1377. TIME_START();
  1378. _sta_mgnt.flags |= RT_WLAN_STATE_AUTOEN;
  1379. }
  1380. else
  1381. {
  1382. TIME_STOP();
  1383. _sta_mgnt.flags &= ~RT_WLAN_STATE_AUTOEN;
  1384. }
  1385. MGNT_UNLOCK();
  1386. #endif
  1387. }
  1388. rt_bool_t rt_wlan_get_autoreconnect_mode(void)
  1389. {
  1390. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  1391. rt_bool_t enable = 0;
  1392. enable = _sta_mgnt.flags & RT_WLAN_STATE_AUTOEN ? 1 : 0;
  1393. RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
  1394. return enable;
  1395. #else
  1396. return RT_FALSE;
  1397. #endif
  1398. }
  1399. /* Call the underlying scan function, which is asynchronous.
  1400. The hotspots scanned are returned by callbacks */
  1401. rt_err_t rt_wlan_scan(void)
  1402. {
  1403. rt_err_t err = RT_EOK;
  1404. if (_sta_is_null())
  1405. {
  1406. return -RT_EIO;
  1407. }
  1408. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1409. MGNT_LOCK();
  1410. err = rt_wlan_dev_scan(STA_DEVICE(), RT_NULL);
  1411. MGNT_UNLOCK();
  1412. return err;
  1413. }
  1414. rt_err_t rt_wlan_scan_with_info(struct rt_wlan_info *info)
  1415. {
  1416. rt_err_t err = RT_EOK;
  1417. struct rt_wlan_complete_des *complete;
  1418. rt_uint32_t set = 0, recved = 0;
  1419. if (_sta_is_null())
  1420. {
  1421. return -RT_EINVAL;
  1422. }
  1423. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1424. if (info != RT_NULL && info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
  1425. {
  1426. RT_WLAN_LOG_E("ssid is to long!");
  1427. return -RT_EINVAL;
  1428. }
  1429. /* Create an event that needs to wait. */
  1430. MGNT_LOCK();
  1431. complete = rt_wlan_complete_create("scan");
  1432. if (complete == RT_NULL)
  1433. {
  1434. MGNT_UNLOCK();
  1435. return -RT_EIO;
  1436. }
  1437. /* run scan */
  1438. err = rt_wlan_dev_scan(STA_DEVICE(), info);
  1439. if (err != RT_EOK)
  1440. {
  1441. rt_wlan_complete_delete(complete);
  1442. MGNT_UNLOCK();
  1443. RT_WLAN_LOG_E("scan sync fail");
  1444. return -RT_ERROR;
  1445. }
  1446. /* Initializing events that need to wait */
  1447. set |= 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1448. /* Check whether there is a waiting event */
  1449. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  1450. rt_wlan_complete_delete(complete);
  1451. /* check event */
  1452. set = 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1453. if (!(recved & set))
  1454. {
  1455. MGNT_UNLOCK();
  1456. RT_WLAN_LOG_E("scan wait timeout!");
  1457. return -RT_ETIMEOUT;
  1458. }
  1459. MGNT_UNLOCK();
  1460. return RT_EOK;
  1461. }
  1462. rt_err_t rt_wlan_set_powersave(int level)
  1463. {
  1464. rt_err_t err = RT_EOK;
  1465. if (_sta_is_null())
  1466. {
  1467. return -RT_EIO;
  1468. }
  1469. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1470. MGNT_LOCK();
  1471. err = rt_wlan_dev_set_powersave(STA_DEVICE(), level);
  1472. MGNT_UNLOCK();
  1473. return err;
  1474. }
  1475. int rt_wlan_get_powersave(void)
  1476. {
  1477. int level;
  1478. if (_sta_is_null())
  1479. {
  1480. return -1;
  1481. }
  1482. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1483. MGNT_LOCK();
  1484. level = rt_wlan_dev_get_powersave(STA_DEVICE());
  1485. MGNT_UNLOCK();
  1486. return level;
  1487. }
  1488. rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter)
  1489. {
  1490. rt_base_t level;
  1491. if (event >= RT_WLAN_EVT_MAX)
  1492. {
  1493. return -RT_EINVAL;
  1494. }
  1495. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1496. MGNT_LOCK();
  1497. /* Registering Callbacks */
  1498. level = rt_hw_interrupt_disable();
  1499. event_tab[event].handler = handler;
  1500. event_tab[event].parameter = parameter;
  1501. rt_hw_interrupt_enable(level);
  1502. MGNT_UNLOCK();
  1503. return RT_EOK;
  1504. }
  1505. rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event)
  1506. {
  1507. rt_base_t level;
  1508. if (event >= RT_WLAN_EVT_MAX)
  1509. {
  1510. return -RT_EINVAL;
  1511. }
  1512. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1513. MGNT_LOCK();
  1514. /* unregister*/
  1515. level = rt_hw_interrupt_disable();
  1516. event_tab[event].handler = RT_NULL;
  1517. event_tab[event].parameter = RT_NULL;
  1518. rt_hw_interrupt_enable(level);
  1519. MGNT_UNLOCK();
  1520. return RT_EOK;
  1521. }
  1522. void rt_wlan_mgnt_lock(void)
  1523. {
  1524. MGNT_LOCK();
  1525. }
  1526. void rt_wlan_mgnt_unlock(void)
  1527. {
  1528. MGNT_UNLOCK();
  1529. }
  1530. int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff)
  1531. {
  1532. rt_base_t level;
  1533. if ((wlan == RT_NULL) || (_sta_mgnt.device != wlan) ||
  1534. (!(_sta_mgnt.state & RT_WLAN_STATE_CONNECT)))
  1535. {
  1536. return -1;
  1537. }
  1538. if (_sta_mgnt.state & RT_WLAN_STATE_READY)
  1539. {
  1540. return 0;
  1541. }
  1542. level = rt_hw_interrupt_disable();
  1543. _sta_mgnt.state |= RT_WLAN_STATE_READY;
  1544. rt_hw_interrupt_enable(level);
  1545. #ifdef RT_WLAN_WORK_THREAD_ENABLE
  1546. rt_wlan_send_to_thread(RT_WLAN_EVT_READY, buff->data, buff->len);
  1547. #else
  1548. {
  1549. void *user_parameter;
  1550. rt_wlan_event_handler handler = RT_NULL;
  1551. level = rt_hw_interrupt_disable();
  1552. handler = event_tab[RT_WLAN_EVT_READY].handler;
  1553. user_parameter = event_tab[RT_WLAN_EVT_READY].parameter;
  1554. rt_hw_interrupt_enable(level);
  1555. if (handler)
  1556. {
  1557. handler(RT_WLAN_EVT_READY, buff, user_parameter);
  1558. }
  1559. }
  1560. #endif
  1561. return 0;
  1562. }
  1563. int rt_wlan_init(void)
  1564. {
  1565. static rt_int8_t _init_flag = 0;
  1566. /* Execute only once */
  1567. if (_init_flag == 0)
  1568. {
  1569. rt_memset(&_sta_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1570. rt_memset(&_ap_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1571. rt_memset(&sta_info, 0, sizeof(struct rt_wlan_sta_des));
  1572. rt_mutex_init(&mgnt_mutex, "mgnt", RT_IPC_FLAG_FIFO);
  1573. rt_mutex_init(&sta_info_mutex, "sta", RT_IPC_FLAG_FIFO);
  1574. rt_mutex_init(&complete_mutex, "complete", RT_IPC_FLAG_FIFO);
  1575. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  1576. rt_timer_init(&reconnect_time, "wifi_tim", rt_wlan_cyclic_check, RT_NULL,
  1577. rt_tick_from_millisecond(AUTO_CONNECTION_PERIOD_MS),
  1578. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  1579. #endif
  1580. _init_flag = 1;
  1581. }
  1582. return 0;
  1583. }
  1584. INIT_PREV_EXPORT(rt_wlan_init);
  1585. #endif