wlan_mgnt.c 54 KB

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