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 --- src/publish.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/publish.c (limited to 'src/publish.c') 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; +} -- cgit v1.3