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