wlan_mgnt.c 49 KB

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