wlan_mgnt.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. int ret = RT_EOK;
  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_memcmp(tgt_info->bssid,info->bssid,RT_WLAN_BSSID_MAX_LENGTH);
  782. }
  783. }
  784. }
  785. rt_err_t rt_wlan_connect(const char *ssid, const char *password)
  786. {
  787. rt_err_t err = RT_EOK;
  788. int ssid_len = 0;
  789. struct rt_wlan_info info;
  790. struct rt_wlan_complete_des *complete;
  791. rt_uint32_t set = 0, recved = 0;
  792. rt_uint32_t scan_retry = RT_WLAN_SCAN_RETRY_CNT;
  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. static struct rt_wlan_info scan_filter_info;
  1422. if (_sta_is_null())
  1423. {
  1424. return -RT_EINVAL;
  1425. }
  1426. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1427. if (info != RT_NULL && info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
  1428. {
  1429. RT_WLAN_LOG_E("ssid is to long!");
  1430. return -RT_EINVAL;
  1431. }
  1432. /* Create an event that needs to wait. */
  1433. MGNT_LOCK();
  1434. complete = rt_wlan_complete_create("scan");
  1435. if (complete == RT_NULL)
  1436. {
  1437. MGNT_UNLOCK();
  1438. return -RT_EIO;
  1439. }
  1440. /* run scan */
  1441. err = rt_wlan_dev_scan(STA_DEVICE(), info);
  1442. if (err != RT_EOK)
  1443. {
  1444. rt_wlan_complete_delete(complete);
  1445. MGNT_UNLOCK();
  1446. RT_WLAN_LOG_E("scan sync fail");
  1447. return -RT_ERROR;
  1448. }
  1449. /* Initializing events that need to wait */
  1450. set |= 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1451. /* Check whether there is a waiting event */
  1452. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  1453. rt_wlan_complete_delete(complete);
  1454. /* check event */
  1455. set = 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1456. if (!(recved & set))
  1457. {
  1458. MGNT_UNLOCK();
  1459. RT_WLAN_LOG_E("scan wait timeout!");
  1460. return -RT_ETIMEOUT;
  1461. }
  1462. MGNT_UNLOCK();
  1463. return RT_EOK;
  1464. }
  1465. rt_err_t rt_wlan_set_powersave(int level)
  1466. {
  1467. rt_err_t err = RT_EOK;
  1468. if (_sta_is_null())
  1469. {
  1470. return -RT_EIO;
  1471. }
  1472. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1473. MGNT_LOCK();
  1474. err = rt_wlan_dev_set_powersave(STA_DEVICE(), level);
  1475. MGNT_UNLOCK();
  1476. return err;
  1477. }
  1478. int rt_wlan_get_powersave(void)
  1479. {
  1480. int level;
  1481. if (_sta_is_null())
  1482. {
  1483. return -1;
  1484. }
  1485. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1486. MGNT_LOCK();
  1487. level = rt_wlan_dev_get_powersave(STA_DEVICE());
  1488. MGNT_UNLOCK();
  1489. return level;
  1490. }
  1491. rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter)
  1492. {
  1493. rt_base_t level;
  1494. if (event >= RT_WLAN_EVT_MAX)
  1495. {
  1496. return -RT_EINVAL;
  1497. }
  1498. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1499. MGNT_LOCK();
  1500. /* Registering Callbacks */
  1501. level = rt_hw_interrupt_disable();
  1502. event_tab[event].handler = handler;
  1503. event_tab[event].parameter = parameter;
  1504. rt_hw_interrupt_enable(level);
  1505. MGNT_UNLOCK();
  1506. return RT_EOK;
  1507. }
  1508. rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event)
  1509. {
  1510. rt_base_t level;
  1511. if (event >= RT_WLAN_EVT_MAX)
  1512. {
  1513. return -RT_EINVAL;
  1514. }
  1515. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1516. MGNT_LOCK();
  1517. /* unregister*/
  1518. level = rt_hw_interrupt_disable();
  1519. event_tab[event].handler = RT_NULL;
  1520. event_tab[event].parameter = RT_NULL;
  1521. rt_hw_interrupt_enable(level);
  1522. MGNT_UNLOCK();
  1523. return RT_EOK;
  1524. }
  1525. void rt_wlan_mgnt_lock(void)
  1526. {
  1527. MGNT_LOCK();
  1528. }
  1529. void rt_wlan_mgnt_unlock(void)
  1530. {
  1531. MGNT_UNLOCK();
  1532. }
  1533. int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff)
  1534. {
  1535. rt_base_t level;
  1536. if ((wlan == RT_NULL) || (_sta_mgnt.device != wlan) ||
  1537. (!(_sta_mgnt.state & RT_WLAN_STATE_CONNECT)))
  1538. {
  1539. return -1;
  1540. }
  1541. if (_sta_mgnt.state & RT_WLAN_STATE_READY)
  1542. {
  1543. return 0;
  1544. }
  1545. level = rt_hw_interrupt_disable();
  1546. _sta_mgnt.state |= RT_WLAN_STATE_READY;
  1547. rt_hw_interrupt_enable(level);
  1548. #ifdef RT_WLAN_WORK_THREAD_ENABLE
  1549. rt_wlan_send_to_thread(RT_WLAN_EVT_READY, buff->data, buff->len);
  1550. #else
  1551. {
  1552. void *user_parameter;
  1553. rt_wlan_event_handler handler = RT_NULL;
  1554. level = rt_hw_interrupt_disable();
  1555. handler = event_tab[RT_WLAN_EVT_READY].handler;
  1556. user_parameter = event_tab[RT_WLAN_EVT_READY].parameter;
  1557. rt_hw_interrupt_enable(level);
  1558. if (handler)
  1559. {
  1560. handler(RT_WLAN_EVT_READY, buff, user_parameter);
  1561. }
  1562. }
  1563. #endif
  1564. return 0;
  1565. }
  1566. int rt_wlan_init(void)
  1567. {
  1568. static rt_int8_t _init_flag = 0;
  1569. /* Execute only once */
  1570. if (_init_flag == 0)
  1571. {
  1572. rt_memset(&_sta_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1573. rt_memset(&_ap_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1574. rt_memset(&sta_info, 0, sizeof(struct rt_wlan_sta_des));
  1575. rt_mutex_init(&mgnt_mutex, "mgnt", RT_IPC_FLAG_FIFO);
  1576. rt_mutex_init(&sta_info_mutex, "sta", RT_IPC_FLAG_FIFO);
  1577. rt_mutex_init(&complete_mutex, "complete", RT_IPC_FLAG_FIFO);
  1578. #ifdef RT_WLAN_AUTO_CONNECT_ENABLE
  1579. rt_timer_init(&reconnect_time, "wifi_tim", rt_wlan_cyclic_check, RT_NULL,
  1580. rt_tick_from_millisecond(AUTO_CONNECTION_PERIOD_MS),
  1581. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  1582. #endif
  1583. _init_flag = 1;
  1584. }
  1585. return 0;
  1586. }
  1587. INIT_PREV_EXPORT(rt_wlan_init);
  1588. #endif