From bd7288a359564f7277123ce163420f4b39639a15 Mon Sep 17 00:00:00 2001 From: Gilles Grandou Date: Thu, 5 Aug 2021 21:44:09 +0200 Subject: [PATCH] wip --- mqtt.c | 106 +++++++++++++++++++++++++++------------------------------ mqtt.h | 2 -- 2 files changed, 50 insertions(+), 58 deletions(-) diff --git a/mqtt.c b/mqtt.c index 07b1f95..972fc28 100644 --- a/mqtt.c +++ b/mqtt.c @@ -1,6 +1,8 @@ #include "mqtt.h" #include "logger.h" +#include + #include #include #include @@ -8,85 +10,77 @@ static void mosq_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str) { - switch (level) { - case MOSQ_LOG_WARNING: - log_warning("%s", str); - break; - - case MOSQ_LOG_ERR: - log_error("%s", str); - break; - - default: - break; - } + switch (level) { + case MOSQ_LOG_WARNING: log_warning("%s", str); break; + case MOSQ_LOG_ERR: log_error("%s", str); break; + } } struct mosquitto *mqtt_open(const char *host, int port, int keepalive) { - struct mosquitto *mosq; - int res; + struct mosquitto *mosq; + int res; - mosquitto_lib_init(); + mosquitto_lib_init(); - /* Create MQTT client. */ - mosq = mosquitto_new(NULL, 1, NULL); - if (mosq == NULL) { - log_error("Cannot create mosquitto client: %s", strerror(errno)); - return NULL; - } + /* Create MQTT client. */ + mosq = mosquitto_new(NULL, 1, NULL); + if (mosq == NULL) { + log_error("Cannot create mosquitto client: %s", strerror(errno)); + return NULL; + } - mosquitto_log_callback_set(mosq, mosq_log_callback); + mosquitto_log_callback_set(mosq, mosq_log_callback); - /* Connect to broker. */ - res = mosquitto_connect(mosq, host, port, keepalive); - if (res != MOSQ_ERR_SUCCESS) { - log_error("Unable to connect to MQTT broker %s:%d: %s", host, port, mosquitto_strerror(res)); - return NULL; - } + /* Connect to broker. */ + res = mosquitto_connect(mosq, host, port, keepalive); + if (res != MOSQ_ERR_SUCCESS) { + log_error("Unable to connect to MQTT broker %s:%d: %s", host, port, mosquitto_strerror(res)); + return NULL; + } - /* Start network loop. */ - res = mosquitto_loop_start(mosq); - if (res != MOSQ_ERR_SUCCESS) { - log_error("Unable to start loop: %s", mosquitto_strerror(res)); - return NULL; - } + /* Start network loop. */ + res = mosquitto_loop_start(mosq); + if (res != MOSQ_ERR_SUCCESS) { + log_error("Unable to start loop: %s", mosquitto_strerror(res)); + return NULL; + } - return mosq; + return mosq; } void mqtt_close(struct mosquitto *mosq) { - mosquitto_loop_stop(mosq, 1); - mosquitto_disconnect(mosq); - mosquitto_destroy(mosq); - mosquitto_lib_cleanup(); + mosquitto_loop_stop(mosq, 1); + mosquitto_disconnect(mosq); + mosquitto_destroy(mosq); + mosquitto_lib_cleanup(); } int mqtt_publish(struct mosquitto *mosq, const char *topic_prefix, const char *topic_suffix, const void *payload, int qos) { - char topic[TOPIC_MAXLEN + 1]; - int res; + char topic[TOPIC_MAXLEN + 1]; + int res; - if (topic_prefix != NULL) { - if (topic_suffix != NULL) { - sprintf(topic, "%s%s", topic_prefix, topic_suffix); - } else { - sprintf(topic, "%s", topic_prefix); - } + if (topic_prefix != NULL) { + if (topic_suffix != NULL) { + sprintf(topic, "%s%s", topic_prefix, topic_suffix); } else { - if (topic_suffix != NULL) { - sprintf(topic, "%s", topic_suffix); - } else { - return -1; - } + sprintf(topic, "%s", topic_prefix); } + } else { + if (topic_suffix != NULL) { + sprintf(topic, "%s", topic_suffix); + } else { + return -1; + } + } - res = mosquitto_publish(mosq, NULL, topic, strlen(payload), payload, qos, 1); - if (res != 0) - log_error("Cannot publish topic %s: %s\n", topic, mosquitto_strerror(res)); + res = mosquitto_publish(mosq, NULL, topic, strlen(payload), payload, qos, 1); + if (res != 0) + log_error("Cannot publish topic %s: %s\n", topic, mosquitto_strerror(res)); - return res; + return res; } diff --git a/mqtt.h b/mqtt.h index 6f189e1..8524d29 100644 --- a/mqtt.h +++ b/mqtt.h @@ -1,8 +1,6 @@ #ifndef __MQTT_H__ #define __MQTT_H__ -#include - #define TOPIC_MAXLEN 255 #define TIC_QOS 0