wlan_mgnt.c 49 KB

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