diff --git a/arduino/libretuya/api/LibreTuyaAPI.h b/arduino/libretuya/api/LibreTuyaAPI.h new file mode 100644 index 0000000..2f59c7d --- /dev/null +++ b/arduino/libretuya/api/LibreTuyaAPI.h @@ -0,0 +1,22 @@ +#pragma once + +#ifndef LT_VERSION +#define LT_VERSION "1.0.0" +#endif + +#include + +#include "LibreTuyaConfig.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#include "lt_logger.h" + +#ifdef __cplusplus +} // extern "C" +#endif + +#define LT_BANNER() \ + LT_LOG(LT_LEVEL_INFO, "main.cpp", __LINE__, "LibreTuya v" LT_VERSION " compiled on " __DATE__ " " __TIME__) diff --git a/arduino/libretuya/api/LibreTuyaConfig.h b/arduino/libretuya/api/LibreTuyaConfig.h new file mode 100644 index 0000000..e197742 --- /dev/null +++ b/arduino/libretuya/api/LibreTuyaConfig.h @@ -0,0 +1,64 @@ +#pragma once + +// see docs/API Configuration + +// Loglevels +#define LT_LEVEL_VERBOSE LT_LEVEL_TRACE +#define LT_LEVEL_TRACE 0 +#define LT_LEVEL_DEBUG 1 +#define LT_LEVEL_INFO 2 +#define LT_LEVEL_WARN 3 +#define LT_LEVEL_ERROR 4 +#define LT_LEVEL_FATAL 5 + +// Logger enabled/disabled +#ifndef LT_LOGGER +#define LT_LOGGER 1 +#endif + +// Logger format options +#ifndef LT_LOGGER_TIMESTAMP +#define LT_LOGGER_TIMESTAMP 1 +#endif + +#ifndef LT_LOGGER_FILE +#define LT_LOGGER_FILE 0 +#endif + +#ifndef LT_LOGGER_TASK +#define LT_LOGGER_TASK 1 +#endif + +#ifndef LT_LOGGER_COLOR +#define LT_LOGGER_COLOR 0 +#endif + +#ifndef LT_PRINTF_BROKEN +#define LT_PRINTF_BROKEN 0 +#endif + +// Global loglevel +#ifndef LT_LOGLEVEL +#define LT_LOGLEVEL LT_LEVEL_INFO +#endif + +// Per-module debugging +#ifndef LT_DEBUG_WIFI +#define LT_DEBUG_WIFI 0 +#endif + +#ifndef LT_DEBUG_WIFI_CLIENT +#define LT_DEBUG_WIFI_CLIENT 0 +#endif + +#ifndef LT_DEBUG_WIFI_SERVER +#define LT_DEBUG_WIFI_SERVER 0 +#endif + +#ifndef LT_DEBUG_WIFI_STA +#define LT_DEBUG_WIFI_STA 0 +#endif + +#ifndef LT_DEBUG_WIFI_AP +#define LT_DEBUG_WIFI_AP 0 +#endif diff --git a/arduino/libretuya/api/lt_logger.c b/arduino/libretuya/api/lt_logger.c new file mode 100644 index 0000000..93f4998 --- /dev/null +++ b/arduino/libretuya/api/lt_logger.c @@ -0,0 +1,126 @@ +#include "lt_logger.h" + +#include + +#if LT_LOGGER_TASK +#include +#include +#endif + +#define COLOR_FMT "\e[0;30m" +#define COLOR_BLACK 0x00 +#define COLOR_RED 0x01 +#define COLOR_GREEN 0x02 +#define COLOR_YELLOW 0x03 +#define COLOR_BLUE 0x04 +#define COLOR_MAGENTA 0x05 +#define COLOR_CYAN 0x06 +#define COLOR_WHITE 0x07 +#define COLOR_BRIGHT_BLACK 0x10 +#define COLOR_BRIGHT_RED 0x11 +#define COLOR_BRIGHT_GREEN 0x12 +#define COLOR_BRIGHT_YELLOW 0x13 +#define COLOR_BRIGHT_BLUE 0x14 +#define COLOR_BRIGHT_MAGENTA 0x15 +#define COLOR_BRIGHT_CYAN 0x16 +#define COLOR_BRIGHT_WHITE 0x17 + +const char levels[] = {'V', 'D', 'I', 'W', 'E', 'F'}; + +#if LT_LOGGER_COLOR +const uint8_t colors[] = { + COLOR_BRIGHT_CYAN, + COLOR_BRIGHT_BLUE, + COLOR_BRIGHT_GREEN, + COLOR_BRIGHT_YELLOW, + COLOR_BRIGHT_RED, + COLOR_BRIGHT_MAGENTA, +}; +#endif + +unsigned long millis(void); + +#if LT_LOGGER_FILE +void lt_log(const uint8_t level, const char *filename, const unsigned short line, const char *format, ...) { +#else +void lt_log(const uint8_t level, const char *format, ...) { +#endif + +#if LT_LOGGER_TIMESTAMP + float seconds = millis() / 1000.0f; +#if LT_PRINTF_BROKEN + char zero[4] = "\x00\x30\x30"; + if (seconds == 0.0f) + zero[0] = '0'; +#endif +#endif + +#if LT_LOGGER_TASK + char task_colon = ':'; + TaskHandle_t task = xTaskGetCurrentTaskHandle(); + char *task_name = pcTaskGetTaskName(task); + if (!task) { + task_name = ""; + task_colon = '-'; + } +#endif + +#if LT_LOGGER_COLOR + char c_bright = '0' + (colors[level] >> 4); + char c_value = '0' + (colors[level] & 0x7); +#endif + + printf( +#if LT_LOGGER_COLOR + "\e[%c;3%cm" +#endif + "%c " +#if LT_LOGGER_TIMESTAMP +#if LT_PRINTF_BROKEN + "[%11.3f%s] " +#else + "[%11.3f] " +#endif +#endif +#if LT_LOGGER_COLOR + "\e[0m" +#endif +#if LT_LOGGER_FILE + "%s:%hu: " +#endif +#if LT_LOGGER_TASK + "%s%c " +#endif + , + levels[level] +#if LT_LOGGER_COLOR + , + c_bright, // whether text is bright + c_value // text color +#endif +#if LT_LOGGER_TIMESTAMP + , + seconds // float +#if LT_PRINTF_BROKEN + , + zero // append missing zeroes if printf "%11.3f" prints "0." +#endif +#endif +#if LT_LOGGER_FILE + , + filename, + line +#endif +#if LT_LOGGER_TASK + , + task_name, + task_colon // printing outside of tasks +#endif + ); + + va_list va_args; + va_start(va_args, format); + vprintf(format, va_args); + va_end(va_args); + printf("\r\n"); +} diff --git a/arduino/libretuya/api/lt_logger.h b/arduino/libretuya/api/lt_logger.h new file mode 100644 index 0000000..918bdc6 --- /dev/null +++ b/arduino/libretuya/api/lt_logger.h @@ -0,0 +1,89 @@ +#pragma once + +#include "LibreTuyaConfig.h" +#include + +#if LT_LOGGER_FILE +#define LT_LOG(level, file, line, ...) lt_log(level, file, line, __VA_ARGS__) +void lt_log(const uint8_t level, const char *filename, const unsigned short line, const char *format, ...); +#else +#define LT_LOG(level, file, line, ...) lt_log(level, __VA_ARGS__) +void lt_log(const uint8_t level, const char *format, ...); +#endif + +#if LT_LEVEL_TRACE >= LT_LOGLEVEL +#define LT_T(...) LT_LOG(LT_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__) +#define LT_V(...) LT_LOG(LT_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__) +#else +#define LT_T(...) +#define LT_V(...) +#endif + +#if LT_LEVEL_DEBUG >= LT_LOGLEVEL +#define LT_D(...) LT_LOG(LT_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__) +#else +#define LT_D(...) +#endif + +#if LT_LEVEL_INFO >= LT_LOGLEVEL +#define LT_I(...) LT_LOG(LT_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__) +#else +#define LT_I(...) +#endif + +#if LT_LEVEL_WARN >= LT_LOGLEVEL +#define LT_W(...) LT_LOG(LT_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__) +#else +#define LT_W(...) +#endif + +#if LT_LEVEL_ERROR >= LT_LOGLEVEL +#define LT_E(...) LT_LOG(LT_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__) +#else +#define LT_E(...) +#endif + +#if LT_LEVEL_FATAL >= LT_LOGLEVEL +#define LT_F(...) LT_LOG(LT_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__) +#else +#define LT_F(...) +#endif + +#define LT_T_MOD(module, ...) \ + do { \ + if (module) { \ + LT_T(__VA_ARGS__) \ + } \ + } while (0) + +#define LT_D_MOD(module, ...) \ + do { \ + if (module) { \ + LT_D(__VA_ARGS__) \ + } \ + } while (0) + +// WiFi.cpp +#define LT_T_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__) +#define LT_V_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__) +#define LT_D_WG(...) LT_D_MOD(LT_DEBUG_WIFI, __VA_ARGS__) + +// WiFiClient.cpp +#define LT_T_WC(...) LT_T_MOD(LT_DEBUG_WIFI_CLIENT, __VA_ARGS__) +#define LT_V_WC(...) LT_T_MOD(LT_DEBUG_WIFI_CLIENT, __VA_ARGS__) +#define LT_D_WC(...) LT_D_MOD(LT_DEBUG_WIFI_CLIENT, __VA_ARGS__) + +// WiFiServer.cpp +#define LT_T_WS(...) LT_T_MOD(LT_DEBUG_WIFI_SERVER, __VA_ARGS__) +#define LT_V_WS(...) LT_T_MOD(LT_DEBUG_WIFI_SERVER, __VA_ARGS__) +#define LT_D_WS(...) LT_D_MOD(LT_DEBUG_WIFI_SERVER, __VA_ARGS__) + +// WiFiSTA.cpp +#define LT_T_WSTA(...) LT_T_MOD(LT_DEBUG_WIFI_STA, __VA_ARGS__) +#define LT_V_WSTA(...) LT_T_MOD(LT_DEBUG_WIFI_STA, __VA_ARGS__) +#define LT_D_WSTA(...) LT_D_MOD(LT_DEBUG_WIFI_STA, __VA_ARGS__) + +// WiFiAP.cpp +#define LT_T_WAP(...) LT_T_MOD(LT_DEBUG_WIFI_AP, __VA_ARGS__) +#define LT_V_WAP(...) LT_T_MOD(LT_DEBUG_WIFI_AP, __VA_ARGS__) +#define LT_D_WAP(...) LT_D_MOD(LT_DEBUG_WIFI_AP, __VA_ARGS__) diff --git a/arduino/realtek-ambz/cores/arduino/Arduino.h b/arduino/realtek-ambz/cores/arduino/Arduino.h index 484f73c..24efee2 100644 --- a/arduino/realtek-ambz/cores/arduino/Arduino.h +++ b/arduino/realtek-ambz/cores/arduino/Arduino.h @@ -23,7 +23,8 @@ #include "WCharacterFixup.h" #endif #define PinMode PinModeArduino // this conflicts with SDK enum -#include "api/ArduinoAPI.h" +#include +#include #undef PinMode #ifdef __cplusplus diff --git a/arduino/realtek-ambz/cores/arduino/main.cpp b/arduino/realtek-ambz/cores/arduino/main.cpp index d62927f..d3b07d6 100644 --- a/arduino/realtek-ambz/cores/arduino/main.cpp +++ b/arduino/realtek-ambz/cores/arduino/main.cpp @@ -86,6 +86,7 @@ void main_task( void const *arg ) */ int main( void ) { + LT_BANNER(); init(); __libc_init_array(); diff --git a/arduino/realtek-ambz/libraries/WiFi/WiFiAP.cpp b/arduino/realtek-ambz/libraries/WiFi/WiFiAP.cpp index 27c312c..04636e5 100644 --- a/arduino/realtek-ambz/libraries/WiFi/WiFiAP.cpp +++ b/arduino/realtek-ambz/libraries/WiFi/WiFiAP.cpp @@ -7,11 +7,15 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo vTaskDelay(20); - if (!ssid || *ssid == 0x00 || strlen(ssid) > 32) + if (!ssid || *ssid == 0x00 || strlen(ssid) > 32) { + LT_W("SSID not specified or too long"); return false; + } - if (passphrase && strlen(passphrase) < 8) + if (passphrase && strlen(passphrase) < 8) { + LT_W("Passphrase too short"); return false; + } strcpy((char *)ap.ssid.val, ssid); ap.ssid.len = strlen(ssid); @@ -30,6 +34,8 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo dhcps_deinit(); + LT_I("Creating SoftAP %s", ssid); + int ret; if (!ssidHidden) { ret = wifi_start_ap( @@ -51,8 +57,10 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo ); } - if (ret < 0) + if (ret < 0) { + LT_E("SoftAP failed; ret=%d", ret); return false; + } uint8_t timeout = 20; unsigned char essid[33]; diff --git a/arduino/realtek-ambz/libraries/WiFi/WiFiClient.cpp b/arduino/realtek-ambz/libraries/WiFi/WiFiClient.cpp index cd1171f..3060e93 100644 --- a/arduino/realtek-ambz/libraries/WiFi/WiFiClient.cpp +++ b/arduino/realtek-ambz/libraries/WiFi/WiFiClient.cpp @@ -2,6 +2,7 @@ #include "WiFiPriv.h" WiFiClient::WiFiClient() { + LT_V_WC("WiFiClient()"); _sock = -1; _connected = false; _rxBuffer = NULL; @@ -9,6 +10,7 @@ WiFiClient::WiFiClient() { } WiFiClient::WiFiClient(int sock) { + LT_V_WC("WiFiClient(%d)", sock); _sock = sock; _connected = true; _rxBuffer = new LwIPRxBuffer(sock); @@ -16,6 +18,7 @@ WiFiClient::WiFiClient(int sock) { } WiFiClient::~WiFiClient() { + LT_V_WC("~WiFiClient()"); stop(); } @@ -69,19 +72,19 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout) { int res = lwip_connect(sock, (struct sockaddr *)&addr, sizeof(addr)); if (res < 0 && errno != EINPROGRESS) { - printf("lwip_connect() errno %d\r\n", errno); + LT_E("Connect failed; errno=%d", errno); lwip_close(sock); return -1; } res = lwip_select(sock + 1, NULL, &fdset, NULL, timeout < 0 ? NULL : &tv); if (res < 0) { - printf("lwip_select() errno %d\r\n", errno); + LT_E("Select failed; errno=%d", errno); lwip_close(sock); return 0; } if (res == 0) { - printf("lwip_select() timeout errno %d\r\n", errno); + LT_E("Select timeout; errno=%d", errno); lwip_close(sock); return 0; } @@ -91,6 +94,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout) { res = lwip_getsockopt(sock, SOL_SOCKET, SO_ERROR, &sockerr, &len); if (res < 0 || sockerr != 0) { + LT_E("Socket error; res=%d, sockerr=%d", res, sockerr); lwip_close(sock); return 0; } @@ -151,6 +155,7 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) { retry--; if (lwip_select(_sock + 1, NULL, &fdset, NULL, &tv) < 0) { + LT_W("Select failed; errno=%d", errno); return 0; } @@ -166,6 +171,7 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) { retry = WIFI_CLIENT_WRITE_RETRY; } } else if (res < 0 && errno != EAGAIN) { + LT_W("Send failed; errno=%d", errno); setWriteError(res); _connected = false; retry = 0; @@ -253,6 +259,7 @@ void WiFiClient::flush() { } void WiFiClient::stop() { + LT_V_WC("stop()"); if (_sock != -1) lwip_close(_sock); _sock = -1; @@ -268,6 +275,7 @@ uint8_t WiFiClient::connected() { switch (errno) { case EWOULDBLOCK: case ENOENT: // caused by vfs + case 0: _connected = true; break; case ENOTCONN: @@ -275,9 +283,11 @@ uint8_t WiFiClient::connected() { case ECONNRESET: case ECONNREFUSED: case ECONNABORTED: + LT_W("Connection closed; errno=%d", errno); _connected = false; break; default: + LT_I("Connection status unknown; errno=%d", errno); _connected = true; break; } diff --git a/arduino/realtek-ambz/libraries/WiFi/WiFiGeneric.cpp b/arduino/realtek-ambz/libraries/WiFi/WiFiGeneric.cpp index 2576c0c..e696734 100644 --- a/arduino/realtek-ambz/libraries/WiFi/WiFiGeneric.cpp +++ b/arduino/realtek-ambz/libraries/WiFi/WiFiGeneric.cpp @@ -9,11 +9,13 @@ int32_t WiFiClass::channel() { bool WiFiClass::mode(WiFiMode mode) { WiFiMode currentMode = getMode(); + LT_D_WG("Mode changing %u -> %u", currentMode, mode); if (mode == currentMode) return true; if (!currentMode && mode && !_initialized) { // initialize wifi first + LT_I("Initializing LwIP"); LwIP_Init(); reset_wifi_struct(); // wifi_manager_init(); // these are events! @@ -21,13 +23,18 @@ bool WiFiClass::mode(WiFiMode mode) { } if (currentMode) { // stop wifi to change mode + LT_D_WG("Stopping WiFi to change mode"); if (wifi_off() != RTW_SUCCESS) return false; vTaskDelay(20); + if (mode == WIFI_MODE_NULL) + return false; } - if (wifi_on((rtw_mode_t)mode) != RTW_SUCCESS) + if (wifi_on((rtw_mode_t)mode) != RTW_SUCCESS) { + LT_E("Error while changing mode(%u)", mode); return false; + } return true; } @@ -62,6 +69,7 @@ bool WiFiClass::enableAP(bool enable) { } bool WiFiClass::setSleep(bool enable) { + LT_D_WG("WiFi sleep mode %u", enable); if (enable) if (wifi_enable_powersave() != RTW_SUCCESS) return false; diff --git a/arduino/realtek-ambz/libraries/WiFi/WiFiSTA.cpp b/arduino/realtek-ambz/libraries/WiFi/WiFiSTA.cpp index 84047ec..49624d2 100644 --- a/arduino/realtek-ambz/libraries/WiFi/WiFiSTA.cpp +++ b/arduino/realtek-ambz/libraries/WiFi/WiFiSTA.cpp @@ -10,11 +10,15 @@ WiFiClass::begin(const char *ssid, const char *passphrase, int32_t channel, cons if (!enableSTA(true)) return WL_CONNECT_FAILED; - if (!ssid || *ssid == 0x00 || strlen(ssid) > 32) + if (!ssid || *ssid == 0x00 || strlen(ssid) > 32) { + LT_W("SSID not specified or too long"); return WL_CONNECT_FAILED; + } - if (passphrase && strlen(passphrase) > 64) + if (passphrase && strlen(passphrase) > 64) { + LT_W("Passphrase too long"); return WL_CONNECT_FAILED; + } memset(wifi.bssid.octet, 0, ETH_ALEN); strcpy((char *)wifi.ssid.val, ssid); @@ -60,6 +64,8 @@ bool WiFiClass::reconnect(const uint8_t *bssid) { int ret; uint8_t dhcpRet; + LT_I("Connecting to %s", wifi.ssid.val); + if (!bssid) { ret = wifi_connect( (char *)wifi.ssid.val, @@ -88,9 +94,11 @@ bool WiFiClass::reconnect(const uint8_t *bssid) { dhcpRet = LwIP_DHCP(0, DHCP_START); if (dhcpRet == DHCP_ADDRESS_ASSIGNED) return true; + LT_E("DHCP failed; dhcpRet=%d", dhcpRet); wifi_disconnect(); return false; } + LT_E("Connection failed; ret=%d", ret); return false; } diff --git a/arduino/realtek-ambz/libraries/WiFi/WiFiServer.cpp b/arduino/realtek-ambz/libraries/WiFi/WiFiServer.cpp index 288d1c4..4f309f8 100644 --- a/arduino/realtek-ambz/libraries/WiFi/WiFiServer.cpp +++ b/arduino/realtek-ambz/libraries/WiFi/WiFiServer.cpp @@ -15,8 +15,10 @@ bool WiFiServer::begin(uint16_t port, bool reuseAddr) { _port = port; _sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (_sock < 0) + if (_sock < 0) { + LT_E("Socket failed; errno=%d", errno); return false; + } int enable = reuseAddr; lwip_setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); @@ -26,11 +28,17 @@ bool WiFiServer::begin(uint16_t port, bool reuseAddr) { addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(_port); - if (lwip_bind(_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) + if (lwip_bind(_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + LT_E("Bind failed; errno=%d", errno); return false; + } - if (lwip_listen(_sock, _maxClients) < 0) + if (lwip_listen(_sock, _maxClients) < 0) { + LT_E("Bind failed; errno=%d", errno); return false; + } + + LT_I("Server running on :%hu", _port); lwip_fcntl(_sock, F_SETFL, O_NONBLOCK); _active = true; @@ -74,6 +82,7 @@ WiFiClient WiFiServer::accept() { // and receive data, so LwIP still sees a connected client that sends nothing. At least // that's what I understand. And any loop that doesn't call delay() seems to block the TCP // stack completely and prevents it from even being pinged. + LT_D_WS("Got client"); delay(5); return WiFiClient(sock); } diff --git a/builder/arduino-common.py b/builder/arduino-common.py index c2c44ae..cbca4b0 100644 --- a/builder/arduino-common.py +++ b/builder/arduino-common.py @@ -30,8 +30,9 @@ sources_api = [ "+<" + API_DIR + "/api/Print.cpp>", "+<" + API_DIR + "/api/Stream.cpp>", "+<" + API_DIR + "/api/String.cpp>", - "+<" + LT_API_DIR + "/api/LwIPRxBuffer.cpp>", "+<" + LT_API_DIR + "/api/IPv6Address.cpp>", + "+<" + LT_API_DIR + "/api/lt_logger.c>", + "+<" + LT_API_DIR + "/api/LwIPRxBuffer.cpp>", # fmt: on ] diff --git a/builder/frameworks/realtek-ambz-arduino.py b/builder/frameworks/realtek-ambz-arduino.py index 2eb2010..65f824b 100644 --- a/builder/frameworks/realtek-ambz-arduino.py +++ b/builder/frameworks/realtek-ambz-arduino.py @@ -61,6 +61,7 @@ env.Append( ("bool", "bool"), # enable LwIPRxBuffer "LT_HAS_LWIP", + ("LT_PRINTF_BROKEN", "1"), # printf does not handle %.3f properly ("zalloc", "os_zalloc"), ], LINKFLAGS=[ diff --git a/builder/frameworks/realtek-ambz-sdk.py b/builder/frameworks/realtek-ambz-sdk.py index 4e074c3..a78e979 100644 --- a/builder/frameworks/realtek-ambz-sdk.py +++ b/builder/frameworks/realtek-ambz-sdk.py @@ -76,6 +76,7 @@ env.Replace( ("LWIP_SO_RCVBUF", "1"), # for ioctl(FIONREAD) ("INT_MAX", "2147483647"), # for RECV_BUFSIZE_DEFAULT ("ERRNO", "1"), # for LwIP + ("vprintf", "rtl_vprintf"), ], LINKFLAGS=[ "-mcpu=cortex-m4", diff --git a/builder/main.py b/builder/main.py index 8a1c4a6..0c76a8a 100644 --- a/builder/main.py +++ b/builder/main.py @@ -24,6 +24,12 @@ env.Replace( SIZETOOL="arm-none-eabi-size", ) +env.Append( + CPPDEFINES=[ + ("LT_VERSION", "0.1.0"), + ] +) + # Flash layout defines flash_layout: dict = board.get("flash") if flash_layout: diff --git a/docs/API Configuration.md b/docs/API Configuration.md new file mode 100644 index 0000000..ede2292 --- /dev/null +++ b/docs/API Configuration.md @@ -0,0 +1,42 @@ +# LibreTuya API Configuration + +Note: see [LibreTuyaConfig.h](../../arduino/libretuya/api/LibreTuyaConfig.h) for most options and their defaults. + +All options are configurable via C++ defines in PlatformIO project file. For example: +```ini +[env:my_board] +build_flags = + -D LT_LOGLEVEL=LT_LEVEL_DEBUG +``` + +## Logging + +- LT_LOGGER - enable/disable LibreTuya logger globally. Enabled by default. +- LT_LOGLEVEL - global LT loglevel: + - LT_LEVEL_TRACE (same as LT_LEVEL_VERBOSE) + - LT_LEVEL_DEBUG + - LT_LEVEL_INFO - default + - LT_LEVEL_WARN + - LT_LEVEL_ERROR + - LT_LEVEL_FATAL +- LT_LOGGER_TIMESTAMP - print program runtime in printk-like format +- LT_LOGGER_FILE - print calling source filename +- LT_LOGGER_TASK - print calling FreeRTOS task (if available) +- LT_LOGGER_COLOR - output ANSI terminal colors +- LT_PRINTF_BROKEN - whether printf outputs "0." for floats with value 0 + +## Debug logging + +The following options enable library-specific debugging messages. They are only effective if `LT_LOGLEVEL` is set below INFO. All of them are disabled by default. + +Platforms should generally call i.e. WiFiClient debugging for client-related code, even if the `WiFiClient.cpp` file is physically absent. + +- LT_DEBUG_WIFI - `WiFi.cpp` +- LT_DEBUG_WIFI_CLIENT - `WiFiClient.cpp` +- LT_DEBUG_WIFI_SERVER - `WiFiServer.cpp` +- LT_DEBUG_WIFI_STA - `WiFiSTA.cpp` +- LT_DEBUG_WIFI_AP - `WiFiAP.cpp` + +## Platform options + +- LT_HAS_LWIP - whether platform SDK has LwIP. This causes `LwIPRxBuffer.cpp` to be compiled for platform libraries to use.