wlan_mgnt.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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. /* 事件处理 */
  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. if (RT_WLAN_DEVICE(device)->mode == mode)
  660. {
  661. RT_WLAN_LOG_D("L:%d this device mode is set");
  662. return RT_EOK;
  663. }
  664. if ((mode == RT_WLAN_STATION) &&
  665. (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_AP_ONLY))
  666. {
  667. RT_WLAN_LOG_I("this device ap mode only");
  668. return -RT_ERROR;
  669. }
  670. else if ((mode == RT_WLAN_AP) &&
  671. (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_STA_ONLY))
  672. {
  673. RT_WLAN_LOG_I("this device sta mode only");
  674. return -RT_ERROR;
  675. }
  676. /*
  677. * device == sta and change to ap, should deinit
  678. * device == ap and change to sta, should deinit
  679. */
  680. MGNT_LOCK();
  681. if (((mode == RT_WLAN_STATION) && (RT_WLAN_DEVICE(device) == AP_DEVICE())) ||
  682. ((mode == RT_WLAN_AP) && (RT_WLAN_DEVICE(device) == STA_DEVICE())))
  683. {
  684. err = rt_wlan_set_mode(dev_name, RT_WLAN_NONE);
  685. if (err != RT_EOK)
  686. {
  687. RT_WLAN_LOG_E("change mode failed!");
  688. MGNT_UNLOCK();
  689. return err;
  690. }
  691. }
  692. /* init device */
  693. err = rt_wlan_dev_init(RT_WLAN_DEVICE(device), mode);
  694. if (err != RT_EOK)
  695. {
  696. RT_WLAN_LOG_E("F:%s L:%d wlan init failed", __FUNCTION__, __LINE__);
  697. MGNT_UNLOCK();
  698. return err;
  699. }
  700. /* the mode is none */
  701. if (mode == RT_WLAN_NONE)
  702. {
  703. if (_sta_mgnt.device == RT_WLAN_DEVICE(device))
  704. {
  705. _sta_mgnt.device = RT_NULL;
  706. _sta_mgnt.state = 0;
  707. up_event_flag = 1;
  708. handler = RT_NULL;
  709. }
  710. else if (_ap_mgnt.device == RT_WLAN_DEVICE(device))
  711. {
  712. _ap_mgnt.state = 0;
  713. _ap_mgnt.device = RT_NULL;
  714. up_event_flag = 1;
  715. handler = RT_NULL;
  716. }
  717. }
  718. /* save sta device */
  719. else if (mode == RT_WLAN_STATION)
  720. {
  721. up_event_flag = 1;
  722. handler = rt_wlan_event_dispatch;
  723. _sta_mgnt.device = RT_WLAN_DEVICE(device);
  724. }
  725. /* save ap device */
  726. else if (mode == RT_WLAN_AP)
  727. {
  728. up_event_flag = 1;
  729. handler = rt_wlan_event_dispatch;
  730. _ap_mgnt.device = RT_WLAN_DEVICE(device);
  731. }
  732. /* update dev event handle */
  733. if (up_event_flag == 1)
  734. {
  735. rt_wlan_dev_event_t event;
  736. for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event++)
  737. {
  738. if (handler)
  739. {
  740. rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), event, handler, RT_NULL);
  741. }
  742. else
  743. {
  744. rt_wlan_dev_unregister_event_handler(RT_WLAN_DEVICE(device), event, handler);
  745. }
  746. }
  747. }
  748. MGNT_UNLOCK();
  749. /* Mount protocol */
  750. #ifdef RT_WLAN_DEFAULT_PROT
  751. rt_wlan_prot_attach(dev_name, RT_WLAN_DEFAULT_PROT);
  752. #endif
  753. return err;
  754. }
  755. rt_wlan_mode_t rt_wlan_get_mode(const char *dev_name)
  756. {
  757. rt_device_t device = RT_NULL;
  758. rt_wlan_mode_t mode;
  759. if (dev_name == RT_NULL)
  760. {
  761. RT_WLAN_LOG_E("name is null");
  762. return RT_WLAN_NONE;
  763. }
  764. /* find device */
  765. device = rt_device_find(dev_name);
  766. if (device == RT_NULL)
  767. {
  768. RT_WLAN_LOG_E("device not find! name:%s", dev_name);
  769. return RT_WLAN_NONE;
  770. }
  771. /* get mode */
  772. mode = RT_WLAN_DEVICE(device)->mode;
  773. RT_WLAN_LOG_D("%s is run dev_name:%s mode:%s%s%s", __FUNCTION__, dev_name,
  774. mode == RT_WLAN_NONE ? "NONE" : "",
  775. mode == RT_WLAN_STATION ? "STA" : "",
  776. mode == RT_WLAN_AP ? "AP" : "");
  777. return mode;
  778. }
  779. rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info)
  780. {
  781. int i, ssid_len;
  782. struct rt_wlan_info *info_best;
  783. struct rt_wlan_scan_result *result;
  784. ssid_len = rt_strlen(ssid);
  785. result = &scan_result;
  786. info_best = RT_NULL;
  787. SRESULT_LOCK();
  788. for (i = 0; i < result->num; i++)
  789. {
  790. /* SSID is equal. */
  791. if ((result->info[i].ssid.len == ssid_len) &&
  792. (rt_memcmp((char *)&result->info[i].ssid.val[0], ssid, ssid_len) == 0))
  793. {
  794. if (info_best == RT_NULL)
  795. {
  796. info_best = &result->info[i];
  797. continue;
  798. }
  799. /* Signal strength effective */
  800. if ((result->info[i].rssi < 0) && (info_best->rssi < 0))
  801. {
  802. /* Find the strongest signal. */
  803. if (result->info[i].rssi > info_best->rssi)
  804. {
  805. info_best = &result->info[i];
  806. continue;
  807. }
  808. else if (result->info[i].rssi < info_best->rssi)
  809. {
  810. continue;
  811. }
  812. }
  813. /* Finding the fastest signal */
  814. if (result->info[i].datarate > info_best->datarate)
  815. {
  816. info_best = &result->info[i];
  817. continue;
  818. }
  819. }
  820. }
  821. SRESULT_UNLOCK();
  822. if (info_best == RT_NULL)
  823. return RT_FALSE;
  824. *info = *info_best;
  825. return RT_TRUE;
  826. }
  827. rt_err_t rt_wlan_connect(const char *ssid, const char *password)
  828. {
  829. rt_err_t err = RT_EOK;
  830. int ssid_len = 0;
  831. struct rt_wlan_info info;
  832. struct rt_wlan_complete_des *complete;
  833. rt_uint32_t set = 0, recved = 0;
  834. /* sta dev Can't be NULL */
  835. if (_sta_is_null())
  836. {
  837. return -RT_EIO;
  838. }
  839. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
  840. if (ssid == RT_NULL)
  841. {
  842. RT_WLAN_LOG_E("ssid is null!");
  843. return -RT_EINVAL;
  844. }
  845. ssid_len = rt_strlen(ssid);
  846. if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
  847. {
  848. RT_WLAN_LOG_E("ssid is to long! ssid:%s len:%d", ssid, ssid_len);
  849. return -RT_EINVAL;
  850. }
  851. if ((rt_wlan_is_connected() == RT_TRUE) &&
  852. (rt_strcmp((char *)&_sta_mgnt.info.ssid.val[0], ssid) == 0))
  853. {
  854. RT_WLAN_LOG_I("wifi is connect ssid:%s", ssid);
  855. return RT_EOK;
  856. }
  857. /* get info from cache */
  858. INVALID_INFO(&info);
  859. MGNT_LOCK();
  860. if (rt_wlan_find_best_by_cache(ssid, &info) != RT_TRUE)
  861. {
  862. rt_wlan_scan_sync();
  863. rt_wlan_find_best_by_cache(ssid, &info);
  864. rt_wlan_scan_result_clean();
  865. }
  866. if (info.ssid.len <= 0)
  867. {
  868. RT_WLAN_LOG_W("not find ap! ssid:%s", ssid);
  869. MGNT_UNLOCK();
  870. return -RT_ERROR;
  871. }
  872. RT_WLAN_LOG_D("find best info ssid:%s mac: %02x %02x %02x %02x %02x %02x",
  873. info.ssid.val, info.bssid[0], info.bssid[1], info.bssid[2], info.bssid[3], info.bssid[4], info.bssid[5]);
  874. /* create event wait complete */
  875. complete = rt_wlan_complete_create("join");
  876. if (complete == RT_NULL)
  877. {
  878. MGNT_UNLOCK();
  879. return -RT_ENOMEM;
  880. }
  881. /* run connect adv */
  882. err = rt_wlan_connect_adv(&info, password);
  883. if (err != RT_EOK)
  884. {
  885. rt_wlan_complete_delete(complete);
  886. MGNT_UNLOCK();
  887. return err;
  888. }
  889. /* Initializing events that need to wait */
  890. set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT;
  891. set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT_FAIL;
  892. /* Check whether there is a waiting event */
  893. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  894. rt_wlan_complete_delete(complete);
  895. /* check event */
  896. set = 0x1 << RT_WLAN_DEV_EVT_CONNECT;
  897. if (!(recved & set))
  898. {
  899. RT_WLAN_LOG_I("wifi connect failed!");
  900. MGNT_UNLOCK();
  901. return -RT_ERROR;
  902. }
  903. MGNT_UNLOCK();
  904. return err;
  905. }
  906. rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password)
  907. {
  908. int password_len = 0;
  909. rt_err_t err = RT_EOK;
  910. if (_sta_is_null())
  911. {
  912. return -RT_EIO;
  913. }
  914. if (info == RT_NULL)
  915. {
  916. RT_WLAN_LOG_E("info is null!");
  917. return -RT_EINVAL;
  918. }
  919. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, info->ssid.val, password);
  920. /* Parameter checking */
  921. if (password != RT_NULL)
  922. {
  923. password_len = rt_strlen(password);
  924. if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
  925. {
  926. RT_WLAN_LOG_E("password is to long! password:%s len:%d", password, password_len);
  927. return -RT_EINVAL;
  928. }
  929. }
  930. if (info->ssid.len == 0 || info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
  931. {
  932. RT_WLAN_LOG_E("ssid is zero or to long! ssid:%s len:%d", info->ssid.val, info->ssid.len);
  933. return -RT_EINVAL;
  934. }
  935. /* is connect ? */
  936. MGNT_LOCK();
  937. if (rt_wlan_is_connected())
  938. {
  939. if ((_sta_mgnt.info.ssid.len == info->ssid.len) &&
  940. (_sta_mgnt.key.len == password_len) &&
  941. (rt_memcmp(&_sta_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
  942. (rt_memcmp(&_sta_mgnt.info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0) &&
  943. (rt_memcmp(&_sta_mgnt.key.val[0], password, password_len) == 0))
  944. {
  945. RT_WLAN_LOG_I("wifi Already Connected");
  946. MGNT_UNLOCK();
  947. return RT_EOK;
  948. }
  949. err = rt_wlan_disconnect();
  950. if (err != RT_EOK)
  951. {
  952. MGNT_UNLOCK();
  953. return err;
  954. }
  955. }
  956. /* save info */
  957. rt_enter_critical();
  958. _sta_mgnt.info = *info;
  959. rt_memcpy(&_sta_mgnt.key.val, password, password_len);
  960. _sta_mgnt.key.len = password_len;
  961. _sta_mgnt.key.val[password_len] = '\0';
  962. rt_exit_critical();
  963. /* run wifi connect */
  964. _sta_mgnt.state |= RT_WLAN_STATE_CONNECTING;
  965. err = rt_wlan_dev_connect(_sta_mgnt.device, info, password, password_len);
  966. if (err != RT_EOK)
  967. {
  968. rt_enter_critical();
  969. rt_memset(&_sta_mgnt.info, 0, sizeof(struct rt_wlan_ssid));
  970. rt_memset(&_sta_mgnt.key, 0, sizeof(struct rt_wlan_key));
  971. rt_exit_critical();
  972. _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING;
  973. MGNT_UNLOCK();
  974. return err;
  975. }
  976. MGNT_UNLOCK();
  977. return err;
  978. }
  979. rt_err_t rt_wlan_disconnect(void)
  980. {
  981. rt_err_t err;
  982. struct rt_wlan_complete_des *complete;
  983. rt_uint32_t recved = 0, set = 0;
  984. /* ap dev Can't be empty */
  985. if (_sta_is_null())
  986. {
  987. return -RT_EIO;
  988. }
  989. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  990. /* run disconnect */
  991. MGNT_LOCK();
  992. /* create event wait complete */
  993. complete = rt_wlan_complete_create("disc");
  994. if (complete == RT_NULL)
  995. {
  996. MGNT_UNLOCK();
  997. return -RT_ENOMEM;
  998. }
  999. err = rt_wlan_dev_disconnect(_sta_mgnt.device);
  1000. if (err != RT_EOK)
  1001. {
  1002. RT_WLAN_LOG_E("wifi disconnect fail");
  1003. rt_wlan_complete_delete(complete);
  1004. MGNT_UNLOCK();
  1005. return err;
  1006. }
  1007. /* Initializing events that need to wait */
  1008. set |= 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
  1009. /* Check whether there is a waiting event */
  1010. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  1011. rt_wlan_complete_delete(complete);
  1012. /* check event */
  1013. set = 0x1 << RT_WLAN_DEV_EVT_DISCONNECT;
  1014. if (!(recved & set))
  1015. {
  1016. RT_WLAN_LOG_E("disconnect failed!");
  1017. MGNT_UNLOCK();
  1018. return -RT_ERROR;
  1019. }
  1020. RT_WLAN_LOG_I("disconnect success!");
  1021. MGNT_UNLOCK();
  1022. return err;
  1023. }
  1024. rt_bool_t rt_wlan_is_connected(void)
  1025. {
  1026. rt_bool_t _connect;
  1027. if (_sta_is_null())
  1028. {
  1029. return RT_FALSE;
  1030. }
  1031. _connect = _sta_mgnt.state & RT_WLAN_STATE_CONNECT ? RT_TRUE : RT_FALSE;
  1032. RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _connect ? "connect" : "disconnect");
  1033. return _connect;
  1034. }
  1035. rt_bool_t rt_wlan_is_ready(void)
  1036. {
  1037. rt_bool_t _ready;
  1038. if (_sta_is_null())
  1039. {
  1040. return RT_FALSE;
  1041. }
  1042. _ready = _sta_mgnt.state & RT_WLAN_STATE_READY ? RT_TRUE : RT_FALSE;
  1043. RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _ready ? "ready" : "not ready");
  1044. return _ready;
  1045. }
  1046. rt_err_t rt_wlan_set_mac(rt_uint8_t mac[6])
  1047. {
  1048. rt_err_t err = RT_EOK;
  1049. if (_sta_is_null())
  1050. {
  1051. return -RT_EIO;
  1052. }
  1053. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
  1054. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1055. MGNT_LOCK();
  1056. err = rt_wlan_dev_set_mac(STA_DEVICE(), mac);
  1057. if (err != RT_EOK)
  1058. {
  1059. RT_WLAN_LOG_E("set sta mac addr fail");
  1060. MGNT_UNLOCK();
  1061. return err;
  1062. }
  1063. MGNT_UNLOCK();
  1064. return err;
  1065. }
  1066. rt_err_t rt_wlan_get_mac(rt_uint8_t mac[6])
  1067. {
  1068. rt_err_t err = RT_EOK;
  1069. if (_sta_is_null())
  1070. {
  1071. return -RT_EIO;
  1072. }
  1073. MGNT_LOCK();
  1074. err = rt_wlan_dev_get_mac(STA_DEVICE(), mac);
  1075. if (err != RT_EOK)
  1076. {
  1077. RT_WLAN_LOG_E("get sta mac addr fail");
  1078. MGNT_UNLOCK();
  1079. return err;
  1080. }
  1081. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x",
  1082. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1083. MGNT_UNLOCK();
  1084. return err;
  1085. }
  1086. rt_err_t rt_wlan_get_info(struct rt_wlan_info *info)
  1087. {
  1088. if (_sta_is_null())
  1089. {
  1090. return -RT_EIO;
  1091. }
  1092. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1093. rt_enter_critical();
  1094. *info = _sta_mgnt.info;
  1095. rt_exit_critical();
  1096. return RT_EOK;
  1097. }
  1098. int rt_wlan_get_rssi(void)
  1099. {
  1100. int rssi = 0;
  1101. if (_sta_is_null())
  1102. {
  1103. return -RT_EIO;
  1104. }
  1105. MGNT_LOCK();
  1106. rssi = rt_wlan_dev_get_rssi(STA_DEVICE());
  1107. RT_WLAN_LOG_D("%s is run rssi:%d", __FUNCTION__, rssi);
  1108. MGNT_UNLOCK();
  1109. return rssi;
  1110. }
  1111. rt_err_t rt_wlan_start_ap(const char *ssid, const char *password)
  1112. {
  1113. rt_err_t err = RT_EOK;
  1114. int ssid_len = 0;
  1115. struct rt_wlan_info info;
  1116. struct rt_wlan_complete_des *complete;
  1117. rt_uint32_t set = 0, recved = 0;
  1118. if (_ap_is_null())
  1119. {
  1120. return -RT_EIO;
  1121. }
  1122. if (ssid == RT_NULL) return -RT_EINVAL;
  1123. rt_memset(&info, 0, sizeof(struct rt_wlan_info));
  1124. RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password);
  1125. if (password)
  1126. {
  1127. info.security = SECURITY_WPA2_AES_PSK;
  1128. }
  1129. ssid_len = rt_strlen(ssid);
  1130. if (ssid_len > RT_WLAN_SSID_MAX_LENGTH)
  1131. {
  1132. RT_WLAN_LOG_E("ssid is to long! len:%d", ssid_len);
  1133. }
  1134. /* copy info */
  1135. rt_memcpy(&info.ssid.val, ssid, ssid_len);
  1136. info.ssid.len = ssid_len;
  1137. info.channel = 6;
  1138. /* Initializing events that need to wait */
  1139. MGNT_LOCK();
  1140. /* create event wait complete */
  1141. complete = rt_wlan_complete_create("start_ap");
  1142. if (complete == RT_NULL)
  1143. {
  1144. MGNT_UNLOCK();
  1145. return -RT_ENOMEM;
  1146. }
  1147. /* start ap */
  1148. err = rt_wlan_start_ap_adv(&info, password);
  1149. if (err != RT_EOK)
  1150. {
  1151. rt_wlan_complete_delete(complete);
  1152. RT_WLAN_LOG_I("start ap failed!");
  1153. MGNT_UNLOCK();
  1154. return err;
  1155. }
  1156. /* Initializing events that need to wait */
  1157. set |= 0x1 << RT_WLAN_DEV_EVT_AP_START;
  1158. set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1159. /* Check whether there is a waiting event */
  1160. rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
  1161. rt_wlan_complete_delete(complete);
  1162. /* check event */
  1163. set = 0x1 << RT_WLAN_DEV_EVT_AP_START;
  1164. if (!(recved & set))
  1165. {
  1166. RT_WLAN_LOG_I("start ap failed!");
  1167. MGNT_UNLOCK();
  1168. return -RT_ERROR;
  1169. }
  1170. RT_WLAN_LOG_I("start ap successs!");
  1171. MGNT_UNLOCK();
  1172. return err;
  1173. }
  1174. rt_err_t rt_wlan_start_ap_adv(struct rt_wlan_info *info, const char *password)
  1175. {
  1176. rt_err_t err = RT_EOK;
  1177. int password_len = 0;
  1178. if (_ap_is_null())
  1179. {
  1180. return -RT_EIO;
  1181. }
  1182. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1183. password_len = rt_strlen(password);
  1184. if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH)
  1185. {
  1186. RT_WLAN_LOG_E("key is to long! len:%d", password_len);
  1187. return -RT_EINVAL;
  1188. }
  1189. /* is start up ? */
  1190. MGNT_LOCK();
  1191. if (rt_wlan_ap_is_active())
  1192. {
  1193. if ((_ap_mgnt.info.ssid.len == info->ssid.len) &&
  1194. (_ap_mgnt.info.security == info->security) &&
  1195. (_ap_mgnt.info.channel == info->channel) &&
  1196. (_ap_mgnt.info.hidden == info->hidden) &&
  1197. (_ap_mgnt.key.len == password_len) &&
  1198. (rt_memcmp(&_ap_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) &&
  1199. (rt_memcmp(&_ap_mgnt.key.val[0], password, password_len)))
  1200. {
  1201. RT_WLAN_LOG_D("wifi Already Start");
  1202. MGNT_UNLOCK();
  1203. return RT_EOK;
  1204. }
  1205. }
  1206. err = rt_wlan_dev_ap_start(AP_DEVICE(), info, password, password_len);
  1207. if (err != RT_EOK)
  1208. {
  1209. MGNT_UNLOCK();
  1210. return err;
  1211. }
  1212. rt_memcpy(&_ap_mgnt.info, info, sizeof(struct rt_wlan_info));
  1213. rt_memcpy(&_ap_mgnt.key.val, password, password_len);
  1214. _ap_mgnt.key.len = password_len;
  1215. MGNT_UNLOCK();
  1216. return err;
  1217. }
  1218. int rt_wlan_ap_is_active(void)
  1219. {
  1220. int _active = 0;
  1221. if (_ap_is_null())
  1222. {
  1223. return 0;
  1224. }
  1225. _active = _ap_mgnt.state & RT_WLAN_STATE_ACTIVE ? 1 : 0;
  1226. RT_WLAN_LOG_D("%s is run active:%s", __FUNCTION__, _active ? "Active" : "Inactive");
  1227. return _active;
  1228. }
  1229. rt_err_t rt_wlan_ap_stop(void)
  1230. {
  1231. rt_err_t err = RT_EOK;
  1232. struct rt_wlan_complete_des *complete;
  1233. rt_uint32_t set = 0, recved = 0;
  1234. if (_ap_is_null())
  1235. {
  1236. return -RT_EIO;
  1237. }
  1238. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1239. MGNT_LOCK();
  1240. /* create event wait complete */
  1241. complete = rt_wlan_complete_create("stop_ap");
  1242. if (complete == RT_NULL)
  1243. {
  1244. MGNT_UNLOCK();
  1245. return -RT_ENOMEM;
  1246. }
  1247. err = rt_wlan_dev_ap_stop(AP_DEVICE());
  1248. if (err != RT_EOK)
  1249. {
  1250. RT_WLAN_LOG_E("ap stop fail");
  1251. rt_wlan_complete_delete(complete);
  1252. MGNT_UNLOCK();
  1253. return err;
  1254. }
  1255. /* Initializing events that need to wait */
  1256. set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1257. /* Check whether there is a waiting event */
  1258. rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved);
  1259. rt_wlan_complete_delete(complete);
  1260. /* check event */
  1261. set = 0x1 << RT_WLAN_DEV_EVT_AP_STOP;
  1262. if (!(recved & set))
  1263. {
  1264. RT_WLAN_LOG_I("ap stop failed!");
  1265. MGNT_UNLOCK();
  1266. return -RT_ERROR;
  1267. }
  1268. RT_WLAN_LOG_I("ap stop success!");
  1269. MGNT_UNLOCK();
  1270. return err;
  1271. }
  1272. rt_err_t rt_wlan_ap_get_info(struct rt_wlan_info *info)
  1273. {
  1274. if (_ap_is_null())
  1275. {
  1276. return -RT_EIO;
  1277. }
  1278. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1279. *info = _ap_mgnt.info;
  1280. return RT_EOK;
  1281. }
  1282. /* get sta number */
  1283. int rt_wlan_ap_get_sta_num(void)
  1284. {
  1285. int sta_num = 0;
  1286. STAINFO_LOCK();
  1287. sta_num = sta_info.num;
  1288. STAINFO_UNLOCK();
  1289. RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, sta_num);
  1290. return sta_num;
  1291. }
  1292. /* get sta info */
  1293. int rt_wlan_ap_get_sta_info(struct rt_wlan_info *info, int num)
  1294. {
  1295. int sta_num = 0, i = 0;
  1296. struct rt_wlan_sta_list *sta_list;
  1297. STAINFO_LOCK();
  1298. /* sta_num = min(sta_info.num, num) */
  1299. sta_num = sta_info.num > num ? num : sta_info.num;
  1300. for (sta_list = sta_info.node; sta_list != RT_NULL && i < sta_num; sta_list = sta_list->next)
  1301. {
  1302. info[i] = sta_list->info;
  1303. i ++;
  1304. }
  1305. STAINFO_UNLOCK();
  1306. RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, i);
  1307. return i;
  1308. }
  1309. /* deauth sta */
  1310. rt_err_t rt_wlan_ap_deauth_sta(rt_uint8_t *mac)
  1311. {
  1312. rt_err_t err = RT_EOK;
  1313. struct rt_wlan_sta_list *sta_list;
  1314. rt_bool_t find_flag = RT_FALSE;
  1315. if (_ap_is_null())
  1316. {
  1317. return -RT_EIO;
  1318. }
  1319. RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x:%d",
  1320. __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  1321. if (mac == RT_NULL)
  1322. {
  1323. RT_WLAN_LOG_E("mac addr is null");
  1324. return -RT_EINVAL;
  1325. }
  1326. MGNT_LOCK();
  1327. if (sta_info.node == RT_NULL || sta_info.num == 0)
  1328. {
  1329. RT_WLAN_LOG_E("No AP");
  1330. MGNT_UNLOCK();
  1331. return -RT_ERROR;
  1332. }
  1333. STAINFO_LOCK();
  1334. /* Search for MAC address from sta list */
  1335. for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_list->next)
  1336. {
  1337. if (rt_memcmp(&sta_list->info.bssid[0], &mac[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)
  1338. {
  1339. find_flag = RT_TRUE;
  1340. break;
  1341. }
  1342. }
  1343. STAINFO_UNLOCK();
  1344. /* No MAC address was found. return */
  1345. if (find_flag != RT_TRUE)
  1346. {
  1347. RT_WLAN_LOG_E("Not find mac addr");
  1348. MGNT_UNLOCK();
  1349. return -RT_ERROR;
  1350. }
  1351. /* Kill STA */
  1352. err = rt_wlan_dev_ap_deauth(AP_DEVICE(), mac);
  1353. if (err != RT_NULL)
  1354. {
  1355. RT_WLAN_LOG_E("deauth sta failed");
  1356. MGNT_UNLOCK();
  1357. return err;
  1358. }
  1359. MGNT_UNLOCK();
  1360. return err;
  1361. }
  1362. rt_err_t rt_wlan_ap_set_country(rt_country_code_t country_code)
  1363. {
  1364. rt_err_t err = RT_EOK;
  1365. if (_ap_is_null())
  1366. {
  1367. return -RT_EIO;
  1368. }
  1369. RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
  1370. MGNT_LOCK();
  1371. err = rt_wlan_dev_set_country(AP_DEVICE(), country_code);
  1372. MGNT_UNLOCK();
  1373. return err;
  1374. }
  1375. rt_country_code_t rt_wlan_ap_get_country(void)
  1376. {
  1377. rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
  1378. if (_ap_is_null())
  1379. {
  1380. return country_code;
  1381. }
  1382. MGNT_LOCK();
  1383. country_code = rt_wlan_dev_get_country(AP_DEVICE());
  1384. RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code);
  1385. MGNT_UNLOCK();
  1386. return country_code;
  1387. }
  1388. void rt_wlan_config_autoreconnect(rt_bool_t enable)
  1389. {
  1390. RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
  1391. MGNT_LOCK();
  1392. if (enable)
  1393. {
  1394. _sta_mgnt.flags |= RT_WLAN_STATE_AUTOEN;
  1395. }
  1396. else
  1397. {
  1398. _sta_mgnt.flags &= ~RT_WLAN_STATE_AUTOEN;
  1399. }
  1400. MGNT_UNLOCK();
  1401. }
  1402. rt_bool_t rt_wlan_get_autoreconnect_mode(void)
  1403. {
  1404. rt_bool_t enable = 0;
  1405. enable = _sta_mgnt.flags & RT_WLAN_STATE_AUTOEN ? 1 : 0;
  1406. RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable);
  1407. return enable;
  1408. }
  1409. /* Call the underlying scan function, which is asynchronous.
  1410. The hotspots scanned are returned by callbacks */
  1411. rt_err_t rt_wlan_scan(void)
  1412. {
  1413. rt_err_t err = RT_EOK;
  1414. if (_sta_is_null())
  1415. {
  1416. return -RT_EIO;
  1417. }
  1418. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1419. MGNT_LOCK();
  1420. err = rt_wlan_dev_scan(STA_DEVICE(), RT_NULL);
  1421. MGNT_UNLOCK();
  1422. return err;
  1423. }
  1424. struct rt_wlan_scan_result *rt_wlan_scan_sync(void)
  1425. {
  1426. struct rt_wlan_scan_result *result;
  1427. /* Execute synchronous scan function */
  1428. MGNT_LOCK();
  1429. result = rt_wlan_scan_with_info(RT_NULL);
  1430. MGNT_UNLOCK();
  1431. return result;
  1432. }
  1433. struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info)
  1434. {
  1435. rt_err_t err = RT_EOK;
  1436. struct rt_wlan_complete_des *complete;
  1437. rt_uint32_t set = 0, recved = 0;
  1438. if (_sta_is_null())
  1439. {
  1440. return RT_NULL;
  1441. }
  1442. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1443. if (info != RT_NULL && info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)
  1444. {
  1445. RT_WLAN_LOG_E("ssid is to long!");
  1446. return RT_NULL;
  1447. }
  1448. /* Create an event that needs to wait. */
  1449. MGNT_LOCK();
  1450. complete = rt_wlan_complete_create("scan");
  1451. if (complete == RT_NULL)
  1452. {
  1453. MGNT_UNLOCK();
  1454. return &scan_result;
  1455. }
  1456. /* run scna */
  1457. err = rt_wlan_dev_scan(STA_DEVICE(), info);
  1458. if (err != RT_EOK)
  1459. {
  1460. rt_wlan_complete_delete(complete);
  1461. RT_WLAN_LOG_E("scna sync fail");
  1462. MGNT_UNLOCK();
  1463. return RT_NULL;
  1464. }
  1465. /* Initializing events that need to wait */
  1466. set |= 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1467. /* Check whether there is a waiting event */
  1468. rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved);
  1469. rt_wlan_complete_delete(complete);
  1470. /* check event */
  1471. set = 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE;
  1472. if (!(recved & set))
  1473. {
  1474. RT_WLAN_LOG_E("scna wait timeout!");
  1475. MGNT_UNLOCK();
  1476. return &scan_result;
  1477. }
  1478. MGNT_UNLOCK();
  1479. return &scan_result;
  1480. }
  1481. int rt_wlan_scan_get_info_num(void)
  1482. {
  1483. int num = 0;
  1484. num = scan_result.num;
  1485. RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, num);
  1486. return num;
  1487. }
  1488. int rt_wlan_scan_get_info(struct rt_wlan_info *info, int num)
  1489. {
  1490. int _num = 0;
  1491. SRESULT_LOCK();
  1492. if (scan_result.num && num > 0)
  1493. {
  1494. _num = scan_result.num > num ? num : scan_result.num;
  1495. rt_memcpy(info, scan_result.info, _num * sizeof(struct rt_wlan_info));
  1496. }
  1497. SRESULT_UNLOCK();
  1498. return _num;
  1499. }
  1500. struct rt_wlan_scan_result *rt_wlan_scan_get_result(void)
  1501. {
  1502. return &scan_result;
  1503. }
  1504. void rt_wlan_scan_result_clean(void)
  1505. {
  1506. MGNT_LOCK();
  1507. SRESULT_LOCK();
  1508. /* If there is data */
  1509. if (scan_result.num)
  1510. {
  1511. scan_result.num = 0;
  1512. rt_free(scan_result.info);
  1513. scan_result.info = RT_NULL;
  1514. }
  1515. SRESULT_UNLOCK();
  1516. MGNT_UNLOCK();
  1517. }
  1518. int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_info, int num)
  1519. {
  1520. int i = 0, count = 0;
  1521. struct rt_wlan_info *scan_info;
  1522. rt_bool_t is_equ = 1;
  1523. rt_uint8_t bssid_zero[RT_WLAN_BSSID_MAX_LENGTH] = { 0 };
  1524. if ((out_info == RT_NULL) || (info == RT_NULL) || (num <= 0))
  1525. {
  1526. return 0;
  1527. }
  1528. SRESULT_LOCK();
  1529. /* Traversing the cache to find a qualified hot spot information */
  1530. for (i = 0; (i < scan_result.num) && (count < num); i++)
  1531. {
  1532. scan_info = &scan_result.info[i];
  1533. if (is_equ && (info->security != SECURITY_UNKNOWN))
  1534. {
  1535. is_equ &= info->security == scan_info->security;
  1536. }
  1537. if (is_equ && ((info->ssid.len > 0) && (info->ssid.len == scan_info->ssid.len)))
  1538. {
  1539. is_equ &= rt_memcmp(&info->ssid.val[0], &scan_info->ssid.val[0], scan_info->ssid.len) == 0;
  1540. }
  1541. if (is_equ && (rt_memcmp(&info->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH)))
  1542. {
  1543. is_equ &= rt_memcmp(&info->bssid[0], &scan_info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0;
  1544. }
  1545. if (is_equ && info->datarate)
  1546. {
  1547. is_equ &= info->datarate == scan_info->datarate;
  1548. }
  1549. if (is_equ && (info->channel >= 0))
  1550. {
  1551. is_equ &= info->channel == scan_info->channel;
  1552. }
  1553. if (is_equ && (info->rssi < 0))
  1554. {
  1555. is_equ &= info->rssi == scan_info->rssi;
  1556. }
  1557. /* Determine whether to find */
  1558. if (is_equ)
  1559. {
  1560. rt_memcpy(&out_info[count], scan_info, sizeof(struct rt_wlan_info));
  1561. count ++;
  1562. }
  1563. }
  1564. SRESULT_UNLOCK();
  1565. return count;
  1566. }
  1567. rt_err_t rt_wlan_set_powersave(int level)
  1568. {
  1569. rt_err_t err = RT_EOK;
  1570. if (_sta_is_null())
  1571. {
  1572. return -RT_EIO;
  1573. }
  1574. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1575. MGNT_LOCK();
  1576. err = rt_wlan_dev_set_powersave(STA_DEVICE(), level);
  1577. MGNT_UNLOCK();
  1578. return err;
  1579. }
  1580. int rt_wlan_get_powersave(void)
  1581. {
  1582. int level;
  1583. if (_sta_is_null())
  1584. {
  1585. return -1;
  1586. }
  1587. RT_WLAN_LOG_D("%s is run", __FUNCTION__);
  1588. MGNT_LOCK();
  1589. level = rt_wlan_dev_get_powersave(STA_DEVICE());
  1590. MGNT_UNLOCK();
  1591. return level;
  1592. }
  1593. rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter)
  1594. {
  1595. rt_base_t level;
  1596. if (event >= RT_WLAN_EVT_MAX)
  1597. {
  1598. return RT_EINVAL;
  1599. }
  1600. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1601. MGNT_UNLOCK();
  1602. /* Registering Callbacks */
  1603. level = rt_hw_interrupt_disable();
  1604. event_tab[event].handler = handler;
  1605. event_tab[event].parameter = parameter;
  1606. rt_hw_interrupt_enable(level);
  1607. MGNT_UNLOCK();
  1608. return RT_EOK;
  1609. }
  1610. rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event)
  1611. {
  1612. rt_base_t level;
  1613. if (event >= RT_WLAN_EVT_MAX)
  1614. {
  1615. return RT_EINVAL;
  1616. }
  1617. RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event);
  1618. MGNT_LOCK();
  1619. /* unregister*/
  1620. level = rt_hw_interrupt_disable();
  1621. event_tab[event].handler = RT_NULL;
  1622. event_tab[event].parameter = RT_NULL;
  1623. rt_hw_interrupt_enable(level);
  1624. MGNT_UNLOCK();
  1625. return RT_EOK;
  1626. }
  1627. void rt_wlan_mgnt_lock(void)
  1628. {
  1629. MGNT_LOCK();
  1630. }
  1631. void rt_wlan_mgnt_unlock(void)
  1632. {
  1633. MGNT_UNLOCK();
  1634. }
  1635. int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff)
  1636. {
  1637. rt_base_t level;
  1638. void *user_parameter;
  1639. rt_wlan_event_handler handler = RT_NULL;
  1640. if ((wlan == RT_NULL) || (_sta_mgnt.device != wlan) ||
  1641. (!(_sta_mgnt.state & RT_WLAN_STATE_CONNECT)))
  1642. {
  1643. return -1;
  1644. }
  1645. if (_sta_mgnt.state & RT_WLAN_STATE_READY)
  1646. {
  1647. return 0;
  1648. }
  1649. level = rt_hw_interrupt_disable();
  1650. _sta_mgnt.state |= RT_WLAN_STATE_READY;
  1651. handler = event_tab[RT_WLAN_EVT_READY].handler;
  1652. user_parameter = event_tab[RT_WLAN_EVT_READY].parameter;
  1653. rt_hw_interrupt_enable(level);
  1654. if (handler)
  1655. {
  1656. handler(RT_WLAN_EVT_READY, buff, user_parameter);
  1657. }
  1658. return 0;
  1659. }
  1660. int rt_wlan_init(void)
  1661. {
  1662. static rt_int8_t _init_flag = 0;
  1663. /* Execute only once */
  1664. if (_init_flag == 0)
  1665. {
  1666. rt_memset(&_sta_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1667. rt_memset(&_ap_mgnt, 0, sizeof(struct rt_wlan_mgnt_des));
  1668. rt_memset(&scan_result, 0, sizeof(struct rt_wlan_scan_result));
  1669. rt_memset(&sta_info, 0, sizeof(struct rt_wlan_sta_des));
  1670. rt_mutex_init(&mgnt_mutex, "mgnt", RT_IPC_FLAG_FIFO);
  1671. rt_mutex_init(&scan_result_mutex, "scan", RT_IPC_FLAG_FIFO);
  1672. rt_mutex_init(&sta_info_mutex, "sta", RT_IPC_FLAG_FIFO);
  1673. rt_mutex_init(&complete_mutex, "complete", RT_IPC_FLAG_FIFO);
  1674. 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);
  1675. rt_timer_start(&reconnect_time);
  1676. _init_flag = 1;
  1677. }
  1678. return 0;
  1679. }
  1680. INIT_PREV_EXPORT(rt_wlan_init);