diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-10-29 17:08:19 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-10-29 17:08:19 +0100 |
| commit | dcc35d185796138838e6165dee4ef5d15d7e1d4c (patch) | |
| tree | f1b38876c8ff305760c6dfe818e270cc1c441bae | |
| parent | 2458e6aa1658b2cc5b6349b89f1276873554ad6f (diff) | |
| download | use-mosquitto-dcc35d185796138838e6165dee4ef5d15d7e1d4c.tar.gz use-mosquitto-dcc35d185796138838e6165dee4ef5d15d7e1d4c.tar.bz2 use-mosquitto-dcc35d185796138838e6165dee4ef5d15d7e1d4c.zip | |
Benutze bessere Namen für Variablen und Argument
| -rw-r--r-- | config.h.in | 8 | ||||
| -rw-r--r-- | src/main.c | 58 |
2 files changed, 41 insertions, 25 deletions
diff --git a/config.h.in b/config.h.in index 0cf0d70..aca08e7 100644 --- a/config.h.in +++ b/config.h.in | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #ifndef CLIENT_ID | 3 | #ifndef CLIENTID |
| 4 | # define CLIENT_ID "my-client-id" | 4 | # define CLIENTID "my-client-id" |
| 5 | #endif | 5 | #endif |
| 6 | 6 | ||
| 7 | #ifndef HOST | 7 | #ifndef HOSTNAME |
| 8 | # define HOST "localhost" | 8 | # define HOSTNAME "localhost" |
| 9 | #endif | 9 | #endif |
| 10 | 10 | ||
| 11 | #ifndef PORT | 11 | #ifndef PORT |
| @@ -41,35 +41,31 @@ init(void) | |||
| 41 | (void) atexit(cleanup); | 41 | (void) atexit(cleanup); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | static void | ||
| 45 | message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) | ||
| 46 | { | ||
| 47 | UNUSED(obj); | ||
| 48 | UNUSED(mosq); | ||
| 49 | printf("message '%.*s' for topic '%s'\n", | ||
| 50 | message->payloadlen, (const char *) message->payload, | ||
| 51 | message->topic); | ||
| 52 | } | ||
| 53 | |||
| 54 | static struct mosquitto * | 44 | static struct mosquitto * |
| 55 | setup(void) | 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) | ||
| 56 | { | 52 | { |
| 57 | struct mosquitto *client = mosquitto_new(CLIENT_ID, true, NULL); | 53 | struct mosquitto *client = mosquitto_new(clientid, true, userdata); |
| 58 | if ( client == NULL ) { | 54 | if ( client == NULL ) { |
| 59 | (void) fprintf(stderr, "error on new client...\n"); | 55 | (void) fprintf(stderr, "error on new client...\n"); |
| 60 | return NULL; | 56 | return NULL; |
| 61 | } | 57 | } |
| 62 | 58 | ||
| 63 | int err = mosquitto_username_pw_set(client, USERNAME, PASSWORD); | 59 | int err = mosquitto_username_pw_set(client, username, password); |
| 64 | if ( err != MOSQ_ERR_SUCCESS ) { | 60 | if ( err != MOSQ_ERR_SUCCESS ) { |
| 65 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); | 61 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); |
| 66 | mosquitto_destroy(client); | 62 | mosquitto_destroy(client); |
| 67 | return NULL; | 63 | return NULL; |
| 68 | } | 64 | } |
| 69 | 65 | ||
| 70 | mosquitto_message_callback_set(client, message_callback); | 66 | mosquitto_message_callback_set(client, callback); |
| 71 | 67 | ||
| 72 | err = mosquitto_connect(client, HOST, PORT, KEEPALIVE); | 68 | err = mosquitto_connect(client, hostname, port, KEEPALIVE); |
| 73 | if ( err != MOSQ_ERR_SUCCESS ) { | 69 | if ( err != MOSQ_ERR_SUCCESS ) { |
| 74 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); | 70 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); |
| 75 | mosquitto_destroy(client); | 71 | mosquitto_destroy(client); |
| @@ -80,9 +76,9 @@ setup(void) | |||
| 80 | } | 76 | } |
| 81 | 77 | ||
| 82 | static void | 78 | static void |
| 83 | run(struct mosquitto *client) | 79 | run(struct mosquitto *client, const char *topic) |
| 84 | { | 80 | { |
| 85 | mosquitto_subscribe(client, NULL, TOPIC, 0); | 81 | mosquitto_subscribe(client, NULL, topic, 0); |
| 86 | 82 | ||
| 87 | while ( !terminate ) { | 83 | while ( !terminate ) { |
| 88 | static const int defaultTimeout = -1; // 1000ms | 84 | static const int defaultTimeout = -1; // 1000ms |
| @@ -103,16 +99,36 @@ shutdown(struct mosquitto *client) | |||
| 103 | mosquitto_destroy(client); | 99 | mosquitto_destroy(client); |
| 104 | } | 100 | } |
| 105 | 101 | ||
| 102 | static void | ||
| 103 | callback(struct mosquitto *client, void *userdata, const struct mosquitto_message *message) | ||
| 104 | { | ||
| 105 | UNUSED(client); | ||
| 106 | UNUSED(userdata); | ||
| 107 | printf("message '%.*s' for topic '%s'\n", | ||
| 108 | message->payloadlen, (const char *) message->payload, | ||
| 109 | message->topic); | ||
| 110 | } | ||
| 111 | |||
| 106 | int | 112 | int |
| 107 | main(void) | 113 | main(void) |
| 108 | { | 114 | { |
| 109 | init(); | 115 | init(); |
| 110 | 116 | ||
| 111 | struct mosquitto *client = setup(); | 117 | struct mosquitto *client = connect( |
| 112 | if ( client ) { | 118 | CLIENT_ID, |
| 113 | run(client); | 119 | USERNAME, |
| 114 | shutdown(client); | 120 | PASSWORD, |
| 121 | HOST, | ||
| 122 | PORT, | ||
| 123 | callback, | ||
| 124 | NULL); | ||
| 125 | |||
| 126 | if ( !client ) { | ||
| 127 | return EXIT_FAILURE; | ||
| 115 | } | 128 | } |
| 116 | 129 | ||
| 130 | run(client, TOPIC); | ||
| 131 | shutdown(client); | ||
| 132 | |||
| 117 | return EXIT_SUCCESS; | 133 | return EXIT_SUCCESS; |
| 118 | } | 134 | } |
