Prep for larger log buffer

This commit is contained in:
Theo Arends 2021-06-09 17:24:28 +02:00
parent 49b5ae71ca
commit 446706f16d
9 changed files with 41 additions and 102 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -17,22 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*********************************************************************************************\
* 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 <core_version.h> // Arduino_Esp8266 version information (ARDUINO_ESP8266_RELEASE and ARDUINO_ESP8266_RELEASE_2_7_1)

View File

@ -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

View File

@ -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

View File

@ -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 <PubSubClient.h>
// 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) {

View File

@ -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

View File

@ -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) {

View File

@ -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