diff --git a/CMakeLists.txt b/CMakeLists.txt index c0a4b86..817a7dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -set(srcs mqtt_client.c lib/mqtt_msg.c lib/platform_esp32_idf.c) +set(srcs mqtt_client.c lib/mqtt_msg.c lib/platform.c) if(CONFIG_MQTT_PROTOCOL_5) list(APPEND srcs lib/mqtt5_msg.c mqtt5_client.c) diff --git a/lib/include/platform.h b/lib/include/platform.h index c647a4b..bc9e3da 100644 --- a/lib/include/platform.h +++ b/lib/include/platform.h @@ -11,9 +11,20 @@ #ifndef _PLATFORM_H__ #define _PLATFORM_H__ -//Support ESP32 -#ifdef ESP_PLATFORM -#include "platform_esp32_idf.h" -#endif +#include + +char *platform_create_id_string(void); +int platform_random(int max); +uint64_t platform_tick_get_ms(void); + +#define ESP_MEM_CHECK(TAG, a, action) if (!(a)) { \ + ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \ + action; \ + } + +#define ESP_OK_CHECK(TAG, a, action) if ((a) != ESP_OK) { \ + ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Failed"); \ + action; \ + } #endif diff --git a/lib/include/platform_esp32_idf.h b/lib/include/platform_esp32_idf.h deleted file mode 100644 index 9623afe..0000000 --- a/lib/include/platform_esp32_idf.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -/* - * This file is subject to the terms and conditions defined in - * file 'LICENSE', which is part of this source code package. - * Tuan PM - */ -#ifndef _ESP_PLATFORM_H__ -#define _ESP_PLATFORM_H__ - -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" - -#include -#include - -char *platform_create_id_string(void); -int platform_random(int max); -uint64_t platform_tick_get_ms(void); - -#define ESP_MEM_CHECK(TAG, a, action) if (!(a)) { \ - ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \ - action; \ - } - -#define ESP_OK_CHECK(TAG, a, action) if ((a) != ESP_OK) { \ - ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Failed"); \ - action; \ - } - -#endif diff --git a/lib/platform_esp32_idf.c b/lib/platform.c similarity index 79% rename from lib/platform_esp32_idf.c rename to lib/platform.c index e7401f0..8c38f99 100644 --- a/lib/platform_esp32_idf.c +++ b/lib/platform.c @@ -5,14 +5,18 @@ */ #include "platform.h" -#ifdef ESP_PLATFORM #include "esp_log.h" #include "esp_mac.h" #include "soc/soc_caps.h" -#include "esp_timer.h" #include "esp_random.h" +#if CONFIG_IDF_TARGET_LINUX +#include +#else +#include "esp_timer.h" +#endif #include #include +#include static const char *TAG = "platform"; @@ -25,6 +29,7 @@ static const char *TAG = "platform"; #elif defined SOC_IEEE802154_SUPPORTED #define MAC_TYPE ESP_MAC_IEEE802154 #endif + char *platform_create_id_string(void) { char *id_string = calloc(1, MAX_ID_STRING); @@ -47,7 +52,12 @@ int platform_random(int max) uint64_t platform_tick_get_ms(void) { +#if CONFIG_IDF_TARGET_LINUX + /* IDF linux does not always provide a linked esp_timer implementation. */ + struct timeval tv; + gettimeofday(&tv, NULL); + return (uint64_t)tv.tv_sec * 1000ULL + (uint64_t)(tv.tv_usec / 1000); +#else return esp_timer_get_time() / (int64_t)1000; -} - #endif +}