From 868d0a1ecbfbcd1f04b5c03403d6cffb439ce8fe Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Tue, 29 Oct 2024 22:34:23 +0100 Subject: Verschiebe MQTT Code in ein eigenes Modul --- compile_flags.txt | 1 + config.mk | 2 +- include/mqtt.h | 16 +++++++++++++ src/main.c | 63 +------------------------------------------------- src/mqtt.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 63 deletions(-) create mode 100644 include/mqtt.h create mode 100644 src/mqtt.c diff --git a/compile_flags.txt b/compile_flags.txt index 255b429..a50455c 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -6,3 +6,4 @@ -pedantic -std=c17 -Isrc +-Iinclude diff --git a/config.mk b/config.mk index d733c58..c6da73f 100644 --- a/config.mk +++ b/config.mk @@ -1,2 +1,2 @@ -CFLAGS=-Wall -Werror -Wextra -Wconversion -Wdouble-promotion -pedantic -std=c17 -Isrc +CFLAGS=-Wall -Werror -Wextra -Wconversion -Wdouble-promotion -pedantic -std=c17 -Isrc -Iinclude LDFLAGS=-lmosquitto diff --git a/include/mqtt.h b/include/mqtt.h new file mode 100644 index 0000000..fa7df1c --- /dev/null +++ b/include/mqtt.h @@ -0,0 +1,16 @@ +#pragma once + +// Project +#include + +struct mosquitto *connect(const char *clientid, // NOLINT + const char *username, // NOLINT + const char *password, // NOLINT + const char *hostname, // NOLINT + int port, + void callback(struct mosquitto *, void *, const struct mosquitto_message *), + void *userdata); + +void run(struct mosquitto *client, const char *topics[], const volatile bool *quit); + +void shutdown(struct mosquitto *client); diff --git a/src/main.c b/src/main.c index 8a25176..5bef36b 100644 --- a/src/main.c +++ b/src/main.c @@ -8,9 +8,8 @@ #include // Project -#include - #include "config.h" +#include "mqtt.h" #ifndef UNUSED # define UNUSED(x) (void) (x) @@ -41,66 +40,6 @@ init(void) (void) atexit(cleanup); } -static struct mosquitto * -connect(const char *clientid, // NOLINT - const char *username, // NOLINT - const char *password, // NOLINT - const char *hostname, // NOLINT - int port, - void callback(struct mosquitto *, void *, const struct mosquitto_message *), - void *userdata) -{ - struct mosquitto *client = mosquitto_new(clientid, true, userdata); - if ( client == NULL ) { - (void) fprintf(stderr, "error on new client...\n"); - return NULL; - } - - int err = mosquitto_username_pw_set(client, username, password); - if ( err != MOSQ_ERR_SUCCESS ) { - (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); - mosquitto_destroy(client); - return NULL; - } - - mosquitto_message_callback_set(client, callback); - - err = mosquitto_connect(client, hostname, port, KEEPALIVE); - if ( err != MOSQ_ERR_SUCCESS ) { - (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); - mosquitto_destroy(client); - return NULL; - } - - return client; -} - -static void -run(struct mosquitto *client, const char *topics[], const volatile bool *quit) -{ - for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { - mosquitto_subscribe(client, NULL, topics[idx], 0); - } - - while ( !*quit ) { - static const int defaultTimeout = -1; // 1000ms - - int err = mosquitto_loop(client, defaultTimeout, 1); - if ( !terminate && err ) { - (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err)); - sleep(WAIT_UNTIL_RECONNECT); // NOLINT - mosquitto_reconnect(client); - } - } -} - -static void -shutdown(struct mosquitto *client) -{ - mosquitto_disconnect(client); - mosquitto_destroy(client); -} - static void callback(struct mosquitto *client, void *userdata, const struct mosquitto_message *message) { diff --git a/src/mqtt.c b/src/mqtt.c new file mode 100644 index 0000000..50cfa6a --- /dev/null +++ b/src/mqtt.c @@ -0,0 +1,69 @@ +// Standard C +#include + +// System +#include + +// Project +#include "config.h" +#include "mqtt.h" + +struct mosquitto * +connect(const char *clientid, // NOLINT + const char *username, // NOLINT + const char *password, // NOLINT + const char *hostname, // NOLINT + int port, + void callback(struct mosquitto *, void *, const struct mosquitto_message *), + void *userdata) +{ + struct mosquitto *client = mosquitto_new(clientid, true, userdata); + if ( client == NULL ) { + (void) fprintf(stderr, "error on new client...\n"); + return NULL; + } + + int err = mosquitto_username_pw_set(client, username, password); + if ( err != MOSQ_ERR_SUCCESS ) { + (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); + mosquitto_destroy(client); + return NULL; + } + + mosquitto_message_callback_set(client, callback); + + err = mosquitto_connect(client, hostname, port, KEEPALIVE); + if ( err != MOSQ_ERR_SUCCESS ) { + (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); + mosquitto_destroy(client); + return NULL; + } + + return client; +} + +void +run(struct mosquitto *client, const char *topics[], const volatile bool *quit) +{ + for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { + mosquitto_subscribe(client, NULL, topics[idx], 0); + } + + while ( !*quit ) { + static const int defaultTimeout = -1; // 1000ms + + int err = mosquitto_loop(client, defaultTimeout, 1); + if ( !*quit && err ) { + (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err)); + sleep(WAIT_UNTIL_RECONNECT); // NOLINT + mosquitto_reconnect(client); + } + } +} + +void +shutdown(struct mosquitto *client) +{ + mosquitto_disconnect(client); + mosquitto_destroy(client); +} -- cgit v1.3