wlan_mgnt.c 54 KB

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