4 * (c) 2020 Martin Mares <mj@ucw.cz>
8 #include <ucw/fastbuf.h>
20 #include <curl/curl.h>
21 #include <mosquitto.h>
25 static struct mosquitto *mosq;
27 static void mqtt_error(const char *operation, int err, bool teardown)
29 msg(L_ERROR, "Mosquitto: %s failed: error %d", operation, err);
32 static void mqtt_publish(const char *topic, const char *fmt, ...)
39 int l = vsnprintf(m, sizeof(m), fmt, args);
40 int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
41 if (err != MOSQ_ERR_SUCCESS)
42 mqtt_error("publish", err, false);
48 static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
50 msg(L_INFO, "MQTT(%d): %s", level, message);
53 static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status)
56 msg(L_DEBUG, "MQTT: Connection established, subscribing");
57 int err = mosquitto_subscribe(mosq, NULL, "bsb/#", 1);
58 if (err != MOSQ_ERR_SUCCESS) {
59 mqtt_error("subscribe", err, false);
62 mqtt_publish("status/bsb-logger", "ok");
64 msg(L_DEBUG, "MQTT: Cannot connect");
68 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m);
70 static void mqtt_connect(void)
76 mosq = mosquitto_new("bsb-logger", 1, NULL);
78 die("Mosquitto: initialization failed");
80 mosquitto_connect_callback_set(mosq, mqtt_conn_callback);
81 mosquitto_log_callback_set(mosq, mqtt_log_callback);
82 mosquitto_message_callback_set(mosq, mqtt_msg_callback);
84 if (mosquitto_will_set(mosq, "status/bsb-logger", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
85 die("Mosquitto: unable to set will");
87 if (mosquitto_tls_set(mosq, "/etc/burrow-mqtt/ca.crt", NULL, "/etc/burrow-mqtt/client.crt", "/etc/burrow-mqtt/client.key", NULL) != MOSQ_ERR_SUCCESS)
88 die("Mosquitto: unable to set TLS parameters");
90 if (mosquitto_connect_async(mosq, "burrow-mqtt", 8883, 60) != MOSQ_ERR_SUCCESS)
91 die("Mosquitto: connect failed");
94 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m)
96 time_t now = time(NULL);
98 if (m->payloadlen >= sizeof(val) - 1) {
99 msg(L_ERROR, "Invalid value for topic %s", m->topic);
102 memcpy(val, m->payload, m->payloadlen);
103 val[m->payloadlen] = 0;
104 msg(L_DEBUG, "MQTT < %s %s", m->topic, val);
106 static struct fastbuf *logfile;
108 logfile = bopen_file("/var/log/bsb-frames", O_WRONLY | O_CREAT | O_APPEND, NULL);
110 if (!strcmp(m->topic, "bsb/frame"))
111 bprintf(logfile, "%jd %s\n", (uintmax_t) time(NULL), val);
113 // FIXME: Log statistics
120 static int use_daemon;
121 static int use_debug;
123 static struct opt_section options = {
125 OPT_HELP("A daemon for logging BSB frames received via MQTT"),
127 OPT_HELP("Options:"),
128 OPT_BOOL('d', "debug", use_debug, 0, "\tLog debugging messages"),
129 OPT_BOOL(0, "daemon", use_daemon, 0, "\tDaemonize"),
136 int main(int argc UNUSED, char **argv)
139 opt_parse(&options, argv+1);
142 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
143 log_set_default_stream(ls);
146 log_default_stream()->levels &= ~(1U << L_DEBUG);
150 int err = mosquitto_loop_forever(mosq, 10000, 1);
151 if (err != MOSQ_ERR_SUCCESS)
152 mqtt_error("loop", err, false);