From 5d04ebab51856fefe9396071f4723d49c48f2d84 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 31 Jan 2023 08:30:21 +0100 Subject: [PATCH] examples: Update socket udp_client to support lwip on linux This uses FreeRTOS simulator and lwip on linux, with options to use tap-io commponent for host network interactions. --- .../sockets/udp_client/CMakeLists.txt | 11 ++++-- .../protocols/sockets/udp_client/README.md | 37 +++++++++++++++++++ .../sockets/udp_client/main/udp_client.c | 4 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/examples/protocols/sockets/udp_client/CMakeLists.txt b/examples/protocols/sockets/udp_client/CMakeLists.txt index ec722eef53c4..b9b38c6afb2d 100644 --- a/examples/protocols/sockets/udp_client/CMakeLists.txt +++ b/examples/protocols/sockets/udp_client/CMakeLists.txt @@ -2,9 +2,14 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +if("${IDF_TARGET}" STREQUAL "linux") + # This example uses an extra component with common functionality for lwip's port on linux target + set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_tapif_io) + set(COMPONENTS main esp_netif protocol_examples_tapif_io startup esp_hw_support esp_system nvs_flash) +else() + # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection on ESP target + set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +endif() include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(udp_client) diff --git a/examples/protocols/sockets/udp_client/README.md b/examples/protocols/sockets/udp_client/README.md index bdcee2210859..cd8b39cf2433 100644 --- a/examples/protocols/sockets/udp_client/README.md +++ b/examples/protocols/sockets/udp_client/README.md @@ -72,3 +72,40 @@ See the Getting Started Guide for full steps to configure and use ESP-IDF to bui ## Troubleshooting Start server first, to receive data sent from the client (application). + +## Running the example for Linux target + +This example could be executed on host system, using lwIP port for linux and FreeRTOS simulator on linux. The socket API used in this example directly calls lwIP implementation. Follow the steps below to configure, build and run the example on linux operating system. + +1. First configure the target (please note that using linux target is currently available only in `preview` stage) +``` +idf.py --preview set-target linux +``` +2. Configure the project +``` +idf.py menuconfig +``` +Choose connection capabilities in `Example Connection Configuration` menu: + +* By default, the `example_connect()` function returns as a no-op, expecting that the connection is already available. This option is preferred when we don't have to interact with outside networking layers and use only lwIP internal interface, such as loopback netif (`lo`). +* If you want to connect lwIP network interface to the host system networking, set `EXAMPLE_CONNECT_LWIP_TAPIF`. + * Configure the interface address information (IP address, GW address and netmask). + * Create a host network interface named `tap0` of *TAP* type. You can use the `./make_tap_netif` script located in the `tapif_io` component directory. + * Optionally set input or output packet loss rate to simulate loosing data of physical interfaces. + * Note about the host side networking: + * This example uses static IP address configured in `tapif_io` component configuration. + * Use the IP ranges that do not overlap with any other IP range of the host system. + * Make sure that the same IP range is configured in `tap0` interface created by tge `./make_tap_netif` script. + * You can leave the defaults in the script and `tapif_io` settings unless any other host network interface uses `192.168.5.x` range. + * Read more about host-side networking in the [`tapif_io` component documentation](../../../common_components/protocol_examples_tapif_io/README.md). + +3. Generate partition table for the application: +``` +idf.by partition-table +``` + +4. Build and run the example the usual way (Note that the flash step is left out) +``` +idf.by build +idf.py monitor +``` diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index 268f6e65daeb..f3e92f9fb9df 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -12,7 +12,6 @@ #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" @@ -23,7 +22,10 @@ #include "lwip/sockets.h" #include "lwip/sys.h" #include + +#ifdef CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN #include "addr_from_stdin.h" +#endif #if defined(CONFIG_EXAMPLE_IPV4) #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR