From f2ad4286d61c31f60848c1c07d248c8bd3f46b42 Mon Sep 17 00:00:00 2001 From: Abhik Roy Date: Fri, 23 Feb 2024 21:32:16 +1100 Subject: [PATCH] feat(console): Added support to join pre-configured network --- components/console_cmd_wifi/CMakeLists.txt | 10 ++++++--- components/console_cmd_wifi/Kconfig.projbuild | 22 +++++++++++++++++++ components/console_cmd_wifi/README.md | 16 +++++++++----- components/console_cmd_wifi/console_wifi.c | 18 ++++++++++----- .../{ => include}/console_wifi.h | 0 5 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 components/console_cmd_wifi/Kconfig.projbuild rename components/console_cmd_wifi/{ => include}/console_wifi.h (100%) diff --git a/components/console_cmd_wifi/CMakeLists.txt b/components/console_cmd_wifi/CMakeLists.txt index 53cc8d1a8d..357f556845 100644 --- a/components/console_cmd_wifi/CMakeLists.txt +++ b/components/console_cmd_wifi/CMakeLists.txt @@ -1,4 +1,8 @@ idf_component_register(SRCS "console_wifi.c" - INCLUDE_DIRS "." - PRIV_REQUIRES esp_netif console esp_wifi - WHOLE_ARCHIVE) + INCLUDE_DIRS "include" + REQUIRES lwip + PRIV_REQUIRES esp_netif console esp_wifi esp_timer) + +if(CONFIG_WIFI_CMD_AUTO_REGISTRATION) + target_link_libraries(${COMPONENT_LIB} "-u console_cmd_wifi_register") +endif() diff --git a/components/console_cmd_wifi/Kconfig.projbuild b/components/console_cmd_wifi/Kconfig.projbuild new file mode 100644 index 0000000000..d71b5cf53e --- /dev/null +++ b/components/console_cmd_wifi/Kconfig.projbuild @@ -0,0 +1,22 @@ +menu "Wifi command Configuration" + + config WIFI_CMD_AUTO_REGISTRATION + bool "Enable Console command wifi Auto-registration" + default y + help + Enabling this allows for the autoregistration of the wifi command. + + config WIFI_CMD_NETWORK_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) to connect to. + + config WIFI_CMD_NETWORK_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) to use. + Can be left blank if the network has no security set. + +endmenu diff --git a/components/console_cmd_wifi/README.md b/components/console_cmd_wifi/README.md index 7cb00f6311..fd7a096c06 100644 --- a/components/console_cmd_wifi/README.md +++ b/components/console_cmd_wifi/README.md @@ -33,12 +33,16 @@ The component offers a console with a command that enables runtime wifi configur ESP_ERROR_CHECK(console_cmd_start()); // Start console ``` +Note: Auto-registration of a specific plugin command can be disabled from menuconfig. + ## Suported command: ### wifi: -* ```wifi help```: Prints the help text for all wifi commands -* ```wifi show network```: Scans and displays upto 10 available wifi networks. -* ```wifi show sta```: Shows the details of wifi station. -* ```wifi sta join ```: Station joins the given wifi network. -* ```wifi sta join ```: Station joins the given unsecured wifi network. -* ```wifi sta leave```: Station leaves the wifi network. +``` + wifi help: Prints the help text for all wifi commands + wifi show network/sta: Scans and displays all available wifi APs./ Shows the details of wifi station. + wifi sta join : Station joins the given wifi network. + wifi sta join : Station joins the given unsecured wifi network. + wifi sta join: Station joins pre-configured the wifi network. + wifi sta leave: Station leaves the wifi network. + ``` diff --git a/components/console_cmd_wifi/console_wifi.c b/components/console_cmd_wifi/console_wifi.c index 6ad949e627..5d94f4ed34 100644 --- a/components/console_cmd_wifi/console_wifi.c +++ b/components/console_cmd_wifi/console_wifi.c @@ -21,6 +21,7 @@ #define DEFAULT_SCAN_LIST_SIZE 10 +#if CONFIG_WIFI_CMD_AUTO_REGISTRATION /** * Static registration of this plugin is achieved by defining the plugin description * structure and placing it into .console_cmd_desc section. @@ -30,6 +31,7 @@ static const console_cmd_plugin_desc_t __attribute__((section(".console_cmd_desc .name = "console_cmd_wifi", .plugin_regd_fn = &console_cmd_wifi_register }; +#endif typedef struct wifi_op_t { char *name; @@ -55,8 +57,9 @@ static const int CONNECTED_BIT = BIT1; static wifi_op_t cmd_list[] = { {.name = "help", .operation = wifi_help_op, .arg_cnt = 2, .start_index = 1, .help = "wifi help: Prints the help text for all wifi commands"}, {.name = "show", .operation = wifi_show_op, .arg_cnt = 3, .start_index = 1, .help = "wifi show network/sta: Scans and displays all available wifi APs./ Shows the details of wifi station."}, - {.name = "join", .operation = wifi_sta_join_op, .arg_cnt = 4, .start_index = 2, .help = "wifi sta join : Station joins the given wifi network."}, - {.name = "join", .operation = wifi_sta_join_op, .arg_cnt = 5, .start_index = 2, .help = "wifi sta join : Station joins the given unsecured wifi network."}, + {.name = "join", .operation = wifi_sta_join_op, .arg_cnt = 5, .start_index = 2, .help = "wifi sta join : Station joins the given wifi network."}, + {.name = "join", .operation = wifi_sta_join_op, .arg_cnt = 4, .start_index = 2, .help = "wifi sta join : Station joins the given unsecured wifi network."}, + {.name = "join", .operation = wifi_sta_join_op, .arg_cnt = 3, .start_index = 2, .help = "wifi sta join: Station joins pre-configured the wifi network."}, {.name = "leave", .operation = wifi_sta_leave_op, .arg_cnt = 3, .start_index = 2, .help = "wifi sta leave: Station leaves the wifi network."}, }; @@ -162,7 +165,6 @@ static esp_err_t wifi_show_op(wifi_op_t *self, int argc, char *argv[]) ESP_LOGI(TAG, "Showing Joind AP details:"); ESP_LOGI(TAG, "*************************"); ESP_LOGI(TAG, "SSID: %s", wifi_config.sta.ssid); - ESP_LOGI(TAG, "Password: %s", wifi_config.sta.password); ESP_LOGI(TAG, "Channel: %d", wifi_config.sta.channel); ESP_LOGI(TAG, "bssid: %02x:%02x:%02x:%02x:%02x:%02x", wifi_config.sta.bssid[0], wifi_config.sta.bssid[1], wifi_config.sta.bssid[2], wifi_config.sta.bssid[3], @@ -184,7 +186,13 @@ static esp_err_t wifi_sta_join_op(wifi_op_t *self, int argc, char *argv[]) return ESP_FAIL; } - strlcpy((char *) wifi_config.sta.ssid, argv[self->start_index + 1], sizeof(wifi_config.sta.ssid)); + if (self->arg_cnt == 3) { + strcpy((char *) wifi_config.sta.ssid, CONFIG_WIFI_CMD_NETWORK_SSID); + strcpy((char *) wifi_config.sta.password, CONFIG_WIFI_CMD_NETWORK_PASSWORD); + } else { + strlcpy((char *) wifi_config.sta.ssid, argv[self->start_index + 1], sizeof(wifi_config.sta.ssid)); + } + if (self->arg_cnt == 5) { strlcpy((char *) wifi_config.sta.password, argv[self->start_index + 2], sizeof(wifi_config.sta.password)); } @@ -260,7 +268,7 @@ static esp_err_t do_cmd_wifi(int argc, char **argv) */ esp_err_t console_cmd_wifi_register(void) { - esp_err_t ret; + esp_err_t ret = ESP_OK; esp_console_cmd_t command = { .command = "wifi", .help = "Command for wifi configuration and monitoring\n For more info run 'wifi help'", diff --git a/components/console_cmd_wifi/console_wifi.h b/components/console_cmd_wifi/include/console_wifi.h similarity index 100% rename from components/console_cmd_wifi/console_wifi.h rename to components/console_cmd_wifi/include/console_wifi.h