diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-03 22:07:54 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-03 22:07:54 +0100 |
| commit | 6d804810804e09c30b348ce7287ac175e41d7881 (patch) | |
| tree | e9cb4164bac336ab3af90dc01650e9d0972a2cc5 /src/publish.c | |
| parent | f15530cbfa7b2e344844a36e1a199301a81b6383 (diff) | |
| download | use-mosquitto-6d804810804e09c30b348ce7287ac175e41d7881.tar.gz use-mosquitto-6d804810804e09c30b348ce7287ac175e41d7881.tar.bz2 use-mosquitto-6d804810804e09c30b348ce7287ac175e41d7881.zip | |
publisher hinzugefügt
Diffstat (limited to 'src/publish.c')
| -rw-r--r-- | src/publish.c | 99 |
1 files changed, 99 insertions, 0 deletions
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 @@ | |||
| 1 | // Standard C | ||
| 2 | #include <signal.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | // System | ||
| 9 | #include <unistd.h> | ||
| 10 | |||
| 11 | // Mosquitto | ||
| 12 | #include <mosquitto.h> | ||
| 13 | |||
| 14 | // Project | ||
| 15 | #include "config.h" | ||
| 16 | #include "mqtt.h" | ||
| 17 | |||
| 18 | static bool | ||
| 19 | streq(const char *lhs, const char *rhs) | ||
| 20 | { | ||
| 21 | return strcmp(lhs, rhs) == 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | static void | ||
| 25 | cleanup_app(void) | ||
| 26 | { | ||
| 27 | mosquitto_lib_cleanup(); | ||
| 28 | } | ||
| 29 | |||
| 30 | static void | ||
| 31 | init_app(void) | ||
| 32 | { | ||
| 33 | mosquitto_lib_init(); | ||
| 34 | (void) atexit(cleanup_app); | ||
| 35 | } | ||
| 36 | |||
| 37 | int | ||
| 38 | main(void) | ||
| 39 | { | ||
| 40 | init_app(); | ||
| 41 | |||
| 42 | struct mosquitto *client = mqtt_connect( | ||
| 43 | NULL, | ||
| 44 | USERNAME, | ||
| 45 | PASSWORD, | ||
| 46 | HOSTNAME, | ||
| 47 | PORT, | ||
| 48 | NULL, | ||
| 49 | NULL); | ||
| 50 | |||
| 51 | if ( !client ) { | ||
| 52 | return EXIT_FAILURE; | ||
| 53 | } | ||
| 54 | |||
| 55 | // logic | ||
| 56 | for ( bool quit = false; !quit; ) { | ||
| 57 | printf("> "); | ||
| 58 | |||
| 59 | char command[100] = { 0 }; // NOLINT | ||
| 60 | if ( fgets(command, sizeof command, stdin) == NULL ) { | ||
| 61 | quit = true; | ||
| 62 | continue; | ||
| 63 | } | ||
| 64 | |||
| 65 | // remove 'newline' symbol at the end | ||
| 66 | command[strcspn(command, "\n")] = '\0'; | ||
| 67 | |||
| 68 | if ( streq(command, "") ) { | ||
| 69 | ; // do nothing | ||
| 70 | } | ||
| 71 | else if ( streq(command, "quit") ) { | ||
| 72 | quit = true; | ||
| 73 | } | ||
| 74 | else if ( streq(command, "send") ) { | ||
| 75 | static const char payload[] = "ich bin ein payload"; | ||
| 76 | static const char topic[] = "testtopic"; | ||
| 77 | |||
| 78 | int err = mosquitto_publish( | ||
| 79 | client, | ||
| 80 | NULL, | ||
| 81 | topic, | ||
| 82 | sizeof payload, | ||
| 83 | payload, | ||
| 84 | 0, | ||
| 85 | false); | ||
| 86 | |||
| 87 | if ( err ) { | ||
| 88 | (void) fprintf(stderr, "error on send: %s\n", mosquitto_strerror(err)); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | (void) fprintf(stderr, "unknown command: '%s'\n", command); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | mqtt_shutdown(client); | ||
| 97 | |||
| 98 | return EXIT_SUCCESS; | ||
| 99 | } | ||
