summaryrefslogtreecommitdiff
path: root/src/publish.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/publish.c')
-rw-r--r--src/publish.c99
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
18static bool
19streq(const char *lhs, const char *rhs)
20{
21 return strcmp(lhs, rhs) == 0;
22}
23
24static void
25cleanup_app(void)
26{
27 mosquitto_lib_cleanup();
28}
29
30static void
31init_app(void)
32{
33 mosquitto_lib_init();
34 (void) atexit(cleanup_app);
35}
36
37int
38main(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}