summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c63
-rw-r--r--src/mqtt.c69
2 files changed, 70 insertions, 62 deletions
diff --git a/src/main.c b/src/main.c
index 8a25176..5bef36b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,9 +8,8 @@
8#include <unistd.h> 8#include <unistd.h>
9 9
10// Project 10// Project
11#include <mosquitto.h>
12
13#include "config.h" 11#include "config.h"
12#include "mqtt.h"
14 13
15#ifndef UNUSED 14#ifndef UNUSED
16# define UNUSED(x) (void) (x) 15# define UNUSED(x) (void) (x)
@@ -41,66 +40,6 @@ init(void)
41 (void) atexit(cleanup); 40 (void) atexit(cleanup);
42} 41}
43 42
44static struct mosquitto *
45connect(const char *clientid, // NOLINT
46 const char *username, // NOLINT
47 const char *password, // NOLINT
48 const char *hostname, // NOLINT
49 int port,
50 void callback(struct mosquitto *, void *, const struct mosquitto_message *),
51 void *userdata)
52{
53 struct mosquitto *client = mosquitto_new(clientid, true, userdata);
54 if ( client == NULL ) {
55 (void) fprintf(stderr, "error on new client...\n");
56 return NULL;
57 }
58
59 int err = mosquitto_username_pw_set(client, username, password);
60 if ( err != MOSQ_ERR_SUCCESS ) {
61 (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err));
62 mosquitto_destroy(client);
63 return NULL;
64 }
65
66 mosquitto_message_callback_set(client, callback);
67
68 err = mosquitto_connect(client, hostname, port, KEEPALIVE);
69 if ( err != MOSQ_ERR_SUCCESS ) {
70 (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err));
71 mosquitto_destroy(client);
72 return NULL;
73 }
74
75 return client;
76}
77
78static void
79run(struct mosquitto *client, const char *topics[], const volatile bool *quit)
80{
81 for ( size_t idx = 0; topics[idx] != NULL; ++idx ) {
82 mosquitto_subscribe(client, NULL, topics[idx], 0);
83 }
84
85 while ( !*quit ) {
86 static const int defaultTimeout = -1; // 1000ms
87
88 int err = mosquitto_loop(client, defaultTimeout, 1);
89 if ( !terminate && err ) {
90 (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err));
91 sleep(WAIT_UNTIL_RECONNECT); // NOLINT
92 mosquitto_reconnect(client);
93 }
94 }
95}
96
97static void
98shutdown(struct mosquitto *client)
99{
100 mosquitto_disconnect(client);
101 mosquitto_destroy(client);
102}
103
104static void 43static void
105callback(struct mosquitto *client, void *userdata, const struct mosquitto_message *message) 44callback(struct mosquitto *client, void *userdata, const struct mosquitto_message *message)
106{ 45{
diff --git a/src/mqtt.c b/src/mqtt.c
new file mode 100644
index 0000000..50cfa6a
--- /dev/null
+++ b/src/mqtt.c
@@ -0,0 +1,69 @@
1// Standard C
2#include <stdio.h>
3
4// System
5#include <unistd.h>
6
7// Project
8#include "config.h"
9#include "mqtt.h"
10
11struct mosquitto *
12connect(const char *clientid, // NOLINT
13 const char *username, // NOLINT
14 const char *password, // NOLINT
15 const char *hostname, // NOLINT
16 int port,
17 void callback(struct mosquitto *, void *, const struct mosquitto_message *),
18 void *userdata)
19{
20 struct mosquitto *client = mosquitto_new(clientid, true, userdata);
21 if ( client == NULL ) {
22 (void) fprintf(stderr, "error on new client...\n");
23 return NULL;
24 }
25
26 int err = mosquitto_username_pw_set(client, username, password);
27 if ( err != MOSQ_ERR_SUCCESS ) {
28 (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err));
29 mosquitto_destroy(client);
30 return NULL;
31 }
32
33 mosquitto_message_callback_set(client, callback);
34
35 err = mosquitto_connect(client, hostname, port, KEEPALIVE);
36 if ( err != MOSQ_ERR_SUCCESS ) {
37 (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err));
38 mosquitto_destroy(client);
39 return NULL;
40 }
41
42 return client;
43}
44
45void
46run(struct mosquitto *client, const char *topics[], const volatile bool *quit)
47{
48 for ( size_t idx = 0; topics[idx] != NULL; ++idx ) {
49 mosquitto_subscribe(client, NULL, topics[idx], 0);
50 }
51
52 while ( !*quit ) {
53 static const int defaultTimeout = -1; // 1000ms
54
55 int err = mosquitto_loop(client, defaultTimeout, 1);
56 if ( !*quit && err ) {
57 (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err));
58 sleep(WAIT_UNTIL_RECONNECT); // NOLINT
59 mosquitto_reconnect(client);
60 }
61 }
62}
63
64void
65shutdown(struct mosquitto *client)
66{
67 mosquitto_disconnect(client);
68 mosquitto_destroy(client);
69}