3 * Copyright (C) 2015 Nicolas JOURDEN
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Nicolas JOURDEN <nicolas.jourden at laposte.net>
25 * Florian octo Forster <octo at collectd.org>
26 * Marc Fournier <marc.fournier at camptocamp.com>
32 #include "utils_time.h"
36 #define CGPS_DEFAULT_HOST "localhost"
37 #define CGPS_DEFAULT_PORT "2947" /* DEFAULT_GPSD_PORT */
38 #define CGPS_DEFAULT_TIMEOUT MS_TO_CDTIME_T (15)
39 #define CGPS_DEFAULT_PAUSE_CONNECT TIME_T_TO_CDTIME_T (5)
40 #define CGPS_MAX_ERROR 100
41 #define CGPS_CONFIG "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n"
50 cdtime_t pause_connect;
60 static cgps_config_t cgps_config_data;
62 static cgps_data_t cgps_data = {NAN, NAN, NAN, NAN};
64 static pthread_t cgps_thread_id;
65 static pthread_mutex_t cgps_data_lock = PTHREAD_MUTEX_INITIALIZER;
66 static pthread_mutex_t cgps_thread_lock = PTHREAD_MUTEX_INITIALIZER;
67 static int cgps_thread_shutdown = CGPS_FALSE;
68 static int cgps_thread_running = CGPS_FALSE;
71 * Non blocking pause for the thread.
73 static int cgps_thread_pause(cdtime_t pTime)
77 struct timespec pause_th;
78 CDTIME_T_TO_TIMESPEC (MS_TO_CDTIME_T(10), &pause_th);
81 if ( (cdtime () - now) > pTime )
86 pthread_mutex_lock (&cgps_thread_lock);
87 if (cgps_thread_shutdown == CGPS_TRUE)
91 pthread_mutex_unlock (&cgps_thread_lock);
92 nanosleep (&pause_th, NULL);
99 * Thread reading from gpsd.
101 static void * cgps_thread (void * pData)
103 struct gps_data_t gpsd_conn;
104 unsigned int err_count;
105 cgps_thread_running = CGPS_TRUE;
109 pthread_mutex_lock (&cgps_thread_lock);
110 if (cgps_thread_shutdown == CGPS_TRUE)
114 pthread_mutex_unlock (&cgps_thread_lock);
118 #if GPSD_API_MAJOR_VERSION > 4
119 int status = gps_open (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
121 int status = gps_open_r (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
125 WARNING ("gps plugin: connecting to %s:%s failed: %s",
126 cgps_config_data.host, cgps_config_data.port, gps_errstr (status));
128 // Here we make a pause until a new tentative to connect, we check also if
129 // the thread does not need to stop.
130 if (cgps_thread_pause(cgps_config_data.pause_connect) == CGPS_FALSE)
138 gps_stream (&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
139 gps_send (&gpsd_conn, CGPS_CONFIG);
143 pthread_mutex_lock (&cgps_thread_lock);
144 if (cgps_thread_shutdown == CGPS_TRUE)
148 pthread_mutex_unlock (&cgps_thread_lock);
150 #if GPSD_API_MAJOR_VERSION > 4
151 long timeout_us = CDTIME_T_TO_US (cgps_config_data.timeout);
152 if (!gps_waiting (&gpsd_conn, (int) timeout_us ))
154 if (!gps_waiting (&gpsd_conn))
160 if (gps_read (&gpsd_conn) == -1)
162 WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count);
165 if (err_count > CGPS_MAX_ERROR)
167 // Server is not responding ...
168 if (gps_send (&gpsd_conn, CGPS_CONFIG) == -1)
170 WARNING ("gps plugin: gpsd seems to be down, reconnecting");
171 gps_close (&gpsd_conn);
174 // Server is responding ...
184 pthread_mutex_lock (&cgps_data_lock);
186 // Number of sats in view:
187 cgps_data.sats_used = (gauge_t) gpsd_conn.satellites_used;
188 cgps_data.sats_visible = (gauge_t) gpsd_conn.satellites_visible;
190 // dilution of precision:
191 cgps_data.vdop = NAN;
192 cgps_data.hdop = NAN;
193 if (cgps_data.sats_used > 0)
195 cgps_data.hdop = gpsd_conn.dop.hdop;
196 cgps_data.vdop = gpsd_conn.dop.vdop;
199 DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
200 cgps_data.sats_used, cgps_data.sats_visible, cgps_data.hdop, cgps_data.vdop);
202 pthread_mutex_unlock (&cgps_data_lock);
207 DEBUG ("gps plugin: thread closing gpsd connection ... ");
208 gps_stream (&gpsd_conn, WATCH_DISABLE, NULL);
209 gps_close (&gpsd_conn);
211 DEBUG ("gps plugin: thread shutting down ... ");
212 cgps_thread_running = CGPS_FALSE;
213 pthread_mutex_unlock (&cgps_thread_lock);
219 * Submit a piece of the data.
221 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
224 value_list_t vl = VALUE_LIST_INIT;
226 values[0].gauge = value;
230 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
231 sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
232 sstrncpy (vl.type, type, sizeof (vl.type));
233 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
235 plugin_dispatch_values (&vl);
239 * Read the data and submit by piece.
241 static int cgps_read (void)
243 cgps_data_t data_copy;
245 pthread_mutex_lock (&cgps_data_lock);
246 data_copy = cgps_data;
247 pthread_mutex_unlock (&cgps_data_lock);
249 cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
250 cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
251 cgps_submit ("satellites", data_copy.sats_used, "used");
252 cgps_submit ("satellites", data_copy.sats_visible, "visible");
258 * Read configuration.
260 static int cgps_config (oconfig_item_t *ci)
264 for (i = 0; i < ci->children_num; i++)
266 oconfig_item_t *child = ci->children + i;
268 if (strcasecmp ("Host", child->key) == 0)
269 cf_util_get_string (child, &cgps_config_data.host);
270 else if (strcasecmp ("Port", child->key) == 0)
271 cf_util_get_service (child, &cgps_config_data.port);
272 else if (strcasecmp ("Timeout", child->key) == 0)
273 cf_util_get_cdtime (child, &cgps_config_data.timeout);
274 else if (strcasecmp ("PauseConnect", child->key) == 0)
275 cf_util_get_cdtime (child, &cgps_config_data.pause_connect);
277 WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
280 // Controlling the value for timeout:
281 // If set too high it blocks the reading (> 5 s), too low it gets not reading (< 500 us).
282 // To avoid any issues we replace "out of range" value by the default value.
284 cgps_config_data.timeout > TIME_T_TO_CDTIME_T(5)
286 cgps_config_data.timeout < US_TO_CDTIME_T(500)
289 WARNING ("gps plugin: timeout set to %.6f sec. setting to default (%.6f).",
290 CDTIME_T_TO_DOUBLE(cgps_config_data.timeout),
291 CDTIME_T_TO_DOUBLE(CGPS_DEFAULT_TIMEOUT)
293 cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
302 static int cgps_init (void)
306 if (cgps_thread_running == CGPS_TRUE)
308 DEBUG ("gps plugin: error gps thread already running ... ");
312 DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.6f sec., pause connect: %.3f sec.}",
313 cgps_config_data.host, cgps_config_data.port,
314 CDTIME_T_TO_DOUBLE (cgps_config_data.timeout),
315 CDTIME_T_TO_DOUBLE (cgps_config_data.pause_connect));
317 status = plugin_thread_create (&cgps_thread_id, NULL, cgps_thread, NULL);
320 ERROR ("gps plugin: pthread_create() failed.");
330 static int cgps_shutdown (void)
334 pthread_mutex_lock (&cgps_thread_lock);
335 cgps_thread_shutdown = CGPS_TRUE;
336 pthread_mutex_unlock (&cgps_thread_lock);
338 pthread_join(cgps_thread_id, &res);
342 pthread_mutex_unlock(&cgps_thread_lock);
343 pthread_mutex_destroy(&cgps_thread_lock);
344 pthread_mutex_unlock(&cgps_data_lock);
345 pthread_mutex_destroy(&cgps_data_lock);
347 sfree (cgps_config_data.port);
348 sfree (cgps_config_data.host);
354 * Register the module.
356 void module_register (void)
358 cgps_config_data.host = sstrdup (CGPS_DEFAULT_HOST);
359 cgps_config_data.port = sstrdup (CGPS_DEFAULT_PORT);
360 cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
361 cgps_config_data.pause_connect = CGPS_DEFAULT_PAUSE_CONNECT;
363 plugin_register_complex_config ("gps", cgps_config);
364 plugin_register_init ("gps", cgps_init);
365 plugin_register_read ("gps", cgps_read);
366 plugin_register_shutdown ("gps", cgps_shutdown);