From 6b720033cc4e9548cb494096e9af858a0722b2be Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Fri, 12 May 2023 20:25:10 -0300 Subject: [PATCH] examples/mqttc: Check for MQTT ACK and avoid passing '\0' This patch will force wait for MQTT ACK (connection) and also fix the issue caused by "strlen(mqtt_cfg.msg) + 1" that will include the "\0" in the payload. It's forbidded by MQTT spec. Some MQTT servers will ignore it, others like TagoIO will refuse the packet. --- examples/mqttc/mqttc_pub.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/mqttc/mqttc_pub.c b/examples/mqttc/mqttc_pub.c index 150218938..c22fc569e 100644 --- a/examples/mqttc/mqttc_pub.c +++ b/examples/mqttc/mqttc_pub.c @@ -212,6 +212,7 @@ static int initserver(FAR const struct mqttc_cfg_s *cfg) int main(int argc, FAR char *argv[]) { int sockfd; + int timeout = 100; enum MQTTErrors mqtterr; pthread_t thrdid; int n = 1; @@ -278,10 +279,22 @@ int main(int argc, FAR char *argv[]) goto err_with_socket; } + /* Wait for MQTT ACK or time-out */ + + while (!mqtt_cfg.client.event_connect && --timeout > 0) + { + usleep(10000); + } + + if (timeout == 0) + { + goto err_with_thrd; + } + while (n--) { mqtterr = mqtt_publish(&mqtt_cfg.client, mqtt_cfg.topic, - mqtt_cfg.msg, strlen(mqtt_cfg.msg) + 1, + mqtt_cfg.msg, strlen(mqtt_cfg.msg), mqtt_cfg.qos); if (mqtterr != MQTT_OK) {