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,6 +7,7 @@
#include "esp_timer.h" #include "esp_timer.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include <pthread.h>
void *create_tt(esp_timer_cb_t cb); void *create_tt(esp_timer_cb_t cb);
@ -37,3 +38,10 @@ esp_err_t esp_timer_delete(esp_timer_handle_t timer)
destroy_tt(timer); destroy_tt(timer);
return ESP_OK; return ESP_OK;
} }
int64_t esp_timer_get_time(void)
{
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return spec.tv_nsec / 1000 + spec.tv_sec * 1000000;
}

View File

@ -32,3 +32,5 @@ esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);
esp_err_t esp_timer_stop(esp_timer_handle_t timer); esp_err_t esp_timer_stop(esp_timer_handle_t timer);
esp_err_t esp_timer_delete(esp_timer_handle_t timer); esp_err_t esp_timer_delete(esp_timer_handle_t timer);
int64_t esp_timer_get_time(void);

View File

@ -7,6 +7,10 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#ifdef __cplusplus
extern "C" {
#endif
#define TaskHandle_t TaskHandle_t #define TaskHandle_t TaskHandle_t
#define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) ) #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )
@ -73,3 +77,7 @@ EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
const EventBits_t uxBitsToSet ); const EventBits_t uxBitsToSet );
uint32_t xQueueSendToBack(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait ); uint32_t xQueueSendToBack(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait );
#ifdef __cplusplus
}
#endif //__cplusplus

View File

@ -6,7 +6,7 @@ endif()
idf_build_get_property(target IDF_TARGET) idf_build_get_property(target IDF_TARGET)
if(${target} STREQUAL "linux") if(${target} STREQUAL "linux")
set(dependencies esp_netif_linux esp_timer_linux esp_system) set(dependencies esp_netif_linux esp_timer esp_system)
set(srcs "mdns.c" ${MDNS_NETWORKING}) set(srcs "mdns.c" ${MDNS_NETWORKING})
else() else()
set(dependencies lwip console esp_netif) set(dependencies lwip console esp_netif)

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS "../.. ../../../../common_components/linux_compat/freertos") set(EXTRA_COMPONENT_DIRS "../.." "../../../../common_components/linux_compat")
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main esp_netif_linux) set(COMPONENTS main esp_netif_linux)

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) set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
if(${IDF_TARGET} STREQUAL "linux") if(${IDF_TARGET} STREQUAL "linux")
list(APPEND EXTRA_COMPONENT_DIRS "../../common_components/linux_compat/freertos" if(WITH_LWIP STREQUAL 1)
"$ENV{IDF_PATH}/examples/protocols/linux_stubs/esp_stubs") set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_tapif_io
set(COMPONENTS main nvs_flash) "../../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() endif()
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mqtt_tcp) project(mqtt_tcp)

View File

@ -1,8 +1,10 @@
set(requires "") idf_component_register(SRCS "app_main.cpp"
if(${IDF_TARGET} STREQUAL "linux") INCLUDE_DIRS ".")
list(APPEND requires esp_stubs esp-tls protocol_examples_common mqtt)
endif()
idf_component_register(SRCS "app_main.c"
INCLUDE_DIRS "."
REQUIRES ${requires})
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") 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 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.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) 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_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; esp_mqtt_client_handle_t client = event->client;
int msg_id; int msg_id;
switch ((esp_mqtt_event_id_t)event_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) static void mqtt_app_start(void)
{ {
esp_mqtt_client_config_t mqtt_cfg = { esp_mqtt_client_config_t mqtt_cfg = {};
.broker.address.uri = CONFIG_BROKER_URL, 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); 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 */ /* 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); 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] Startup..");
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); 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. * examples/protocols/README.md for more information about this function.
*/ */
ESP_ERROR_CHECK(example_connect()); 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(); mqtt_app_start();
vTaskDelay(pdMS_TO_TICKS(100000)); vTaskDelay(pdMS_TO_TICKS(1000));
} }