mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
mqtt on_message: make char * conversion more clear
This commit is contained in:
16
src/mqtt.cpp
16
src/mqtt.cpp
@@ -203,7 +203,7 @@ void Mqtt::show_mqtt(uuid::console::Shell & shell) {
|
||||
// simulate receiving a MQTT message, used only for testing
|
||||
void Mqtt::incoming(const char * topic, const char * payload) {
|
||||
if (payload != nullptr) {
|
||||
on_message(topic, payload, strlen(payload));
|
||||
on_message(topic, (const uint8_t *) payload, strlen(payload));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -211,15 +211,13 @@ void Mqtt::incoming(const char * topic, const char * payload) {
|
||||
// received an MQTT message that we subscribed too
|
||||
// topic is the full path
|
||||
// payload is json or a single string and converted to a json with key 'value'
|
||||
void Mqtt::on_message(const char * topic, const char * payload, size_t len) const {
|
||||
// the payload is not terminated correctly, so make a copy
|
||||
void Mqtt::on_message(const char * topic, const uint8_t * payload, size_t len) const {
|
||||
// the payload is not terminated
|
||||
// convert payload to a null-terminated char string
|
||||
// see https://www.emelis.net/espMqttClient/#code-samples
|
||||
char message[len + 1];
|
||||
if (payload != nullptr) {
|
||||
strlcpy(message, payload, len + 1);
|
||||
} else {
|
||||
message[0] = '\0';
|
||||
}
|
||||
memcpy(message, payload, len);
|
||||
message[len] = '\0';
|
||||
|
||||
#if defined(EMSESP_DEBUG)
|
||||
if (len) {
|
||||
@@ -390,7 +388,7 @@ void Mqtt::start() {
|
||||
|
||||
EMSESP::esp8266React.onMessage(
|
||||
[this](const espMqttClientTypes::MessageProperties & properties, const char * topic, const uint8_t * payload, size_t len, size_t index, size_t total) {
|
||||
on_message(topic, (const char *)payload, len); // receiving mqtt
|
||||
on_message(topic, payload, len); // receiving mqtt
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -230,7 +230,7 @@ class Mqtt {
|
||||
static void queue_unsubscribe_message(const std::string & topic);
|
||||
|
||||
void on_publish(uint16_t packetId) const;
|
||||
void on_message(const char * topic, const char * payload, size_t len) const;
|
||||
void on_message(const char * topic, const uint8_t * payload, size_t len) const;
|
||||
|
||||
// function handlers for MQTT subscriptions
|
||||
struct MQTTSubFunction {
|
||||
|
||||
Reference in New Issue
Block a user