From 8548dabbd6838de51b52afa5352a11cd07c02af1 Mon Sep 17 00:00:00 2001 From: Abhik Roy Date: Thu, 14 Dec 2023 20:19:22 +1100 Subject: [PATCH] fix(console): Fixed ifconfig command for PPP interface and IPv4 only --- components/console_cmd_ifconfig/README.md | 49 +++++++++++++++++++ .../console_cmd_ifconfig/console_ifconfig.c | 48 +++++++++++------- 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/components/console_cmd_ifconfig/README.md b/components/console_cmd_ifconfig/README.md index 98110d577..59afeda7a 100644 --- a/components/console_cmd_ifconfig/README.md +++ b/components/console_cmd_ifconfig/README.md @@ -62,3 +62,52 @@ For more details refer [IDF Component Manager](https://docs.espressif.com/projec ifconfig dhcp client : Enable or disable the DHCP client. Note: Disabling the DHCP server and client enables the use of static IP configuration. ``` + +## Usage: + +### Creating an ethernet interface +``` +esp> ifconfig eth init +Internal(IP101): pins: 23,18, Id: 0 +esp> ifconfig netif create 0 +``` + +### Removing an interface and deinitializing ethernet +``` +esp> ifconfig netif destroy en1 +I (8351266) ethernet_init: Ethernet(IP101[23,18]) Link Down +I (8351266) ethernet_init: Ethernet(IP101[23,18]) Stopped +esp> ifconfig eth deinit +``` + +### Set default interface +``` +esp> ifconfig en1 default +``` + +### Enable NAPT on an interface +``` +esp> ifconfig en1 napt enable +I (8467116) console_ifconfig: Setting napt enable on en1 +``` + +### Enable DHCP client on an interface +``` +esp> ifconfig en1 dhcp client enable +``` + +### Enable static IP on an interface +``` +esp> ifconfig en1 dhcp client disable +``` + +### Set static IP address +``` +esp> ifconfig en1 ip 192.168.5.2 +I (111466) console_ifconfig: Setting ip: 192.168.5.2 +esp> ifconfig en1 mask 255.255.255.0 +I (130946) console_ifconfig: Setting mask: 255.255.255.0 +esp> ifconfig en1 gw 192.168.5.1 +I (143576) console_ifconfig: Setting gw: 192.168.5.1 +I (143576) esp_netif_handlers: eth ip: 192.168.5.2, mask: 255.255.255.0, gw: 192.168.5.1 +``` diff --git a/components/console_cmd_ifconfig/console_ifconfig.c b/components/console_cmd_ifconfig/console_ifconfig.c index c7525378c..249cf512f 100644 --- a/components/console_cmd_ifconfig/console_ifconfig.c +++ b/components/console_cmd_ifconfig/console_ifconfig.c @@ -16,7 +16,9 @@ #include "argtable3/argtable3.h" #include "esp_log.h" #include "esp_netif_net_stack.h" +#if CONFIG_LWIP_IPV6 #include "lwip/ip6.h" +#endif #include "lwip/opt.h" #include "ethernet_init.h" #include "console_ifconfig.h" @@ -65,7 +67,9 @@ netif_op_t cmd_list[] = { {.name = "ifconfig", .operation = ifcfg_print_op, .arg_cnt = 1, .start_index = 0, .netif_flag = false, .help = "ifconfig: Display a list of all esp_netif interfaces along with their information"}, {.name = "ifconfig", .operation = ifcfg_print_op, .arg_cnt = 2, .start_index = 0, .netif_flag = true, .help = "ifconfig : Provide the details of the named interface"}, {.name = "default", .operation = ifcfg_basic_op, .arg_cnt = 3, .start_index = 2, .netif_flag = true, .help = "ifconfig default: Set the specified interface as the default interface"}, +#if CONFIG_LWIP_IPV6 {.name = "ip6", .operation = ifcfg_basic_op, .arg_cnt = 3, .start_index = 2, .netif_flag = true, .help = "ifconfig ip6: Enable IPv6 on the specified interface"}, +#endif {.name = "up", .operation = ifcfg_lwip_op, .arg_cnt = 3, .start_index = 2, .netif_flag = true, .help = "ifconfig up: Enable the specified interface"}, {.name = "down", .operation = ifcfg_lwip_op, .arg_cnt = 3, .start_index = 2, .netif_flag = true, .help = "ifconfig down: Disable the specified interface"}, {.name = "link", .operation = ifcfg_lwip_op, .arg_cnt = 4, .start_index = 2, .netif_flag = true, .help = "ifconfig link : Enable or disable the link of the specified interface"}, @@ -99,10 +103,10 @@ static esp_netif_t *get_esp_netif_from_ifname(char *if_name) char interface[10]; /* Get interface details and obtain the global IPv6 address */ -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0) - while ((esp_netif = esp_netif_next(esp_netif)) != NULL) { -#else +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) while ((esp_netif = esp_netif_next_unsafe(esp_netif)) != NULL) { +#else + while ((esp_netif = esp_netif_next(esp_netif)) != NULL) { #endif ret = esp_netif_get_netif_impl_name(esp_netif, interface); @@ -128,12 +132,13 @@ static esp_err_t ifcfg_basic_op(netif_op_t *self, int argc, char *argv[], esp_ne return ESP_OK; } +#if CONFIG_LWIP_IPV6 /* Enable IPv6 on this interface */ if (!strcmp("ip6", argv[self->start_index])) { ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif)); return ESP_OK; } - +#endif return ESP_FAIL; } @@ -216,10 +221,10 @@ static esp_err_t set_napt(char *if_name, bool state) /* Get interface details and own global ipv6 address */ for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) { -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0) - esp_netif = esp_netif_next(esp_netif); -#else +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) esp_netif = esp_netif_next_unsafe(esp_netif); +#else + esp_netif = esp_netif_next(esp_netif); #endif ret = esp_netif_get_netif_impl_name(esp_netif, interface); if ((ESP_FAIL == ret) || (NULL == esp_netif)) { @@ -228,9 +233,7 @@ static esp_err_t set_napt(char *if_name, bool state) } if (!strcmp(interface, if_name)) { - struct netif *lwip_netif = esp_netif_get_netif_impl(esp_netif); - ip_napt_enable_netif(lwip_netif, state); - return ESP_OK; + return esp_netif_napt_enable(esp_netif); } } @@ -303,8 +306,10 @@ static void print_iface_details(esp_netif_t *esp_netif) esp_netif_ip_info_t ip_info; uint8_t mac[NETIF_MAX_HWADDR_LEN]; char interface[10]; +#if CONFIG_LWIP_IPV6 int ip6_addrs_count = 0; esp_ip6_addr_t ip6[LWIP_IPV6_NUM_ADDRESSES]; +#endif esp_err_t ret = ESP_FAIL; esp_netif_dhcp_status_t status; @@ -326,7 +331,9 @@ static void print_iface_details(esp_netif_t *esp_netif) #else ESP_LOGI(TAG, "Interface Name: %s", interface); #endif - ESP_LOGI(TAG, "Interface Number: %d", lwip_netif->num); + if (lwip_netif != NULL) { + ESP_LOGI(TAG, "Interface Number: %d", lwip_netif->num); + } /* Print MAC address */ esp_netif_get_mac(esp_netif, mac); @@ -350,19 +357,25 @@ static void print_iface_details(esp_netif_t *esp_netif) #if IP_NAPT /* Print NAPT status*/ - ESP_LOGI(TAG, "NAPT: %s", lwip_netif->napt ? "enabled" : "disabled"); + if (lwip_netif != NULL) { + ESP_LOGI(TAG, "NAPT: %s", lwip_netif->napt ? "enabled" : "disabled"); + } #endif +#if CONFIG_LWIP_IPV6 /* Print IPv6 Address */ ip6_addrs_count = esp_netif_get_all_ip6(esp_netif, ip6); for (int j = 0; j < ip6_addrs_count; ++j) { ESP_LOGI(TAG, "IPv6 address: " IPV6STR, IPV62STR(ip6[j])); } +#endif /* Print Interface and Link Status*/ ESP_LOGI(TAG, "Interface Status: %s", esp_netif_is_netif_up(esp_netif) ? "UP" : "DOWN"); - ESP_LOGI(TAG, "Link Status: %s\n", netif_is_link_up(lwip_netif) ? "UP" : "DOWN"); - + if (lwip_netif != NULL) { + ESP_LOGI(TAG, "Link Status: %s", netif_is_link_up(lwip_netif) ? "UP" : "DOWN"); + } + ESP_LOGI(TAG, ""); } @@ -376,10 +389,10 @@ static esp_err_t ifcfg_print_op(netif_op_t *self, int argc, char *argv[], esp_ne /* Get interface details and own global ipv6 address of all interfaces */ for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) { -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0) - esp_netif = esp_netif_next(esp_netif); -#else +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) esp_netif = esp_netif_next_unsafe(esp_netif); +#else + esp_netif = esp_netif_next(esp_netif); #endif print_iface_details(esp_netif); } @@ -424,6 +437,7 @@ static esp_err_t get_netif_config(uint16_t id, esp_netif_config_t *eth_cfg_o) return ESP_FAIL; } *esp_eth_base_config = (esp_netif_inherent_config_t)ESP_NETIF_INHERENT_DEFAULT_ETH(); + esp_eth_base_config->if_key = if_key; eth_cfg_o->base = esp_eth_base_config;