fix(common): Improve linux port

This commit is contained in:
David Cermak
2023-04-14 15:57:12 +02:00
parent 588465d9db
commit 7d4755f119
10 changed files with 91 additions and 64 deletions

View File

@ -1,19 +1,21 @@
# The following four lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
# This project serves as a demo to enable using esp-mqtt on ESP platform targets as well as on linux
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.
# For ESP32 platform target
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
if(${IDF_TARGET} STREQUAL "linux")
# For linux-target we have two options:
# - With lwIP (must be defined on command line, e.g. idf.py -DWITH_LWIP=1)
# access networking from linux `tap` interface (TAP networking mode)
# - Without lwIP (must be defined on command line, e.g. idf.py -DWITH_LWIP=0)
# no designated interface, accesses user network via linux/socket sys calls
if(WITH_LWIP STREQUAL 1)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_tapif_io
"../../common_components/linux_compat/esp_timer")
set(COMPONENTS main esp_netif lwip protocol_examples_tapif_io startup esp_hw_support esp_system nvs_flash mqtt esp_timer)
else()
list(APPEND EXTRA_COMPONENT_DIRS
"../../common_components/linux_compat/freertos"
"../../common_components/linux_compat/esp_timer"
"$ENV{IDF_PATH}/examples/protocols/linux_stubs/esp_stubs")
set(COMPONENTS main nvs_flash esp-tls esp_stubs mqtt protocol_examples_common esp_timer)
@ -21,4 +23,4 @@ if(${IDF_TARGET} STREQUAL "linux")
endif()
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mqtt_tcp)
project(esp_mqtt_demo)

41
examples/mqtt/README.md Normal file
View File

@ -0,0 +1,41 @@
# MQTT demo application that runs on linux
## Overview
This is a simple example demonstrating connecting to an MQTT broker, subscribing and publishing some data.
This example uses IDF build system and could be configured to be build and executed:
* for any ESP32 family chip
* for linux target
## How to use example
### Hardware Required
To run this example, you need any ESP32 development board or just PC/virtual machine/container running linux operating system.
### Host build modes
Linux build is supported in these two modes:
* `WITH_LWIP=0`: Without lwIP component. The project uses linux BSD socket interface to interact with TCP/IP stack. There's no connection phase, we use the host network as users. This mode is often referred to as user-side networking.
* `WITH_LWIP=1`: Including lwIP component, which is added to the list of required components and compiled on host. In this mode, we have to map the host network (linux TCP/IP stack) to the target network (lwip). We use IDF's [`tapif_io`](https://github.com/espressif/esp-idf/tree/master/examples/common_components/protocol_examples_tapif_io) component to create a network interface, which will be used to pass packets to and from the simulated target. Please refer to the [README](https://github.com/espressif/esp-idf/tree/master/examples/common_components/protocol_examples_tapif_io#readme) for more details about the host side networking.
### Building on linux
1) Configure linux target
```bash
idf.py --preview set-target linux
```
2) Build the project with preferred components (with or without lwip)
```bash
idf.py -DWITH_LWIP=0 build # Building without lwip (user networking)
idf.py -DWITH_LWIP=1 build # Building with lwip (TAP networking)
```
3) Run the project
It is possible to run the project elf file directly, or using `idf.py` monitor target (no need to flash):
```bash
idf.py monitor
```
idf.py -DWITH_LWIP=0 build # Building without lwip (user networking)

View File

@ -2,9 +2,3 @@ idf_component_register(SRCS "app_main.cpp"
INCLUDE_DIRS ".")
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
if(${IDF_TARGET} STREQUAL "linux")
if(WITH_LWIP STREQUAL "1")
target_compile_definitions(${COMPONENT_LIB} PUBLIC WITH_LWIP)
endif()
endif()

View File

@ -6,8 +6,4 @@ menu "Example Configuration"
help
URL of the broker to connect to
config BROKER_URL_FROM_STDIN
bool
default y if BROKER_URL = "FROM_STDIN"
endmenu

View File

@ -12,15 +12,13 @@
#include "esp_event.h"
#include "esp_netif.h"
#include "protocol_examples_common.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "esp_log.h"
#include "mqtt_client.h"
static const char *TAG = "MQTT_EXAMPLE";
static const char *TAG = "esp_mqtt_demo";
static void log_error_if_nonzero(const char *message, int error_code)
@ -109,8 +107,6 @@ static void mqtt_app_start(void)
esp_mqtt_client_start(client);
}
#include "esp_netif.h"
#include "netdb.h"
extern "C" void app_main(void)
{
@ -120,10 +116,10 @@ extern "C" void app_main(void)
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE);
esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
esp_log_level_set("esp_mqtt_demo", ESP_LOG_VERBOSE);
esp_log_level_set("transport_base", ESP_LOG_VERBOSE);
esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
esp_log_level_set("transport", ESP_LOG_VERBOSE);
esp_log_level_set("outbox", ESP_LOG_VERBOSE);
ESP_ERROR_CHECK(nvs_flash_init());
@ -135,13 +131,6 @@ extern "C" void app_main(void)
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
#if CONFIG_IDF_TARGET_LINUX && WITH_LWIP
esp_netif_dns_info_t dns;
dns.ip.u_addr.ip4.addr = ipaddr_addr("8.8.8.8");
dns.ip.type = IPADDR_TYPE_V4;
ESP_ERROR_CHECK(esp_netif_set_dns_info(esp_netif_get_handle_from_ifkey("TAP"), ESP_NETIF_DNS_MAIN, &dns));
#endif
mqtt_app_start();
vTaskDelay(pdMS_TO_TICKS(1000));
}