Enable to disconnect at shutdown, changed default values, fixed time unit issue,...
[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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_time.h"
31 #include "configfile.h"
32
33 #define GPS_DEFAULT_HOST    "localhost"
34 #define GPS_DEFAULT_PORT    "2947"
35 #define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (0.015)
36 #define GPS_DEFAULT_PAUSE   TIME_T_TO_CDTIME_T (1)
37 #define GPS_MAX_ERROR       100
38 #define GPS_CONFIG          "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n"
39
40 #include <gps.h>
41 #include <pthread.h>
42
43 typedef struct {
44   char *host;
45   char *port;
46   cdtime_t timeout;
47   cdtime_t pause;
48 } cgps_config_t;
49
50 typedef struct {
51   gauge_t sats_used;
52   gauge_t sats_visible;
53   gauge_t hdop;
54   gauge_t vdop;
55 } cgps_data_t;
56
57 // Thread items:
58 static pthread_t connector = (pthread_t) 0;
59
60 static cgps_config_t config;
61
62 static cgps_data_t      data = {NAN, NAN, NAN, NAN};
63 static pthread_mutex_t  data_lock = PTHREAD_MUTEX_INITIALIZER;
64 static struct gps_data_t gpsd_conn;
65
66 /**
67  * Thread reading from gpsd.
68  */
69 static void * cgps_thread (void * pData)
70 {
71   int err_count;
72
73   while (1)
74   {
75     err_count = 0;
76
77 #if GPSD_API_MAJOR_VERSION > 4
78     int status = gps_open (config.host, config.port, &gpsd_conn);
79 #else
80     int status = gps_open_r (config.host, config.port, &gpsd_conn);
81 #endif
82     if (status < 0)
83     {
84       WARNING ("gps plugin: connecting to %s:%s failed: %s",
85                config.host, config.port, gps_errstr (status));
86       sleep (60);
87       continue;
88     }
89
90     gps_stream (&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
91     gps_send (&gpsd_conn, GPS_CONFIG);
92
93     while (1)
94     {
95 #if GPSD_API_MAJOR_VERSION > 4
96       long timeout_us = CDTIME_T_TO_US (config.timeout);
97       if (!gps_waiting (&gpsd_conn, (int) timeout_us ))
98 #else
99       if (!gps_waiting (&gpsd_conn))
100 #endif
101       {
102         struct timespec pause_ns;
103         CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns);
104         nanosleep (&pause_ns, NULL);
105         continue;
106       }
107
108       if (gps_read (&gpsd_conn) == -1)
109       {
110         WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count);
111         err_count++;
112
113         if (err_count > GPS_MAX_ERROR)
114         {
115           // Server is not responding ...
116           if (gps_send (&gpsd_conn, GPS_CONFIG) == -1)
117           {
118             WARNING ("gps plugin: gpsd seems to be down, reconnecting");
119             gps_close (&gpsd_conn);
120             break;
121           }
122           // Server is responding ...
123           else
124           {
125             err_count = 0;
126           }
127         }
128
129         continue;
130       }
131
132       pthread_mutex_lock (&data_lock);
133
134       // Number of sats in view:
135       data.sats_used = (gauge_t) gpsd_conn.satellites_used;
136       data.sats_visible = (gauge_t) gpsd_conn.satellites_visible;
137
138       // dilution of precision:
139       data.vdop = NAN; data.hdop = NAN;
140       if (data.sats_used > 0)
141       {
142         data.hdop = gpsd_conn.dop.hdop;
143         data.vdop = gpsd_conn.dop.vdop;
144       }
145
146
147       DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
148              data.sats_used, data.sats_visible, data.hdop, data.vdop);
149
150       pthread_mutex_unlock (&data_lock);
151     }
152   }
153
154   gps_stream (&gpsd_conn, WATCH_DISABLE, /* data = */ NULL);
155   gps_close (&gpsd_conn);
156
157   pthread_exit ((void *) 0);
158 }
159
160 /**
161  * Submit a piece of the data.
162  */
163 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
164 {
165   value_t values[1];
166   value_list_t vl = VALUE_LIST_INIT;
167
168   values[0].gauge = value;
169
170   vl.values = values;
171   vl.values_len = 1;
172   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
173   sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
174   sstrncpy (vl.type, type, sizeof (vl.type));
175   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
176
177   plugin_dispatch_values (&vl);
178 }
179
180 /**
181  * Read the data and submit by piece.
182  */
183 static int cgps_read ()
184 {
185   cgps_data_t data_copy;
186
187   pthread_mutex_lock (&data_lock);
188   data_copy = data;
189   pthread_mutex_unlock (&data_lock);
190
191   cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
192   cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
193   cgps_submit ("satellites", data_copy.sats_used, "used");
194   cgps_submit ("satellites", data_copy.sats_visible, "visible");
195
196   return (0);
197 }
198
199 /**
200  * Read configuration.
201  */
202 static int cgps_config (oconfig_item_t *ci)
203 {
204   int i;
205
206   for (i = 0; i < ci->children_num; i++)
207   {
208     oconfig_item_t *child = ci->children + i;
209
210     if (strcasecmp ("Host", child->key) == 0)
211       cf_util_get_string (child, &config.host);
212     else if (strcasecmp ("Port", child->key) == 0)
213       cf_util_get_service (child, &config.port);
214     else if (strcasecmp ("Timeout", child->key) == 0)
215       cf_util_get_cdtime (child, &config.timeout);
216     else if (strcasecmp ("Pause", child->key) == 0)
217       cf_util_get_cdtime (child, &config.pause);
218     else
219       WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
220   }
221
222   return 0;
223 }
224
225 /**
226  * Init.
227  */
228 static int cgps_init (void)
229 {
230   int status;
231
232   DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.6f sec., pause: %.3f sec.}",
233          config.host, config.port,
234          CDTIME_T_TO_DOUBLE (config.timeout), CDTIME_T_TO_DOUBLE (config.pause));
235
236   status = plugin_thread_create (&connector, NULL, cgps_thread, NULL);
237   if (status != 0)
238   {
239     ERROR ("gps plugin: pthread_create() failed.");
240     return (-1);
241   }
242
243   return (0);
244 }
245
246 /**
247  * Shutdown.
248  */
249 static int cgps_shutdown (void)
250 {
251   if (connector != ((pthread_t) 0))
252   {
253     pthread_kill (connector, SIGTERM);
254     connector = (pthread_t) 0;
255   }
256
257   gps_close (&gpsd_conn);
258
259   sfree (config.port);
260   sfree (config.host);
261
262   return (0);
263 }
264
265 /**
266  * Register the module.
267  */
268 void module_register (void)
269 {
270   config.host = sstrdup (GPS_DEFAULT_HOST);
271   config.port = sstrdup (GPS_DEFAULT_PORT);
272   config.timeout = GPS_DEFAULT_TIMEOUT;
273   config.pause = GPS_DEFAULT_PAUSE;
274
275   plugin_register_complex_config ("gps", cgps_config);
276   plugin_register_init ("gps", cgps_init);
277   plugin_register_read ("gps", cgps_read);
278   plugin_register_shutdown ("gps", cgps_shutdown);
279 }