diff --git a/components/mdns/tests/host_test/components/esp_timer_linux/CMakeLists.txt b/common_components/linux_compat/esp_timer/CMakeLists.txt similarity index 100% rename from components/mdns/tests/host_test/components/esp_timer_linux/CMakeLists.txt rename to common_components/linux_compat/esp_timer/CMakeLists.txt diff --git a/components/mdns/tests/host_test/components/esp_timer_linux/esp_timer_linux.c b/common_components/linux_compat/esp_timer/esp_timer_linux.c similarity index 82% rename from components/mdns/tests/host_test/components/esp_timer_linux/esp_timer_linux.c rename to common_components/linux_compat/esp_timer/esp_timer_linux.c index 2131e4141..e1886890f 100644 --- a/components/mdns/tests/host_test/components/esp_timer_linux/esp_timer_linux.c +++ b/common_components/linux_compat/esp_timer/esp_timer_linux.c @@ -7,6 +7,7 @@ #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include 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); 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; +} diff --git a/components/mdns/tests/host_test/components/esp_timer_linux/include/esp_timer.h b/common_components/linux_compat/esp_timer/include/esp_timer.h similarity index 96% rename from components/mdns/tests/host_test/components/esp_timer_linux/include/esp_timer.h rename to common_components/linux_compat/esp_timer/include/esp_timer.h index decf7634b..aa27c9d93 100644 --- a/components/mdns/tests/host_test/components/esp_timer_linux/include/esp_timer.h +++ b/common_components/linux_compat/esp_timer/include/esp_timer.h @@ -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_delete(esp_timer_handle_t timer); + +int64_t esp_timer_get_time(void); diff --git a/components/mdns/tests/host_test/components/esp_timer_linux/timer_task.cpp b/common_components/linux_compat/esp_timer/timer_task.cpp similarity index 100% rename from components/mdns/tests/host_test/components/esp_timer_linux/timer_task.cpp rename to common_components/linux_compat/esp_timer/timer_task.cpp diff --git a/components/mdns/tests/host_test/components/esp_timer_linux/timer_task.hpp b/common_components/linux_compat/esp_timer/timer_task.hpp similarity index 100% rename from components/mdns/tests/host_test/components/esp_timer_linux/timer_task.hpp rename to common_components/linux_compat/esp_timer/timer_task.hpp diff --git a/common_components/linux_compat/freertos/include/freertos/task.h b/common_components/linux_compat/freertos/include/freertos/task.h index ee2b8d859..5fb518466 100644 --- a/common_components/linux_compat/freertos/include/freertos/task.h +++ b/common_components/linux_compat/freertos/include/freertos/task.h @@ -7,6 +7,10 @@ #include "freertos/FreeRTOS.h" +#ifdef __cplusplus +extern "C" { +#endif + #define TaskHandle_t TaskHandle_t #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) ) @@ -73,3 +77,7 @@ EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ); uint32_t xQueueSendToBack(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait ); + +#ifdef __cplusplus +} +#endif //__cplusplus diff --git a/components/mdns/CMakeLists.txt b/components/mdns/CMakeLists.txt index f385c65c8..7551f38bf 100644 --- a/components/mdns/CMakeLists.txt +++ b/components/mdns/CMakeLists.txt @@ -6,7 +6,7 @@ endif() idf_build_get_property(target IDF_TARGET) 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}) else() set(dependencies lwip console esp_netif) diff --git a/components/mdns/tests/host_test/CMakeLists.txt b/components/mdns/tests/host_test/CMakeLists.txt index ec230d2f9..79ac7b3ba 100644 --- a/components/mdns/tests/host_test/CMakeLists.txt +++ b/components/mdns/tests/host_test/CMakeLists.txt @@ -1,6 +1,6 @@ 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) set(COMPONENTS main esp_netif_linux) diff --git a/components/mdns/tests/unit_test/pytest_mdns.py b/components/mdns/tests/unit_test/pytest_app_mdns.py similarity index 100% rename from components/mdns/tests/unit_test/pytest_mdns.py rename to components/mdns/tests/unit_test/pytest_app_mdns.py diff --git a/examples/mqtt/CMakeLists.txt b/examples/mqtt/CMakeLists.txt index 0fbb53986..522cb199e 100644 --- a/examples/mqtt/CMakeLists.txt +++ b/examples/mqtt/CMakeLists.txt @@ -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) diff --git a/examples/mqtt/main/CMakeLists.txt b/examples/mqtt/main/CMakeLists.txt index 04fc1ba97..eafc10f15 100644 --- a/examples/mqtt/main/CMakeLists.txt +++ b/examples/mqtt/main/CMakeLists.txt @@ -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() diff --git a/examples/mqtt/main/app_main.c b/examples/mqtt/main/app_main.cpp similarity index 86% rename from examples/mqtt/main/app_main.c rename to examples/mqtt/main/app_main.cpp index cce974159..fe6a6b37e 100644 --- a/examples/mqtt/main/app_main.c +++ b/examples/mqtt/main/app_main.cpp @@ -3,7 +3,6 @@ * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ - #include #include #include @@ -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)); }