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, ...)
38 int l = vsnprintf(m, sizeof(m), fmt, args);
39 int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
40 if (err != MOSQ_ERR_SUCCESS)
41 mqtt_error("publish", err, false);
46 static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
48 // msg(L_INFO, "MQTT(%d): %s", level, message);
51 static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status)
54 msg(L_DEBUG, "MQTT: Connection established, subscribing");
55 int err = mosquitto_subscribe(mosq, NULL, "bsb/#", 1);
56 if (err != MOSQ_ERR_SUCCESS) {
57 mqtt_error("subscribe", err, false);
60 mqtt_publish("status/bsb-logger", "ok");
62 msg(L_DEBUG, "MQTT: Cannot connect");
66 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m);
68 static void mqtt_connect(void)
74 mosq = mosquitto_new("bsb-logger", 1, NULL);
76 die("Mosquitto: initialization failed");
78 mosquitto_connect_callback_set(mosq, mqtt_conn_callback);
79 mosquitto_log_callback_set(mosq, mqtt_log_callback);
80 mosquitto_message_callback_set(mosq, mqtt_msg_callback);
82 if (mosquitto_will_set(mosq, "status/bsb-logger", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
83 die("Mosquitto: unable to set will");
85 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)
86 die("Mosquitto: unable to set TLS parameters");
88 if (mosquitto_connect_async(mosq, "burrow-mqtt", 8883, 60) != MOSQ_ERR_SUCCESS)
89 die("Mosquitto: connect failed");
92 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m)
94 time_t now = time(NULL);
96 if (m->payloadlen >= sizeof(val) - 1) {
97 msg(L_ERROR, "Invalid value for topic %s", m->topic);
100 memcpy(val, m->payload, m->payloadlen);
101 val[m->payloadlen] = 0;
102 msg(L_DEBUG, "MQTT < %s %s", m->topic, val);
104 static struct fastbuf *logfile;
106 logfile = bopen_file("/var/log/bsb-frames", O_WRONLY | O_CREAT | O_APPEND, NULL);
108 if (!strcmp(m->topic, "bsb/frame"))
109 bprintf(logfile, "%jd %s\n", (uintmax_t) time(NULL), val);
111 // FIXME: Log statistics
118 static int use_daemon;
119 static int use_debug;
121 static struct opt_section options = {
123 OPT_HELP("A daemon for logging BSB frames received via MQTT"),
125 OPT_HELP("Options:"),
126 OPT_BOOL('d', "debug", use_debug, 0, "\tLog debugging messages"),
127 OPT_BOOL(0, "daemon", use_daemon, 0, "\tDaemonize"),
134 int main(int argc UNUSED, char **argv)
137 opt_parse(&options, argv+1);
140 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
141 log_set_default_stream(ls);
144 log_default_stream()->levels &= ~(1U << L_DEBUG);
148 int err = mosquitto_loop_forever(mosq, 10000, 1);
149 if (err != MOSQ_ERR_SUCCESS)
150 mqtt_error("loop", err, false);