diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-10-29 22:34:23 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-10-29 22:34:23 +0100 |
| commit | 868d0a1ecbfbcd1f04b5c03403d6cffb439ce8fe (patch) | |
| tree | e992bb530f623e53df9fe15ef1cdbdfc3128092a /src/main.c | |
| parent | 0714998ef54e8c1a46af7ec48698faa3abdf457a (diff) | |
| download | use-mosquitto-868d0a1ecbfbcd1f04b5c03403d6cffb439ce8fe.tar.gz use-mosquitto-868d0a1ecbfbcd1f04b5c03403d6cffb439ce8fe.tar.bz2 use-mosquitto-868d0a1ecbfbcd1f04b5c03403d6cffb439ce8fe.zip | |
Verschiebe MQTT Code in ein eigenes Modul
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 63 |
1 files changed, 1 insertions, 62 deletions
| @@ -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 | ||
| 44 | static struct mosquitto * | ||
| 45 | connect(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 | |||
| 78 | static void | ||
| 79 | run(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 | |||
| 97 | static void | ||
| 98 | shutdown(struct mosquitto *client) | ||
| 99 | { | ||
| 100 | mosquitto_disconnect(client); | ||
| 101 | mosquitto_destroy(client); | ||
| 102 | } | ||
| 103 | |||
| 104 | static void | 43 | static void |
| 105 | callback(struct mosquitto *client, void *userdata, const struct mosquitto_message *message) | 44 | callback(struct mosquitto *client, void *userdata, const struct mosquitto_message *message) |
| 106 | { | 45 | { |
