summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-11-04 16:18:20 +0100
committerThomas Schmucker <ts@its1.de>2024-11-04 16:18:20 +0100
commit9e339087e328d445f38173401f77502297a638f4 (patch)
tree776bda843c2204df08b0fb99580418af13d42f0f
parent6d804810804e09c30b348ce7287ac175e41d7881 (diff)
downloaduse-mosquitto-9e339087e328d445f38173401f77502297a638f4.tar.gz
use-mosquitto-9e339087e328d445f38173401f77502297a638f4.tar.bz2
use-mosquitto-9e339087e328d445f38173401f77502297a638f4.zip
Verschiebe callback-handling nach 'mqtt_run'
-rw-r--r--include/mqtt.h6
-rw-r--r--src/mqtt.c14
-rw-r--r--src/publish.c1
-rw-r--r--src/subscribe.c6
4 files changed, 16 insertions, 11 deletions
diff --git a/include/mqtt.h b/include/mqtt.h
index 8dad5eb..c0c612d 100644
--- a/include/mqtt.h
+++ b/include/mqtt.h
@@ -8,9 +8,11 @@ struct mosquitto *mqtt_connect(const char *clientid, // NOLINT
8 const char *password, // NOLINT 8 const char *password, // NOLINT
9 const char *hostname, // NOLINT 9 const char *hostname, // NOLINT
10 int port, 10 int port,
11 void callback(struct mosquitto *, void *, const struct mosquitto_message *),
12 void *userdata); 11 void *userdata);
13 12
14void mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit); 13void mqtt_run(struct mosquitto *client,
14 const char *topics[],
15 void callback(struct mosquitto *, void *, const struct mosquitto_message *),
16 const volatile bool *quit);
15 17
16void mqtt_shutdown(struct mosquitto *client); 18void mqtt_shutdown(struct mosquitto *client);
diff --git a/src/mqtt.c b/src/mqtt.c
index 32e2ea9..34e7ee8 100644
--- a/src/mqtt.c
+++ b/src/mqtt.c
@@ -14,7 +14,6 @@ mqtt_connect(const char *clientid, // NOLINT
14 const char *password, // NOLINT 14 const char *password, // NOLINT
15 const char *hostname, // NOLINT 15 const char *hostname, // NOLINT
16 int port, 16 int port,
17 void callback(struct mosquitto *, void *, const struct mosquitto_message *),
18 void *userdata) 17 void *userdata)
19{ 18{
20 struct mosquitto *client = mosquitto_new(clientid, true, userdata); 19 struct mosquitto *client = mosquitto_new(clientid, true, userdata);
@@ -30,10 +29,6 @@ mqtt_connect(const char *clientid, // NOLINT
30 return NULL; 29 return NULL;
31 } 30 }
32 31
33 if ( callback ) {
34 mosquitto_message_callback_set(client, callback);
35 }
36
37 err = mosquitto_connect(client, hostname, port, KEEPALIVE); 32 err = mosquitto_connect(client, hostname, port, KEEPALIVE);
38 if ( err != MOSQ_ERR_SUCCESS ) { 33 if ( err != MOSQ_ERR_SUCCESS ) {
39 (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); 34 (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err));
@@ -45,8 +40,15 @@ mqtt_connect(const char *clientid, // NOLINT
45} 40}
46 41
47void 42void
48mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) 43mqtt_run(struct mosquitto *client,
44 const char *topics[],
45 void callback(struct mosquitto *, void *, const struct mosquitto_message *),
46 const volatile bool *quit)
49{ 47{
48 if ( callback ) {
49 mosquitto_message_callback_set(client, callback);
50 }
51
50 int *mid = NULL; 52 int *mid = NULL;
51 53
52 for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { 54 for ( size_t idx = 0; topics[idx] != NULL; ++idx ) {
diff --git a/src/publish.c b/src/publish.c
index 6afc5b4..cc19502 100644
--- a/src/publish.c
+++ b/src/publish.c
@@ -45,7 +45,6 @@ main(void)
45 PASSWORD, 45 PASSWORD,
46 HOSTNAME, 46 HOSTNAME,
47 PORT, 47 PORT,
48 NULL,
49 NULL); 48 NULL);
50 49
51 if ( !client ) { 50 if ( !client ) {
diff --git a/src/subscribe.c b/src/subscribe.c
index e7d5883..b016283 100644
--- a/src/subscribe.c
+++ b/src/subscribe.c
@@ -65,14 +65,16 @@ main(void)
65 PASSWORD, 65 PASSWORD,
66 HOSTNAME, 66 HOSTNAME,
67 PORT, 67 PORT,
68 callback,
69 NULL); 68 NULL);
70 69
71 if ( !client ) { 70 if ( !client ) {
72 return EXIT_FAILURE; 71 return EXIT_FAILURE;
73 } 72 }
74 73
75 mqtt_run(client, (const char *[]){ TOPIC, "testtopic", NULL }, &terminate); 74 mqtt_run(client,
75 (const char *[]){ TOPIC, "testtopic", NULL },
76 callback,
77 &terminate);
76 78
77 mqtt_shutdown(client); 79 mqtt_shutdown(client);
78 80