diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-04 16:18:20 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-04 16:18:20 +0100 |
| commit | 9e339087e328d445f38173401f77502297a638f4 (patch) | |
| tree | 776bda843c2204df08b0fb99580418af13d42f0f /src | |
| parent | 6d804810804e09c30b348ce7287ac175e41d7881 (diff) | |
| download | use-mosquitto-9e339087e328d445f38173401f77502297a638f4.tar.gz use-mosquitto-9e339087e328d445f38173401f77502297a638f4.tar.bz2 use-mosquitto-9e339087e328d445f38173401f77502297a638f4.zip | |
Verschiebe callback-handling nach 'mqtt_run'
Diffstat (limited to 'src')
| -rw-r--r-- | src/mqtt.c | 14 | ||||
| -rw-r--r-- | src/publish.c | 1 | ||||
| -rw-r--r-- | src/subscribe.c | 6 |
3 files changed, 12 insertions, 9 deletions
| @@ -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 | ||
| 47 | void | 42 | void |
| 48 | mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) | 43 | mqtt_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 | ||
