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.
This commit is contained in:
Alan Carvalho de Assis 2023-05-12 20:25:10 -03:00 committed by Petro Karashchenko
parent 04243050f7
commit 6b720033cc

View File

@ -212,6 +212,7 @@ static int initserver(FAR const struct mqttc_cfg_s *cfg)
int main(int argc, FAR char *argv[]) int main(int argc, FAR char *argv[])
{ {
int sockfd; int sockfd;
int timeout = 100;
enum MQTTErrors mqtterr; enum MQTTErrors mqtterr;
pthread_t thrdid; pthread_t thrdid;
int n = 1; int n = 1;
@ -278,10 +279,22 @@ int main(int argc, FAR char *argv[])
goto err_with_socket; 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--) while (n--)
{ {
mqtterr = mqtt_publish(&mqtt_cfg.client, mqtt_cfg.topic, 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); mqtt_cfg.qos);
if (mqtterr != MQTT_OK) if (mqtterr != MQTT_OK)
{ {