refactor: Clean up platform file and rename to a more generic name

This commit is contained in:
Euripedes Rocha Filho
2026-08-17 10:35:03 +02:00
parent 69b661e7a4
commit 4eafba1c18
4 changed files with 30 additions and 43 deletions
+1 -1
View File
@@ -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)
+15 -4
View File
@@ -11,9 +11,20 @@
#ifndef _PLATFORM_H__
#define _PLATFORM_H__
//Support ESP32
#ifdef ESP_PLATFORM
#include "platform_esp32_idf.h"
#endif
#include <stdint.h>
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
-34
View File
@@ -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 <tuanpm at live dot com>
*/
#ifndef _ESP_PLATFORM_H__
#define _ESP_PLATFORM_H__
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include <stdint.h>
#include <sys/time.h>
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
+14 -4
View File
@@ -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 <sys/time.h>
#else
#include "esp_timer.h"
#endif
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
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
}