|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: CC0-1.0 |
| 5 | + */ |
| 6 | +#include <esp_log.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <inttypes.h> |
| 9 | +#include "sdkconfig.h" |
| 10 | +#include "freertos/FreeRTOS.h" |
| 11 | +#include "freertos/task.h" |
| 12 | +#include "esp_chip_info.h" |
| 13 | +#include "esp_system.h" |
| 14 | +#include "ctshell_esp32.h" |
| 15 | + |
| 16 | +/* |
| 17 | + * 注册根菜单: "net" |
| 18 | + * func=NULL, attr=MENU, 这是一个纯容器 |
| 19 | + */ |
| 20 | +CTSHELL_EXPORT_CMD(net, NULL, "Network tools", CTSHELL_ATTR_MENU); |
| 21 | + |
| 22 | +/* |
| 23 | + * 注册二级具体命令: "net ip" |
| 24 | + * parent="net", 挂载在 net 下 |
| 25 | + * 这是一个叶子节点,输入 "net ip" 直接执行 |
| 26 | + */ |
| 27 | +int cmd_net_ip(int argc, char *argv[]) { |
| 28 | + ctshell_printf("IP Address : 192.168.1.100\r\n"); |
| 29 | + ctshell_printf("Subnet Mask: 255.255.255.0\r\n"); |
| 30 | + return 0; |
| 31 | +} |
| 32 | +CTSHELL_EXPORT_SUBCMD(net, ip, cmd_net_ip, "Show IP address"); |
| 33 | + |
| 34 | +/* |
| 35 | + * 注册二级菜单容器: "net wifi" |
| 36 | + * parent="net" |
| 37 | + * func=NULL, 这是一个纯容器 |
| 38 | + */ |
| 39 | +CTSHELL_EXPORT_SUBCMD(net, wifi, NULL, "WiFi management"); |
| 40 | + |
| 41 | +/* |
| 42 | + * 注册三级具体命令: "net wifi connect" |
| 43 | + * parent="net_wifi", 注意:父节点名是前两级名称的拼接 (net + _ + wifi) |
| 44 | + */ |
| 45 | +int cmd_wifi_connect(int argc, char *argv[]) { |
| 46 | + ctshell_arg_parser_t parser; |
| 47 | + ctshell_args_init(&parser, argc, argv); |
| 48 | + |
| 49 | + // 定义参数: -s <ssid> 和 -p <password> |
| 50 | + ctshell_expect_str(&parser, "-s", "ssid"); |
| 51 | + ctshell_expect_str(&parser, "-p", "password"); |
| 52 | + |
| 53 | + ctshell_args_parse(&parser); |
| 54 | + |
| 55 | + if (ctshell_has(&parser, "ssid") && ctshell_has(&parser, "password")) { |
| 56 | + char *ssid = ctshell_get_str(&parser, "ssid"); |
| 57 | + char *pwd = ctshell_get_str(&parser, "password"); |
| 58 | + |
| 59 | + ctshell_printf("Connecting to %s (Key: %s)...\r\n", ssid, pwd); |
| 60 | + } else { |
| 61 | + ctshell_printf("Usage: net wifi connect -s <ssid> -p <password>\r\n"); |
| 62 | + } |
| 63 | + return 0; |
| 64 | +} |
| 65 | +CTSHELL_EXPORT_SUBCMD(net_wifi, connect, cmd_wifi_connect, "Connect to AP"); |
| 66 | + |
| 67 | +void app_main(void) { |
| 68 | + ctshell_esp32_init(); |
| 69 | + |
| 70 | + for (;;) { |
| 71 | + vTaskDelay(portMAX_DELAY); |
| 72 | + } |
| 73 | +} |
0 commit comments