dev_wlan_cmd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-13 tyx the first version
  9. * 2024-03-24 Evlers fixed a duplicate issue with the wifi scan command
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <dev_wlan_mgnt.h>
  14. #include <dev_wlan_cfg.h>
  15. #include <dev_wlan_prot.h>
  16. #define DBG_TAG "WLAN.cmd"
  17. #ifdef RT_WLAN_MGNT_DEBUG
  18. #define DBG_LVL DBG_LOG
  19. #else
  20. #define DBG_LVL DBG_INFO
  21. #endif /* RT_WLAN_MGNT_DEBUG */
  22. #include <rtdbg.h>
  23. static struct rt_wlan_scan_result scan_result;
  24. static struct rt_wlan_info *scan_filter = RT_NULL;
  25. #if defined(RT_WLAN_MANAGE_ENABLE) && defined(RT_WLAN_MSH_CMD_ENABLE)
  26. struct wifi_cmd_des
  27. {
  28. const char *cmd;
  29. int (*fun)(int argc, char *argv[]);
  30. };
  31. static int wifi_help(int argc, char *argv[]);
  32. static int wifi_scan(int argc, char *argv[]);
  33. static int wifi_status(int argc, char *argv[]);
  34. static int wifi_join(int argc, char *argv[]);
  35. static int wifi_ap(int argc, char *argv[]);
  36. static int wifi_list_sta(int argc, char *argv[]);
  37. static int wifi_disconnect(int argc, char *argv[]);
  38. static int wifi_ap_stop(int argc, char *argv[]);
  39. #ifdef RT_WLAN_CMD_DEBUG
  40. /* just for debug */
  41. static int wifi_debug(int argc, char *argv[]);
  42. static int wifi_debug_save_cfg(int argc, char *argv[]);
  43. static int wifi_debug_dump_cfg(int argc, char *argv[]);
  44. static int wifi_debug_clear_cfg(int argc, char *argv[]);
  45. static int wifi_debug_dump_prot(int argc, char *argv[]);
  46. static int wifi_debug_set_mode(int argc, char *argv[]);
  47. static int wifi_debug_set_prot(int argc, char *argv[]);
  48. static int wifi_debug_set_autoconnect(int argc, char *argv[]);
  49. #endif
  50. /* cmd table */
  51. static const struct wifi_cmd_des cmd_tab[] = {
  52. { "scan", wifi_scan },
  53. { "help", wifi_help },
  54. { "status", wifi_status },
  55. { "join", wifi_join },
  56. { "ap", wifi_ap },
  57. { "list_sta", wifi_list_sta },
  58. { "disc", wifi_disconnect },
  59. { "ap_stop", wifi_ap_stop },
  60. { "smartconfig", RT_NULL },
  61. #ifdef RT_WLAN_CMD_DEBUG
  62. { "-d", wifi_debug },
  63. #endif
  64. };
  65. #ifdef RT_WLAN_CMD_DEBUG
  66. /* debug cmd table */
  67. static const struct wifi_cmd_des debug_tab[] = {
  68. { "save_cfg", wifi_debug_save_cfg },
  69. { "dump_cfg", wifi_debug_dump_cfg },
  70. { "clear_cfg", wifi_debug_clear_cfg },
  71. #ifdef RT_WLAN_PROT_ENABLE
  72. { "dump_prot", wifi_debug_dump_prot },
  73. { "mode", wifi_debug_set_mode },
  74. { "prot", wifi_debug_set_prot },
  75. #else
  76. { "mode", wifi_debug_set_mode },
  77. #endif /* RT_WLAN_PROT_ENABLE */
  78. { "auto", wifi_debug_set_autoconnect },
  79. };
  80. #endif
  81. static int wifi_help(int argc, char *argv[])
  82. {
  83. rt_kprintf("wifi\n");
  84. rt_kprintf("wifi help\n");
  85. rt_kprintf("wifi scan [SSID]\n");
  86. rt_kprintf("wifi join [SSID] [PASSWORD]\n");
  87. rt_kprintf("wifi ap SSID [PASSWORD]\n");
  88. rt_kprintf("wifi disc\n");
  89. rt_kprintf("wifi ap_stop\n");
  90. rt_kprintf("wifi status\n");
  91. rt_kprintf("wifi smartconfig\n");
  92. #ifdef RT_WLAN_CMD_DEBUG
  93. rt_kprintf("wifi -d debug command\n");
  94. #endif
  95. return 0;
  96. }
  97. static int wifi_status(int argc, char *argv[])
  98. {
  99. int rssi;
  100. struct rt_wlan_info info;
  101. if (argc > 2)
  102. return -1;
  103. if (rt_wlan_is_connected() == 1)
  104. {
  105. rssi = rt_wlan_get_rssi();
  106. rt_wlan_get_info(&info);
  107. rt_kprintf("Wi-Fi STA Info\n");
  108. rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]);
  109. rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0],
  110. info.bssid[1],
  111. info.bssid[2],
  112. info.bssid[3],
  113. info.bssid[4],
  114. info.bssid[5]);
  115. rt_kprintf("Channel: %d\n", info.channel);
  116. rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000);
  117. rt_kprintf("RSSI: %d\n", rssi);
  118. }
  119. else
  120. {
  121. rt_kprintf("wifi disconnected!\n");
  122. }
  123. if (rt_wlan_ap_is_active() == 1)
  124. {
  125. rt_wlan_ap_get_info(&info);
  126. rt_kprintf("Wi-Fi AP Info\n");
  127. rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]);
  128. rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0],
  129. info.bssid[1],
  130. info.bssid[2],
  131. info.bssid[3],
  132. info.bssid[4],
  133. info.bssid[5]);
  134. rt_kprintf("Channel: %d\n", info.channel);
  135. rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000);
  136. rt_kprintf("hidden: %s\n", info.hidden ? "Enable" : "Disable");
  137. }
  138. else
  139. {
  140. rt_kprintf("wifi ap not start!\n");
  141. }
  142. rt_kprintf("Auto Connect status:%s!\n", (rt_wlan_get_autoreconnect_mode() ? "Enable" : "Disable"));
  143. return 0;
  144. }
  145. static rt_bool_t wifi_info_isequ(struct rt_wlan_info *info1, struct rt_wlan_info *info2)
  146. {
  147. rt_bool_t is_equ = 1;
  148. rt_uint8_t bssid_zero[RT_WLAN_BSSID_MAX_LENGTH] = { 0 };
  149. if (is_equ && (info1->security != SECURITY_UNKNOWN) && (info2->security != SECURITY_UNKNOWN))
  150. {
  151. is_equ &= info2->security == info1->security;
  152. }
  153. if (is_equ && ((info1->ssid.len > 0) && (info2->ssid.len > 0)))
  154. {
  155. is_equ &= info1->ssid.len == info2->ssid.len;
  156. is_equ &= rt_memcmp(&info2->ssid.val[0], &info1->ssid.val[0], info1->ssid.len) == 0;
  157. }
  158. if (is_equ && (rt_memcmp(&info1->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH)) &&
  159. (rt_memcmp(&info2->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH)))
  160. {
  161. is_equ &= rt_memcmp(&info1->bssid[0], &info2->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0;
  162. }
  163. if (is_equ && info1->datarate && info2->datarate)
  164. {
  165. is_equ &= info1->datarate == info2->datarate;
  166. }
  167. if (is_equ && (info1->channel >= 0) && (info2->channel >= 0))
  168. {
  169. is_equ &= info1->channel == info2->channel;
  170. }
  171. if (is_equ && (info1->rssi < 0) && (info2->rssi < 0))
  172. {
  173. is_equ &= info1->rssi == info2->rssi;
  174. }
  175. return is_equ;
  176. }
  177. static rt_err_t wifi_scan_result_cache(struct rt_wlan_info *info)
  178. {
  179. struct rt_wlan_info *ptable;
  180. rt_err_t err = RT_EOK;
  181. int i, insert = -1;
  182. rt_base_t level;
  183. if ((info == RT_NULL) || (info->ssid.len == 0))
  184. return -RT_EINVAL;
  185. LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len,
  186. info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]);
  187. /* scanning result filtering */
  188. level = rt_hw_interrupt_disable();
  189. if (scan_filter)
  190. {
  191. struct rt_wlan_info _tmp_info = *scan_filter;
  192. rt_hw_interrupt_enable(level);
  193. if (wifi_info_isequ(&_tmp_info, info) != RT_TRUE)
  194. {
  195. return RT_EOK;
  196. }
  197. }
  198. else
  199. {
  200. rt_hw_interrupt_enable(level);
  201. }
  202. /* de-duplicatio */
  203. for (i = 0; i < scan_result.num; i++)
  204. {
  205. if ((info->ssid.len == scan_result.info[i].ssid.len) &&
  206. (rt_memcmp(&info->bssid[0], &scan_result.info[i].bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0))
  207. {
  208. return RT_EOK;
  209. }
  210. #ifdef RT_WLAN_SCAN_SORT
  211. if (insert >= 0)
  212. {
  213. continue;
  214. }
  215. /* Signal intensity comparison */
  216. if ((info->rssi < 0) && (scan_result.info[i].rssi < 0))
  217. {
  218. if (info->rssi > scan_result.info[i].rssi)
  219. {
  220. insert = i;
  221. continue;
  222. }
  223. else if (info->rssi < scan_result.info[i].rssi)
  224. {
  225. continue;
  226. }
  227. }
  228. /* Channel comparison */
  229. if (info->channel < scan_result.info[i].channel)
  230. {
  231. insert = i;
  232. continue;
  233. }
  234. else if (info->channel > scan_result.info[i].channel)
  235. {
  236. continue;
  237. }
  238. /* data rate comparison */
  239. if ((info->datarate > scan_result.info[i].datarate))
  240. {
  241. insert = i;
  242. continue;
  243. }
  244. else if (info->datarate < scan_result.info[i].datarate)
  245. {
  246. continue;
  247. }
  248. #endif
  249. }
  250. /* Insert the end */
  251. if (insert == -1)
  252. insert = scan_result.num;
  253. if (scan_result.num >= RT_WLAN_SCAN_CACHE_NUM)
  254. return RT_EOK;
  255. /* malloc memory */
  256. ptable = rt_malloc(sizeof(struct rt_wlan_info) * (scan_result.num + 1));
  257. if (ptable == RT_NULL)
  258. {
  259. LOG_E("wlan info malloc failed!");
  260. return -RT_ENOMEM;
  261. }
  262. scan_result.num++;
  263. /* copy info */
  264. for (i = 0; i < scan_result.num; i++)
  265. {
  266. if (i < insert)
  267. {
  268. ptable[i] = scan_result.info[i];
  269. }
  270. else if (i > insert)
  271. {
  272. ptable[i] = scan_result.info[i - 1];
  273. }
  274. else if (i == insert)
  275. {
  276. ptable[i] = *info;
  277. }
  278. }
  279. rt_free(scan_result.info);
  280. scan_result.info = ptable;
  281. return err;
  282. }
  283. static void wifi_scan_result_clean(void)
  284. {
  285. /* If there is data */
  286. if (scan_result.num)
  287. {
  288. scan_result.num = 0;
  289. rt_free(scan_result.info);
  290. scan_result.info = RT_NULL;
  291. }
  292. }
  293. static void print_ap_info(struct rt_wlan_info *info, int index)
  294. {
  295. char *security;
  296. if (index == 0)
  297. {
  298. rt_kprintf(" SSID MAC security rssi chn Mbps\n");
  299. rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n");
  300. }
  301. {
  302. rt_kprintf("%-32.32s", &(info->ssid.val[0]));
  303. rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ",
  304. info->bssid[0],
  305. info->bssid[1],
  306. info->bssid[2],
  307. info->bssid[3],
  308. info->bssid[4],
  309. info->bssid[5]);
  310. switch (info->security)
  311. {
  312. case SECURITY_OPEN:
  313. security = "OPEN";
  314. break;
  315. case SECURITY_WEP_PSK:
  316. security = "WEP_PSK";
  317. break;
  318. case SECURITY_WEP_SHARED:
  319. security = "WEP_SHARED";
  320. break;
  321. case SECURITY_WPA_TKIP_PSK:
  322. security = "WPA_TKIP_PSK";
  323. break;
  324. case SECURITY_WPA_AES_PSK:
  325. security = "WPA_AES_PSK";
  326. break;
  327. case SECURITY_WPA2_AES_PSK:
  328. security = "WPA2_AES_PSK";
  329. break;
  330. case SECURITY_WPA2_TKIP_PSK:
  331. security = "WPA2_TKIP_PSK";
  332. break;
  333. case SECURITY_WPA2_MIXED_PSK:
  334. security = "WPA2_MIXED_PSK";
  335. break;
  336. case SECURITY_WPS_OPEN:
  337. security = "WPS_OPEN";
  338. break;
  339. case SECURITY_WPS_SECURE:
  340. security = "WPS_SECURE";
  341. break;
  342. default:
  343. security = "UNKNOWN";
  344. break;
  345. }
  346. rt_kprintf("%-14.14s ", security);
  347. rt_kprintf("%-4d ", info->rssi);
  348. rt_kprintf("%3d ", info->channel);
  349. rt_kprintf("%4d\n", info->datarate / 1000000);
  350. }
  351. }
  352. static void user_ap_info_callback(int event, struct rt_wlan_buff *buff, void *parameter)
  353. {
  354. struct rt_wlan_info *info = RT_NULL;
  355. int index = 0;
  356. int ret = RT_EOK;
  357. rt_uint32_t last_num = scan_result.num;
  358. RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT);
  359. RT_ASSERT(buff != RT_NULL);
  360. RT_ASSERT(parameter != RT_NULL);
  361. info = (struct rt_wlan_info *)buff->data;
  362. index = *((int *)(parameter));
  363. ret = wifi_scan_result_cache(info);
  364. if (ret == RT_EOK)
  365. {
  366. if (scan_filter == RT_NULL ||
  367. (scan_filter != RT_NULL &&
  368. scan_filter->ssid.len == info->ssid.len &&
  369. rt_memcmp(&scan_filter->ssid.val[0], &info->ssid.val[0], scan_filter->ssid.len) == 0))
  370. {
  371. /*Check whether a new ap is added*/
  372. if (last_num < scan_result.num)
  373. {
  374. /*Print the info*/
  375. print_ap_info(info, index);
  376. }
  377. index++;
  378. *((int *)(parameter)) = index;
  379. }
  380. }
  381. }
  382. static int wifi_scan(int argc, char *argv[])
  383. {
  384. struct rt_wlan_info *info = RT_NULL;
  385. struct rt_wlan_info filter;
  386. int ret = 0;
  387. int i = 0;
  388. if (argc > 3)
  389. return -1;
  390. if (argc == 3)
  391. {
  392. INVALID_INFO(&filter);
  393. SSID_SET(&filter, argv[2]);
  394. info = &filter;
  395. }
  396. ret = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT, user_ap_info_callback, &i);
  397. if (ret != RT_EOK)
  398. {
  399. LOG_E("Scan register user callback error:%d!\n", ret);
  400. return 0;
  401. }
  402. if (info)
  403. {
  404. scan_filter = info;
  405. }
  406. /*Todo: what can i do for it return val */
  407. ret = rt_wlan_scan_with_info(info);
  408. if (ret != RT_EOK)
  409. {
  410. LOG_E("Scan with info error:%d!\n", ret);
  411. }
  412. /* clean scan result */
  413. wifi_scan_result_clean();
  414. if (info)
  415. {
  416. scan_filter = RT_NULL;
  417. }
  418. return 0;
  419. }
  420. static int wifi_join(int argc, char *argv[])
  421. {
  422. const char *ssid = RT_NULL;
  423. const char *key = RT_NULL;
  424. struct rt_wlan_cfg_info cfg_info;
  425. rt_memset(&cfg_info, 0, sizeof(cfg_info));
  426. if (argc == 2)
  427. {
  428. #ifdef RT_WLAN_CFG_ENABLE
  429. /* get info to connect */
  430. if (rt_wlan_cfg_read_index(&cfg_info, 0) == 1)
  431. {
  432. ssid = (char *)(&cfg_info.info.ssid.val[0]);
  433. if (cfg_info.key.len)
  434. key = (char *)(&cfg_info.key.val[0]);
  435. }
  436. else
  437. #endif
  438. {
  439. rt_kprintf("not find connect info\n");
  440. }
  441. }
  442. else if (argc == 3)
  443. {
  444. /* ssid */
  445. ssid = argv[2];
  446. }
  447. else if (argc == 4)
  448. {
  449. ssid = argv[2];
  450. /* password */
  451. key = argv[3];
  452. }
  453. else
  454. {
  455. return -1;
  456. }
  457. rt_wlan_connect(ssid, key);
  458. return 0;
  459. }
  460. static int wifi_ap(int argc, char *argv[])
  461. {
  462. const char *ssid = RT_NULL;
  463. const char *key = RT_NULL;
  464. if (argc == 3)
  465. {
  466. ssid = argv[2];
  467. }
  468. else if (argc == 4)
  469. {
  470. ssid = argv[2];
  471. key = argv[3];
  472. }
  473. else
  474. {
  475. return -1;
  476. }
  477. rt_wlan_start_ap(ssid, key);
  478. return 0;
  479. }
  480. static int wifi_list_sta(int argc, char *argv[])
  481. {
  482. struct rt_wlan_info *sta_info;
  483. int num, i;
  484. if (argc > 2)
  485. return -1;
  486. num = rt_wlan_ap_get_sta_num();
  487. sta_info = rt_malloc(sizeof(struct rt_wlan_info) * num);
  488. if (sta_info == RT_NULL)
  489. {
  490. rt_kprintf("num:%d\n", num);
  491. return 0;
  492. }
  493. rt_wlan_ap_get_sta_info(sta_info, num);
  494. rt_kprintf("num:%d\n", num);
  495. for (i = 0; i < num; i++)
  496. {
  497. rt_kprintf("sta mac %02x:%02x:%02x:%02x:%02x:%02x\n",
  498. sta_info[i].bssid[0], sta_info[i].bssid[1], sta_info[i].bssid[2],
  499. sta_info[i].bssid[3], sta_info[i].bssid[4], sta_info[i].bssid[5]);
  500. }
  501. rt_free(sta_info);
  502. return 0;
  503. }
  504. static int wifi_disconnect(int argc, char *argv[])
  505. {
  506. if (argc != 2)
  507. {
  508. return -1;
  509. }
  510. rt_wlan_disconnect();
  511. return 0;
  512. }
  513. static int wifi_ap_stop(int argc, char *argv[])
  514. {
  515. if (argc != 2)
  516. {
  517. return -1;
  518. }
  519. rt_wlan_ap_stop();
  520. return 0;
  521. }
  522. #ifdef RT_WLAN_CMD_DEBUG
  523. /* just for debug */
  524. static int wifi_debug_help(int argc, char *argv[])
  525. {
  526. rt_kprintf("save_cfg ssid [password]\n");
  527. rt_kprintf("dump_cfg\n");
  528. rt_kprintf("clear_cfg\n");
  529. rt_kprintf("dump_prot\n");
  530. rt_kprintf("mode sta/ap dev_name\n");
  531. rt_kprintf("prot lwip dev_name\n");
  532. rt_kprintf("auto enable/disable\n");
  533. return 0;
  534. }
  535. static int wifi_debug_save_cfg(int argc, char *argv[])
  536. {
  537. struct rt_wlan_cfg_info cfg_info;
  538. int len;
  539. char *ssid = RT_NULL, *password = RT_NULL;
  540. rt_memset(&cfg_info, 0, sizeof(cfg_info));
  541. INVALID_INFO(&cfg_info.info);
  542. if (argc == 2)
  543. {
  544. ssid = argv[1];
  545. }
  546. else if (argc == 3)
  547. {
  548. ssid = argv[1];
  549. password = argv[2];
  550. }
  551. else
  552. {
  553. return -1;
  554. }
  555. if (ssid != RT_NULL)
  556. {
  557. len = rt_strlen(ssid);
  558. if (len > RT_WLAN_SSID_MAX_LENGTH)
  559. {
  560. rt_kprintf("ssid is to long");
  561. return 0;
  562. }
  563. rt_memcpy(&cfg_info.info.ssid.val[0], ssid, len);
  564. cfg_info.info.ssid.len = len;
  565. }
  566. if (password != RT_NULL)
  567. {
  568. len = rt_strlen(password);
  569. if (len > RT_WLAN_PASSWORD_MAX_LENGTH)
  570. {
  571. rt_kprintf("password is to long");
  572. return 0;
  573. }
  574. rt_memcpy(&cfg_info.key.val[0], password, len);
  575. cfg_info.key.len = len;
  576. }
  577. #ifdef RT_WLAN_CFG_ENABLE
  578. rt_wlan_cfg_save(&cfg_info);
  579. #endif
  580. return 0;
  581. }
  582. static int wifi_debug_dump_cfg(int argc, char *argv[])
  583. {
  584. if (argc == 1)
  585. {
  586. #ifdef RT_WLAN_CFG_ENABLE
  587. rt_wlan_cfg_dump();
  588. #endif
  589. }
  590. else
  591. {
  592. return -1;
  593. }
  594. return 0;
  595. }
  596. static int wifi_debug_clear_cfg(int argc, char *argv[])
  597. {
  598. if (argc == 1)
  599. {
  600. #ifdef RT_WLAN_CFG_ENABLE
  601. rt_wlan_cfg_delete_all();
  602. rt_wlan_cfg_cache_save();
  603. #endif
  604. }
  605. else
  606. {
  607. return -1;
  608. }
  609. return 0;
  610. }
  611. static int wifi_debug_dump_prot(int argc, char *argv[])
  612. {
  613. if (argc == 1)
  614. {
  615. #ifdef RT_WLAN_PROT_ENABLE
  616. rt_wlan_prot_dump();
  617. #else
  618. rt_kprintf("wlan protocol disabled\r\n");
  619. return -1;
  620. #endif
  621. }
  622. else
  623. {
  624. return -1;
  625. }
  626. return 0;
  627. }
  628. static int wifi_debug_set_mode(int argc, char *argv[])
  629. {
  630. rt_wlan_mode_t mode;
  631. if (argc != 3)
  632. return -1;
  633. if (rt_strcmp("sta", argv[1]) == 0)
  634. {
  635. mode = RT_WLAN_STATION;
  636. }
  637. else if (rt_strcmp("ap", argv[1]) == 0)
  638. {
  639. mode = RT_WLAN_AP;
  640. }
  641. else if (rt_strcmp("none", argv[1]) == 0)
  642. {
  643. mode = RT_WLAN_NONE;
  644. }
  645. else
  646. return -1;
  647. rt_wlan_set_mode(argv[2], mode);
  648. return 0;
  649. }
  650. static int wifi_debug_set_prot(int argc, char *argv[])
  651. {
  652. if (argc != 3)
  653. {
  654. return -1;
  655. }
  656. #ifdef RT_WLAN_PROT_ENABLE
  657. rt_wlan_prot_attach(argv[2], argv[1]);
  658. #else
  659. rt_kprintf("wlan protocol disabled\r\n");
  660. return -1;
  661. #endif
  662. return 0;
  663. }
  664. static int wifi_debug_set_autoconnect(int argc, char *argv[])
  665. {
  666. if (argc == 2)
  667. {
  668. if (rt_strcmp(argv[1], "enable") == 0)
  669. rt_wlan_config_autoreconnect(RT_TRUE);
  670. else if (rt_strcmp(argv[1], "disable") == 0)
  671. rt_wlan_config_autoreconnect(RT_FALSE);
  672. }
  673. else
  674. {
  675. return -1;
  676. }
  677. return 0;
  678. }
  679. static int wifi_debug(int argc, char *argv[])
  680. {
  681. int i, result = 0;
  682. const struct wifi_cmd_des *run_cmd = RT_NULL;
  683. if (argc < 3)
  684. {
  685. wifi_debug_help(0, RT_NULL);
  686. return 0;
  687. }
  688. for (i = 0; i < sizeof(debug_tab) / sizeof(debug_tab[0]); i++)
  689. {
  690. if (rt_strcmp(debug_tab[i].cmd, argv[2]) == 0)
  691. {
  692. run_cmd = &debug_tab[i];
  693. break;
  694. }
  695. }
  696. if (run_cmd == RT_NULL)
  697. {
  698. wifi_debug_help(0, RT_NULL);
  699. return 0;
  700. }
  701. if (run_cmd->fun != RT_NULL)
  702. {
  703. result = run_cmd->fun(argc - 2, &argv[2]);
  704. }
  705. if (result)
  706. {
  707. wifi_debug_help(argc - 2, &argv[2]);
  708. }
  709. return 0;
  710. }
  711. #endif
  712. static int wifi_msh(int argc, char *argv[])
  713. {
  714. int i, result = 0;
  715. const struct wifi_cmd_des *run_cmd = RT_NULL;
  716. if (argc == 1)
  717. {
  718. wifi_help(argc, argv);
  719. return 0;
  720. }
  721. /* find fun */
  722. for (i = 0; i < sizeof(cmd_tab) / sizeof(cmd_tab[0]); i++)
  723. {
  724. if (rt_strcmp(cmd_tab[i].cmd, argv[1]) == 0)
  725. {
  726. run_cmd = &cmd_tab[i];
  727. break;
  728. }
  729. }
  730. /* not find fun, print help */
  731. if (run_cmd == RT_NULL)
  732. {
  733. wifi_help(argc, argv);
  734. return 0;
  735. }
  736. /* run fun */
  737. if (run_cmd->fun != RT_NULL)
  738. {
  739. result = run_cmd->fun(argc, argv);
  740. }
  741. if (result)
  742. {
  743. wifi_help(argc, argv);
  744. }
  745. return 0;
  746. }
  747. #if defined(RT_USING_FINSH)
  748. MSH_CMD_EXPORT_ALIAS(wifi_msh, wifi, wifi command);
  749. #endif
  750. #endif