diff options
| -rw-r--r-- | include/mqtt.h | 20 | ||||
| -rw-r--r-- | src/main.c | 28 | ||||
| -rw-r--r-- | src/mqtt.c | 18 |
3 files changed, 35 insertions, 31 deletions
diff --git a/include/mqtt.h b/include/mqtt.h index fa7df1c..8dad5eb 100644 --- a/include/mqtt.h +++ b/include/mqtt.h | |||
| @@ -1,16 +1,16 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | // Project | 3 | // mosquitto |
| 4 | #include <mosquitto.h> | 4 | #include <mosquitto.h> |
| 5 | 5 | ||
| 6 | struct mosquitto *connect(const char *clientid, // NOLINT | 6 | struct mosquitto *mqtt_connect(const char *clientid, // NOLINT |
| 7 | const char *username, // NOLINT | 7 | const char *username, // NOLINT |
| 8 | const char *password, // NOLINT | 8 | const char *password, // NOLINT |
| 9 | const char *hostname, // NOLINT | 9 | const char *hostname, // NOLINT |
| 10 | int port, | 10 | int port, |
| 11 | void callback(struct mosquitto *, void *, const struct mosquitto_message *), | 11 | void callback(struct mosquitto *, void *, const struct mosquitto_message *), |
| 12 | void *userdata); | 12 | void *userdata); |
| 13 | 13 | ||
| 14 | void run(struct mosquitto *client, const char *topics[], const volatile bool *quit); | 14 | void mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit); |
| 15 | 15 | ||
| 16 | void shutdown(struct mosquitto *client); | 16 | void mqtt_shutdown(struct mosquitto *client); |
| @@ -7,6 +7,9 @@ | |||
| 7 | // System | 7 | // System |
| 8 | #include <unistd.h> | 8 | #include <unistd.h> |
| 9 | 9 | ||
| 10 | // Mosquitto | ||
| 11 | #include <mosquitto.h> | ||
| 12 | |||
| 10 | // Project | 13 | // Project |
| 11 | #include "config.h" | 14 | #include "config.h" |
| 12 | #include "mqtt.h" | 15 | #include "mqtt.h" |
| @@ -15,10 +18,10 @@ | |||
| 15 | # define UNUSED(x) (void) (x) | 18 | # define UNUSED(x) (void) (x) |
| 16 | #endif | 19 | #endif |
| 17 | 20 | ||
| 18 | static volatile bool terminate = false; // NOLINT | 21 | static volatile bool terminate; // NOLINT |
| 19 | 22 | ||
| 20 | static void | 23 | static void |
| 21 | handler(int signal) | 24 | signal_handler(int signal) |
| 22 | { | 25 | { |
| 23 | if ( signal == SIGINT ) { | 26 | if ( signal == SIGINT ) { |
| 24 | terminate = true; | 27 | terminate = true; |
| @@ -26,18 +29,19 @@ handler(int signal) | |||
| 26 | } | 29 | } |
| 27 | 30 | ||
| 28 | static void | 31 | static void |
| 29 | cleanup(void) | 32 | cleanup_app(void) |
| 30 | { | 33 | { |
| 31 | mosquitto_lib_cleanup(); | 34 | mosquitto_lib_cleanup(); |
| 32 | } | 35 | } |
| 33 | 36 | ||
| 34 | static void | 37 | static void |
| 35 | init(void) | 38 | init_app(void) |
| 36 | { | 39 | { |
| 37 | (void) signal(SIGINT, handler); | 40 | terminate = false; |
| 41 | (void) signal(SIGINT, signal_handler); | ||
| 38 | 42 | ||
| 39 | mosquitto_lib_init(); | 43 | mosquitto_lib_init(); |
| 40 | (void) atexit(cleanup); | 44 | (void) atexit(cleanup_app); |
| 41 | } | 45 | } |
| 42 | 46 | ||
| 43 | static void | 47 | static void |
| @@ -53,13 +57,13 @@ callback(struct mosquitto *client, void *userdata, const struct mosquitto_messag | |||
| 53 | int | 57 | int |
| 54 | main(void) | 58 | main(void) |
| 55 | { | 59 | { |
| 56 | init(); | 60 | init_app(); |
| 57 | 61 | ||
| 58 | struct mosquitto *client = connect( | 62 | struct mosquitto *client = mqtt_connect( |
| 59 | CLIENT_ID, | 63 | CLIENTID, |
| 60 | USERNAME, | 64 | USERNAME, |
| 61 | PASSWORD, | 65 | PASSWORD, |
| 62 | HOST, | 66 | HOSTNAME, |
| 63 | PORT, | 67 | PORT, |
| 64 | callback, | 68 | callback, |
| 65 | NULL); | 69 | NULL); |
| @@ -68,9 +72,9 @@ main(void) | |||
| 68 | return EXIT_FAILURE; | 72 | return EXIT_FAILURE; |
| 69 | } | 73 | } |
| 70 | 74 | ||
| 71 | run(client, (const char *[]){ TOPIC, NULL }, &terminate); | 75 | mqtt_run(client, (const char *[]){ TOPIC, NULL }, &terminate); |
| 72 | 76 | ||
| 73 | shutdown(client); | 77 | mqtt_shutdown(client); |
| 74 | 78 | ||
| 75 | return EXIT_SUCCESS; | 79 | return EXIT_SUCCESS; |
| 76 | } | 80 | } |
| @@ -9,13 +9,13 @@ | |||
| 9 | #include "mqtt.h" | 9 | #include "mqtt.h" |
| 10 | 10 | ||
| 11 | struct mosquitto * | 11 | struct mosquitto * |
| 12 | connect(const char *clientid, // NOLINT | 12 | mqtt_connect(const char *clientid, // NOLINT |
| 13 | const char *username, // NOLINT | 13 | const char *username, // 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 *), | 17 | void callback(struct mosquitto *, void *, const struct mosquitto_message *), |
| 18 | void *userdata) | 18 | void *userdata) |
| 19 | { | 19 | { |
| 20 | struct mosquitto *client = mosquitto_new(clientid, true, userdata); | 20 | struct mosquitto *client = mosquitto_new(clientid, true, userdata); |
| 21 | if ( client == NULL ) { | 21 | if ( client == NULL ) { |
| @@ -43,7 +43,7 @@ connect(const char *clientid, // NOLINT | |||
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | void | 45 | void |
| 46 | run(struct mosquitto *client, const char *topics[], const volatile bool *quit) | 46 | mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) |
| 47 | { | 47 | { |
| 48 | for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { | 48 | for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { |
| 49 | mosquitto_subscribe(client, NULL, topics[idx], 0); | 49 | mosquitto_subscribe(client, NULL, topics[idx], 0); |
| @@ -62,7 +62,7 @@ run(struct mosquitto *client, const char *topics[], const volatile bool *quit) | |||
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | void | 64 | void |
| 65 | shutdown(struct mosquitto *client) | 65 | mqtt_shutdown(struct mosquitto *client) |
| 66 | { | 66 | { |
| 67 | mosquitto_disconnect(client); | 67 | mosquitto_disconnect(client); |
| 68 | mosquitto_destroy(client); | 68 | mosquitto_destroy(client); |
