wlan_mgnt.c 47 KB

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