wlan_cmd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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-13 tyx the first version
  9. */
  10. #include <rtthread.h>
  11. #include <wlan_mgnt.h>
  12. #include <wlan_cfg.h>
  13. #include <wlan_prot.h>
  14. struct wifi_cmd_des
  15. {
  16. const char *cmd;
  17. int (*fun)(int argc, char *argv[]);
  18. };
  19. static int wifi_help(int argc, char *argv[]);
  20. static int wifi_scan(int argc, char *argv[]);
  21. static int wifi_status(int argc, char *argv[]);
  22. static int wifi_join(int argc, char *argv[]);
  23. static int wifi_ap(int argc, char *argv[]);
  24. static int wifi_list_sta(int argc, char *argv[]);
  25. static int wifi_disconnect(int argc, char *argv[]);
  26. static int wifi_ap_stop(int argc, char *argv[]);
  27. #ifdef RT_WLAN_CMD_DEBUG
  28. /* just for debug */
  29. static int wifi_debug(int argc, char *argv[]);
  30. static int wifi_debug_save_cfg(int argc, char *argv[]);
  31. static int wifi_debug_dump_cfg(int argc, char *argv[]);
  32. static int wifi_debug_clear_cfg(int argc, char *argv[]);
  33. static int wifi_debug_dump_prot(int argc, char *argv[]);
  34. static int wifi_debug_set_mode(int argc, char *argv[]);
  35. static int wifi_debug_set_prot(int argc, char *argv[]);
  36. static int wifi_debug_set_autoconnect(int argc, char *argv[]);
  37. #endif
  38. /* cmd table */
  39. static const struct wifi_cmd_des cmd_tab[] =
  40. {
  41. {"scan", wifi_scan},
  42. {"help", wifi_help},
  43. {"status", wifi_status},
  44. {"join", wifi_join},
  45. {"ap", wifi_ap},
  46. {"list_sta", wifi_list_sta},
  47. {"disc", wifi_disconnect},
  48. {"ap_stop", wifi_ap_stop},
  49. {"smartconfig", RT_NULL},
  50. #ifdef RT_WLAN_CMD_DEBUG
  51. {"-d", wifi_debug},
  52. #endif
  53. };
  54. #ifdef RT_WLAN_CMD_DEBUG
  55. /* debug cmd table */
  56. static const struct wifi_cmd_des debug_tab[] =
  57. {
  58. {"save_cfg", wifi_debug_save_cfg},
  59. {"dump_cfg", wifi_debug_dump_cfg},
  60. {"clear_cfg", wifi_debug_clear_cfg},
  61. {"dump_prot", wifi_debug_dump_prot},
  62. {"mode", wifi_debug_set_mode},
  63. {"prot", wifi_debug_set_prot},
  64. {"auto", wifi_debug_set_autoconnect},
  65. };
  66. #endif
  67. static int wifi_help(int argc, char *argv[])
  68. {
  69. rt_kprintf("wifi\n");
  70. rt_kprintf("wifi help\n");
  71. rt_kprintf("wifi scan\n");
  72. rt_kprintf("wifi join [SSID] [PASSWORD]\n");
  73. rt_kprintf("wifi ap SSID [PASSWORD]\n");
  74. rt_kprintf("wifi disc\n");
  75. rt_kprintf("wifi ap_stop\n");
  76. rt_kprintf("wifi status\n");
  77. rt_kprintf("wifi smartconfig\n");
  78. #ifdef RT_WLAN_CMD_DEBUG
  79. rt_kprintf("wifi -d debug command\n");
  80. #endif
  81. return 0;
  82. }
  83. static int wifi_status(int argc, char *argv[])
  84. {
  85. int rssi;
  86. struct rt_wlan_info info;
  87. if (argc > 2)
  88. return -1;
  89. if (rt_wlan_is_connected() == 1)
  90. {
  91. rssi = rt_wlan_get_rssi();
  92. rt_wlan_get_info(&info);
  93. rt_kprintf("Wi-Fi STA Info\n");
  94. rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]);
  95. rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0],
  96. info.bssid[1],
  97. info.bssid[2],
  98. info.bssid[3],
  99. info.bssid[4],
  100. info.bssid[5]);
  101. rt_kprintf("Channel: %d\n", info.channel);
  102. rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000);
  103. rt_kprintf("RSSI: %d\n", rssi);
  104. }
  105. else
  106. {
  107. rt_kprintf("wifi disconnected!\n");
  108. }
  109. if (rt_wlan_ap_is_active() == 1)
  110. {
  111. rt_wlan_ap_get_info(&info);
  112. rt_kprintf("Wi-Fi AP Info\n");
  113. rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]);
  114. rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0],
  115. info.bssid[1],
  116. info.bssid[2],
  117. info.bssid[3],
  118. info.bssid[4],
  119. info.bssid[5]);
  120. rt_kprintf("Channel: %d\n", info.channel);
  121. rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000);
  122. rt_kprintf("hidden: %s\n", info.hidden ? "Enable" : "Disable");
  123. }
  124. else
  125. {
  126. rt_kprintf("wifi ap not start!\n");
  127. }
  128. rt_kprintf("Auto Connect status:%s!\n", (rt_wlan_get_autoreconnect_mode() ? "Enable" : "Disable"));
  129. return 0;
  130. }
  131. static int wifi_scan(int argc, char *argv[])
  132. {
  133. struct rt_wlan_scan_result *scan_result = RT_NULL;
  134. if (argc > 2)
  135. return -1;
  136. /* scan ap info */
  137. scan_result = rt_wlan_scan_sync();
  138. if (scan_result)
  139. {
  140. int index, num;
  141. char *security;
  142. num = scan_result->num;
  143. rt_kprintf(" SSID MAC security rssi chn Mbps\n");
  144. rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n");
  145. for (index = 0; index < num; index ++)
  146. {
  147. rt_kprintf("%-32.32s", &scan_result->info[index].ssid.val[0]);
  148. rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ",
  149. scan_result->info[index].bssid[0],
  150. scan_result->info[index].bssid[1],
  151. scan_result->info[index].bssid[2],
  152. scan_result->info[index].bssid[3],
  153. scan_result->info[index].bssid[4],
  154. scan_result->info[index].bssid[5]
  155. );
  156. switch (scan_result->info[index].security)
  157. {
  158. case SECURITY_OPEN:
  159. security = "OPEN";
  160. break;
  161. case SECURITY_WEP_PSK:
  162. security = "WEP_PSK";
  163. break;
  164. case SECURITY_WEP_SHARED:
  165. security = "WEP_SHARED";
  166. break;
  167. case SECURITY_WPA_TKIP_PSK:
  168. security = "WPA_TKIP_PSK";
  169. break;
  170. case SECURITY_WPA_AES_PSK:
  171. security = "WPA_AES_PSK";
  172. break;
  173. case SECURITY_WPA2_AES_PSK:
  174. security = "WPA2_AES_PSK";
  175. break;
  176. case SECURITY_WPA2_TKIP_PSK:
  177. security = "WPA2_TKIP_PSK";
  178. break;
  179. case SECURITY_WPA2_MIXED_PSK:
  180. security = "WPA2_MIXED_PSK";
  181. break;
  182. case SECURITY_WPS_OPEN:
  183. security = "WPS_OPEN";
  184. break;
  185. case SECURITY_WPS_SECURE:
  186. security = "WPS_SECURE";
  187. break;
  188. default:
  189. security = "UNKNOWN";
  190. break;
  191. }
  192. rt_kprintf("%-14.14s ", security);
  193. rt_kprintf("%-4d ", scan_result->info[index].rssi);
  194. rt_kprintf("%3d ", scan_result->info[index].channel);
  195. rt_kprintf("%4d\n", scan_result->info[index].datarate / 1000000);
  196. }
  197. rt_wlan_scan_result_clean();
  198. }
  199. else
  200. {
  201. rt_kprintf("wifi scan result is null\n");
  202. }
  203. return 0;
  204. }
  205. static int wifi_join(int argc, char *argv[])
  206. {
  207. const char *ssid = RT_NULL;
  208. const char *key = RT_NULL;
  209. struct rt_wlan_cfg_info cfg_info;
  210. if (argc == 2)
  211. {
  212. /* get info to connect */
  213. if (rt_wlan_cfg_read_index(&cfg_info, 0) == 1)
  214. {
  215. ssid = (char *)(&cfg_info.info.ssid.val[0]);
  216. if (cfg_info.key.len)
  217. key = (char *)(&cfg_info.key.val[0]);
  218. }
  219. else
  220. {
  221. rt_kprintf("not find info\n");
  222. }
  223. }
  224. else if (argc == 3)
  225. {
  226. /* ssid */
  227. ssid = argv[2];
  228. }
  229. else if (argc == 4)
  230. {
  231. ssid = argv[2];
  232. /* password */
  233. key = argv[3];
  234. }
  235. else
  236. {
  237. return -1;
  238. }
  239. rt_wlan_connect(ssid, key);
  240. return 0;
  241. }
  242. static int wifi_ap(int argc, char *argv[])
  243. {
  244. const char *ssid = RT_NULL;
  245. const char *key = RT_NULL;
  246. if (argc == 3)
  247. {
  248. ssid = argv[2];
  249. }
  250. else if (argc == 4)
  251. {
  252. ssid = argv[2];
  253. key = argv[3];
  254. }
  255. else
  256. {
  257. return -1;
  258. }
  259. rt_wlan_start_ap(ssid, key);
  260. return 0;
  261. }
  262. static int wifi_list_sta(int argc, char *argv[])
  263. {
  264. struct rt_wlan_info *sta_info;
  265. int num, i;
  266. if (argc > 2)
  267. return -1;
  268. num = rt_wlan_ap_get_sta_num();
  269. sta_info = rt_malloc(sizeof(struct rt_wlan_info) * num);
  270. if (sta_info == RT_NULL)
  271. {
  272. rt_kprintf("num:%d\n", num);
  273. return 0;
  274. }
  275. rt_wlan_ap_get_sta_info(sta_info, num);
  276. rt_kprintf("num:%d\n", num);
  277. for (i = 0; i < num; i++)
  278. {
  279. rt_kprintf("sta mac %02x:%02x:%02x:%02x:%02x:%02x\n",
  280. sta_info[i].bssid[0], sta_info[i].bssid[1], sta_info[i].bssid[2],
  281. sta_info[i].bssid[3], sta_info[i].bssid[4], sta_info[i].bssid[5]);
  282. }
  283. rt_free(sta_info);
  284. return 0;
  285. }
  286. static int wifi_disconnect(int argc, char *argv[])
  287. {
  288. if (argc != 2)
  289. {
  290. return -1;
  291. }
  292. rt_wlan_disconnect();
  293. return 0;
  294. }
  295. static int wifi_ap_stop(int argc, char *argv[])
  296. {
  297. if (argc != 2)
  298. {
  299. return -1;
  300. }
  301. rt_wlan_ap_stop();
  302. return 0;
  303. }
  304. #ifdef RT_WLAN_CMD_DEBUG
  305. /* just for debug */
  306. static int wifi_debug_help(int argc, char *argv[])
  307. {
  308. rt_kprintf("save_cfg ssid [password]\n");
  309. rt_kprintf("dump_cfg\n");
  310. rt_kprintf("clear_cfg\n");
  311. rt_kprintf("dump_prot\n");
  312. rt_kprintf("mode sta/ap dev_name\n");
  313. rt_kprintf("prot lwip dev_name\n");
  314. rt_kprintf("auto enable/disable\n");
  315. return 0;
  316. }
  317. static int wifi_debug_save_cfg(int argc, char *argv[])
  318. {
  319. struct rt_wlan_cfg_info cfg_info;
  320. int len;
  321. char *ssid = RT_NULL, *password = RT_NULL;
  322. rt_memset(&cfg_info, 0, sizeof(cfg_info));
  323. INVALID_INFO(&cfg_info.info);
  324. if (argc == 2)
  325. {
  326. ssid = argv[1];
  327. }
  328. else if (argc == 3)
  329. {
  330. ssid = argv[1];
  331. password = argv[2];
  332. }
  333. else
  334. {
  335. return -1;
  336. }
  337. if (ssid != RT_NULL)
  338. {
  339. len = rt_strlen(ssid);
  340. if (len > RT_WLAN_SSID_MAX_LENGTH)
  341. {
  342. rt_kprintf("ssid is to long");
  343. return 0;
  344. }
  345. rt_memcpy(&cfg_info.info.ssid.val[0], ssid, len);
  346. cfg_info.info.ssid.len = len;
  347. }
  348. if (password != RT_NULL)
  349. {
  350. len = rt_strlen(password);
  351. if (len > RT_WLAN_PASSWORD_MAX_LENGTH)
  352. {
  353. rt_kprintf("password is to long");
  354. return 0;
  355. }
  356. rt_memcpy(&cfg_info.key.val[0], password, len);
  357. cfg_info.key.len = len;
  358. }
  359. rt_wlan_cfg_save(&cfg_info);
  360. return 0;
  361. }
  362. static int wifi_debug_dump_cfg(int argc, char *argv[])
  363. {
  364. if (argc == 1)
  365. {
  366. rt_wlan_cfg_dump();
  367. }
  368. else
  369. {
  370. return -1;
  371. }
  372. return 0;
  373. }
  374. static int wifi_debug_clear_cfg(int argc, char *argv[])
  375. {
  376. if (argc == 1)
  377. {
  378. rt_wlan_cfg_delete_all();
  379. rt_wlan_cfg_cache_save();
  380. }
  381. else
  382. {
  383. return -1;
  384. }
  385. return 0;
  386. }
  387. static int wifi_debug_dump_prot(int argc, char *argv[])
  388. {
  389. if (argc == 1)
  390. {
  391. rt_wlan_prot_dump();
  392. }
  393. else
  394. {
  395. return -1;
  396. }
  397. return 0;
  398. }
  399. static int wifi_debug_set_mode(int argc, char *argv[])
  400. {
  401. rt_wlan_mode_t mode;
  402. if (argc != 3)
  403. return -1;
  404. if (rt_strcmp("sta", argv[1]) == 0)
  405. {
  406. mode = RT_WLAN_STATION;
  407. }
  408. else if (rt_strcmp("ap", argv[1]) == 0)
  409. {
  410. mode = RT_WLAN_AP;
  411. }
  412. else
  413. return -1;
  414. rt_wlan_set_mode(argv[2], mode);
  415. return 0;
  416. }
  417. static int wifi_debug_set_prot(int argc, char *argv[])
  418. {
  419. if (argc != 3)
  420. {
  421. return -1;
  422. }
  423. rt_wlan_prot_attach(argv[2], argv[1]);
  424. return 0;
  425. }
  426. static int wifi_debug_set_autoconnect(int argc, char *argv[])
  427. {
  428. if (argc == 2)
  429. {
  430. if (rt_strcmp(argv[1], "enable") == 0)
  431. rt_wlan_config_autoreconnect(RT_TRUE);
  432. else if (rt_strcmp(argv[1], "disable") == 0)
  433. rt_wlan_config_autoreconnect(RT_FALSE);
  434. }
  435. else
  436. {
  437. return -1;
  438. }
  439. return 0;
  440. }
  441. static int wifi_debug(int argc, char *argv[])
  442. {
  443. int i, result = 0;
  444. const struct wifi_cmd_des *run_cmd = RT_NULL;
  445. if (argc < 3)
  446. {
  447. wifi_debug_help(0, RT_NULL);
  448. return 0;
  449. }
  450. for (i = 0; i < sizeof(debug_tab) / sizeof(debug_tab[0]); i++)
  451. {
  452. if (rt_strcmp(debug_tab[i].cmd, argv[2]) == 0)
  453. {
  454. run_cmd = &debug_tab[i];
  455. break;
  456. }
  457. }
  458. if (run_cmd == RT_NULL)
  459. {
  460. wifi_debug_help(0, RT_NULL);
  461. return 0;
  462. }
  463. if (run_cmd->fun != RT_NULL)
  464. {
  465. result = run_cmd->fun(argc - 2, &argv[2]);
  466. }
  467. if (result)
  468. {
  469. wifi_debug_help(argc - 2, &argv[2]);
  470. }
  471. return 0;
  472. }
  473. #endif
  474. static int wifi_msh(int argc, char *argv[])
  475. {
  476. int i, result = 0;
  477. const struct wifi_cmd_des *run_cmd = RT_NULL;
  478. if (argc == 1)
  479. {
  480. wifi_help(argc, argv);
  481. return 0;
  482. }
  483. /* find fun */
  484. for (i = 0; i < sizeof(cmd_tab) / sizeof(cmd_tab[0]); i++)
  485. {
  486. if (rt_strcmp(cmd_tab[i].cmd, argv[1]) == 0)
  487. {
  488. run_cmd = &cmd_tab[i];
  489. break;
  490. }
  491. }
  492. /* not find fun, print help */
  493. if (run_cmd == RT_NULL)
  494. {
  495. wifi_help(argc, argv);
  496. return 0;
  497. }
  498. /* run fun */
  499. if (run_cmd->fun != RT_NULL)
  500. {
  501. result = run_cmd->fun(argc, argv);
  502. }
  503. if (result)
  504. {
  505. wifi_help(argc, argv);
  506. }
  507. return 0;
  508. }
  509. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  510. FINSH_FUNCTION_EXPORT_ALIAS(wifi_msh, __cmd_wifi, wifi command.);
  511. #endif