feat(examples): Add support for lwip build under linux

This commit is contained in:
David Cermak
2023-04-04 12:57:38 +02:00
parent c443326a34
commit 588465d9db
12 changed files with 56 additions and 21 deletions

View File

@ -7,11 +7,18 @@ cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
if(${IDF_TARGET} STREQUAL "linux")
list(APPEND EXTRA_COMPONENT_DIRS "../../common_components/linux_compat/freertos"
"$ENV{IDF_PATH}/examples/protocols/linux_stubs/esp_stubs")
set(COMPONENTS main nvs_flash)
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)
endif()
endif()
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mqtt_tcp)

View File

@ -1,8 +1,10 @@
set(requires "")
if(${IDF_TARGET} STREQUAL "linux")
list(APPEND requires esp_stubs esp-tls protocol_examples_common mqtt)
endif()
idf_component_register(SRCS "app_main.c"
INCLUDE_DIRS "."
REQUIRES ${requires})
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

@ -3,7 +3,6 @@
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
@ -44,7 +43,7 @@ static void log_error_if_nonzero(const char *message, int error_code)
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
esp_mqtt_event_handle_t event = event_data;
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
esp_mqtt_client_handle_t client = event->client;
int msg_id;
switch ((esp_mqtt_event_id_t)event_id) {
@ -100,17 +99,20 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
static void mqtt_app_start(void)
{
esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = CONFIG_BROKER_URL,
};
esp_mqtt_client_config_t mqtt_cfg = {};
mqtt_cfg.broker.address.uri = CONFIG_BROKER_URL;
mqtt_cfg.credentials.client_id = "idf_on_linux_client";
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_register_event(client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_start(client);
}
void app_main(void)
#include "esp_netif.h"
#include "netdb.h"
extern "C" void app_main(void)
{
ESP_LOGI(TAG, "[APP] Startup..");
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
@ -133,7 +135,13 @@ 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(100000));
vTaskDelay(pdMS_TO_TICKS(1000));
}