From 9e339087e328d445f38173401f77502297a638f4 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Mon, 4 Nov 2024 16:18:20 +0100 Subject: Verschiebe callback-handling nach 'mqtt_run' --- include/mqtt.h | 6 ++++-- src/mqtt.c | 14 ++++++++------ src/publish.c | 1 - src/subscribe.c | 6 ++++-- 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 const char *password, // NOLINT const char *hostname, // NOLINT int port, - void callback(struct mosquitto *, void *, const struct mosquitto_message *), void *userdata); -void mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit); +void mqtt_run(struct mosquitto *client, + const char *topics[], + void callback(struct mosquitto *, void *, const struct mosquitto_message *), + const volatile bool *quit); void 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 const char *password, // NOLINT const char *hostname, // NOLINT int port, - void callback(struct mosquitto *, void *, const struct mosquitto_message *), void *userdata) { struct mosquitto *client = mosquitto_new(clientid, true, userdata); @@ -30,10 +29,6 @@ mqtt_connect(const char *clientid, // NOLINT return NULL; } - if ( callback ) { - mosquitto_message_callback_set(client, callback); - } - err = mosquitto_connect(client, hostname, port, KEEPALIVE); if ( err != MOSQ_ERR_SUCCESS ) { (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); @@ -45,8 +40,15 @@ mqtt_connect(const char *clientid, // NOLINT } void -mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) +mqtt_run(struct mosquitto *client, + const char *topics[], + void callback(struct mosquitto *, void *, const struct mosquitto_message *), + const volatile bool *quit) { + if ( callback ) { + mosquitto_message_callback_set(client, callback); + } + int *mid = NULL; 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) PASSWORD, HOSTNAME, PORT, - NULL, NULL); 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) PASSWORD, HOSTNAME, PORT, - callback, NULL); if ( !client ) { return EXIT_FAILURE; } - mqtt_run(client, (const char *[]){ TOPIC, "testtopic", NULL }, &terminate); + mqtt_run(client, + (const char *[]){ TOPIC, "testtopic", NULL }, + callback, + &terminate); mqtt_shutdown(client); -- cgit v1.3