2018-01-17 01:21:58 +01:00
|
|
|
/** Node type: mqtt
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2018-01-17 01:21:58 +01:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2018-01-17 01:21:58 +01:00
|
|
|
#include <mosquitto.h>
|
|
|
|
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/mqtt.hpp>
|
2018-02-06 21:26:12 +01:00
|
|
|
#include <villas/plugin.h>
|
2019-04-23 13:15:00 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/format_type.h>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2020-09-30 11:22:59 +02:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2019-09-12 17:05:18 +02:00
|
|
|
// Each process has a list of clients for which a thread invokes the mosquitto loop
|
|
|
|
static struct vlist clients;
|
|
|
|
static pthread_t thread;
|
2021-02-16 14:15:14 +01:00
|
|
|
static Logger logger;
|
2019-09-12 17:05:18 +02:00
|
|
|
|
|
|
|
static void * mosquitto_loop_thread(void *ctx)
|
|
|
|
{
|
2019-09-16 09:48:22 +02:00
|
|
|
int ret;
|
2019-10-03 11:28:19 +02:00
|
|
|
|
|
|
|
// Set the cancel type of this thread to async
|
2019-09-16 09:48:22 +02:00
|
|
|
ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr);
|
2021-02-16 14:15:14 +01:00
|
|
|
if (ret != 0)
|
|
|
|
throw RuntimeError("Unable to set cancel type of MQTT communication thread to asynchronous.");
|
2019-09-16 09:48:22 +02:00
|
|
|
|
2019-10-03 11:28:19 +02:00
|
|
|
while (true) {
|
2019-09-16 09:48:22 +02:00
|
|
|
for (unsigned i = 0; i < vlist_length(&clients); i++) {
|
2021-02-16 14:15:14 +01:00
|
|
|
struct vnode *n = (struct vnode *) vlist_at(&clients, i);
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
2019-09-16 09:48:22 +02:00
|
|
|
|
2019-10-03 11:28:19 +02:00
|
|
|
// Execute mosquitto loop for this client
|
2019-09-16 09:48:22 +02:00
|
|
|
ret = mosquitto_loop(m->client, 0, 1);
|
2019-10-03 11:28:19 +02:00
|
|
|
if (ret) {
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Connection error: {}, attempting reconnect", mosquitto_strerror(ret));
|
2019-10-03 11:28:19 +02:00
|
|
|
|
2019-09-16 09:48:22 +02:00
|
|
|
ret = mosquitto_reconnect(m->client);
|
2019-10-03 11:28:19 +02:00
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Reconnection to broker failed: {}", mosquitto_strerror(ret));
|
2019-10-03 11:28:19 +02:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Successfully reconnected to broker: {}", mosquitto_strerror(ret));
|
2019-10-03 11:28:19 +02:00
|
|
|
|
2019-10-23 15:21:51 +02:00
|
|
|
ret = mosquitto_loop(m->client, 0, 1);
|
2019-10-03 11:28:19 +02:00
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Persisting connection error: {}", mosquitto_strerror(ret));
|
2019-09-16 09:48:22 +02:00
|
|
|
}
|
2019-10-03 11:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 09:48:22 +02:00
|
|
|
|
|
|
|
return nullptr;
|
2019-09-12 17:05:18 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
static void mqtt_log_cb(struct mosquitto *mosq, void *ctx, int level, const char *str)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
struct vnode *n = (struct vnode *) ctx;
|
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
switch (level) {
|
|
|
|
case MOSQ_LOG_NONE:
|
|
|
|
case MOSQ_LOG_INFO:
|
|
|
|
case MOSQ_LOG_NOTICE:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("{}", str);
|
2018-01-17 01:21:58 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOSQ_LOG_WARNING:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("{}", str);
|
2018-01-17 01:21:58 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOSQ_LOG_ERR:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->error("{}", str);
|
2018-01-17 01:21:58 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOSQ_LOG_DEBUG:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->debug("{}", str);
|
2018-01-17 01:21:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
static void mqtt_connect_cb(struct mosquitto *mosq, void *ctx, int result)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
struct vnode *n = (struct vnode *) ctx;
|
2018-01-17 01:21:58 +01:00
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2018-05-26 02:23:32 +02:00
|
|
|
int ret;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("Connected to broker {}", m->host);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2018-08-20 18:26:53 +02:00
|
|
|
if (m->subscribe) {
|
2019-04-22 23:45:38 +02:00
|
|
|
ret = mosquitto_subscribe(m->client, nullptr, m->subscribe, m->qos);
|
2018-07-13 13:36:13 +02:00
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Failed to subscribe to topic '{}': {}", m->subscribe, mosquitto_strerror(ret));
|
2018-07-13 13:36:13 +02:00
|
|
|
}
|
2018-08-20 18:26:53 +02:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("No subscription as no topic is configured");
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
static void mqtt_disconnect_cb(struct mosquitto *mosq, void *ctx, int result)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
struct vnode *n = (struct vnode *) ctx;
|
2018-01-17 01:21:58 +01:00
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("Disconnected from broker {}", m->host);
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
static void mqtt_message_cb(struct mosquitto *mosq, void *ctx, const struct mosquitto_message *msg)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2021-02-16 14:15:14 +01:00
|
|
|
struct vnode *n = (struct vnode *) ctx;
|
2018-01-17 01:21:58 +01:00
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
2019-03-25 16:01:17 +01:00
|
|
|
struct sample *smps[n->in.vectorize];
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->debug("Received a message of {} bytes from broker {}", msg->payloadlen, m->host);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2019-03-25 16:01:17 +01:00
|
|
|
ret = sample_alloc_many(&m->pool, smps, n->in.vectorize);
|
|
|
|
if (ret <= 0) {
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Pool underrun in subscriber");
|
2018-01-17 01:21:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
ret = io_sscan(&m->io, (char *) msg->payload, msg->payloadlen, nullptr, smps, n->in.vectorize);
|
2018-05-26 02:23:32 +02:00
|
|
|
if (ret < 0) {
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Received an invalid message");
|
|
|
|
n->logger->warn(" Payload: {}", (char *) msg->payload);
|
2018-05-26 02:23:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-10-03 11:28:19 +02:00
|
|
|
|
2018-07-17 11:20:38 +02:00
|
|
|
if (ret == 0) {
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->debug("Skip empty message");
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_decref_many(smps, n->in.vectorize);
|
2018-01-17 01:21:58 +01:00
|
|
|
return;
|
2018-05-26 02:23:32 +02:00
|
|
|
}
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = queue_signalled_push_many(&m->queue, (void **) smps, n->in.vectorize);
|
|
|
|
if (ret < (int) n->in.vectorize)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Failed to enqueue samples");
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
static void mqtt_subscribe_cb(struct mosquitto *mosq, void *ctx, int mid, int qos_count, const int *granted_qos)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
struct vnode *n = (struct vnode *) ctx;
|
2018-01-17 01:21:58 +01:00
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("Subscribed to broker {}", m->host);
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_reverse(struct vnode *n)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
|
|
|
SWAP(m->publish, m->subscribe);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:22:59 +02:00
|
|
|
int mqtt_init(struct vnode *n)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2021-02-15 15:17:44 +01:00
|
|
|
m->client = mosquitto_new(nullptr, true, (void *) n);
|
2020-09-30 11:22:59 +02:00
|
|
|
if (!m->client)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = mosquitto_threaded_set(m->client, true);
|
|
|
|
if (ret)
|
|
|
|
goto mosquitto_error;
|
|
|
|
|
|
|
|
mosquitto_log_callback_set(m->client, mqtt_log_cb);
|
|
|
|
mosquitto_connect_callback_set(m->client, mqtt_connect_cb);
|
|
|
|
mosquitto_disconnect_callback_set(m->client, mqtt_disconnect_cb);
|
|
|
|
mosquitto_message_callback_set(m->client, mqtt_message_cb);
|
|
|
|
mosquitto_subscribe_callback_set(m->client, mqtt_subscribe_cb);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
m->port = 1883;
|
|
|
|
m->qos = 0;
|
|
|
|
m->retain = 0;
|
2020-12-04 09:09:09 +01:00
|
|
|
m->keepalive = 5; /* 5 second, minimum required for libmosquitto */
|
2020-09-30 11:38:38 +02:00
|
|
|
|
|
|
|
m->host = nullptr;
|
|
|
|
m->username = nullptr;
|
|
|
|
m->password = nullptr;
|
|
|
|
m->publish = nullptr;
|
|
|
|
m->subscribe = nullptr;
|
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
m->ssl.enabled = 0;
|
|
|
|
m->ssl.insecure = 0;
|
2020-09-30 11:38:38 +02:00
|
|
|
m->ssl.cafile = nullptr;
|
|
|
|
m->ssl.capath = nullptr;
|
|
|
|
m->ssl.certfile = nullptr;
|
|
|
|
m->ssl.keyfile = nullptr;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2020-09-30 11:22:59 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
mosquitto_error:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("{}", mosquitto_strerror(ret));
|
2020-09-30 11:22:59 +02:00
|
|
|
|
|
|
|
return ret;
|
2021-01-08 15:16:15 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int mqtt_parse(struct vnode *n, json_t *json)
|
2020-09-30 11:22:59 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
|
|
|
const char *host;
|
|
|
|
const char *format = "villas.binary";
|
|
|
|
const char *publish = nullptr;
|
|
|
|
const char *subscribe = nullptr;
|
|
|
|
const char *username = nullptr;
|
|
|
|
const char *password = nullptr;
|
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
json_error_t err;
|
2019-04-22 23:45:38 +02:00
|
|
|
json_t *json_ssl = nullptr;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: { s?: s }, s?: { s?: s }, s?: s, s: s, s?: i, s?: i, s?: i, s?: b, s?: s, s?: s, s?: o }",
|
2018-08-20 18:26:53 +02:00
|
|
|
"out",
|
|
|
|
"publish", &publish,
|
|
|
|
"in",
|
|
|
|
"subscribe", &subscribe,
|
2018-01-17 01:21:58 +01:00
|
|
|
"format", &format,
|
|
|
|
"host", &host,
|
|
|
|
"port", &m->port,
|
|
|
|
"qos", &m->qos,
|
|
|
|
"keepalive", &m->keepalive,
|
|
|
|
"retain", &m->retain,
|
|
|
|
"username", &username,
|
|
|
|
"password", &password,
|
|
|
|
"ssl", &json_ssl
|
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-mqtt");
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
m->host = strdup(host);
|
2019-04-22 23:45:38 +02:00
|
|
|
m->publish = publish ? strdup(publish) : nullptr;
|
|
|
|
m->subscribe = subscribe ? strdup(subscribe) : nullptr;
|
|
|
|
m->username = username ? strdup(username) : nullptr;
|
|
|
|
m->password = password ? strdup(password) : nullptr;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2019-01-12 13:48:52 +01:00
|
|
|
if (!m->publish && !m->subscribe)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, "node-config-node-mqtt", "At least one topic has to be specified for node {}", node_name(n));
|
2019-01-12 13:48:52 +01:00
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
if (json_ssl) {
|
2020-09-30 11:38:38 +02:00
|
|
|
m->ssl.enabled = 1;
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *cafile = nullptr;
|
|
|
|
const char *capath = nullptr;
|
|
|
|
const char *certfile = nullptr;
|
|
|
|
const char *keyfile = nullptr;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2020-09-30 11:22:59 +02:00
|
|
|
ret = json_unpack_ex(json_ssl, &err, 0, "{ s?: b, s?: b, s?: s, s?: s, s?: s, s?: s }",
|
2018-01-17 01:21:58 +01:00
|
|
|
"enabled", &m->ssl.enabled,
|
|
|
|
"insecure", &m->ssl.insecure,
|
|
|
|
"cafile", &cafile,
|
|
|
|
"capath", &capath,
|
|
|
|
"certfile", &certfile,
|
|
|
|
"keyfile", &keyfile
|
|
|
|
);
|
|
|
|
if (ret)
|
2020-09-30 11:22:59 +02:00
|
|
|
throw ConfigError(json_ssl, err, "node-config-node-mqtt-ssl", "Failed to parse SSL configuration of node {}", node_name(n));
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
if (m->ssl.enabled && !cafile && !capath)
|
2020-09-30 11:22:59 +02:00
|
|
|
throw ConfigError(json_ssl, "node-config-node-mqtt-ssl", "Either 'ssl.cafile' or 'ssl.capath' settings must be set for node {}.", node_name(n));
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
m->ssl.cafile = cafile ? strdup(cafile) : nullptr;
|
|
|
|
m->ssl.capath = capath ? strdup(capath) : nullptr;
|
|
|
|
m->ssl.certfile = certfile ? strdup(certfile) : nullptr;
|
|
|
|
m->ssl.keyfile = keyfile ? strdup(keyfile) : nullptr;
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
m->format = format_type_lookup(format);
|
2018-01-17 01:21:58 +01:00
|
|
|
if (!m->format)
|
2020-09-30 11:22:59 +02:00
|
|
|
throw ConfigError(json_ssl, "node-config-node-mqtt-format", "Invalid format '{}' for node {}", format, node_name(n));
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2019-03-26 07:10:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_check(struct vnode *n)
|
2019-03-26 07:10:07 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2018-05-26 02:23:32 +02:00
|
|
|
ret = mosquitto_sub_topic_check(m->subscribe);
|
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Invalid subscribe topic: '{}': {}", m->subscribe, mosquitto_strerror(ret));
|
2018-05-26 02:23:32 +02:00
|
|
|
|
|
|
|
ret = mosquitto_pub_topic_check(m->publish);
|
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Invalid publish topic: '{}': {}", m->publish, mosquitto_strerror(ret));
|
2018-05-26 02:23:32 +02:00
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:00:05 +02:00
|
|
|
int mqtt_prepare(struct vnode *n)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
|
|
|
ret = io_init(&m->io, m->format, &n->in.signals, (int) SampleFlags::HAS_ALL & ~(int) SampleFlags::HAS_OFFSET);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = pool_init(&m->pool, 1024, SAMPLE_LENGTH(vlist_length(&n->in.signals)));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = queue_signalled_init(&m->queue, 1024);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * mqtt_print(struct vnode *n)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
char *buf = nullptr;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2020-09-30 11:22:59 +02:00
|
|
|
strcatf(&buf, "format=%s, host=%s, port=%d, keepalive=%d, ssl=%s", format_type_name(m->format),
|
2018-01-17 01:21:58 +01:00
|
|
|
m->host,
|
|
|
|
m->port,
|
2020-09-30 11:22:59 +02:00
|
|
|
m->keepalive,
|
2018-01-17 01:21:58 +01:00
|
|
|
m->ssl.enabled ? "yes" : "no"
|
|
|
|
);
|
|
|
|
|
2018-05-24 15:50:43 +02:00
|
|
|
/* Only show if not default */
|
|
|
|
if (m->username)
|
|
|
|
strcatf(&buf, ", username=%s", m->username);
|
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
if (m->publish)
|
2018-08-20 18:26:53 +02:00
|
|
|
strcatf(&buf, ", out.publish=%s", m->publish);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
if (m->subscribe)
|
2018-08-20 18:26:53 +02:00
|
|
|
strcatf(&buf, ", in.subscribe=%s", m->subscribe);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_destroy(struct vnode *n)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
|
|
|
mosquitto_destroy(m->client);
|
|
|
|
|
2020-09-30 11:22:59 +02:00
|
|
|
ret = io_destroy(&m->io);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
ret = pool_destroy(&m->pool);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = queue_signalled_destroy(&m->queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (m->publish)
|
|
|
|
free(m->publish);
|
|
|
|
if (m->subscribe)
|
|
|
|
free(m->subscribe);
|
|
|
|
if (m->password)
|
|
|
|
free(m->password);
|
|
|
|
if (m->username)
|
|
|
|
free(m->username);
|
|
|
|
|
|
|
|
free(m->host);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_start(struct vnode *n)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2021-02-15 15:17:44 +01:00
|
|
|
if (m->username && m->password) {
|
|
|
|
ret = mosquitto_username_pw_set(m->client, m->username, m->password);
|
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
|
|
|
goto mosquitto_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->ssl.enabled) {
|
|
|
|
ret = mosquitto_tls_set(m->client, m->ssl.cafile, m->ssl.capath, m->ssl.certfile, m->ssl.keyfile, nullptr);
|
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
|
|
|
goto mosquitto_error;
|
|
|
|
|
|
|
|
ret = mosquitto_tls_insecure_set(m->client, m->ssl.insecure);
|
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
|
|
|
goto mosquitto_error;
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:08:20 +02:00
|
|
|
ret = mosquitto_connect(m->client, m->host, m->port, m->keepalive);
|
2020-09-30 11:22:59 +02:00
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2019-02-12 19:56:36 +01:00
|
|
|
goto mosquitto_error;
|
2018-05-24 09:08:20 +02:00
|
|
|
|
2019-10-03 11:28:19 +02:00
|
|
|
// Add client to global list of MQTT clients
|
2019-09-12 17:05:18 +02:00
|
|
|
// so that thread can call mosquitto loop for this client
|
2019-09-16 09:48:22 +02:00
|
|
|
vlist_push(&clients, n);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
return 0;
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
mosquitto_error:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("{}", mosquitto_strerror(ret));
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
return ret;
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_stop(struct vnode *n)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2019-10-03 11:28:19 +02:00
|
|
|
// Unregister client from global MQTT client list
|
2019-09-16 09:48:22 +02:00
|
|
|
// so that mosquitto loop is no longer invoked for this client
|
|
|
|
// important to do that before disconnecting from broker, otherwise, mosquitto thread will attempt to reconnect
|
|
|
|
vlist_remove(&clients, vlist_index(&clients, n));
|
2019-09-13 09:56:50 +02:00
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
ret = mosquitto_disconnect(m->client);
|
2020-09-30 11:22:59 +02:00
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2019-02-12 19:56:36 +01:00
|
|
|
goto mosquitto_error;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
return 0;
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
mosquitto_error:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("{}", mosquitto_strerror(ret));
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
return ret;
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2019-04-23 13:14:47 +02:00
|
|
|
int mqtt_type_start(villas::node::SuperNode *sn)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
logger = logging.get("node:mqtt");
|
|
|
|
|
2019-09-16 09:48:22 +02:00
|
|
|
ret = vlist_init(&clients);
|
2020-09-10 11:11:42 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-09-12 17:05:18 +02:00
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
ret = mosquitto_lib_init();
|
2020-09-30 11:22:59 +02:00
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2019-02-12 19:56:36 +01:00
|
|
|
goto mosquitto_error;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2019-10-03 11:28:19 +02:00
|
|
|
// Start thread here to run mosquitto loop for registered clients
|
2019-09-16 09:48:22 +02:00
|
|
|
ret = pthread_create(&thread, nullptr, mosquitto_loop_thread, nullptr);
|
2020-09-10 11:11:42 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-09-12 17:05:18 +02:00
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
return 0;
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
mosquitto_error:
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("{}", mosquitto_strerror(ret));
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
return ret;
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
int mqtt_type_stop()
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-10-03 11:28:19 +02:00
|
|
|
// Stop thread here that executes mosquitto loop
|
2019-09-16 09:48:22 +02:00
|
|
|
ret = pthread_cancel(thread);
|
|
|
|
if (ret)
|
2020-09-10 11:11:42 +02:00
|
|
|
return ret;
|
2021-02-16 14:15:14 +01:00
|
|
|
|
|
|
|
logger->debug("Called pthread_cancel() on MQTT communication management thread.");
|
2019-09-12 17:05:18 +02:00
|
|
|
|
2019-09-16 09:48:22 +02:00
|
|
|
ret = pthread_join(thread, nullptr);
|
2020-09-10 11:11:42 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-09-12 17:05:18 +02:00
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
ret = mosquitto_lib_cleanup();
|
2020-09-30 11:22:59 +02:00
|
|
|
if (ret != MOSQ_ERR_SUCCESS)
|
2019-02-12 19:56:36 +01:00
|
|
|
goto mosquitto_error;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2019-10-03 11:28:19 +02:00
|
|
|
// When this is called the list of clients should be empty
|
2020-09-30 11:22:59 +02:00
|
|
|
if (vlist_length(&clients) > 0)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("List of MQTT clients contains elements at time of destruction. Call node_stop for each MQTT node before stopping node type!");
|
2020-09-30 11:22:59 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = vlist_destroy(&clients, nullptr, false);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-09-12 17:05:18 +02:00
|
|
|
|
2018-01-17 01:21:58 +01:00
|
|
|
return 0;
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
mosquitto_error:
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("{}", mosquitto_strerror(ret));
|
2019-02-12 19:56:36 +01:00
|
|
|
|
|
|
|
return ret;
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int pulled;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
2018-07-11 18:14:29 +02:00
|
|
|
struct sample *smpt[cnt];
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
pulled = queue_signalled_pull_many(&m->queue, (void **) smpt, cnt);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
sample_copy_many(smps, smpt, pulled);
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_decref_many(smpt, pulled);
|
2018-01-17 01:21:58 +01:00
|
|
|
|
|
|
|
return pulled;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
|
|
|
size_t wbytes;
|
|
|
|
|
|
|
|
char data[1500];
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
ret = io_sprint(&m->io, data, sizeof(data), &wbytes, smps, cnt);
|
2018-05-12 15:25:29 +02:00
|
|
|
if (ret < 0)
|
2018-05-23 14:58:03 +02:00
|
|
|
return ret;
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2018-07-16 20:17:08 +02:00
|
|
|
if (m->publish) {
|
2019-04-22 23:45:38 +02:00
|
|
|
ret = mosquitto_publish(m->client, nullptr /* mid */, m->publish, wbytes, data, m->qos, m->retain);
|
2018-07-16 20:17:08 +02:00
|
|
|
if (ret != MOSQ_ERR_SUCCESS) {
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Publish failed: {}", mosquitto_strerror(ret));
|
2018-07-16 20:17:08 +02:00
|
|
|
return -abs(ret);
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 15:47:34 +01:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("No publish possible because no publish topic is configured");
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
return cnt;
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int mqtt_poll_fds(struct vnode *n, int fds[])
|
2018-01-17 01:21:58 +01:00
|
|
|
{
|
|
|
|
struct mqtt *m = (struct mqtt *) n->_vd;
|
|
|
|
|
2019-01-21 15:47:34 +01:00
|
|
|
fds[0] = queue_signalled_fd(&m->queue);
|
|
|
|
|
|
|
|
return 1;
|
2018-01-17 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
static struct plugin p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
|
|
|
p.name = "mqtt";
|
|
|
|
p.description = "Message Queuing Telemetry Transport (libmosquitto)";
|
2019-06-23 16:13:23 +02:00
|
|
|
p.type = PluginType::NODE;
|
|
|
|
p.node.instances.state = State::DESTROYED;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.vectorize = 0;
|
|
|
|
p.node.size = sizeof(struct mqtt);
|
|
|
|
p.node.type.start = mqtt_type_start;
|
|
|
|
p.node.type.stop = mqtt_type_stop;
|
|
|
|
p.node.destroy = mqtt_destroy;
|
2021-01-08 15:16:15 +01:00
|
|
|
p.node.prepare = mqtt_prepare;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.parse = mqtt_parse;
|
|
|
|
p.node.check = mqtt_check;
|
2021-02-15 15:17:44 +01:00
|
|
|
p.node.prepare = mqtt_prepare;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.print = mqtt_print;
|
2020-09-30 11:22:59 +02:00
|
|
|
p.node.init = mqtt_init;
|
|
|
|
p.node.destroy = mqtt_destroy;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.start = mqtt_start;
|
|
|
|
p.node.stop = mqtt_stop;
|
|
|
|
p.node.read = mqtt_read;
|
|
|
|
p.node.write = mqtt_write;
|
|
|
|
p.node.reverse = mqtt_reverse;
|
|
|
|
p.node.poll_fds = mqtt_poll_fds;
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret = vlist_init(&p.node.instances);
|
|
|
|
if (!ret)
|
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-04-23 00:36:06 +02:00
|
|
|
}
|
2018-01-17 01:21:58 +01:00
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
__attribute__((destructor(110)))
|
|
|
|
static void deregister_plugin() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-04-23 00:36:06 +02:00
|
|
|
}
|