summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c58
1 files changed, 37 insertions, 21 deletions
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)
41 (void) atexit(cleanup); 41 (void) atexit(cleanup);
42} 42}
43 43
44static void
45message_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
54static struct mosquitto * 44static struct mosquitto *
55setup(void) 45connect(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
82static void 78static void
83run(struct mosquitto *client) 79run(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
102static void
103callback(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
106int 112int
107main(void) 113main(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}