wlan_cmd.c 13 KB

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