wlan_cmd.c 19 KB

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