wlan_cmd.c 13.6 KB
Newer Older
B
bernard 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
bernard 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
B
bernard 已提交
5 6 7
 *
 * Change Logs:
 * Date           Author       Notes
8
 * 2018-08-13     tyx          the first version
B
bernard 已提交
9 10 11
 */

#include <rtthread.h>
12 13 14
#include <wlan_mgnt.h>
#include <wlan_cfg.h>
#include <wlan_prot.h>
B
bernard 已提交
15

16 17
#if defined(RT_WLAN_MANAGE_ENABLE) && defined(RT_WLAN_MSH_CMD_ENABLE)

18
struct wifi_cmd_des
B
bernard 已提交
19
{
20 21 22 23 24 25 26 27 28
    const char *cmd;
    int (*fun)(int argc, char *argv[]);
};

static int wifi_help(int argc, char *argv[]);
static int wifi_scan(int argc, char *argv[]);
static int wifi_status(int argc, char *argv[]);
static int wifi_join(int argc, char *argv[]);
static int wifi_ap(int argc, char *argv[]);
29
static int wifi_list_sta(int argc, char *argv[]);
30 31 32
static int wifi_disconnect(int argc, char *argv[]);
static int wifi_ap_stop(int argc, char *argv[]);

33
#ifdef RT_WLAN_CMD_DEBUG
34
/* just for debug  */
35
static int wifi_debug(int argc, char *argv[]);
36 37 38 39 40 41 42
static int wifi_debug_save_cfg(int argc, char *argv[]);
static int wifi_debug_dump_cfg(int argc, char *argv[]);
static int wifi_debug_clear_cfg(int argc, char *argv[]);
static int wifi_debug_dump_prot(int argc, char *argv[]);
static int wifi_debug_set_mode(int argc, char *argv[]);
static int wifi_debug_set_prot(int argc, char *argv[]);
static int wifi_debug_set_autoconnect(int argc, char *argv[]);
43
#endif
44 45 46 47 48 49 50 51 52

/* cmd table */
static const struct wifi_cmd_des cmd_tab[] =
{
    {"scan", wifi_scan},
    {"help", wifi_help},
    {"status", wifi_status},
    {"join", wifi_join},
    {"ap", wifi_ap},
53
    {"list_sta", wifi_list_sta},
54 55 56
    {"disc", wifi_disconnect},
    {"ap_stop", wifi_ap_stop},
    {"smartconfig", RT_NULL},
57
#ifdef RT_WLAN_CMD_DEBUG
58
    {"-d", wifi_debug},
59
#endif
60 61
};

62
#ifdef RT_WLAN_CMD_DEBUG
63 64 65 66 67 68 69 70 71 72 73
/* debug cmd table */
static const struct wifi_cmd_des debug_tab[] =
{
    {"save_cfg", wifi_debug_save_cfg},
    {"dump_cfg", wifi_debug_dump_cfg},
    {"clear_cfg", wifi_debug_clear_cfg},
    {"dump_prot", wifi_debug_dump_prot},
    {"mode", wifi_debug_set_mode},
    {"prot", wifi_debug_set_prot},
    {"auto", wifi_debug_set_autoconnect},
};
74
#endif
75 76 77 78 79

static int wifi_help(int argc, char *argv[])
{
    rt_kprintf("wifi\n");
    rt_kprintf("wifi help\n");
80
    rt_kprintf("wifi scan [SSID]\n");
81 82 83 84 85 86
    rt_kprintf("wifi join [SSID] [PASSWORD]\n");
    rt_kprintf("wifi ap SSID [PASSWORD]\n");
    rt_kprintf("wifi disc\n");
    rt_kprintf("wifi ap_stop\n");
    rt_kprintf("wifi status\n");
    rt_kprintf("wifi smartconfig\n");
87
#ifdef RT_WLAN_CMD_DEBUG
88
    rt_kprintf("wifi -d debug command\n");
89
#endif
90
    return 0;
B
bernard 已提交
91 92
}

93
static int wifi_status(int argc, char *argv[])
B
bernard 已提交
94
{
95 96
    int rssi;
    struct rt_wlan_info info;
B
bernard 已提交
97

98 99
    if (argc > 2)
        return -1;
B
bernard 已提交
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    if (rt_wlan_is_connected() == 1)
    {
        rssi = rt_wlan_get_rssi();
        rt_wlan_get_info(&info);
        rt_kprintf("Wi-Fi STA Info\n");
        rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]);
        rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0],
                   info.bssid[1],
                   info.bssid[2],
                   info.bssid[3],
                   info.bssid[4],
                   info.bssid[5]);
        rt_kprintf("Channel: %d\n", info.channel);
        rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000);
        rt_kprintf("RSSI: %d\n", rssi);
    }
    else
B
bernard 已提交
118
    {
119
        rt_kprintf("wifi disconnected!\n");
B
bernard 已提交
120 121
    }

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    if (rt_wlan_ap_is_active() == 1)
    {
        rt_wlan_ap_get_info(&info);
        rt_kprintf("Wi-Fi AP Info\n");
        rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]);
        rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0],
                   info.bssid[1],
                   info.bssid[2],
                   info.bssid[3],
                   info.bssid[4],
                   info.bssid[5]);
        rt_kprintf("Channel: %d\n", info.channel);
        rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000);
        rt_kprintf("hidden: %s\n", info.hidden ? "Enable" : "Disable");
    }
    else
    {
        rt_kprintf("wifi ap not start!\n");
    }
    rt_kprintf("Auto Connect status:%s!\n", (rt_wlan_get_autoreconnect_mode() ? "Enable" : "Disable"));
B
bernard 已提交
142 143 144
    return 0;
}

145
static int wifi_scan(int argc, char *argv[])
B
bernard 已提交
146
{
147
    struct rt_wlan_scan_result *scan_result = RT_NULL;
148 149
    struct rt_wlan_info *info = RT_NULL;
    struct rt_wlan_info filter;
B
bernard 已提交
150

151
    if (argc > 3)
B
bernard 已提交
152 153
        return -1;

154 155 156 157 158 159 160 161 162
    if (argc == 3)
    {
        INVALID_INFO(&filter);
        SSID_SET(&filter, argv[2]);
        info = &filter;
    }

    /* clean scan result */
    rt_wlan_scan_result_clean();
163
    /* scan ap info */
164
    scan_result = rt_wlan_scan_with_info(info);
165
    if (scan_result)
B
bernard 已提交
166
    {
167 168
        int index, num;
        char *security;
B
bernard 已提交
169

170 171 172 173
        num = scan_result->num;
        rt_kprintf("             SSID                      MAC            security    rssi chn Mbps\n");
        rt_kprintf("------------------------------- -----------------  -------------- ---- --- ----\n");
        for (index = 0; index < num; index ++)
B
bernard 已提交
174
        {
175 176 177 178 179 180 181 182 183 184
            rt_kprintf("%-32.32s", &scan_result->info[index].ssid.val[0]);
            rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x  ",
                       scan_result->info[index].bssid[0],
                       scan_result->info[index].bssid[1],
                       scan_result->info[index].bssid[2],
                       scan_result->info[index].bssid[3],
                       scan_result->info[index].bssid[4],
                       scan_result->info[index].bssid[5]
                      );
            switch (scan_result->info[index].security)
B
bernard 已提交
185
            {
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            case SECURITY_OPEN:
                security = "OPEN";
                break;
            case SECURITY_WEP_PSK:
                security = "WEP_PSK";
                break;
            case SECURITY_WEP_SHARED:
                security = "WEP_SHARED";
                break;
            case SECURITY_WPA_TKIP_PSK:
                security = "WPA_TKIP_PSK";
                break;
            case SECURITY_WPA_AES_PSK:
                security = "WPA_AES_PSK";
                break;
            case SECURITY_WPA2_AES_PSK:
                security = "WPA2_AES_PSK";
                break;
            case SECURITY_WPA2_TKIP_PSK:
                security = "WPA2_TKIP_PSK";
                break;
            case SECURITY_WPA2_MIXED_PSK:
                security = "WPA2_MIXED_PSK";
                break;
            case SECURITY_WPS_OPEN:
                security = "WPS_OPEN";
                break;
            case SECURITY_WPS_SECURE:
                security = "WPS_SECURE";
                break;
            default:
                security = "UNKNOWN";
                break;
B
bernard 已提交
219
            }
220 221 222 223
            rt_kprintf("%-14.14s ", security);
            rt_kprintf("%-4d ", scan_result->info[index].rssi);
            rt_kprintf("%3d ", scan_result->info[index].channel);
            rt_kprintf("%4d\n", scan_result->info[index].datarate / 1000000);
B
bernard 已提交
224
        }
225
        rt_wlan_scan_result_clean();
B
bernard 已提交
226
    }
227
    else
B
bernard 已提交
228
    {
229 230 231 232
        rt_kprintf("wifi scan result is null\n");
    }
    return 0;
}
B
bernard 已提交
233

234 235 236 237 238
static int wifi_join(int argc, char *argv[])
{
    const char *ssid = RT_NULL;
    const char *key = RT_NULL;
    struct rt_wlan_cfg_info cfg_info;
B
bernard 已提交
239

240
    rt_memset(&cfg_info, 0, sizeof(cfg_info));
241 242
    if (argc ==  2)
    {
243
#ifdef RT_WLAN_CFG_ENABLE
244 245
        /* get info to connect */
        if (rt_wlan_cfg_read_index(&cfg_info, 0) == 1)
B
bernard 已提交
246
        {
247 248 249
            ssid = (char *)(&cfg_info.info.ssid.val[0]);
            if (cfg_info.key.len)
                key = (char *)(&cfg_info.key.val[0]);
B
bernard 已提交
250
        }
251
        else
252
#endif
B
bernard 已提交
253
        {
254
            rt_kprintf("not find connect info\n");
B
bernard 已提交
255 256
        }
    }
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    else if (argc == 3)
    {
        /* ssid */
        ssid = argv[2];
    }
    else if (argc == 4)
    {
        ssid = argv[2];
        /* password */
        key = argv[3];
    }
    else
    {
        return -1;
    }
    rt_wlan_connect(ssid, key);
B
bernard 已提交
273 274 275
    return 0;
}

276
static int wifi_ap(int argc, char *argv[])
B
bernard 已提交
277
{
278 279
    const char *ssid = RT_NULL;
    const char *key = RT_NULL;
B
bernard 已提交
280

281
    if (argc == 3)
B
bernard 已提交
282
    {
283 284 285 286 287 288
        ssid = argv[2];
    }
    else if (argc == 4)
    {
        ssid = argv[2];
        key = argv[3];
B
bernard 已提交
289 290 291
    }
    else
    {
292
        return -1;
B
bernard 已提交
293 294
    }

295 296 297
    rt_wlan_start_ap(ssid, key);
    return 0;
}
B
bernard 已提交
298

299
static int wifi_list_sta(int argc, char *argv[])
300 301 302
{
    struct rt_wlan_info *sta_info;
    int num, i;
B
bernard 已提交
303

304 305 306 307 308 309 310 311
    if (argc > 2)
        return -1;
    num = rt_wlan_ap_get_sta_num();
    sta_info = rt_malloc(sizeof(struct rt_wlan_info) * num);
    if (sta_info == RT_NULL)
    {
        rt_kprintf("num:%d\n", num);
        return 0;
B
bernard 已提交
312
    }
313 314 315 316 317 318 319 320 321
    rt_wlan_ap_get_sta_info(sta_info, num);
    rt_kprintf("num:%d\n", num);
    for (i = 0; i < num; i++)
    {
        rt_kprintf("sta mac  %02x:%02x:%02x:%02x:%02x:%02x\n",
                   sta_info[i].bssid[0], sta_info[i].bssid[1], sta_info[i].bssid[2],
                   sta_info[i].bssid[3], sta_info[i].bssid[4], sta_info[i].bssid[5]);
    }
    rt_free(sta_info);
B
bernard 已提交
322 323 324
    return 0;
}

325
static int wifi_disconnect(int argc, char *argv[])
B
bernard 已提交
326
{
327 328 329 330
    if (argc != 2)
    {
        return -1;
    }
B
bernard 已提交
331

332
    rt_wlan_disconnect();
B
bernard 已提交
333 334 335
    return 0;
}

336
static int wifi_ap_stop(int argc, char *argv[])
B
bernard 已提交
337
{
338
    if (argc != 2)
B
bernard 已提交
339
    {
340
        return -1;
B
bernard 已提交
341 342
    }

343
    rt_wlan_ap_stop();
B
bernard 已提交
344 345 346
    return 0;
}

347
#ifdef RT_WLAN_CMD_DEBUG
348 349
/* just for debug */
static int wifi_debug_help(int argc, char *argv[])
B
bernard 已提交
350
{
351 352 353 354 355 356 357 358 359
    rt_kprintf("save_cfg ssid [password]\n");
    rt_kprintf("dump_cfg\n");
    rt_kprintf("clear_cfg\n");
    rt_kprintf("dump_prot\n");
    rt_kprintf("mode sta/ap dev_name\n");
    rt_kprintf("prot lwip dev_name\n");
    rt_kprintf("auto enable/disable\n");
    return 0;
}
B
bernard 已提交
360

361 362 363 364 365
static int wifi_debug_save_cfg(int argc, char *argv[])
{
    struct rt_wlan_cfg_info cfg_info;
    int len;
    char *ssid = RT_NULL, *password = RT_NULL;
B
bernard 已提交
366

367 368 369
    rt_memset(&cfg_info, 0, sizeof(cfg_info));
    INVALID_INFO(&cfg_info.info);
    if (argc == 2)
B
bernard 已提交
370
    {
371 372 373 374 375 376
        ssid = argv[1];
    }
    else if (argc == 3)
    {
        ssid = argv[1];
        password = argv[2];
B
bernard 已提交
377 378 379
    }
    else
    {
380 381 382 383 384 385 386
        return -1;
    }

    if (ssid != RT_NULL)
    {
        len = rt_strlen(ssid);
        if (len > RT_WLAN_SSID_MAX_LENGTH)
B
bernard 已提交
387
        {
388 389
            rt_kprintf("ssid is to long");
            return 0;
B
bernard 已提交
390
        }
391 392 393
        rt_memcpy(&cfg_info.info.ssid.val[0], ssid, len);
        cfg_info.info.ssid.len = len;
    }
B
bernard 已提交
394

395 396 397 398
    if (password != RT_NULL)
    {
        len = rt_strlen(password);
        if (len > RT_WLAN_PASSWORD_MAX_LENGTH)
B
bernard 已提交
399
        {
400 401
            rt_kprintf("password is to long");
            return 0;
B
bernard 已提交
402
        }
403 404
        rt_memcpy(&cfg_info.key.val[0], password, len);
        cfg_info.key.len = len;
B
bernard 已提交
405
    }
406
#ifdef RT_WLAN_CFG_ENABLE
407
    rt_wlan_cfg_save(&cfg_info);
408
#endif
409
    return 0;
B
bernard 已提交
410 411
}

412
static int wifi_debug_dump_cfg(int argc, char *argv[])
B
bernard 已提交
413
{
414 415
    if (argc == 1)
    {
416
#ifdef RT_WLAN_CFG_ENABLE
417
        rt_wlan_cfg_dump();
418
#endif
419 420 421 422 423 424
    }
    else
    {
        return -1;
    }
    return 0;
B
bernard 已提交
425 426
}

427
static int wifi_debug_clear_cfg(int argc, char *argv[])
B
bernard 已提交
428 429 430
{
    if (argc == 1)
    {
431
#ifdef RT_WLAN_CFG_ENABLE
432 433
        rt_wlan_cfg_delete_all();
        rt_wlan_cfg_cache_save();
434
#endif
B
bernard 已提交
435
    }
436
    else
B
bernard 已提交
437
    {
438
        return -1;
B
bernard 已提交
439
    }
440 441
    return 0;
}
B
bernard 已提交
442

443 444 445
static int wifi_debug_dump_prot(int argc, char *argv[])
{
    if (argc == 1)
B
bernard 已提交
446
    {
447 448 449 450 451 452 453 454
        rt_wlan_prot_dump();
    }
    else
    {
        return -1;
    }
    return 0;
}
B
bernard 已提交
455

456 457 458
static int wifi_debug_set_mode(int argc, char *argv[])
{
    rt_wlan_mode_t mode;
B
bernard 已提交
459

460 461
    if (argc != 3)
        return -1;
B
bernard 已提交
462

463 464 465
    if (rt_strcmp("sta", argv[1]) == 0)
    {
        mode = RT_WLAN_STATION;
B
bernard 已提交
466
    }
467
    else if (rt_strcmp("ap", argv[1]) == 0)
B
bernard 已提交
468
    {
469
        mode = RT_WLAN_AP;
B
bernard 已提交
470
    }
471 472 473 474
    else if (rt_strcmp("none", argv[1]) == 0)
    {
        mode = RT_WLAN_NONE;
    }
475 476
    else
        return -1;
B
bernard 已提交
477

478 479 480 481 482 483 484
    rt_wlan_set_mode(argv[2], mode);
    return 0;
}

static int wifi_debug_set_prot(int argc, char *argv[])
{
    if (argc != 3)
B
bernard 已提交
485
    {
486
        return -1;
B
bernard 已提交
487 488
    }

489 490 491
    rt_wlan_prot_attach(argv[2], argv[1]);
    return 0;
}
B
bernard 已提交
492

493 494 495
static int wifi_debug_set_autoconnect(int argc, char *argv[])
{
    if (argc == 2)
B
bernard 已提交
496
    {
497 498 499 500
        if (rt_strcmp(argv[1], "enable") == 0)
            rt_wlan_config_autoreconnect(RT_TRUE);
        else if (rt_strcmp(argv[1], "disable") == 0)
            rt_wlan_config_autoreconnect(RT_FALSE);
B
bernard 已提交
501
    }
502
    else
B
bernard 已提交
503
    {
504
        return -1;
B
bernard 已提交
505
    }
506 507 508 509 510 511 512 513 514
    return 0;
}

static int wifi_debug(int argc, char *argv[])
{
    int i, result = 0;
    const struct wifi_cmd_des *run_cmd = RT_NULL;

    if (argc < 3)
B
bernard 已提交
515
    {
516 517 518
        wifi_debug_help(0, RT_NULL);
        return 0;
    }
B
bernard 已提交
519

520 521 522
    for (i = 0; i < sizeof(debug_tab) / sizeof(debug_tab[0]); i++)
    {
        if (rt_strcmp(debug_tab[i].cmd, argv[2]) == 0)
B
bernard 已提交
523
        {
524 525
            run_cmd = &debug_tab[i];
            break;
B
bernard 已提交
526 527
        }
    }
528 529

    if (run_cmd == RT_NULL)
B
bernard 已提交
530
    {
531 532 533
        wifi_debug_help(0, RT_NULL);
        return 0;
    }
B
bernard 已提交
534

535 536 537
    if (run_cmd->fun != RT_NULL)
    {
        result = run_cmd->fun(argc - 2, &argv[2]);
B
bernard 已提交
538
    }
539 540

    if (result)
B
bernard 已提交
541
    {
542 543 544 545
        wifi_debug_help(argc - 2, &argv[2]);
    }
    return 0;
}
546
#endif
B
bernard 已提交
547

548 549 550 551
static int wifi_msh(int argc, char *argv[])
{
    int i, result = 0;
    const struct wifi_cmd_des *run_cmd = RT_NULL;
552

553
    if (argc == 1)
B
bernard 已提交
554
    {
555 556 557
        wifi_help(argc, argv);
        return 0;
    }
B
bernard 已提交
558

559 560 561 562
    /* find fun */
    for (i = 0; i < sizeof(cmd_tab) / sizeof(cmd_tab[0]); i++)
    {
        if (rt_strcmp(cmd_tab[i].cmd, argv[1]) == 0)
B
bernard 已提交
563
        {
564 565
            run_cmd = &cmd_tab[i];
            break;
B
bernard 已提交
566
        }
567
    }
B
bernard 已提交
568

569 570 571 572
    /* not find fun, print help */
    if (run_cmd == RT_NULL)
    {
        wifi_help(argc, argv);
B
bernard 已提交
573 574 575
        return 0;
    }

576 577 578 579 580 581 582 583 584 585
    /* run fun */
    if (run_cmd->fun != RT_NULL)
    {
        result = run_cmd->fun(argc, argv);
    }

    if (result)
    {
        wifi_help(argc, argv);
    }
B
bernard 已提交
586 587
    return 0;
}
588

马志远 已提交
589
#if defined(RT_USING_FINSH)
590
MSH_CMD_EXPORT_ALIAS(wifi_msh, wifi, wifi command);
591
#endif
592 593

#endif