From dcc35d185796138838e6165dee4ef5d15d7e1d4c Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Tue, 29 Oct 2024 17:08:19 +0100 Subject: Benutze bessere Namen für Variablen und Argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 58 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index a181171..1b5bac7 100644 --- a/src/main.c +++ b/src/main.c @@ -41,35 +41,31 @@ init(void) (void) atexit(cleanup); } -static void -message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) -{ - UNUSED(obj); - UNUSED(mosq); - printf("message '%.*s' for topic '%s'\n", - message->payloadlen, (const char *) message->payload, - message->topic); -} - static struct mosquitto * -setup(void) +connect(const char *clientid, // NOLINT + const char *username, // 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(CLIENT_ID, true, NULL); + struct mosquitto *client = mosquitto_new(clientid, true, userdata); if ( client == NULL ) { (void) fprintf(stderr, "error on new client...\n"); return NULL; } - int err = mosquitto_username_pw_set(client, USERNAME, PASSWORD); + int err = mosquitto_username_pw_set(client, username, password); if ( err != MOSQ_ERR_SUCCESS ) { (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); mosquitto_destroy(client); return NULL; } - mosquitto_message_callback_set(client, message_callback); + mosquitto_message_callback_set(client, callback); - err = mosquitto_connect(client, HOST, PORT, KEEPALIVE); + err = mosquitto_connect(client, hostname, port, KEEPALIVE); if ( err != MOSQ_ERR_SUCCESS ) { (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); mosquitto_destroy(client); @@ -80,9 +76,9 @@ setup(void) } static void -run(struct mosquitto *client) +run(struct mosquitto *client, const char *topic) { - mosquitto_subscribe(client, NULL, TOPIC, 0); + mosquitto_subscribe(client, NULL, topic, 0); while ( !terminate ) { static const int defaultTimeout = -1; // 1000ms @@ -103,16 +99,36 @@ shutdown(struct mosquitto *client) mosquitto_destroy(client); } +static void +callback(struct mosquitto *client, void *userdata, const struct mosquitto_message *message) +{ + UNUSED(client); + UNUSED(userdata); + printf("message '%.*s' for topic '%s'\n", + message->payloadlen, (const char *) message->payload, + message->topic); +} + int main(void) { init(); - struct mosquitto *client = setup(); - if ( client ) { - run(client); - shutdown(client); + struct mosquitto *client = connect( + CLIENT_ID, + USERNAME, + PASSWORD, + HOST, + PORT, + callback, + NULL); + + if ( !client ) { + return EXIT_FAILURE; } + run(client, TOPIC); + shutdown(client); + return EXIT_SUCCESS; } -- cgit v1.3