1
0

dev_wlan_mgnt.c 47 KB

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