wlan_cmd.c 19 KB

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