From 446706f16d48c48d1d27652d91bae48239d36401 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 9 Jun 2021 17:24:28 +0200 Subject: [PATCH] Prep for larger log buffer --- tasmota/support.ino | 55 +++-------------------------- tasmota/tasmota.h | 12 +++++-- tasmota/tasmota.ino | 16 --------- tasmota/tasmota_globals.h | 10 +----- tasmota/xdrv_01_webserver.ino | 3 +- tasmota/xdrv_02_9_mqtt.ino | 8 ++--- tasmota/xdrv_52_3_berry_tasmota.ino | 2 ++ tasmota/xdrv_79_esp32_ble.ino | 29 +++++++-------- tasmota/xdrv_interface.ino | 8 +++-- 9 files changed, 41 insertions(+), 102 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index cd717b960..cf570b9ad 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1174,7 +1174,7 @@ char* ResponseGetTime(uint32_t format, char* time_str) uint32_t ResponseSize(void) { #ifdef MQTT_DATA_STRING - return MESSZ; + return MAX_LOGSZ; // Arbitratry max length satisfying full log entry #else return sizeof(TasmotaGlobal.mqtt_data); #endif @@ -1191,7 +1191,8 @@ uint32_t ResponseLength(void) { void ResponseClear(void) { // Reset string length to zero #ifdef MQTT_DATA_STRING - TasmotaGlobal.mqtt_data = ""; +// TasmotaGlobal.mqtt_data = ""; + TasmotaGlobal.mqtt_data = (const char*) nullptr; #else TasmotaGlobal.mqtt_data[0] = '\0'; #endif @@ -1347,10 +1348,6 @@ void TemplateConvert(uint8_t template8[], uint16_t template16[]) { template16[i] = GpioConvert(template8[i]); } template16[(sizeof(mytmplt) / 2) -2] = Adc0Convert(template8[sizeof(mytmplt8285) -1]); - -// AddLog(LOG_LEVEL_DEBUG, PSTR("FNC: TemplateConvert")); -// AddLogBuffer(LOG_LEVEL_DEBUG, template8, sizeof(mytmplt8285)); -// AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t*)template16, sizeof(mytmplt) / 2, 2); } void ConvertGpios(void) { @@ -1363,50 +1360,8 @@ void ConvertGpios(void) { } Settings.my_gp.io[(sizeof(myio) / 2) -1] = Adc0Convert(Settings.ex_my_adc0); Settings.gpio16_converted = 0xF5A0; - -// AddLog(LOG_LEVEL_DEBUG, PSTR("FNC: ConvertGpios")); -// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)&Settings.ex_my_gp8.io, sizeof(myio8)); -// AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t *)&Settings.my_gp.io, sizeof(myio) / 2, 2); } } - -/* -void DumpConvertTable(void) { - bool jsflg = false; - uint32_t lines = 1; - for (uint32_t i = 0; i < nitems(kGpioConvert); i++) { - uint32_t data = pgm_read_word(kGpioConvert + i); - if (!jsflg) { - Response_P(PSTR("{\"GPIOConversion%d\":{"), lines); - } else { - ResponseAppend_P(PSTR(",")); - } - jsflg = true; - if ((ResponseAppend_P(PSTR("\"%d\":\"%d\""), i, data) > (MAX_LOGSZ - TOPSZ)) || (i == nitems(kGpioConvert) -1)) { - ResponseJsonEndEnd(); - MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, XdrvMailbox.command); - jsflg = false; - lines++; - } - } - for (uint32_t i = 0; i < nitems(kAdcNiceList); i++) { - uint32_t data = pgm_read_word(kAdcNiceList + i); - if (!jsflg) { - Response_P(PSTR("{\"ADC0Conversion%d\":{"), lines); - } else { - ResponseAppend_P(PSTR(",")); - } - jsflg = true; - if ((ResponseAppend_P(PSTR("\"%d\":\"%d\""), i, data) > (MAX_LOGSZ - TOPSZ)) || (i == nitems(kAdcNiceList) -1)) { - ResponseJsonEndEnd(); - MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, XdrvMailbox.command); - jsflg = false; - lines++; - } - } - ResponseClear(); -} -*/ #endif // ESP8266 int IRAM_ATTR Pin(uint32_t gpio, uint32_t index = 0); @@ -2314,7 +2269,7 @@ bool NeedLogRefresh(uint32_t req_loglevel, uint32_t index) { #endif // ESP32 // Skip initial buffer fill - if (strlen(TasmotaGlobal.log_buffer) < LOG_BUFFER_SIZE - MAX_LOGSZ) { return false; } + if (strlen(TasmotaGlobal.log_buffer) < LOG_BUFFER_SIZE / 2) { return false; } char* line; size_t len; @@ -2399,7 +2354,7 @@ void AddLogData(uint32_t loglevel, const char* log_data) { // Each entry has this format: [index][loglevel][log data]['\1'] uint32_t log_data_len = strlen(log_data); - if (log_data_len + 64 > LOG_BUFFER_SIZE) { + if (log_data_len > MAX_LOGSZ) { sprintf_P((char*)log_data + 128, PSTR(" ... truncated %d"), log_data_len); } diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index d087bd5fb..96bca14c2 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -25,6 +25,7 @@ \*********************************************************************************************/ #define XFUNC_PTR_IN_ROM // Enable for keeping tables in ROM (PROGMEM) which seem to have access issues on some flash types +#define MQTT_DATA_STRING // Use heap instead of fixed memory for TasmotaGlobal.mqtt_data /*********************************************************************************************\ * Default sensor states @@ -171,9 +172,14 @@ const uint16_t INPUT_BUFFER_SIZE = 520; // Max number of characters in seria const uint16_t FLOATSZ = 16; // Max number of characters in float result from dtostrfd (max 32) const uint16_t CMDSZ = 24; // Max number of characters in command const uint16_t TOPSZ = 151; // Max number of characters in topic string -const uint16_t LOGSZ = 128; // Max number of characters in AddLog_P log -const uint16_t MAX_LOGSZ = 700; // Max number of characters in log -const uint16_t MIN_MESSZ = 1040; // Min number of characters in MQTT message (1200 - TOPSZ - 9 header bytes) +const uint16_t LOG_BUFFER_SIZE = 4096; // Max number of characters in logbuffer used by weblog, syslog and mqttlog +//const uint16_t LOG_BUFFER_SIZE = 5120; // Max number of characters in logbuffer used by weblog, syslog and mqttlog +//const uint16_t LOG_BUFFER_SIZE = 6144; // Max number of characters in logbuffer used by weblog, syslog and mqttlog +#ifdef MQTT_DATA_STRING +const uint16_t MAX_LOGSZ = LOG_BUFFER_SIZE -96; // Max number of characters in log line - may be overruled which will truncate log entry +#else +const uint16_t MAX_LOGSZ = 700; // Max number of characters in log line +#endif const uint8_t SENSOR_MAX_MISS = 5; // Max number of missed sensor reads before deciding it's offline diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 3e6549424..3b71ba0dc 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -17,22 +17,6 @@ along with this program. If not, see . */ -/*********************************************************************************************\ - * Preferred IDE is Visual Studio Code with PlatformIO extension which doesn't need prerequisites - * - * Limited support for Arduino IDE needs Prerequisites: - * - Change libraries/PubSubClient/src/PubSubClient.h - * #define MQTT_MAX_PACKET_SIZE 1200 - * - * Arduino IDE 1.8.12 and up parameters for partly support - * - Select IDE Tools - Board: "Generic ESP8266 Module" - * - Select IDE Tools - Flash Mode: "DOUT (compatible)" - * - Select IDE Tools - Flash Size: "1M (FS:none OTA:~502KB)" - * - Select IDE Tools - LwIP Variant: "v2 Higher Bandwidth (no feature)" - * - Select IDE Tools - VTables: "Flash" - * - Select IDE Tools - Espressif FW: "nonos-sdk-2.2.1+100 (190703)" -\*********************************************************************************************/ - // Location specific includes #ifndef ESP32_STAGE // ESP32 Stage has no core_version.h file. Disable include via PlatformIO Option #include // Arduino_Esp8266 version information (ARDUINO_ESP8266_RELEASE and ARDUINO_ESP8266_RELEASE_2_7_1) diff --git a/tasmota/tasmota_globals.h b/tasmota/tasmota_globals.h index 2dea686d5..f3b58d864 100644 --- a/tasmota/tasmota_globals.h +++ b/tasmota/tasmota_globals.h @@ -193,7 +193,6 @@ String EthernetMacAddress(void); #ifdef USE_PID #define USE_TIMEPROP #endif - // See https://github.com/esp8266/Arduino/pull/4889 #undef NO_EXTRA_4K_HEAP // Allocate 4k heap for WPS in ESP8166/Arduino core v2.4.2 (was always allocated in previous versions) @@ -244,8 +243,6 @@ String EthernetMacAddress(void); #define WS2812_LEDS 30 // [Pixels] Number of LEDs #endif -const uint16_t LOG_BUFFER_SIZE = 4000; // Max number of characters in logbuffer used by weblog, syslog and mqttlog - #if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2) || defined(ARDUINO_ESP8266_RELEASE_2_5_0) || defined(ARDUINO_ESP8266_RELEASE_2_5_1) || defined(ARDUINO_ESP8266_RELEASE_2_5_2) #error "Arduino ESP8266 Core versions before 2.7.1 are not supported" #endif @@ -266,8 +263,6 @@ const uint16_t LOG_BUFFER_SIZE = 4000; // Max number of characters in lo #define TASM_FILE_AUTOEXEC "/autoexec.bat" // Commands executed after restart #define TASM_FILE_CONFIG "/config.sys" // Settings executed after restart -#define MQTT_DATA_STRING // Use heap instead of fixed memory for TasmotaGlobal.mqtt_data - #ifndef MQTT_MAX_PACKET_SIZE #define MQTT_MAX_PACKET_SIZE 1200 // Bytes //#define MQTT_MAX_PACKET_SIZE 2048 // Bytes @@ -289,10 +284,7 @@ const uint16_t LOG_BUFFER_SIZE = 4000; // Max number of characters in lo #endif #ifndef MESSZ -//#define MESSZ 1040 // Max number of characters in JSON message string (Hass discovery and nice MQTT_MAX_PACKET_SIZE = 1200) -#define MESSZ (MQTT_MAX_PACKET_SIZE -TOPSZ -7) // Max number of characters in JSON message string -//#define MESSZ MQTT_MAX_PACKET_SIZE // Max number of characters in JSON message string -//#define MESSZ 2048 // Max number of characters in JSON message string +#define MESSZ 1040 // Max number of characters in JSON message string (Hass discovery and nice MQTT_MAX_PACKET_SIZE = 1200) #endif #ifndef USE_DEVICE_GROUPS diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index f9228171b..b7f39480a 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -47,8 +47,7 @@ #endif // If the first is true, but this is false, the device will restart but the user will see // a window telling that the WiFi Configuration was Ok and that the window can be closed. -//const uint16_t CHUNKED_BUFFER_SIZE = (MESSZ / 2) - 100; // Chunk buffer size (should be smaller than half mqtt_data size = MESSZ) -const uint16_t CHUNKED_BUFFER_SIZE = 800; // Chunk buffer size +const uint16_t CHUNKED_BUFFER_SIZE = 500; // Chunk buffer size const uint16_t HTTP_REFRESH_TIME = 2345; // milliseconds const uint16_t HTTP_RESTART_RECONNECT_TIME = 10000; // milliseconds - Allow time for restart and wifi reconnect diff --git a/tasmota/xdrv_02_9_mqtt.ino b/tasmota/xdrv_02_9_mqtt.ino index bd43f57c9..0f56a0026 100644 --- a/tasmota/xdrv_02_9_mqtt.ino +++ b/tasmota/xdrv_02_9_mqtt.ino @@ -187,15 +187,13 @@ void MqttDisableLogging(bool state) { * void MqttDisconnect() * void MqttSubscribeLib(char *topic) * bool MqttPublishLib(const char* topic, const uint8_t* payload, unsigned int plength, bool retained) + * + * Change/Verify PubSubClient.h defines: + * #define MQTT_MAX_PACKET_SIZE 1200 // Tasmota v8.1.0.8 \*********************************************************************************************/ #include -// Max message size calculated by PubSubClient is (MQTT_MAX_PACKET_SIZE < 5 + 2 + strlen(topic) + plength) -#if (MQTT_MAX_PACKET_SIZE -TOPSZ -7) < MIN_MESSZ // If the max message size is too small, throw an error at compile time. See PubSubClient.cpp line 359 - #error "MQTT_MAX_PACKET_SIZE is too small in libraries/PubSubClient/src/PubSubClient.h, increase it to at least 1200" -#endif - PubSubClient MqttClient; void MqttInit(void) { diff --git a/tasmota/xdrv_52_3_berry_tasmota.ino b/tasmota/xdrv_52_3_berry_tasmota.ino index 4aedf3330..711c641f2 100644 --- a/tasmota/xdrv_52_3_berry_tasmota.ino +++ b/tasmota/xdrv_52_3_berry_tasmota.ino @@ -432,6 +432,8 @@ void berry_log(const char * berry_buf) { AddLog(LOG_LEVEL_INFO, PSTR("%s"), berry_buf); } +const uint16_t LOGSZ = 128; // Max number of characters in log line + extern "C" { void berry_log_C(const char * berry_buf, ...) { // To save stack space support logging for max text length of 128 characters diff --git a/tasmota/xdrv_79_esp32_ble.ino b/tasmota/xdrv_79_esp32_ble.ino index 8593d94f0..e145ceb79 100644 --- a/tasmota/xdrv_79_esp32_ble.ino +++ b/tasmota/xdrv_79_esp32_ble.ino @@ -3145,7 +3145,20 @@ void CmndBLEOperation(void){ \*********************************************************************************************/ static void BLEPostMQTTSeenDevices(int type) { int remains = 0; -/* +#ifdef MQTT_DATA_STRING + ResponseTime_P(PSTR("")); + String response_time = TasmotaGlobal.mqtt_data; + + int maxlen = 1024; + char dest[maxlen]; + do { + Response_P(response_time.c_str()); // Keep using same time stamp + remains = getSeenDevicesToJson(dest, maxlen); + ResponseAppend_P(dest); + // no retain - this is present devices, not historic + MqttPublishPrefixTopicRulesProcess_P((1== type) ? TELE : STAT, PSTR("BLE")); + } while (remains); +#else nextSeenDev = 0; memset(TasmotaGlobal.mqtt_data, 0, sizeof(TasmotaGlobal.mqtt_data)); @@ -3160,19 +3173,7 @@ static void BLEPostMQTTSeenDevices(int type) { MqttPublishPrefixTopicRulesProcess_P((1== type) ? TELE : STAT, PSTR("BLE")); } while (remains); // } -*/ - - // Once TasmotaGlobal.mqtt_data is changed from (limited by MESSZ) char array to (unlimited) String the below code can be simplified - char dest[ResponseSize()]; - int maxlen = ResponseSize() -64; - - do { - remains = getSeenDevicesToJson(dest, maxlen); - ResponseTime_P(dest); - // no retain - this is present devices, not historic - MqttPublishPrefixTopicRulesProcess_P((1== type) ? TELE : STAT, PSTR("BLE")); - } while (remains); - +#endif } static void BLEPostMQTT(bool onlycompleted) { diff --git a/tasmota/xdrv_interface.ino b/tasmota/xdrv_interface.ino index c0d99d71e..f3825e0c7 100644 --- a/tasmota/xdrv_interface.ino +++ b/tasmota/xdrv_interface.ino @@ -1079,9 +1079,9 @@ void XsnsDriverState(void) /*********************************************************************************************/ -bool XdrvRulesProcess(bool teleperiod, const char* payload) { +bool XdrvRulesProcess(bool teleperiod, const char* event) { char* data_save = XdrvMailbox.data; - XdrvMailbox.data = (char*)payload; + XdrvMailbox.data = (char*)event; bool rule_handled = XdrvCallDriver(10, (teleperiod) ? FUNC_TELEPERIOD_RULES_PROCESS : FUNC_RULES_PROCESS); #ifdef USE_BERRY // events are passed to both Rules engine AND Berry engine @@ -1094,7 +1094,9 @@ bool XdrvRulesProcess(bool teleperiod, const char* payload) { bool XdrvRulesProcess(bool teleperiod) { #ifdef MQTT_DATA_STRING - return XdrvRulesProcess(teleperiod, TasmotaGlobal.mqtt_data.c_str()); + bool result = XdrvRulesProcess(teleperiod, TasmotaGlobal.mqtt_data.c_str()); + ResponseClear(); + return result; #else return XdrvRulesProcess(teleperiod, TasmotaGlobal.mqtt_data); #endif