diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-10-25 17:12:34 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-10-25 17:12:34 +0200 |
| commit | 5bd0a0cd5e6951723398aadc601f1fcc2a6187b8 (patch) | |
| tree | aaf18f8697579eea7a0d20c81c92f09df97591cd /src/main.c | |
| parent | f2f0a815ccdecb9c65c6d661f9b57cc8ba0ab81e (diff) | |
| download | use-mosquitto-5bd0a0cd5e6951723398aadc601f1fcc2a6187b8.tar.gz use-mosquitto-5bd0a0cd5e6951723398aadc601f1fcc2a6187b8.tar.bz2 use-mosquitto-5bd0a0cd5e6951723398aadc601f1fcc2a6187b8.zip | |
Teile den Code in einzelne Funktionen auf
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 59 |
1 files changed, 42 insertions, 17 deletions
| @@ -34,40 +34,44 @@ handler(int signal) | |||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static void | 36 | static void |
| 37 | message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) | ||
| 38 | { | ||
| 39 | UNUSED(obj); | ||
| 40 | UNUSED(mosq); | ||
| 41 | printf("message '%.*s' for topic '%s'\n", | ||
| 42 | message->payloadlen, (const char *) message->payload, | ||
| 43 | message->topic); | ||
| 44 | } | ||
| 45 | |||
| 46 | static void | ||
| 47 | cleanup(void) | 37 | cleanup(void) |
| 48 | { | 38 | { |
| 49 | mosquitto_lib_cleanup(); | 39 | mosquitto_lib_cleanup(); |
| 50 | } | 40 | } |
| 51 | 41 | ||
| 52 | int | 42 | static void |
| 53 | main(void) | 43 | init(void) |
| 54 | { | 44 | { |
| 55 | (void) signal(SIGINT, handler); | 45 | (void) signal(SIGINT, handler); |
| 56 | 46 | ||
| 57 | mosquitto_lib_init(); | 47 | mosquitto_lib_init(); |
| 58 | (void) atexit(cleanup); | 48 | (void) atexit(cleanup); |
| 49 | } | ||
| 59 | 50 | ||
| 51 | static void | ||
| 52 | message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) | ||
| 53 | { | ||
| 54 | UNUSED(obj); | ||
| 55 | UNUSED(mosq); | ||
| 56 | printf("message '%.*s' for topic '%s'\n", | ||
| 57 | message->payloadlen, (const char *) message->payload, | ||
| 58 | message->topic); | ||
| 59 | } | ||
| 60 | |||
| 61 | static struct mosquitto * | ||
| 62 | setup(void) | ||
| 63 | { | ||
| 60 | struct mosquitto *client = mosquitto_new(CLIENT_ID, true, NULL); | 64 | struct mosquitto *client = mosquitto_new(CLIENT_ID, true, NULL); |
| 61 | if ( client == NULL ) { | 65 | if ( client == NULL ) { |
| 62 | (void) fprintf(stderr, "error on new client...\n"); | 66 | (void) fprintf(stderr, "error on new client...\n"); |
| 63 | return EXIT_FAILURE; | 67 | return NULL; |
| 64 | } | 68 | } |
| 65 | 69 | ||
| 66 | int err = mosquitto_username_pw_set(client, USERNAME, PASSWORD); | 70 | int err = mosquitto_username_pw_set(client, USERNAME, PASSWORD); |
| 67 | if ( err != MOSQ_ERR_SUCCESS ) { | 71 | if ( err != MOSQ_ERR_SUCCESS ) { |
| 68 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); | 72 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); |
| 69 | mosquitto_destroy(client); | 73 | mosquitto_destroy(client); |
| 70 | return EXIT_FAILURE; | 74 | return NULL; |
| 71 | } | 75 | } |
| 72 | 76 | ||
| 73 | mosquitto_message_callback_set(client, message_callback); | 77 | mosquitto_message_callback_set(client, message_callback); |
| @@ -76,25 +80,46 @@ main(void) | |||
| 76 | if ( err != MOSQ_ERR_SUCCESS ) { | 80 | if ( err != MOSQ_ERR_SUCCESS ) { |
| 77 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); | 81 | (void) fprintf(stderr, "error: %s\n", mosquitto_strerror(err)); |
| 78 | mosquitto_destroy(client); | 82 | mosquitto_destroy(client); |
| 79 | return EXIT_FAILURE; | 83 | return NULL; |
| 80 | } | 84 | } |
| 81 | 85 | ||
| 86 | return client; | ||
| 87 | } | ||
| 88 | |||
| 89 | static void | ||
| 90 | run(struct mosquitto *client) | ||
| 91 | { | ||
| 82 | mosquitto_subscribe(client, NULL, TOPIC, 0); | 92 | mosquitto_subscribe(client, NULL, TOPIC, 0); |
| 83 | 93 | ||
| 84 | while ( !terminate ) { | 94 | while ( !terminate ) { |
| 85 | static const int defaultTimeout = -1; // 1000ms | 95 | static const int defaultTimeout = -1; // 1000ms |
| 86 | 96 | ||
| 87 | err = mosquitto_loop(client, defaultTimeout, 1); | 97 | int err = mosquitto_loop(client, defaultTimeout, 1); |
| 88 | if ( !terminate && err ) { | 98 | if ( !terminate && err ) { |
| 89 | (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err)); | 99 | (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err)); |
| 90 | sleep(WAIT_UNTIL_RECONNECT); // NOLINT | 100 | sleep(WAIT_UNTIL_RECONNECT); // NOLINT |
| 91 | mosquitto_reconnect(client); | 101 | mosquitto_reconnect(client); |
| 92 | } | 102 | } |
| 93 | } | 103 | } |
| 104 | } | ||
| 94 | 105 | ||
| 95 | puts("shutdown..."); | 106 | static void |
| 107 | shutdown(struct mosquitto *client) | ||
| 108 | { | ||
| 96 | mosquitto_disconnect(client); | 109 | mosquitto_disconnect(client); |
| 97 | mosquitto_destroy(client); | 110 | mosquitto_destroy(client); |
| 111 | } | ||
| 112 | |||
| 113 | int | ||
| 114 | main(void) | ||
| 115 | { | ||
| 116 | init(); | ||
| 117 | |||
| 118 | struct mosquitto *client = setup(); | ||
| 119 | if ( client ) { | ||
| 120 | run(client); | ||
| 121 | shutdown(client); | ||
| 122 | } | ||
| 98 | 123 | ||
| 99 | return EXIT_SUCCESS; | 124 | return EXIT_SUCCESS; |
| 100 | } | 125 | } |
