wlan_mgnt.c 54 KB

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