This commit is contained in:
Gilles Grandou 2021-08-05 21:44:09 +02:00
parent 23fd1c3da0
commit bd7288a359
2 changed files with 50 additions and 58 deletions

106
mqtt.c
View File

@ -1,6 +1,8 @@
#include "mqtt.h" #include "mqtt.h"
#include "logger.h" #include "logger.h"
#include <mosquitto.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -8,85 +10,77 @@
static void mosq_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str) static void mosq_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{ {
switch (level) { switch (level) {
case MOSQ_LOG_WARNING: case MOSQ_LOG_WARNING: log_warning("%s", str); break;
log_warning("%s", str); case MOSQ_LOG_ERR: log_error("%s", str); break;
break; }
case MOSQ_LOG_ERR:
log_error("%s", str);
break;
default:
break;
}
} }
struct mosquitto *mqtt_open(const char *host, int port, int keepalive) struct mosquitto *mqtt_open(const char *host, int port, int keepalive)
{ {
struct mosquitto *mosq; struct mosquitto *mosq;
int res; int res;
mosquitto_lib_init(); mosquitto_lib_init();
/* Create MQTT client. */ /* Create MQTT client. */
mosq = mosquitto_new(NULL, 1, NULL); mosq = mosquitto_new(NULL, 1, NULL);
if (mosq == NULL) { if (mosq == NULL) {
log_error("Cannot create mosquitto client: %s", strerror(errno)); log_error("Cannot create mosquitto client: %s", strerror(errno));
return NULL; return NULL;
} }
mosquitto_log_callback_set(mosq, mosq_log_callback); mosquitto_log_callback_set(mosq, mosq_log_callback);
/* Connect to broker. */ /* Connect to broker. */
res = mosquitto_connect(mosq, host, port, keepalive); res = mosquitto_connect(mosq, host, port, keepalive);
if (res != MOSQ_ERR_SUCCESS) { if (res != MOSQ_ERR_SUCCESS) {
log_error("Unable to connect to MQTT broker %s:%d: %s", host, port, mosquitto_strerror(res)); log_error("Unable to connect to MQTT broker %s:%d: %s", host, port, mosquitto_strerror(res));
return NULL; return NULL;
} }
/* Start network loop. */ /* Start network loop. */
res = mosquitto_loop_start(mosq); res = mosquitto_loop_start(mosq);
if (res != MOSQ_ERR_SUCCESS) { if (res != MOSQ_ERR_SUCCESS) {
log_error("Unable to start loop: %s", mosquitto_strerror(res)); log_error("Unable to start loop: %s", mosquitto_strerror(res));
return NULL; return NULL;
} }
return mosq; return mosq;
} }
void mqtt_close(struct mosquitto *mosq) void mqtt_close(struct mosquitto *mosq)
{ {
mosquitto_loop_stop(mosq, 1); mosquitto_loop_stop(mosq, 1);
mosquitto_disconnect(mosq); mosquitto_disconnect(mosq);
mosquitto_destroy(mosq); mosquitto_destroy(mosq);
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
} }
int mqtt_publish(struct mosquitto *mosq, const char *topic_prefix, const char *topic_suffix, const void *payload, int qos) int mqtt_publish(struct mosquitto *mosq, const char *topic_prefix, const char *topic_suffix, const void *payload, int qos)
{ {
char topic[TOPIC_MAXLEN + 1]; char topic[TOPIC_MAXLEN + 1];
int res; int res;
if (topic_prefix != NULL) { if (topic_prefix != NULL) {
if (topic_suffix != NULL) { if (topic_suffix != NULL) {
sprintf(topic, "%s%s", topic_prefix, topic_suffix); sprintf(topic, "%s%s", topic_prefix, topic_suffix);
} else {
sprintf(topic, "%s", topic_prefix);
}
} else { } else {
if (topic_suffix != NULL) { sprintf(topic, "%s", topic_prefix);
sprintf(topic, "%s", topic_suffix);
} else {
return -1;
}
} }
} else {
if (topic_suffix != NULL) {
sprintf(topic, "%s", topic_suffix);
} else {
return -1;
}
}
res = mosquitto_publish(mosq, NULL, topic, strlen(payload), payload, qos, 1); res = mosquitto_publish(mosq, NULL, topic, strlen(payload), payload, qos, 1);
if (res != 0) if (res != 0)
log_error("Cannot publish topic %s: %s\n", topic, mosquitto_strerror(res)); log_error("Cannot publish topic %s: %s\n", topic, mosquitto_strerror(res));
return res; return res;
} }

2
mqtt.h
View File

@ -1,8 +1,6 @@
#ifndef __MQTT_H__ #ifndef __MQTT_H__
#define __MQTT_H__ #define __MQTT_H__
#include <mosquitto.h>
#define TOPIC_MAXLEN 255 #define TOPIC_MAXLEN 255
#define TIC_QOS 0 #define TIC_QOS 0