Fix MQTT data corruption on `MQTTLog 4

Fix MQTT data corruption on ``MQTTLog 4`` (#9571)
This commit is contained in:
Theo Arends 2020-10-19 12:39:27 +02:00
parent 54383f77c4
commit f79ed4464e
3 changed files with 11 additions and 6 deletions

View File

@ -23,6 +23,7 @@ All notable changes to this project will be documented in this file.
- Correct Energy period display shortly after midnight by gominoa (#9536)
- Rule handling of Var or Mem using text regression from v8.5.0.1 (#9540)
- TuyaMcu energy display regression from v8.5.0.1 (#9547)
- MQTT data corruption on ``MQTTLog 4`` (#9571)
## [9.0.0.1] - 20201010
### Added

View File

@ -386,7 +386,7 @@ char* RemoveControlCharacter(char* p) {
*write++ = ch;
}
}
if (write != p) { *write-- = '\0'; }
*write++ = '\0';
return p;
}

View File

@ -220,6 +220,12 @@ bool MqttPublishLib(const char* topic, bool retained)
return result;
}
void MqttDumpData(char* topic, char* data, uint32_t data_len) {
char dump_data[data_len +1];
memcpy(dump_data, mqtt_data, sizeof(dump_data)); // Make another copy for removing optional control characters
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_MQTT D_DATA_SIZE " %d, \"%s %s\""), data_len, topic, RemoveControlCharacter(dump_data));
}
void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len)
{
#ifdef USE_DEBUG_DRIVER
@ -242,14 +248,12 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len
char topic[TOPSZ];
strlcpy(topic, mqtt_topic, sizeof(topic));
mqtt_data[data_len] = 0;
char data[data_len +1];
memcpy(data, mqtt_data, sizeof(data));
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_MQTT D_DATA_SIZE " %d, \"%s %s\""), data_len, topic, RemoveControlCharacter(data));
// AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_MQTT D_DATA_SIZE " %d, \"%s %s\""), data_len, topic, data);
// if (LOG_LEVEL_DEBUG_MORE <= seriallog_level) { Serial.println(data); }
memcpy(data, mqtt_data, sizeof(data));
MqttDumpData(topic, data, data_len); // Use a function to save stack space used by dump_data
// MQTT pre-processing
XdrvMailbox.index = strlen(topic);