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.
This commit is contained in:
David Cermak 2023-01-31 08:30:21 +01:00
parent 854e16feb3
commit 5d04ebab51
3 changed files with 48 additions and 4 deletions

View File

@ -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)

View File

@ -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
```

View File

@ -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 <lwip/netdb.h>
#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