#include "collectd.h"
-#include "common.h"
#include "plugin.h"
+#include "utils/common/common.h"
#include "utils_complain.h"
#include <mosquitto.h>
* Data types
*/
struct mqtt_client_conf {
- _Bool publish;
+ bool publish;
char *name;
struct mosquitto *mosq;
- _Bool connected;
+ bool connected;
char *host;
int port;
/* For publishing */
char *topic_prefix;
- _Bool store_rates;
- _Bool retain;
+ bool store_rates;
+ bool retain;
/* For subscribing */
pthread_t thread;
- _Bool loop;
+ bool loop;
char *topic;
- _Bool clean_session;
+ bool clean_session;
c_complain_t complaint_cantpublish;
pthread_mutex_t lock;
};
typedef struct mqtt_client_conf mqtt_client_conf_t;
-static mqtt_client_conf_t **subscribers = NULL;
-static size_t subscribers_num = 0;
+static mqtt_client_conf_t **subscribers;
+static size_t subscribers_num;
/*
* Functions
if (conf->connected)
(void)mosquitto_disconnect(conf->mosq);
- conf->connected = 0;
+ conf->connected = false;
(void)mosquitto_destroy(conf->mosq);
sfree(conf->host);
status = mosquitto_reconnect(conf->mosq);
if (status != MOSQ_ERR_SUCCESS) {
- char errbuf[1024];
ERROR("mqtt_connect_broker: mosquitto_connect failed: %s",
- (status == MOSQ_ERR_ERRNO) ? sstrerror(errno, errbuf, sizeof(errbuf))
- : mosquitto_strerror(status));
+ (status == MOSQ_ERR_ERRNO) ? STRERRNO : mosquitto_strerror(status));
return -1;
}
- conf->connected = 1;
+ conf->connected = true;
c_release(LOG_INFO, &conf->complaint_cantpublish,
"mqtt plugin: successfully reconnected to broker \"%s:%d\"",
status =
mosquitto_username_pw_set(conf->mosq, conf->username, conf->password);
if (status != MOSQ_ERR_SUCCESS) {
- char errbuf[1024];
ERROR("mqtt plugin: mosquitto_username_pw_set failed: %s",
- (status == MOSQ_ERR_ERRNO)
- ? sstrerror(errno, errbuf, sizeof(errbuf))
- : mosquitto_strerror(status));
+ (status == MOSQ_ERR_ERRNO) ? STRERRNO : mosquitto_strerror(status));
mosquitto_destroy(conf->mosq);
conf->mosq = NULL;
mosquitto_connect(conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
#endif
if (status != MOSQ_ERR_SUCCESS) {
- char errbuf[1024];
ERROR("mqtt plugin: mosquitto_connect failed: %s",
- (status == MOSQ_ERR_ERRNO) ? sstrerror(errno, errbuf, sizeof(errbuf))
- : mosquitto_strerror(status));
+ (status == MOSQ_ERR_ERRNO) ? STRERRNO : mosquitto_strerror(status));
mosquitto_destroy(conf->mosq);
conf->mosq = NULL;
}
}
- conf->connected = 1;
+ conf->connected = true;
return 0;
} /* mqtt_connect */
/* max_packets = */ 100);
#endif
if (status == MOSQ_ERR_CONN_LOST) {
- conf->connected = 0;
+ conf->connected = false;
continue;
} else if (status != MOSQ_ERR_SUCCESS) {
ERROR("mqtt plugin: mosquitto_loop failed: %s",
mosquitto_strerror(status));
mosquitto_destroy(conf->mosq);
conf->mosq = NULL;
- conf->connected = 0;
+ conf->connected = false;
continue;
}
#endif
conf->qos, conf->retain);
if (status != MOSQ_ERR_SUCCESS) {
- char errbuf[1024];
c_complain(LOG_ERR, &conf->complaint_cantpublish,
"mqtt plugin: mosquitto_publish failed: %s",
- (status == MOSQ_ERR_ERRNO)
- ? sstrerror(errno, errbuf, sizeof(errbuf))
- : mosquitto_strerror(status));
+ (status == MOSQ_ERR_ERRNO) ? STRERRNO
+ : mosquitto_strerror(status));
/* Mark our connection "down" regardless of the error as a safety
* measure; we will try to reconnect the next time we have to publish a
* message */
- conf->connected = 0;
+ conf->connected = false;
mosquitto_disconnect(conf->mosq);
pthread_mutex_unlock(&conf->lock);
ERROR("mqtt plugin: calloc failed.");
return -1;
}
- conf->publish = 1;
+ conf->publish = true;
conf->name = NULL;
status = cf_util_get_string(ci, &conf->name);
conf->client_id = NULL;
conf->qos = 0;
conf->topic_prefix = strdup(MQTT_DEFAULT_TOPIC_PREFIX);
- conf->store_rates = 1;
+ conf->store_rates = true;
status = pthread_mutex_init(&conf->lock, NULL);
if (status != 0) {
ERROR("mqtt plugin: calloc failed.");
return -1;
}
- conf->publish = 0;
+ conf->publish = false;
conf->name = NULL;
status = cf_util_get_string(ci, &conf->name);
conf->client_id = NULL;
conf->qos = 2;
conf->topic = strdup(MQTT_DEFAULT_TOPIC);
- conf->clean_session = 1;
+ conf->clean_session = true;
status = pthread_mutex_init(&conf->lock, NULL);
if (status != 0) {
/* args = */ subscribers[i],
/* name = */ "mqtt");
if (status != 0) {
- char errbuf[1024];
- ERROR("mqtt plugin: pthread_create failed: %s",
- sstrerror(errno, errbuf, sizeof(errbuf)));
+ ERROR("mqtt plugin: pthread_create failed: %s", STRERRNO);
continue;
}
}