diff --git a/arduino/libretuya/compat/md5.h b/arduino/libretuya/compat/md5.h new file mode 100644 index 0000000..63b9c8a --- /dev/null +++ b/arduino/libretuya/compat/md5.h @@ -0,0 +1,6 @@ +/* Copyright (c) Kuba Szczodrzyński 2022-06-04. */ + +#pragma once + +// lowercase "md5.h" to allow including actual MD5.h on case-sensitive OSes +#include diff --git a/arduino/libretuya/core/LibreTuyaAPI.h b/arduino/libretuya/core/LibreTuyaAPI.h index 0347729..4d72540 100644 --- a/arduino/libretuya/core/LibreTuyaAPI.h +++ b/arduino/libretuya/core/LibreTuyaAPI.h @@ -15,6 +15,7 @@ #define LT_BOARD_STR STRINGIFY_MACRO(LT_BOARD) // Includes +#include "LibreTuyaCompat.h" #include "LibreTuyaConfig.h" #include @@ -39,10 +40,6 @@ extern "C" { "LibreTuya v" LT_VERSION_STR " on " LT_BOARD_STR ", compiled at " __DATE__ " " __TIME__ \ ) -// ArduinoCore-API doesn't define these anymore -#define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) -#define PGM_VOID_P const void * - void lt_rand_bytes(uint8_t *buf, size_t len); #ifdef __cplusplus diff --git a/arduino/libretuya/core/LibreTuyaCompat.cpp b/arduino/libretuya/core/LibreTuyaCompat.cpp new file mode 100644 index 0000000..203f390 --- /dev/null +++ b/arduino/libretuya/core/LibreTuyaCompat.cpp @@ -0,0 +1,33 @@ +/* Copyright (c) Kuba Szczodrzyński 2022-06-04. */ + +#include "LibreTuyaCompat.h" + +#if LT_HAS_FREERTOS +BaseType_t xTaskCreateUniversal( + TaskFunction_t pxTaskCode, + const char *const pcName, + const uint32_t usStackDepth, + void *const pvParameters, + UBaseType_t uxPriority, + TaskHandle_t *const pxCreatedTask, + const BaseType_t xCoreID +) { + // #ifndef CONFIG_FREERTOS_UNICORE + // if (xCoreID >= 0 && xCoreID < 2) { + // return xTaskCreatePinnedToCore( + // pxTaskCode, + // pcName, + // usStackDepth, + // pvParameters, + // uxPriority, + // pxCreatedTask, + // xCoreID + // ); + // } else { + // #endif + return xTaskCreate(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask); + // #ifndef CONFIG_FREERTOS_UNICORE + // } + // #endif +} +#endif diff --git a/arduino/libretuya/core/LibreTuyaCompat.h b/arduino/libretuya/core/LibreTuyaCompat.h new file mode 100644 index 0000000..564d0a7 --- /dev/null +++ b/arduino/libretuya/core/LibreTuyaCompat.h @@ -0,0 +1,39 @@ +/* Copyright (c) Kuba Szczodrzyński 2022-06-04. */ + +#pragma once + +#include + +#if LT_HAS_FREERTOS +#include +#include +#endif + +// Definitions for error constants. +#define esp_err_t int +#define ESP_OK 0 /*!< esp_err_t value indicating success (no error) */ +#define ESP_FAIL -1 /*!< Generic esp_err_t code indicating failure */ + +// ArduinoCore-API doesn't define these anymore +#define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) +#define PGM_VOID_P const void * +#define vsnprintf_P vsnprintf + +// FreeRTOS utilities +#if LT_HAS_FREERTOS +// if xCoreID < 0 or CPU is unicore, it will use xTaskCreate, else xTaskCreatePinnedToCore +// allows to easily handle all possible situations without repetitive code +BaseType_t xTaskCreateUniversal( + TaskFunction_t pxTaskCode, + const char *const pcName, + const uint32_t usStackDepth, + void *const pvParameters, + UBaseType_t uxPriority, + TaskHandle_t *const pxCreatedTask, + const BaseType_t xCoreID +); +#define xTaskCreatePinnedToCore xTaskCreateUniversal +#endif + +// Default values from sdkconfig.h +#define CONFIG_LWIP_MAX_ACTIVE_TCP 16 diff --git a/arduino/libretuya/core/lt_logger.h b/arduino/libretuya/core/lt_logger.h index 3199da1..9765317 100644 --- a/arduino/libretuya/core/lt_logger.h +++ b/arduino/libretuya/core/lt_logger.h @@ -81,6 +81,8 @@ void lt_log(const uint8_t level, const char *format, ...); #define ESP_EARLY_LOGI(...) LT_I(__VA_ARGS__) #define ESP_EARLY_LOGW(...) LT_W(__VA_ARGS__) #define ESP_EARLY_LOGE(...) LT_E(__VA_ARGS__) +#define ets_printf(...) LT_I(__VA_ARGS__) +#define ETS_PRINTF(...) LT_I(__VA_ARGS__) #define LT_T_MOD(module, ...) \ do { \ diff --git a/arduino/libretuya/libraries/MD5/MD5.h b/arduino/libretuya/libraries/MD5/MD5.h index ea0c98f..593d72a 100644 --- a/arduino/libretuya/libraries/MD5/MD5.h +++ b/arduino/libretuya/libraries/MD5/MD5.h @@ -18,6 +18,9 @@ extern "C" { #define LT_MD5_CTX_T void #endif +// for compatibility with ESP8266 +typedef LT_MD5_CTX_T md5_context_t; + void MD5Init(LT_MD5_CTX_T *context); void MD5Update(LT_MD5_CTX_T *context, const unsigned char *buf, unsigned len); void MD5Final(unsigned char digest[16], LT_MD5_CTX_T *context); diff --git a/builder/frameworks/realtek-ambz-arduino.py b/builder/frameworks/realtek-ambz-arduino.py index 139a34e..825a7ad 100644 --- a/builder/frameworks/realtek-ambz-arduino.py +++ b/builder/frameworks/realtek-ambz-arduino.py @@ -49,11 +49,6 @@ env.Append( # which conflicts with C++ built-in bool # so it's either -fpermissive or this: ("bool", "bool"), - # enable LwIPRxBuffer - ("LT_HAS_LWIP", "1"), - # enable LwIPmDNS - ("LT_HAS_LWIP2", "1"), - ("LT_PRINTF_BROKEN", "1"), # printf does not handle %.3f properly ], LINKFLAGS=[ "--specs=nosys.specs", diff --git a/builder/frameworks/realtek-ambz-sdk.py b/builder/frameworks/realtek-ambz-sdk.py index 7de8817..7a4c347 100644 --- a/builder/frameworks/realtek-ambz-sdk.py +++ b/builder/frameworks/realtek-ambz-sdk.py @@ -30,6 +30,13 @@ env.Append( "-fsigned-char", ], CPPDEFINES=[ + # LibreTuya configuration + ("LT_HAS_LWIP", "1"), + ("LT_HAS_LWIP2", "1"), + ("LT_HAS_FREERTOS", "1"), + ("LT_HAS_MBEDTLS", "1"), + ("LT_PRINTF_BROKEN", "1"), # printf does not handle %.3f properly + # other options "M3", "CONFIG_PLATFORM_8711B", # LwIP options diff --git a/builder/utils.py b/builder/utils.py index 770acfd..b7cd086 100644 --- a/builder/utils.py +++ b/builder/utils.py @@ -51,6 +51,7 @@ def env_add_defaults(env, family_name: str, sdk_name: str): "$LD_DIR", ], CPPDEFINES=[ + ("LIBRETUYA", "1"), ("LT_VERSION", platform.version), ("LT_BOARD", board.get("build.variant")), ("F_CPU", board.get("build.f_cpu")), diff --git a/docs/config.md b/docs/config.md index 1a80bcb..5383f06 100644 --- a/docs/config.md +++ b/docs/config.md @@ -55,3 +55,5 @@ Families should generally call i.e. WiFiClient debugging for client-related code - LT_HAS_LWIP - whether family SDK has LwIP. This causes `LwIPRxBuffer.cpp` to be compiled for family libraries to use. - LT_HAS_LWIP2 - whether family has LwIP v2.0.0 or newer. This causes `LwIPmDNS.cpp` to be compiled. +- LT_HAS_FREERTOS - whether family supports FreeRTOS +- LT_HAS_MBEDTLS - whether family has mbedTLS