summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--makefile9
-rw-r--r--src/mqtt.c12
-rw-r--r--src/publish.c99
-rw-r--r--src/subscribe.c2
4 files changed, 117 insertions, 5 deletions
diff --git a/makefile b/makefile
index 03e0931..32d07ef 100644
--- a/makefile
+++ b/makefile
@@ -1,18 +1,23 @@
1include config.mk 1include config.mk
2 2
3SUBSCRIBE=bin/subscribe 3SUBSCRIBE=bin/subscribe
4PUBLISH=bin/publish
4 5
5SUBSCRIBE_OBJ_FILES=obj/subscribe.o obj/mqtt.o 6SUBSCRIBE_OBJ_FILES=obj/subscribe.o obj/mqtt.o
7PUBLISH_OBJ_FILES=obj/publish.o obj/mqtt.o
6 8
7release: $(SUBSCRIBE) 9release: $(SUBSCRIBE) $(PUBLISH)
8release: CFLAGS+=-DNDEBUG -O2 10release: CFLAGS+=-DNDEBUG -O2
9 11
10debug: $(SUBSCRIBE) 12debug: $(SUBSCRIBE) $(PUBLISH)
11debug: CFLAGS+=-g 13debug: CFLAGS+=-g
12 14
13$(SUBSCRIBE): $(SUBSCRIBE_OBJ_FILES) | bin 15$(SUBSCRIBE): $(SUBSCRIBE_OBJ_FILES) | bin
14 cc $(LDFLAGS) $^ -o $@ 16 cc $(LDFLAGS) $^ -o $@
15 17
18$(PUBLISH): $(PUBLISH_OBJ_FILES) | bin
19 cc $(LDFLAGS) $^ -o $@
20
16obj/%.o: src/%.c src/config.h | obj 21obj/%.o: src/%.c src/config.h | obj
17 cc $(CFLAGS) -c $< -o $@ 22 cc $(CFLAGS) -c $< -o $@
18 23
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
30 return NULL; 30 return NULL;
31 } 31 }
32 32
33 mosquitto_message_callback_set(client, callback); 33 if ( callback ) {
34 mosquitto_message_callback_set(client, callback);
35 }
34 36
35 err = mosquitto_connect(client, hostname, port, KEEPALIVE); 37 err = mosquitto_connect(client, hostname, port, KEEPALIVE);
36 if ( err != MOSQ_ERR_SUCCESS ) { 38 if ( err != MOSQ_ERR_SUCCESS ) {
@@ -45,8 +47,10 @@ mqtt_connect(const char *clientid, // NOLINT
45void 47void
46mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) 48mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit)
47{ 49{
50 int *mid = NULL;
51
48 for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { 52 for ( size_t idx = 0; topics[idx] != NULL; ++idx ) {
49 mosquitto_subscribe(client, NULL, topics[idx], 0); 53 mosquitto_subscribe(client, mid, topics[idx], 0);
50 } 54 }
51 55
52 while ( !*quit ) { 56 while ( !*quit ) {
@@ -59,6 +63,10 @@ mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *qu
59 mosquitto_reconnect(client); 63 mosquitto_reconnect(client);
60 } 64 }
61 } 65 }
66
67 for ( size_t idx = 0; topics[idx] != NULL; ++idx ) {
68 mosquitto_unsubscribe(client, mid, topics[idx]);
69 }
62} 70}
63 71
64void 72void
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}
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)
72 return EXIT_FAILURE; 72 return EXIT_FAILURE;
73 } 73 }
74 74
75 mqtt_run(client, (const char *[]){ TOPIC, NULL }, &terminate); 75 mqtt_run(client, (const char *[]){ TOPIC, "testtopic", NULL }, &terminate);
76 76
77 mqtt_shutdown(client); 77 mqtt_shutdown(client);
78 78