dev_wlan_mgnt.c 48 KB

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