d0cec6c42847a89dd2df56569a9f29dc875bb2e8
[collectd.git] / src / gps.c
1 /**
2  * collectd - src/gps.c
3  * Copyright (C) 2015  Nicolas JOURDEN
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Nicolas JOURDEN <nicolas.jourden at laposte.net>
25  *   Florian octo Forster <octo at collectd.org>
26  *   Marc Fournier <marc.fournier at camptocamp.com>
27  **/
28
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "utils_time.h"
33 #include "configfile.h"
34
35 #define CGPS_TRUE                  1
36 #define CGPS_FALSE                 0
37 #define CGPS_DEFAULT_HOST          "localhost"
38 #define CGPS_DEFAULT_PORT          "2947"
39 #define CGPS_DEFAULT_TIMEOUT       TIME_T_TO_CDTIME_T (0.015)
40 #define CGPS_DEFAULT_PAUSE_READ    TIME_T_TO_CDTIME_T (1)
41 #define CGPS_DEFAULT_PAUSE_CONNECT TIME_T_TO_CDTIME_T (5)
42 #define CGPS_MAX_ERROR             100
43 #define CGPS_CONFIG                "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n"
44
45 #include <gps.h>
46 #include <pthread.h>
47
48 typedef struct {
49   char *host;
50   char *port;
51   cdtime_t timeout;
52   cdtime_t pause_read;
53   cdtime_t pause_connect;
54 } cgps_config_t;
55
56 typedef struct {
57   gauge_t sats_used;
58   gauge_t sats_visible;
59   gauge_t hdop;
60   gauge_t vdop;
61 } cgps_data_t;
62
63 static cgps_config_t cgps_config_data;
64
65 static cgps_data_t cgps_data = {NAN, NAN, NAN, NAN};
66
67 static pthread_t cgps_thread_id;
68 static pthread_mutex_t  cgps_data_lock = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_mutex_t  cgps_thread_lock = PTHREAD_MUTEX_INITIALIZER;
70 static int cgps_thread_shutdown = CGPS_FALSE;
71 static int cgps_thread_running = CGPS_FALSE;
72
73 /**
74  * Non blocking pause for the thread.
75  */
76 static int cgps_thread_pause(cdtime_t pTime)
77 {
78   cdtime_t now;
79   now = cdtime ();
80   struct timespec pause_th;
81   CDTIME_T_TO_TIMESPEC (MS_TO_CDTIME_T(10), &pause_th);
82   while (CGPS_TRUE)
83   {
84     if ( (cdtime () - now) > pTime )
85     {
86       break;
87     }
88
89     pthread_mutex_lock (&cgps_thread_lock);
90     if (cgps_thread_shutdown == CGPS_TRUE)
91     {
92       return CGPS_FALSE;
93     }
94     pthread_mutex_unlock (&cgps_thread_lock);
95     nanosleep (&pause_th, NULL);
96  }
97
98  return CGPS_TRUE;
99 }
100
101 /**
102  * Thread reading from gpsd.
103  */
104 static void * cgps_thread (void * pData)
105 {
106   struct gps_data_t gpsd_conn;
107   unsigned int err_count;
108   cgps_thread_running = CGPS_TRUE;
109
110   while (CGPS_TRUE)
111   {
112     pthread_mutex_lock (&cgps_thread_lock);
113     if (cgps_thread_shutdown == CGPS_TRUE)
114     {
115       goto quit;
116     }
117     pthread_mutex_unlock (&cgps_thread_lock);
118
119     err_count = 0;
120
121 #if GPSD_API_MAJOR_VERSION > 4
122     int status = gps_open (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
123 #else
124     int status = gps_open_r (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
125 #endif
126     if (status < 0)
127     {
128       WARNING ("gps plugin: connecting to %s:%s failed: %s",
129                cgps_config_data.host, cgps_config_data.port, gps_errstr (status));
130
131       // Here we make a pause until a new tentative to connect, we check also if
132       // the thread does not need to stop.
133       if (cgps_thread_pause(cgps_config_data.pause_connect) == CGPS_FALSE)
134       {
135         goto quit;
136       }
137
138       continue;
139     }
140
141     gps_stream (&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
142     gps_send (&gpsd_conn, CGPS_CONFIG);
143
144     while (CGPS_TRUE)
145     {
146       pthread_mutex_lock (&cgps_thread_lock);
147       if (cgps_thread_shutdown == CGPS_TRUE)
148       {
149         goto stop;
150       }
151       pthread_mutex_unlock (&cgps_thread_lock);
152
153 #if GPSD_API_MAJOR_VERSION > 4
154       long timeout_us = CDTIME_T_TO_US (cgps_config_data.timeout);
155       if (!gps_waiting (&gpsd_conn, (int) timeout_us ))
156 #else
157       if (!gps_waiting (&gpsd_conn))
158 #endif
159       {
160         if (cgps_thread_pause(cgps_config_data.pause_read) == CGPS_FALSE)
161         {
162           goto stop;
163         }
164
165         continue;
166       }
167
168       if (gps_read (&gpsd_conn) == -1)
169       {
170         WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count);
171         err_count++;
172
173         if (err_count > CGPS_MAX_ERROR)
174         {
175           // Server is not responding ...
176           if (gps_send (&gpsd_conn, CGPS_CONFIG) == -1)
177           {
178             WARNING ("gps plugin: gpsd seems to be down, reconnecting");
179             gps_close (&gpsd_conn);
180             break;
181           }
182           // Server is responding ...
183           else
184           {
185             err_count = 0;
186           }
187         }
188
189         continue;
190       }
191
192       pthread_mutex_lock (&cgps_data_lock);
193
194       // Number of sats in view:
195       cgps_data.sats_used = (gauge_t) gpsd_conn.satellites_used;
196       cgps_data.sats_visible = (gauge_t) gpsd_conn.satellites_visible;
197
198       // dilution of precision:
199       cgps_data.vdop = NAN;
200       cgps_data.hdop = NAN;
201       if (cgps_data.sats_used > 0)
202       {
203         cgps_data.hdop = gpsd_conn.dop.hdop;
204         cgps_data.vdop = gpsd_conn.dop.vdop;
205       }
206
207       DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
208              cgps_data.sats_used, cgps_data.sats_visible, cgps_data.hdop, cgps_data.vdop);
209
210       pthread_mutex_unlock (&cgps_data_lock);
211     }
212   }
213
214 stop:
215   DEBUG ("gps plugin: thread closing gpsd connection ... ");
216   gps_stream (&gpsd_conn, WATCH_DISABLE, NULL);
217   gps_close (&gpsd_conn);
218 quit:
219   DEBUG ("gps plugin: thread shutting down ... ");
220   cgps_thread_running = CGPS_FALSE;
221   pthread_mutex_unlock (&cgps_thread_lock);
222   pthread_exit (NULL);
223 }
224
225
226 /**
227  * Submit a piece of the data.
228  */
229 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
230 {
231   value_t values[1];
232   value_list_t vl = VALUE_LIST_INIT;
233
234   values[0].gauge = value;
235
236   vl.values = values;
237   vl.values_len = 1;
238   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
239   sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
240   sstrncpy (vl.type, type, sizeof (vl.type));
241   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
242
243   plugin_dispatch_values (&vl);
244 }
245
246 /**
247  * Read the data and submit by piece.
248  */
249 static int cgps_read ()
250 {
251   cgps_data_t data_copy;
252
253   pthread_mutex_lock (&cgps_data_lock);
254   data_copy = cgps_data;
255   pthread_mutex_unlock (&cgps_data_lock);
256
257   cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
258   cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
259   cgps_submit ("satellites", data_copy.sats_used, "used");
260   cgps_submit ("satellites", data_copy.sats_visible, "visible");
261
262   return (0);
263 }
264
265 /**
266  * Read configuration.
267  */
268 static int cgps_config (oconfig_item_t *ci)
269 {
270   int i;
271
272   for (i = 0; i < ci->children_num; i++)
273   {
274     oconfig_item_t *child = ci->children + i;
275
276     if (strcasecmp ("Host", child->key) == 0)
277       cf_util_get_string (child, &cgps_config_data.host);
278     else if (strcasecmp ("Port", child->key) == 0)
279       cf_util_get_service (child, &cgps_config_data.port);
280     else if (strcasecmp ("Timeout", child->key) == 0)
281       cf_util_get_cdtime (child, &cgps_config_data.timeout);
282     else if (strcasecmp ("Pauseread", child->key) == 0)
283       cf_util_get_cdtime (child, &cgps_config_data.pause_read);
284     else if (strcasecmp ("PauseConnect", child->key) == 0)
285       cf_util_get_cdtime (child, &cgps_config_data.pause_connect);
286     else
287       WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
288   }
289
290   return (0);
291 }
292
293 /**
294  * Init.
295  */
296 static int cgps_init (void)
297 {
298   int status;
299
300   if (cgps_thread_running == CGPS_TRUE)
301   {
302     DEBUG ("gps plugin: error gps thread already running ... ");
303     return 0;
304   }
305
306   DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.6f sec., \
307 pause read: %.3f sec, pause connect: %.3f sec.}",
308          cgps_config_data.host, cgps_config_data.port,
309          CDTIME_T_TO_DOUBLE (cgps_config_data.timeout),
310          CDTIME_T_TO_DOUBLE (cgps_config_data.pause_read),
311          CDTIME_T_TO_DOUBLE (cgps_config_data.pause_connect));
312
313   status = plugin_thread_create (&cgps_thread_id, NULL, cgps_thread, NULL);
314   if (status != 0)
315   {
316     ERROR ("gps plugin: pthread_create() failed.");
317     return (-1);
318   }
319
320   return (0);
321 }
322
323 /**
324  * Shutdown.
325  */
326 static int cgps_shutdown (void)
327 {
328   void * res;
329
330   pthread_mutex_lock (&cgps_thread_lock);
331   cgps_thread_shutdown = CGPS_TRUE;
332   pthread_mutex_unlock (&cgps_thread_lock);
333
334   pthread_join(cgps_thread_id, &res);
335   free(res);
336
337   // Clean mutex:
338   pthread_mutex_unlock(&cgps_thread_lock);
339   pthread_mutex_destroy(&cgps_thread_lock);
340   pthread_mutex_unlock(&cgps_data_lock);
341   pthread_mutex_destroy(&cgps_data_lock);
342
343   sfree (cgps_config_data.port);
344   sfree (cgps_config_data.host);
345
346   return (0);
347 }
348
349 /**
350  * Register the module.
351  */
352 void module_register (void)
353 {
354   cgps_config_data.host = sstrdup (CGPS_DEFAULT_HOST);
355   cgps_config_data.port = sstrdup (CGPS_DEFAULT_PORT);
356   cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
357   cgps_config_data.pause_read = CGPS_DEFAULT_PAUSE_READ;
358   cgps_config_data.pause_connect = CGPS_DEFAULT_PAUSE_CONNECT;
359
360   plugin_register_complex_config ("gps", cgps_config);
361   plugin_register_init ("gps", cgps_init);
362   plugin_register_read ("gps", cgps_read);
363   plugin_register_shutdown ("gps", cgps_shutdown);
364 }