diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/mqtt.c | 12 | ||||
| -rw-r--r-- | src/publish.c | 99 | ||||
| -rw-r--r-- | src/subscribe.c | 2 |
3 files changed, 110 insertions, 3 deletions
| @@ -30,7 +30,9 @@ mqtt_connect(const char *clientid, // NOLINT | |||
| 30 | return NULL; | 30 | return NULL; |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | mosquitto_message_callback_set(client, callback); | 33 | if ( callback ) { |
| 34 | mosquitto_message_callback_set(client, callback); | ||
| 35 | } | ||
| 34 | 36 | ||
| 35 | err = mosquitto_connect(client, hostname, port, KEEPALIVE); | 37 | err = mosquitto_connect(client, hostname, port, KEEPALIVE); |
| 36 | if ( err != MOSQ_ERR_SUCCESS ) { | 38 | if ( err != MOSQ_ERR_SUCCESS ) { |
| @@ -45,8 +47,10 @@ mqtt_connect(const char *clientid, // NOLINT | |||
| 45 | void | 47 | void |
| 46 | mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) | 48 | mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *quit) |
| 47 | { | 49 | { |
| 50 | int *mid = NULL; | ||
| 51 | |||
| 48 | for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { | 52 | for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { |
| 49 | mosquitto_subscribe(client, NULL, topics[idx], 0); | 53 | mosquitto_subscribe(client, mid, topics[idx], 0); |
| 50 | } | 54 | } |
| 51 | 55 | ||
| 52 | while ( !*quit ) { | 56 | while ( !*quit ) { |
| @@ -59,6 +63,10 @@ mqtt_run(struct mosquitto *client, const char *topics[], const volatile bool *qu | |||
| 59 | mosquitto_reconnect(client); | 63 | mosquitto_reconnect(client); |
| 60 | } | 64 | } |
| 61 | } | 65 | } |
| 66 | |||
| 67 | for ( size_t idx = 0; topics[idx] != NULL; ++idx ) { | ||
| 68 | mosquitto_unsubscribe(client, mid, topics[idx]); | ||
| 69 | } | ||
| 62 | } | 70 | } |
| 63 | 71 | ||
| 64 | void | 72 | void |
diff --git a/src/publish.c b/src/publish.c new file mode 100644 index 0000000..6afc5b4 --- /dev/null +++ b/src/publish.c | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | // Standard C | ||
| 2 | #include <signal.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | // System | ||
| 9 | #include <unistd.h> | ||
| 10 | |||
| 11 | // Mosquitto | ||
| 12 | #include <mosquitto.h> | ||
| 13 | |||
| 14 | // Project | ||
| 15 | #include "config.h" | ||
| 16 | #include "mqtt.h" | ||
| 17 | |||
| 18 | static bool | ||
| 19 | streq(const char *lhs, const char *rhs) | ||
| 20 | { | ||
| 21 | return strcmp(lhs, rhs) == 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | static void | ||
| 25 | cleanup_app(void) | ||
| 26 | { | ||
| 27 | mosquitto_lib_cleanup(); | ||
| 28 | } | ||
| 29 | |||
| 30 | static void | ||
| 31 | init_app(void) | ||
| 32 | { | ||
| 33 | mosquitto_lib_init(); | ||
| 34 | (void) atexit(cleanup_app); | ||
| 35 | } | ||
| 36 | |||
| 37 | int | ||
| 38 | main(void) | ||
| 39 | { | ||
| 40 | init_app(); | ||
| 41 | |||
| 42 | struct mosquitto *client = mqtt_connect( | ||
| 43 | NULL, | ||
| 44 | USERNAME, | ||
| 45 | PASSWORD, | ||
| 46 | HOSTNAME, | ||
| 47 | PORT, | ||
| 48 | NULL, | ||
| 49 | NULL); | ||
| 50 | |||
| 51 | if ( !client ) { | ||
| 52 | return EXIT_FAILURE; | ||
| 53 | } | ||
| 54 | |||
| 55 | // logic | ||
| 56 | for ( bool quit = false; !quit; ) { | ||
| 57 | printf("> "); | ||
| 58 | |||
| 59 | char command[100] = { 0 }; // NOLINT | ||
| 60 | if ( fgets(command, sizeof command, stdin) == NULL ) { | ||
| 61 | quit = true; | ||
| 62 | continue; | ||
| 63 | } | ||
| 64 | |||
| 65 | // remove 'newline' symbol at the end | ||
| 66 | command[strcspn(command, "\n")] = '\0'; | ||
| 67 | |||
| 68 | if ( streq(command, "") ) { | ||
| 69 | ; // do nothing | ||
| 70 | } | ||
| 71 | else if ( streq(command, "quit") ) { | ||
| 72 | quit = true; | ||
| 73 | } | ||
| 74 | else if ( streq(command, "send") ) { | ||
| 75 | static const char payload[] = "ich bin ein payload"; | ||
| 76 | static const char topic[] = "testtopic"; | ||
| 77 | |||
| 78 | int err = mosquitto_publish( | ||
| 79 | client, | ||
| 80 | NULL, | ||
| 81 | topic, | ||
| 82 | sizeof payload, | ||
| 83 | payload, | ||
| 84 | 0, | ||
| 85 | false); | ||
| 86 | |||
| 87 | if ( err ) { | ||
| 88 | (void) fprintf(stderr, "error on send: %s\n", mosquitto_strerror(err)); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | (void) fprintf(stderr, "unknown command: '%s'\n", command); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | mqtt_shutdown(client); | ||
| 97 | |||
| 98 | return EXIT_SUCCESS; | ||
| 99 | } | ||
diff --git a/src/subscribe.c b/src/subscribe.c index 82e7606..e7d5883 100644 --- a/src/subscribe.c +++ b/src/subscribe.c | |||
| @@ -72,7 +72,7 @@ main(void) | |||
| 72 | return EXIT_FAILURE; | 72 | return EXIT_FAILURE; |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | mqtt_run(client, (const char *[]){ TOPIC, NULL }, &terminate); | 75 | mqtt_run(client, (const char *[]){ TOPIC, "testtopic", NULL }, &terminate); |
| 76 | 76 | ||
| 77 | mqtt_shutdown(client); | 77 | mqtt_shutdown(client); |
| 78 | 78 | ||
