From 5fc1bb3a4454a1bbed92a8019e3b272bd5cf5825 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Nov 2018 00:40:26 +0800 Subject: [PATCH 01/91] examples: add component for protocol examples network functionality --- CMakeLists.txt | 5 + Kconfig.projbuild | 153 +++++++++++++++ component.mk | 0 connect.c | 291 +++++++++++++++++++++++++++++ include/protocol_examples_common.h | 59 ++++++ stdin_out.c | 30 +++ 6 files changed, 538 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Kconfig.projbuild create mode 100644 component.mk create mode 100644 connect.c create mode 100644 include/protocol_examples_common.h create mode 100644 stdin_out.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000000..784909da03 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,5 @@ +set(COMPONENT_SRCS "connect.c" + "stdin_out.c") +set(COMPONENT_ADD_INCLUDEDIRS "include") + +register_component() diff --git a/Kconfig.projbuild b/Kconfig.projbuild new file mode 100644 index 0000000000..fe095582b9 --- /dev/null +++ b/Kconfig.projbuild @@ -0,0 +1,153 @@ +menu "Example Connection Configuration" + + choice EXAMPLE_CONNECT_INTERFACE + prompt "Connect using" + default EXAMPLE_CONNECT_WIFI + help + Protocol examples can use Wi-Fi or Ethernet to connect to the network. + Choose which interface to use. + + config EXAMPLE_CONNECT_WIFI + bool "Wi-Fi" + + config EXAMPLE_CONNECT_ETHERNET + bool "Ethernet" + + endchoice + + config EXAMPLE_WIFI_SSID + depends on EXAMPLE_CONNECT_WIFI + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + + config EXAMPLE_WIFI_PASSWORD + depends on EXAMPLE_CONNECT_WIFI + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + Can be left blank if the network has no security set. + + + choice PHY_MODEL + prompt "Ethernet PHY" + depends on EXAMPLE_CONNECT_ETHERNET + default CONFIG_PHY_TLK110 + help + Select the PHY driver to use for the example. + + config PHY_IP101 + bool "IP101" + help + IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver. + Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it. + + config PHY_TLK110 + bool "TI TLK110 PHY" + help + Select this to use the TI TLK110 PHY + + config PHY_LAN8720 + bool "Microchip LAN8720 PHY" + help + Select this to use the Microchip LAN8720 PHY + + endchoice + + + config PHY_ADDRESS + int "PHY Address (0-31)" + depends on EXAMPLE_CONNECT_ETHERNET + default 31 + range 0 31 + help + Select the PHY Address (0-31) for the hardware configuration and PHY model. + TLK110 default 31 + LAN8720 default 1 or 0 + + + choice PHY_CLOCK_MODE + prompt "EMAC clock mode" + depends on EXAMPLE_CONNECT_ETHERNET + default PHY_CLOCK_GPIO0_IN + help + Select external (input on GPIO0) or internal (output on GPIO16 or GPIO17) clock + + + config PHY_CLOCK_GPIO0_IN + bool "GPIO0 input" + depends on EXAMPLE_CONNECT_ETHERNET + help + Input of 50MHz PHY clock on GPIO0. + + config PHY_CLOCK_GPIO0_OUT + bool "GPIO0 Output" + help + Output the internal 50MHz RMII clock on GPIO0. + + config PHY_CLOCK_GPIO16_OUT + bool "GPIO16 output" + depends on EXAMPLE_CONNECT_ETHERNET + help + Output the internal 50MHz APLL clock on GPIO16. + + config PHY_CLOCK_GPIO17_OUT + bool "GPIO17 output (inverted)" + depends on EXAMPLE_CONNECT_ETHERNET + help + Output the internal 50MHz APLL clock on GPIO17 (inverted signal). + + endchoice + + config PHY_CLOCK_MODE + int + depends on EXAMPLE_CONNECT_ETHERNET + default 0 if PHY_CLOCK_GPIO0_IN + default 1 if PHY_CLOCK_GPIO0_OUT + default 2 if PHY_CLOCK_GPIO16_OUT + default 3 if PHY_CLOCK_GPIO17_OUT + + + config PHY_USE_POWER_PIN + bool "Use PHY Power (enable/disable) pin" + depends on EXAMPLE_CONNECT_ETHERNET + default y + help + Use a GPIO "power pin" to power the PHY on/off during operation. + Consult the example README for more details + + config PHY_POWER_PIN + int "PHY Power GPIO" + depends on EXAMPLE_CONNECT_ETHERNET + default 17 + range 0 33 + depends on PHY_USE_POWER_PIN + help + GPIO number to use for powering on/off the PHY. + + config PHY_SMI_MDC_PIN + int "SMI MDC Pin" + depends on EXAMPLE_CONNECT_ETHERNET + default 23 + range 0 33 + help + GPIO number to use for SMI clock output MDC to PHY. + + config PHY_SMI_MDIO_PIN + int "SMI MDIO Pin" + depends on EXAMPLE_CONNECT_ETHERNET + default 18 + range 0 33 + help + GPIO number to use for SMI data pin MDIO to/from PHY. + + config EXAMPLE_CONNECT_IPV6 + bool "Obtain IPv6 link-local address" + default y + help + By default, examples will wait until IPv4 and IPv6 addresses are obtained. + Disable this option if the network does not support IPv6. + +endmenu diff --git a/component.mk b/component.mk new file mode 100644 index 0000000000..e69de29bb2 diff --git a/connect.c b/connect.c new file mode 100644 index 0000000000..37d9df6dbe --- /dev/null +++ b/connect.c @@ -0,0 +1,291 @@ +/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. + */ + +#include +#include "protocol_examples_common.h" +#include "sdkconfig.h" +#include "esp_event.h" +#include "esp_wifi.h" +#include "esp_eth.h" +#include "esp_log.h" +#include "tcpip_adapter.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "lwip/err.h" +#include "lwip/sys.h" + +#define GOT_IPV4_BIT BIT(0) +#define GOT_IPV6_BIT BIT(1) + +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 +#define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT) +#else +#define CONNECTED_BITS (GOT_IPV4_BIT) +#endif + +static EventGroupHandle_t s_connect_event_group; +static ip4_addr_t s_ip_addr; +static const char* s_connection_name; + +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 +static ip6_addr_t s_ipv6_addr; +#endif + + +static const char *TAG = "example_connect"; + +/* set up connection, Wi-Fi or Ethernet */ +static void start(); + +/* tear down connection, release resources */ +static void stop(); + +static void on_got_ip(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); + xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); +} + +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + +static void on_got_ipv6(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ip_event_got_ip6_t* event = (ip_event_got_ip6_t*) event_data; + memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); + xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); +} + +#endif // CONFIG_EXAMPLE_CONNECT_IPV6 + +esp_err_t example_connect() +{ + if (s_connect_event_group != NULL) { + return ESP_ERR_INVALID_STATE; + } + s_connect_event_group = xEventGroupCreate(); + start(); + xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY); + ESP_LOGI(TAG, "Connected to %s", s_connection_name); + ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_LOGI(TAG, "IPv6 address: " IPV6STR, IPV62STR(s_ipv6_addr)); +#endif + return ESP_OK; +} + +esp_err_t example_disconnect() +{ + if (s_connect_event_group == NULL) { + return ESP_ERR_INVALID_STATE; + } + vEventGroupDelete(s_connect_event_group); + s_connect_event_group = NULL; + stop(); + ESP_LOGI(TAG, "Disconnected from %s", s_connection_name); + s_connection_name = NULL; + return ESP_OK; +} + +#ifdef CONFIG_EXAMPLE_CONNECT_WIFI + +static void on_wifi_disconnect(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); + ESP_ERROR_CHECK( esp_wifi_connect() ); +} + +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + +static void on_wifi_connect(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); +} + +#endif // CONFIG_EXAMPLE_CONNECT_IPV6 + +static void start() +{ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); +#endif + + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_EXAMPLE_WIFI_SSID, + .password = CONFIG_EXAMPLE_WIFI_PASSWORD, + }, + }; + ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_ERROR_CHECK(esp_wifi_connect()); + s_connection_name = CONFIG_EXAMPLE_WIFI_SSID; +} + +static void stop() +{ + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect)); + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect)); +#endif + ESP_ERROR_CHECK(esp_wifi_stop()); + ESP_ERROR_CHECK(esp_wifi_deinit()); +} +#endif // CONFIG_EXAMPLE_CONNECT_WIFI + + +#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET + +#include "driver/gpio.h" + +#ifdef CONFIG_PHY_LAN8720 +#include "eth_phy/phy_lan8720.h" +#define DEFAULT_ETHERNET_PHY_CONFIG phy_lan8720_default_ethernet_config +#endif +#ifdef CONFIG_PHY_TLK110 +#include "eth_phy/phy_tlk110.h" +#define DEFAULT_ETHERNET_PHY_CONFIG phy_tlk110_default_ethernet_config +#elif CONFIG_PHY_IP101 +#include "eth_phy/phy_ip101.h" +#define DEFAULT_ETHERNET_PHY_CONFIG phy_ip101_default_ethernet_config +#endif + +#define PIN_PHY_POWER CONFIG_PHY_POWER_PIN +#define PIN_SMI_MDC CONFIG_PHY_SMI_MDC_PIN +#define PIN_SMI_MDIO CONFIG_PHY_SMI_MDIO_PIN + +#ifdef CONFIG_PHY_USE_POWER_PIN +/** + * @brief re-define power enable func for phy + * + * @param enable true to enable, false to disable + * + * @note This function replaces the default PHY power on/off function. + * If this GPIO is not connected on your device (and PHY is always powered), + * you can use the default PHY-specific power on/off function. + */ +static void phy_device_power_enable_via_gpio(bool enable) +{ + assert(DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable); + + if (!enable) { + DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(false); + } + + gpio_pad_select_gpio(PIN_PHY_POWER); + gpio_set_direction(PIN_PHY_POWER, GPIO_MODE_OUTPUT); + if (enable == true) { + gpio_set_level(PIN_PHY_POWER, 1); + ESP_LOGI(TAG, "Power On Ethernet PHY"); + } else { + gpio_set_level(PIN_PHY_POWER, 0); + ESP_LOGI(TAG, "Power Off Ethernet PHY"); + } + + vTaskDelay(1); // Allow the power up/down to take effect, min 300us + + if (enable) { + /* call the default PHY-specific power on function */ + DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(true); + } +} +#endif + +/** + * @brief gpio specific init + * + * @note RMII data pins are fixed in esp32: + * TXD0 <=> GPIO19 + * TXD1 <=> GPIO22 + * TX_EN <=> GPIO21 + * RXD0 <=> GPIO25 + * RXD1 <=> GPIO26 + * CLK <=> GPIO0 + * + */ +static void eth_gpio_config_rmii(void) +{ + phy_rmii_configure_data_interface_pins(); + phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO); +} + +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + +/** Event handler for Ethernet events */ +static void on_eth_event(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + ESP_LOGI(TAG, "Ethernet Link Up"); + tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH); + break; + default: + break; + } +} + +#endif // CONFIG_EXAMPLE_CONNECT_IPV6 + +static void start() +{ + eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; + config.phy_addr = CONFIG_PHY_ADDRESS; + config.gpio_config = eth_gpio_config_rmii; + config.tcpip_input = tcpip_adapter_eth_input; + config.clock_mode = CONFIG_PHY_CLOCK_MODE; + +#ifdef CONFIG_PHY_USE_POWER_PIN + /* Replace the default 'power enable' function with an example-specific one + that toggles a power GPIO. */ + config.phy_power_enable = phy_device_power_enable_via_gpio; +#endif + + ESP_ERROR_CHECK(esp_eth_init(&config)); + + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); +#endif + + ESP_ERROR_CHECK(esp_eth_enable()); + + s_connection_name = "Ethernet"; +} + +static void stop() +{ + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); + ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); +#endif + + ESP_ERROR_CHECK(esp_eth_disable()); + ESP_ERROR_CHECK(esp_eth_deinit()); +} + +#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h new file mode 100644 index 0000000000..e250db14d5 --- /dev/null +++ b/include/protocol_examples_common.h @@ -0,0 +1,59 @@ +/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include "esp_err.h" +#include "tcpip_adapter.h" + +#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET +#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_ETH +#endif + +#ifdef CONFIG_EXAMPLE_CONNECT_WIFI +#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_STA +#endif + +/** + * @brief Configure Wi-Fi or Ethernet, connect, wait for IP + * + * This all-in-one helper function is used in protocols examples to + * reduce the amount of boilerplate in the example. + * + * It is not intended to be used in real world applications. + * See examples under examples/wifi/getting_started/ and examples/ethernet/ + * for more complete Wi-Fi or Ethernet initialization code. + * + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + * + * @return ESP_OK on successful connection + */ +esp_err_t example_connect(); + +/** + * Counterpart to example_connect, de-initializes Wi-Fi or Ethernet + */ +esp_err_t example_disconnect(); + +/** + * @brief Configure stdin and stdout to use blocking I/O + * + * This helper function is used in ASIO examples. It wraps installing the + * UART driver and configuring VFS layer to use UART driver for console I/O. + */ +esp_err_t example_configure_stdin_stdout(); + +#ifdef __cplusplus +} +#endif diff --git a/stdin_out.c b/stdin_out.c new file mode 100644 index 0000000000..a4bc2cca49 --- /dev/null +++ b/stdin_out.c @@ -0,0 +1,30 @@ +/* Common functions for protocol examples, to configure stdin and stdout. + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. + */ + +#include "protocol_examples_common.h" +#include "esp_err.h" +#include "esp_vfs_dev.h" +#include "driver/uart.h" +#include "sdkconfig.h" + +esp_err_t example_configure_stdin_stdout() +{ + // Initialize VFS & UART so we can use std::cout/cin + setvbuf(stdin, NULL, _IONBF, 0); + setvbuf(stdout, NULL, _IONBF, 0); + /* Install UART driver for interrupt-driven reads and writes */ + ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM, + 256, 0, 0, NULL, 0) ); + /* Tell VFS to use UART driver */ + esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); + esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); + /* Move the caret to the beginning of the next line on '\n' */ + esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); + return ESP_OK; +} From 4d504243be469fd9f2e76a2ce6c8780be171c7dd Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Tue, 30 Apr 2019 12:51:55 +0200 Subject: [PATCH 02/91] Rename Kconfig options (components/esp32) --- stdin_out.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdin_out.c b/stdin_out.c index a4bc2cca49..cb2220db4b 100644 --- a/stdin_out.c +++ b/stdin_out.c @@ -19,10 +19,10 @@ esp_err_t example_configure_stdin_stdout() setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); /* Install UART driver for interrupt-driven reads and writes */ - ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM, + ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0) ); /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); + esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); From 5861cfd39433579756c972e9a7a09e433bd85318 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Thu, 9 May 2019 16:43:06 +0200 Subject: [PATCH 03/91] Rename Kconfig options (examples) --- Kconfig.projbuild | 44 ++++++++++++++++++++++---------------------- connect.c | 20 ++++++++++---------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index fe095582b9..4a370b5634 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -31,25 +31,25 @@ menu "Example Connection Configuration" Can be left blank if the network has no security set. - choice PHY_MODEL + choice EXAMPLE_PHY_MODEL prompt "Ethernet PHY" depends on EXAMPLE_CONNECT_ETHERNET - default CONFIG_PHY_TLK110 + default EXAMPLE_PHY_TLK110 help Select the PHY driver to use for the example. - config PHY_IP101 + config EXAMPLE_PHY_IP101 bool "IP101" help IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver. Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it. - config PHY_TLK110 + config EXAMPLE_PHY_TLK110 bool "TI TLK110 PHY" help Select this to use the TI TLK110 PHY - config PHY_LAN8720 + config EXAMPLE_PHY_LAN8720 bool "Microchip LAN8720 PHY" help Select this to use the Microchip LAN8720 PHY @@ -57,7 +57,7 @@ menu "Example Connection Configuration" endchoice - config PHY_ADDRESS + config EXAMPLE_PHY_ADDRESS int "PHY Address (0-31)" depends on EXAMPLE_CONNECT_ETHERNET default 31 @@ -68,32 +68,32 @@ menu "Example Connection Configuration" LAN8720 default 1 or 0 - choice PHY_CLOCK_MODE + choice EXAMPLE_PHY_CLOCK_MODE prompt "EMAC clock mode" depends on EXAMPLE_CONNECT_ETHERNET - default PHY_CLOCK_GPIO0_IN + default EXAMPLE_PHY_CLOCK_GPIO0_IN help Select external (input on GPIO0) or internal (output on GPIO16 or GPIO17) clock - config PHY_CLOCK_GPIO0_IN + config EXAMPLE_PHY_CLOCK_GPIO0_IN bool "GPIO0 input" depends on EXAMPLE_CONNECT_ETHERNET help Input of 50MHz PHY clock on GPIO0. - config PHY_CLOCK_GPIO0_OUT + config EXAMPLE_PHY_CLOCK_GPIO0_OUT bool "GPIO0 Output" help Output the internal 50MHz RMII clock on GPIO0. - config PHY_CLOCK_GPIO16_OUT + config EXAMPLE_PHY_CLOCK_GPIO16_OUT bool "GPIO16 output" depends on EXAMPLE_CONNECT_ETHERNET help Output the internal 50MHz APLL clock on GPIO16. - config PHY_CLOCK_GPIO17_OUT + config EXAMPLE_PHY_CLOCK_GPIO17_OUT bool "GPIO17 output (inverted)" depends on EXAMPLE_CONNECT_ETHERNET help @@ -101,16 +101,16 @@ menu "Example Connection Configuration" endchoice - config PHY_CLOCK_MODE + config EXAMPLE_PHY_CLOCK_MODE int depends on EXAMPLE_CONNECT_ETHERNET - default 0 if PHY_CLOCK_GPIO0_IN - default 1 if PHY_CLOCK_GPIO0_OUT - default 2 if PHY_CLOCK_GPIO16_OUT - default 3 if PHY_CLOCK_GPIO17_OUT + default 0 if EXAMPLE_PHY_CLOCK_GPIO0_IN + default 1 if EXAMPLE_PHY_CLOCK_GPIO0_OUT + default 2 if EXAMPLE_PHY_CLOCK_GPIO16_OUT + default 3 if EXAMPLE_PHY_CLOCK_GPIO17_OUT - config PHY_USE_POWER_PIN + config EXAMPLE_PHY_USE_POWER_PIN bool "Use PHY Power (enable/disable) pin" depends on EXAMPLE_CONNECT_ETHERNET default y @@ -118,16 +118,16 @@ menu "Example Connection Configuration" Use a GPIO "power pin" to power the PHY on/off during operation. Consult the example README for more details - config PHY_POWER_PIN + config EXAMPLE_PHY_POWER_PIN int "PHY Power GPIO" depends on EXAMPLE_CONNECT_ETHERNET default 17 range 0 33 - depends on PHY_USE_POWER_PIN + depends on EXAMPLE_PHY_USE_POWER_PIN help GPIO number to use for powering on/off the PHY. - config PHY_SMI_MDC_PIN + config EXAMPLE_PHY_SMI_MDC_PIN int "SMI MDC Pin" depends on EXAMPLE_CONNECT_ETHERNET default 23 @@ -135,7 +135,7 @@ menu "Example Connection Configuration" help GPIO number to use for SMI clock output MDC to PHY. - config PHY_SMI_MDIO_PIN + config EXAMPLE_PHY_SMI_MDIO_PIN int "SMI MDIO Pin" depends on EXAMPLE_CONNECT_ETHERNET default 18 diff --git a/connect.c b/connect.c index 37d9df6dbe..718d498f71 100644 --- a/connect.c +++ b/connect.c @@ -160,23 +160,23 @@ static void stop() #include "driver/gpio.h" -#ifdef CONFIG_PHY_LAN8720 +#ifdef CONFIG_EXAMPLE_PHY_LAN8720 #include "eth_phy/phy_lan8720.h" #define DEFAULT_ETHERNET_PHY_CONFIG phy_lan8720_default_ethernet_config #endif -#ifdef CONFIG_PHY_TLK110 +#ifdef CONFIG_EXAMPLE_PHY_TLK110 #include "eth_phy/phy_tlk110.h" #define DEFAULT_ETHERNET_PHY_CONFIG phy_tlk110_default_ethernet_config -#elif CONFIG_PHY_IP101 +#elif CONFIG_EXAMPLE_PHY_IP101 #include "eth_phy/phy_ip101.h" #define DEFAULT_ETHERNET_PHY_CONFIG phy_ip101_default_ethernet_config #endif -#define PIN_PHY_POWER CONFIG_PHY_POWER_PIN -#define PIN_SMI_MDC CONFIG_PHY_SMI_MDC_PIN -#define PIN_SMI_MDIO CONFIG_PHY_SMI_MDIO_PIN +#define PIN_PHY_POWER CONFIG_EXAMPLE_PHY_POWER_PIN +#define PIN_SMI_MDC CONFIG_EXAMPLE_PHY_SMI_MDC_PIN +#define PIN_SMI_MDIO CONFIG_EXAMPLE_PHY_SMI_MDIO_PIN -#ifdef CONFIG_PHY_USE_POWER_PIN +#ifdef CONFIG_EXAMPLE_PHY_USE_POWER_PIN /** * @brief re-define power enable func for phy * @@ -252,12 +252,12 @@ static void on_eth_event(void* arg, esp_event_base_t event_base, static void start() { eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; - config.phy_addr = CONFIG_PHY_ADDRESS; + config.phy_addr = CONFIG_EXAMPLE_PHY_ADDRESS; config.gpio_config = eth_gpio_config_rmii; config.tcpip_input = tcpip_adapter_eth_input; - config.clock_mode = CONFIG_PHY_CLOCK_MODE; + config.clock_mode = CONFIG_EXAMPLE_PHY_CLOCK_MODE; -#ifdef CONFIG_PHY_USE_POWER_PIN +#ifdef CONFIG_EXAMPLE_PHY_USE_POWER_PIN /* Replace the default 'power enable' function with an example-specific one that toggles a power GPIO. */ config.phy_power_enable = phy_device_power_enable_via_gpio; From 57ca1fa69a9afb3affeea51bca39fc6776859a8d Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Sun, 28 Apr 2019 15:38:46 +0800 Subject: [PATCH 04/91] examples: use new component registration api --- CMakeLists.txt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 784909da03..0ccb219b4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,2 @@ -set(COMPONENT_SRCS "connect.c" - "stdin_out.c") -set(COMPONENT_ADD_INCLUDEDIRS "include") - -register_component() +idf_component_register(SRCS "connect.c" "stdin_out.c" + INCLUDE_DIRS "include") From bc3825352a97c91e56c9c6974e4d9ede2ae88606 Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Wed, 10 Apr 2019 16:24:50 +0800 Subject: [PATCH 05/91] add esp_eth component --- Kconfig.projbuild | 112 +++++----------------------------- connect.c | 152 ++++++++++++---------------------------------- 2 files changed, 56 insertions(+), 208 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 4a370b5634..7de1c43591 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -30,119 +30,39 @@ menu "Example Connection Configuration" WiFi password (WPA or WPA2) for the example to use. Can be left blank if the network has no security set. - - choice EXAMPLE_PHY_MODEL - prompt "Ethernet PHY" + choice EXAMPLE_ETH_PHY_MODEL depends on EXAMPLE_CONNECT_ETHERNET - default EXAMPLE_PHY_TLK110 + prompt "Ethernet PHY Device" + default EXAMPLE_ETH_PHY_IP101 help Select the PHY driver to use for the example. - config EXAMPLE_PHY_IP101 + config EXAMPLE_ETH_PHY_IP101 bool "IP101" help IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver. Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it. - config EXAMPLE_PHY_TLK110 - bool "TI TLK110 PHY" - help - Select this to use the TI TLK110 PHY - - config EXAMPLE_PHY_LAN8720 - bool "Microchip LAN8720 PHY" - help - Select this to use the Microchip LAN8720 PHY - - endchoice - - - config EXAMPLE_PHY_ADDRESS - int "PHY Address (0-31)" - depends on EXAMPLE_CONNECT_ETHERNET - default 31 - range 0 31 - help - Select the PHY Address (0-31) for the hardware configuration and PHY model. - TLK110 default 31 - LAN8720 default 1 or 0 - - - choice EXAMPLE_PHY_CLOCK_MODE - prompt "EMAC clock mode" - depends on EXAMPLE_CONNECT_ETHERNET - default EXAMPLE_PHY_CLOCK_GPIO0_IN - help - Select external (input on GPIO0) or internal (output on GPIO16 or GPIO17) clock - - - config EXAMPLE_PHY_CLOCK_GPIO0_IN - bool "GPIO0 input" - depends on EXAMPLE_CONNECT_ETHERNET - help - Input of 50MHz PHY clock on GPIO0. - - config EXAMPLE_PHY_CLOCK_GPIO0_OUT - bool "GPIO0 Output" + config EXAMPLE_ETH_PHY_RTL8201 + bool "RTL8201/SR8201" help - Output the internal 50MHz RMII clock on GPIO0. + RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX. + Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it. - config EXAMPLE_PHY_CLOCK_GPIO16_OUT - bool "GPIO16 output" - depends on EXAMPLE_CONNECT_ETHERNET + config EXAMPLE_ETH_PHY_LAN8720 + bool "LAN8720" help - Output the internal 50MHz APLL clock on GPIO16. + LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support. + Goto https://www.microchip.com/LAN8720A for more information about it. - config EXAMPLE_PHY_CLOCK_GPIO17_OUT - bool "GPIO17 output (inverted)" - depends on EXAMPLE_CONNECT_ETHERNET + config EXAMPLE_ETH_PHY_DP83848 + bool "DP83848" help - Output the internal 50MHz APLL clock on GPIO17 (inverted signal). + DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver. + Goto http://www.ti.com/product/DP83848J for more information about it. endchoice - config EXAMPLE_PHY_CLOCK_MODE - int - depends on EXAMPLE_CONNECT_ETHERNET - default 0 if EXAMPLE_PHY_CLOCK_GPIO0_IN - default 1 if EXAMPLE_PHY_CLOCK_GPIO0_OUT - default 2 if EXAMPLE_PHY_CLOCK_GPIO16_OUT - default 3 if EXAMPLE_PHY_CLOCK_GPIO17_OUT - - - config EXAMPLE_PHY_USE_POWER_PIN - bool "Use PHY Power (enable/disable) pin" - depends on EXAMPLE_CONNECT_ETHERNET - default y - help - Use a GPIO "power pin" to power the PHY on/off during operation. - Consult the example README for more details - - config EXAMPLE_PHY_POWER_PIN - int "PHY Power GPIO" - depends on EXAMPLE_CONNECT_ETHERNET - default 17 - range 0 33 - depends on EXAMPLE_PHY_USE_POWER_PIN - help - GPIO number to use for powering on/off the PHY. - - config EXAMPLE_PHY_SMI_MDC_PIN - int "SMI MDC Pin" - depends on EXAMPLE_CONNECT_ETHERNET - default 23 - range 0 33 - help - GPIO number to use for SMI clock output MDC to PHY. - - config EXAMPLE_PHY_SMI_MDIO_PIN - int "SMI MDIO Pin" - depends on EXAMPLE_CONNECT_ETHERNET - default 18 - range 0 33 - help - GPIO number to use for SMI data pin MDIO to/from PHY. - config EXAMPLE_CONNECT_IPV6 bool "Obtain IPv6 link-local address" default y diff --git a/connect.c b/connect.c index 718d498f71..f12d40dda5 100644 --- a/connect.c +++ b/connect.c @@ -21,24 +21,23 @@ #include "lwip/err.h" #include "lwip/sys.h" -#define GOT_IPV4_BIT BIT(0) -#define GOT_IPV6_BIT BIT(1) +#define GOT_IPV4_BIT BIT(0) +#define GOT_IPV6_BIT BIT(1) #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -#define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT) +#define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT) #else -#define CONNECTED_BITS (GOT_IPV4_BIT) +#define CONNECTED_BITS (GOT_IPV4_BIT) #endif static EventGroupHandle_t s_connect_event_group; static ip4_addr_t s_ip_addr; -static const char* s_connection_name; +static const char *s_connection_name; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 static ip6_addr_t s_ipv6_addr; #endif - static const char *TAG = "example_connect"; /* set up connection, Wi-Fi or Ethernet */ @@ -47,20 +46,20 @@ static void start(); /* tear down connection, release resources */ static void stop(); -static void on_got_ip(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void on_got_ip(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { - ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); } #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -static void on_got_ipv6(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void on_got_ipv6(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { - ip_event_got_ip6_t* event = (ip_event_got_ip6_t*) event_data; + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); } @@ -98,17 +97,17 @@ esp_err_t example_disconnect() #ifdef CONFIG_EXAMPLE_CONNECT_WIFI -static void on_wifi_disconnect(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); - ESP_ERROR_CHECK( esp_wifi_connect() ); + ESP_ERROR_CHECK(esp_wifi_connect()); } #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -static void on_wifi_connect(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void on_wifi_connect(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); } @@ -155,87 +154,13 @@ static void stop() } #endif // CONFIG_EXAMPLE_CONNECT_WIFI - #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET -#include "driver/gpio.h" - -#ifdef CONFIG_EXAMPLE_PHY_LAN8720 -#include "eth_phy/phy_lan8720.h" -#define DEFAULT_ETHERNET_PHY_CONFIG phy_lan8720_default_ethernet_config -#endif -#ifdef CONFIG_EXAMPLE_PHY_TLK110 -#include "eth_phy/phy_tlk110.h" -#define DEFAULT_ETHERNET_PHY_CONFIG phy_tlk110_default_ethernet_config -#elif CONFIG_EXAMPLE_PHY_IP101 -#include "eth_phy/phy_ip101.h" -#define DEFAULT_ETHERNET_PHY_CONFIG phy_ip101_default_ethernet_config -#endif - -#define PIN_PHY_POWER CONFIG_EXAMPLE_PHY_POWER_PIN -#define PIN_SMI_MDC CONFIG_EXAMPLE_PHY_SMI_MDC_PIN -#define PIN_SMI_MDIO CONFIG_EXAMPLE_PHY_SMI_MDIO_PIN - -#ifdef CONFIG_EXAMPLE_PHY_USE_POWER_PIN -/** - * @brief re-define power enable func for phy - * - * @param enable true to enable, false to disable - * - * @note This function replaces the default PHY power on/off function. - * If this GPIO is not connected on your device (and PHY is always powered), - * you can use the default PHY-specific power on/off function. - */ -static void phy_device_power_enable_via_gpio(bool enable) -{ - assert(DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable); - - if (!enable) { - DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(false); - } - - gpio_pad_select_gpio(PIN_PHY_POWER); - gpio_set_direction(PIN_PHY_POWER, GPIO_MODE_OUTPUT); - if (enable == true) { - gpio_set_level(PIN_PHY_POWER, 1); - ESP_LOGI(TAG, "Power On Ethernet PHY"); - } else { - gpio_set_level(PIN_PHY_POWER, 0); - ESP_LOGI(TAG, "Power Off Ethernet PHY"); - } - - vTaskDelay(1); // Allow the power up/down to take effect, min 300us - - if (enable) { - /* call the default PHY-specific power on function */ - DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(true); - } -} -#endif - -/** - * @brief gpio specific init - * - * @note RMII data pins are fixed in esp32: - * TXD0 <=> GPIO19 - * TXD1 <=> GPIO22 - * TX_EN <=> GPIO21 - * RXD0 <=> GPIO25 - * RXD1 <=> GPIO26 - * CLK <=> GPIO0 - * - */ -static void eth_gpio_config_rmii(void) -{ - phy_rmii_configure_data_interface_pins(); - phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO); -} - #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 /** Event handler for Ethernet events */ -static void on_eth_event(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void on_eth_event(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { switch (event_id) { case ETHERNET_EVENT_CONNECTED: @@ -249,30 +174,33 @@ static void on_eth_event(void* arg, esp_event_base_t event_base, #endif // CONFIG_EXAMPLE_CONNECT_IPV6 +static esp_eth_handle_t s_eth_handle = NULL; +static esp_eth_mac_t *s_mac = NULL; +static esp_eth_phy_t *s_phy = NULL; + static void start() { - eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; - config.phy_addr = CONFIG_EXAMPLE_PHY_ADDRESS; - config.gpio_config = eth_gpio_config_rmii; - config.tcpip_input = tcpip_adapter_eth_input; - config.clock_mode = CONFIG_EXAMPLE_PHY_CLOCK_MODE; - -#ifdef CONFIG_EXAMPLE_PHY_USE_POWER_PIN - /* Replace the default 'power enable' function with an example-specific one - that toggles a power GPIO. */ - config.phy_power_enable = phy_device_power_enable_via_gpio; -#endif - - ESP_ERROR_CHECK(esp_eth_init(&config)); - + ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers()); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); #endif + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + s_mac = esp_eth_mac_new_esp32(&mac_config); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); +#if CONFIG_EXAMPLE_ETH_PHY_IP101 + s_phy = esp_eth_phy_new_ip101(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 + s_phy = esp_eth_phy_new_rtl8201(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_LAN8720 + s_phy = esp_eth_phy_new_lan8720(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_DP83848 + s_phy = esp_eth_phy_new_dp83848(&phy_config); +#endif + esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); - ESP_ERROR_CHECK(esp_eth_enable()); - + ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); s_connection_name = "Ethernet"; } @@ -283,9 +211,9 @@ static void stop() ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); #endif - - ESP_ERROR_CHECK(esp_eth_disable()); - ESP_ERROR_CHECK(esp_eth_deinit()); + ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); + ESP_ERROR_CHECK(s_phy->del(s_phy)); + ESP_ERROR_CHECK(s_mac->del(s_mac)); } #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET From 89855febff593b705aed78f781abe9ce754117a1 Mon Sep 17 00:00:00 2001 From: Xia Xiaotian Date: Mon, 17 Jun 2019 11:50:37 +0800 Subject: [PATCH 06/91] run WiFi on ESP32SBETA --- Kconfig.projbuild | 2 ++ connect.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 4a370b5634..36f2161168 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -145,6 +145,8 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_IPV6 bool "Obtain IPv6 link-local address" + depends on IDF_TARGET_ESP32 + # ToDo: remove once IPV6 is supported on esp32s2 default y help By default, examples will wait until IPv4 and IPv6 addresses are obtained. diff --git a/connect.c b/connect.c index 718d498f71..e3f42d1b88 100644 --- a/connect.c +++ b/connect.c @@ -12,7 +12,9 @@ #include "sdkconfig.h" #include "esp_event.h" #include "esp_wifi.h" +#if CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_eth.h" +#endif #include "esp_log.h" #include "tcpip_adapter.h" #include "freertos/FreeRTOS.h" From 7b54195233f35f20b9e6be15ae704cf05763e9c2 Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Tue, 25 Jun 2019 19:36:56 +0800 Subject: [PATCH 07/91] ethernet: support dm9051 1. move resource allocation from xxx_init to xxx_new 2. fix enabling tx checksum insertion by mistake 3. iperf example: enlarge max arguments 4. add examples for spi-ethernet Closes https://github.com/espressif/esp-idf/issues/3715 Closes https://github.com/espressif/esp-idf/issues/3711 --- Kconfig.projbuild | 149 +++++++++++++++++++++++++++++++++------------- connect.c | 29 ++++++++- 2 files changed, 134 insertions(+), 44 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 7de1c43591..f5d288e9ae 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -1,5 +1,4 @@ menu "Example Connection Configuration" - choice EXAMPLE_CONNECT_INTERFACE prompt "Connect using" default EXAMPLE_CONNECT_WIFI @@ -15,53 +14,120 @@ menu "Example Connection Configuration" endchoice - config EXAMPLE_WIFI_SSID - depends on EXAMPLE_CONNECT_WIFI - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config EXAMPLE_WIFI_PASSWORD - depends on EXAMPLE_CONNECT_WIFI - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - - choice EXAMPLE_ETH_PHY_MODEL - depends on EXAMPLE_CONNECT_ETHERNET - prompt "Ethernet PHY Device" - default EXAMPLE_ETH_PHY_IP101 - help - Select the PHY driver to use for the example. - - config EXAMPLE_ETH_PHY_IP101 - bool "IP101" + if EXAMPLE_CONNECT_WIFI + config EXAMPLE_WIFI_SSID + string "WiFi SSID" + default "myssid" help - IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver. - Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it. + SSID (network name) for the example to connect to. - config EXAMPLE_ETH_PHY_RTL8201 - bool "RTL8201/SR8201" + config EXAMPLE_WIFI_PASSWORD + string "WiFi Password" + default "mypassword" help - RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX. - Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it. + WiFi password (WPA or WPA2) for the example to use. + Can be left blank if the network has no security set. + endif - config EXAMPLE_ETH_PHY_LAN8720 - bool "LAN8720" + if EXAMPLE_CONNECT_ETHERNET + choice EXAMPLE_USE_ETHERNET + prompt "Ethernet Type" + default EXAMPLE_USE_INTERNAL_ETHERNET if IDF_TARGET_ESP32 + default EXAMPLE_USE_SPI_ETHERNET if !IDF_TARGET_ESP32 help - LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support. - Goto https://www.microchip.com/LAN8720A for more information about it. + Select which kind of Ethernet will be used in the example. - config EXAMPLE_ETH_PHY_DP83848 - bool "DP83848" - help - DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver. - Goto http://www.ti.com/product/DP83848J for more information about it. + config EXAMPLE_USE_INTERNAL_ETHERNET + depends on IDF_TARGET_ESP32 + select ETH_USE_ESP32_EMAC + bool "Internal EMAC" + help + Select internal Ethernet MAC controller. - endchoice + config EXAMPLE_USE_SPI_ETHERNET + bool "SPI Ethernet Module" + select ETH_USE_SPI_ETHERNET + help + Select external SPI-Ethernet module. + endchoice + + if EXAMPLE_USE_INTERNAL_ETHERNET + choice EXAMPLE_ETH_PHY_MODEL + prompt "Ethernet PHY Device" + default EXAMPLE_ETH_PHY_IP101 + help + Select the Ethernet PHY device to use in the example. + + config EXAMPLE_ETH_PHY_IP101 + bool "IP101" + help + IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver. + Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it. + + config EXAMPLE_ETH_PHY_RTL8201 + bool "RTL8201/SR8201" + help + RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX. + Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it. + + config EXAMPLE_ETH_PHY_LAN8720 + bool "LAN8720" + help + LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support. + Goto https://www.microchip.com/LAN8720A for more information about it. + + config EXAMPLE_ETH_PHY_DP83848 + bool "DP83848" + help + DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver. + Goto http://www.ti.com/product/DP83848J for more information about it. + endchoice + endif + + if EXAMPLE_USE_SPI_ETHERNET + config EXAMPLE_ETH_SPI_HOST + int "SPI Host Number" + range 0 2 + default 1 + help + Set the SPI host used to communicate with DM9051. + + config EXAMPLE_ETH_SCLK_GPIO + int "SPI SCLK GPIO number" + range 0 33 + default 19 + help + Set the GPIO number used by SPI SCLK. + + config EXAMPLE_ETH_MOSI_GPIO + int "SPI MOSI GPIO number" + range 0 33 + default 23 + help + Set the GPIO number used by SPI MOSI. + + config EXAMPLE_ETH_MISO_GPIO + int "SPI MISO GPIO number" + range 0 33 + default 25 + help + Set the GPIO number used by SPI MISO. + + config EXAMPLE_ETH_CS_GPIO + int "SPI CS GPIO number" + range 0 33 + default 22 + help + Set the GPIO number used by SPI CS. + + config EXAMPLE_ETH_SPI_CLOCK_MHZ + int "SPI clock speed (MHz)" + range 20 80 + default 20 + help + Set the clock speed (MHz) of SPI interface. + endif + endif config EXAMPLE_CONNECT_IPV6 bool "Obtain IPv6 link-local address" @@ -69,5 +135,4 @@ menu "Example Connection Configuration" help By default, examples will wait until IPv4 and IPv6 addresses are obtained. Disable this option if the network does not support IPv6. - endmenu diff --git a/connect.c b/connect.c index f12d40dda5..68895905a4 100644 --- a/connect.c +++ b/connect.c @@ -187,8 +187,9 @@ static void start() ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); #endif eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - s_mac = esp_eth_mac_new_esp32(&mac_config); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); +#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET + s_mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 s_phy = esp_eth_phy_new_ip101(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 @@ -197,9 +198,33 @@ static void start() s_phy = esp_eth_phy_new_lan8720(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_DP83848 s_phy = esp_eth_phy_new_dp83848(&phy_config); +#endif +#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET + gpio_install_isr_service(0); + spi_device_handle_t spi_handle = NULL; + spi_bus_config_t buscfg = { + .miso_io_num = CONFIG_EXAMPLE_ETH_MISO_GPIO, + .mosi_io_num = CONFIG_EXAMPLE_ETH_MOSI_GPIO, + .sclk_io_num = CONFIG_EXAMPLE_ETH_SCLK_GPIO, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + }; + ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); + spi_device_interface_config_t devcfg = { + .command_bits = 1, + .address_bits = 7, + .mode = 0, + .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .spics_io_num = CONFIG_EXAMPLE_ETH_CS_GPIO, + .queue_size = 20 + }; + ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); + /* dm9051 ethernet driver is based on spi driver, so need to specify the spi handle */ + mac_config.spi_hdl = spi_handle; + s_mac = esp_eth_mac_new_dm9051(&mac_config); + s_phy = esp_eth_phy_new_dm9051(&phy_config); #endif esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); - ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); s_connection_name = "Ethernet"; } From a8fc4b8399b0c8ca4c7b81fc8299be0da879f56f Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Tue, 16 Jul 2019 16:33:30 +0700 Subject: [PATCH 08/91] tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) --- connect.c | 16 ++++++++-------- include/protocol_examples_common.h | 6 +++--- stdin_out.c | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/connect.c b/connect.c index 68895905a4..8ee8aea9d4 100644 --- a/connect.c +++ b/connect.c @@ -41,10 +41,10 @@ static ip6_addr_t s_ipv6_addr; static const char *TAG = "example_connect"; /* set up connection, Wi-Fi or Ethernet */ -static void start(); +static void start(void); /* tear down connection, release resources */ -static void stop(); +static void stop(void); static void on_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) @@ -66,7 +66,7 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, #endif // CONFIG_EXAMPLE_CONNECT_IPV6 -esp_err_t example_connect() +esp_err_t example_connect(void) { if (s_connect_event_group != NULL) { return ESP_ERR_INVALID_STATE; @@ -82,7 +82,7 @@ esp_err_t example_connect() return ESP_OK; } -esp_err_t example_disconnect() +esp_err_t example_disconnect(void) { if (s_connect_event_group == NULL) { return ESP_ERR_INVALID_STATE; @@ -114,7 +114,7 @@ static void on_wifi_connect(void *arg, esp_event_base_t event_base, #endif // CONFIG_EXAMPLE_CONNECT_IPV6 -static void start() +static void start(void) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); @@ -141,7 +141,7 @@ static void start() s_connection_name = CONFIG_EXAMPLE_WIFI_SSID; } -static void stop() +static void stop(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect)); ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip)); @@ -178,7 +178,7 @@ static esp_eth_handle_t s_eth_handle = NULL; static esp_eth_mac_t *s_mac = NULL; static esp_eth_phy_t *s_phy = NULL; -static void start() +static void start(void) { ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers()); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); @@ -229,7 +229,7 @@ static void start() s_connection_name = "Ethernet"; } -static void stop() +static void stop(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index e250db14d5..d4f6e1fbbb 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -39,12 +39,12 @@ extern "C" { * * @return ESP_OK on successful connection */ -esp_err_t example_connect(); +esp_err_t example_connect(void); /** * Counterpart to example_connect, de-initializes Wi-Fi or Ethernet */ -esp_err_t example_disconnect(); +esp_err_t example_disconnect(void); /** * @brief Configure stdin and stdout to use blocking I/O @@ -52,7 +52,7 @@ esp_err_t example_disconnect(); * This helper function is used in ASIO examples. It wraps installing the * UART driver and configuring VFS layer to use UART driver for console I/O. */ -esp_err_t example_configure_stdin_stdout(); +esp_err_t example_configure_stdin_stdout(void); #ifdef __cplusplus } diff --git a/stdin_out.c b/stdin_out.c index cb2220db4b..10cc2167fe 100644 --- a/stdin_out.c +++ b/stdin_out.c @@ -13,7 +13,7 @@ #include "driver/uart.h" #include "sdkconfig.h" -esp_err_t example_configure_stdin_stdout() +esp_err_t example_configure_stdin_stdout(void) { // Initialize VFS & UART so we can use std::cout/cin setvbuf(stdin, NULL, _IONBF, 0); From 0a9a25abe373e42b3badf3bf33148cd172a3fb0a Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Wed, 18 Sep 2019 18:28:55 +0800 Subject: [PATCH 09/91] ethernet: update spi-ethernet api --- connect.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/connect.c b/connect.c index 8ee8aea9d4..38d68a3a52 100644 --- a/connect.c +++ b/connect.c @@ -219,9 +219,9 @@ static void start(void) .queue_size = 20 }; ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* dm9051 ethernet driver is based on spi driver, so need to specify the spi handle */ - mac_config.spi_hdl = spi_handle; - s_mac = esp_eth_mac_new_dm9051(&mac_config); + /* dm9051 ethernet driver is based on spi driver */ + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); s_phy = esp_eth_phy_new_dm9051(&phy_config); #endif esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); From 77eb7d3bc10c2baf63a3590f2a0ab96cb6c75f94 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 7 Oct 2019 16:46:25 +0200 Subject: [PATCH 10/91] examples: gracefully shut down Wi-Fi before restart This fixes the issue that if Wi-Fi is stopped from a shutdown handler, the code in connect.c tries to reconnect, and fails because Wi-Fi is already stopped. Also make the error check in connect.c less strict. --- connect.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/connect.c b/connect.c index 38d68a3a52..36e5326eb5 100644 --- a/connect.c +++ b/connect.c @@ -73,6 +73,7 @@ esp_err_t example_connect(void) } s_connect_event_group = xEventGroupCreate(); start(); + ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY); ESP_LOGI(TAG, "Connected to %s", s_connection_name); ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr)); @@ -101,7 +102,11 @@ static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); - ESP_ERROR_CHECK(esp_wifi_connect()); + esp_err_t err = esp_wifi_connect(); + if (err == ESP_ERR_WIFI_NOT_STARTED) { + return; + } + ESP_ERROR_CHECK(err); } #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 @@ -149,7 +154,11 @@ static void stop(void) ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect)); #endif - ESP_ERROR_CHECK(esp_wifi_stop()); + esp_err_t err = esp_wifi_stop(); + if (err == ESP_ERR_WIFI_NOT_INIT) { + return; + } + ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(esp_wifi_deinit()); } #endif // CONFIG_EXAMPLE_CONNECT_WIFI From 729d6bd840f3f65dc90791569cf9fa0833ca8cf0 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 1 Oct 2019 18:50:34 +0200 Subject: [PATCH 11/91] ethernet: support OpenCores ethernet MAC OpenCores Ethernet MAC has a relatively simple interface, and is already supported in QEMU. This makes it a good candidate for enabling network support when running IDF apps in QEMU, compared to the relatively more complex task of writing a QEMU model of ESP32 EMAC. This driver is written with QEMU in mind: it does not implement or handle things that aren't implemented or handled in the QEMU model: error flags, error interrupts. The transmit part of the driver also assumes that the TX operation is done immediately when the TX descriptor is written (which is the case with QEMU), hence waiting for the TX operation to complete is not necessary. For simplicity, the driver assumes that the peripheral register occupy the same memory range as the ESP32 EMAC registers, and the same interrupt source number is used. --- Kconfig.projbuild | 11 +++++++++++ connect.c | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index f5d288e9ae..e1fee9b611 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -49,6 +49,17 @@ menu "Example Connection Configuration" select ETH_USE_SPI_ETHERNET help Select external SPI-Ethernet module. + + config EXAMPLE_USE_OPENETH + bool "OpenCores Ethernet MAC (EXPERIMENTAL)" + select ETH_USE_OPENETH + help + When this option is enabled, the example is built with support for + OpenCores Ethernet MAC, which allows testing the example in QEMU. + Note that this option is used for internal testing purposes, and + not officially supported. Examples built with this option enabled + will not run on a real ESP32 chip. + endchoice if EXAMPLE_USE_INTERNAL_ETHERNET diff --git a/connect.c b/connect.c index 36e5326eb5..1c6bd98d19 100644 --- a/connect.c +++ b/connect.c @@ -232,7 +232,12 @@ static void start(void) eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); s_phy = esp_eth_phy_new_dm9051(&phy_config); +#elif CONFIG_EXAMPLE_USE_OPENETH + phy_config.autonego_timeout_ms = 100; + s_mac = esp_eth_mac_new_openeth(&mac_config); + s_phy = esp_eth_phy_new_dp83848(&phy_config); #endif + esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); s_connection_name = "Ethernet"; From 0f6e648bd6c0d574fef5da2fd38360c2865e08ee Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 31 Aug 2019 16:08:46 +0200 Subject: [PATCH 12/91] examples: common component initialization code to use new esp_netif instead of tcpip_adapter --- CMakeLists.txt | 4 ++- connect.c | 58 ++++++++++++++++++++++++------ include/protocol_examples_common.h | 11 ++++-- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ccb219b4a..7c4ebe34d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,2 +1,4 @@ idf_component_register(SRCS "connect.c" "stdin_out.c" - INCLUDE_DIRS "include") + INCLUDE_DIRS "include" + PRIV_REQUIRES esp_netif + ) diff --git a/connect.c b/connect.c index 0e9b6a5eae..b261e0b4f9 100644 --- a/connect.c +++ b/connect.c @@ -16,7 +16,7 @@ #include "esp_eth.h" #endif #include "esp_log.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" @@ -33,11 +33,12 @@ #endif static EventGroupHandle_t s_connect_event_group; -static ip4_addr_t s_ip_addr; +static esp_ip4_addr_t s_ip_addr; static const char *s_connection_name; +static esp_netif_t *s_example_esp_netif = NULL; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -static ip6_addr_t s_ipv6_addr; +static esp_ip6_addr_t s_ipv6_addr; #endif static const char *TAG = "example_connect"; @@ -51,6 +52,7 @@ static void stop(void); static void on_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + ESP_LOGI(TAG, "Got IP event!"); ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); @@ -61,6 +63,7 @@ static void on_got_ip(void *arg, esp_event_base_t event_base, static void on_got_ipv6(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + ESP_LOGI(TAG, "Got IPv6 event!"); ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); @@ -76,6 +79,7 @@ esp_err_t example_connect(void) s_connect_event_group = xEventGroupCreate(); start(); ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); + ESP_LOGI(TAG, "Waiting for IP"); xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY); ESP_LOGI(TAG, "Connected to %s", s_connection_name); ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr)); @@ -113,10 +117,10 @@ static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -static void on_wifi_connect(void *arg, esp_event_base_t event_base, +static void on_wifi_connect(void *esp_netif, esp_event_base_t event_base, int32_t event_id, void *event_data) { - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); + esp_netif_create_ip6_linklocal(esp_netif); } #endif // CONFIG_EXAMPLE_CONNECT_IPV6 @@ -126,10 +130,20 @@ static void start(void) wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA(); + + esp_netif_t* netif = esp_netif_new(&netif_config); + + assert(netif); + + esp_wifi_set_default_wifi_sta_handlers(netif); + + s_example_esp_netif = netif; + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); #endif @@ -162,6 +176,9 @@ static void stop(void) } ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(esp_wifi_deinit()); + + esp_netif_destroy(s_example_esp_netif); + s_example_esp_netif = NULL; } #endif // CONFIG_EXAMPLE_CONNECT_WIFI @@ -170,13 +187,13 @@ static void stop(void) #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 /** Event handler for Ethernet events */ -static void on_eth_event(void *arg, esp_event_base_t event_base, +static void on_eth_event(void *esp_netif, esp_event_base_t event_base, int32_t event_id, void *event_data) { switch (event_id) { case ETHERNET_EVENT_CONNECTED: ESP_LOGI(TAG, "Ethernet Link Up"); - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH); + esp_netif_create_ip6_linklocal(esp_netif); break; default: break; @@ -191,10 +208,19 @@ static esp_eth_phy_t *s_phy = NULL; static void start(void) { - ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers()); + esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_ETH(); + + esp_netif_t* netif = esp_netif_new(&netif_config); + + assert(netif); + + ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif)); + + s_example_esp_netif = netif; + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); #endif eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); @@ -241,7 +267,10 @@ static void start(void) #endif esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); + ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); + + esp_netif_attach(netif, s_eth_handle); s_connection_name = "Ethernet"; } @@ -255,6 +284,15 @@ static void stop(void) ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac)); + + esp_eth_clear_default_handlers(s_example_esp_netif); + esp_netif_destroy(s_example_esp_netif); + s_example_esp_netif = NULL; } #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET + +esp_netif_t *get_example_netif(void) +{ + return s_example_esp_netif; +} \ No newline at end of file diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index d4f6e1fbbb..98f67e3486 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -14,14 +14,14 @@ extern "C" { #endif #include "esp_err.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET -#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_ETH +#define EXAMPLE_INTERFACE get_example_netif() #endif #ifdef CONFIG_EXAMPLE_CONNECT_WIFI -#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_STA +#define EXAMPLE_INTERFACE get_example_netif() #endif /** @@ -54,6 +54,11 @@ esp_err_t example_disconnect(void); */ esp_err_t example_configure_stdin_stdout(void); +/** + * @brief Returns esp-netif pointer created by example_connect() + * + */ +esp_netif_t *get_example_netif(void); #ifdef __cplusplus } #endif From 9c6fdc8447d8e7c2ad621dfb9496f53fd12d9d4a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 4 Sep 2019 13:58:29 +0200 Subject: [PATCH 13/91] esp_netif and examples: using wifi driver handle, update examples and tests to pass the CI --- connect.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/connect.c b/connect.c index b261e0b4f9..f9b974fba4 100644 --- a/connect.c +++ b/connect.c @@ -12,6 +12,7 @@ #include "sdkconfig.h" #include "esp_event.h" #include "esp_wifi.h" +#include "esp_wifi_default.h" #if CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_eth.h" #endif @@ -136,7 +137,7 @@ static void start(void) assert(netif); - esp_wifi_set_default_wifi_sta_handlers(netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); s_example_esp_netif = netif; @@ -176,7 +177,7 @@ static void stop(void) } ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(esp_wifi_deinit()); - + ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_esp_netif)); esp_netif_destroy(s_example_esp_netif); s_example_esp_netif = NULL; } From e673e3bbee95064cae1ca8e3c835099bae92886d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 15 Sep 2019 19:49:45 +0200 Subject: [PATCH 14/91] esp_netif: extract wifi_netif module as an abstraction to wifi universal interface defined by if handle and callback --- connect.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/connect.c b/connect.c index f9b974fba4..b161e9a6ec 100644 --- a/connect.c +++ b/connect.c @@ -137,7 +137,8 @@ static void start(void) assert(netif); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); + esp_netif_attach_wifi_station(netif); + esp_wifi_set_default_wifi_sta_handlers(); s_example_esp_netif = netif; From c7bbe7d5b6a146382b3fe230259a823147e5f2ea Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Thu, 14 Nov 2019 12:03:14 +0800 Subject: [PATCH 15/91] ethernet: add gpio number into config structure --- Kconfig.projbuild | 54 ++++++++++++++++++++++++++++++++++++++--------- connect.c | 24 +++++++++++++-------- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 0e69979836..f8ceb1e405 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -33,7 +33,7 @@ menu "Example Connection Configuration" choice EXAMPLE_USE_ETHERNET prompt "Ethernet Type" default EXAMPLE_USE_INTERNAL_ETHERNET if IDF_TARGET_ESP32 - default EXAMPLE_USE_SPI_ETHERNET if !IDF_TARGET_ESP32 + default EXAMPLE_USE_DM9051 if !IDF_TARGET_ESP32 help Select which kind of Ethernet will be used in the example. @@ -44,9 +44,10 @@ menu "Example Connection Configuration" help Select internal Ethernet MAC controller. - config EXAMPLE_USE_SPI_ETHERNET - bool "SPI Ethernet Module" + config EXAMPLE_USE_DM9051 + bool "DM9051 Module" select ETH_USE_SPI_ETHERNET + select ETH_SPI_ETHERNET_DM9051 help Select external SPI-Ethernet module. @@ -93,51 +94,84 @@ menu "Example Connection Configuration" DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver. Goto http://www.ti.com/product/DP83848J for more information about it. endchoice + + config EXAMPLE_ETH_MDC_GPIO + int "SMI MDC GPIO number" + default 23 + help + Set the GPIO number used by SMI MDC. + + config EXAMPLE_ETH_MDIO_GPIO + int "SMI MDIO GPIO number" + default 18 + help + Set the GPIO number used by SMI MDIO. endif - if EXAMPLE_USE_SPI_ETHERNET - config EXAMPLE_ETH_SPI_HOST + if EXAMPLE_USE_DM9051 + config EXAMPLE_DM9051_SPI_HOST int "SPI Host Number" range 0 2 default 1 help Set the SPI host used to communicate with DM9051. - config EXAMPLE_ETH_SCLK_GPIO + config EXAMPLE_DM9051_SCLK_GPIO int "SPI SCLK GPIO number" range 0 33 default 19 help Set the GPIO number used by SPI SCLK. - config EXAMPLE_ETH_MOSI_GPIO + config EXAMPLE_DM9051_MOSI_GPIO int "SPI MOSI GPIO number" range 0 33 default 23 help Set the GPIO number used by SPI MOSI. - config EXAMPLE_ETH_MISO_GPIO + config EXAMPLE_DM9051_MISO_GPIO int "SPI MISO GPIO number" range 0 33 default 25 help Set the GPIO number used by SPI MISO. - config EXAMPLE_ETH_CS_GPIO + config EXAMPLE_DM9051_CS_GPIO int "SPI CS GPIO number" range 0 33 default 22 help Set the GPIO number used by SPI CS. - config EXAMPLE_ETH_SPI_CLOCK_MHZ + config EXAMPLE_DM9051_SPI_CLOCK_MHZ int "SPI clock speed (MHz)" range 20 80 default 20 help Set the clock speed (MHz) of SPI interface. + + config EXAMPLE_DM9051_INT_GPIO + int "Interrupt GPIO number" + default 4 + help + Set the GPIO number used by DM9051 interrupt. endif + + config EXAMPLE_ETH_PHY_RST_GPIO + int "PHY Reset GPIO number" + default 5 + help + Set the GPIO number used to reset PHY chip. + Set to -1 to disable PHY chip hardware reset. + + config EXAMPLE_ETH_PHY_ADDR + int "PHY Address" + range 0 31 if EXAMPLE_USE_INTERNAL_ETHERNET + range 1 1 if !EXAMPLE_USE_INTERNAL_ETHERNET + default 1 + help + Set PHY address according your board schematic. endif config EXAMPLE_CONNECT_IPV6 diff --git a/connect.c b/connect.c index b161e9a6ec..f71b323b21 100644 --- a/connect.c +++ b/connect.c @@ -18,6 +18,7 @@ #endif #include "esp_log.h" #include "esp_netif.h" +#include "driver/gpio.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" @@ -227,7 +228,11 @@ static void start(void) #endif eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET + mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; s_mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 s_phy = esp_eth_phy_new_ip101(&phy_config); @@ -238,28 +243,29 @@ static void start(void) #elif CONFIG_EXAMPLE_ETH_PHY_DP83848 s_phy = esp_eth_phy_new_dp83848(&phy_config); #endif -#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET +#elif CONFIG_EXAMPLE_USE_DM9051 gpio_install_isr_service(0); spi_device_handle_t spi_handle = NULL; spi_bus_config_t buscfg = { - .miso_io_num = CONFIG_EXAMPLE_ETH_MISO_GPIO, - .mosi_io_num = CONFIG_EXAMPLE_ETH_MOSI_GPIO, - .sclk_io_num = CONFIG_EXAMPLE_ETH_SCLK_GPIO, + .miso_io_num = CONFIG_EXAMPLE_DM9051_MISO_GPIO, + .mosi_io_num = CONFIG_EXAMPLE_DM9051_MOSI_GPIO, + .sclk_io_num = CONFIG_EXAMPLE_DM9051_SCLK_GPIO, .quadwp_io_num = -1, .quadhd_io_num = -1, }; - ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); + ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_DM9051_SPI_HOST, &buscfg, 1)); spi_device_interface_config_t devcfg = { .command_bits = 1, .address_bits = 7, .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_CS_GPIO, + .clock_speed_hz = CONFIG_EXAMPLE_DM9051_SPI_CLOCK_MHZ * 1000 * 1000, + .spics_io_num = CONFIG_EXAMPLE_DM9051_CS_GPIO, .queue_size = 20 }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); + ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_DM9051_SPI_HOST, &devcfg, &spi_handle)); /* dm9051 ethernet driver is based on spi driver */ eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + dm9051_config.int_gpio_num = CONFIG_EXAMPLE_DM9051_INT_GPIO; s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); s_phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_EXAMPLE_USE_OPENETH @@ -297,4 +303,4 @@ static void stop(void) esp_netif_t *get_example_netif(void) { return s_example_esp_netif; -} \ No newline at end of file +} From 4895f466aae82646e3e6068ca1cb853d275927b6 Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 26 Nov 2019 17:48:38 +0800 Subject: [PATCH 16/91] ethernet: move netif glue && add ref counter 1. move netif glue into single file 2. add reference counter for Ethernet driver --- connect.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/connect.c b/connect.c index f71b323b21..f33aa70514 100644 --- a/connect.c +++ b/connect.c @@ -134,7 +134,7 @@ static void start(void) esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA(); - esp_netif_t* netif = esp_netif_new(&netif_config); + esp_netif_t *netif = esp_netif_new(&netif_config); assert(netif); @@ -212,15 +212,12 @@ static esp_eth_phy_t *s_phy = NULL; static void start(void) { esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_ETH(); - - esp_netif_t* netif = esp_netif_new(&netif_config); - + esp_netif_t *netif = esp_netif_new(&netif_config); assert(netif); - - ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif)); - s_example_esp_netif = netif; - + // Set default handlers to process TCP/IP stuffs + ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif)); + // Register user defined event handers ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); @@ -274,11 +271,12 @@ static void start(void) s_phy = esp_eth_phy_new_dp83848(&phy_config); #endif + // Install Ethernet driver esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); - ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); - - esp_netif_attach(netif, s_eth_handle); + // combine driver with netif + esp_netif_attach(netif, esp_eth_new_netif_glue(s_eth_handle)); + esp_eth_start(s_eth_handle); s_connection_name = "Ethernet"; } From e55a8a76571c76d4154ea0232e72b0f392fe6d70 Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 3 Dec 2019 15:37:34 +0800 Subject: [PATCH 17/91] ethernet: warning when double start/stop --- connect.c | 1 + 1 file changed, 1 insertion(+) diff --git a/connect.c b/connect.c index f33aa70514..b3765d8786 100644 --- a/connect.c +++ b/connect.c @@ -287,6 +287,7 @@ static void stop(void) ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); #endif + ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle)); ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac)); From 6f4bac0da79fad0016ba3aa3bbe6d9902d1ab1d4 Mon Sep 17 00:00:00 2001 From: morris Date: Mon, 13 Jan 2020 21:34:23 +0800 Subject: [PATCH 18/91] ethernet: work with cache disabled add ETH_MAC_FLAG_WORK_WITH_CACHE_DISABLE flag, make ethenret driver possible to work when cache disabled Closes https://github.com/espressif/esp-idf/issues/4406 --- connect.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/connect.c b/connect.c index b3765d8786..87b218047e 100644 --- a/connect.c +++ b/connect.c @@ -208,6 +208,7 @@ static void on_eth_event(void *esp_netif, esp_event_base_t event_base, static esp_eth_handle_t s_eth_handle = NULL; static esp_eth_mac_t *s_mac = NULL; static esp_eth_phy_t *s_phy = NULL; +static void *s_eth_glue = NULL; static void start(void) { @@ -275,7 +276,8 @@ static void start(void) esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); // combine driver with netif - esp_netif_attach(netif, esp_eth_new_netif_glue(s_eth_handle)); + s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); + esp_netif_attach(netif, s_eth_glue); esp_eth_start(s_eth_handle); s_connection_name = "Ethernet"; } @@ -288,11 +290,12 @@ static void stop(void) ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); #endif ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle)); + ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); + ESP_ERROR_CHECK(esp_eth_clear_default_handlers(s_example_esp_netif)); ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac)); - esp_eth_clear_default_handlers(s_example_esp_netif); esp_netif_destroy(s_example_esp_netif); s_example_esp_netif = NULL; } From e496a8357d64f2ffe8c62728e29699f57298cde9 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 5 Mar 2020 07:38:21 +0100 Subject: [PATCH 19/91] examples: enable IPv6 in example common connect for esp32s2 Closes IDF-1115 --- Kconfig.projbuild | 2 -- 1 file changed, 2 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index f8ceb1e405..6965c46a02 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -176,8 +176,6 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_IPV6 bool "Obtain IPv6 link-local address" - depends on IDF_TARGET_ESP32 - # ToDo: remove once IPV6 is supported on esp32s2 default y help By default, examples will wait until IPv4 and IPv6 addresses are obtained. From fd314811c008f0598f7c05569195e02f43910be4 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 3 Mar 2020 13:57:32 +0100 Subject: [PATCH 20/91] examples: common connect code to ignore GOT_IP6_EVENT if comes from unrelated netif --- connect.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/connect.c b/connect.c index 87b218047e..9713edda2c 100644 --- a/connect.c +++ b/connect.c @@ -65,8 +65,12 @@ static void on_got_ip(void *arg, esp_event_base_t event_base, static void on_got_ipv6(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { - ESP_LOGI(TAG, "Got IPv6 event!"); ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + if (event->esp_netif != s_example_esp_netif) { + ESP_LOGD(TAG, "Got IPv6 from another netif: ignored"); + return; + } + ESP_LOGI(TAG, "Got IPv6 event!"); memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); } From 2dc962b3e6de79ea6fb301ce68fb17c269194791 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 6 Feb 2020 16:48:36 +0100 Subject: [PATCH 21/91] esp-netif: support for ipv6 addr types and indices --- connect.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/connect.c b/connect.c index 9713edda2c..de7e98208d 100644 --- a/connect.c +++ b/connect.c @@ -73,6 +73,8 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, ESP_LOGI(TAG, "Got IPv6 event!"); memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); + ESP_LOGI(TAG, "IPv6 address: " IPV6STR ", index: %d, type: %d", IPV62STR(s_ipv6_addr), event->ip_index, esp_ip6_get_addr_type(&s_ipv6_addr)); + } #endif // CONFIG_EXAMPLE_CONNECT_IPV6 From 70f33539019cf2c5a896041885a995f3312ac745 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 7 Feb 2020 09:19:55 +0100 Subject: [PATCH 22/91] common_connect: add support for getting multiple IPv6 addresses --- Kconfig.projbuild | 39 +++++++++++++++++++++++++++++++++++++-- connect.c | 32 +++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 6965c46a02..f47b8a6af5 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -175,9 +175,44 @@ menu "Example Connection Configuration" endif config EXAMPLE_CONNECT_IPV6 - bool "Obtain IPv6 link-local address" + bool "Obtain IPv6 address" default y help - By default, examples will wait until IPv4 and IPv6 addresses are obtained. + By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. Disable this option if the network does not support IPv6. + Choose the preferred IPv6 address type if the connection code should wait until other than + the local link address gets assigned. + + if EXAMPLE_CONNECT_IPV6 + choice EXAMPLE_CONNECT_PREFERRED_IPV6 + prompt "Preferred IPv6 Type" + default EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK + help + Select which kind of IPv6 address the connect logic waits for. + + config EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK + bool "Local Link Address" + help + Blocks until Local link address assigned. + + config EXAMPLE_CONNECT_IPV6_PREF_GLOBAL + bool "Global Address" + help + Blocks until Global address assigned. + + config EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL + bool "Site Local Address" + help + Blocks until Site link address assigned. + + config EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL + bool "Unique Local Link Address" + help + Blocks until Unique local address assigned. + + endchoice + + endif + + endmenu diff --git a/connect.c b/connect.c index de7e98208d..e845837cb6 100644 --- a/connect.c +++ b/connect.c @@ -30,6 +30,17 @@ #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 #define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT) + +#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL +#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL +#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL +#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL +#endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_... + #else #define CONNECTED_BITS (GOT_IPV4_BIT) #endif @@ -41,6 +52,16 @@ static esp_netif_t *s_example_esp_netif = NULL; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 static esp_ip6_addr_t s_ipv6_addr; + +/* types of ipv6 addresses to be displayed on ipv6 events */ +static const char *s_ipv6_addr_types[] = { + "ESP_IP6_ADDR_IS_UNKNOWN", + "ESP_IP6_ADDR_IS_GLOBAL", + "ESP_IP6_ADDR_IS_LINK_LOCAL", + "ESP_IP6_ADDR_IS_SITE_LOCAL", + "ESP_IP6_ADDR_IS_UNIQUE_LOCAL", + "ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6" + }; #endif static const char *TAG = "example_connect"; @@ -70,11 +91,12 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, ESP_LOGD(TAG, "Got IPv6 from another netif: ignored"); return; } - ESP_LOGI(TAG, "Got IPv6 event!"); - memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); - xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); - ESP_LOGI(TAG, "IPv6 address: " IPV6STR ", index: %d, type: %d", IPV62STR(s_ipv6_addr), event->ip_index, esp_ip6_get_addr_type(&s_ipv6_addr)); - + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); + ESP_LOGI(TAG, "Got IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]); + if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { + memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); + xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); + } } #endif // CONFIG_EXAMPLE_CONNECT_IPV6 From fefa2997f768be8c6bcf3898b0f4e39812a9c5c1 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 19 Mar 2020 11:45:02 +0100 Subject: [PATCH 23/91] examples: add socket stdin utils to common connect component --- CMakeLists.txt | 2 +- addr_from_stdin.c | 65 +++++++++++++++++++++++++++++++++++++++ include/addr_from_stdin.h | 44 ++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 addr_from_stdin.c create mode 100644 include/addr_from_stdin.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c4ebe34d6..5c19957ba6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "connect.c" "stdin_out.c" +idf_component_register(SRCS "connect.c" "stdin_out.c" "addr_from_stdin.c" INCLUDE_DIRS "include" PRIV_REQUIRES esp_netif ) diff --git a/addr_from_stdin.c b/addr_from_stdin.c new file mode 100644 index 0000000000..460fe9a2ec --- /dev/null +++ b/addr_from_stdin.c @@ -0,0 +1,65 @@ +#include +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "protocol_examples_common.h" + +#include "lwip/sockets.h" +#include +#include + +#define HOST_IP_SIZE 128 + +esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, struct sockaddr_in6 *dest_addr) +{ + char host_ip[HOST_IP_SIZE]; + int len; + static bool already_init = false; + + // this function could be called multiple times -> make sure UART init runs only once + if (!already_init) { + example_configure_stdin_stdout(); + already_init = true; + } + + // ignore empty or LF only string (could receive from DUT class) + do { + fgets(host_ip, HOST_IP_SIZE, stdin); + len = strlen(host_ip); + } while (len<=1 && host_ip[0] == '\n'); + host_ip[len - 1] = '\0'; + + struct addrinfo hints, *addr_list, *cur; + memset( &hints, 0, sizeof( hints ) ); + + // run getaddrinfo() to decide on the IP protocol + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = sock_type; + hints.ai_protocol = IPPROTO_TCP; + if( getaddrinfo( host_ip, NULL, &hints, &addr_list ) != 0 ) { + return ESP_FAIL; + } + for( cur = addr_list; cur != NULL; cur = cur->ai_next ) { + memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr)); + if (cur->ai_family == AF_INET) { + *ip_protocol = IPPROTO_IP; + *addr_family = AF_INET; + // add port number and return on first IPv4 match + ((struct sockaddr_in*)dest_addr)->sin_port = htons(port); + freeaddrinfo( addr_list ); + return ESP_OK; + + } else if (cur->ai_family == AF_INET6) { + *ip_protocol = IPPROTO_IPV6; + *addr_family = AF_INET6; + // add port and interface number and return on first IPv6 match + dest_addr->sin6_port = htons(port); + dest_addr->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); + freeaddrinfo( addr_list ); + return ESP_OK; + } + } + // no match found + freeaddrinfo( addr_list ); + return ESP_FAIL; +} \ No newline at end of file diff --git a/include/addr_from_stdin.h b/include/addr_from_stdin.h new file mode 100644 index 0000000000..ab5043aed6 --- /dev/null +++ b/include/addr_from_stdin.h @@ -0,0 +1,44 @@ +/* Common utilities for socket address input interface: + The API get_addr_from_stdin() is mainly used by socket client examples which read IP address from stdin (if configured). + This option is typically used in the CI, but could be enabled in the project configuration. + In that case this component is used to receive a string that is evaluated and processed to output + socket structures to open a connectio + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include "lwip/sys.h" +#include +#include + +/** + * @brief Read and evaluate IP address from stdin + * + * This API reads stdin and parses the input address using getaddrinfo() + * to fill in struct sockaddr_in6 (for both IPv4 and IPv6) used to open + * a socket. IP protocol is guessed from the IP address string. + * + * @param[in] port port number of expected connection + * @param[in] sock_type expected protocol: SOCK_STREAM or SOCK_DGRAM + * @param[out] ip_protocol resultant IP protocol: IPPROTO_IP or IPPROTO_IP6 + * @param[out] addr_family resultant address family: AF_INET or AF_INET6 + * @param[out] dest_addr sockaddr_in6 structure (for both IPv4 and IPv6) + * @return ESP_OK on success, ESP_FAIL otherwise + */ +esp_err_t get_addr_from_stdin(int port, int sock_type, + int *ip_protocol, + int *addr_family, + struct sockaddr_in6 *dest_addr); + +#ifdef __cplusplus +} +#endif \ No newline at end of file From f12116842ab1376d0c69e55d82bef56d6df3e206 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 23 Apr 2020 16:23:46 +0200 Subject: [PATCH 24/91] examples: common connect component to use both interfaces at once --- Kconfig.projbuild | 26 ++-- connect.c | 192 +++++++++++++++++++++-------- include/protocol_examples_common.h | 14 +++ 3 files changed, 170 insertions(+), 62 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index f47b8a6af5..eccf778890 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -1,18 +1,11 @@ menu "Example Connection Configuration" - choice EXAMPLE_CONNECT_INTERFACE - prompt "Connect using" - default EXAMPLE_CONNECT_WIFI - help - Protocol examples can use Wi-Fi or Ethernet to connect to the network. - Choose which interface to use. - - config EXAMPLE_CONNECT_WIFI - bool "Wi-Fi" - config EXAMPLE_CONNECT_ETHERNET - bool "Ethernet" - - endchoice + config EXAMPLE_CONNECT_WIFI + bool "connect using WiFi interface" + default y + help + Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. + Choose this option to connect with WiFi if EXAMPLE_CONNECT_WIFI config EXAMPLE_WIFI_SSID @@ -29,6 +22,13 @@ menu "Example Connection Configuration" Can be left blank if the network has no security set. endif + config EXAMPLE_CONNECT_ETHERNET + bool "connect using Ethernet interface" + default n + help + Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. + Choose this option to connect with Ethernet + if EXAMPLE_CONNECT_ETHERNET choice EXAMPLE_USE_ETHERNET prompt "Ethernet Type" diff --git a/connect.c b/connect.c index e845837cb6..653a43ea00 100644 --- a/connect.c +++ b/connect.c @@ -25,11 +25,9 @@ #include "lwip/err.h" #include "lwip/sys.h" -#define GOT_IPV4_BIT BIT(0) -#define GOT_IPV6_BIT BIT(1) - #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -#define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT) +#define MAX_IP6_ADDRS_PER_NETIF (5) +#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces*2) #if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK) #define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL @@ -42,12 +40,12 @@ #endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_... #else -#define CONNECTED_BITS (GOT_IPV4_BIT) +#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces) #endif -static EventGroupHandle_t s_connect_event_group; +static int s_active_interfaces = 0; +static xSemaphoreHandle s_semph_get_ip_addrs; static esp_ip4_addr_t s_ip_addr; -static const char *s_connection_name; static esp_netif_t *s_example_esp_netif = NULL; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 @@ -66,19 +64,72 @@ static const char *s_ipv6_addr_types[] = { static const char *TAG = "example_connect"; -/* set up connection, Wi-Fi or Ethernet */ -static void start(void); +#if CONFIG_EXAMPLE_CONNECT_WIFI +static esp_netif_t* wifi_start(void); +static void wifi_stop(void); +#endif +#if CONFIG_EXAMPLE_CONNECT_ETHERNET +static esp_netif_t* eth_start(void); +static void eth_stop(void); +#endif + +/** + * @brief Checks the netif description if it contains specified prefix. + * All netifs created withing common connect component are prefixed with the module TAG, + * so it returns true if the specified netif is owned by this module + */ +static bool is_our_netif(const char *prefix, esp_netif_t *netif) +{ + return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix)-1) == 0; +} + +/* set up connection, Wi-Fi and/or Ethernet */ +static void start(void) +{ + +#if CONFIG_EXAMPLE_CONNECT_WIFI + s_example_esp_netif = wifi_start(); + s_active_interfaces++; +#endif + +#if CONFIG_EXAMPLE_CONNECT_ETHERNET + s_example_esp_netif = eth_start(); + s_active_interfaces++; +#endif + +#if CONFIG_EXAMPLE_CONNECT_WIFI && CONFIG_EXAMPLE_CONNECT_ETHERNET + /* if both intefaces at once, clear out to indicate that multiple netifs are active */ + s_example_esp_netif = NULL; +#endif + + s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); +} /* tear down connection, release resources */ -static void stop(void); +static void stop(void) +{ +#if CONFIG_EXAMPLE_CONNECT_WIFI + wifi_stop(); + s_active_interfaces--; +#endif + +#if CONFIG_EXAMPLE_CONNECT_ETHERNET + eth_stop(); + s_active_interfaces--; +#endif +} static void on_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { - ESP_LOGI(TAG, "Got IP event!"); ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + if (!is_our_netif(TAG, event->esp_netif)) { + ESP_LOGW(TAG, "Got IPv4 from another interface \"%s\": ignored", esp_netif_get_desc(event->esp_netif)); + return; + } + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); - xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); + xSemaphoreGive(s_semph_get_ip_addrs); } #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 @@ -87,15 +138,16 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; - if (event->esp_netif != s_example_esp_netif) { - ESP_LOGD(TAG, "Got IPv6 from another netif: ignored"); + if (!is_our_netif(TAG, event->esp_netif)) { + ESP_LOGW(TAG, "Got IPv6 from another netif: ignored"); return; } esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); - ESP_LOGI(TAG, "Got IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]); + ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), + IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]); if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); - xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); + xSemaphoreGive(s_semph_get_ip_addrs); } } @@ -103,32 +155,47 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, esp_err_t example_connect(void) { - if (s_connect_event_group != NULL) { + if (s_semph_get_ip_addrs != NULL) { return ESP_ERR_INVALID_STATE; } - s_connect_event_group = xEventGroupCreate(); start(); ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); - ESP_LOGI(TAG, "Waiting for IP"); - xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to %s", s_connection_name); - ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr)); + ESP_LOGI(TAG, "Waiting for IP(s)"); + for (int i=0; idel(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac)); - esp_netif_destroy(s_example_esp_netif); + esp_netif_destroy(eth_netif); s_example_esp_netif = NULL; } @@ -334,3 +413,18 @@ esp_netif_t *get_example_netif(void) { return s_example_esp_netif; } + +esp_netif_t *get_example_netif_from_desc(const char *desc) +{ + esp_netif_t *netif = NULL; + char *expected_desc; + asprintf(&expected_desc, "%s: %s", TAG, desc); + while ((netif = esp_netif_next(netif)) != NULL) { + if (strcmp(esp_netif_get_desc(netif), expected_desc) == 0) { + free(expected_desc); + return netif; + } + } + free(expected_desc); + return netif; +} diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index 98f67e3486..859264df06 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -57,8 +57,22 @@ esp_err_t example_configure_stdin_stdout(void); /** * @brief Returns esp-netif pointer created by example_connect() * + * @note If multiple interfaces active at once, this API return NULL + * In that case the get_example_netif_from_desc() should be used + * to get esp-netif pointer based on interface description */ esp_netif_t *get_example_netif(void); + +/** + * @brief Returns esp-netif pointer created by example_connect() described by + * the supplied desc field + * + * @param desc Textual interface of created network interface, for example "sta" + * indicate default WiFi station, "eth" default Ethernet interface. + * + */ +esp_netif_t *get_example_netif_from_desc(const char *desc); + #ifdef __cplusplus } #endif From 41b21d90410c8a3661b94bc2b9a6116410b04a6f Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Mon, 18 May 2020 16:51:57 +0530 Subject: [PATCH 25/91] protocol_examples_common: keep buffering enabled on stdout - Newlib uses significantly more stack space when printing to an unbuffered stream - For examples tests, disabling buffering on stdout is not really required This issue was found during one of the OTA example test failure, root cause being stack overflow in `esp_event` task. --- stdin_out.c | 1 - 1 file changed, 1 deletion(-) diff --git a/stdin_out.c b/stdin_out.c index 10cc2167fe..8f95b8ac7d 100644 --- a/stdin_out.c +++ b/stdin_out.c @@ -17,7 +17,6 @@ esp_err_t example_configure_stdin_stdout(void) { // Initialize VFS & UART so we can use std::cout/cin setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); /* Install UART driver for interrupt-driven reads and writes */ ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0) ); From 3ccb7390990ab27793a0677d990ffd5ac3da14f2 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 22 May 2020 10:41:55 +0200 Subject: [PATCH 26/91] examples: common connect: fix build error if ipv6 disabled Declaration of local variable esp_ip6_addr_t ip6[]; was active even if IPV6 disabled in sdkconfig. Introduced in 06711c7c367cc89b3525e01c5dfe490edfe64c5a --- connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect.c b/connect.c index 653a43ea00..ed502210c8 100644 --- a/connect.c +++ b/connect.c @@ -167,7 +167,6 @@ esp_err_t example_connect(void) // iterate over active interfaces, and print out IPs of "our" netifs esp_netif_t *netif = NULL; esp_netif_ip_info_t ip; - esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF]; for (int i=0; i Date: Tue, 9 Jun 2020 17:00:47 +0800 Subject: [PATCH 27/91] vfs: support vfs uart set line endings with specified uart number --- stdin_out.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdin_out.c b/stdin_out.c index 8f95b8ac7d..b57e0e7156 100644 --- a/stdin_out.c +++ b/stdin_out.c @@ -22,8 +22,8 @@ esp_err_t example_configure_stdin_stdout(void) 256, 0, 0, NULL, 0) ); /* Tell VFS to use UART driver */ esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); - esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); + esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); + esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); return ESP_OK; } From 3a2931c367b45eeb215fec3591323b2be8be0bde Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 5 Jun 2020 14:29:32 +0200 Subject: [PATCH 28/91] examples: common connect to also support no connection flow if no inteface chosen This is useful for testing if there's no need for external network and connection --- Kconfig.projbuild | 1 + connect.c | 13 ++++++++++++- include/protocol_examples_common.h | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index eccf778890..5875ccdfa0 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -177,6 +177,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_IPV6 bool "Obtain IPv6 address" default y + depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET help By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. Disable this option if the network does not support IPv6. diff --git a/connect.c b/connect.c index ed502210c8..dfba995e81 100644 --- a/connect.c +++ b/connect.c @@ -43,9 +43,10 @@ #define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces) #endif +#define EXAMPLE_DO_CONNECT CONFIG_EXAMPLE_CONNECT_WIFI || CONFIG_EXAMPLE_CONNECT_ETHERNET + static int s_active_interfaces = 0; static xSemaphoreHandle s_semph_get_ip_addrs; -static esp_ip4_addr_t s_ip_addr; static esp_netif_t *s_example_esp_netif = NULL; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 @@ -102,7 +103,11 @@ static void start(void) s_example_esp_netif = NULL; #endif +#if EXAMPLE_DO_CONNECT + /* create semaphore if at least one interface is active */ s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); +#endif + } /* tear down connection, release resources */ @@ -119,6 +124,9 @@ static void stop(void) #endif } +#if EXAMPLE_DO_CONNECT +static esp_ip4_addr_t s_ip_addr; + static void on_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { @@ -131,6 +139,7 @@ static void on_got_ip(void *arg, esp_event_base_t event_base, memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); xSemaphoreGive(s_semph_get_ip_addrs); } +#endif #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 @@ -155,9 +164,11 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, esp_err_t example_connect(void) { +#if EXAMPLE_DO_CONNECT if (s_semph_get_ip_addrs != NULL) { return ESP_ERR_INVALID_STATE; } +#endif start(); ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); ESP_LOGI(TAG, "Waiting for IP(s)"); diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index 859264df06..8c0a0a30e0 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -24,6 +24,11 @@ extern "C" { #define EXAMPLE_INTERFACE get_example_netif() #endif +#if !defined (CONFIG_EXAMPLE_CONNECT_ETHERNET) && !defined (CONFIG_EXAMPLE_CONNECT_WIFI) +// This is useful for some tests which do not need a network connection +#define EXAMPLE_INTERFACE NULL +#endif + /** * @brief Configure Wi-Fi or Ethernet, connect, wait for IP * From 80ebc85653ce6619ca4b4f1e416595b9b9fe510d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 29 Jul 2020 10:18:34 +0200 Subject: [PATCH 29/91] examples: Common connect component: Unregister shutdown handler on disconnection To be able to connect smoothly after disconnecting, we have to unregister all handlers including shutdown handler on disconnection --- connect.c | 1 + 1 file changed, 1 insertion(+) diff --git a/connect.c b/connect.c index dfba995e81..6ef826f429 100644 --- a/connect.c +++ b/connect.c @@ -207,6 +207,7 @@ esp_err_t example_disconnect(void) vSemaphoreDelete(s_semph_get_ip_addrs); s_semph_get_ip_addrs = NULL; stop(); + ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&stop)); return ESP_OK; } From 3fb3d67e4d81d950930110d91c350e25a89d2a06 Mon Sep 17 00:00:00 2001 From: nx518 <50616530+nx518@users.noreply.github.com> Date: Fri, 6 Nov 2020 09:59:40 +0100 Subject: [PATCH 30/91] lwip: Added description to Kconfig option on IPv6 SLAAC Closes https://github.com/espressif/esp-idf/issues/6076 Merges https://github.com/espressif/esp-idf/pull/6078 --- Kconfig.projbuild | 1 + 1 file changed, 1 insertion(+) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 5875ccdfa0..ed7f51c592 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -183,6 +183,7 @@ menu "Example Connection Configuration" Disable this option if the network does not support IPv6. Choose the preferred IPv6 address type if the connection code should wait until other than the local link address gets assigned. + Consider enabling IPv6 stateless address autoconfiguration (SLAAC) in the LWIP component. if EXAMPLE_CONNECT_IPV6 choice EXAMPLE_CONNECT_PREFERRED_IPV6 From 5fdb386110678335a87e1f8638a3033c094f9ef9 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 10 Nov 2020 18:40:01 +1100 Subject: [PATCH 31/91] Whitespace: Automated whitespace fixes (large commit) Apply the pre-commit hook whitespace fixes to all files in the repo. (Line endings, blank lines at end of file, trailing whitespace) --- addr_from_stdin.c | 2 +- include/addr_from_stdin.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/addr_from_stdin.c b/addr_from_stdin.c index 460fe9a2ec..0320a27bf3 100644 --- a/addr_from_stdin.c +++ b/addr_from_stdin.c @@ -62,4 +62,4 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad // no match found freeaddrinfo( addr_list ); return ESP_FAIL; -} \ No newline at end of file +} diff --git a/include/addr_from_stdin.h b/include/addr_from_stdin.h index ab5043aed6..959a7ed099 100644 --- a/include/addr_from_stdin.h +++ b/include/addr_from_stdin.h @@ -41,4 +41,4 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, #ifdef __cplusplus } -#endif \ No newline at end of file +#endif From 02a71ffab98940f05d7644ac1b162f138b9fb6fa Mon Sep 17 00:00:00 2001 From: morris Date: Fri, 6 Nov 2020 16:06:20 +0800 Subject: [PATCH 32/91] eth: support W5500 in network examples --- Kconfig.projbuild | 42 +++++++++++++++------------ connect.c | 72 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 40 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index ed7f51c592..56a6f9c952 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -33,7 +33,7 @@ menu "Example Connection Configuration" choice EXAMPLE_USE_ETHERNET prompt "Ethernet Type" default EXAMPLE_USE_INTERNAL_ETHERNET if IDF_TARGET_ESP32 - default EXAMPLE_USE_DM9051 if !IDF_TARGET_ESP32 + default EXAMPLE_USE_W5500 help Select which kind of Ethernet will be used in the example. @@ -51,6 +51,13 @@ menu "Example Connection Configuration" help Select external SPI-Ethernet module. + config EXAMPLE_USE_W5500 + bool "W5500 Module" + select ETH_USE_SPI_ETHERNET + select ETH_SPI_ETHERNET_W5500 + help + Select external SPI-Ethernet module (W5500). + config EXAMPLE_USE_OPENETH bool "OpenCores Ethernet MAC (EXPERIMENTAL)" select ETH_USE_OPENETH @@ -108,55 +115,55 @@ menu "Example Connection Configuration" Set the GPIO number used by SMI MDIO. endif - if EXAMPLE_USE_DM9051 - config EXAMPLE_DM9051_SPI_HOST + if ETH_USE_SPI_ETHERNET + config EXAMPLE_ETH_SPI_HOST int "SPI Host Number" range 0 2 default 1 help - Set the SPI host used to communicate with DM9051. + Set the SPI host used to communicate with the SPI Ethernet Controller. - config EXAMPLE_DM9051_SCLK_GPIO + config EXAMPLE_ETH_SPI_SCLK_GPIO int "SPI SCLK GPIO number" range 0 33 - default 19 + default 20 help Set the GPIO number used by SPI SCLK. - config EXAMPLE_DM9051_MOSI_GPIO + config EXAMPLE_ETH_SPI_MOSI_GPIO int "SPI MOSI GPIO number" range 0 33 - default 23 + default 19 help Set the GPIO number used by SPI MOSI. - config EXAMPLE_DM9051_MISO_GPIO + config EXAMPLE_ETH_SPI_MISO_GPIO int "SPI MISO GPIO number" range 0 33 - default 25 + default 18 help Set the GPIO number used by SPI MISO. - config EXAMPLE_DM9051_CS_GPIO + config EXAMPLE_ETH_SPI_CS_GPIO int "SPI CS GPIO number" range 0 33 - default 22 + default 21 help Set the GPIO number used by SPI CS. - config EXAMPLE_DM9051_SPI_CLOCK_MHZ + config EXAMPLE_ETH_SPI_CLOCK_MHZ int "SPI clock speed (MHz)" range 20 80 - default 20 + default 36 help Set the clock speed (MHz) of SPI interface. - config EXAMPLE_DM9051_INT_GPIO + config EXAMPLE_ETH_SPI_INT_GPIO int "Interrupt GPIO number" default 4 help - Set the GPIO number used by DM9051 interrupt. - endif + Set the GPIO number used by the SPI Ethernet module interrupt line. + endif # ETH_USE_SPI_ETHERNET config EXAMPLE_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" @@ -168,7 +175,6 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_PHY_ADDR int "PHY Address" range 0 31 if EXAMPLE_USE_INTERNAL_ETHERNET - range 1 1 if !EXAMPLE_USE_INTERNAL_ETHERNET default 1 help Set PHY address according your board schematic. diff --git a/connect.c b/connect.c index 6ef826f429..2449ab940a 100644 --- a/connect.c +++ b/connect.c @@ -15,7 +15,10 @@ #include "esp_wifi_default.h" #if CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_eth.h" -#endif +#if CONFIG_ETH_USE_SPI_ETHERNET +#include "driver/spi_master.h" +#endif // CONFIG_ETH_USE_SPI_ETHERNET +#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_log.h" #include "esp_netif.h" #include "driver/gpio.h" @@ -60,17 +63,17 @@ static const char *s_ipv6_addr_types[] = { "ESP_IP6_ADDR_IS_SITE_LOCAL", "ESP_IP6_ADDR_IS_UNIQUE_LOCAL", "ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6" - }; +}; #endif static const char *TAG = "example_connect"; #if CONFIG_EXAMPLE_CONNECT_WIFI -static esp_netif_t* wifi_start(void); +static esp_netif_t *wifi_start(void); static void wifi_stop(void); #endif #if CONFIG_EXAMPLE_CONNECT_ETHERNET -static esp_netif_t* eth_start(void); +static esp_netif_t *eth_start(void); static void eth_stop(void); #endif @@ -81,7 +84,7 @@ static void eth_stop(void); */ static bool is_our_netif(const char *prefix, esp_netif_t *netif) { - return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix)-1) == 0; + return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0; } /* set up connection, Wi-Fi and/or Ethernet */ @@ -153,7 +156,7 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, } esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), - IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]); + IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]); if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); xSemaphoreGive(s_semph_get_ip_addrs); @@ -172,13 +175,13 @@ esp_err_t example_connect(void) start(); ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); ESP_LOGI(TAG, "Waiting for IP(s)"); - for (int i=0; i Date: Fri, 27 Nov 2020 11:44:42 +0800 Subject: [PATCH 33/91] eth: hide spi configuration when using internal emac --- Kconfig.projbuild | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 56a6f9c952..e3275ec3f4 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -30,7 +30,10 @@ menu "Example Connection Configuration" Choose this option to connect with Ethernet if EXAMPLE_CONNECT_ETHERNET - choice EXAMPLE_USE_ETHERNET + config EXAMPLE_USE_SPI_ETHERNET + bool + + choice EXAMPLE_ETHERNET_TYPE prompt "Ethernet Type" default EXAMPLE_USE_INTERNAL_ETHERNET if IDF_TARGET_ESP32 default EXAMPLE_USE_W5500 @@ -46,6 +49,7 @@ menu "Example Connection Configuration" config EXAMPLE_USE_DM9051 bool "DM9051 Module" + select EXAMPLE_USE_SPI_ETHERNET select ETH_USE_SPI_ETHERNET select ETH_SPI_ETHERNET_DM9051 help @@ -53,6 +57,7 @@ menu "Example Connection Configuration" config EXAMPLE_USE_W5500 bool "W5500 Module" + select EXAMPLE_USE_SPI_ETHERNET select ETH_USE_SPI_ETHERNET select ETH_SPI_ETHERNET_W5500 help @@ -68,7 +73,7 @@ menu "Example Connection Configuration" not officially supported. Examples built with this option enabled will not run on a real ESP32 chip. - endchoice + endchoice # EXAMPLE_ETHERNET_TYPE if EXAMPLE_USE_INTERNAL_ETHERNET choice EXAMPLE_ETH_PHY_MODEL @@ -115,7 +120,7 @@ menu "Example Connection Configuration" Set the GPIO number used by SMI MDIO. endif - if ETH_USE_SPI_ETHERNET + if EXAMPLE_USE_SPI_ETHERNET config EXAMPLE_ETH_SPI_HOST int "SPI Host Number" range 0 2 @@ -163,7 +168,7 @@ menu "Example Connection Configuration" default 4 help Set the GPIO number used by the SPI Ethernet module interrupt line. - endif # ETH_USE_SPI_ETHERNET + endif # EXAMPLE_USE_SPI_ETHERNET config EXAMPLE_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" @@ -178,7 +183,7 @@ menu "Example Connection Configuration" default 1 help Set PHY address according your board schematic. - endif + endif # EXAMPLE_CONNECT_ETHERNET config EXAMPLE_CONNECT_IPV6 bool "Obtain IPv6 address" From d5445a4f3735619e25ba29ead4ece620d33950fe Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 11 Jan 2021 14:35:29 +0800 Subject: [PATCH 34/91] example: We should not check the return value of esp_wifi_connect() in any case --- connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect.c b/connect.c index 2449ab940a..25fa8e07c9 100644 --- a/connect.c +++ b/connect.c @@ -271,7 +271,7 @@ static esp_netif_t *wifi_start(void) ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); - ESP_ERROR_CHECK(esp_wifi_connect()); + esp_wifi_connect(); return netif; } From 65120e41c2f18d5446b935a239b7076fcb9e16b8 Mon Sep 17 00:00:00 2001 From: xiehang Date: Tue, 19 Jan 2021 11:55:44 +0800 Subject: [PATCH 35/91] esp_wifi: Modify ESP_IF_WIFI_STA to WIFI_IF_STA --- connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect.c b/connect.c index 25fa8e07c9..d6d2c0f2c2 100644 --- a/connect.c +++ b/connect.c @@ -269,7 +269,7 @@ static esp_netif_t *wifi_start(void) }; ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); esp_wifi_connect(); return netif; From 17d4e1887f5aae0b2a124460fa5227dda3229fe5 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Mon, 18 Jan 2021 19:16:06 +0800 Subject: [PATCH 36/91] examples: Strip IPv6 function in example and use sockaddr_storage to replace sockaddr_in6 --- Kconfig.projbuild | 1 + addr_from_stdin.c | 11 +++++++---- include/addr_from_stdin.h | 6 +++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index e3275ec3f4..5e2af8b8c3 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -189,6 +189,7 @@ menu "Example Connection Configuration" bool "Obtain IPv6 address" default y depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET + select LWIP_IPV6 help By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. Disable this option if the network does not support IPv6. diff --git a/addr_from_stdin.c b/addr_from_stdin.c index 0320a27bf3..c907ffd96e 100644 --- a/addr_from_stdin.c +++ b/addr_from_stdin.c @@ -10,7 +10,7 @@ #define HOST_IP_SIZE 128 -esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, struct sockaddr_in6 *dest_addr) +esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, struct sockaddr_storage *dest_addr) { char host_ip[HOST_IP_SIZE]; int len; @@ -49,15 +49,18 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad freeaddrinfo( addr_list ); return ESP_OK; - } else if (cur->ai_family == AF_INET6) { + } +#if CONFIG_LWIP_IPV6 + else if (cur->ai_family == AF_INET6) { *ip_protocol = IPPROTO_IPV6; *addr_family = AF_INET6; // add port and interface number and return on first IPv6 match - dest_addr->sin6_port = htons(port); - dest_addr->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); + ((struct sockaddr_in6*)dest_addr)->sin6_port = htons(port); + ((struct sockaddr_in6*)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); freeaddrinfo( addr_list ); return ESP_OK; } +#endif } // no match found freeaddrinfo( addr_list ); diff --git a/include/addr_from_stdin.h b/include/addr_from_stdin.h index 959a7ed099..9a059c149c 100644 --- a/include/addr_from_stdin.h +++ b/include/addr_from_stdin.h @@ -24,20 +24,20 @@ extern "C" { * @brief Read and evaluate IP address from stdin * * This API reads stdin and parses the input address using getaddrinfo() - * to fill in struct sockaddr_in6 (for both IPv4 and IPv6) used to open + * to fill in struct sockaddr_storage (for both IPv4 and IPv6) used to open * a socket. IP protocol is guessed from the IP address string. * * @param[in] port port number of expected connection * @param[in] sock_type expected protocol: SOCK_STREAM or SOCK_DGRAM * @param[out] ip_protocol resultant IP protocol: IPPROTO_IP or IPPROTO_IP6 * @param[out] addr_family resultant address family: AF_INET or AF_INET6 - * @param[out] dest_addr sockaddr_in6 structure (for both IPv4 and IPv6) + * @param[out] dest_addr sockaddr_storage structure (for both IPv4 and IPv6) * @return ESP_OK on success, ESP_FAIL otherwise */ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, - struct sockaddr_in6 *dest_addr); + struct sockaddr_storage *dest_addr); #ifdef __cplusplus } From 287028402e683d2053234c9f01efdf83b78fd117 Mon Sep 17 00:00:00 2001 From: ronghulin Date: Thu, 25 Feb 2021 19:47:53 +0800 Subject: [PATCH 37/91] Bugfix: Connect example to add scan mode config Closes https://github.com/espressif/esp-idf/issues/6595 --- Kconfig.projbuild | 68 +++++++++++++++++++++++++++++++++++++++++++++++ connect.c | 36 +++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 5e2af8b8c3..afbdf08a26 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -20,6 +20,74 @@ menu "Example Connection Configuration" help WiFi password (WPA or WPA2) for the example to use. Can be left blank if the network has no security set. + + choice EXAMPLE_WIFI_SCAN_METHOD + prompt "WiFi Scan Method" + default EXAMPLE_WIFI_SCAN_METHOD_FAST + help + WiFi scan method: + + If "Fast" is selected, scan will end after find SSID match AP. + + If "All Channel" is selected, scan will end after scan all the channel. + + config EXAMPLE_WIFI_SCAN_METHOD_FAST + bool "Fast" + config EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL + bool "All Channel" + endchoice + + menu "WiFi Scan threshold" + config EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD + int "WiFi minimum rssi" + range -127 0 + + default -127 + help + The minimum rssi to accept in the scan mode. + + choice EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD + prompt "WiFi Scan auth mode threshold" + default EXAMPLE_WIFI_AUTH_OPEN + help + The weakest authmode to accept in the scan mode. + + config EXAMPLE_WIFI_AUTH_OPEN + bool "OPEN" + config EXAMPLE_WIFI_AUTH_WEP + bool "WEP" + config EXAMPLE_WIFI_AUTH_WPA_PSK + bool "WPA PSK" + config EXAMPLE_WIFI_AUTH_WPA2_PSK + bool "WPA2 PSK" + config EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK + bool "WPA WPA2 PSK" + config EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE + bool "WPA2 ENTERPRISE" + config EXAMPLE_WIFI_AUTH_WPA3_PSK + bool "WPA3 PSK" + config EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK + bool "WPA2 WPA3 PSK" + config EXAMPLE_WIFI_AUTH_WAPI_PSK + bool "WAPI PSK" + endchoice + endmenu + + choice EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD + prompt "WiFi Connect AP Sort Method" + default EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL + help + WiFi connect AP sort method: + + If "Signal" is selected, Sort matched APs in scan list by RSSI. + + If "Security" is selected, Sort matched APs in scan list by security mode. + + config EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL + bool "Signal" + config EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY + bool "Security" + endchoice endif config EXAMPLE_CONNECT_ETHERNET diff --git a/connect.c b/connect.c index d6d2c0f2c2..40bac5fec4 100644 --- a/connect.c +++ b/connect.c @@ -48,6 +48,38 @@ #define EXAMPLE_DO_CONNECT CONFIG_EXAMPLE_CONNECT_WIFI || CONFIG_EXAMPLE_CONNECT_ETHERNET +#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST +#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN +#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL +#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN +#endif + +#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL +#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL +#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY +#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY +#endif + +#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN +#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK +#endif + static int s_active_interfaces = 0; static xSemaphoreHandle s_semph_get_ip_addrs; static esp_netif_t *s_example_esp_netif = NULL; @@ -265,6 +297,10 @@ static esp_netif_t *wifi_start(void) .sta = { .ssid = CONFIG_EXAMPLE_WIFI_SSID, .password = CONFIG_EXAMPLE_WIFI_PASSWORD, + .scan_method = EXAMPLE_WIFI_SCAN_METHOD, + .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD, + .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD, + .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD, }, }; ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); From 0c4737e488e0040f5a0bb441793568d6b6d1b812 Mon Sep 17 00:00:00 2001 From: He Yin Ling Date: Wed, 9 Jun 2021 10:01:17 +0800 Subject: [PATCH 38/91] example: set example wifi scan method to all channel: in CI example test we could have runners with same SSID in the same lab. Use scan on all channel will let DUT connect to the AP with best RSSI. --- Kconfig.projbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index afbdf08a26..cd4028d9da 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -23,7 +23,7 @@ menu "Example Connection Configuration" choice EXAMPLE_WIFI_SCAN_METHOD prompt "WiFi Scan Method" - default EXAMPLE_WIFI_SCAN_METHOD_FAST + default EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL help WiFi scan method: From 3febe80976fba06256ebaa31dcf2bb0d3bceadb6 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Thu, 24 Jun 2021 11:03:11 +0200 Subject: [PATCH 39/91] examples: Update Ethernet examples to use new PHY LAN87xx init function Ethernet examples device usage and Kconfig options synchronized --- Kconfig.projbuild | 13 ++++++++++--- connect.c | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index cd4028d9da..62d6b60770 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -162,11 +162,18 @@ menu "Example Connection Configuration" RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX. Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it. - config EXAMPLE_ETH_PHY_LAN8720 - bool "LAN8720" + config EXAMPLE_ETH_PHY_LAN87XX + bool "LAN87xx" help + Below chips are supported: + LAN8710A is a small footprint MII/RMII 10/100 Ethernet Transceiver with HP Auto-MDIX and + flexPWR® Technology. LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support. - Goto https://www.microchip.com/LAN8720A for more information about it. + LAN8740A/LAN8741A is a small footprint MII/RMII 10/100 Energy Efficient Ethernet Transceiver + with HP Auto-MDIX and flexPWR® Technology. + LAN8742A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX and + flexPWR® Technology. + Goto https://www.microchip.com for more information about them. config EXAMPLE_ETH_PHY_DP83848 bool "DP83848" diff --git a/connect.c b/connect.c index 40bac5fec4..d1063b43a0 100644 --- a/connect.c +++ b/connect.c @@ -393,8 +393,8 @@ static esp_netif_t *eth_start(void) s_phy = esp_eth_phy_new_ip101(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 s_phy = esp_eth_phy_new_rtl8201(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_LAN8720 - s_phy = esp_eth_phy_new_lan8720(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX + s_phy = esp_eth_phy_new_lan87xx(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_DP83848 s_phy = esp_eth_phy_new_dp83848(&phy_config); #endif From e20c5d7fcf40ae3dbcff6fdf045d63ebd1394ee8 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sun, 15 Aug 2021 21:58:42 +0500 Subject: [PATCH 40/91] examples/protocols: fix compilation when CONFIG_EXAMPLE_USE_OPENETH=y The code checked CONFIG_ETH_USE_SPI_ETHERNET (which is usually set), but CONFIG_EXAMPLE_ETH_SPI_xxx_GPIO options are only defined if CONFIG_EXAMPLE_USE_SPI_ETHERNET is set. Fix the ifdef accordingly. Regression from aea901f0. --- connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect.c b/connect.c index d1063b43a0..3bf58549ad 100644 --- a/connect.c +++ b/connect.c @@ -398,7 +398,7 @@ static esp_netif_t *eth_start(void) #elif CONFIG_EXAMPLE_ETH_PHY_DP83848 s_phy = esp_eth_phy_new_dp83848(&phy_config); #endif -#elif CONFIG_ETH_USE_SPI_ETHERNET +#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET gpio_install_isr_service(0); spi_device_handle_t spi_handle = NULL; spi_bus_config_t buscfg = { From ad6f55d40febf0f86a781a1963361f44ecc9cbd1 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Tue, 3 Aug 2021 13:34:52 +0200 Subject: [PATCH 41/91] esp_eth: add support for multiple Ethernets modules at a time Ethernet driver events properly bounded with ESP NETIF actions to support multiple Ethernet modules used at a time. Components using Ethernet updated to conform with new API. Closes https://github.com/espressif/esp-idf/issues/7318 --- connect.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/connect.c b/connect.c index d1063b43a0..a62f67f7f9 100644 --- a/connect.c +++ b/connect.c @@ -343,7 +343,7 @@ static void on_eth_event(void *esp_netif, esp_event_base_t event_base, switch (event_id) { case ETHERNET_EVENT_CONNECTED: ESP_LOGI(TAG, "Ethernet Link Up"); - esp_netif_create_ip6_linklocal(esp_netif); + ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif)); break; default: break; @@ -355,7 +355,7 @@ static void on_eth_event(void *esp_netif, esp_event_base_t event_base, static esp_eth_handle_t s_eth_handle = NULL; static esp_eth_mac_t *s_mac = NULL; static esp_eth_phy_t *s_phy = NULL; -static void *s_eth_glue = NULL; +static esp_eth_netif_glue_handle_t s_eth_glue = NULL; static esp_netif_t *eth_start(void) { @@ -373,14 +373,7 @@ static esp_netif_t *eth_start(void) esp_netif_t *netif = esp_netif_new(&netif_config); assert(netif); free(desc); - // Set default handlers to process TCP/IP stuffs - ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif)); - // Register user defined event handers - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); -#endif + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; @@ -460,6 +453,14 @@ static esp_netif_t *eth_start(void) // combine driver with netif s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); esp_netif_attach(netif, s_eth_glue); + + // Register user defined event handers + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); +#endif + esp_eth_start(s_eth_handle); return netif; } @@ -474,7 +475,6 @@ static void eth_stop(void) #endif ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle)); ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); - ESP_ERROR_CHECK(esp_eth_clear_default_handlers(eth_netif)); ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac)); From 6f48454352e092ecc6392a765edabf37e5753915 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Fri, 13 Aug 2021 16:50:39 +0200 Subject: [PATCH 42/91] Eth_examples: added support of ESP32-S3 chip Defined SPI modules default GPIO values for ESP32-S3 SPI bus needs to be initialized with SPI_DMA_CH_AUTO option --- Kconfig.projbuild | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 62d6b60770..079cbb260d 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -1,5 +1,16 @@ menu "Example Connection Configuration" + config EXAMPLE_GPIO_RANGE_MIN + int + default 0 + + config EXAMPLE_GPIO_RANGE_MAX + int + default 33 if IDF_TARGET_ESP32 + default 46 if IDF_TARGET_ESP32S2 + default 19 if IDF_TARGET_ESP32C3 + default 48 if IDF_TARGET_ESP32S3 + config EXAMPLE_CONNECT_WIFI bool "connect using WiFi interface" default y @@ -184,12 +195,14 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_MDC_GPIO int "SMI MDC GPIO number" + range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX default 23 help Set the GPIO number used by SMI MDC. config EXAMPLE_ETH_MDIO_GPIO int "SMI MDIO GPIO number" + range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX default 18 help Set the GPIO number used by SMI MDIO. @@ -205,41 +218,42 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_SPI_SCLK_GPIO int "SPI SCLK GPIO number" - range 0 33 - default 20 + range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + default 14 help Set the GPIO number used by SPI SCLK. config EXAMPLE_ETH_SPI_MOSI_GPIO int "SPI MOSI GPIO number" - range 0 33 - default 19 + range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + default 13 help Set the GPIO number used by SPI MOSI. config EXAMPLE_ETH_SPI_MISO_GPIO int "SPI MISO GPIO number" - range 0 33 - default 18 + range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + default 12 help Set the GPIO number used by SPI MISO. config EXAMPLE_ETH_SPI_CS_GPIO int "SPI CS GPIO number" - range 0 33 - default 21 + range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + default 15 help Set the GPIO number used by SPI CS. config EXAMPLE_ETH_SPI_CLOCK_MHZ int "SPI clock speed (MHz)" - range 20 80 + range 5 80 default 36 help Set the clock speed (MHz) of SPI interface. config EXAMPLE_ETH_SPI_INT_GPIO int "Interrupt GPIO number" + range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX default 4 help Set the GPIO number used by the SPI Ethernet module interrupt line. @@ -247,6 +261,7 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" + range -1 EXAMPLE_GPIO_RANGE_MAX default 5 help Set the GPIO number used to reset PHY chip. From ab704f463492de5570e7fc4e6047cfc91b4690fa Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Fri, 5 Nov 2021 15:38:25 +0100 Subject: [PATCH 43/91] Build & config: Remove leftover files from the unsupported "make" build system --- component.mk | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 component.mk diff --git a/component.mk b/component.mk deleted file mode 100644 index e69de29bb2..0000000000 From 2beb7cce9d2714dfed3425bc34104d970edef477 Mon Sep 17 00:00:00 2001 From: Fischerauer Christian Date: Tue, 16 Nov 2021 16:18:43 +0100 Subject: [PATCH 44/91] esp_eth: rework KSZ80xx implementation and add more KSZ80xx PHYs * add support for KSZ8001, KSZ8021, KSZ8031, KSZ8051 and KSZ8061 * remove duplicate code * simplify architecture to make the code base extensible (for future work) --- Kconfig.projbuild | 9 +++++++++ connect.c | 2 ++ 2 files changed, 11 insertions(+) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 079cbb260d..867863e48c 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -191,6 +191,15 @@ menu "Example Connection Configuration" help DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver. Goto http://www.ti.com/product/DP83848J for more information about it. + + config EXAMPLE_ETH_PHY_KSZ80XX + bool "KSZ80xx" + help + With the KSZ80xx series, Microchip offers single-chip 10BASE-T/100BASE-TX + Ethernet Physical Layer Tranceivers (PHY). + The following chips are supported: KSZ8001, KSZ8021, KSZ8031, KSZ8041, + KSZ8051, KSZ8061, KSZ8081, KSZ8091 + Goto https://www.microchip.com for more information about them. endchoice config EXAMPLE_ETH_MDC_GPIO diff --git a/connect.c b/connect.c index 0f2dc3f9c4..b3b03940f0 100644 --- a/connect.c +++ b/connect.c @@ -390,6 +390,8 @@ static esp_netif_t *eth_start(void) s_phy = esp_eth_phy_new_lan87xx(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_DP83848 s_phy = esp_eth_phy_new_dp83848(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX + s_phy = esp_eth_phy_new_ksz80xx(&phy_config); #endif #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET gpio_install_isr_service(0); From 90018b3098467c2e0bc8f2a1ffe5dda5489615fe Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Tue, 8 Feb 2022 17:39:38 +0800 Subject: [PATCH 45/91] freertos: Remove legacy data types This commit removes the usage of all legacy FreeRTOS data types that are exposed via configENABLE_BACKWARD_COMPATIBILITY. Legacy types can still be used by enabling CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY. --- connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect.c b/connect.c index b3b03940f0..50925b76a8 100644 --- a/connect.c +++ b/connect.c @@ -81,7 +81,7 @@ #endif static int s_active_interfaces = 0; -static xSemaphoreHandle s_semph_get_ip_addrs; +static SemaphoreHandle_t s_semph_get_ip_addrs; static esp_netif_t *s_example_esp_netif = NULL; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 From e4466969bd113b0124eff0dc2492432abb9ebbf5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 15 Dec 2021 16:30:29 +0100 Subject: [PATCH 46/91] esp_eth: Make EMAC DMA burst size configurable Merges https://github.com/espressif/esp-idf/pull/7874 Closes https://github.com/espressif/esp-idf/issues/7380 --- connect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connect.c b/connect.c index 50925b76a8..fb357dea66 100644 --- a/connect.c +++ b/connect.c @@ -379,8 +379,8 @@ static esp_netif_t *eth_start(void) phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET - mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + mac_config.esp32_emac.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + mac_config.esp32_emac.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; s_mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 s_phy = esp_eth_phy_new_ip101(&phy_config); From c39a74333a9204201763d48c5979c5cb57ee4ba4 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 24 Jan 2022 15:40:11 +0100 Subject: [PATCH 47/91] esp_eth: Update esp32's EMAC API to decouple driver and vendor config --- connect.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/connect.c b/connect.c index fb357dea66..c54deae670 100644 --- a/connect.c +++ b/connect.c @@ -379,9 +379,10 @@ static esp_netif_t *eth_start(void) phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET - mac_config.esp32_emac.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - mac_config.esp32_emac.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; - s_mac = esp_eth_mac_new_esp32(&mac_config); + eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); + esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 s_phy = esp_eth_phy_new_ip101(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 From 0a628f4d6abb6d6bcf12aeca9f28e58a2872d6db Mon Sep 17 00:00:00 2001 From: Omar Chebib Date: Fri, 28 Jan 2022 19:47:04 +0800 Subject: [PATCH 48/91] G0: target component (components/esp32*) doesn't depend on driver anymore --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c19957ba6..5f9317e17d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register(SRCS "connect.c" "stdin_out.c" "addr_from_stdin.c" INCLUDE_DIRS "include" - PRIV_REQUIRES esp_netif + PRIV_REQUIRES esp_netif driver ) From 9ddf6921fa06fd636d030ac35db205ede2d8670c Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Thu, 31 Mar 2022 14:52:27 +0200 Subject: [PATCH 49/91] examples: added ESP-NETIF L2 TAP example --- connect.c | 6 ++++++ include/protocol_examples_common.h | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/connect.c b/connect.c index c54deae670..85878d78b0 100644 --- a/connect.c +++ b/connect.c @@ -479,6 +479,7 @@ static void eth_stop(void) ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle)); ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); + s_eth_handle = NULL; ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac)); @@ -486,6 +487,11 @@ static void eth_stop(void) s_example_esp_netif = NULL; } +esp_eth_handle_t get_example_eth_handle(void) +{ + return s_eth_handle; +} + #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET esp_netif_t *get_example_netif(void) diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index 8c0a0a30e0..7afc78a745 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -78,6 +78,15 @@ esp_netif_t *get_example_netif(void); */ esp_netif_t *get_example_netif_from_desc(const char *desc); +#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET +/** + * @brief Get the example Ethernet driver handle + * + * @return esp_eth_handle_t + */ +esp_eth_handle_t get_example_eth_handle(void); +#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET + #ifdef __cplusplus } #endif From fda74d673fb4783c371919f306c9c9a4be68615a Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Wed, 20 Apr 2022 11:02:26 +0200 Subject: [PATCH 50/91] Examples: common source for GPIO range in Kconfigs defined --- Kconfig.projbuild | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 867863e48c..86ee4f140c 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -1,15 +1,6 @@ menu "Example Connection Configuration" - config EXAMPLE_GPIO_RANGE_MIN - int - default 0 - - config EXAMPLE_GPIO_RANGE_MAX - int - default 33 if IDF_TARGET_ESP32 - default 46 if IDF_TARGET_ESP32S2 - default 19 if IDF_TARGET_ESP32C3 - default 48 if IDF_TARGET_ESP32S3 + orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps" config EXAMPLE_CONNECT_WIFI bool "connect using WiFi interface" @@ -204,14 +195,14 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_MDC_GPIO int "SMI MDC GPIO number" - range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 23 help Set the GPIO number used by SMI MDC. config EXAMPLE_ETH_MDIO_GPIO int "SMI MDIO GPIO number" - range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 18 help Set the GPIO number used by SMI MDIO. @@ -227,28 +218,28 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_SPI_SCLK_GPIO int "SPI SCLK GPIO number" - range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 14 help Set the GPIO number used by SPI SCLK. config EXAMPLE_ETH_SPI_MOSI_GPIO int "SPI MOSI GPIO number" - range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 13 help Set the GPIO number used by SPI MOSI. config EXAMPLE_ETH_SPI_MISO_GPIO int "SPI MISO GPIO number" - range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX default 12 help Set the GPIO number used by SPI MISO. config EXAMPLE_ETH_SPI_CS_GPIO int "SPI CS GPIO number" - range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 15 help Set the GPIO number used by SPI CS. @@ -262,7 +253,7 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_SPI_INT_GPIO int "Interrupt GPIO number" - range EXAMPLE_GPIO_RANGE_MIN EXAMPLE_GPIO_RANGE_MAX + range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX default 4 help Set the GPIO number used by the SPI Ethernet module interrupt line. @@ -270,7 +261,7 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" - range -1 EXAMPLE_GPIO_RANGE_MAX + range -1 ENV_GPIO_OUT_RANGE_MAX default 5 help Set the GPIO number used to reset PHY chip. From cd9f12cea5721172b117816a44153c5b32e6eca4 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 2 May 2022 15:47:05 +0200 Subject: [PATCH 51/91] esp-netif: Make dependency on esp-eth optional * esp-netif to optionally depend on esp-eth (only for l2tap config) * esp_eth.h now includes the original ethernet header and the ethernet-netif glue layer * Updated examples and test to explicitely use esp-eth dependency if needed --- CMakeLists.txt | 2 +- include/protocol_examples_common.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f9317e17d..5f903a75d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register(SRCS "connect.c" "stdin_out.c" "addr_from_stdin.c" INCLUDE_DIRS "include" - PRIV_REQUIRES esp_netif driver + PRIV_REQUIRES esp_netif driver esp_eth ) diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index 7afc78a745..d43e94e0d0 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -15,6 +15,7 @@ extern "C" { #include "esp_err.h" #include "esp_netif.h" +#include "esp_eth.h" #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET #define EXAMPLE_INTERFACE get_example_netif() From 8d8228fee2057d17d424cb2d98981d33b9f065c3 Mon Sep 17 00:00:00 2001 From: Harshit Malpani Date: Tue, 14 Jun 2022 12:32:16 +0530 Subject: [PATCH 52/91] Add config option to set stack size for emac_rx task --- Kconfig.projbuild | 6 ++++++ connect.c | 1 + 2 files changed, 7 insertions(+) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 86ee4f140c..7182563eae 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -100,6 +100,12 @@ menu "Example Connection Configuration" Choose this option to connect with Ethernet if EXAMPLE_CONNECT_ETHERNET + config EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE + int "emac_rx task stack size" + default 2048 + help + This set stack size for emac_rx task + config EXAMPLE_USE_SPI_ETHERNET bool diff --git a/connect.c b/connect.c index 85878d78b0..5f648bc59b 100644 --- a/connect.c +++ b/connect.c @@ -375,6 +375,7 @@ static esp_netif_t *eth_start(void) free(desc); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + mac_config.rx_task_stack_size = CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE; eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; From e83639f957c34ad0cb49d1eb2e279524539b74df Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Thu, 23 Jun 2022 10:13:50 +0000 Subject: [PATCH 53/91] esp_eth: SPI Ethernet modules initialization simplification --- connect.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/connect.c b/connect.c index 5f648bc59b..07e74de0da 100644 --- a/connect.c +++ b/connect.c @@ -397,7 +397,6 @@ static esp_netif_t *eth_start(void) #endif #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET gpio_install_isr_service(0); - spi_device_handle_t spi_handle = NULL; spi_bus_config_t buscfg = { .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, @@ -406,33 +405,21 @@ static esp_netif_t *eth_start(void) .quadhd_io_num = -1, }; ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); -#if CONFIG_EXAMPLE_USE_DM9051 - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, .queue_size = 20 }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); +#if CONFIG_EXAMPLE_USE_DM9051 /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); s_phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_EXAMPLE_USE_W5500 - spi_device_interface_config_t devcfg = { - .command_bits = 16, // Actually it's the address phase in W5500 SPI frame - .address_bits = 8, // Actually it's the control phase in W5500 SPI frame - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); /* w5500 ethernet driver is based on spi driver */ - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); s_phy = esp_eth_phy_new_w5500(&phy_config); From 1c4b6f72508c0b028b52d06f5424af6620f4e770 Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Sun, 3 Jul 2022 15:24:39 +0800 Subject: [PATCH 54/91] common_components: add wifi connect console commands --- CMakeLists.txt | 29 +- Kconfig.projbuild | 18 +- addr_from_stdin.c | 2 +- connect.c | 473 +++-------------------------- console_cmd.c | 87 ++++++ eth_connect.c | 219 +++++++++++++ include/addr_from_stdin.h | 8 +- include/example_common_private.h | 60 ++++ include/protocol_examples_common.h | 49 +-- stdin_out.c | 5 + wifi_connect.c | 236 ++++++++++++++ 11 files changed, 725 insertions(+), 461 deletions(-) create mode 100644 console_cmd.c create mode 100644 eth_connect.c create mode 100644 include/example_common_private.h create mode 100644 wifi_connect.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f903a75d0..3bf73579da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,25 @@ -idf_component_register(SRCS "connect.c" "stdin_out.c" "addr_from_stdin.c" - INCLUDE_DIRS "include" - PRIV_REQUIRES esp_netif driver esp_eth - ) +set(srcs "stdin_out.c" + "addr_from_stdin.c" + "connect.c" + "wifi_connect.c") + +if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) + list(APPEND srcs "console_cmd.c") +endif() + +if(CONFIG_EXAMPLE_CONNECT_ETHERNET) + list(APPEND srcs "eth_connect.c") +endif() + + +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS "include" + PRIV_REQUIRES esp_netif driver) + +if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) + idf_component_optional_requires(PRIVATE console) +endif() + +if(CONFIG_EXAMPLE_CONNECT_ETHERNET) + idf_component_optional_requires(PRIVATE esp_eth) +endif() diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 7182563eae..27e81c0fd3 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -10,13 +10,29 @@ menu "Example Connection Configuration" Choose this option to connect with WiFi if EXAMPLE_CONNECT_WIFI + config EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + bool "Get ssid and password from stdin" + default n + help + Give the WiFi SSID and password from stdin. + + config EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD + depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + bool "Provide wifi connect commands" + default y + help + Provide wifi connect commands for esp_console. + Please use `register_wifi_connect_commands` to register them. + config EXAMPLE_WIFI_SSID + depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN string "WiFi SSID" default "myssid" help SSID (network name) for the example to connect to. config EXAMPLE_WIFI_PASSWORD + depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN string "WiFi Password" default "mypassword" help @@ -282,9 +298,9 @@ menu "Example Connection Configuration" endif # EXAMPLE_CONNECT_ETHERNET config EXAMPLE_CONNECT_IPV6 + depends on (EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET) bool "Obtain IPv6 address" default y - depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET select LWIP_IPV6 help By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. diff --git a/addr_from_stdin.c b/addr_from_stdin.c index c907ffd96e..5c79be614f 100644 --- a/addr_from_stdin.c +++ b/addr_from_stdin.c @@ -50,7 +50,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad return ESP_OK; } -#if CONFIG_LWIP_IPV6 +#if CONFIG_EXAMPLE_CONNECT_IPV6 else if (cur->ai_family == AF_INET6) { *ip_protocol = IPPROTO_IPV6; *addr_family = AF_INET6; diff --git a/connect.c b/connect.c index 07e74de0da..e141e52af6 100644 --- a/connect.c +++ b/connect.c @@ -1,94 +1,29 @@ -/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ #include #include "protocol_examples_common.h" +#include "example_common_private.h" #include "sdkconfig.h" #include "esp_event.h" #include "esp_wifi.h" #include "esp_wifi_default.h" -#if CONFIG_EXAMPLE_CONNECT_ETHERNET -#include "esp_eth.h" -#if CONFIG_ETH_USE_SPI_ETHERNET -#include "driver/spi_master.h" -#endif // CONFIG_ETH_USE_SPI_ETHERNET -#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_log.h" #include "esp_netif.h" -#include "driver/gpio.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "lwip/err.h" #include "lwip/sys.h" -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -#define MAX_IP6_ADDRS_PER_NETIF (5) -#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces*2) - -#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL -#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL -#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL -#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL -#endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_... - -#else -#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces) -#endif - -#define EXAMPLE_DO_CONNECT CONFIG_EXAMPLE_CONNECT_WIFI || CONFIG_EXAMPLE_CONNECT_ETHERNET - -#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST -#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN -#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL -#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN -#endif - -#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL -#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL -#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY -#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY -#endif - -#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN -#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK -#endif - -static int s_active_interfaces = 0; -static SemaphoreHandle_t s_semph_get_ip_addrs; -static esp_netif_t *s_example_esp_netif = NULL; - -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -static esp_ip6_addr_t s_ipv6_addr; +static const char *TAG = "example_common"; +#if CONFIG_EXAMPLE_CONNECT_IPV6 /* types of ipv6 addresses to be displayed on ipv6 events */ -static const char *s_ipv6_addr_types[] = { +const char *example_ipv6_addr_types_to_str[6] = { "ESP_IP6_ADDR_IS_UNKNOWN", "ESP_IP6_ADDR_IS_GLOBAL", "ESP_IP6_ADDR_IS_LINK_LOCAL", @@ -98,406 +33,84 @@ static const char *s_ipv6_addr_types[] = { }; #endif -static const char *TAG = "example_connect"; - -#if CONFIG_EXAMPLE_CONNECT_WIFI -static esp_netif_t *wifi_start(void); -static void wifi_stop(void); -#endif -#if CONFIG_EXAMPLE_CONNECT_ETHERNET -static esp_netif_t *eth_start(void); -static void eth_stop(void); -#endif - /** * @brief Checks the netif description if it contains specified prefix. * All netifs created withing common connect component are prefixed with the module TAG, * so it returns true if the specified netif is owned by this module */ -static bool is_our_netif(const char *prefix, esp_netif_t *netif) +bool example_is_our_netif(const char *prefix, esp_netif_t *netif) { return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0; } -/* set up connection, Wi-Fi and/or Ethernet */ -static void start(void) -{ - -#if CONFIG_EXAMPLE_CONNECT_WIFI - s_example_esp_netif = wifi_start(); - s_active_interfaces++; -#endif - -#if CONFIG_EXAMPLE_CONNECT_ETHERNET - s_example_esp_netif = eth_start(); - s_active_interfaces++; -#endif - -#if CONFIG_EXAMPLE_CONNECT_WIFI && CONFIG_EXAMPLE_CONNECT_ETHERNET - /* if both intefaces at once, clear out to indicate that multiple netifs are active */ - s_example_esp_netif = NULL; -#endif - -#if EXAMPLE_DO_CONNECT - /* create semaphore if at least one interface is active */ - s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); -#endif - -} - -/* tear down connection, release resources */ -static void stop(void) -{ -#if CONFIG_EXAMPLE_CONNECT_WIFI - wifi_stop(); - s_active_interfaces--; -#endif - -#if CONFIG_EXAMPLE_CONNECT_ETHERNET - eth_stop(); - s_active_interfaces--; -#endif -} - -#if EXAMPLE_DO_CONNECT -static esp_ip4_addr_t s_ip_addr; - -static void on_got_ip(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - if (!is_our_netif(TAG, event->esp_netif)) { - ESP_LOGW(TAG, "Got IPv4 from another interface \"%s\": ignored", esp_netif_get_desc(event->esp_netif)); - return; - } - ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); - memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); - xSemaphoreGive(s_semph_get_ip_addrs); -} -#endif - -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - -static void on_got_ipv6(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) +esp_netif_t *get_example_netif_from_desc(const char *desc) { - ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; - if (!is_our_netif(TAG, event->esp_netif)) { - ESP_LOGW(TAG, "Got IPv6 from another netif: ignored"); - return; - } - esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); - ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), - IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]); - if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { - memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); - xSemaphoreGive(s_semph_get_ip_addrs); + esp_netif_t *netif = NULL; + while ((netif = esp_netif_next(netif)) != NULL) { + if (strcmp(esp_netif_get_desc(netif), desc) == 0) { + return netif; + } } + return netif; } -#endif // CONFIG_EXAMPLE_CONNECT_IPV6 - -esp_err_t example_connect(void) +void example_print_all_netif_ips(const char *prefix) { -#if EXAMPLE_DO_CONNECT - if (s_semph_get_ip_addrs != NULL) { - return ESP_ERR_INVALID_STATE; - } -#endif - start(); - ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); - ESP_LOGI(TAG, "Waiting for IP(s)"); - for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) { - xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); - } // iterate over active interfaces, and print out IPs of "our" netifs esp_netif_t *netif = NULL; esp_netif_ip_info_t ip; for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) { netif = esp_netif_next(netif); - if (is_our_netif(TAG, netif)) { + if (example_is_our_netif(prefix, netif)) { ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip)); - ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip)); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip)); +#if CONFIG_EXAMPLE_CONNECT_IPV6 esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF]; int ip6_addrs = esp_netif_get_all_ip6(netif, ip6); for (int j = 0; j < ip6_addrs; ++j) { esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j])); - ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), s_ipv6_addr_types[ipv6_type]); + ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), example_ipv6_addr_types_to_str[ipv6_type]); } #endif - } } - return ESP_OK; } -esp_err_t example_disconnect(void) -{ - if (s_semph_get_ip_addrs == NULL) { - return ESP_ERR_INVALID_STATE; - } - vSemaphoreDelete(s_semph_get_ip_addrs); - s_semph_get_ip_addrs = NULL; - stop(); - ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&stop)); - return ESP_OK; -} - -#ifdef CONFIG_EXAMPLE_CONNECT_WIFI - -static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); - esp_err_t err = esp_wifi_connect(); - if (err == ESP_ERR_WIFI_NOT_STARTED) { - return; - } - ESP_ERROR_CHECK(err); -} - -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - -static void on_wifi_connect(void *esp_netif, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - esp_netif_create_ip6_linklocal(esp_netif); -} - -#endif // CONFIG_EXAMPLE_CONNECT_IPV6 - -static esp_netif_t *wifi_start(void) -{ - char *desc; - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - - esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); - // Prefix the interface description with the module TAG - // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) - asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc); - esp_netif_config.if_desc = desc; - esp_netif_config.route_prio = 128; - esp_netif_t *netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config); - free(desc); - esp_wifi_set_default_wifi_sta_handlers(); - - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL)); - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL)); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif)); - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); -#endif - - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_EXAMPLE_WIFI_SSID, - .password = CONFIG_EXAMPLE_WIFI_PASSWORD, - .scan_method = EXAMPLE_WIFI_SCAN_METHOD, - .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD, - .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD, - .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD, - }, - }; - ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); - esp_wifi_connect(); - return netif; -} - -static void wifi_stop(void) -{ - esp_netif_t *wifi_netif = get_example_netif_from_desc("sta"); - ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect)); - ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip)); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); - ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect)); -#endif - esp_err_t err = esp_wifi_stop(); - if (err == ESP_ERR_WIFI_NOT_INIT) { - return; - } - ESP_ERROR_CHECK(err); - ESP_ERROR_CHECK(esp_wifi_deinit()); - ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(wifi_netif)); - esp_netif_destroy(wifi_netif); - s_example_esp_netif = NULL; -} -#endif // CONFIG_EXAMPLE_CONNECT_WIFI - -#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET - -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - -/** Event handler for Ethernet events */ -static void on_eth_event(void *esp_netif, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - switch (event_id) { - case ETHERNET_EVENT_CONNECTED: - ESP_LOGI(TAG, "Ethernet Link Up"); - ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif)); - break; - default: - break; - } -} - -#endif // CONFIG_EXAMPLE_CONNECT_IPV6 - -static esp_eth_handle_t s_eth_handle = NULL; -static esp_eth_mac_t *s_mac = NULL; -static esp_eth_phy_t *s_phy = NULL; -static esp_eth_netif_glue_handle_t s_eth_glue = NULL; -static esp_netif_t *eth_start(void) +esp_err_t example_connect(void) { - char *desc; - esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH(); - // Prefix the interface description with the module TAG - // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) - asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc); - esp_netif_config.if_desc = desc; - esp_netif_config.route_prio = 64; - esp_netif_config_t netif_config = { - .base = &esp_netif_config, - .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH - }; - esp_netif_t *netif = esp_netif_new(&netif_config); - assert(netif); - free(desc); - - eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - mac_config.rx_task_stack_size = CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE; - eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); - phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; - phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; -#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET - eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); - esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; - s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); -#if CONFIG_EXAMPLE_ETH_PHY_IP101 - s_phy = esp_eth_phy_new_ip101(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 - s_phy = esp_eth_phy_new_rtl8201(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX - s_phy = esp_eth_phy_new_lan87xx(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_DP83848 - s_phy = esp_eth_phy_new_dp83848(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX - s_phy = esp_eth_phy_new_ksz80xx(&phy_config); -#endif -#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET - gpio_install_isr_service(0); - spi_bus_config_t buscfg = { - .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, - .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, - .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO, - .quadwp_io_num = -1, - .quadhd_io_num = -1, - }; - ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); - spi_device_interface_config_t spi_devcfg = { - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; -#if CONFIG_EXAMPLE_USE_DM9051 - /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); - dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; - s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); - s_phy = esp_eth_phy_new_dm9051(&phy_config); -#elif CONFIG_EXAMPLE_USE_W5500 - /* w5500 ethernet driver is based on spi driver */ - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); - w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; - s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); - s_phy = esp_eth_phy_new_w5500(&phy_config); -#endif -#elif CONFIG_EXAMPLE_USE_OPENETH - phy_config.autonego_timeout_ms = 100; - s_mac = esp_eth_mac_new_openeth(&mac_config); - s_phy = esp_eth_phy_new_dp83848(&phy_config); +#if CONFIG_EXAMPLE_CONNECT_ETHERNET + example_ethernet_connect(); + ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown)); #endif - - // Install Ethernet driver - esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); - ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); -#if !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET - /* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually. - 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control. - */ - ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) { - 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 - })); +#if CONFIG_EXAMPLE_CONNECT_WIFI + example_wifi_connect(); + ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown)); #endif - // combine driver with netif - s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); - esp_netif_attach(netif, s_eth_glue); - // Register user defined event handers - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); +#if CONFIG_EXAMPLE_CONNECT_ETHERNET + example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH); #endif - - esp_eth_start(s_eth_handle); - return netif; -} - -static void eth_stop(void) -{ - esp_netif_t *eth_netif = get_example_netif_from_desc("eth"); - ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip)); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); - ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); +#if CONFIG_EXAMPLE_CONNECT_WIFI + example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA); #endif - ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle)); - ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); - ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); - s_eth_handle = NULL; - ESP_ERROR_CHECK(s_phy->del(s_phy)); - ESP_ERROR_CHECK(s_mac->del(s_mac)); - - esp_netif_destroy(eth_netif); - s_example_esp_netif = NULL; -} -esp_eth_handle_t get_example_eth_handle(void) -{ - return s_eth_handle; + return ESP_OK; } -#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET -esp_netif_t *get_example_netif(void) -{ - return s_example_esp_netif; -} -esp_netif_t *get_example_netif_from_desc(const char *desc) +esp_err_t example_disconnect(void) { - esp_netif_t *netif = NULL; - char *expected_desc; - asprintf(&expected_desc, "%s: %s", TAG, desc); - while ((netif = esp_netif_next(netif)) != NULL) { - if (strcmp(esp_netif_get_desc(netif), expected_desc) == 0) { - free(expected_desc); - return netif; - } - } - free(expected_desc); - return netif; +#if CONFIG_EXAMPLE_CONNECT_ETHERNET + example_ethernet_shutdown(); + ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_ethernet_shutdown)); +#endif +#if CONFIG_EXAMPLE_CONNECT_WIFI + example_wifi_shutdown(); + ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_wifi_shutdown)); +#endif + return ESP_OK; } diff --git a/console_cmd.c b/console_cmd.c new file mode 100644 index 0000000000..4b3c20d7eb --- /dev/null +++ b/console_cmd.c @@ -0,0 +1,87 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + + +#include +#include "protocol_examples_common.h" +#include "example_common_private.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_console.h" +#include "argtable3/argtable3.h" + + +static const char *TAG = "example_console"; + +typedef struct { + struct arg_str *ssid; + struct arg_str *password; + struct arg_int *channel; + struct arg_end *end; +} wifi_connect_args_t; +static wifi_connect_args_t connect_args; + +static int cmd_do_wifi_connect(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &connect_args); + + if (nerrors != 0) { + arg_print_errors(stderr, connect_args.end, argv[0]); + return 1; + } + + wifi_config_t wifi_config = { + .sta = { + .scan_method = WIFI_ALL_CHANNEL_SCAN, + .sort_method = WIFI_CONNECT_AP_BY_SIGNAL, + }, + }; + if (connect_args.channel->count > 0) { + wifi_config.sta.channel = (uint8_t)(connect_args.channel->ival[0]); + } + const char *ssid = connect_args.ssid->sval[0]; + const char *pass = connect_args.password->sval[0]; + strlcpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid)); + if (pass) { + strlcpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password)); + } + example_wifi_sta_do_connect(wifi_config, false); + return 0; +} + +static int cmd_do_wifi_disconnect(int argc, char **argv) +{ + example_wifi_sta_do_disconnect(); + return 0; +} + +void register_wifi_connect_commands(void) +{ + ESP_LOGI(TAG, "Registering WiFi connect commands."); + example_wifi_start(); + + connect_args.ssid = arg_str1(NULL, NULL, "", "SSID of AP"); + connect_args.password = arg_str0(NULL, NULL, "", "password of AP"); + connect_args.channel = arg_int0("n", "channel", "", "channel of AP"); + connect_args.end = arg_end(2); + const esp_console_cmd_t wifi_connect_cmd = { + .command = "wifi_connect", + .help = "WiFi is station mode, join specified soft-AP", + .hint = NULL, + .func = &cmd_do_wifi_connect, + .argtable = &connect_args + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&wifi_connect_cmd) ); + + + const esp_console_cmd_t wifi_disconnect_cmd = { + .command = "wifi_disconnect", + .help = "Do wifi disconnect", + .hint = NULL, + .func = &cmd_do_wifi_disconnect, + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&wifi_disconnect_cmd) ); +} diff --git a/eth_connect.c b/eth_connect.c new file mode 100644 index 0000000000..71f06e2910 --- /dev/null +++ b/eth_connect.c @@ -0,0 +1,219 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include +#include "protocol_examples_common.h" +#include "example_common_private.h" +#include "esp_event.h" +#include "esp_eth.h" +#if CONFIG_ETH_USE_SPI_ETHERNET +#include "driver/spi_master.h" +#endif // CONFIG_ETH_USE_SPI_ETHERNET +#include "esp_log.h" +#include "driver/gpio.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" + + +static const char *TAG = "ethernet_connect"; +static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; + +static esp_netif_t *eth_start(void); +static void eth_stop(void); + + +/** Event handler for Ethernet events */ + +static void eth_on_got_ip(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) { + return; + } + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + if (s_semph_get_ip_addrs) { + xSemaphoreGive(s_semph_get_ip_addrs); + } +} + +#if CONFIG_EXAMPLE_CONNECT_IPV6 + +static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) { + return; + } + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); + ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), + IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); + if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { + xSemaphoreGive(s_semph_get_ip_addrs); + } +} + +static void on_eth_event(void *esp_netif, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + ESP_LOGI(TAG, "Ethernet Link Up"); + ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif)); + break; + default: + break; + } +} + +#endif // CONFIG_EXAMPLE_CONNECT_IPV6 + +static esp_eth_handle_t s_eth_handle = NULL; +static esp_eth_mac_t *s_mac = NULL; +static esp_eth_phy_t *s_phy = NULL; +static esp_eth_netif_glue_handle_t s_eth_glue = NULL; + +static esp_netif_t *eth_start(void) +{ + esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH(); + // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) + esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_ETH; + esp_netif_config.route_prio = 64; + esp_netif_config_t netif_config = { + .base = &esp_netif_config, + .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH + }; + esp_netif_t *netif = esp_netif_new(&netif_config); + assert(netif); + + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + mac_config.rx_task_stack_size = CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE; + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; +#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET + eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); + esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); +#if CONFIG_EXAMPLE_ETH_PHY_IP101 + s_phy = esp_eth_phy_new_ip101(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 + s_phy = esp_eth_phy_new_rtl8201(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX + s_phy = esp_eth_phy_new_lan87xx(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_DP83848 + s_phy = esp_eth_phy_new_dp83848(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX + s_phy = esp_eth_phy_new_ksz80xx(&phy_config); +#endif +#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET + gpio_install_isr_service(0); + spi_bus_config_t buscfg = { + .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, + .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, + .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + }; + ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); + spi_device_interface_config_t spi_devcfg = { + .mode = 0, + .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, + .queue_size = 20 + }; +#if CONFIG_EXAMPLE_USE_DM9051 + /* dm9051 ethernet driver is based on spi driver */ + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); + dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; + s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); + s_phy = esp_eth_phy_new_dm9051(&phy_config); +#elif CONFIG_EXAMPLE_USE_W5500 + /* w5500 ethernet driver is based on spi driver */ + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); + w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; + s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); + s_phy = esp_eth_phy_new_w5500(&phy_config); +#endif +#elif CONFIG_EXAMPLE_USE_OPENETH + phy_config.autonego_timeout_ms = 100; + s_mac = esp_eth_mac_new_openeth(&mac_config); + s_phy = esp_eth_phy_new_dp83848(&phy_config); +#endif + + // Install Ethernet driver + esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); + ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); +#if !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET + /* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually. + 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control. + */ + ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) { + 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 + })); +#endif + // combine driver with netif + s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); + esp_netif_attach(netif, s_eth_glue); + + // Register user defined event handers + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, ð_on_got_ip, NULL)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, ð_on_got_ipv6, NULL)); +#endif + + esp_eth_start(s_eth_handle); + return netif; +} + +static void eth_stop(void) +{ + esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH); + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, ð_on_got_ip)); +#if CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, ð_on_got_ipv6)); + ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); +#endif + ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle)); + ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); + ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); + s_eth_handle = NULL; + ESP_ERROR_CHECK(s_phy->del(s_phy)); + ESP_ERROR_CHECK(s_mac->del(s_mac)); + + esp_netif_destroy(eth_netif); +} + +esp_eth_handle_t get_example_eth_handle(void) +{ + return s_eth_handle; +} + +/* tear down connection, release resources */ +void example_ethernet_shutdown(void) +{ + if (s_semph_get_ip_addrs == NULL) { + return; + } + vSemaphoreDelete(s_semph_get_ip_addrs); + s_semph_get_ip_addrs = NULL; + eth_stop(); +} + +esp_err_t example_ethernet_connect(void) +{ + s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); + eth_start(); + ESP_LOGI(TAG, "Waiting for IP(s)."); + for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) { + xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); + } + return ESP_OK; +} diff --git a/include/addr_from_stdin.h b/include/addr_from_stdin.h index 9a059c149c..827f8c25ac 100644 --- a/include/addr_from_stdin.h +++ b/include/addr_from_stdin.h @@ -12,14 +12,14 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - #include "lwip/sys.h" #include #include +#ifdef __cplusplus +extern "C" { +#endif + /** * @brief Read and evaluate IP address from stdin * diff --git a/include/example_common_private.h b/include/example_common_private.h new file mode 100644 index 0000000000..03518bfbac --- /dev/null +++ b/include/example_common_private.h @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +/* Private Funtions of protocol example common */ + +#pragma once + +#include "esp_err.h" +#include "esp_wifi.h" +#include "sdkconfig.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if CONFIG_EXAMPLE_CONNECT_IPV6 +#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (2) +#else +#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (1) +#endif + + +#if CONFIG_EXAMPLE_CONNECT_IPV6 +#define MAX_IP6_ADDRS_PER_NETIF (5) + +#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL +#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL +#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL +#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL) +#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL +#endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_... + +#endif + + +#if CONFIG_EXAMPLE_CONNECT_IPV6 +extern const char *example_ipv6_addr_types_to_str[6]; +#endif + +void example_wifi_start(void); +void example_wifi_stop(void); +esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait); +esp_err_t example_wifi_sta_do_disconnect(void); +bool example_is_our_netif(const char *prefix, esp_netif_t *netif); +void example_print_all_netif_ips(const char *prefix); +void example_wifi_shutdown(void); +esp_err_t example_wifi_connect(void); +void example_ethernet_shutdown(void); +esp_err_t example_ethernet_connect(void); + + + +#ifdef __cplusplus +} +#endif diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index d43e94e0d0..3a318c6526 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -9,25 +9,32 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - +#include "sdkconfig.h" #include "esp_err.h" #include "esp_netif.h" +#if CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_eth.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif -#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET -#define EXAMPLE_INTERFACE get_example_netif() +#if CONFIG_EXAMPLE_CONNECT_WIFI +#define EXAMPLE_NETIF_DESC_STA "example_netif_sta" #endif -#ifdef CONFIG_EXAMPLE_CONNECT_WIFI -#define EXAMPLE_INTERFACE get_example_netif() +#if CONFIG_EXAMPLE_CONNECT_ETHERNET +#define EXAMPLE_NETIF_DESC_ETH "example_netif_eth" #endif -#if !defined (CONFIG_EXAMPLE_CONNECT_ETHERNET) && !defined (CONFIG_EXAMPLE_CONNECT_WIFI) -// This is useful for some tests which do not need a network connection -#define EXAMPLE_INTERFACE NULL +/* Example default interface, prefer the ethernet one if running in example-test (CI) configuration */ +#if CONFIG_EXAMPLE_CONNECT_ETHERNET +#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH) +#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH) +#elif CONFIG_EXAMPLE_CONNECT_WIFI +#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) +#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) #endif /** @@ -60,15 +67,6 @@ esp_err_t example_disconnect(void); */ esp_err_t example_configure_stdin_stdout(void); -/** - * @brief Returns esp-netif pointer created by example_connect() - * - * @note If multiple interfaces active at once, this API return NULL - * In that case the get_example_netif_from_desc() should be used - * to get esp-netif pointer based on interface description - */ -esp_netif_t *get_example_netif(void); - /** * @brief Returns esp-netif pointer created by example_connect() described by * the supplied desc field @@ -79,7 +77,16 @@ esp_netif_t *get_example_netif(void); */ esp_netif_t *get_example_netif_from_desc(const char *desc); -#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET +#if CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD +/** + * @brief Register wifi connect commands + * + * @note Provide wifi connect commands using esp_console. + */ +void register_wifi_connect_commands(void); +#endif + +#if CONFIG_EXAMPLE_CONNECT_ETHERNET /** * @brief Get the example Ethernet driver handle * diff --git a/stdin_out.c b/stdin_out.c index b57e0e7156..5fbc8a38af 100644 --- a/stdin_out.c +++ b/stdin_out.c @@ -15,6 +15,10 @@ esp_err_t example_configure_stdin_stdout(void) { + static bool configured = false; + if (configured) { + return ESP_OK; + } // Initialize VFS & UART so we can use std::cout/cin setvbuf(stdin, NULL, _IONBF, 0); /* Install UART driver for interrupt-driven reads and writes */ @@ -25,5 +29,6 @@ esp_err_t example_configure_stdin_stdout(void) esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + configured = true; return ESP_OK; } diff --git a/wifi_connect.c b/wifi_connect.c new file mode 100644 index 0000000000..d899141cb0 --- /dev/null +++ b/wifi_connect.c @@ -0,0 +1,236 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. + */ + +#include +#include "protocol_examples_common.h" +#include "example_common_private.h" +#include "esp_log.h" + +#if CONFIG_EXAMPLE_CONNECT_WIFI + +static const char *TAG = "example_connect"; +static esp_netif_t *s_example_sta_netif = NULL; +static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; + +#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST +#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN +#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL +#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN +#endif + +#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL +#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL +#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY +#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY +#endif + +#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN +#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK +#endif + + +static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); + esp_err_t err = esp_wifi_connect(); + if (err == ESP_ERR_WIFI_NOT_STARTED) { + return; + } + ESP_ERROR_CHECK(err); +} + +static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ +#if CONFIG_EXAMPLE_CONNECT_IPV6 + esp_netif_create_ip6_linklocal(esp_netif); +#endif // CONFIG_EXAMPLE_CONNECT_IPV6 +} + +static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { + return; + } + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + if (s_semph_get_ip_addrs) { + xSemaphoreGive(s_semph_get_ip_addrs); + } else { + ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&event->ip_info.ip)); + } +} + +#if CONFIG_EXAMPLE_CONNECT_IPV6 +static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { + return; + } + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); + ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), + IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); + + if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { + if (s_semph_get_ip_addrs) { + xSemaphoreGive(s_semph_get_ip_addrs); + } else { + ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); + } + } +} +#endif // CONFIG_EXAMPLE_CONNECT_IPV6 + + +void example_wifi_start(void) +{ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); + // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) + esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_STA; + esp_netif_config.route_prio = 128; + s_example_sta_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config); + esp_wifi_set_default_wifi_sta_handlers(); + + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); +} + + +void example_wifi_stop(void) +{ + esp_err_t err = esp_wifi_stop(); + if (err == ESP_ERR_WIFI_NOT_INIT) { + return; + } + ESP_ERROR_CHECK(err); + ESP_ERROR_CHECK(esp_wifi_deinit()); + ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_sta_netif)); + esp_netif_destroy(s_example_sta_netif); + s_example_sta_netif = NULL; +} + + +esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) +{ + if (wait) { + s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); + } + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif)); +#if CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6, NULL)); +#endif + + ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); + esp_err_t ret = esp_wifi_connect(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "WiFi connect failed! ret:%x", ret); + return ret; + } + if (wait) { + ESP_LOGI(TAG, "Waiting for IP(s)"); + for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) { + xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); + } + } + return ESP_OK; +} + +esp_err_t example_wifi_sta_do_disconnect(void) +{ + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect)); + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip)); + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect)); +#if CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6)); +#endif + if (s_semph_get_ip_addrs) { + vSemaphoreDelete(s_semph_get_ip_addrs); + } + return esp_wifi_disconnect(); +} + +void example_wifi_shutdown(void) +{ + example_wifi_sta_do_disconnect(); + example_wifi_stop(); +} + +esp_err_t example_wifi_connect(void) +{ + ESP_LOGI(TAG, "Start example_connect."); + example_wifi_start(); + wifi_config_t wifi_config = { + .sta = { +#if !CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + .ssid = CONFIG_EXAMPLE_WIFI_SSID, + .password = CONFIG_EXAMPLE_WIFI_PASSWORD, +#endif + .scan_method = EXAMPLE_WIFI_SCAN_METHOD, + .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD, + .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD, + .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD, + }, + }; +#if CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + example_configure_stdin_stdout(); + char buf[32+64+2] = {0}; + ESP_LOGI(TAG, "Please input ssid password:"); + fgets(buf, sizeof(buf), stdin); + int len = strlen(buf); + buf[len-1] = '\0'; + memset(wifi_config.sta.ssid, 0, 32); + char *temp = strtok(buf, " "); + strncpy((char*)wifi_config.sta.ssid, temp, 32); + memset(wifi_config.sta.password, 0, 64); + temp = strtok(NULL, " "); + if (temp) { + strncpy((char*)wifi_config.sta.password, temp, 64); + } else { + wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN; + } +#endif + example_wifi_sta_do_connect(wifi_config, true); + return ESP_OK; +} + + +#endif /* CONFIG_EXAMPLE_CONNECT_WIFI */ From 392bc2128a4823a33305cb70c7149dfea6e7388f Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Mon, 4 Jul 2022 22:13:16 +0800 Subject: [PATCH 55/91] example: use example common componments in esp_local_ctrl --- Kconfig.projbuild | 7 +++++++ connect.c | 10 +++++++--- wifi_connect.c | 22 +++++++++++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 27e81c0fd3..ec6f731cc3 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -39,6 +39,13 @@ menu "Example Connection Configuration" WiFi password (WPA or WPA2) for the example to use. Can be left blank if the network has no security set. + config EXAMPLE_WIFI_CONN_MAX_RETRY + int "Maximum retry" + default 6 + help + Set the Maximum retry to avoid station reconnecting to the AP unlimited, + in case the AP is really inexistent. + choice EXAMPLE_WIFI_SCAN_METHOD prompt "WiFi Scan Method" default EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL diff --git a/connect.c b/connect.c index e141e52af6..8a7e28266b 100644 --- a/connect.c +++ b/connect.c @@ -82,17 +82,22 @@ void example_print_all_netif_ips(const char *prefix) esp_err_t example_connect(void) { #if CONFIG_EXAMPLE_CONNECT_ETHERNET - example_ethernet_connect(); + if (example_ethernet_connect() != ESP_OK) { + return ESP_FAIL; + } ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown)); #endif #if CONFIG_EXAMPLE_CONNECT_WIFI - example_wifi_connect(); + if (example_wifi_connect() != ESP_OK) { + return ESP_FAIL; + } ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown)); #endif #if CONFIG_EXAMPLE_CONNECT_ETHERNET example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH); #endif + #if CONFIG_EXAMPLE_CONNECT_WIFI example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA); #endif @@ -101,7 +106,6 @@ esp_err_t example_connect(void) } - esp_err_t example_disconnect(void) { #if CONFIG_EXAMPLE_CONNECT_ETHERNET diff --git a/wifi_connect.c b/wifi_connect.c index d899141cb0..a93a927e48 100644 --- a/wifi_connect.c +++ b/wifi_connect.c @@ -55,10 +55,23 @@ static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK #endif +static int s_retry_num = 0; static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + s_retry_num++; + if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { + ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num); + if (s_semph_get_ip_addrs) { + /* let example_wifi_sta_do_connect() return */ + xSemaphoreGive(s_semph_get_ip_addrs); +#if CONFIG_EXAMPLE_CONNECT_IPV6 + xSemaphoreGive(s_semph_get_ip_addrs); +#endif + } + return; + } ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); esp_err_t err = esp_wifi_connect(); if (err == ESP_ERR_WIFI_NOT_STARTED) { @@ -78,6 +91,7 @@ static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t ev static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + s_retry_num = 0; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { return; @@ -150,7 +164,7 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) if (wait) { s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); } - + s_retry_num = 0; ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif)); @@ -170,6 +184,9 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) { xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); } + if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { + return ESP_FAIL; + } } return ESP_OK; } @@ -228,8 +245,7 @@ esp_err_t example_wifi_connect(void) wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN; } #endif - example_wifi_sta_do_connect(wifi_config, true); - return ESP_OK; + return example_wifi_sta_do_connect(wifi_config, true); } From 6a53b1cec0878f6f317664f62d8af1f55096999c Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Thu, 7 Jul 2022 00:34:06 +0800 Subject: [PATCH 56/91] CI: Improve common test methods also fix ota test cases --- Kconfig.projbuild | 4 ++-- console_cmd.c | 2 +- include/protocol_examples_common.h | 5 +++-- wifi_connect.c | 18 ++++++++++-------- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index ec6f731cc3..ed382457d7 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -22,7 +22,7 @@ menu "Example Connection Configuration" default y help Provide wifi connect commands for esp_console. - Please use `register_wifi_connect_commands` to register them. + Please use `example_register_wifi_connect_commands` to register them. config EXAMPLE_WIFI_SSID depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN @@ -305,7 +305,7 @@ menu "Example Connection Configuration" endif # EXAMPLE_CONNECT_ETHERNET config EXAMPLE_CONNECT_IPV6 - depends on (EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET) + depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET bool "Obtain IPv6 address" default y select LWIP_IPV6 diff --git a/console_cmd.c b/console_cmd.c index 4b3c20d7eb..342a92c769 100644 --- a/console_cmd.c +++ b/console_cmd.c @@ -58,7 +58,7 @@ static int cmd_do_wifi_disconnect(int argc, char **argv) return 0; } -void register_wifi_connect_commands(void) +void example_register_wifi_connect_commands(void) { ESP_LOGI(TAG, "Registering WiFi connect commands."); example_wifi_start(); diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index 3a318c6526..7417c1e635 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -81,9 +81,10 @@ esp_netif_t *get_example_netif_from_desc(const char *desc); /** * @brief Register wifi connect commands * - * @note Provide wifi connect commands using esp_console. + * Provide a simple wifi_connect command in esp_console. + * This function can be used after esp_console is initialized. */ -void register_wifi_connect_commands(void); +void example_register_wifi_connect_commands(void); #endif #if CONFIG_EXAMPLE_CONNECT_ETHERNET diff --git a/wifi_connect.c b/wifi_connect.c index a93a927e48..682367fffc 100644 --- a/wifi_connect.c +++ b/wifi_connect.c @@ -229,18 +229,20 @@ esp_err_t example_wifi_connect(void) }; #if CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN example_configure_stdin_stdout(); - char buf[32+64+2] = {0}; + char buf[sizeof(wifi_config.sta.ssid)+sizeof(wifi_config.sta.password)+2] = {0}; ESP_LOGI(TAG, "Please input ssid password:"); fgets(buf, sizeof(buf), stdin); int len = strlen(buf); - buf[len-1] = '\0'; - memset(wifi_config.sta.ssid, 0, 32); - char *temp = strtok(buf, " "); - strncpy((char*)wifi_config.sta.ssid, temp, 32); - memset(wifi_config.sta.password, 0, 64); - temp = strtok(NULL, " "); + buf[len-1] = '\0'; /* removes '\n' */ + memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid)); + + char *rest = NULL; + char *temp = strtok_r(buf, " ", &rest); + strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid)); + memset(wifi_config.sta.password, 0, sizeof(wifi_config.sta.password)); + temp = strtok_r(NULL, " ", &rest); if (temp) { - strncpy((char*)wifi_config.sta.password, temp, 64); + strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password)); } else { wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN; } From 08beaffccf777cac87ceedd695e5cb75b80c9baa Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 11 May 2022 16:01:48 +0200 Subject: [PATCH 57/91] esp_netif/lwip: Fix deps cycles to "lwip -> esp_netif -> phy-drivers" Fix dependency tree so that lwip doesn't depend on any specific network interface component. Network interface drivers shall depend on esp_netif. esp_netif shall depend on lwip (but not on any specific interface driver) -- it optionally depends on vfs and esp_eth (need ethernet header for L2/bridge mode) --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bf73579da..f155329bfd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ endif() idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" - PRIV_REQUIRES esp_netif driver) + PRIV_REQUIRES esp_netif driver esp_wifi vfs) if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) idf_component_optional_requires(PRIVATE console) From b721c62a94ff4234e4ea3bfe5fe420ec4939f609 Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Wed, 31 Aug 2022 22:51:03 +0800 Subject: [PATCH 58/91] example_common: fix netif ips may be printed before got ipv4 --- eth_connect.c | 32 +++++++++++++++++++------- include/example_common_private.h | 7 ------ wifi_connect.c | 39 ++++++++++++++++++++++++-------- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/eth_connect.c b/eth_connect.c index 71f06e2910..0b7d8f4b1a 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -21,6 +21,9 @@ static const char *TAG = "ethernet_connect"; static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; +#if CONFIG_EXAMPLE_CONNECT_IPV6 +static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; +#endif static esp_netif_t *eth_start(void); static void eth_stop(void); @@ -36,9 +39,7 @@ static void eth_on_got_ip(void *arg, esp_event_base_t event_base, return; } ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); - if (s_semph_get_ip_addrs) { - xSemaphoreGive(s_semph_get_ip_addrs); - } + xSemaphoreGive(s_semph_get_ip_addrs); } #if CONFIG_EXAMPLE_CONNECT_IPV6 @@ -54,7 +55,7 @@ static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base, ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { - xSemaphoreGive(s_semph_get_ip_addrs); + xSemaphoreGive(s_semph_get_ip6_addrs); } } @@ -204,16 +205,31 @@ void example_ethernet_shutdown(void) } vSemaphoreDelete(s_semph_get_ip_addrs); s_semph_get_ip_addrs = NULL; +#if CONFIG_EXAMPLE_CONNECT_IPV6 + vSemaphoreDelete(s_semph_get_ip6_addrs); + s_semph_get_ip6_addrs = NULL; +#endif eth_stop(); } esp_err_t example_ethernet_connect(void) { - s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); + s_semph_get_ip_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip_addrs == NULL) { + return ESP_ERR_NO_MEM; + } +#if CONFIG_EXAMPLE_CONNECT_IPV6 + s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip6_addrs == NULL) { + vSemaphoreDelete(s_semph_get_ip_addrs); + return ESP_ERR_NO_MEM; + } +#endif eth_start(); ESP_LOGI(TAG, "Waiting for IP(s)."); - for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) { - xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); - } + xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); +#if CONFIG_EXAMPLE_CONNECT_IPV6 + xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); +#endif return ESP_OK; } diff --git a/include/example_common_private.h b/include/example_common_private.h index 03518bfbac..4921e477ea 100644 --- a/include/example_common_private.h +++ b/include/example_common_private.h @@ -15,13 +15,6 @@ extern "C" { #endif -#if CONFIG_EXAMPLE_CONNECT_IPV6 -#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (2) -#else -#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (1) -#endif - - #if CONFIG_EXAMPLE_CONNECT_IPV6 #define MAX_IP6_ADDRS_PER_NETIF (5) diff --git a/wifi_connect.c b/wifi_connect.c index 682367fffc..3e10407d93 100644 --- a/wifi_connect.c +++ b/wifi_connect.c @@ -22,6 +22,9 @@ static const char *TAG = "example_connect"; static esp_netif_t *s_example_sta_netif = NULL; static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; +#if CONFIG_EXAMPLE_CONNECT_IPV6 +static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; +#endif #if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST #define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN @@ -63,13 +66,15 @@ static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event s_retry_num++; if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num); + /* let example_wifi_sta_do_connect() return */ if (s_semph_get_ip_addrs) { - /* let example_wifi_sta_do_connect() return */ xSemaphoreGive(s_semph_get_ip_addrs); + } #if CONFIG_EXAMPLE_CONNECT_IPV6 - xSemaphoreGive(s_semph_get_ip_addrs); -#endif + if (s_semph_get_ip6_addrs) { + xSemaphoreGive(s_semph_get_ip6_addrs); } +#endif return; } ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); @@ -117,8 +122,8 @@ static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_ba IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { - if (s_semph_get_ip_addrs) { - xSemaphoreGive(s_semph_get_ip_addrs); + if (s_semph_get_ip6_addrs) { + xSemaphoreGive(s_semph_get_ip6_addrs); } else { ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); } @@ -162,7 +167,17 @@ void example_wifi_stop(void) esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) { if (wait) { - s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); + s_semph_get_ip_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip_addrs == NULL) { + return ESP_ERR_NO_MEM; + } +#if CONFIG_EXAMPLE_CONNECT_IPV6 + s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip6_addrs == NULL) { + vSemaphoreDelete(s_semph_get_ip_addrs); + return ESP_ERR_NO_MEM; + } +#endif } s_retry_num = 0; ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL)); @@ -181,9 +196,10 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) } if (wait) { ESP_LOGI(TAG, "Waiting for IP(s)"); - for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) { - xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); - } + xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); +#if CONFIG_EXAMPLE_CONNECT_IPV6 + xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); +#endif if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { return ESP_FAIL; } @@ -202,6 +218,11 @@ esp_err_t example_wifi_sta_do_disconnect(void) if (s_semph_get_ip_addrs) { vSemaphoreDelete(s_semph_get_ip_addrs); } +#if CONFIG_EXAMPLE_CONNECT_IPV6 + if (s_semph_get_ip6_addrs) { + vSemaphoreDelete(s_semph_get_ip6_addrs); + } +#endif return esp_wifi_disconnect(); } From ac929c30d2f8fbd19fc76829d908789abb4c9151 Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Wed, 25 Jan 2023 16:45:01 +0530 Subject: [PATCH 59/91] examples/protocols: Added URI encoding/decoding feature - http_server/simple: Decoding received query - esp_http_client: Sending encoded query --- CMakeLists.txt | 3 +- include/protocol_examples_utils.h | 49 ++++ protocol_examples_utils.c | 388 ++++++++++++++++++++++++++++++ 3 files changed, 439 insertions(+), 1 deletion(-) create mode 100644 include/protocol_examples_utils.h create mode 100644 protocol_examples_utils.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f155329bfd..15981d027c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,8 @@ set(srcs "stdin_out.c" "addr_from_stdin.c" "connect.c" - "wifi_connect.c") + "wifi_connect.c" + "protocol_examples_utils.c") if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) list(APPEND srcs "console_cmd.c") diff --git a/include/protocol_examples_utils.h b/include/protocol_examples_utils.h new file mode 100644 index 0000000000..79031171dc --- /dev/null +++ b/include/protocol_examples_utils.h @@ -0,0 +1,49 @@ +/* + * Utility functions for protocol examples + * + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @brief Encode an URI + * + * @param dest a destination memory location + * @param src the source string + * @param len the length of the source string + * @return uint32_t the count of escaped characters + * + * @note Please allocate the destination buffer keeping in mind that encoding a + * special character will take up 3 bytes (for '%' and two hex digits). + * In the worst-case scenario, the destination buffer will have to be 3 times + * that of the source string. + */ +uint32_t example_uri_encode(char *dest, const char *src, size_t len); + +/** + * @brief Decode an URI + * + * @param dest a destination memory location + * @param src the source string + * @param len the length of the source string + * + * @note Please allocate the destination buffer keeping in mind that a decoded + * special character will take up 2 less bytes than its encoded form. + * In the worst-case scenario, the destination buffer will have to be + * the same size that of the source string. + */ +void example_uri_decode(char *dest, const char *src, size_t len); + +#ifdef __cplusplus +} +#endif diff --git a/protocol_examples_utils.c b/protocol_examples_utils.c new file mode 100644 index 0000000000..e6e3de5f62 --- /dev/null +++ b/protocol_examples_utils.c @@ -0,0 +1,388 @@ +/* + * Utility functions for protocol examples + * + * SPDX-FileCopyrightText: 2002-2021 Igor Sysoev + * 2011-2022 Nginx, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + * + * SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD + */ +/* + * Copyright (C) 2002-2021 Igor Sysoev + * Copyright (C) 2011-2022 Nginx, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "protocol_examples_utils.h" + +/* Type of Escape algorithms to be used */ +#define NGX_ESCAPE_URI (0) +#define NGX_ESCAPE_ARGS (1) +#define NGX_ESCAPE_URI_COMPONENT (2) +#define NGX_ESCAPE_HTML (3) +#define NGX_ESCAPE_REFRESH (4) +#define NGX_ESCAPE_MEMCACHED (5) +#define NGX_ESCAPE_MAIL_AUTH (6) + +/* Type of Unescape algorithms to be used */ +#define NGX_UNESCAPE_URI (1) +#define NGX_UNESCAPE_REDIRECT (2) + + +uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, unsigned int type) +{ + unsigned int n; + uint32_t *escape; + static u_char hex[] = "0123456789ABCDEF"; + + /* + * Per RFC 3986 only the following chars are allowed in URIs unescaped: + * + * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" + * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" + * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" + * / "*" / "+" / "," / ";" / "=" + * + * And "%" can appear as a part of escaping itself. The following + * characters are not allowed and need to be escaped: %00-%1F, %7F-%FF, + * " ", """, "<", ">", "\", "^", "`", "{", "|", "}". + */ + + /* " ", "#", "%", "?", not allowed */ + + static uint32_t uri[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0xd000002d, /* 1101 0000 0000 0000 0000 0000 0010 1101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", "#", "%", "&", "+", ";", "?", not allowed */ + + static uint32_t args[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0xd800086d, /* 1101 1000 0000 0000 0000 1000 0110 1101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* not ALPHA, DIGIT, "-", ".", "_", "~" */ + + static uint32_t uri_component[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0xfc009fff, /* 1111 1100 0000 0000 1001 1111 1111 1111 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x78000001, /* 0111 1000 0000 0000 0000 0000 0000 0001 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", "#", """, "%", "'", not allowed */ + + static uint32_t html[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x500000ad, /* 0101 0000 0000 0000 0000 0000 1010 1101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", """, "'", not allowed */ + + static uint32_t refresh[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x50000085, /* 0101 0000 0000 0000 0000 0000 1000 0101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xd8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", "%", %00-%1F */ + + static uint32_t memcached[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + }; + + /* mail_auth is the same as memcached */ + + static uint32_t *map[] = + { uri, args, uri_component, html, refresh, memcached, memcached }; + + + escape = map[type]; + + if (dst == NULL) { + + /* find the number of the characters to be escaped */ + + n = 0; + + while (size) { + if (escape[*src >> 5] & (1U << (*src & 0x1f))) { + n++; + } + src++; + size--; + } + + return (uintptr_t) n; + } + + while (size) { + if (escape[*src >> 5] & (1U << (*src & 0x1f))) { + *dst++ = '%'; + *dst++ = hex[*src >> 4]; + *dst++ = hex[*src & 0xf]; + src++; + + } else { + *dst++ = *src++; + } + size--; + } + + return (uintptr_t) dst; +} + + +void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type) +{ + u_char *d, *s, ch, c, decoded; + enum { + sw_usual = 0, + sw_quoted, + sw_quoted_second + } state; + + d = *dst; + s = *src; + + state = 0; + decoded = 0; + + while (size--) { + + ch = *s++; + + switch (state) { + case sw_usual: + if (ch == '?' + && (type & (NGX_UNESCAPE_URI | NGX_UNESCAPE_REDIRECT))) { + *d++ = ch; + goto done; + } + + if (ch == '%') { + state = sw_quoted; + break; + } + + *d++ = ch; + break; + + case sw_quoted: + + if (ch >= '0' && ch <= '9') { + decoded = (u_char) (ch - '0'); + state = sw_quoted_second; + break; + } + + c = (u_char) (ch | 0x20); + if (c >= 'a' && c <= 'f') { + decoded = (u_char) (c - 'a' + 10); + state = sw_quoted_second; + break; + } + + /* the invalid quoted character */ + + state = sw_usual; + + *d++ = ch; + + break; + + case sw_quoted_second: + + state = sw_usual; + + if (ch >= '0' && ch <= '9') { + ch = (u_char) ((decoded << 4) + (ch - '0')); + + if (type & NGX_UNESCAPE_REDIRECT) { + if (ch > '%' && ch < 0x7f) { + *d++ = ch; + break; + } + + *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1); + + break; + } + + *d++ = ch; + + break; + } + + c = (u_char) (ch | 0x20); + if (c >= 'a' && c <= 'f') { + ch = (u_char) ((decoded << 4) + (c - 'a') + 10); + + if (type & NGX_UNESCAPE_URI) { + if (ch == '?') { + *d++ = ch; + goto done; + } + + *d++ = ch; + break; + } + + if (type & NGX_UNESCAPE_REDIRECT) { + if (ch == '?') { + *d++ = ch; + goto done; + } + + if (ch > '%' && ch < 0x7f) { + *d++ = ch; + break; + } + + *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1); + break; + } + + *d++ = ch; + + break; + } + + /* the invalid quoted character */ + + break; + } + } + +done: + + *dst = d; + *src = s; +} + + +uint32_t example_uri_encode(char *dest, const char *src, size_t len) +{ + if (!src || !dest) { + return 0; + } + + uintptr_t ret = ngx_escape_uri((unsigned char *)dest, (unsigned char *)src, len, NGX_ESCAPE_URI_COMPONENT); + return (uint32_t)(ret - (uintptr_t)dest); +} + + +void example_uri_decode(char *dest, const char *src, size_t len) +{ + if (!src || !dest) { + return; + } + + unsigned char *src_ptr = (unsigned char *)src; + unsigned char *dst_ptr = (unsigned char *)dest; + ngx_unescape_uri(&dst_ptr, &src_ptr, len, NGX_UNESCAPE_URI); +} From cfd89dbe6c6a59917625fff8f23f3237eb49b4ab Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 31 Jan 2023 08:29:00 +0100 Subject: [PATCH 60/91] examples: Add common linux component tapif_io That can be used with linux target on lwip to pass packets from lwip to linux host networking stack, e.g. routing the trafic to internet. --- CMakeLists.txt | 10 ++++++++++ Kconfig.projbuild | 2 ++ 2 files changed, 12 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15981d027c..2a7352d5fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,13 @@ +idf_build_get_property(target IDF_TARGET) + +if(${target} STREQUAL "linux") + # Header only library for linux + + idf_component_register(INCLUDE_DIRS include + PRIV_REQUIRES tapif_io) + return() +endif() + set(srcs "stdin_out.c" "addr_from_stdin.c" "connect.c" diff --git a/Kconfig.projbuild b/Kconfig.projbuild index ed382457d7..f839bb5e21 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -4,6 +4,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_WIFI bool "connect using WiFi interface" + depends on !IDF_TARGET_LINUX default y help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. @@ -117,6 +118,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_ETHERNET bool "connect using Ethernet interface" + depends on !IDF_TARGET_LINUX default n help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. From 9cb3afd767d73cab945a746258521e69ae95cb9c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 3 Oct 2022 17:28:01 +0200 Subject: [PATCH 61/91] lwip: Support IPv6 only mode --- Kconfig.projbuild | 5 +++++ addr_from_stdin.c | 7 ++++--- connect.c | 6 ++++-- eth_connect.c | 6 +++++- wifi_connect.c | 4 +++- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index f839bb5e21..fe92d24db4 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -306,6 +306,11 @@ menu "Example Connection Configuration" Set PHY address according your board schematic. endif # EXAMPLE_CONNECT_ETHERNET + config EXAMPLE_CONNECT_IPV4 + bool + depends on LWIP_IPV4 + default y + config EXAMPLE_CONNECT_IPV6 depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET bool "Obtain IPv6 address" diff --git a/addr_from_stdin.c b/addr_from_stdin.c index 5c79be614f..e4c94b1a14 100644 --- a/addr_from_stdin.c +++ b/addr_from_stdin.c @@ -41,6 +41,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad } for( cur = addr_list; cur != NULL; cur = cur->ai_next ) { memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr)); +#if CONFIG_EXAMPLE_CONNECT_IPV4 if (cur->ai_family == AF_INET) { *ip_protocol = IPPROTO_IP; *addr_family = AF_INET; @@ -48,10 +49,10 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad ((struct sockaddr_in*)dest_addr)->sin_port = htons(port); freeaddrinfo( addr_list ); return ESP_OK; - } +#endif // IPV4 #if CONFIG_EXAMPLE_CONNECT_IPV6 - else if (cur->ai_family == AF_INET6) { + if (cur->ai_family == AF_INET6) { *ip_protocol = IPPROTO_IPV6; *addr_family = AF_INET6; // add port and interface number and return on first IPv6 match @@ -60,7 +61,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad freeaddrinfo( addr_list ); return ESP_OK; } -#endif +#endif // IPV6 } // no match found freeaddrinfo( addr_list ); diff --git a/connect.c b/connect.c index 8a7e28266b..d40870eb02 100644 --- a/connect.c +++ b/connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -58,14 +58,16 @@ void example_print_all_netif_ips(const char *prefix) { // iterate over active interfaces, and print out IPs of "our" netifs esp_netif_t *netif = NULL; - esp_netif_ip_info_t ip; for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) { netif = esp_netif_next(netif); if (example_is_our_netif(prefix, netif)) { ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); +#if CONFIG_LWIP_IPV4 + esp_netif_ip_info_t ip; ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip)); ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip)); +#endif #if CONFIG_EXAMPLE_CONNECT_IPV6 esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF]; int ip6_addrs = esp_netif_get_all_ip6(netif, ip6); diff --git a/eth_connect.c b/eth_connect.c index 0b7d8f4b1a..fce806c6aa 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -214,10 +214,12 @@ void example_ethernet_shutdown(void) esp_err_t example_ethernet_connect(void) { +#if CONFIG_EXAMPLE_CONNECT_IPV4 s_semph_get_ip_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip_addrs == NULL) { return ESP_ERR_NO_MEM; } +#endif #if CONFIG_EXAMPLE_CONNECT_IPV6 s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip6_addrs == NULL) { @@ -227,7 +229,9 @@ esp_err_t example_ethernet_connect(void) #endif eth_start(); ESP_LOGI(TAG, "Waiting for IP(s)."); +#if CONFIG_EXAMPLE_CONNECT_IPV4 xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); +#endif #if CONFIG_EXAMPLE_CONNECT_IPV6 xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); #endif diff --git a/wifi_connect.c b/wifi_connect.c index 3e10407d93..e4e1468688 100644 --- a/wifi_connect.c +++ b/wifi_connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -196,7 +196,9 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) } if (wait) { ESP_LOGI(TAG, "Waiting for IP(s)"); +#if CONFIG_EXAMPLE_CONNECT_IPV4 xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); +#endif #if CONFIG_EXAMPLE_CONNECT_IPV6 xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); #endif From b4c01c06c39ec0be321137e4af4d1023abfdd29d Mon Sep 17 00:00:00 2001 From: Harshit Malpani Date: Mon, 13 Feb 2023 10:23:16 +0530 Subject: [PATCH 62/91] fix esp_http_client_example to build for Linux target. Made `protocol_examples_common` compatible for Linux target --- CMakeLists.txt | 2 +- connect.c | 2 +- include/example_common_private.h | 2 +- include/protocol_examples_common.h | 7 +++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a7352d5fd..4f9ea953cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ if(${target} STREQUAL "linux") # Header only library for linux idf_component_register(INCLUDE_DIRS include - PRIV_REQUIRES tapif_io) + SRCS protocol_examples_utils.c) return() endif() diff --git a/connect.c b/connect.c index 8a7e28266b..6debc20813 100644 --- a/connect.c +++ b/connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ diff --git a/include/example_common_private.h b/include/example_common_private.h index 4921e477ea..b85d26d636 100644 --- a/include/example_common_private.h +++ b/include/example_common_private.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index 7417c1e635..430cdae7f9 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -11,15 +11,18 @@ #include "sdkconfig.h" #include "esp_err.h" +#if !CONFIG_IDF_TARGET_LINUX #include "esp_netif.h" #if CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_eth.h" #endif +#endif // !CONFIG_IDF_TARGET_LINUX #ifdef __cplusplus extern "C" { #endif +#if !CONFIG_IDF_TARGET_LINUX #if CONFIG_EXAMPLE_CONNECT_WIFI #define EXAMPLE_NETIF_DESC_STA "example_netif_sta" #endif @@ -96,6 +99,10 @@ void example_register_wifi_connect_commands(void); esp_eth_handle_t get_example_eth_handle(void); #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET +#else +static inline esp_err_t example_connect(void) {return ESP_OK;} +#endif // !CONFIG_IDF_TARGET_LINUX + #ifdef __cplusplus } #endif From 5349391ea4f95ca607d97a4ea9160f52d9702bd9 Mon Sep 17 00:00:00 2001 From: xieqinan Date: Mon, 3 Apr 2023 14:48:22 +0800 Subject: [PATCH 63/91] comm_components: eth_start initializes spi bus with SPI_DMA_CH_AUTO Close https://github.com/espressif/esp-idf/issues/11083 --- eth_connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth_connect.c b/eth_connect.c index fce806c6aa..72cfa8987b 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -122,7 +122,7 @@ static esp_netif_t *eth_start(void) .quadwp_io_num = -1, .quadhd_io_num = -1, }; - ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); + ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, From afb49079a517dffb802f25b7e5d5977a76e18c45 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 29 May 2023 14:47:43 +0200 Subject: [PATCH 64/91] protocols/examples: Disable Wifi connection if not supported - Disable Kconfig option for Wifi if not supported by the SoC - Enable building mqtt examples when target is set to esp32h2 --- Kconfig.projbuild | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index fe92d24db4..7eb2a8fd6f 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -4,7 +4,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_WIFI bool "connect using WiFi interface" - depends on !IDF_TARGET_LINUX + depends on !IDF_TARGET_LINUX && SOC_WIFI_SUPPORTED default y help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. @@ -119,7 +119,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_ETHERNET bool "connect using Ethernet interface" depends on !IDF_TARGET_LINUX - default n + default y if !SOC_WIFI_SUPPORTED help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. Choose this option to connect with Ethernet From 5846a13b98114b7f97e1eea897009f6a67206f79 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 3 Oct 2023 17:38:18 +0200 Subject: [PATCH 65/91] fix(esp_netif): Mark esp_netif_next deprecated and fix usages * Uses netif_find_if() in IPv6 examples * Fixes esp_netif_next() usage in L2TAP --- connect.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/connect.c b/connect.c index d40870eb02..6abc2a3d24 100644 --- a/connect.c +++ b/connect.c @@ -43,23 +43,22 @@ bool example_is_our_netif(const char *prefix, esp_netif_t *netif) return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0; } +static bool netif_desc_matches_with(esp_netif_t *netif, void *ctx) +{ + return strcmp(ctx, esp_netif_get_desc(netif)) == 0; +} + esp_netif_t *get_example_netif_from_desc(const char *desc) { - esp_netif_t *netif = NULL; - while ((netif = esp_netif_next(netif)) != NULL) { - if (strcmp(esp_netif_get_desc(netif), desc) == 0) { - return netif; - } - } - return netif; + return esp_netif_find_if(netif_desc_matches_with, (void*)desc); } -void example_print_all_netif_ips(const char *prefix) +static esp_err_t print_all_ips_tcpip(void* ctx) { + const char *prefix = ctx; // iterate over active interfaces, and print out IPs of "our" netifs esp_netif_t *netif = NULL; - for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) { - netif = esp_netif_next(netif); + while ((netif = esp_netif_next_unsafe(netif)) != NULL) { if (example_is_our_netif(prefix, netif)) { ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); #if CONFIG_LWIP_IPV4 @@ -78,6 +77,13 @@ void example_print_all_netif_ips(const char *prefix) #endif } } + return ESP_OK; +} + +void example_print_all_netif_ips(const char *prefix) +{ + // Print all IPs in TCPIP context to avoid potential races of removing/adding netifs when iterating over the list + esp_netif_tcpip_exec(print_all_ips_tcpip, (void*) prefix); } From fb102ff098fbd72c27321b5760161e6ddf7a4659 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 4 Oct 2023 17:35:57 +0200 Subject: [PATCH 66/91] feat(examples): Add PPP to common connection component --- CMakeLists.txt | 8 + Kconfig.projbuild | 66 +++++++- README.md | 58 +++++++ connect.c | 10 ++ include/example_common_private.h | 3 + include/protocol_examples_common.h | 7 + ppp_connect.c | 260 +++++++++++++++++++++++++++++ 7 files changed, 411 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 ppp_connect.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f9ea953cf..a8f9b81c05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,10 @@ if(CONFIG_EXAMPLE_CONNECT_ETHERNET) list(APPEND srcs "eth_connect.c") endif() +if(CONFIG_EXAMPLE_CONNECT_PPP) + list(APPEND srcs "ppp_connect.c") +endif() + idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" @@ -34,3 +38,7 @@ endif() if(CONFIG_EXAMPLE_CONNECT_ETHERNET) idf_component_optional_requires(PRIVATE esp_eth) endif() + +if(CONFIG_EXAMPLE_CONNECT_PPP) + idf_component_optional_requires(PRIVATE esp_tinyusb espressif__esp_tinyusb) +endif() diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 7eb2a8fd6f..6329d3a475 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -306,16 +306,80 @@ menu "Example Connection Configuration" Set PHY address according your board schematic. endif # EXAMPLE_CONNECT_ETHERNET + config EXAMPLE_CONNECT_PPP + bool "connect using Point to Point interface" + select LWIP_PPP_SUPPORT + help + Protocol examples can use PPP connection over serial line. + Choose this option to connect to the ppp server running + on your laptop over a serial line (either UART or USB ACM) + + if EXAMPLE_CONNECT_PPP + choice EXAMPLE_CONNECT_PPP_DEVICE + prompt "Choose PPP device" + default EXAMPLE_CONNECT_PPP_DEVICE_USB + help + Select which peripheral to use to connect to the PPP server. + + config EXAMPLE_CONNECT_PPP_DEVICE_USB + bool "USB" + depends on SOC_USB_OTG_SUPPORTED + select TINYUSB_CDC_ENABLED + help + Use USB ACM device. + + config EXAMPLE_CONNECT_PPP_DEVICE_UART + bool "UART" + help + Use UART. + + endchoice + + menu "UART Configuration" + depends on EXAMPLE_CONNECT_PPP_DEVICE_UART + config EXAMPLE_CONNECT_UART_TX_PIN + int "TXD Pin Number" + default 4 + range 0 31 + help + Pin number of UART TX. + + config EXAMPLE_CONNECT_UART_RX_PIN + int "RXD Pin Number" + default 5 + range 0 31 + help + Pin number of UART RX. + + config EXAMPLE_CONNECT_UART_BAUDRATE + int "UART Baudrate" + default 115200 + range 9600 3000000 + help + Baudrate of the UART device + + endmenu + + config EXAMPLE_PPP_CONN_MAX_RETRY + int "Maximum retry" + default 6 + help + Set the Maximum retry to avoid station reconnecting if the pppd + is not available + + endif # EXAMPLE_CONNECT_PPP + config EXAMPLE_CONNECT_IPV4 bool depends on LWIP_IPV4 default y config EXAMPLE_CONNECT_IPV6 - depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET + depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET || EXAMPLE_CONNECT_PPP bool "Obtain IPv6 address" default y select LWIP_IPV6 + select LWIP_PPP_ENABLE_IPV6 if EXAMPLE_CONNECT_PPP help By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. Disable this option if the network does not support IPv6. diff --git a/README.md b/README.md new file mode 100644 index 0000000000..f80d6b2fa8 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# protocol_example_connect + +This component implements the most common connection methods for ESP32 boards. It should be used mainly in examples of ESP-IDF to demonstrate functionality of network protocols and other libraries, that need the connection step as a prerequisite. + +## How to use this component + +Choose the preferred interface (WiFi, Ethernet, PPPoS) to connect to the network and configure the interface. + +It is possible to enable multiple interfaces simultaneously making the connection phase to block until all the chosen interfaces acquire IP addresses. +It is also possible to disable all interfaces, skipping the connection phase altogether. + +### WiFi + +Choose WiFi connection method (for chipsets that support it) and configure basic WiFi connection properties: +* WiFi SSID +* WiFI password +* Maximum connection retry (connection would be aborted if it doesn't succeed after specified number of retries) +* WiFi scan method (including RSSI and authorization mode threshold) + + + +### Ethernet + +Choose Ethernet connection if your board supports it. The most common settings is using Espressif Ethernet Kit, which is also the recommended HW for this selection. You can also select an SPI ethernet device (if your chipset doesn't support internal EMAC or if you prefer). It is also possible to use OpenCores Ethernet MAC if you're running the example under QEMU. + +### PPP + +Point to point connection method creates a simple IP tunnel to the counterpart device (running PPP server), typically a Linux machine with pppd service. We currently support only PPP over Serial (using UART or USB CDC). This is useful for simple testing of networking layers, but with some additional configuration on the server side, we could simulate standard model of internet connectivity. The PPP server could be also represented by a cellular modem device with pre-configured connectivity and already switched to PPP mode (this setup is not very flexible though, so we suggest using a standard modem library implementing commands and modes, e.g. [esp_modem](https://components.espressif.com/component/espressif/esp_modem) ). + +> [!Note] +> Note that if you choose USB device, you have to manually add a dependency on `esp_tinyusb` component. This step is necessary to keep the `protocol_example_connect` component simple and dependency free. Please run this command from your project location to add the dependency: +> ```bash +> idf.py add-dependency espressif/esp_tinyusb^1 +> ``` + +#### Setup a PPP server + +Connect the board using UART or USB and note the device name, which would be typically: +* `/dev/ttyACMx` for USB devices +* `/dev/ttyUSBx` for UART devices + +Run the pppd server: + +```bash +sudo pppd /dev/ttyACM0 115200 192.168.11.1:192.168.11.2 ms-dns 8.8.8.8 modem local noauth debug nocrtscts nodetach +ipv6 +``` + +Please update the parameters with the correct serial device, baud rate, IP addresses, DNS server, use `+ipv6` if `EXAMPLE_CONNECT_IPV6=y`. + +#### Connection to outside + +In order to access other network endpoints, we have to configure some IP/translation rules. The easiest method is to setup a masquerade of the PPPD created interface (`ppp0`) to your default networking interface (`${ETH0}`). Here is an example of such rule: + +```bash +sudo iptables -t nat -A POSTROUTING -o ${ETH0} -j MASQUERADE +sudo iptables -A FORWARD -i ${ETH0} -o ppp0 -m state --state RELATED,ESTABLISHED -j ACCEPT +sudo iptables -A FORWARD -i ppp0 -o ${ETH0} -j ACCEPT +``` diff --git a/connect.c b/connect.c index 6abc2a3d24..f6aa9bbee8 100644 --- a/connect.c +++ b/connect.c @@ -101,6 +101,12 @@ esp_err_t example_connect(void) } ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown)); #endif +#if CONFIG_EXAMPLE_CONNECT_PPP + if (example_ppp_connect() != ESP_OK) { + return ESP_FAIL; + } + ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ppp_shutdown)); +#endif #if CONFIG_EXAMPLE_CONNECT_ETHERNET example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH); @@ -110,6 +116,10 @@ esp_err_t example_connect(void) example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA); #endif +#if CONFIG_EXAMPLE_CONNECT_PPP + example_print_all_netif_ips(EXAMPLE_NETIF_DESC_PPP); +#endif + return ESP_OK; } diff --git a/include/example_common_private.h b/include/example_common_private.h index b85d26d636..8a0b880ecb 100644 --- a/include/example_common_private.h +++ b/include/example_common_private.h @@ -45,6 +45,9 @@ void example_wifi_shutdown(void); esp_err_t example_wifi_connect(void); void example_ethernet_shutdown(void); esp_err_t example_ethernet_connect(void); +esp_err_t example_ppp_connect(void); +void example_ppp_start(void); +void example_ppp_shutdown(void); diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index 430cdae7f9..fc2e54cd35 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -31,6 +31,10 @@ extern "C" { #define EXAMPLE_NETIF_DESC_ETH "example_netif_eth" #endif +#if CONFIG_EXAMPLE_CONNECT_PPP +#define EXAMPLE_NETIF_DESC_PPP "example_netif_ppp" +#endif + /* Example default interface, prefer the ethernet one if running in example-test (CI) configuration */ #if CONFIG_EXAMPLE_CONNECT_ETHERNET #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH) @@ -38,6 +42,9 @@ extern "C" { #elif CONFIG_EXAMPLE_CONNECT_WIFI #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) #define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) +#elif CONFIG_EXAMPLE_CONNECT_PPP +#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP) +#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP) #endif /** diff --git a/ppp_connect.c b/ppp_connect.c new file mode 100644 index 0000000000..090a3ed69c --- /dev/null +++ b/ppp_connect.c @@ -0,0 +1,260 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include +#include +#include "sdkconfig.h" +#include "protocol_examples_common.h" +#include "example_common_private.h" + +#if CONFIG_EXAMPLE_CONNECT_PPP +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_netif_ppp.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +#include "tinyusb.h" +#include "tusb_cdc_acm.h" + +static int s_itf; +static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE]; + +#else // DEVICE is UART + +#include "driver/uart.h" +#define BUF_SIZE (1024) +static bool s_stop_task = false; + +#endif // CONNECT_PPP_DEVICE + + +static const char *TAG = "example_connect_ppp"; +static int s_retry_num = 0; +static EventGroupHandle_t s_event_group = NULL; +static esp_netif_t *s_netif; +static const int GOT_IPV4 = BIT0; +static const int CONNECTION_FAILED = BIT1; +#if CONFIG_EXAMPLE_CONNECT_IPV6 +static const int GOT_IPV6 = BIT2; +#define CONNECT_BITS (GOT_IPV4|GOT_IPV6|CONNECTION_FAILED) +#else +#define CONNECT_BITS (GOT_IPV4|CONNECTION_FAILED) +#endif + +static esp_err_t transmit(void *h, void *buffer, size_t len) +{ + ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE); +#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB + tinyusb_cdcacm_write_queue(s_itf, buffer, len); + tinyusb_cdcacm_write_flush(s_itf, 0); +#else // DEVICE_UART + uart_write_bytes(UART_NUM_1, buffer, len); +#endif // CONNECT_PPP_DEVICE + return ESP_OK; +} + +static esp_netif_driver_ifconfig_t driver_cfg = { + .handle = (void *)1, // singleton driver, just to != NULL + .transmit = transmit, +}; +const esp_netif_driver_ifconfig_t *ppp_driver_cfg = &driver_cfg; + +static void on_ip_event(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + + if (event_id == IP_EVENT_PPP_GOT_IP) { + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + if (!example_is_our_netif(EXAMPLE_NETIF_DESC_PPP, event->esp_netif)) { + return; + } + esp_netif_t *netif = event->esp_netif; + esp_netif_dns_info_t dns_info; + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info); + ESP_LOGI(TAG, "Main DNS server : " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4)); + xEventGroupSetBits(s_event_group, GOT_IPV4); +#if CONFIG_EXAMPLE_CONNECT_IPV6 + } else if (event_id == IP_EVENT_GOT_IP6) { + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + if (!example_is_our_netif(EXAMPLE_NETIF_DESC_PPP, event->esp_netif)) { + return; + } + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); + ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), + IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); + if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { + xEventGroupSetBits(s_event_group, GOT_IPV6); + } +#endif + } else if (event_id == IP_EVENT_PPP_LOST_IP) { + ESP_LOGI(TAG, "Disconnect from PPP Server"); + s_retry_num++; + if (s_retry_num > CONFIG_EXAMPLE_PPP_CONN_MAX_RETRY) { + ESP_LOGE(TAG, "PPP Connection failed %d times, stop reconnecting.", s_retry_num); + xEventGroupSetBits(s_event_group, CONNECTION_FAILED); + } else { + ESP_LOGI(TAG, "PPP Connection failed %d times, try to reconnect.", s_retry_num); + esp_netif_action_start(s_netif, 0, 0, 0); + esp_netif_action_connected(s_netif, 0, 0, 0); + } + + } +} + +#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +static void cdc_rx_callback(int itf, cdcacm_event_t *event) +{ + size_t rx_size = 0; + if (itf != s_itf) { + // Not our channel + return; + } + esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size); + if (ret == ESP_OK) { + ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_VERBOSE); + // pass the received data to the network interface + esp_netif_receive(s_netif, buf, rx_size, NULL); + } else { + ESP_LOGE(TAG, "Read error"); + } +} + +static void line_state_changed(int itf, cdcacm_event_t *event) +{ + s_itf = itf; // use this channel for the netif communication + ESP_LOGI(TAG, "Line state changed on channel %d", itf); +} +#else // DEVICE is UART + +static void ppp_task(void *args) +{ + uart_config_t uart_config = {}; + uart_config.baud_rate = CONFIG_EXAMPLE_CONNECT_UART_BAUDRATE; + uart_config.data_bits = UART_DATA_8_BITS; + uart_config.parity = UART_PARITY_DISABLE; + uart_config.stop_bits = UART_STOP_BITS_1; + uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; + uart_config.source_clk = UART_SCLK_DEFAULT; + + QueueHandle_t event_queue; + ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, BUF_SIZE, 0, 16, &event_queue, 0)); + ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart_config)); + ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, CONFIG_EXAMPLE_CONNECT_UART_TX_PIN, CONFIG_EXAMPLE_CONNECT_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); + ESP_ERROR_CHECK(uart_set_rx_timeout(UART_NUM_1, 1)); + + char *buffer = (char*)malloc(BUF_SIZE); + uart_event_t event; + esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, s_netif); + esp_netif_action_start(s_netif, 0, 0, 0); + esp_netif_action_connected(s_netif, 0, 0, 0); + while (!s_stop_task) { + xQueueReceive(event_queue, &event, pdMS_TO_TICKS(1000)); + if (event.type == UART_DATA) { + size_t len; + uart_get_buffered_data_len(UART_NUM_1, &len); + if (len) { + len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0); + ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE); + esp_netif_receive(s_netif, buffer, len, NULL); + } + } else { + ESP_LOGW(TAG, "Received UART event: %d", event.type); + } + } + free(buffer); + vTaskDelete(NULL); +} + +#endif // CONNECT_PPP_DEVICE + +esp_err_t example_ppp_connect(void) +{ + ESP_LOGI(TAG, "Start example_connect."); + +#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB + ESP_LOGI(TAG, "USB initialization"); + const tinyusb_config_t tusb_cfg = { + .device_descriptor = NULL, + .string_descriptor = NULL, + .external_phy = false, + .configuration_descriptor = NULL, + }; + + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); + + tinyusb_config_cdcacm_t acm_cfg = { + .usb_dev = TINYUSB_USBDEV_0, + .cdc_port = TINYUSB_CDC_ACM_0, + .callback_rx = &cdc_rx_callback, + .callback_rx_wanted_char = NULL, + .callback_line_state_changed = NULL, + .callback_line_coding_changed = NULL + }; + + ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + /* the second way to register a callback */ + ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( + TINYUSB_CDC_ACM_0, + CDC_EVENT_LINE_STATE_CHANGED, + &line_state_changed)); +#endif // CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB + + s_event_group = xEventGroupCreate(); + + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event, NULL)); + + esp_netif_inherent_config_t base_netif_cfg = ESP_NETIF_INHERENT_DEFAULT_PPP(); + base_netif_cfg.if_desc = EXAMPLE_NETIF_DESC_PPP; + esp_netif_config_t netif_ppp_config = { .base = &base_netif_cfg, + .driver = ppp_driver_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_PPP + }; + + s_netif = esp_netif_new(&netif_ppp_config); + assert(s_netif); +#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB + esp_netif_action_start(s_netif, 0, 0, 0); + esp_netif_action_connected(s_netif, 0, 0, 0); +#else // DEVICE is UART + s_stop_task = false; + if (xTaskCreate(ppp_task, "ppp connect", 4096, NULL, 5, NULL) != pdTRUE) { + ESP_LOGE(TAG, "Failed to create a ppp connection task"); + return ESP_FAIL; + } +#endif // CONNECT_PPP_DEVICE + + ESP_LOGI(TAG, "Waiting for IP address"); + EventBits_t bits = xEventGroupWaitBits(s_event_group, CONNECT_BITS, pdFALSE, pdFALSE, portMAX_DELAY); + if (bits & CONNECTION_FAILED) { + ESP_LOGE(TAG, "Connection failed!"); + return ESP_FAIL; + } + ESP_LOGI(TAG, "Connected!"); + + return ESP_OK; +} + +void example_ppp_shutdown(void) +{ + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event)); +#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_UART + s_stop_task = true; + vTaskDelay(pdMS_TO_TICKS(1000)); // wait for the ppp task to stop +#endif + + esp_netif_action_disconnected(s_netif, 0, 0, 0); + + vEventGroupDelete(s_event_group); + esp_netif_action_stop(s_netif, 0, 0, 0); + esp_netif_destroy(s_netif); + s_netif = NULL; + s_event_group = NULL; +} + +#endif // CONFIG_EXAMPLE_CONNECT_PPP From 1c3ff83419dd6a09ae944341d5e32030051aebb0 Mon Sep 17 00:00:00 2001 From: Song Ruo Jing Date: Fri, 24 Nov 2023 14:56:08 +0800 Subject: [PATCH 67/91] refactor(uart_vfs): Move uart implementation of vfs to esp_driver_uart Deprecated esp_vfs_dev_uart_xxx APIs vfs_uart test case moved to esp_driver_uart test_apps Astyle fixed for uart_vfs --- stdin_out.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdin_out.c b/stdin_out.c index 5fbc8a38af..9f3a5163f3 100644 --- a/stdin_out.c +++ b/stdin_out.c @@ -9,7 +9,7 @@ #include "protocol_examples_common.h" #include "esp_err.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" #include "sdkconfig.h" @@ -25,10 +25,10 @@ esp_err_t example_configure_stdin_stdout(void) ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0) ); /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); - esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); + uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); configured = true; return ESP_OK; } From f9a5c76218ef7570c5784c0c67eae6465a9755ef Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Sun, 31 Dec 2023 13:55:10 +0800 Subject: [PATCH 68/91] refactor(parlio): make parlio driver as component --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8f9b81c05..e2470c7efa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ endif() idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" - PRIV_REQUIRES esp_netif driver esp_wifi vfs) + PRIV_REQUIRES esp_netif driver esp_wifi vfs console esp_eth) if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) idf_component_optional_requires(PRIVATE console) From fd50379eba79e292c3e7864d839c531a2e9b342e Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Fri, 19 Jan 2024 09:42:33 +0100 Subject: [PATCH 69/91] fix(examples): fixed common_connect example to support ESP32P4 internal EMAC --- Kconfig.projbuild | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 6329d3a475..a5b654d50e 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -136,13 +136,13 @@ menu "Example Connection Configuration" choice EXAMPLE_ETHERNET_TYPE prompt "Ethernet Type" - default EXAMPLE_USE_INTERNAL_ETHERNET if IDF_TARGET_ESP32 + default EXAMPLE_USE_INTERNAL_ETHERNET if SOC_EMAC_SUPPORTED default EXAMPLE_USE_W5500 help Select which kind of Ethernet will be used in the example. config EXAMPLE_USE_INTERNAL_ETHERNET - depends on IDF_TARGET_ESP32 + depends on SOC_EMAC_SUPPORTED select ETH_USE_ESP32_EMAC bool "Internal EMAC" help @@ -227,14 +227,16 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_MDC_GPIO int "SMI MDC GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX - default 23 + default 23 if IDF_TARGET_ESP32 + default 31 if IDF_TARGET_ESP32P4 help Set the GPIO number used by SMI MDC. config EXAMPLE_ETH_MDIO_GPIO int "SMI MDIO GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX - default 18 + default 18 if IDF_TARGET_ESP32 + default 27 if IDF_TARGET_ESP32P4 help Set the GPIO number used by SMI MDIO. endif @@ -293,6 +295,7 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" range -1 ENV_GPIO_OUT_RANGE_MAX + default 26 if IDF_TARGET_ESP32P4 default 5 help Set the GPIO number used to reset PHY chip. From f3b02bbaae36428500ca0ef83296168e1fd6944a Mon Sep 17 00:00:00 2001 From: Tomas Rezucha Date: Mon, 22 Jan 2024 21:51:37 +0800 Subject: [PATCH 70/91] fix(examples): Make esp_eth public dependency of protocol_examples_comon --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e2470c7efa..d8bccb1e99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,7 @@ if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) endif() if(CONFIG_EXAMPLE_CONNECT_ETHERNET) - idf_component_optional_requires(PRIVATE esp_eth) + idf_component_optional_requires(PUBLIC esp_eth) endif() if(CONFIG_EXAMPLE_CONNECT_PPP) From bda57cc54340f83699e1fb358a03b67a26b4a4d9 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Fri, 26 Apr 2024 12:27:54 +0200 Subject: [PATCH 71/91] feat(esp_eth): a new folder structure of the driver and other improvements Fixed memory leak in emac_esp_new_dma function. Polished ESP EMAC cache management. Added emac_periph definitions based on SoC features and improved(generalized) ESP EMAC GPIO initialization. Added ESP EMAC GPIO reservation. Added check for frame error condition indicated by EMAC DMA and created a target test. --- eth_connect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eth_connect.c b/eth_connect.c index 72cfa8987b..74f3cb3b24 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -99,8 +99,8 @@ static esp_netif_t *eth_start(void) phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); - esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + esp32_emac_config.smi_gpio.mdc_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + esp32_emac_config.smi_gpio.mdio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 s_phy = esp_eth_phy_new_ip101(&phy_config); From 4bc1a8ff8916843abd116b163d05cffb5ab2a15f Mon Sep 17 00:00:00 2001 From: zwx Date: Thu, 23 May 2024 16:02:15 +0800 Subject: [PATCH 72/91] feat(eth) configure eth mac using esp_read_mac * Closes https://github.com/espressif/esp-idf/issues/13808 --- eth_connect.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/eth_connect.c b/eth_connect.c index 74f3cb3b24..8e89dbe28e 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -13,6 +13,7 @@ #include "driver/spi_master.h" #endif // CONFIG_ETH_USE_SPI_ETHERNET #include "esp_log.h" +#include "esp_mac.h" #include "driver/gpio.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -153,11 +154,12 @@ static esp_netif_t *eth_start(void) ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); #if !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET /* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually. - 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control. + We set the ESP_MAC_ETH mac address as the default, if you want to use ESP_MAC_EFUSE_CUSTOM mac address, please enable the + configuration: `ESP_MAC_USE_CUSTOM_MAC_AS_BASE_MAC` */ - ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) { - 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 - })); + uint8_t eth_mac[6] = {0}; + ESP_ERROR_CHECK(esp_read_mac(eth_mac, ESP_MAC_ETH)); + ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, eth_mac)); #endif // combine driver with netif s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); From d7efe211d3bc61c9df0ece4b3d3c75e23550f4e3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 26 Sep 2023 07:20:51 +0200 Subject: [PATCH 73/91] fix(examples): Add wifi_remote option to common connect example * Add MQTT test configuration with WiFi on ESP32-P4 * Document esp_wifi_remote workflow in the example's README --- Kconfig.projbuild | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index a5b654d50e..1b450b9236 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -4,8 +4,8 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_WIFI bool "connect using WiFi interface" - depends on !IDF_TARGET_LINUX && SOC_WIFI_SUPPORTED - default y + depends on !IDF_TARGET_LINUX && (SOC_WIFI_SUPPORTED || ESP_WIFI_REMOTE_ENABLED) + default y if SOC_WIFI_SUPPORTED help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. Choose this option to connect with WiFi @@ -119,7 +119,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_ETHERNET bool "connect using Ethernet interface" depends on !IDF_TARGET_LINUX - default y if !SOC_WIFI_SUPPORTED + default y if !EXAMPLE_CONNECT_WIFI help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. Choose this option to connect with Ethernet @@ -218,7 +218,7 @@ menu "Example Connection Configuration" bool "KSZ80xx" help With the KSZ80xx series, Microchip offers single-chip 10BASE-T/100BASE-TX - Ethernet Physical Layer Tranceivers (PHY). + Ethernet Physical Layer Transceivers (PHY). The following chips are supported: KSZ8001, KSZ8021, KSZ8031, KSZ8041, KSZ8051, KSZ8061, KSZ8081, KSZ8091 Goto https://www.microchip.com for more information about them. From e20546b71525fd6b3b2b1c0ff855882c729ea4fd Mon Sep 17 00:00:00 2001 From: Xu Si Yu Date: Wed, 26 Jun 2024 19:55:10 +0800 Subject: [PATCH 74/91] fix(common_components): unregister event handler if wifi disconnect and stop reconnecting --- include/protocol_examples_common.h | 32 +++++++++++++++++++++++++++ wifi_connect.c | 35 ++---------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index fc2e54cd35..b23f452acc 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -35,6 +35,38 @@ extern "C" { #define EXAMPLE_NETIF_DESC_PPP "example_netif_ppp" #endif +#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST +#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN +#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL +#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN +#endif + +#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL +#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL +#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY +#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY +#endif + +#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN +#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK +#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK +#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK +#endif + /* Example default interface, prefer the ethernet one if running in example-test (CI) configuration */ #if CONFIG_EXAMPLE_CONNECT_ETHERNET #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH) diff --git a/wifi_connect.c b/wifi_connect.c index e4e1468688..ed95bccb2f 100644 --- a/wifi_connect.c +++ b/wifi_connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -26,38 +26,6 @@ static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; #endif -#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST -#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN -#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL -#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN -#endif - -#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL -#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL -#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY -#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY -#endif - -#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN -#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK -#endif - static int s_retry_num = 0; static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base, @@ -75,6 +43,7 @@ static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event xSemaphoreGive(s_semph_get_ip6_addrs); } #endif + example_wifi_sta_do_disconnect(); return; } ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); From 76a0db7f5ac499ea4c3758293b032ebcfe874463 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Thu, 8 Aug 2024 18:11:38 +0200 Subject: [PATCH 75/91] fix(esp_eth): fixed default GPIO to match new ver. of P4 devboard --- Kconfig.projbuild | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 1b450b9236..800f136a09 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -236,7 +236,7 @@ menu "Example Connection Configuration" int "SMI MDIO GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 18 if IDF_TARGET_ESP32 - default 27 if IDF_TARGET_ESP32P4 + default 52 if IDF_TARGET_ESP32P4 help Set the GPIO number used by SMI MDIO. endif @@ -295,7 +295,7 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" range -1 ENV_GPIO_OUT_RANGE_MAX - default 26 if IDF_TARGET_ESP32P4 + default 51 if IDF_TARGET_ESP32P4 default 5 help Set the GPIO number used to reset PHY chip. From 61607b80968c4bebf66eca20ee4c53539a79a130 Mon Sep 17 00:00:00 2001 From: jgujarathi Date: Mon, 1 Jul 2024 12:18:43 +0530 Subject: [PATCH 76/91] fix(examples) : Common component's wifi disconnect handler should ignore roaming disconnect - Common component's wifi disconnect handler should ignore roaming disconnect (WIFI_REASON_ROAMING) as connect gets issued internally in these cases. --- wifi_connect.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/wifi_connect.c b/wifi_connect.c index ed95bccb2f..1bfc1b35f4 100644 --- a/wifi_connect.c +++ b/wifi_connect.c @@ -46,7 +46,12 @@ static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event example_wifi_sta_do_disconnect(); return; } - ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); + wifi_event_sta_disconnected_t *disconn = event_data; + if (disconn->reason == WIFI_REASON_ROAMING) { + ESP_LOGD(TAG, "station roaming, do nothing"); + return; + } + ESP_LOGI(TAG, "Wi-Fi disconnected %d, trying to reconnect...", disconn->reason); esp_err_t err = esp_wifi_connect(); if (err == ESP_ERR_WIFI_NOT_STARTED) { return; From 9af5f6f71c6161c71a829c29362127d482916a9e Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Thu, 19 Sep 2024 15:54:26 +0200 Subject: [PATCH 77/91] feat(esp_eth): added new generic PHY driver --- Kconfig.projbuild | 9 +++++++++ eth_connect.c | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 800f136a09..a890bfbe07 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -183,6 +183,15 @@ menu "Example Connection Configuration" help Select the Ethernet PHY device to use in the example. + config EXAMPLE_ETH_PHY_GENERIC + bool "Generic 802.3 PHY" + help + Any Ethernet PHY chip compliant with IEEE 802.3 can be used. However, while + basic functionality should always work, some specific features might be limited, + even if the PHY meets IEEE 802.3 standard. A typical example is loopback + functionality, where certain PHYs may require setting a specific speed mode to + operate correctly. + config EXAMPLE_ETH_PHY_IP101 bool "IP101" help diff --git a/eth_connect.c b/eth_connect.c index 8e89dbe28e..86e856527c 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -103,7 +103,9 @@ static esp_netif_t *eth_start(void) esp32_emac_config.smi_gpio.mdc_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; esp32_emac_config.smi_gpio.mdio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); -#if CONFIG_EXAMPLE_ETH_PHY_IP101 +#if CONFIG_EXAMPLE_ETH_PHY_GENERIC + s_phy = esp_eth_phy_new_generic(&phy_config); +#elif CONFIG_EXAMPLE_ETH_PHY_IP101 s_phy = esp_eth_phy_new_ip101(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 s_phy = esp_eth_phy_new_rtl8201(&phy_config); From c735d730629b6197fb2e825a647d53ae4d313495 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sun, 15 Sep 2024 11:22:51 +0200 Subject: [PATCH 78/91] fix(build): clean up dependencies on driver component --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8bccb1e99..1af6081ddf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ endif() idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" - PRIV_REQUIRES esp_netif driver esp_wifi vfs console esp_eth) + PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console esp_eth) if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) idf_component_optional_requires(PRIVATE console) From 3a5ad7b179fcb4dad6f0589d70b4f2c5e907e752 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 1 Oct 2024 19:33:52 +0200 Subject: [PATCH 79/91] fix(common_connect): Fix example's stdin/out to setup UART interrupt once Function example_configure_stdin_stdout() was used for simple UART I/O operation in CI to enter test env configuration (wifi ssid, IPs, etc). It could be called multiple times, but didn't handle the situation where we install UART interrupt from multiple source (e.g. in ICMP tests, where we first need to enter wifi credentials of test AP and then we start ping-cmd console to handle ping commands) --- stdin_out.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdin_out.c b/stdin_out.c index 9f3a5163f3..57c61fb8f8 100644 --- a/stdin_out.c +++ b/stdin_out.c @@ -15,8 +15,7 @@ esp_err_t example_configure_stdin_stdout(void) { - static bool configured = false; - if (configured) { + if (uart_is_driver_installed((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM)) { return ESP_OK; } // Initialize VFS & UART so we can use std::cout/cin @@ -29,6 +28,5 @@ esp_err_t example_configure_stdin_stdout(void) uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); - configured = true; return ESP_OK; } From c1dddd8d206757da90718b6155b9504a6648f7a4 Mon Sep 17 00:00:00 2001 From: yinqingzhao Date: Wed, 6 Nov 2024 15:17:31 +0800 Subject: [PATCH 80/91] feat(wifi): avoid compiling components related to wifi when wifi is not supported --- Kconfig.projbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index a890bfbe07..50a973bffc 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -4,7 +4,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_WIFI bool "connect using WiFi interface" - depends on !IDF_TARGET_LINUX && (SOC_WIFI_SUPPORTED || ESP_WIFI_REMOTE_ENABLED) + depends on !IDF_TARGET_LINUX && (SOC_WIFI_SUPPORTED || ESP_WIFI_REMOTE_ENABLED || ESP_HOST_WIFI_ENABLED) default y if SOC_WIFI_SUPPORTED help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. From b399cc1c264383785e59b9aa75a4c294ad5ee956 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 9 Dec 2024 10:51:19 +0100 Subject: [PATCH 81/91] fix(protocol_examples_common): don't override MAC address for openeth The intention of the code block was to set MAC address for SPI Ethernet modules, however !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET also affected the case of CONFIG_EXAMPLE_USE_OPENETH. This commit corrects the code to match the original intention. Related to https://github.com/espressif/qemu/issues/107 --- eth_connect.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/eth_connect.c b/eth_connect.c index 86e856527c..0cd105621d 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -154,7 +154,8 @@ static esp_netif_t *eth_start(void) // Install Ethernet driver esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); -#if !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET + +#if CONFIG_EXAMPLE_USE_SPI_ETHERNET /* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually. We set the ESP_MAC_ETH mac address as the default, if you want to use ESP_MAC_EFUSE_CUSTOM mac address, please enable the configuration: `ESP_MAC_USE_CUSTOM_MAC_AS_BASE_MAC` @@ -162,7 +163,8 @@ static esp_netif_t *eth_start(void) uint8_t eth_mac[6] = {0}; ESP_ERROR_CHECK(esp_read_mac(eth_mac, ESP_MAC_ETH)); ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, eth_mac)); -#endif +#endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET + // combine driver with netif s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); esp_netif_attach(netif, s_eth_glue); From d24391e6065fbbab85907bbcd675cd7a990c919d Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Tue, 12 Nov 2024 14:39:14 +0800 Subject: [PATCH 82/91] feat(protocol_examples_common): Add Thread connect to support Thread for the protocol examples --- CMakeLists.txt | 10 +- Kconfig.projbuild | 78 ++++++++++++- README.md | 10 +- connect.c | 16 ++- include/example_common_private.h | 4 +- include/protocol_examples_common.h | 7 ++ include/protocol_examples_thread_config.h | 105 +++++++++++++++++ thread_connect.c | 130 ++++++++++++++++++++++ 8 files changed, 350 insertions(+), 10 deletions(-) create mode 100644 include/protocol_examples_thread_config.h create mode 100644 thread_connect.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1af6081ddf..8d0501a9b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,10 @@ if(CONFIG_EXAMPLE_CONNECT_ETHERNET) list(APPEND srcs "eth_connect.c") endif() +if(CONFIG_EXAMPLE_CONNECT_THREAD) + list(APPEND srcs "thread_connect.c") +endif() + if(CONFIG_EXAMPLE_CONNECT_PPP) list(APPEND srcs "ppp_connect.c") endif() @@ -29,7 +33,7 @@ endif() idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" - PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console esp_eth) + PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console esp_eth openthread) if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) idf_component_optional_requires(PRIVATE console) @@ -39,6 +43,10 @@ if(CONFIG_EXAMPLE_CONNECT_ETHERNET) idf_component_optional_requires(PUBLIC esp_eth) endif() +if(CONFIG_EXAMPLE_CONNECT_THREAD) + idf_component_optional_requires(PRIVATE openthread) +endif() + if(CONFIG_EXAMPLE_CONNECT_PPP) idf_component_optional_requires(PRIVATE esp_tinyusb espressif__esp_tinyusb) endif() diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 50a973bffc..f9ccbfb569 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -7,7 +7,7 @@ menu "Example Connection Configuration" depends on !IDF_TARGET_LINUX && (SOC_WIFI_SUPPORTED || ESP_WIFI_REMOTE_ENABLED || ESP_HOST_WIFI_ENABLED) default y if SOC_WIFI_SUPPORTED help - Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. + Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. Choose this option to connect with WiFi if EXAMPLE_CONNECT_WIFI @@ -119,9 +119,9 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_ETHERNET bool "connect using Ethernet interface" depends on !IDF_TARGET_LINUX - default y if !EXAMPLE_CONNECT_WIFI + default y if !EXAMPLE_CONNECT_WIFI && !EXAMPLE_CONNECT_THREAD help - Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. + Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. Choose this option to connect with Ethernet if EXAMPLE_CONNECT_ETHERNET @@ -381,13 +381,83 @@ menu "Example Connection Configuration" endif # EXAMPLE_CONNECT_PPP + config EXAMPLE_CONNECT_THREAD + bool "Connect using Thread interface" + depends on !IDF_TARGET_LINUX && OPENTHREAD_ENABLED + default y if SOC_IEEE802154_SUPPORTED + select EXAMPLE_CONNECT_IPV6 + help + Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. + Choose this option to connect with Thread. + The operational active dataset of the Thread network can be configured in openthread + component at '->Components->OpenThread->Thread Core Features->Thread Operational Dataset' + + if EXAMPLE_CONNECT_THREAD + config EXAMPLE_THREAD_TASK_STACK_SIZE + int "Example Thread task stack size" + default 8192 + help + Thread task stack size + + menu "Radio Spinel Options" + depends on OPENTHREAD_RADIO_SPINEL_UART || OPENTHREAD_RADIO_SPINEL_SPI + + config EXAMPLE_THREAD_UART_RX_PIN + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart Rx Pin" + default 17 + + config EXAMPLE_THREAD_UART_TX_PIN + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart Tx pin" + default 18 + + config EXAMPLE_THREAD_UART_BAUD + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart baud rate" + default 460800 + + config EXAMPLE_THREAD_UART_PORT + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart port" + default 1 + + config EXAMPLE_THREAD_SPI_CS_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI CS Pin" + default 10 + + config EXAMPLE_THREAD_SPI_SCLK_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI SCLK Pin" + default 12 + + config EXAMPLE_THREAD_SPI_MISO_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI MISO Pin" + default 13 + + config EXAMPLE_THREAD_SPI_MOSI_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI MOSI Pin" + default 11 + + config EXAMPLE_THREAD_SPI_INTR_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI Interrupt Pin" + default 8 + endmenu + + endif + config EXAMPLE_CONNECT_IPV4 bool depends on LWIP_IPV4 + default n if EXAMPLE_CONNECT_THREAD default y config EXAMPLE_CONNECT_IPV6 - depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET || EXAMPLE_CONNECT_PPP + depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET || EXAMPLE_CONNECT_PPP || EXAMPLE_CONNECT_THREAD bool "Obtain IPv6 address" default y select LWIP_IPV6 diff --git a/README.md b/README.md index f80d6b2fa8..00111dbfc5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This component implements the most common connection methods for ESP32 boards. I ## How to use this component -Choose the preferred interface (WiFi, Ethernet, PPPoS) to connect to the network and configure the interface. +Choose the preferred interface (WiFi, Ethernet, Thread, PPPoS) to connect to the network and configure the interface. It is possible to enable multiple interfaces simultaneously making the connection phase to block until all the chosen interfaces acquire IP addresses. It is also possible to disable all interfaces, skipping the connection phase altogether. @@ -23,6 +23,14 @@ Choose WiFi connection method (for chipsets that support it) and configure basic Choose Ethernet connection if your board supports it. The most common settings is using Espressif Ethernet Kit, which is also the recommended HW for this selection. You can also select an SPI ethernet device (if your chipset doesn't support internal EMAC or if you prefer). It is also possible to use OpenCores Ethernet MAC if you're running the example under QEMU. +### Thread + +Choose Thread connection if your board supports IEEE802.15.4 native radio or works with [OpenThread RCP](../../openthread/ot_rcp/README.md). You can configure the Thread network at menuconfig '->Components->OpenThread->Thread Core Features->Thread Operational Dataset'. + +If the Thread end-device joins a Thread network with a Thread Border Router that has the NAT64 feature enabled, the end-device can access the Internet with the standard DNS APIs after configuring the following properties: +* Enable DNS64 client ('->Components->OpenThread->Thread Core Features->Enable DNS64 client') +* Enable custom DNS external resolve Hook ('->Components->LWIP->Hooks->DNS external resolve Hook->Custom implementation') + ### PPP Point to point connection method creates a simple IP tunnel to the counterpart device (running PPP server), typically a Linux machine with pppd service. We currently support only PPP over Serial (using UART or USB CDC). This is useful for simple testing of networking layers, but with some additional configuration on the server side, we could simulate standard model of internet connectivity. The PPP server could be also represented by a cellular modem device with pre-configured connectivity and already switched to PPP mode (this setup is not very flexible though, so we suggest using a standard modem library implementing commands and modes, e.g. [esp_modem](https://components.espressif.com/component/espressif/esp_modem) ). diff --git a/connect.c b/connect.c index f6aa9bbee8..65fa2b8568 100644 --- a/connect.c +++ b/connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -35,7 +35,7 @@ const char *example_ipv6_addr_types_to_str[6] = { /** * @brief Checks the netif description if it contains specified prefix. - * All netifs created withing common connect component are prefixed with the module TAG, + * All netifs created within common connect component are prefixed with the module TAG, * so it returns true if the specified netif is owned by this module */ bool example_is_our_netif(const char *prefix, esp_netif_t *netif) @@ -61,7 +61,7 @@ static esp_err_t print_all_ips_tcpip(void* ctx) while ((netif = esp_netif_next_unsafe(netif)) != NULL) { if (example_is_our_netif(prefix, netif)) { ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); -#if CONFIG_LWIP_IPV4 +#if CONFIG_EXAMPLE_CONNECT_IPV4 esp_netif_ip_info_t ip; ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip)); @@ -101,6 +101,12 @@ esp_err_t example_connect(void) } ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown)); #endif +#if CONFIG_EXAMPLE_CONNECT_THREAD + if (example_thread_connect() != ESP_OK) { + return ESP_FAIL; + } + ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_thread_shutdown)); +#endif #if CONFIG_EXAMPLE_CONNECT_PPP if (example_ppp_connect() != ESP_OK) { return ESP_FAIL; @@ -116,6 +122,10 @@ esp_err_t example_connect(void) example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA); #endif +#if CONFIG_EXAMPLE_CONNECT_THREAD + example_print_all_netif_ips(EXAMPLE_NETIF_DESC_THREAD); +#endif + #if CONFIG_EXAMPLE_CONNECT_PPP example_print_all_netif_ips(EXAMPLE_NETIF_DESC_PPP); #endif diff --git a/include/example_common_private.h b/include/example_common_private.h index 8a0b880ecb..ad6ef537e4 100644 --- a/include/example_common_private.h +++ b/include/example_common_private.h @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ -/* Private Funtions of protocol example common */ +/* Private Functions of protocol example common */ #pragma once @@ -45,6 +45,8 @@ void example_wifi_shutdown(void); esp_err_t example_wifi_connect(void); void example_ethernet_shutdown(void); esp_err_t example_ethernet_connect(void); +void example_thread_shutdown(void); +esp_err_t example_thread_connect(void); esp_err_t example_ppp_connect(void); void example_ppp_start(void); void example_ppp_shutdown(void); diff --git a/include/protocol_examples_common.h b/include/protocol_examples_common.h index b23f452acc..54e2c07a11 100644 --- a/include/protocol_examples_common.h +++ b/include/protocol_examples_common.h @@ -31,6 +31,10 @@ extern "C" { #define EXAMPLE_NETIF_DESC_ETH "example_netif_eth" #endif +#if CONFIG_EXAMPLE_CONNECT_THREAD +#define EXAMPLE_NETIF_DESC_THREAD "example_netif_thread" +#endif + #if CONFIG_EXAMPLE_CONNECT_PPP #define EXAMPLE_NETIF_DESC_PPP "example_netif_ppp" #endif @@ -74,6 +78,9 @@ extern "C" { #elif CONFIG_EXAMPLE_CONNECT_WIFI #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) #define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) +#elif CONFIG_EXAMPLE_CONNECT_THREAD +#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_THREAD) +#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_THREAD) #elif CONFIG_EXAMPLE_CONNECT_PPP #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP) #define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP) diff --git a/include/protocol_examples_thread_config.h b/include/protocol_examples_thread_config.h new file mode 100644 index 0000000000..306a9270cb --- /dev/null +++ b/include/protocol_examples_thread_config.h @@ -0,0 +1,105 @@ +/* + * Thread configurations for protocol examples + * + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once + +#include + +#include + +#ifdef CONFIG_OPENTHREAD_RADIO_NATIVE +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_NATIVE, \ + } + +#elif defined(CONFIG_OPENTHREAD_RADIO_SPINEL_UART) +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_UART_RCP, \ + .radio_uart_config = \ + { \ + .port = CONFIG_EXAMPLE_THREAD_UART_PORT, \ + .uart_config = \ + { \ + .baud_rate = CONFIG_EXAMPLE_THREAD_UART_BAUD, \ + .data_bits = UART_DATA_8_BITS, \ + .parity = UART_PARITY_DISABLE, \ + .stop_bits = UART_STOP_BITS_1, \ + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \ + .rx_flow_ctrl_thresh = 0, \ + .source_clk = UART_SCLK_DEFAULT, \ + }, \ + .rx_pin = CONFIG_EXAMPLE_THREAD_UART_RX_PIN, \ + .tx_pin = CONFIG_EXAMPLE_THREAD_UART_TX_PIN, \ + }, \ + } +#elif defined(CONFIG_OPENTHREAD_RADIO_SPINEL_SPI) +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_SPI_RCP, \ + .radio_spi_config = \ + { \ + .host_device = SPI2_HOST, \ + .dma_channel = 2, \ + .spi_interface = \ + { \ + .mosi_io_num = CONFIG_EXAMPLE_THREAD_SPI_MOSI_PIN, \ + .miso_io_num = CONFIG_EXAMPLE_THREAD_SPI_MISO_PIN, \ + .sclk_io_num = CONFIG_EXAMPLE_THREAD_SPI_SCLK_PIN, \ + .quadwp_io_num = -1, \ + .quadhd_io_num = -1, \ + }, \ + .spi_device = \ + { \ + .cs_ena_pretrans = 2, \ + .input_delay_ns = 100, \ + .mode = 0, \ + .clock_speed_hz = 2500 * 1000, \ + .spics_io_num = CONFIG_EXAMPLE_THREAD_SPI_CS_PIN, \ + .queue_size = 5, \ + }, \ + .intr_pin = CONFIG_EXAMPLE_THREAD_SPI_INTR_PIN, \ + }, \ + } +#endif + +#if CONFIG_OPENTHREAD_CONSOLE_TYPE_UART +#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \ + { \ + .host_connection_mode = HOST_CONNECTION_MODE_CLI_UART, \ + .host_uart_config = \ + { \ + .port = 0, \ + .uart_config = \ + { \ + .baud_rate = 115200, \ + .data_bits = UART_DATA_8_BITS, \ + .parity = UART_PARITY_DISABLE, \ + .stop_bits = UART_STOP_BITS_1, \ + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \ + .rx_flow_ctrl_thresh = 0, \ + .source_clk = UART_SCLK_DEFAULT, \ + }, \ + .rx_pin = UART_PIN_NO_CHANGE, \ + .tx_pin = UART_PIN_NO_CHANGE, \ + }, \ + } +#elif CONFIG_OPENTHREAD_CONSOLE_TYPE_USB_SERIAL_JTAG +#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \ + { \ + .host_connection_mode = HOST_CONNECTION_MODE_CLI_USB, \ + .host_usb_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(), \ + } +#endif + +#define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \ + { \ + .storage_partition_name = "nvs", \ + .netif_queue_size = 10, \ + .task_queue_size = 10, \ + } diff --git a/thread_connect.c b/thread_connect.c new file mode 100644 index 0000000000..f4dae26e56 --- /dev/null +++ b/thread_connect.c @@ -0,0 +1,130 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include "esp_err.h" +#include "esp_event.h" +#include "esp_event_base.h" +#include "esp_vfs_eventfd.h" +#include "example_common_private.h" +#include "protocol_examples_common.h" +#include "protocol_examples_thread_config.h" +#include "esp_log.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +static TaskHandle_t s_ot_task_handle = NULL; +static esp_netif_t *s_openthread_netif = NULL; +static SemaphoreHandle_t s_semph_thread_attached = NULL; +static SemaphoreHandle_t s_semph_thread_set_dns_server = NULL; +static const char *TAG = "example_connect"; + +static void thread_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, + void* event_data) +{ + if (event_base == OPENTHREAD_EVENT) { + if (event_id == OPENTHREAD_EVENT_ATTACHED) { + xSemaphoreGive(s_semph_thread_attached); + } else if (event_id == OPENTHREAD_EVENT_SET_DNS_SERVER) { + xSemaphoreGive(s_semph_thread_set_dns_server); + } + } +} + +static void ot_task_worker(void *aContext) +{ + esp_openthread_platform_config_t config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }; + + esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_OPENTHREAD(); + esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_THREAD; + esp_netif_config_t cfg = { + .base = &esp_netif_config, + .stack = &g_esp_netif_netstack_default_openthread, + }; + s_openthread_netif = esp_netif_new(&cfg); + assert(s_openthread_netif != NULL); + + // Initialize the OpenThread stack + ESP_ERROR_CHECK(esp_openthread_init(&config)); + ESP_ERROR_CHECK(esp_netif_attach(s_openthread_netif, esp_openthread_netif_glue_init(&config))); + esp_openthread_lock_acquire(portMAX_DELAY); + (void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL); + esp_openthread_cli_init(); + esp_openthread_cli_create_task(); + otOperationalDatasetTlvs dataset; + otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset); + if (error != OT_ERROR_NONE) { + ESP_ERROR_CHECK(esp_openthread_auto_start(NULL)); + } else { + ESP_ERROR_CHECK(esp_openthread_auto_start(&dataset)); + } + esp_openthread_lock_release(); + + // Run the main loop + esp_openthread_launch_mainloop(); + + // Clean up + esp_openthread_netif_glue_deinit(); + esp_netif_destroy(s_openthread_netif); + esp_vfs_eventfd_unregister(); + vTaskDelete(NULL); +} + +/* tear down connection, release resources */ +void example_thread_shutdown(void) +{ + vTaskDelete(s_ot_task_handle); + esp_openthread_netif_glue_deinit(); + esp_netif_destroy(s_openthread_netif); + esp_vfs_eventfd_unregister(); + vSemaphoreDelete(s_semph_thread_set_dns_server); + vSemaphoreDelete(s_semph_thread_attached); +} + +esp_err_t example_thread_connect(void) +{ + s_semph_thread_attached = xSemaphoreCreateBinary(); + if (s_semph_thread_attached == NULL) { + return ESP_ERR_NO_MEM; + } + s_semph_thread_set_dns_server = xSemaphoreCreateBinary(); + if (s_semph_thread_set_dns_server == NULL) { + vSemaphoreDelete(s_semph_thread_attached); + return ESP_ERR_NO_MEM; + } + // 4 eventfds might be used for Thread + // * netif + // * ot task queue + // * radio driver + // * border router + esp_vfs_eventfd_config_t eventfd_config = { + .max_fds = 4, + }; + esp_vfs_eventfd_register(&eventfd_config); + ESP_ERROR_CHECK(esp_event_handler_register(OPENTHREAD_EVENT, ESP_EVENT_ANY_ID, thread_event_handler, NULL)); + if (xTaskCreate(ot_task_worker, "ot_br_main", CONFIG_EXAMPLE_THREAD_TASK_STACK_SIZE, NULL, 5, &s_ot_task_handle) != pdPASS) { + vSemaphoreDelete(s_semph_thread_attached); + vSemaphoreDelete(s_semph_thread_set_dns_server); + ESP_LOGE(TAG, "Failed to create openthread task"); + return ESP_FAIL; + } + xSemaphoreTake(s_semph_thread_attached, portMAX_DELAY); + // Wait 1s for the Thread device to set its DNS server with the NAT64 prefix. + if (xSemaphoreTake(s_semph_thread_set_dns_server, 1000 / portTICK_PERIOD_MS) != pdPASS) { + ESP_LOGW(TAG, "DNS server is not set for the Thread device, might be unable to access the Internet"); + } + return ESP_OK; +} From 79e823f588509d7df05bb91121bffcd17f27fa11 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Tue, 17 Dec 2024 10:16:38 +0800 Subject: [PATCH 83/91] fix(openthread): Fix CI failure of ot_trel example build --- include/protocol_examples_thread_config.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/protocol_examples_thread_config.h b/include/protocol_examples_thread_config.h index 306a9270cb..532f828b65 100644 --- a/include/protocol_examples_thread_config.h +++ b/include/protocol_examples_thread_config.h @@ -66,6 +66,11 @@ .intr_pin = CONFIG_EXAMPLE_THREAD_SPI_INTR_PIN, \ }, \ } +#else +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_TREL, \ + } #endif #if CONFIG_OPENTHREAD_CONSOLE_TYPE_UART @@ -95,6 +100,11 @@ .host_connection_mode = HOST_CONNECTION_MODE_CLI_USB, \ .host_usb_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(), \ } +#else +#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \ + { \ + .host_connection_mode = HOST_CONNECTION_MODE_NONE, \ + } #endif #define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \ From 18b1a0727feccbd90aa804f900c86efd97205615 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 20 Feb 2025 11:32:09 +0100 Subject: [PATCH 84/91] fix(connect): Fix wifi_connect to delete semaphores after waiting on them On wifi-disconnect (after all retries), we signal to the IP semaphores and delete them immediately, while the wait thread might be still holding on them causing: ``` I (1682) example_connect: Waiting for IP(s) I (4092) example_connect: Wi-Fi disconnected 201, trying to reconnect... I (6502) example_connect: WiFi Connect failed 2 times, stop reconnect. assert failed: spinlock_acquire spinlock.h:142 (lock->count == 0) ``` --- wifi_connect.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/wifi_connect.c b/wifi_connect.c index 1bfc1b35f4..a2b88200ee 100644 --- a/wifi_connect.c +++ b/wifi_connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -172,9 +172,13 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) ESP_LOGI(TAG, "Waiting for IP(s)"); #if CONFIG_EXAMPLE_CONNECT_IPV4 xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); + vSemaphoreDelete(s_semph_get_ip_addrs); + s_semph_get_ip_addrs = NULL; #endif #if CONFIG_EXAMPLE_CONNECT_IPV6 xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); + vSemaphoreDelete(s_semph_get_ip6_addrs); + s_semph_get_ip6_addrs = NULL; #endif if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { return ESP_FAIL; @@ -190,14 +194,6 @@ esp_err_t example_wifi_sta_do_disconnect(void) ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect)); #if CONFIG_EXAMPLE_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6)); -#endif - if (s_semph_get_ip_addrs) { - vSemaphoreDelete(s_semph_get_ip_addrs); - } -#if CONFIG_EXAMPLE_CONNECT_IPV6 - if (s_semph_get_ip6_addrs) { - vSemaphoreDelete(s_semph_get_ip6_addrs); - } #endif return esp_wifi_disconnect(); } From fbf2291ba9f98b239c3dc5ba76f19b85649c3e48 Mon Sep 17 00:00:00 2001 From: Shen Meng Jing Date: Wed, 30 Apr 2025 18:35:39 +0800 Subject: [PATCH 85/91] docs: Fix some typos --- eth_connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth_connect.c b/eth_connect.c index 0cd105621d..ad48069021 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -169,7 +169,7 @@ static esp_netif_t *eth_start(void) s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); esp_netif_attach(netif, s_eth_glue); - // Register user defined event handers + // Register user defined event handlers ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, ð_on_got_ip, NULL)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); From 7de66c177e8f3310586c3d4c4ef1640721e12da3 Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Mon, 14 Jul 2025 14:41:11 +0200 Subject: [PATCH 86/91] feat(examples): improved example for Ethernet SPI polling mode without interrupt Update example for polling mode, without interrupt pin --- Kconfig.projbuild | 10 +++++++++- eth_connect.c | 14 ++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index f9ccbfb569..98d541f34d 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -295,10 +295,18 @@ menu "Example Connection Configuration" config EXAMPLE_ETH_SPI_INT_GPIO int "Interrupt GPIO number" - range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX + range -1 ENV_GPIO_IN_RANGE_MAX default 4 help Set the GPIO number used by the SPI Ethernet module interrupt line. + Set -1 to use SPI Ethernet module in polling mode. + + config EXAMPLE_ETH_SPI_POLLING_MS_VAL + depends on EXAMPLE_ETH_SPI_INT_GPIO < 0 + int "Polling period in msec of SPI Ethernet Module" + default 10 + help + Set SPI Ethernet module polling period. endif # EXAMPLE_USE_SPI_ETHERNET config EXAMPLE_ETH_PHY_RST_GPIO diff --git a/eth_connect.c b/eth_connect.c index ad48069021..a69a0bd217 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -115,7 +115,7 @@ static esp_netif_t *eth_start(void) s_phy = esp_eth_phy_new_dp83848(&phy_config); #elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX s_phy = esp_eth_phy_new_ksz80xx(&phy_config); -#endif +#endif // CONFIG_EXAMPLE_ETH_PHY_GENERIC #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET gpio_install_isr_service(0); spi_bus_config_t buscfg = { @@ -136,20 +136,26 @@ static esp_netif_t *eth_start(void) /* dm9051 ethernet driver is based on spi driver */ eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; +#if CONFIG_EXAMPLE_ETH_SPI_INT_GPIO < 0 + dm9051_config.poll_period_ms = CONFIG_EXAMPLE_ETH_SPI_POLLING_MS_VAL; +#endif // CONFIG_EXAMPLE_ETH_SPI_INT_GPIO s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); s_phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_EXAMPLE_USE_W5500 /* w5500 ethernet driver is based on spi driver */ eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; +#if CONFIG_EXAMPLE_ETH_SPI_INT_GPIO < 0 + w5500_config.poll_period_ms = CONFIG_EXAMPLE_ETH_SPI_POLLING_MS_VAL; +#endif // CONFIG_EXAMPLE_ETH_SPI_INT_GPIO s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); s_phy = esp_eth_phy_new_w5500(&phy_config); -#endif +#endif // CONFIG_EXAMPLE_USE_DM9051 #elif CONFIG_EXAMPLE_USE_OPENETH phy_config.autonego_timeout_ms = 100; s_mac = esp_eth_mac_new_openeth(&mac_config); s_phy = esp_eth_phy_new_dp83848(&phy_config); -#endif +#endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET // Install Ethernet driver esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); From 14dbcb555aaa4c205500913dc060fc70ce5781ec Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Mon, 15 Sep 2025 15:31:47 +0200 Subject: [PATCH 87/91] feat(esp_eth): removed SPI Ethernet and PHY drivers from IDF --- Kconfig.projbuild | 119 ++-------------------------------------------- eth_connect.c | 65 +++---------------------- 2 files changed, 11 insertions(+), 173 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 98d541f34d..4286042bb0 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -137,7 +137,7 @@ menu "Example Connection Configuration" choice EXAMPLE_ETHERNET_TYPE prompt "Ethernet Type" default EXAMPLE_USE_INTERNAL_ETHERNET if SOC_EMAC_SUPPORTED - default EXAMPLE_USE_W5500 + default EXAMPLE_USE_DUMMY help Select which kind of Ethernet will be used in the example. @@ -148,21 +148,11 @@ menu "Example Connection Configuration" help Select internal Ethernet MAC controller. - config EXAMPLE_USE_DM9051 - bool "DM9051 Module" - select EXAMPLE_USE_SPI_ETHERNET + config EXAMPLE_USE_DUMMY + bool "DUMMY Module" select ETH_USE_SPI_ETHERNET - select ETH_SPI_ETHERNET_DM9051 help - Select external SPI-Ethernet module. - - config EXAMPLE_USE_W5500 - bool "W5500 Module" - select EXAMPLE_USE_SPI_ETHERNET - select ETH_USE_SPI_ETHERNET - select ETH_SPI_ETHERNET_W5500 - help - Select external SPI-Ethernet module (W5500). + Dummy option to just to pass builds, will be fixed by IDF-14059 config EXAMPLE_USE_OPENETH bool "OpenCores Ethernet MAC (EXPERIMENTAL)" @@ -179,7 +169,7 @@ menu "Example Connection Configuration" if EXAMPLE_USE_INTERNAL_ETHERNET choice EXAMPLE_ETH_PHY_MODEL prompt "Ethernet PHY Device" - default EXAMPLE_ETH_PHY_IP101 + default EXAMPLE_ETH_PHY_GENERIC help Select the Ethernet PHY device to use in the example. @@ -191,46 +181,6 @@ menu "Example Connection Configuration" even if the PHY meets IEEE 802.3 standard. A typical example is loopback functionality, where certain PHYs may require setting a specific speed mode to operate correctly. - - config EXAMPLE_ETH_PHY_IP101 - bool "IP101" - help - IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver. - Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it. - - config EXAMPLE_ETH_PHY_RTL8201 - bool "RTL8201/SR8201" - help - RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX. - Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it. - - config EXAMPLE_ETH_PHY_LAN87XX - bool "LAN87xx" - help - Below chips are supported: - LAN8710A is a small footprint MII/RMII 10/100 Ethernet Transceiver with HP Auto-MDIX and - flexPWR® Technology. - LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support. - LAN8740A/LAN8741A is a small footprint MII/RMII 10/100 Energy Efficient Ethernet Transceiver - with HP Auto-MDIX and flexPWR® Technology. - LAN8742A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX and - flexPWR® Technology. - Goto https://www.microchip.com for more information about them. - - config EXAMPLE_ETH_PHY_DP83848 - bool "DP83848" - help - DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver. - Goto http://www.ti.com/product/DP83848J for more information about it. - - config EXAMPLE_ETH_PHY_KSZ80XX - bool "KSZ80xx" - help - With the KSZ80xx series, Microchip offers single-chip 10BASE-T/100BASE-TX - Ethernet Physical Layer Transceivers (PHY). - The following chips are supported: KSZ8001, KSZ8021, KSZ8031, KSZ8041, - KSZ8051, KSZ8061, KSZ8081, KSZ8091 - Goto https://www.microchip.com for more information about them. endchoice config EXAMPLE_ETH_MDC_GPIO @@ -250,65 +200,6 @@ menu "Example Connection Configuration" Set the GPIO number used by SMI MDIO. endif - if EXAMPLE_USE_SPI_ETHERNET - config EXAMPLE_ETH_SPI_HOST - int "SPI Host Number" - range 0 2 - default 1 - help - Set the SPI host used to communicate with the SPI Ethernet Controller. - - config EXAMPLE_ETH_SPI_SCLK_GPIO - int "SPI SCLK GPIO number" - range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX - default 14 - help - Set the GPIO number used by SPI SCLK. - - config EXAMPLE_ETH_SPI_MOSI_GPIO - int "SPI MOSI GPIO number" - range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX - default 13 - help - Set the GPIO number used by SPI MOSI. - - config EXAMPLE_ETH_SPI_MISO_GPIO - int "SPI MISO GPIO number" - range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX - default 12 - help - Set the GPIO number used by SPI MISO. - - config EXAMPLE_ETH_SPI_CS_GPIO - int "SPI CS GPIO number" - range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX - default 15 - help - Set the GPIO number used by SPI CS. - - config EXAMPLE_ETH_SPI_CLOCK_MHZ - int "SPI clock speed (MHz)" - range 5 80 - default 36 - help - Set the clock speed (MHz) of SPI interface. - - config EXAMPLE_ETH_SPI_INT_GPIO - int "Interrupt GPIO number" - range -1 ENV_GPIO_IN_RANGE_MAX - default 4 - help - Set the GPIO number used by the SPI Ethernet module interrupt line. - Set -1 to use SPI Ethernet module in polling mode. - - config EXAMPLE_ETH_SPI_POLLING_MS_VAL - depends on EXAMPLE_ETH_SPI_INT_GPIO < 0 - int "Polling period in msec of SPI Ethernet Module" - default 10 - help - Set SPI Ethernet module polling period. - endif # EXAMPLE_USE_SPI_ETHERNET - config EXAMPLE_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" range -1 ENV_GPIO_OUT_RANGE_MAX diff --git a/eth_connect.c b/eth_connect.c index a69a0bd217..0361ab62f3 100644 --- a/eth_connect.c +++ b/eth_connect.c @@ -9,9 +9,6 @@ #include "example_common_private.h" #include "esp_event.h" #include "esp_eth.h" -#if CONFIG_ETH_USE_SPI_ETHERNET -#include "driver/spi_master.h" -#endif // CONFIG_ETH_USE_SPI_ETHERNET #include "esp_log.h" #include "esp_mac.h" #include "driver/gpio.h" @@ -82,6 +79,10 @@ static esp_eth_netif_glue_handle_t s_eth_glue = NULL; static esp_netif_t *eth_start(void) { +// TODO just to pass builds, will be fixed by IDF-14059 +#if CONFIG_EXAMPLE_USE_DUMMY + return NULL; +#else esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH(); // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_ETH; @@ -105,72 +106,17 @@ static esp_netif_t *eth_start(void) s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); #if CONFIG_EXAMPLE_ETH_PHY_GENERIC s_phy = esp_eth_phy_new_generic(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_IP101 - s_phy = esp_eth_phy_new_ip101(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_RTL8201 - s_phy = esp_eth_phy_new_rtl8201(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX - s_phy = esp_eth_phy_new_lan87xx(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_DP83848 - s_phy = esp_eth_phy_new_dp83848(&phy_config); -#elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX - s_phy = esp_eth_phy_new_ksz80xx(&phy_config); #endif // CONFIG_EXAMPLE_ETH_PHY_GENERIC -#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET - gpio_install_isr_service(0); - spi_bus_config_t buscfg = { - .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, - .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, - .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO, - .quadwp_io_num = -1, - .quadhd_io_num = -1, - }; - ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); - spi_device_interface_config_t spi_devcfg = { - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; -#if CONFIG_EXAMPLE_USE_DM9051 - /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); - dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; -#if CONFIG_EXAMPLE_ETH_SPI_INT_GPIO < 0 - dm9051_config.poll_period_ms = CONFIG_EXAMPLE_ETH_SPI_POLLING_MS_VAL; -#endif // CONFIG_EXAMPLE_ETH_SPI_INT_GPIO - s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); - s_phy = esp_eth_phy_new_dm9051(&phy_config); -#elif CONFIG_EXAMPLE_USE_W5500 - /* w5500 ethernet driver is based on spi driver */ - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); - w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; -#if CONFIG_EXAMPLE_ETH_SPI_INT_GPIO < 0 - w5500_config.poll_period_ms = CONFIG_EXAMPLE_ETH_SPI_POLLING_MS_VAL; -#endif // CONFIG_EXAMPLE_ETH_SPI_INT_GPIO - s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); - s_phy = esp_eth_phy_new_w5500(&phy_config); -#endif // CONFIG_EXAMPLE_USE_DM9051 #elif CONFIG_EXAMPLE_USE_OPENETH phy_config.autonego_timeout_ms = 100; s_mac = esp_eth_mac_new_openeth(&mac_config); - s_phy = esp_eth_phy_new_dp83848(&phy_config); + s_phy = esp_eth_phy_new_generic(&phy_config); #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET // Install Ethernet driver esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); -#if CONFIG_EXAMPLE_USE_SPI_ETHERNET - /* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually. - We set the ESP_MAC_ETH mac address as the default, if you want to use ESP_MAC_EFUSE_CUSTOM mac address, please enable the - configuration: `ESP_MAC_USE_CUSTOM_MAC_AS_BASE_MAC` - */ - uint8_t eth_mac[6] = {0}; - ESP_ERROR_CHECK(esp_read_mac(eth_mac, ESP_MAC_ETH)); - ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, eth_mac)); -#endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET - // combine driver with netif s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); esp_netif_attach(netif, s_eth_glue); @@ -184,6 +130,7 @@ static esp_netif_t *eth_start(void) esp_eth_start(s_eth_handle); return netif; +#endif // CONFIG_EXAMPLE_USE_DUMMY } static void eth_stop(void) From 86c2be1975e808f1e4b62bd7b8b74c4f2c60c04f Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Tue, 7 Oct 2025 13:28:45 +0200 Subject: [PATCH 88/91] fix(protocol_examples_common): Temporal fix of backwards compatibility --- sdkconfig.rename | 1 + 1 file changed, 1 insertion(+) create mode 100644 sdkconfig.rename diff --git a/sdkconfig.rename b/sdkconfig.rename new file mode 100644 index 0000000000..fccfd425c2 --- /dev/null +++ b/sdkconfig.rename @@ -0,0 +1 @@ +CONFIG_EXAMPLE_ETH_PHY_IP101 CONFIG_EXAMPLE_ETH_PHY_GENERIC From bd37fc531787d03bb01e8af4a90b139082197258 Mon Sep 17 00:00:00 2001 From: Abhik Roy Date: Tue, 11 Nov 2025 23:06:32 +1100 Subject: [PATCH 89/91] feat(common): Move protocol_examples_common to components/net_connect --- CMakeLists.txt => components/net_connect/CMakeLists.txt | 0 Kconfig.projbuild => components/net_connect/Kconfig.projbuild | 0 addr_from_stdin.c => components/net_connect/addr_from_stdin.c | 0 connect.c => components/net_connect/connect.c | 0 console_cmd.c => components/net_connect/console_cmd.c | 0 eth_connect.c => components/net_connect/eth_connect.c | 0 {include => components/net_connect/include}/addr_from_stdin.h | 0 .../net_connect/include}/example_common_private.h | 0 .../net_connect/include}/protocol_examples_common.h | 0 .../net_connect/include}/protocol_examples_thread_config.h | 0 .../net_connect/include}/protocol_examples_utils.h | 0 ppp_connect.c => components/net_connect/ppp_connect.c | 0 .../net_connect/protocol_examples_utils.c | 0 sdkconfig.rename => components/net_connect/sdkconfig.rename | 0 stdin_out.c => components/net_connect/stdin_out.c | 0 thread_connect.c => components/net_connect/thread_connect.c | 0 wifi_connect.c => components/net_connect/wifi_connect.c | 0 17 files changed, 0 insertions(+), 0 deletions(-) rename CMakeLists.txt => components/net_connect/CMakeLists.txt (100%) rename Kconfig.projbuild => components/net_connect/Kconfig.projbuild (100%) rename addr_from_stdin.c => components/net_connect/addr_from_stdin.c (100%) rename connect.c => components/net_connect/connect.c (100%) rename console_cmd.c => components/net_connect/console_cmd.c (100%) rename eth_connect.c => components/net_connect/eth_connect.c (100%) rename {include => components/net_connect/include}/addr_from_stdin.h (100%) rename {include => components/net_connect/include}/example_common_private.h (100%) rename {include => components/net_connect/include}/protocol_examples_common.h (100%) rename {include => components/net_connect/include}/protocol_examples_thread_config.h (100%) rename {include => components/net_connect/include}/protocol_examples_utils.h (100%) rename ppp_connect.c => components/net_connect/ppp_connect.c (100%) rename protocol_examples_utils.c => components/net_connect/protocol_examples_utils.c (100%) rename sdkconfig.rename => components/net_connect/sdkconfig.rename (100%) rename stdin_out.c => components/net_connect/stdin_out.c (100%) rename thread_connect.c => components/net_connect/thread_connect.c (100%) rename wifi_connect.c => components/net_connect/wifi_connect.c (100%) diff --git a/CMakeLists.txt b/components/net_connect/CMakeLists.txt similarity index 100% rename from CMakeLists.txt rename to components/net_connect/CMakeLists.txt diff --git a/Kconfig.projbuild b/components/net_connect/Kconfig.projbuild similarity index 100% rename from Kconfig.projbuild rename to components/net_connect/Kconfig.projbuild diff --git a/addr_from_stdin.c b/components/net_connect/addr_from_stdin.c similarity index 100% rename from addr_from_stdin.c rename to components/net_connect/addr_from_stdin.c diff --git a/connect.c b/components/net_connect/connect.c similarity index 100% rename from connect.c rename to components/net_connect/connect.c diff --git a/console_cmd.c b/components/net_connect/console_cmd.c similarity index 100% rename from console_cmd.c rename to components/net_connect/console_cmd.c diff --git a/eth_connect.c b/components/net_connect/eth_connect.c similarity index 100% rename from eth_connect.c rename to components/net_connect/eth_connect.c diff --git a/include/addr_from_stdin.h b/components/net_connect/include/addr_from_stdin.h similarity index 100% rename from include/addr_from_stdin.h rename to components/net_connect/include/addr_from_stdin.h diff --git a/include/example_common_private.h b/components/net_connect/include/example_common_private.h similarity index 100% rename from include/example_common_private.h rename to components/net_connect/include/example_common_private.h diff --git a/include/protocol_examples_common.h b/components/net_connect/include/protocol_examples_common.h similarity index 100% rename from include/protocol_examples_common.h rename to components/net_connect/include/protocol_examples_common.h diff --git a/include/protocol_examples_thread_config.h b/components/net_connect/include/protocol_examples_thread_config.h similarity index 100% rename from include/protocol_examples_thread_config.h rename to components/net_connect/include/protocol_examples_thread_config.h diff --git a/include/protocol_examples_utils.h b/components/net_connect/include/protocol_examples_utils.h similarity index 100% rename from include/protocol_examples_utils.h rename to components/net_connect/include/protocol_examples_utils.h diff --git a/ppp_connect.c b/components/net_connect/ppp_connect.c similarity index 100% rename from ppp_connect.c rename to components/net_connect/ppp_connect.c diff --git a/protocol_examples_utils.c b/components/net_connect/protocol_examples_utils.c similarity index 100% rename from protocol_examples_utils.c rename to components/net_connect/protocol_examples_utils.c diff --git a/sdkconfig.rename b/components/net_connect/sdkconfig.rename similarity index 100% rename from sdkconfig.rename rename to components/net_connect/sdkconfig.rename diff --git a/stdin_out.c b/components/net_connect/stdin_out.c similarity index 100% rename from stdin_out.c rename to components/net_connect/stdin_out.c diff --git a/thread_connect.c b/components/net_connect/thread_connect.c similarity index 100% rename from thread_connect.c rename to components/net_connect/thread_connect.c diff --git a/wifi_connect.c b/components/net_connect/wifi_connect.c similarity index 100% rename from wifi_connect.c rename to components/net_connect/wifi_connect.c From 5a64f51a1c7ed1b4d0ea8c356306e1d633e5eaf9 Mon Sep 17 00:00:00 2001 From: Abhik Roy Date: Tue, 11 Nov 2025 23:12:22 +1100 Subject: [PATCH 90/91] feat(common): Refactor API and config from example_* to net_connect_* Added example to net_connect component. --- components/net_connect/CMakeLists.txt | 16 +- components/net_connect/Kconfig.projbuild | 180 +++++++++--------- components/net_connect/README.md | 66 +++++++ components/net_connect/addr_from_stdin.c | 16 +- components/net_connect/connect.c | 84 ++++---- components/net_connect/console_cmd.c | 16 +- components/net_connect/eth_connect.c | 68 +++---- components/net_connect/idf_component.yml | 7 + .../net_connect/include/addr_from_stdin.h | 14 +- .../include/example_common_private.h | 58 ------ components/net_connect/include/net_connect.h | 151 +++++++++++++++ .../net_connect/include/net_connect_private.h | 58 ++++++ .../include/protocol_examples_common.h | 154 --------------- .../include/protocol_examples_thread_config.h | 22 +-- .../include/protocol_examples_utils.h | 4 +- components/net_connect/ppp_connect.c | 50 ++--- .../net_connect/protocol_examples_utils.c | 5 +- components/net_connect/stdin_out.c | 13 +- components/net_connect/thread_connect.c | 16 +- components/net_connect/wifi_connect.c | 98 +++++----- 20 files changed, 577 insertions(+), 519 deletions(-) create mode 100644 components/net_connect/README.md create mode 100644 components/net_connect/idf_component.yml delete mode 100644 components/net_connect/include/example_common_private.h create mode 100644 components/net_connect/include/net_connect.h create mode 100644 components/net_connect/include/net_connect_private.h delete mode 100644 components/net_connect/include/protocol_examples_common.h diff --git a/components/net_connect/CMakeLists.txt b/components/net_connect/CMakeLists.txt index 8d0501a9b1..9258de8b52 100644 --- a/components/net_connect/CMakeLists.txt +++ b/components/net_connect/CMakeLists.txt @@ -14,19 +14,19 @@ set(srcs "stdin_out.c" "wifi_connect.c" "protocol_examples_utils.c") -if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) +if(CONFIG_NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD) list(APPEND srcs "console_cmd.c") endif() -if(CONFIG_EXAMPLE_CONNECT_ETHERNET) +if(CONFIG_NET_CONNECT_CONNECT_ETHERNET) list(APPEND srcs "eth_connect.c") endif() -if(CONFIG_EXAMPLE_CONNECT_THREAD) +if(CONFIG_NET_CONNECT_CONNECT_THREAD) list(APPEND srcs "thread_connect.c") endif() -if(CONFIG_EXAMPLE_CONNECT_PPP) +if(CONFIG_NET_CONNECT_CONNECT_PPP) list(APPEND srcs "ppp_connect.c") endif() @@ -35,18 +35,18 @@ idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console esp_eth openthread) -if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD) +if(CONFIG_NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD) idf_component_optional_requires(PRIVATE console) endif() -if(CONFIG_EXAMPLE_CONNECT_ETHERNET) +if(CONFIG_NET_CONNECT_CONNECT_ETHERNET) idf_component_optional_requires(PUBLIC esp_eth) endif() -if(CONFIG_EXAMPLE_CONNECT_THREAD) +if(CONFIG_NET_CONNECT_CONNECT_THREAD) idf_component_optional_requires(PRIVATE openthread) endif() -if(CONFIG_EXAMPLE_CONNECT_PPP) +if(CONFIG_NET_CONNECT_CONNECT_PPP) idf_component_optional_requires(PRIVATE esp_tinyusb espressif__esp_tinyusb) endif() diff --git a/components/net_connect/Kconfig.projbuild b/components/net_connect/Kconfig.projbuild index 4286042bb0..a571465c0d 100644 --- a/components/net_connect/Kconfig.projbuild +++ b/components/net_connect/Kconfig.projbuild @@ -1,8 +1,8 @@ -menu "Example Connection Configuration" +menu "Net Connect Configuration" orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps" - config EXAMPLE_CONNECT_WIFI + config NET_CONNECT_CONNECT_WIFI bool "connect using WiFi interface" depends on !IDF_TARGET_LINUX && (SOC_WIFI_SUPPORTED || ESP_WIFI_REMOTE_ENABLED || ESP_HOST_WIFI_ENABLED) default y if SOC_WIFI_SUPPORTED @@ -10,46 +10,46 @@ menu "Example Connection Configuration" Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. Choose this option to connect with WiFi - if EXAMPLE_CONNECT_WIFI - config EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + if NET_CONNECT_CONNECT_WIFI + config NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN bool "Get ssid and password from stdin" default n help Give the WiFi SSID and password from stdin. - config EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD - depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + config NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD + depends on !NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN bool "Provide wifi connect commands" default y help Provide wifi connect commands for esp_console. Please use `example_register_wifi_connect_commands` to register them. - config EXAMPLE_WIFI_SSID - depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + config NET_CONNECT_WIFI_SSID + depends on !NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN string "WiFi SSID" default "myssid" help SSID (network name) for the example to connect to. - config EXAMPLE_WIFI_PASSWORD - depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN + config NET_CONNECT_WIFI_PASSWORD + depends on !NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN string "WiFi Password" default "mypassword" help WiFi password (WPA or WPA2) for the example to use. Can be left blank if the network has no security set. - config EXAMPLE_WIFI_CONN_MAX_RETRY + config NET_CONNECT_WIFI_CONN_MAX_RETRY int "Maximum retry" default 6 help Set the Maximum retry to avoid station reconnecting to the AP unlimited, in case the AP is really inexistent. - choice EXAMPLE_WIFI_SCAN_METHOD + choice NET_CONNECT_WIFI_SCAN_METHOD prompt "WiFi Scan Method" - default EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL + default NET_CONNECT_WIFI_SCAN_METHOD_ALL_CHANNEL help WiFi scan method: @@ -57,14 +57,14 @@ menu "Example Connection Configuration" If "All Channel" is selected, scan will end after scan all the channel. - config EXAMPLE_WIFI_SCAN_METHOD_FAST + config NET_CONNECT_WIFI_SCAN_METHOD_FAST bool "Fast" - config EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL + config NET_CONNECT_WIFI_SCAN_METHOD_ALL_CHANNEL bool "All Channel" endchoice menu "WiFi Scan threshold" - config EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD + config NET_CONNECT_WIFI_SCAN_RSSI_THRESHOLD int "WiFi minimum rssi" range -127 0 @@ -72,36 +72,36 @@ menu "Example Connection Configuration" help The minimum rssi to accept in the scan mode. - choice EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD + choice NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD prompt "WiFi Scan auth mode threshold" - default EXAMPLE_WIFI_AUTH_OPEN + default NET_CONNECT_WIFI_AUTH_OPEN help The weakest authmode to accept in the scan mode. - config EXAMPLE_WIFI_AUTH_OPEN + config NET_CONNECT_WIFI_AUTH_OPEN bool "OPEN" - config EXAMPLE_WIFI_AUTH_WEP + config NET_CONNECT_WIFI_AUTH_WEP bool "WEP" - config EXAMPLE_WIFI_AUTH_WPA_PSK + config NET_CONNECT_WIFI_AUTH_WPA_PSK bool "WPA PSK" - config EXAMPLE_WIFI_AUTH_WPA2_PSK + config NET_CONNECT_WIFI_AUTH_WPA2_PSK bool "WPA2 PSK" - config EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK + config NET_CONNECT_WIFI_AUTH_WPA_WPA2_PSK bool "WPA WPA2 PSK" - config EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE + config NET_CONNECT_WIFI_AUTH_WPA2_ENTERPRISE bool "WPA2 ENTERPRISE" - config EXAMPLE_WIFI_AUTH_WPA3_PSK + config NET_CONNECT_WIFI_AUTH_WPA3_PSK bool "WPA3 PSK" - config EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK + config NET_CONNECT_WIFI_AUTH_WPA2_WPA3_PSK bool "WPA2 WPA3 PSK" - config EXAMPLE_WIFI_AUTH_WAPI_PSK + config NET_CONNECT_WIFI_AUTH_WAPI_PSK bool "WAPI PSK" endchoice endmenu - choice EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD + choice NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD prompt "WiFi Connect AP Sort Method" - default EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL + default NET_CONNECT_WIFI_CONNECT_AP_BY_SIGNAL help WiFi connect AP sort method: @@ -109,52 +109,52 @@ menu "Example Connection Configuration" If "Security" is selected, Sort matched APs in scan list by security mode. - config EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL + config NET_CONNECT_WIFI_CONNECT_AP_BY_SIGNAL bool "Signal" - config EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY + config NET_CONNECT_WIFI_CONNECT_AP_BY_SECURITY bool "Security" endchoice endif - config EXAMPLE_CONNECT_ETHERNET + config NET_CONNECT_CONNECT_ETHERNET bool "connect using Ethernet interface" depends on !IDF_TARGET_LINUX - default y if !EXAMPLE_CONNECT_WIFI && !EXAMPLE_CONNECT_THREAD + default y if !NET_CONNECT_CONNECT_WIFI && !NET_CONNECT_CONNECT_THREAD help Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. Choose this option to connect with Ethernet - if EXAMPLE_CONNECT_ETHERNET - config EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE + if NET_CONNECT_CONNECT_ETHERNET + config NET_CONNECT_ETHERNET_EMAC_TASK_STACK_SIZE int "emac_rx task stack size" default 2048 help This set stack size for emac_rx task - config EXAMPLE_USE_SPI_ETHERNET + config NET_CONNECT_USE_SPI_ETHERNET bool - choice EXAMPLE_ETHERNET_TYPE + choice NET_CONNECT_ETHERNET_TYPE prompt "Ethernet Type" - default EXAMPLE_USE_INTERNAL_ETHERNET if SOC_EMAC_SUPPORTED - default EXAMPLE_USE_DUMMY + default NET_CONNECT_USE_INTERNAL_ETHERNET if SOC_EMAC_SUPPORTED + default NET_CONNECT_USE_DUMMY help Select which kind of Ethernet will be used in the example. - config EXAMPLE_USE_INTERNAL_ETHERNET + config NET_CONNECT_USE_INTERNAL_ETHERNET depends on SOC_EMAC_SUPPORTED select ETH_USE_ESP32_EMAC bool "Internal EMAC" help Select internal Ethernet MAC controller. - config EXAMPLE_USE_DUMMY + config NET_CONNECT_USE_DUMMY bool "DUMMY Module" select ETH_USE_SPI_ETHERNET help Dummy option to just to pass builds, will be fixed by IDF-14059 - config EXAMPLE_USE_OPENETH + config NET_CONNECT_USE_OPENETH bool "OpenCores Ethernet MAC (EXPERIMENTAL)" select ETH_USE_OPENETH help @@ -164,16 +164,16 @@ menu "Example Connection Configuration" not officially supported. Examples built with this option enabled will not run on a real ESP32 chip. - endchoice # EXAMPLE_ETHERNET_TYPE + endchoice # NET_CONNECT_ETHERNET_TYPE - if EXAMPLE_USE_INTERNAL_ETHERNET - choice EXAMPLE_ETH_PHY_MODEL + if NET_CONNECT_USE_INTERNAL_ETHERNET + choice NET_CONNECT_ETH_PHY_MODEL prompt "Ethernet PHY Device" - default EXAMPLE_ETH_PHY_GENERIC + default NET_CONNECT_ETH_PHY_GENERIC help Select the Ethernet PHY device to use in the example. - config EXAMPLE_ETH_PHY_GENERIC + config NET_CONNECT_ETH_PHY_GENERIC bool "Generic 802.3 PHY" help Any Ethernet PHY chip compliant with IEEE 802.3 can be used. However, while @@ -183,7 +183,7 @@ menu "Example Connection Configuration" operate correctly. endchoice - config EXAMPLE_ETH_MDC_GPIO + config NET_CONNECT_ETH_MDC_GPIO int "SMI MDC GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 23 if IDF_TARGET_ESP32 @@ -191,7 +191,7 @@ menu "Example Connection Configuration" help Set the GPIO number used by SMI MDC. - config EXAMPLE_ETH_MDIO_GPIO + config NET_CONNECT_ETH_MDIO_GPIO int "SMI MDIO GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX default 18 if IDF_TARGET_ESP32 @@ -200,7 +200,7 @@ menu "Example Connection Configuration" Set the GPIO number used by SMI MDIO. endif - config EXAMPLE_ETH_PHY_RST_GPIO + config NET_CONNECT_ETH_PHY_RST_GPIO int "PHY Reset GPIO number" range -1 ENV_GPIO_OUT_RANGE_MAX default 51 if IDF_TARGET_ESP32P4 @@ -209,15 +209,15 @@ menu "Example Connection Configuration" Set the GPIO number used to reset PHY chip. Set to -1 to disable PHY chip hardware reset. - config EXAMPLE_ETH_PHY_ADDR + config NET_CONNECT_ETH_PHY_ADDR int "PHY Address" - range 0 31 if EXAMPLE_USE_INTERNAL_ETHERNET + range 0 31 if NET_CONNECT_USE_INTERNAL_ETHERNET default 1 help Set PHY address according your board schematic. - endif # EXAMPLE_CONNECT_ETHERNET + endif # NET_CONNECT_CONNECT_ETHERNET - config EXAMPLE_CONNECT_PPP + config NET_CONNECT_CONNECT_PPP bool "connect using Point to Point interface" select LWIP_PPP_SUPPORT help @@ -225,21 +225,21 @@ menu "Example Connection Configuration" Choose this option to connect to the ppp server running on your laptop over a serial line (either UART or USB ACM) - if EXAMPLE_CONNECT_PPP - choice EXAMPLE_CONNECT_PPP_DEVICE + if NET_CONNECT_CONNECT_PPP + choice NET_CONNECT_CONNECT_PPP_DEVICE prompt "Choose PPP device" - default EXAMPLE_CONNECT_PPP_DEVICE_USB + default NET_CONNECT_CONNECT_PPP_DEVICE_USB help Select which peripheral to use to connect to the PPP server. - config EXAMPLE_CONNECT_PPP_DEVICE_USB + config NET_CONNECT_CONNECT_PPP_DEVICE_USB bool "USB" depends on SOC_USB_OTG_SUPPORTED select TINYUSB_CDC_ENABLED help Use USB ACM device. - config EXAMPLE_CONNECT_PPP_DEVICE_UART + config NET_CONNECT_CONNECT_PPP_DEVICE_UART bool "UART" help Use UART. @@ -247,22 +247,22 @@ menu "Example Connection Configuration" endchoice menu "UART Configuration" - depends on EXAMPLE_CONNECT_PPP_DEVICE_UART - config EXAMPLE_CONNECT_UART_TX_PIN + depends on NET_CONNECT_CONNECT_PPP_DEVICE_UART + config NET_CONNECT_CONNECT_UART_TX_PIN int "TXD Pin Number" default 4 range 0 31 help Pin number of UART TX. - config EXAMPLE_CONNECT_UART_RX_PIN + config NET_CONNECT_CONNECT_UART_RX_PIN int "RXD Pin Number" default 5 range 0 31 help Pin number of UART RX. - config EXAMPLE_CONNECT_UART_BAUDRATE + config NET_CONNECT_CONNECT_UART_BAUDRATE int "UART Baudrate" default 115200 range 9600 3000000 @@ -271,28 +271,28 @@ menu "Example Connection Configuration" endmenu - config EXAMPLE_PPP_CONN_MAX_RETRY + config NET_CONNECT_PPP_CONN_MAX_RETRY int "Maximum retry" default 6 help Set the Maximum retry to avoid station reconnecting if the pppd is not available - endif # EXAMPLE_CONNECT_PPP + endif # NET_CONNECT_CONNECT_PPP - config EXAMPLE_CONNECT_THREAD + config NET_CONNECT_CONNECT_THREAD bool "Connect using Thread interface" depends on !IDF_TARGET_LINUX && OPENTHREAD_ENABLED default y if SOC_IEEE802154_SUPPORTED - select EXAMPLE_CONNECT_IPV6 + select NET_CONNECT_CONNECT_IPV6 help Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. Choose this option to connect with Thread. The operational active dataset of the Thread network can be configured in openthread component at '->Components->OpenThread->Thread Core Features->Thread Operational Dataset' - if EXAMPLE_CONNECT_THREAD - config EXAMPLE_THREAD_TASK_STACK_SIZE + if NET_CONNECT_CONNECT_THREAD + config NET_CONNECT_THREAD_TASK_STACK_SIZE int "Example Thread task stack size" default 8192 help @@ -301,47 +301,47 @@ menu "Example Connection Configuration" menu "Radio Spinel Options" depends on OPENTHREAD_RADIO_SPINEL_UART || OPENTHREAD_RADIO_SPINEL_SPI - config EXAMPLE_THREAD_UART_RX_PIN + config NET_CONNECT_THREAD_UART_RX_PIN depends on OPENTHREAD_RADIO_SPINEL_UART int "Uart Rx Pin" default 17 - config EXAMPLE_THREAD_UART_TX_PIN + config NET_CONNECT_THREAD_UART_TX_PIN depends on OPENTHREAD_RADIO_SPINEL_UART int "Uart Tx pin" default 18 - config EXAMPLE_THREAD_UART_BAUD + config NET_CONNECT_THREAD_UART_BAUD depends on OPENTHREAD_RADIO_SPINEL_UART int "Uart baud rate" default 460800 - config EXAMPLE_THREAD_UART_PORT + config NET_CONNECT_THREAD_UART_PORT depends on OPENTHREAD_RADIO_SPINEL_UART int "Uart port" default 1 - config EXAMPLE_THREAD_SPI_CS_PIN + config NET_CONNECT_THREAD_SPI_CS_PIN depends on OPENTHREAD_RADIO_SPINEL_SPI int "SPI CS Pin" default 10 - config EXAMPLE_THREAD_SPI_SCLK_PIN + config NET_CONNECT_THREAD_SPI_SCLK_PIN depends on OPENTHREAD_RADIO_SPINEL_SPI int "SPI SCLK Pin" default 12 - config EXAMPLE_THREAD_SPI_MISO_PIN + config NET_CONNECT_THREAD_SPI_MISO_PIN depends on OPENTHREAD_RADIO_SPINEL_SPI int "SPI MISO Pin" default 13 - config EXAMPLE_THREAD_SPI_MOSI_PIN + config NET_CONNECT_THREAD_SPI_MOSI_PIN depends on OPENTHREAD_RADIO_SPINEL_SPI int "SPI MOSI Pin" default 11 - config EXAMPLE_THREAD_SPI_INTR_PIN + config NET_CONNECT_THREAD_SPI_INTR_PIN depends on OPENTHREAD_RADIO_SPINEL_SPI int "SPI Interrupt Pin" default 8 @@ -349,18 +349,18 @@ menu "Example Connection Configuration" endif - config EXAMPLE_CONNECT_IPV4 + config NET_CONNECT_CONNECT_IPV4 bool depends on LWIP_IPV4 - default n if EXAMPLE_CONNECT_THREAD + default n if NET_CONNECT_CONNECT_THREAD default y - config EXAMPLE_CONNECT_IPV6 - depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET || EXAMPLE_CONNECT_PPP || EXAMPLE_CONNECT_THREAD + config NET_CONNECT_CONNECT_IPV6 + depends on NET_CONNECT_CONNECT_WIFI || NET_CONNECT_CONNECT_ETHERNET || NET_CONNECT_CONNECT_PPP || NET_CONNECT_CONNECT_THREAD bool "Obtain IPv6 address" default y select LWIP_IPV6 - select LWIP_PPP_ENABLE_IPV6 if EXAMPLE_CONNECT_PPP + select LWIP_PPP_ENABLE_IPV6 if NET_CONNECT_CONNECT_PPP help By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. Disable this option if the network does not support IPv6. @@ -368,29 +368,29 @@ menu "Example Connection Configuration" the local link address gets assigned. Consider enabling IPv6 stateless address autoconfiguration (SLAAC) in the LWIP component. - if EXAMPLE_CONNECT_IPV6 - choice EXAMPLE_CONNECT_PREFERRED_IPV6 + if NET_CONNECT_CONNECT_IPV6 + choice NET_CONNECT_CONNECT_PREFERRED_IPV6 prompt "Preferred IPv6 Type" - default EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK + default NET_CONNECT_CONNECT_IPV6_PREF_LOCAL_LINK help Select which kind of IPv6 address the connect logic waits for. - config EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK + config NET_CONNECT_CONNECT_IPV6_PREF_LOCAL_LINK bool "Local Link Address" help Blocks until Local link address assigned. - config EXAMPLE_CONNECT_IPV6_PREF_GLOBAL + config NET_CONNECT_CONNECT_IPV6_PREF_GLOBAL bool "Global Address" help Blocks until Global address assigned. - config EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL + config NET_CONNECT_CONNECT_IPV6_PREF_SITE_LOCAL bool "Site Local Address" help Blocks until Site link address assigned. - config EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL + config NET_CONNECT_CONNECT_IPV6_PREF_UNIQUE_LOCAL bool "Unique Local Link Address" help Blocks until Unique local address assigned. diff --git a/components/net_connect/README.md b/components/net_connect/README.md new file mode 100644 index 0000000000..91d1305bbb --- /dev/null +++ b/components/net_connect/README.md @@ -0,0 +1,66 @@ +# net_connect + +This component implements the most common connection methods for ESP32 boards. It provides WiFi, Ethernet, Thread, and PPP connection functionality for network-enabled applications. + +## How to use this component + +Choose the preferred interface (WiFi, Ethernet, Thread, PPPoS) to connect to the network and configure the interface. + +It is possible to enable multiple interfaces simultaneously making the connection phase to block until all the chosen interfaces acquire IP addresses. +It is also possible to disable all interfaces, skipping the connection phase altogether. + +### WiFi + +Choose WiFi connection method (for chipsets that support it) and configure basic WiFi connection properties: +* WiFi SSID +* WiFI password +* Maximum connection retry (connection would be aborted if it doesn't succeed after specified number of retries) +* WiFi scan method (including RSSI and authorization mode threshold) + + + +### Ethernet + +Choose Ethernet connection if your board supports it. The most common settings is using Espressif Ethernet Kit, which is also the recommended HW for this selection. You can also select an SPI ethernet device (if your chipset doesn't support internal EMAC or if you prefer). It is also possible to use OpenCores Ethernet MAC if you're running the example under QEMU. + +### Thread + +Choose Thread connection if your board supports IEEE802.15.4 native radio or works with [OpenThread RCP](../../openthread/ot_rcp/README.md). You can configure the Thread network at menuconfig '->Components->OpenThread->Thread Core Features->Thread Operational Dataset'. + +If the Thread end-device joins a Thread network with a Thread Border Router that has the NAT64 feature enabled, the end-device can access the Internet with the standard DNS APIs after configuring the following properties: +* Enable DNS64 client ('->Components->OpenThread->Thread Core Features->Enable DNS64 client') +* Enable custom DNS external resolve Hook ('->Components->LWIP->Hooks->DNS external resolve Hook->Custom implementation') + +### PPP + +Point to point connection method creates a simple IP tunnel to the counterpart device (running PPP server), typically a Linux machine with pppd service. We currently support only PPP over Serial (using UART or USB CDC). This is useful for simple testing of networking layers, but with some additional configuration on the server side, we could simulate standard model of internet connectivity. The PPP server could be also represented by a cellular modem device with pre-configured connectivity and already switched to PPP mode (this setup is not very flexible though, so we suggest using a standard modem library implementing commands and modes, e.g. [esp_modem](https://components.espressif.com/component/espressif/esp_modem) ). + +> [!Note] +> Note that if you choose USB device, you have to manually add a dependency on `esp_tinyusb` component. This step is necessary to keep the `net_connect` component simple and dependency free. Please run this command from your project location to add the dependency: +> ```bash +> idf.py add-dependency espressif/esp_tinyusb^1 +> ``` + +#### Setup a PPP server + +Connect the board using UART or USB and note the device name, which would be typically: +* `/dev/ttyACMx` for USB devices +* `/dev/ttyUSBx` for UART devices + +Run the pppd server: + +```bash +sudo pppd /dev/ttyACM0 115200 192.168.11.1:192.168.11.2 ms-dns 8.8.8.8 modem local noauth debug nocrtscts nodetach +ipv6 +``` + +Please update the parameters with the correct serial device, baud rate, IP addresses, DNS server, use `+ipv6` if `CONFIG_NET_CONNECT_IPV6=y`. + +#### Connection to outside + +In order to access other network endpoints, we have to configure some IP/translation rules. The easiest method is to setup a masquerade of the PPPD created interface (`ppp0`) to your default networking interface (`${ETH0}`). Here is an example of such rule: + +```bash +sudo iptables -t nat -A POSTROUTING -o ${ETH0} -j MASQUERADE +sudo iptables -A FORWARD -i ${ETH0} -o ppp0 -m state --state RELATED,ESTABLISHED -j ACCEPT +sudo iptables -A FORWARD -i ppp0 -o ${ETH0} -j ACCEPT +``` diff --git a/components/net_connect/addr_from_stdin.c b/components/net_connect/addr_from_stdin.c index e4c94b1a14..91a613a1a5 100644 --- a/components/net_connect/addr_from_stdin.c +++ b/components/net_connect/addr_from_stdin.c @@ -1,8 +1,14 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + #include #include "esp_system.h" #include "esp_log.h" #include "esp_netif.h" -#include "protocol_examples_common.h" +#include "net_connect.h" #include "lwip/sockets.h" #include @@ -18,7 +24,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad // this function could be called multiple times -> make sure UART init runs only once if (!already_init) { - example_configure_stdin_stdout(); + net_configure_stdin_stdout(); already_init = true; } @@ -41,7 +47,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad } for( cur = addr_list; cur != NULL; cur = cur->ai_next ) { memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr)); -#if CONFIG_EXAMPLE_CONNECT_IPV4 +#if CONFIG_NET_CONNECT_CONNECT_IPV4 if (cur->ai_family == AF_INET) { *ip_protocol = IPPROTO_IP; *addr_family = AF_INET; @@ -51,13 +57,13 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad return ESP_OK; } #endif // IPV4 -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 if (cur->ai_family == AF_INET6) { *ip_protocol = IPPROTO_IPV6; *addr_family = AF_INET6; // add port and interface number and return on first IPv6 match ((struct sockaddr_in6*)dest_addr)->sin6_port = htons(port); - ((struct sockaddr_in6*)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); + ((struct sockaddr_in6*)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(NET_CONNECT_INTERFACE); freeaddrinfo( addr_list ); return ESP_OK; } diff --git a/components/net_connect/connect.c b/components/net_connect/connect.c index 65fa2b8568..1f2507e790 100644 --- a/components/net_connect/connect.c +++ b/components/net_connect/connect.c @@ -1,12 +1,12 @@ /* - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * SPDX-License-Identifier: Apache-2.0 */ #include -#include "protocol_examples_common.h" -#include "example_common_private.h" +#include "net_connect.h" +#include "net_connect_private.h" #include "sdkconfig.h" #include "esp_event.h" #include "esp_wifi.h" @@ -19,11 +19,11 @@ #include "lwip/err.h" #include "lwip/sys.h" -static const char *TAG = "example_common"; +static const char *TAG = "net_connect"; -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 /* types of ipv6 addresses to be displayed on ipv6 events */ -const char *example_ipv6_addr_types_to_str[6] = { +const char *net_connect_ipv6_addr_types_to_str[6] = { "ESP_IP6_ADDR_IS_UNKNOWN", "ESP_IP6_ADDR_IS_GLOBAL", "ESP_IP6_ADDR_IS_LINK_LOCAL", @@ -38,7 +38,7 @@ const char *example_ipv6_addr_types_to_str[6] = { * All netifs created within common connect component are prefixed with the module TAG, * so it returns true if the specified netif is owned by this module */ -bool example_is_our_netif(const char *prefix, esp_netif_t *netif) +bool net_connect_is_our_netif(const char *prefix, esp_netif_t *netif) { return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0; } @@ -48,7 +48,7 @@ static bool netif_desc_matches_with(esp_netif_t *netif, void *ctx) return strcmp(ctx, esp_netif_get_desc(netif)) == 0; } -esp_netif_t *get_example_netif_from_desc(const char *desc) +esp_netif_t *net_get_netif_from_desc(const char *desc) { return esp_netif_find_if(netif_desc_matches_with, (void*)desc); } @@ -59,20 +59,20 @@ static esp_err_t print_all_ips_tcpip(void* ctx) // iterate over active interfaces, and print out IPs of "our" netifs esp_netif_t *netif = NULL; while ((netif = esp_netif_next_unsafe(netif)) != NULL) { - if (example_is_our_netif(prefix, netif)) { + if (net_connect_is_our_netif(prefix, netif)) { ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); -#if CONFIG_EXAMPLE_CONNECT_IPV4 +#if CONFIG_NET_CONNECT_CONNECT_IPV4 esp_netif_ip_info_t ip; ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip)); ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip)); #endif -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF]; int ip6_addrs = esp_netif_get_all_ip6(netif, ip6); for (int j = 0; j < ip6_addrs; ++j) { esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j])); - ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), example_ipv6_addr_types_to_str[ipv6_type]); + ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), net_connect_ipv6_addr_types_to_str[ipv6_type]); } #endif } @@ -80,69 +80,69 @@ static esp_err_t print_all_ips_tcpip(void* ctx) return ESP_OK; } -void example_print_all_netif_ips(const char *prefix) +void net_connect_print_all_netif_ips(const char *prefix) { // Print all IPs in TCPIP context to avoid potential races of removing/adding netifs when iterating over the list esp_netif_tcpip_exec(print_all_ips_tcpip, (void*) prefix); } -esp_err_t example_connect(void) +esp_err_t net_connect(void) { -#if CONFIG_EXAMPLE_CONNECT_ETHERNET - if (example_ethernet_connect() != ESP_OK) { +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET + if (net_connect_ethernet_connect() != ESP_OK) { return ESP_FAIL; } - ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown)); + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_ethernet_shutdown)); #endif -#if CONFIG_EXAMPLE_CONNECT_WIFI - if (example_wifi_connect() != ESP_OK) { +#if CONFIG_NET_CONNECT_CONNECT_WIFI + if (net_connect_wifi_connect() != ESP_OK) { return ESP_FAIL; } - ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown)); + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_wifi_shutdown)); #endif -#if CONFIG_EXAMPLE_CONNECT_THREAD - if (example_thread_connect() != ESP_OK) { +#if CONFIG_NET_CONNECT_CONNECT_THREAD + if (net_connect_thread_connect() != ESP_OK) { return ESP_FAIL; } - ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_thread_shutdown)); + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_thread_shutdown)); #endif -#if CONFIG_EXAMPLE_CONNECT_PPP - if (example_ppp_connect() != ESP_OK) { +#if CONFIG_NET_CONNECT_CONNECT_PPP + if (net_connect_ppp_connect() != ESP_OK) { return ESP_FAIL; } - ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ppp_shutdown)); + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_ppp_shutdown)); #endif -#if CONFIG_EXAMPLE_CONNECT_ETHERNET - example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH); +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_ETH); #endif -#if CONFIG_EXAMPLE_CONNECT_WIFI - example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA); +#if CONFIG_NET_CONNECT_CONNECT_WIFI + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_STA); #endif -#if CONFIG_EXAMPLE_CONNECT_THREAD - example_print_all_netif_ips(EXAMPLE_NETIF_DESC_THREAD); +#if CONFIG_NET_CONNECT_CONNECT_THREAD + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_THREAD); #endif -#if CONFIG_EXAMPLE_CONNECT_PPP - example_print_all_netif_ips(EXAMPLE_NETIF_DESC_PPP); +#if CONFIG_NET_CONNECT_CONNECT_PPP + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_PPP); #endif return ESP_OK; } -esp_err_t example_disconnect(void) +esp_err_t net_disconnect(void) { -#if CONFIG_EXAMPLE_CONNECT_ETHERNET - example_ethernet_shutdown(); - ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_ethernet_shutdown)); +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET + net_connect_ethernet_shutdown(); + ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&net_connect_ethernet_shutdown)); #endif -#if CONFIG_EXAMPLE_CONNECT_WIFI - example_wifi_shutdown(); - ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_wifi_shutdown)); +#if CONFIG_NET_CONNECT_CONNECT_WIFI + net_connect_wifi_shutdown(); + ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&net_connect_wifi_shutdown)); #endif return ESP_OK; } diff --git a/components/net_connect/console_cmd.c b/components/net_connect/console_cmd.c index 342a92c769..1c20e5f567 100644 --- a/components/net_connect/console_cmd.c +++ b/components/net_connect/console_cmd.c @@ -1,13 +1,13 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * SPDX-License-Identifier: Apache-2.0 */ #include -#include "protocol_examples_common.h" -#include "example_common_private.h" +#include "net_connect.h" +#include "net_connect_private.h" #include "esp_wifi.h" #include "esp_log.h" #include "esp_console.h" @@ -48,20 +48,20 @@ static int cmd_do_wifi_connect(int argc, char **argv) if (pass) { strlcpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password)); } - example_wifi_sta_do_connect(wifi_config, false); + net_connect_wifi_sta_do_connect(wifi_config, false); return 0; } static int cmd_do_wifi_disconnect(int argc, char **argv) { - example_wifi_sta_do_disconnect(); + net_connect_wifi_sta_do_disconnect(); return 0; } -void example_register_wifi_connect_commands(void) +void net_register_wifi_connect_commands(void) { ESP_LOGI(TAG, "Registering WiFi connect commands."); - example_wifi_start(); + net_connect_wifi_start(); connect_args.ssid = arg_str1(NULL, NULL, "", "SSID of AP"); connect_args.password = arg_str0(NULL, NULL, "", "password of AP"); diff --git a/components/net_connect/eth_connect.c b/components/net_connect/eth_connect.c index 0361ab62f3..52cdda37b1 100644 --- a/components/net_connect/eth_connect.c +++ b/components/net_connect/eth_connect.c @@ -1,12 +1,12 @@ /* * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * SPDX-License-Identifier: Apache-2.0 */ #include -#include "protocol_examples_common.h" -#include "example_common_private.h" +#include "net_connect.h" +#include "net_connect_private.h" #include "esp_event.h" #include "esp_eth.h" #include "esp_log.h" @@ -19,7 +19,7 @@ static const char *TAG = "ethernet_connect"; static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; #endif @@ -33,26 +33,26 @@ static void eth_on_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) { + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_ETH, event->esp_netif)) { return; } ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); xSemaphoreGive(s_semph_get_ip_addrs); } -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; - if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) { + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_ETH, event->esp_netif)) { return; } esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), - IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); - if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { + IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); + if (ipv6_type == NET_CONNECT_PREFERRED_IPV6_TYPE) { xSemaphoreGive(s_semph_get_ip6_addrs); } } @@ -70,7 +70,7 @@ static void on_eth_event(void *esp_netif, esp_event_base_t event_base, } } -#endif // CONFIG_EXAMPLE_CONNECT_IPV6 +#endif // CONFIG_NET_CONNECT_CONNECT_IPV6 static esp_eth_handle_t s_eth_handle = NULL; static esp_eth_mac_t *s_mac = NULL; @@ -80,12 +80,12 @@ static esp_eth_netif_glue_handle_t s_eth_glue = NULL; static esp_netif_t *eth_start(void) { // TODO just to pass builds, will be fixed by IDF-14059 -#if CONFIG_EXAMPLE_USE_DUMMY +#if CONFIG_NET_CONNECT_USE_DUMMY return NULL; #else esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH(); // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) - esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_ETH; + esp_netif_config.if_desc = NET_CONNECT_NETIF_DESC_ETH; esp_netif_config.route_prio = 64; esp_netif_config_t netif_config = { .base = &esp_netif_config, @@ -95,23 +95,23 @@ static esp_netif_t *eth_start(void) assert(netif); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - mac_config.rx_task_stack_size = CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE; + mac_config.rx_task_stack_size = CONFIG_NET_CONNECT_ETHERNET_EMAC_TASK_STACK_SIZE; eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); - phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; - phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; -#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET + phy_config.phy_addr = CONFIG_NET_CONNECT_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_NET_CONNECT_ETH_PHY_RST_GPIO; +#if CONFIG_NET_CONNECT_USE_INTERNAL_ETHERNET eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); - esp32_emac_config.smi_gpio.mdc_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - esp32_emac_config.smi_gpio.mdio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + esp32_emac_config.smi_gpio.mdc_num = CONFIG_NET_CONNECT_ETH_MDC_GPIO; + esp32_emac_config.smi_gpio.mdio_num = CONFIG_NET_CONNECT_ETH_MDIO_GPIO; s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); -#if CONFIG_EXAMPLE_ETH_PHY_GENERIC +#if CONFIG_NET_CONNECT_ETH_PHY_GENERIC s_phy = esp_eth_phy_new_generic(&phy_config); -#endif // CONFIG_EXAMPLE_ETH_PHY_GENERIC -#elif CONFIG_EXAMPLE_USE_OPENETH +#endif // CONFIG_NET_CONNECT_ETH_PHY_GENERIC +#elif CONFIG_NET_CONNECT_USE_OPENETH phy_config.autonego_timeout_ms = 100; s_mac = esp_eth_mac_new_openeth(&mac_config); s_phy = esp_eth_phy_new_generic(&phy_config); -#endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET +#endif // CONFIG_NET_CONNECT_USE_INTERNAL_ETHERNET // Install Ethernet driver esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); @@ -123,21 +123,21 @@ static esp_netif_t *eth_start(void) // Register user defined event handlers ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, ð_on_got_ip, NULL)); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 +#ifdef CONFIG_NET_CONNECT_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, ð_on_got_ipv6, NULL)); #endif esp_eth_start(s_eth_handle); return netif; -#endif // CONFIG_EXAMPLE_USE_DUMMY +#endif // CONFIG_NET_CONNECT_USE_DUMMY } static void eth_stop(void) { - esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH); + esp_netif_t *eth_netif = net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_ETH); ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, ð_on_got_ip)); -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, ð_on_got_ipv6)); ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); #endif @@ -151,35 +151,35 @@ static void eth_stop(void) esp_netif_destroy(eth_netif); } -esp_eth_handle_t get_example_eth_handle(void) +esp_eth_handle_t net_get_eth_handle(void) { return s_eth_handle; } /* tear down connection, release resources */ -void example_ethernet_shutdown(void) +void net_connect_ethernet_shutdown(void) { if (s_semph_get_ip_addrs == NULL) { return; } vSemaphoreDelete(s_semph_get_ip_addrs); s_semph_get_ip_addrs = NULL; -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 vSemaphoreDelete(s_semph_get_ip6_addrs); s_semph_get_ip6_addrs = NULL; #endif eth_stop(); } -esp_err_t example_ethernet_connect(void) +esp_err_t net_connect_ethernet_connect(void) { -#if CONFIG_EXAMPLE_CONNECT_IPV4 +#if CONFIG_NET_CONNECT_CONNECT_IPV4 s_semph_get_ip_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip_addrs == NULL) { return ESP_ERR_NO_MEM; } #endif -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip6_addrs == NULL) { vSemaphoreDelete(s_semph_get_ip_addrs); @@ -188,10 +188,10 @@ esp_err_t example_ethernet_connect(void) #endif eth_start(); ESP_LOGI(TAG, "Waiting for IP(s)."); -#if CONFIG_EXAMPLE_CONNECT_IPV4 +#if CONFIG_NET_CONNECT_CONNECT_IPV4 xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); #endif -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); #endif return ESP_OK; diff --git a/components/net_connect/idf_component.yml b/components/net_connect/idf_component.yml new file mode 100644 index 0000000000..200f6936ff --- /dev/null +++ b/components/net_connect/idf_component.yml @@ -0,0 +1,7 @@ +version: 1.0.0 +url: https://github.com/espressif/esp-protocols/tree/master/components/net_connect +description: Network connection component providing WiFi, Ethernet, Thread, and PPP connection methods for ESP32 boards. +dependencies: + idf: + version: '>=5.0' + diff --git a/components/net_connect/include/addr_from_stdin.h b/components/net_connect/include/addr_from_stdin.h index 827f8c25ac..4e1a10cf82 100644 --- a/components/net_connect/include/addr_from_stdin.h +++ b/components/net_connect/include/addr_from_stdin.h @@ -1,13 +1,7 @@ -/* Common utilities for socket address input interface: - The API get_addr_from_stdin() is mainly used by socket client examples which read IP address from stdin (if configured). - This option is typically used in the CI, but could be enabled in the project configuration. - In that case this component is used to receive a string that is evaluated and processed to output - socket structures to open a connectio - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 */ #pragma once diff --git a/components/net_connect/include/example_common_private.h b/components/net_connect/include/example_common_private.h deleted file mode 100644 index ad6ef537e4..0000000000 --- a/components/net_connect/include/example_common_private.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -/* Private Functions of protocol example common */ - -#pragma once - -#include "esp_err.h" -#include "esp_wifi.h" -#include "sdkconfig.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#if CONFIG_EXAMPLE_CONNECT_IPV6 -#define MAX_IP6_ADDRS_PER_NETIF (5) - -#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL -#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL -#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL -#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL) -#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL -#endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_... - -#endif - - -#if CONFIG_EXAMPLE_CONNECT_IPV6 -extern const char *example_ipv6_addr_types_to_str[6]; -#endif - -void example_wifi_start(void); -void example_wifi_stop(void); -esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait); -esp_err_t example_wifi_sta_do_disconnect(void); -bool example_is_our_netif(const char *prefix, esp_netif_t *netif); -void example_print_all_netif_ips(const char *prefix); -void example_wifi_shutdown(void); -esp_err_t example_wifi_connect(void); -void example_ethernet_shutdown(void); -esp_err_t example_ethernet_connect(void); -void example_thread_shutdown(void); -esp_err_t example_thread_connect(void); -esp_err_t example_ppp_connect(void); -void example_ppp_start(void); -void example_ppp_shutdown(void); - - - -#ifdef __cplusplus -} -#endif diff --git a/components/net_connect/include/net_connect.h b/components/net_connect/include/net_connect.h new file mode 100644 index 0000000000..364018d215 --- /dev/null +++ b/components/net_connect/include/net_connect.h @@ -0,0 +1,151 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "sdkconfig.h" +#include "esp_err.h" +#if !CONFIG_IDF_TARGET_LINUX +#include "esp_netif.h" +#if CONFIG_NET_CONNECT_ETHERNET +#include "esp_eth.h" +#endif +#endif // !CONFIG_IDF_TARGET_LINUX + +#ifdef __cplusplus +extern "C" { +#endif + +#if !CONFIG_IDF_TARGET_LINUX +#if CONFIG_NET_CONNECT_CONNECT_WIFI +#define NET_CONNECT_NETIF_DESC_STA "net_connect_netif_sta" +#endif + +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET +#define NET_CONNECT_NETIF_DESC_ETH "net_connect_netif_eth" +#endif + +#if CONFIG_NET_CONNECT_CONNECT_THREAD +#define NET_CONNECT_NETIF_DESC_THREAD "net_connect_netif_thread" +#endif + +#if CONFIG_NET_CONNECT_CONNECT_PPP +#define NET_CONNECT_NETIF_DESC_PPP "net_connect_netif_ppp" +#endif + +#if CONFIG_NET_CONNECT_WIFI_SCAN_METHOD_FAST +#define NET_CONNECT_WIFI_SCAN_METHOD WIFI_FAST_SCAN +#elif CONFIG_NET_CONNECT_WIFI_SCAN_METHOD_ALL_CHANNEL +#define NET_CONNECT_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN +#endif + +#if CONFIG_NET_CONNECT_WIFI_CONNECT_AP_BY_SIGNAL +#define NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL +#elif CONFIG_NET_CONNECT_WIFI_CONNECT_AP_BY_SECURITY +#define NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY +#endif + +#if CONFIG_NET_CONNECT_WIFI_AUTH_OPEN +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WEP +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA2_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA_WPA2_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA2_ENTERPRISE +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA3_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA2_WPA3_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WAPI_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK +#endif + +/* Network connect default interface, prefer the ethernet one if running in example-test (CI) configuration */ +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_ETH) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_ETH) +#elif CONFIG_NET_CONNECT_CONNECT_WIFI +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_STA) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_STA) +#elif CONFIG_NET_CONNECT_CONNECT_THREAD +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_THREAD) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_THREAD) +#elif CONFIG_NET_CONNECT_CONNECT_PPP +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_PPP) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_PPP) +#endif + +/** + * @brief Configure Wi-Fi or Ethernet, connect, wait for IP + * + * This all-in-one helper function is used in protocols examples to + * reduce the amount of boilerplate in the example. + * + * It is not intended to be used in real world applications. + * See examples under examples/wifi/getting_started/ and examples/ethernet/ + * for more complete Wi-Fi or Ethernet initialization code. + * + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + * + * @return ESP_OK on successful connection + */ +esp_err_t net_connect(void); + +/** + * Counterpart to net_connect, de-initializes Wi-Fi or Ethernet + */ +esp_err_t net_disconnect(void); + +/** + * @brief Configure stdin and stdout to use blocking I/O + * + * This helper function is used in ASIO examples. It wraps installing the + * UART driver and configuring VFS layer to use UART driver for console I/O. + */ +esp_err_t net_configure_stdin_stdout(void); + +/** + * @brief Returns esp-netif pointer created by net_connect() described by + * the supplied desc field + * + * @param desc Textual interface of created network interface, for example "sta" + * indicate default WiFi station, "eth" default Ethernet interface. + * + */ +esp_netif_t *net_get_netif_from_desc(const char *desc); + +#if CONFIG_NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD +/** + * @brief Register wifi connect commands + * + * Provide a simple wifi_connect command in esp_console. + * This function can be used after esp_console is initialized. + */ +void net_register_wifi_connect_commands(void); +#endif + +#if CONFIG_NET_CONNECT_ETHERNET +/** + * @brief Get the network connect Ethernet driver handle + * + * @return esp_eth_handle_t + */ +esp_eth_handle_t net_get_eth_handle(void); +#endif // CONFIG_NET_CONNECT_ETHERNET + +#else +static inline esp_err_t net_connect(void) {return ESP_OK;} +#endif // !CONFIG_IDF_TARGET_LINUX + +#ifdef __cplusplus +} +#endif diff --git a/components/net_connect/include/net_connect_private.h b/components/net_connect/include/net_connect_private.h new file mode 100644 index 0000000000..cb14635d26 --- /dev/null +++ b/components/net_connect/include/net_connect_private.h @@ -0,0 +1,58 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/* Private Functions of protocol example common */ + +#pragma once + +#include "esp_err.h" +#include "esp_wifi.h" +#include "sdkconfig.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +#define MAX_IP6_ADDRS_PER_NETIF (5) + +#if defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_LOCAL_LINK) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL +#elif defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_GLOBAL) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL +#elif defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_SITE_LOCAL) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL +#elif defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_UNIQUE_LOCAL) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL +#endif // if-elif CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_... + +#endif + + +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +extern const char *net_connect_ipv6_addr_types_to_str[6]; +#endif + +void net_connect_wifi_start(void); +void net_connect_wifi_stop(void); +esp_err_t net_connect_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait); +esp_err_t net_connect_wifi_sta_do_disconnect(void); +bool net_connect_is_our_netif(const char *prefix, esp_netif_t *netif); +void net_connect_print_all_netif_ips(const char *prefix); +void net_connect_wifi_shutdown(void); +esp_err_t net_connect_wifi_connect(void); +void net_connect_ethernet_shutdown(void); +esp_err_t net_connect_ethernet_connect(void); +void net_connect_thread_shutdown(void); +esp_err_t net_connect_thread_connect(void); +esp_err_t net_connect_ppp_connect(void); +void net_connect_ppp_start(void); +void net_connect_ppp_shutdown(void); + + + +#ifdef __cplusplus +} +#endif diff --git a/components/net_connect/include/protocol_examples_common.h b/components/net_connect/include/protocol_examples_common.h deleted file mode 100644 index 54e2c07a11..0000000000 --- a/components/net_connect/include/protocol_examples_common.h +++ /dev/null @@ -1,154 +0,0 @@ -/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. - */ - -#pragma once - -#include "sdkconfig.h" -#include "esp_err.h" -#if !CONFIG_IDF_TARGET_LINUX -#include "esp_netif.h" -#if CONFIG_EXAMPLE_CONNECT_ETHERNET -#include "esp_eth.h" -#endif -#endif // !CONFIG_IDF_TARGET_LINUX - -#ifdef __cplusplus -extern "C" { -#endif - -#if !CONFIG_IDF_TARGET_LINUX -#if CONFIG_EXAMPLE_CONNECT_WIFI -#define EXAMPLE_NETIF_DESC_STA "example_netif_sta" -#endif - -#if CONFIG_EXAMPLE_CONNECT_ETHERNET -#define EXAMPLE_NETIF_DESC_ETH "example_netif_eth" -#endif - -#if CONFIG_EXAMPLE_CONNECT_THREAD -#define EXAMPLE_NETIF_DESC_THREAD "example_netif_thread" -#endif - -#if CONFIG_EXAMPLE_CONNECT_PPP -#define EXAMPLE_NETIF_DESC_PPP "example_netif_ppp" -#endif - -#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST -#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN -#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL -#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN -#endif - -#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL -#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL -#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY -#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY -#endif - -#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN -#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK -#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK -#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK -#endif - -/* Example default interface, prefer the ethernet one if running in example-test (CI) configuration */ -#if CONFIG_EXAMPLE_CONNECT_ETHERNET -#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH) -#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH) -#elif CONFIG_EXAMPLE_CONNECT_WIFI -#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) -#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA) -#elif CONFIG_EXAMPLE_CONNECT_THREAD -#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_THREAD) -#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_THREAD) -#elif CONFIG_EXAMPLE_CONNECT_PPP -#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP) -#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP) -#endif - -/** - * @brief Configure Wi-Fi or Ethernet, connect, wait for IP - * - * This all-in-one helper function is used in protocols examples to - * reduce the amount of boilerplate in the example. - * - * It is not intended to be used in real world applications. - * See examples under examples/wifi/getting_started/ and examples/ethernet/ - * for more complete Wi-Fi or Ethernet initialization code. - * - * Read "Establishing Wi-Fi or Ethernet Connection" section in - * examples/protocols/README.md for more information about this function. - * - * @return ESP_OK on successful connection - */ -esp_err_t example_connect(void); - -/** - * Counterpart to example_connect, de-initializes Wi-Fi or Ethernet - */ -esp_err_t example_disconnect(void); - -/** - * @brief Configure stdin and stdout to use blocking I/O - * - * This helper function is used in ASIO examples. It wraps installing the - * UART driver and configuring VFS layer to use UART driver for console I/O. - */ -esp_err_t example_configure_stdin_stdout(void); - -/** - * @brief Returns esp-netif pointer created by example_connect() described by - * the supplied desc field - * - * @param desc Textual interface of created network interface, for example "sta" - * indicate default WiFi station, "eth" default Ethernet interface. - * - */ -esp_netif_t *get_example_netif_from_desc(const char *desc); - -#if CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD -/** - * @brief Register wifi connect commands - * - * Provide a simple wifi_connect command in esp_console. - * This function can be used after esp_console is initialized. - */ -void example_register_wifi_connect_commands(void); -#endif - -#if CONFIG_EXAMPLE_CONNECT_ETHERNET -/** - * @brief Get the example Ethernet driver handle - * - * @return esp_eth_handle_t - */ -esp_eth_handle_t get_example_eth_handle(void); -#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET - -#else -static inline esp_err_t example_connect(void) {return ESP_OK;} -#endif // !CONFIG_IDF_TARGET_LINUX - -#ifdef __cplusplus -} -#endif diff --git a/components/net_connect/include/protocol_examples_thread_config.h b/components/net_connect/include/protocol_examples_thread_config.h index 532f828b65..cdd269599c 100644 --- a/components/net_connect/include/protocol_examples_thread_config.h +++ b/components/net_connect/include/protocol_examples_thread_config.h @@ -1,9 +1,9 @@ /* * Thread configurations for protocol examples * - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * SPDX-License-Identifier: Apache-2.0 */ #pragma once @@ -23,10 +23,10 @@ .radio_mode = RADIO_MODE_UART_RCP, \ .radio_uart_config = \ { \ - .port = CONFIG_EXAMPLE_THREAD_UART_PORT, \ + .port = CONFIG_NET_CONNECT_THREAD_UART_PORT, \ .uart_config = \ { \ - .baud_rate = CONFIG_EXAMPLE_THREAD_UART_BAUD, \ + .baud_rate = CONFIG_NET_CONNECT_THREAD_UART_BAUD, \ .data_bits = UART_DATA_8_BITS, \ .parity = UART_PARITY_DISABLE, \ .stop_bits = UART_STOP_BITS_1, \ @@ -34,8 +34,8 @@ .rx_flow_ctrl_thresh = 0, \ .source_clk = UART_SCLK_DEFAULT, \ }, \ - .rx_pin = CONFIG_EXAMPLE_THREAD_UART_RX_PIN, \ - .tx_pin = CONFIG_EXAMPLE_THREAD_UART_TX_PIN, \ + .rx_pin = CONFIG_NET_CONNECT_THREAD_UART_RX_PIN, \ + .tx_pin = CONFIG_NET_CONNECT_THREAD_UART_TX_PIN, \ }, \ } #elif defined(CONFIG_OPENTHREAD_RADIO_SPINEL_SPI) @@ -48,9 +48,9 @@ .dma_channel = 2, \ .spi_interface = \ { \ - .mosi_io_num = CONFIG_EXAMPLE_THREAD_SPI_MOSI_PIN, \ - .miso_io_num = CONFIG_EXAMPLE_THREAD_SPI_MISO_PIN, \ - .sclk_io_num = CONFIG_EXAMPLE_THREAD_SPI_SCLK_PIN, \ + .mosi_io_num = CONFIG_NET_CONNECT_THREAD_SPI_MOSI_PIN, \ + .miso_io_num = CONFIG_NET_CONNECT_THREAD_SPI_MISO_PIN, \ + .sclk_io_num = CONFIG_NET_CONNECT_THREAD_SPI_SCLK_PIN, \ .quadwp_io_num = -1, \ .quadhd_io_num = -1, \ }, \ @@ -60,10 +60,10 @@ .input_delay_ns = 100, \ .mode = 0, \ .clock_speed_hz = 2500 * 1000, \ - .spics_io_num = CONFIG_EXAMPLE_THREAD_SPI_CS_PIN, \ + .spics_io_num = CONFIG_NET_CONNECT_THREAD_SPI_CS_PIN, \ .queue_size = 5, \ }, \ - .intr_pin = CONFIG_EXAMPLE_THREAD_SPI_INTR_PIN, \ + .intr_pin = CONFIG_NET_CONNECT_THREAD_SPI_INTR_PIN, \ }, \ } #else diff --git a/components/net_connect/include/protocol_examples_utils.h b/components/net_connect/include/protocol_examples_utils.h index 79031171dc..dd39aa2582 100644 --- a/components/net_connect/include/protocol_examples_utils.h +++ b/components/net_connect/include/protocol_examples_utils.h @@ -1,9 +1,9 @@ /* * Utility functions for protocol examples * - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * SPDX-License-Identifier: Apache-2.0 */ #pragma once diff --git a/components/net_connect/ppp_connect.c b/components/net_connect/ppp_connect.c index 090a3ed69c..84e79d7431 100644 --- a/components/net_connect/ppp_connect.c +++ b/components/net_connect/ppp_connect.c @@ -1,23 +1,23 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * SPDX-License-Identifier: Apache-2.0 */ #include #include #include "sdkconfig.h" -#include "protocol_examples_common.h" -#include "example_common_private.h" +#include "net_connect.h" +#include "net_connect_private.h" -#if CONFIG_EXAMPLE_CONNECT_PPP +#if CONFIG_NET_CONNECT_CONNECT_PPP #include "esp_log.h" #include "esp_netif.h" #include "esp_netif_ppp.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB #include "tinyusb.h" #include "tusb_cdc_acm.h" @@ -39,7 +39,7 @@ static EventGroupHandle_t s_event_group = NULL; static esp_netif_t *s_netif; static const int GOT_IPV4 = BIT0; static const int CONNECTION_FAILED = BIT1; -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 static const int GOT_IPV6 = BIT2; #define CONNECT_BITS (GOT_IPV4|GOT_IPV6|CONNECTION_FAILED) #else @@ -49,7 +49,7 @@ static const int GOT_IPV6 = BIT2; static esp_err_t transmit(void *h, void *buffer, size_t len) { ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE); -#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB tinyusb_cdcacm_write_queue(s_itf, buffer, len); tinyusb_cdcacm_write_flush(s_itf, 0); #else // DEVICE_UART @@ -70,7 +70,7 @@ static void on_ip_event(void *arg, esp_event_base_t event_base, if (event_id == IP_EVENT_PPP_GOT_IP) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - if (!example_is_our_netif(EXAMPLE_NETIF_DESC_PPP, event->esp_netif)) { + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_PPP, event->esp_netif)) { return; } esp_netif_t *netif = event->esp_netif; @@ -79,23 +79,23 @@ static void on_ip_event(void *arg, esp_event_base_t event_base, esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info); ESP_LOGI(TAG, "Main DNS server : " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4)); xEventGroupSetBits(s_event_group, GOT_IPV4); -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 } else if (event_id == IP_EVENT_GOT_IP6) { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; - if (!example_is_our_netif(EXAMPLE_NETIF_DESC_PPP, event->esp_netif)) { + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_PPP, event->esp_netif)) { return; } esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), - IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); - if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { + IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); + if (ipv6_type == NET_CONNECT_PREFERRED_IPV6_TYPE) { xEventGroupSetBits(s_event_group, GOT_IPV6); } #endif } else if (event_id == IP_EVENT_PPP_LOST_IP) { ESP_LOGI(TAG, "Disconnect from PPP Server"); s_retry_num++; - if (s_retry_num > CONFIG_EXAMPLE_PPP_CONN_MAX_RETRY) { + if (s_retry_num > CONFIG_NET_CONNECT_PPP_CONN_MAX_RETRY) { ESP_LOGE(TAG, "PPP Connection failed %d times, stop reconnecting.", s_retry_num); xEventGroupSetBits(s_event_group, CONNECTION_FAILED); } else { @@ -107,7 +107,7 @@ static void on_ip_event(void *arg, esp_event_base_t event_base, } } -#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB static void cdc_rx_callback(int itf, cdcacm_event_t *event) { size_t rx_size = 0; @@ -135,7 +135,7 @@ static void line_state_changed(int itf, cdcacm_event_t *event) static void ppp_task(void *args) { uart_config_t uart_config = {}; - uart_config.baud_rate = CONFIG_EXAMPLE_CONNECT_UART_BAUDRATE; + uart_config.baud_rate = CONFIG_NET_CONNECT_CONNECT_UART_BAUDRATE; uart_config.data_bits = UART_DATA_8_BITS; uart_config.parity = UART_PARITY_DISABLE; uart_config.stop_bits = UART_STOP_BITS_1; @@ -145,7 +145,7 @@ static void ppp_task(void *args) QueueHandle_t event_queue; ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, BUF_SIZE, 0, 16, &event_queue, 0)); ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart_config)); - ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, CONFIG_EXAMPLE_CONNECT_UART_TX_PIN, CONFIG_EXAMPLE_CONNECT_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); + ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, CONFIG_NET_CONNECT_CONNECT_UART_TX_PIN, CONFIG_NET_CONNECT_CONNECT_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); ESP_ERROR_CHECK(uart_set_rx_timeout(UART_NUM_1, 1)); char *buffer = (char*)malloc(BUF_SIZE); @@ -173,11 +173,11 @@ static void ppp_task(void *args) #endif // CONNECT_PPP_DEVICE -esp_err_t example_ppp_connect(void) +esp_err_t net_connect_ppp_connect(void) { ESP_LOGI(TAG, "Start example_connect."); -#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB ESP_LOGI(TAG, "USB initialization"); const tinyusb_config_t tusb_cfg = { .device_descriptor = NULL, @@ -203,14 +203,14 @@ esp_err_t example_ppp_connect(void) TINYUSB_CDC_ACM_0, CDC_EVENT_LINE_STATE_CHANGED, &line_state_changed)); -#endif // CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +#endif // CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB s_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event, NULL)); esp_netif_inherent_config_t base_netif_cfg = ESP_NETIF_INHERENT_DEFAULT_PPP(); - base_netif_cfg.if_desc = EXAMPLE_NETIF_DESC_PPP; + base_netif_cfg.if_desc = NET_CONNECT_NETIF_DESC_PPP; esp_netif_config_t netif_ppp_config = { .base = &base_netif_cfg, .driver = ppp_driver_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_PPP @@ -218,7 +218,7 @@ esp_err_t example_ppp_connect(void) s_netif = esp_netif_new(&netif_ppp_config); assert(s_netif); -#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB esp_netif_action_start(s_netif, 0, 0, 0); esp_netif_action_connected(s_netif, 0, 0, 0); #else // DEVICE is UART @@ -240,10 +240,10 @@ esp_err_t example_ppp_connect(void) return ESP_OK; } -void example_ppp_shutdown(void) +void net_connect_ppp_shutdown(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event)); -#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_UART +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_UART s_stop_task = true; vTaskDelay(pdMS_TO_TICKS(1000)); // wait for the ppp task to stop #endif @@ -257,4 +257,4 @@ void example_ppp_shutdown(void) s_event_group = NULL; } -#endif // CONFIG_EXAMPLE_CONNECT_PPP +#endif // CONFIG_NET_CONNECT_CONNECT_PPP diff --git a/components/net_connect/protocol_examples_utils.c b/components/net_connect/protocol_examples_utils.c index e6e3de5f62..361623735e 100644 --- a/components/net_connect/protocol_examples_utils.c +++ b/components/net_connect/protocol_examples_utils.c @@ -3,10 +3,9 @@ * * SPDX-FileCopyrightText: 2002-2021 Igor Sysoev * 2011-2022 Nginx, Inc. + * 2023-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: BSD-2-Clause - * - * SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-License-Identifier: Apache-2.0 */ /* * Copyright (C) 2002-2021 Igor Sysoev diff --git a/components/net_connect/stdin_out.c b/components/net_connect/stdin_out.c index 57c61fb8f8..daa6b1e2c7 100644 --- a/components/net_connect/stdin_out.c +++ b/components/net_connect/stdin_out.c @@ -1,13 +1,10 @@ -/* Common functions for protocol examples, to configure stdin and stdout. - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 */ -#include "protocol_examples_common.h" +#include "net_connect.h" #include "esp_err.h" #include "driver/uart_vfs.h" #include "driver/uart.h" diff --git a/components/net_connect/thread_connect.c b/components/net_connect/thread_connect.c index f4dae26e56..3899d3c4ba 100644 --- a/components/net_connect/thread_connect.c +++ b/components/net_connect/thread_connect.c @@ -1,15 +1,15 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * SPDX-License-Identifier: Apache-2.0 */ #include "esp_err.h" #include "esp_event.h" #include "esp_event_base.h" #include "esp_vfs_eventfd.h" -#include "example_common_private.h" -#include "protocol_examples_common.h" +#include "net_connect_private.h" +#include "net_connect.h" #include "protocol_examples_thread_config.h" #include "esp_log.h" #include @@ -49,7 +49,7 @@ static void ot_task_worker(void *aContext) }; esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_OPENTHREAD(); - esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_THREAD; + esp_netif_config.if_desc = NET_CONNECT_NETIF_DESC_THREAD; esp_netif_config_t cfg = { .base = &esp_netif_config, .stack = &g_esp_netif_netstack_default_openthread, @@ -84,7 +84,7 @@ static void ot_task_worker(void *aContext) } /* tear down connection, release resources */ -void example_thread_shutdown(void) +void net_connect_thread_shutdown(void) { vTaskDelete(s_ot_task_handle); esp_openthread_netif_glue_deinit(); @@ -94,7 +94,7 @@ void example_thread_shutdown(void) vSemaphoreDelete(s_semph_thread_attached); } -esp_err_t example_thread_connect(void) +esp_err_t net_connect_thread_connect(void) { s_semph_thread_attached = xSemaphoreCreateBinary(); if (s_semph_thread_attached == NULL) { @@ -115,7 +115,7 @@ esp_err_t example_thread_connect(void) }; esp_vfs_eventfd_register(&eventfd_config); ESP_ERROR_CHECK(esp_event_handler_register(OPENTHREAD_EVENT, ESP_EVENT_ANY_ID, thread_event_handler, NULL)); - if (xTaskCreate(ot_task_worker, "ot_br_main", CONFIG_EXAMPLE_THREAD_TASK_STACK_SIZE, NULL, 5, &s_ot_task_handle) != pdPASS) { + if (xTaskCreate(ot_task_worker, "ot_br_main", CONFIG_NET_CONNECT_THREAD_TASK_STACK_SIZE, NULL, 5, &s_ot_task_handle) != pdPASS) { vSemaphoreDelete(s_semph_thread_attached); vSemaphoreDelete(s_semph_thread_set_dns_server); ESP_LOGE(TAG, "Failed to create openthread task"); diff --git a/components/net_connect/wifi_connect.c b/components/net_connect/wifi_connect.c index a2b88200ee..4e933e41e3 100644 --- a/components/net_connect/wifi_connect.c +++ b/components/net_connect/wifi_connect.c @@ -1,28 +1,20 @@ /* * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. + * SPDX-License-Identifier: Apache-2.0 */ #include -#include "protocol_examples_common.h" -#include "example_common_private.h" +#include "net_connect.h" +#include "net_connect_private.h" #include "esp_log.h" -#if CONFIG_EXAMPLE_CONNECT_WIFI +#if CONFIG_NET_CONNECT_CONNECT_WIFI static const char *TAG = "example_connect"; static esp_netif_t *s_example_sta_netif = NULL; static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; #endif @@ -32,18 +24,18 @@ static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event int32_t event_id, void *event_data) { s_retry_num++; - if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { + if (s_retry_num > CONFIG_NET_CONNECT_WIFI_CONN_MAX_RETRY) { ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num); - /* let example_wifi_sta_do_connect() return */ + /* let net_connect_wifi_sta_do_connect() return */ if (s_semph_get_ip_addrs) { xSemaphoreGive(s_semph_get_ip_addrs); } -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 if (s_semph_get_ip6_addrs) { xSemaphoreGive(s_semph_get_ip6_addrs); } #endif - example_wifi_sta_do_disconnect(); + net_connect_wifi_sta_do_disconnect(); return; } wifi_event_sta_disconnected_t *disconn = event_data; @@ -62,9 +54,9 @@ static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base, int32_t event_id, void *event_data) { -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 esp_netif_create_ip6_linklocal(esp_netif); -#endif // CONFIG_EXAMPLE_CONNECT_IPV6 +#endif // CONFIG_NET_CONNECT_CONNECT_IPV6 } static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base, @@ -72,7 +64,7 @@ static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base { s_retry_num = 0; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_STA, event->esp_netif)) { return; } ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); @@ -83,37 +75,37 @@ static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base } } -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; - if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_STA, event->esp_netif)) { return; } esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), - IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); + IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); - if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { + if (ipv6_type == NET_CONNECT_PREFERRED_IPV6_TYPE) { if (s_semph_get_ip6_addrs) { xSemaphoreGive(s_semph_get_ip6_addrs); } else { - ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); + ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); } } } -#endif // CONFIG_EXAMPLE_CONNECT_IPV6 +#endif // CONFIG_NET_CONNECT_CONNECT_IPV6 -void example_wifi_start(void) +void net_connect_wifi_start(void) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) - esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_STA; + esp_netif_config.if_desc = NET_CONNECT_NETIF_DESC_STA; esp_netif_config.route_prio = 128; s_example_sta_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config); esp_wifi_set_default_wifi_sta_handlers(); @@ -124,7 +116,7 @@ void example_wifi_start(void) } -void example_wifi_stop(void) +void net_connect_wifi_stop(void) { esp_err_t err = esp_wifi_stop(); if (err == ESP_ERR_WIFI_NOT_INIT) { @@ -138,14 +130,14 @@ void example_wifi_stop(void) } -esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) +esp_err_t net_connect_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) { if (wait) { s_semph_get_ip_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip_addrs == NULL) { return ESP_ERR_NO_MEM; } -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip6_addrs == NULL) { vSemaphoreDelete(s_semph_get_ip_addrs); @@ -157,7 +149,7 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif)); -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6, NULL)); #endif @@ -170,58 +162,58 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) } if (wait) { ESP_LOGI(TAG, "Waiting for IP(s)"); -#if CONFIG_EXAMPLE_CONNECT_IPV4 +#if CONFIG_NET_CONNECT_CONNECT_IPV4 xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); vSemaphoreDelete(s_semph_get_ip_addrs); s_semph_get_ip_addrs = NULL; #endif -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); vSemaphoreDelete(s_semph_get_ip6_addrs); s_semph_get_ip6_addrs = NULL; #endif - if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { + if (s_retry_num > CONFIG_NET_CONNECT_WIFI_CONN_MAX_RETRY) { return ESP_FAIL; } } return ESP_OK; } -esp_err_t example_wifi_sta_do_disconnect(void) +esp_err_t net_connect_wifi_sta_do_disconnect(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect)); ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip)); ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect)); -#if CONFIG_EXAMPLE_CONNECT_IPV6 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6)); #endif return esp_wifi_disconnect(); } -void example_wifi_shutdown(void) +void net_connect_wifi_shutdown(void) { - example_wifi_sta_do_disconnect(); - example_wifi_stop(); + net_connect_wifi_sta_do_disconnect(); + net_connect_wifi_stop(); } -esp_err_t example_wifi_connect(void) +esp_err_t net_connect_wifi_connect(void) { ESP_LOGI(TAG, "Start example_connect."); - example_wifi_start(); + net_connect_wifi_start(); wifi_config_t wifi_config = { .sta = { -#if !CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN - .ssid = CONFIG_EXAMPLE_WIFI_SSID, - .password = CONFIG_EXAMPLE_WIFI_PASSWORD, +#if !CONFIG_NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + .ssid = CONFIG_NET_CONNECT_WIFI_SSID, + .password = CONFIG_NET_CONNECT_WIFI_PASSWORD, #endif - .scan_method = EXAMPLE_WIFI_SCAN_METHOD, - .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD, - .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD, - .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD, + .scan_method = NET_CONNECT_WIFI_SCAN_METHOD, + .sort_method = NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD, + .threshold.rssi = CONFIG_NET_CONNECT_WIFI_SCAN_RSSI_THRESHOLD, + .threshold.authmode = NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD, }, }; -#if CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN - example_configure_stdin_stdout(); +#if CONFIG_NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + net_configure_stdin_stdout(); char buf[sizeof(wifi_config.sta.ssid)+sizeof(wifi_config.sta.password)+2] = {0}; ESP_LOGI(TAG, "Please input ssid password:"); fgets(buf, sizeof(buf), stdin); @@ -240,8 +232,8 @@ esp_err_t example_wifi_connect(void) wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN; } #endif - return example_wifi_sta_do_connect(wifi_config, true); + return net_connect_wifi_sta_do_connect(wifi_config, true); } -#endif /* CONFIG_EXAMPLE_CONNECT_WIFI */ +#endif /* CONFIG_NET_CONNECT_CONNECT_WIFI */ From 24d6b845abe99a3cc080159596717e214adc0cfc Mon Sep 17 00:00:00 2001 From: Abhik Roy Date: Wed, 12 Nov 2025 00:17:22 +1100 Subject: [PATCH 91/91] fix(common): Added formatting fixes --- .../freertos/include/freertos/FreeRTOS.h | 2 +- .../examples/host/main/register_iperf.c | 6 ++-- .../modem_console/main/console_helper.hpp | 2 +- .../examples/modem_console/main/ping_handle.c | 4 +-- .../main/sock_commands_sim7600.cpp | 20 +++++------ .../components/SIM7070_gnss/SIM7070_gnss.cpp | 6 ++-- .../include/cxx_include/esp_modem_buffer.hpp | 2 +- .../esp_modem/src/esp_modem_uart_linux.cpp | 2 +- .../src/esp_modem_vfs_socket_creator.cpp | 2 +- .../test/host_test/main/LoopbackTerm.cpp | 2 +- .../esp_modem/test/target/main/pppd_test.cpp | 2 +- .../test/target_iperf/main/cmd_pppclient.c | 6 ++-- .../manual_ota/transport_batch_tls.cpp | 2 +- .../examples/ssl/main/mqtt_ssl_example.cpp | 2 +- .../udp_mutual_auth/main/udp_mutual.cpp | 6 ++-- components/mbedtls_cxx/mbedtls_wrap.cpp | 10 +++--- .../mdns/tests/test_afl_fuzz_host/mdns_di.h | 12 +++---- components/mosquitto/port/signals.c | 2 +- components/net_connect/addr_from_stdin.c | 14 ++++---- components/net_connect/console_cmd.c | 4 +-- components/net_connect/eth_connect.c | 4 +-- components/net_connect/idf_component.yml | 1 - components/net_connect/include/net_connect.h | 5 ++- components/net_connect/ppp_connect.c | 36 +++++++++---------- .../net_connect/protocol_examples_utils.c | 12 +++---- components/net_connect/stdin_out.c | 6 ++-- components/net_connect/wifi_connect.c | 12 +++---- .../multiple_netifs/main/check_connection.c | 4 +-- .../slip_custom_netif/main/slip_client_main.c | 2 +- 29 files changed, 96 insertions(+), 94 deletions(-) diff --git a/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h b/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h index 83b4fff0a2..9dc370539c 100644 --- a/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h +++ b/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h @@ -21,7 +21,7 @@ typedef void *EventGroupHandle_t; typedef uint32_t TickType_t; typedef TickType_t EventBits_t; -typedef void (*TaskFunction_t)( void * ); +typedef void (*TaskFunction_t)(void *); typedef unsigned int UBaseType_t; typedef int BaseType_t; diff --git a/components/eppp_link/examples/host/main/register_iperf.c b/components/eppp_link/examples/host/main/register_iperf.c index 63fded10c5..b8a6348c12 100644 --- a/components/eppp_link/examples/host/main/register_iperf.c +++ b/components/eppp_link/examples/host/main/register_iperf.c @@ -139,9 +139,9 @@ static int ppp_cmd_iperf(int argc, char **argv) cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp", cfg.flag & IPERF_FLAG_SERVER ? "server" : "client", (uint16_t) cfg.source_ip4 & 0xFF, - (uint16_t) (cfg.source_ip4 >> 8) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 16) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 24) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 8) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 16) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 24) & 0xFF, cfg.sport, cfg.destination_ip4 & 0xFF, (cfg.destination_ip4 >> 8) & 0xFF, (cfg.destination_ip4 >> 16) & 0xFF, (cfg.destination_ip4 >> 24) & 0xFF, cfg.dport, diff --git a/components/esp_modem/examples/modem_console/main/console_helper.hpp b/components/esp_modem/examples/modem_console/main/console_helper.hpp index c6012a7e02..ceae2a7755 100644 --- a/components/esp_modem/examples/modem_console/main/console_helper.hpp +++ b/components/esp_modem/examples/modem_console/main/console_helper.hpp @@ -80,7 +80,7 @@ class ConsoleCommand { * @param f Function callback for the command */ template explicit ConsoleCommand(const char *command, const char *help, const T *arg_struct, size_t srg_struct_size, - std::function f): func(std::move(f)) + std::function f): func(std::move(f)) { size_t args_plain_size = srg_struct_size / sizeof(CommandArgs); auto first_arg = reinterpret_cast(arg_struct); diff --git a/components/esp_modem/examples/modem_console/main/ping_handle.c b/components/esp_modem/examples/modem_console/main/ping_handle.c index 7163ebfb21..2839d899f7 100644 --- a/components/esp_modem/examples/modem_console/main/ping_handle.c +++ b/components/esp_modem/examples/modem_console/main/ping_handle.c @@ -101,10 +101,10 @@ static int do_ping_cmd(int argc, char **argv) return 1; } if (res->ai_family == AF_INET) { - struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; + struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr; inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); } else { - struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr; + struct in6_addr addr6 = ((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr; inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6); } freeaddrinfo(res); diff --git a/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp b/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp index 5e1343ceb0..30b82f2954 100644 --- a/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp +++ b/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp @@ -18,13 +18,13 @@ using namespace esp_modem; command_result net_open(CommandableIf *term) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string response; auto ret = dce_commands::generic_get_string(term, "AT+NETOPEN?\r", response, 1000); if (ret != command_result::OK) { return ret; } - ESP_LOGV(TAG, "%s", response.data() ); + ESP_LOGV(TAG, "%s", response.data()); if (response.find("+NETOPEN: 1") != std::string::npos) { ESP_LOGD(TAG, "Already there"); ret = command_result::OK; @@ -42,23 +42,23 @@ command_result net_open(CommandableIf *term) command_result net_close(CommandableIf *term) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); return dce_commands::generic_command(term, "AT+NETCLOSE\r", "+NETCLOSE:", "ERROR", 30000); } command_result tcp_open(CommandableIf *term, const std::string &host, int port, int timeout) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); auto ret = dce_commands::generic_command(term, "AT+CIPRXGET=1\r", "OK", "ERROR", 50000); if (ret != command_result::OK) { ESP_LOGE(TAG, "Setting Rx mode failed!"); return ret; } - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string ip_open = R"(AT+CIPOPEN=0,"TCP",")" + host + "\"," + std::to_string(port) + "\r"; ret = dce_commands::generic_command(term, ip_open, "+CIPOPEN: 0,0", "ERROR", timeout); if (ret != command_result::OK) { - ESP_LOGE(TAG, "%s Failed", __func__ ); + ESP_LOGE(TAG, "%s Failed", __func__); return ret; } return command_result::OK; @@ -66,13 +66,13 @@ command_result tcp_open(CommandableIf *term, const std::string &host, int port, command_result tcp_close(CommandableIf *term) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); return dce_commands::generic_command(term, "AT+CIPCLOSE=0\r", "+CIPCLOSE:", "ERROR", 10000); } command_result tcp_send(CommandableIf *term, uint8_t *data, size_t len) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string send = "AT+CIPSEND=0," + std::to_string(len) + "\r"; auto ret = term->command(send, [&](uint8_t *data, size_t len) { std::string_view response((char *)data, len); @@ -107,7 +107,7 @@ command_result tcp_send(CommandableIf *term, uint8_t *data, size_t len) uint8_t ctrl_z = '\x1A'; term->write(&ctrl_z, 1); int count = 0; - while (ret == command_result::TIMEOUT && count++ < 1000 ) { + while (ret == command_result::TIMEOUT && count++ < 1000) { vTaskDelay(pdMS_TO_TICKS(1000)); } term->on_read(nullptr); @@ -116,7 +116,7 @@ command_result tcp_send(CommandableIf *term, uint8_t *data, size_t len) command_result tcp_recv(CommandableIf *term, uint8_t *data, size_t len, size_t &out_len) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string out; auto ret = dce_commands::generic_get_string(term, "AT+CIPRXGET=4,0\r", out); if (ret != command_result::OK) { diff --git a/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp b/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp index 981b3ce44b..c5082390e4 100644 --- a/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp +++ b/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp @@ -43,8 +43,8 @@ class LocalFactory: public Factory { * @return unique pointer of the resultant DCE */ std::unique_ptr create_SIM7070_GNSS_dce(const esp_modem::dce_config *config, - std::shared_ptr dte, - esp_netif_t *netif) + std::shared_ptr dte, + esp_netif_t *netif) { return gnss_factory::LocalFactory::create(config, std::move(dte), netif); } @@ -52,7 +52,7 @@ std::unique_ptr create_SIM7070_GNSS_dce(const esp_modem::dce_config *c esp_modem::command_result get_gnss_information_sim70xx_lib(esp_modem::CommandableIf *t, sim70xx_gps_t &gps) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string str_out; auto ret = esp_modem::dce_commands::generic_get_string(t, "AT+CGNSINF\r", str_out); if (ret != esp_modem::command_result::OK) { diff --git a/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp b/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp index a26ee8e017..41111941d2 100644 --- a/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp +++ b/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp @@ -14,7 +14,7 @@ namespace esp_modem { */ struct unique_buffer { explicit unique_buffer(size_t size); - unique_buffer (unique_buffer const &) = delete; + unique_buffer(unique_buffer const &) = delete; unique_buffer &operator=(unique_buffer const &) = delete; unique_buffer(unique_buffer &&other) noexcept { diff --git a/components/esp_modem/src/esp_modem_uart_linux.cpp b/components/esp_modem/src/esp_modem_uart_linux.cpp index e6aa222a4f..e4e67fcc90 100644 --- a/components/esp_modem/src/esp_modem_uart_linux.cpp +++ b/components/esp_modem/src/esp_modem_uart_linux.cpp @@ -17,7 +17,7 @@ constexpr const char *TAG = "uart_resource"; uart_resource::uart_resource(const esp_modem_uart_term_config *config, QueueHandle_t *event_queue, int fd): port(-1) { - ESP_LOGD(TAG, "Creating uart resource" ); + ESP_LOGD(TAG, "Creating uart resource"); struct termios tty = {}; ESP_MODEM_THROW_IF_FALSE(tcgetattr(fd, &tty) == 0, "Failed to tcgetattr()"); diff --git a/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp b/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp index 7571d2dfe2..7a80d01e94 100644 --- a/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp +++ b/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp @@ -52,7 +52,7 @@ static esp_err_t hostname_to_fd(const char *host, int port, int *fd) auto *p = reinterpret_cast(address_info->ai_addr); p->sin_port = htons(port); ESP_LOGI(TAG, "[sock=%d] Resolved IPv4 address: %s", *fd, inet_ntoa(p->sin_addr)); - memcpy(&address, p, sizeof(struct sockaddr )); + memcpy(&address, p, sizeof(struct sockaddr)); } else { ESP_LOGE(TAG, "Unsupported protocol family %d", address_info->ai_family); close(*fd); diff --git a/components/esp_modem/test/host_test/main/LoopbackTerm.cpp b/components/esp_modem/test/host_test/main/LoopbackTerm.cpp index 935c634fa8..0dea11e109 100644 --- a/components/esp_modem/test/host_test/main/LoopbackTerm.cpp +++ b/components/esp_modem/test/host_test/main/LoopbackTerm.cpp @@ -26,7 +26,7 @@ int LoopbackTerm::write(uint8_t *data, size_t len) async_results.push_back(std::move(ret)); return len; } - if (len > 2 && (data[len - 1] == '\r' || data[len - 1] == '+') ) { // Simple AT responder + if (len > 2 && (data[len - 1] == '\r' || data[len - 1] == '+')) { // Simple AT responder std::string command((char *)data, len); std::string response; if (command == "+++") { diff --git a/components/esp_modem/test/target/main/pppd_test.cpp b/components/esp_modem/test/target/main/pppd_test.cpp index cd9bfb8bd9..302eba544f 100644 --- a/components/esp_modem/test/target/main/pppd_test.cpp +++ b/components/esp_modem/test/target/main/pppd_test.cpp @@ -131,7 +131,7 @@ extern "C" { ESP_LOGE(TAG, "Signal handler %d", nr); } - _sig_func_ptr signal (int nr, _sig_func_ptr) + _sig_func_ptr signal(int nr, _sig_func_ptr) { return handle; } diff --git a/components/esp_modem/test/target_iperf/main/cmd_pppclient.c b/components/esp_modem/test/target_iperf/main/cmd_pppclient.c index 0bb6a89699..13cbce44ce 100644 --- a/components/esp_modem/test/target_iperf/main/cmd_pppclient.c +++ b/components/esp_modem/test/target_iperf/main/cmd_pppclient.c @@ -223,9 +223,9 @@ static int ppp_cmd_iperf(int argc, char **argv) cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp", cfg.flag & IPERF_FLAG_SERVER ? "server" : "client", (uint16_t) cfg.source_ip4 & 0xFF, - (uint16_t) (cfg.source_ip4 >> 8) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 16) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 24) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 8) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 16) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 24) & 0xFF, cfg.sport, cfg.destination_ip4 & 0xFF, (cfg.destination_ip4 >> 8) & 0xFF, (cfg.destination_ip4 >> 16) & 0xFF, (cfg.destination_ip4 >> 24) & 0xFF, cfg.dport, diff --git a/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp b/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp index 160172b27f..c7a98be6f5 100644 --- a/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp +++ b/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp @@ -226,7 +226,7 @@ int TlsTransport::preread(size_t len, int timeout_ms) while (len != read_len) { int l = esp_transport_read(transport_, buf.data() + read_len, len - read_len, timeout_ms); ESP_LOGD(TAG, "need %d read %d already %d", len, l, read_len); - if ((l == ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN || l == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT ) && read_len > 0) { + if ((l == ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN || l == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) && read_len > 0) { return read_len; } if (l <= 0) { diff --git a/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp b/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp index 3bdf35640d..c60f4e8c58 100644 --- a/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp +++ b/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp @@ -85,6 +85,6 @@ extern "C" void app_main(void) MyClient client{broker, credentials, config}; while (true) { constexpr TickType_t xDelay = 500 / portTICK_PERIOD_MS; - vTaskDelay( xDelay ); + vTaskDelay(xDelay); } } diff --git a/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp b/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp index c71e6b0b4a..1e21a9b4cd 100644 --- a/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp +++ b/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp @@ -39,11 +39,11 @@ class SecureLink: public Tls { int recv_timeout(unsigned char *buf, size_t len, int timeout) override { struct timeval tv { - timeout / 1000, (timeout % 1000 ) * 1000 + timeout / 1000, (timeout % 1000) * 1000 }; fd_set read_fds; - FD_ZERO( &read_fds ); - FD_SET( sock, &read_fds ); + FD_ZERO(&read_fds); + FD_SET(sock, &read_fds); int ret = select(sock + 1, &read_fds, nullptr, nullptr, timeout == 0 ? nullptr : &tv); if (ret == 0) { diff --git a/components/mbedtls_cxx/mbedtls_wrap.cpp b/components/mbedtls_cxx/mbedtls_wrap.cpp index c486191592..ce5f4b27a7 100644 --- a/components/mbedtls_cxx/mbedtls_wrap.cpp +++ b/components/mbedtls_cxx/mbedtls_wrap.cpp @@ -93,8 +93,8 @@ int Tls::handshake() int ret = 0; mbedtls_ssl_set_bio(&ssl_, this, bio_write, bio_read, is_dtls_ ? bio_read_tout : nullptr); - while ( ( ret = mbedtls_ssl_handshake( &ssl_ ) ) != 0 ) { - if ( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { + while ((ret = mbedtls_ssl_handshake(&ssl_)) != 0) { + if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { #if CONFIG_MBEDTLS_SSL_PROTO_DTLS if (is_server_ && is_dtls_ && ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { // hello verification requested -> restart the session with this client_id @@ -104,7 +104,7 @@ int Tls::handshake() continue; } #endif // MBEDTLS_SSL_PROTO_DTLS - print_error( "mbedtls_ssl_handshake returned", ret ); + print_error("mbedtls_ssl_handshake returned", ret); return -1; } delay(); @@ -132,12 +132,12 @@ int Tls::bio_read_tout(void *ctx, unsigned char *buf, size_t len, uint32_t timeo int Tls::write(const unsigned char *buf, size_t len) { - return mbedtls_ssl_write( &ssl_, buf, len ); + return mbedtls_ssl_write(&ssl_, buf, len); } int Tls::read(unsigned char *buf, size_t len) { - return mbedtls_ssl_read( &ssl_, buf, len ); + return mbedtls_ssl_read(&ssl_, buf, len); } bool Tls::set_own_cert(const_buf crt, const_buf key) diff --git a/components/mdns/tests/test_afl_fuzz_host/mdns_di.h b/components/mdns/tests/test_afl_fuzz_host/mdns_di.h index ed56960f47..2e9c848b05 100644 --- a/components/mdns/tests/test_afl_fuzz_host/mdns_di.h +++ b/components/mdns/tests/test_afl_fuzz_host/mdns_di.h @@ -11,18 +11,18 @@ #include "mdns.h" #include "mdns_private.h" -void (*mdns_test_static_execute_action)(mdns_action_t *) = NULL; +void (*mdns_test_static_execute_action)(mdns_action_t *) = NULL; mdns_srv_item_t *(*mdns_test_static_mdns_get_service_item)(const char *service, const char *proto, const char *hostname) = NULL; mdns_search_once_t *(*mdns_test_static_search_init)(const char *name, const char *service, const char *proto, uint16_t type, bool unicast, - uint32_t timeout, uint8_t max_results, - mdns_query_notify_t notifier) = NULL; -esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t *search) = NULL; -void (*mdns_test_static_search_free)(mdns_search_once_t *search) = NULL; + uint32_t timeout, uint8_t max_results, + mdns_query_notify_t notifier) = NULL; +esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t *search) = NULL; +void (*mdns_test_static_search_free)(mdns_search_once_t *search) = NULL; static void _mdns_execute_action(mdns_action_t *action); static mdns_srv_item_t *_mdns_get_service_item(const char *service, const char *proto, const char *hostname); static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast, - uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier); + uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier); static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t *search); static void _mdns_search_free(mdns_search_once_t *search); diff --git a/components/mosquitto/port/signals.c b/components/mosquitto/port/signals.c index f1559b51aa..c7999aad13 100644 --- a/components/mosquitto/port/signals.c +++ b/components/mosquitto/port/signals.c @@ -7,7 +7,7 @@ */ #include "signal.h" -int sigprocmask (int, const sigset_t *, sigset_t *) +int sigprocmask(int, const sigset_t *, sigset_t *) { return 0; } diff --git a/components/net_connect/addr_from_stdin.c b/components/net_connect/addr_from_stdin.c index 91a613a1a5..9aac72d7b5 100644 --- a/components/net_connect/addr_from_stdin.c +++ b/components/net_connect/addr_from_stdin.c @@ -32,20 +32,20 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad do { fgets(host_ip, HOST_IP_SIZE, stdin); len = strlen(host_ip); - } while (len<=1 && host_ip[0] == '\n'); + } while (len <= 1 && host_ip[0] == '\n'); host_ip[len - 1] = '\0'; struct addrinfo hints, *addr_list, *cur; - memset( &hints, 0, sizeof( hints ) ); + memset(&hints, 0, sizeof(hints)); // run getaddrinfo() to decide on the IP protocol hints.ai_family = AF_UNSPEC; hints.ai_socktype = sock_type; hints.ai_protocol = IPPROTO_TCP; - if( getaddrinfo( host_ip, NULL, &hints, &addr_list ) != 0 ) { + if (getaddrinfo(host_ip, NULL, &hints, &addr_list) != 0) { return ESP_FAIL; } - for( cur = addr_list; cur != NULL; cur = cur->ai_next ) { + for (cur = addr_list; cur != NULL; cur = cur->ai_next) { memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr)); #if CONFIG_NET_CONNECT_CONNECT_IPV4 if (cur->ai_family == AF_INET) { @@ -53,7 +53,7 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad *addr_family = AF_INET; // add port number and return on first IPv4 match ((struct sockaddr_in*)dest_addr)->sin_port = htons(port); - freeaddrinfo( addr_list ); + freeaddrinfo(addr_list); return ESP_OK; } #endif // IPV4 @@ -64,12 +64,12 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad // add port and interface number and return on first IPv6 match ((struct sockaddr_in6*)dest_addr)->sin6_port = htons(port); ((struct sockaddr_in6*)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(NET_CONNECT_INTERFACE); - freeaddrinfo( addr_list ); + freeaddrinfo(addr_list); return ESP_OK; } #endif // IPV6 } // no match found - freeaddrinfo( addr_list ); + freeaddrinfo(addr_list); return ESP_FAIL; } diff --git a/components/net_connect/console_cmd.c b/components/net_connect/console_cmd.c index 1c20e5f567..094121b978 100644 --- a/components/net_connect/console_cmd.c +++ b/components/net_connect/console_cmd.c @@ -74,7 +74,7 @@ void net_register_wifi_connect_commands(void) .func = &cmd_do_wifi_connect, .argtable = &connect_args }; - ESP_ERROR_CHECK( esp_console_cmd_register(&wifi_connect_cmd) ); + ESP_ERROR_CHECK(esp_console_cmd_register(&wifi_connect_cmd)); const esp_console_cmd_t wifi_disconnect_cmd = { @@ -83,5 +83,5 @@ void net_register_wifi_connect_commands(void) .hint = NULL, .func = &cmd_do_wifi_disconnect, }; - ESP_ERROR_CHECK( esp_console_cmd_register(&wifi_disconnect_cmd) ); + ESP_ERROR_CHECK(esp_console_cmd_register(&wifi_disconnect_cmd)); } diff --git a/components/net_connect/eth_connect.c b/components/net_connect/eth_connect.c index 52cdda37b1..026ea555d7 100644 --- a/components/net_connect/eth_connect.c +++ b/components/net_connect/eth_connect.c @@ -30,7 +30,7 @@ static void eth_stop(void); /** Event handler for Ethernet events */ static void eth_on_got_ip(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_ETH, event->esp_netif)) { @@ -43,7 +43,7 @@ static void eth_on_got_ip(void *arg, esp_event_base_t event_base, #if CONFIG_NET_CONNECT_CONNECT_IPV6 static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_ETH, event->esp_netif)) { diff --git a/components/net_connect/idf_component.yml b/components/net_connect/idf_component.yml index 200f6936ff..69fbd24b5e 100644 --- a/components/net_connect/idf_component.yml +++ b/components/net_connect/idf_component.yml @@ -4,4 +4,3 @@ description: Network connection component providing WiFi, Ethernet, Thread, and dependencies: idf: version: '>=5.0' - diff --git a/components/net_connect/include/net_connect.h b/components/net_connect/include/net_connect.h index 364018d215..f11087dc60 100644 --- a/components/net_connect/include/net_connect.h +++ b/components/net_connect/include/net_connect.h @@ -143,7 +143,10 @@ esp_eth_handle_t net_get_eth_handle(void); #endif // CONFIG_NET_CONNECT_ETHERNET #else -static inline esp_err_t net_connect(void) {return ESP_OK;} +static inline esp_err_t net_connect(void) +{ + return ESP_OK; +} #endif // !CONFIG_IDF_TARGET_LINUX #ifdef __cplusplus diff --git a/components/net_connect/ppp_connect.c b/components/net_connect/ppp_connect.c index 84e79d7431..5b68ecb1e9 100644 --- a/components/net_connect/ppp_connect.c +++ b/components/net_connect/ppp_connect.c @@ -59,8 +59,8 @@ static esp_err_t transmit(void *h, void *buffer, size_t len) } static esp_netif_driver_ifconfig_t driver_cfg = { - .handle = (void *)1, // singleton driver, just to != NULL - .transmit = transmit, + .handle = (void *)1, // singleton driver, just to != NULL + .transmit = transmit, }; const esp_netif_driver_ifconfig_t *ppp_driver_cfg = &driver_cfg; @@ -180,29 +180,29 @@ esp_err_t net_connect_ppp_connect(void) #if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB ESP_LOGI(TAG, "USB initialization"); const tinyusb_config_t tusb_cfg = { - .device_descriptor = NULL, - .string_descriptor = NULL, - .external_phy = false, - .configuration_descriptor = NULL, + .device_descriptor = NULL, + .string_descriptor = NULL, + .external_phy = false, + .configuration_descriptor = NULL, }; ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); tinyusb_config_cdcacm_t acm_cfg = { - .usb_dev = TINYUSB_USBDEV_0, - .cdc_port = TINYUSB_CDC_ACM_0, - .callback_rx = &cdc_rx_callback, - .callback_rx_wanted_char = NULL, - .callback_line_state_changed = NULL, - .callback_line_coding_changed = NULL + .usb_dev = TINYUSB_USBDEV_0, + .cdc_port = TINYUSB_CDC_ACM_0, + .callback_rx = &cdc_rx_callback, + .callback_rx_wanted_char = NULL, + .callback_line_state_changed = NULL, + .callback_line_coding_changed = NULL }; ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); /* the second way to register a callback */ ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( - TINYUSB_CDC_ACM_0, - CDC_EVENT_LINE_STATE_CHANGED, - &line_state_changed)); + TINYUSB_CDC_ACM_0, + CDC_EVENT_LINE_STATE_CHANGED, + &line_state_changed)); #endif // CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB s_event_group = xEventGroupCreate(); @@ -212,9 +212,9 @@ esp_err_t net_connect_ppp_connect(void) esp_netif_inherent_config_t base_netif_cfg = ESP_NETIF_INHERENT_DEFAULT_PPP(); base_netif_cfg.if_desc = NET_CONNECT_NETIF_DESC_PPP; esp_netif_config_t netif_ppp_config = { .base = &base_netif_cfg, - .driver = ppp_driver_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_PPP - }; + .driver = ppp_driver_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_PPP + }; s_netif = esp_netif_new(&netif_ppp_config); assert(s_netif); diff --git a/components/net_connect/protocol_examples_utils.c b/components/net_connect/protocol_examples_utils.c index 361623735e..09296fc131 100644 --- a/components/net_connect/protocol_examples_utils.c +++ b/components/net_connect/protocol_examples_utils.c @@ -274,14 +274,14 @@ void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type case sw_quoted: if (ch >= '0' && ch <= '9') { - decoded = (u_char) (ch - '0'); + decoded = (u_char)(ch - '0'); state = sw_quoted_second; break; } - c = (u_char) (ch | 0x20); + c = (u_char)(ch | 0x20); if (c >= 'a' && c <= 'f') { - decoded = (u_char) (c - 'a' + 10); + decoded = (u_char)(c - 'a' + 10); state = sw_quoted_second; break; } @@ -299,7 +299,7 @@ void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type state = sw_usual; if (ch >= '0' && ch <= '9') { - ch = (u_char) ((decoded << 4) + (ch - '0')); + ch = (u_char)((decoded << 4) + (ch - '0')); if (type & NGX_UNESCAPE_REDIRECT) { if (ch > '%' && ch < 0x7f) { @@ -317,9 +317,9 @@ void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type break; } - c = (u_char) (ch | 0x20); + c = (u_char)(ch | 0x20); if (c >= 'a' && c <= 'f') { - ch = (u_char) ((decoded << 4) + (c - 'a') + 10); + ch = (u_char)((decoded << 4) + (c - 'a') + 10); if (type & NGX_UNESCAPE_URI) { if (ch == '?') { diff --git a/components/net_connect/stdin_out.c b/components/net_connect/stdin_out.c index daa6b1e2c7..9049802404 100644 --- a/components/net_connect/stdin_out.c +++ b/components/net_connect/stdin_out.c @@ -13,13 +13,13 @@ esp_err_t example_configure_stdin_stdout(void) { if (uart_is_driver_installed((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM)) { - return ESP_OK; + return ESP_OK; } // Initialize VFS & UART so we can use std::cout/cin setvbuf(stdin, NULL, _IONBF, 0); /* Install UART driver for interrupt-driven reads and writes */ - ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, - 256, 0, 0, NULL, 0) ); + ESP_ERROR_CHECK(uart_driver_install((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, + 256, 0, 0, NULL, 0)); /* Tell VFS to use UART driver */ uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); diff --git a/components/net_connect/wifi_connect.c b/components/net_connect/wifi_connect.c index 4e933e41e3..16cf486f4f 100644 --- a/components/net_connect/wifi_connect.c +++ b/components/net_connect/wifi_connect.c @@ -21,7 +21,7 @@ static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; static int s_retry_num = 0; static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { s_retry_num++; if (s_retry_num > CONFIG_NET_CONNECT_WIFI_CONN_MAX_RETRY) { @@ -52,7 +52,7 @@ static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event } static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { #if CONFIG_NET_CONNECT_CONNECT_IPV6 esp_netif_create_ip6_linklocal(esp_netif); @@ -60,7 +60,7 @@ static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t ev } static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { s_retry_num = 0; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; @@ -77,7 +77,7 @@ static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base #if CONFIG_NET_CONNECT_CONNECT_IPV6 static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_STA, event->esp_netif)) { @@ -214,11 +214,11 @@ esp_err_t net_connect_wifi_connect(void) }; #if CONFIG_NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN net_configure_stdin_stdout(); - char buf[sizeof(wifi_config.sta.ssid)+sizeof(wifi_config.sta.password)+2] = {0}; + char buf[sizeof(wifi_config.sta.ssid) + sizeof(wifi_config.sta.password) + 2] = {0}; ESP_LOGI(TAG, "Please input ssid password:"); fgets(buf, sizeof(buf), stdin); int len = strlen(buf); - buf[len-1] = '\0'; /* removes '\n' */ + buf[len - 1] = '\0'; /* removes '\n' */ memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid)); char *rest = NULL; diff --git a/examples/esp_netif/multiple_netifs/main/check_connection.c b/examples/esp_netif/multiple_netifs/main/check_connection.c index 64c2e21cac..a020b9dd9b 100644 --- a/examples/esp_netif/multiple_netifs/main/check_connection.c +++ b/examples/esp_netif/multiple_netifs/main/check_connection.c @@ -83,10 +83,10 @@ esp_err_t check_connectivity(const char *host) return ESP_ERR_NOT_FOUND; } if (res->ai_family == AF_INET) { - struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; + struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr; inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); } else { - struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr; + struct in6_addr addr6 = ((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr; inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6); } freeaddrinfo(res); diff --git a/examples/esp_netif/slip_custom_netif/main/slip_client_main.c b/examples/esp_netif/slip_custom_netif/main/slip_client_main.c index 56c2a87ce1..010a3309d3 100644 --- a/examples/esp_netif/slip_custom_netif/main/slip_client_main.c +++ b/examples/esp_netif/slip_custom_netif/main/slip_client_main.c @@ -169,7 +169,7 @@ static bool slip_rx_filter(slip_modem_handle slip, uint8_t *data, uint32_t len) #if CONFIG_EXAMPLE_IPV4 static const esp_netif_ip_info_t s_slip_ip4 = { - .ip = { .addr = ESP_IP4TOADDR( 10, 0, 0, 2) }, + .ip = { .addr = ESP_IP4TOADDR(10, 0, 0, 2) }, }; #endif