Wed, 23 Apr 2014 14:25:09 +0200
Added library
1 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2014 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with EC-65K; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
3 | 26 | #include <getopt.h> |
27 | #include <sys/types.h> | |
28 | #include <signal.h> | |
6 | 29 | #include <string.h> |
30 | #include <errno.h> | |
31 | ||
32 | #include <mosquitto.h> | |
1 | 33 | |
34 | ||
35 | #include "main.h" | |
36 | ||
37 | ||
6 | 38 | #define STATUS_CONNECTING 0 |
39 | #define STATUS_CONNACK_RECVD 1 | |
40 | #define STATUS_WAITING 2 | |
41 | ||
42 | /* Global variables for use in callbacks. See sub_client.c for an example of | |
43 | * using a struct to hold variables for use in callbacks. */ | |
44 | static char *topic = NULL; | |
45 | //static char *message = NULL; | |
46 | //static long msglen = 0; | |
47 | static int qos = 0; | |
48 | //static int retain = 0; | |
49 | static int status = STATUS_CONNECTING; | |
50 | static int mid_sent = 0; | |
51 | static int last_mid = -1; | |
52 | static int last_mid_sent = -1; | |
53 | static bool connected = true; | |
54 | //static char *username = NULL; | |
55 | //static char *password = NULL; | |
56 | static bool disconnect_sent = false; | |
57 | static bool quiet = false; | |
58 | static bool debug = false; | |
59 | static bool shutdown = false; | |
60 | ||
61 | ||
62 | ||
1 | 63 | void help(void) |
64 | { | |
65 | fprintf(stdout, "Usage: thermomeneters [-d] [-h]\n"); | |
66 | fprintf(stdout, " -d --debug Debug on\n"); | |
67 | fprintf(stdout, " -h --help Display this help\n"); | |
68 | } | |
69 | ||
70 | ||
71 | ||
72 | void die(int onsig) | |
73 | { | |
74 | switch (onsig) { | |
6 | 75 | case SIGHUP: fprintf(stdout, "[main] Hangup detected\n"); |
1 | 76 | break; |
6 | 77 | case SIGINT: fprintf(stdout, "[main] Interrupt from keyboard\n"); |
1 | 78 | break; |
6 | 79 | case SIGTERM: fprintf(stdout, "[main] Termination signal received\n"); |
1 | 80 | break; |
6 | 81 | default: fprintf(stdout, "[main] die on signal %d\n", onsig); |
1 | 82 | } |
83 | ||
6 | 84 | shutdown = true; |
85 | } | |
86 | ||
87 | ||
88 | ||
89 | void my_connect_callback(struct mosquitto *mosq, void *obj, int result) | |
90 | { | |
91 | int rc = MOSQ_ERR_SUCCESS; | |
92 | ||
93 | fprintf(stdout, (char *)"my_connect_callback result=%d\n", result); | |
94 | if (!result) { | |
95 | status = STATUS_CONNACK_RECVD; | |
96 | } else { | |
97 | fprintf(stderr, "%s\n", mosquitto_connack_string(result)); | |
98 | } | |
99 | } | |
100 | ||
101 | ||
102 | ||
103 | void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) | |
104 | { | |
105 | fprintf(stdout, (char *)"my_disconnect_callback\n"); | |
106 | connected = false; | |
107 | } | |
108 | ||
109 | ||
110 | ||
111 | void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) | |
112 | { | |
113 | fprintf(stdout, (char *)"my_publish_callback mid=%d\n", mid); | |
114 | ||
115 | last_mid_sent = mid; | |
116 | } | |
117 | ||
118 | ||
119 | ||
120 | void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) | |
121 | { | |
7 | 122 | printf("log: %s\n", str); |
1 | 123 | } |
124 | ||
125 | ||
126 | ||
127 | int main(int argc, char *argv[]) | |
128 | { | |
6 | 129 | int i, c, len, rc, rc2; |
130 | char *id = NULL; | |
131 | char *host = (char *)"lx02.mbse.ym"; | |
132 | int port = 1883; | |
133 | struct mosquitto *mosq = NULL; | |
134 | char hostname[256], buf[1024]; | |
135 | int keepalive = 60; | |
136 | unsigned int max_inflight = 20; | |
137 | char err[1024]; | |
1 | 138 | |
139 | while (1) { | |
140 | int option_index = 0; | |
141 | static struct option long_options[] = { | |
142 | {"debug", 0, 0, 'c'}, | |
143 | {"help", 0, 0, 'h'}, | |
144 | {0, 0, 0, 0} | |
145 | }; | |
146 | ||
147 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
148 | if (c == -1) | |
149 | break; | |
150 | ||
151 | switch (c) { | |
6 | 152 | case 'd': debug = true; |
1 | 153 | break; |
154 | case 'h': help(); | |
155 | return 1; | |
156 | } | |
157 | } | |
158 | ||
159 | /* | |
160 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
161 | * but that's live. This daemon should only be stopped by SIGTERM. | |
162 | * Don't catch SIGCHLD. | |
163 | */ | |
164 | for (i = 0; i < NSIG; i++) { | |
165 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
166 | signal(i, (void (*))die); | |
167 | } | |
168 | ||
6 | 169 | /* |
170 | * Initialize mosquitto communication | |
171 | */ | |
172 | mosquitto_lib_init(); | |
173 | hostname[0] = '\0'; | |
174 | gethostname(hostname, 256); | |
175 | hostname[255] = '\0'; | |
176 | len = strlen("thermometers/") + 1 + strlen(hostname); | |
177 | id = malloc(len); | |
178 | if(!id) { | |
179 | if (!quiet) | |
180 | fprintf(stderr, "Error: Out of memory.\n"); | |
181 | mosquitto_lib_cleanup(); | |
182 | return 1; | |
183 | } | |
184 | snprintf(id, len, "thermometers/%s", hostname); | |
185 | if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { | |
186 | /* | |
187 | * Enforce maximum client id length of 23 characters | |
188 | */ | |
189 | id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; | |
190 | } | |
1 | 191 | |
6 | 192 | fprintf(stdout, "id: %s\n", id); |
193 | ||
194 | mosq = mosquitto_new(id, true, NULL); | |
195 | if(!mosq) { | |
196 | switch(errno) { | |
197 | case ENOMEM: | |
198 | if (!quiet) | |
199 | fprintf(stderr, "Error: Out of memory.\n"); | |
200 | break; | |
201 | case EINVAL: | |
202 | if (!quiet) | |
203 | fprintf(stderr, "Error: Invalid id.\n"); | |
204 | break; | |
205 | } | |
206 | mosquitto_lib_cleanup(); | |
207 | return 1; | |
208 | } | |
209 | ||
210 | if(debug) { | |
211 | mosquitto_log_callback_set(mosq, my_log_callback); | |
212 | } | |
213 | ||
214 | /* | |
215 | * Set our will | |
216 | */ | |
217 | topic = malloc(28 + strlen(hostname)); | |
218 | sprintf(topic, "clients/%s/thermometers/state", hostname); | |
219 | sprintf(buf, "0"); | |
220 | rc = mosquitto_will_set(mosq, topic, strlen(buf), buf, qos, true); | |
221 | if (rc) { | |
222 | if (rc == MOSQ_ERR_INVAL) { | |
223 | fprintf(stderr, "Input parameters invalid\n"); | |
224 | } else if (rc == MOSQ_ERR_NOMEM) { | |
225 | fprintf(stderr, "Out of Memory\n"); | |
226 | } else if (rc == MOSQ_ERR_PAYLOAD_SIZE) { | |
227 | fprintf(stderr, "Invalid payload size\n"); | |
228 | } | |
229 | mosquitto_lib_cleanup(); | |
230 | return rc; | |
231 | } | |
232 | ||
233 | mosquitto_max_inflight_messages_set(mosq, max_inflight); | |
234 | mosquitto_connect_callback_set(mosq, my_connect_callback); | |
235 | mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); | |
236 | mosquitto_publish_callback_set(mosq, my_publish_callback); | |
237 | ||
238 | rc = mosquitto_connect(mosq, host, port, keepalive); | |
239 | if (rc) { | |
240 | if (rc == MOSQ_ERR_ERRNO) { | |
241 | strerror_r(errno, err, 1024); | |
242 | fprintf(stderr, "Error: %s\n", err); | |
243 | } else { | |
244 | fprintf(stderr, "Unable to connect (%d).\n", rc); | |
245 | } | |
246 | mosquitto_lib_cleanup(); | |
247 | return rc; | |
248 | } | |
249 | ||
250 | /* | |
251 | * Initialise is complete, report our presence state | |
252 | */ | |
253 | mosquitto_loop_start(mosq); | |
254 | ||
255 | // topic = malloc(28 + strlen(hostname)); | |
256 | sprintf(topic, "clients/%s/thermometers/state", hostname); | |
257 | sprintf(buf, "1"); | |
258 | rc2 = mosquitto_publish(mosq, &mid_sent, topic, strlen(buf), buf, qos, 1); | |
259 | free(topic); | |
260 | ||
261 | ||
262 | fprintf(stdout, (char *)"Enter loop, connected %d\n", connected); | |
263 | do { | |
264 | if (status == STATUS_CONNACK_RECVD) { | |
265 | // fprintf(stdout, (char *)"Ok\n"); | |
266 | // if(fgets(buf, 1024, stdin)){ | |
267 | // buf[strlen(buf)-1] = '\0'; | |
268 | // rc2 = mosquitto_publish(mosq, &mid_sent, topic, strlen(buf), buf, qos, retain); | |
269 | // if(rc2){ | |
270 | // if(!quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2); | |
271 | // mosquitto_disconnect(mosq); | |
272 | // } | |
273 | // } else | |
274 | if (shutdown) { | |
275 | fprintf(stdout, (char *)"Shutdown\n"); | |
276 | topic = malloc(28 + strlen(hostname)); | |
277 | sprintf(topic, "clients/%s/thermometers/state", hostname); | |
278 | sprintf(buf, "0"); | |
279 | rc2 = mosquitto_publish(mosq, &mid_sent, topic, strlen(buf), buf, qos, true); | |
280 | free(topic); | |
281 | last_mid = mid_sent; | |
282 | status = STATUS_WAITING; | |
283 | } | |
284 | } else if (status == STATUS_WAITING) { | |
285 | fprintf(stdout, (char *)"Waiting\n"); | |
286 | if (last_mid_sent == last_mid && disconnect_sent == false) { | |
287 | mosquitto_disconnect(mosq); | |
288 | disconnect_sent = true; | |
289 | } | |
290 | usleep(100000); | |
291 | } | |
292 | rc = MOSQ_ERR_SUCCESS; | |
293 | ||
294 | } while(rc == MOSQ_ERR_SUCCESS && connected); | |
295 | fprintf(stdout, (char *)"Out of loop\n"); | |
296 | ||
297 | mosquitto_loop_stop(mosq, false); | |
298 | ||
299 | mosquitto_destroy(mosq); | |
300 | mosquitto_lib_cleanup(); | |
301 | ||
302 | fprintf(stdout, (char *)"Bye Bye\n"); | |
1 | 303 | return 0; |
304 | } | |
305 |