From 6d804810804e09c30b348ce7287ac175e41d7881 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 3 Nov 2024 22:07:54 +0100 Subject: publisher hinzugefügt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- makefile | 9 ++++-- src/mqtt.c | 12 +++++-- src/publish.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/subscribe.c | 2 +- 4 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 src/publish.c diff --git a/makefile b/makefile index 03e0931..32d07ef 100644 --- a/makefile +++ b/makefile @@ -1,18 +1,23 @@ include config.mk SUBSCRIBE=bin/subscribe +PUBLISH=bin/publish SUBSCRIBE_OBJ_FILES=obj/subscribe.o obj/mqtt.o +PUBLISH_OBJ_FILES=obj/publish.o obj/mqtt.o -release: $(SUBSCRIBE) +release: $(SUBSCRIBE) $(PUBLISH) release: CFLAGS+=-DNDEBUG -O2 -debug: $(SUBSCRIBE) +debug: $(SUBSCRIBE) $(PUBLISH) debug: CFLAGS+=-g $(SUBSCRIBE): $(SUBSCRIBE_OBJ_FILES) | bin cc $(LDFLAGS) $^ -o $@ +$(PUBLISH): $(PUBLISH_OBJ_FILES) | bin + cc $(LDFLAGS) $^ -o $@ + obj/%.o: src/%.c src/config.h | obj cc $(CFLAGS) -c $< -o $@ diff --git a/src/mqtt.c b/src/mqtt.c index ce89b0b..32e2ea9 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -30,7 +30,9 @@ mqtt_connect(const char *clientid, // NOLINT return NULL; } - mosquitto_message_callback_set(client, callback); + if ( callback ) { + mosquitto_message_callback_set(client, callback); + } err = mosquitto_connect(client, hostname, port, KEEPALIVE); if ( err != MOSQ_ERR_SUCCESS ) { @@ -45,8 +47,10 @@ mqtt_connect(const char *clientid, // NOLINT void mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) { + int *mid = NULL; + for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { - mosquitto_subscribe(client, NULL, topics[idx], 0); + mosquitto_subscribe(client, mid, topics[idx], 0); } while ( !*quit ) { @@ -59,6 +63,10 @@ mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *qu mosquitto_reconnect(client); } } + + for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { + mosquitto_unsubscribe(client, mid, topics[idx]); + } } void diff --git a/src/publish.c b/src/publish.c new file mode 100644 index 0000000..6afc5b4 --- /dev/null +++ b/src/publish.c @@ -0,0 +1,99 @@ +// Standard C +#include +#include +#include +#include +#include + +// System +#include + +// Mosquitto +#include + +// Project +#include "config.h" +#include "mqtt.h" + +static bool +streq(const char *lhs, const char *rhs) +{ + return strcmp(lhs, rhs) == 0; +} + +static void +cleanup_app(void) +{ + mosquitto_lib_cleanup(); +} + +static void +init_app(void) +{ + mosquitto_lib_init(); + (void) atexit(cleanup_app); +} + +int +main(void) +{ + init_app(); + + struct mosquitto *client = mqtt_connect( + NULL, + USERNAME, + PASSWORD, + HOSTNAME, + PORT, + NULL, + NULL); + + if ( !client ) { + return EXIT_FAILURE; + } + + // logic + for ( bool quit = false; !quit; ) { + printf("> "); + + char command[100] = { 0 }; // NOLINT + if ( fgets(command, sizeof command, stdin) == NULL ) { + quit = true; + continue; + } + + // remove 'newline' symbol at the end + command[strcspn(command, "\n")] = '\0'; + + if ( streq(command, "") ) { + ; // do nothing + } + else if ( streq(command, "quit") ) { + quit = true; + } + else if ( streq(command, "send") ) { + static const char payload[] = "ich bin ein payload"; + static const char topic[] = "testtopic"; + + int err = mosquitto_publish( + client, + NULL, + topic, + sizeof payload, + payload, + 0, + false); + + if ( err ) { + (void) fprintf(stderr, "error on send: %s\n", mosquitto_strerror(err)); + } + } + else { + (void) fprintf(stderr, "unknown command: '%s'\n", command); + } + } + + mqtt_shutdown(client); + + return EXIT_SUCCESS; +} diff --git a/src/subscribe.c b/src/subscribe.c index 82e7606..e7d5883 100644 --- a/src/subscribe.c +++ b/src/subscribe.c @@ -72,7 +72,7 @@ main(void) return EXIT_FAILURE; } - mqtt_run(client, (const char *[]){ TOPIC, NULL }, &terminate); + mqtt_run(client, (const char *[]){ TOPIC, "testtopic", NULL }, &terminate); mqtt_shutdown(client); -- cgit v1.3