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"
33 #include "configfile.h"
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"
53 cdtime_t pause_connect;
63 static cgps_config_t cgps_config_data;
65 static cgps_data_t cgps_data = {NAN, NAN, NAN, NAN};
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;
74 * Non blocking pause for the thread.
76 static int cgps_thread_pause(cdtime_t pTime)
80 struct timespec pause_th;
81 CDTIME_T_TO_TIMESPEC (MS_TO_CDTIME_T(10), &pause_th);
84 if ( (cdtime () - now) > pTime )
89 pthread_mutex_lock (&cgps_thread_lock);
90 if (cgps_thread_shutdown == CGPS_TRUE)
94 pthread_mutex_unlock (&cgps_thread_lock);
95 nanosleep (&pause_th, NULL);
102 * Thread reading from gpsd.
104 static void * cgps_thread (void * pData)
106 struct gps_data_t gpsd_conn;
107 unsigned int err_count;
108 cgps_thread_running = CGPS_TRUE;
112 pthread_mutex_lock (&cgps_thread_lock);
113 if (cgps_thread_shutdown == CGPS_TRUE)
117 pthread_mutex_unlock (&cgps_thread_lock);
121 #if GPSD_API_MAJOR_VERSION > 4
122 int status = gps_open (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
124 int status = gps_open_r (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
128 WARNING ("gps plugin: connecting to %s:%s failed: %s",
129 cgps_config_data.host, cgps_config_data.port, gps_errstr (status));
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)
141 gps_stream (&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
142 gps_send (&gpsd_conn, CGPS_CONFIG);
146 pthread_mutex_lock (&cgps_thread_lock);
147 if (cgps_thread_shutdown == CGPS_TRUE)
151 pthread_mutex_unlock (&cgps_thread_lock);
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 ))
157 if (!gps_waiting (&gpsd_conn))
160 if (cgps_thread_pause(cgps_config_data.pause_read) == CGPS_FALSE)
168 if (gps_read (&gpsd_conn) == -1)
170 WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count);
173 if (err_count > CGPS_MAX_ERROR)
175 // Server is not responding ...
176 if (gps_send (&gpsd_conn, CGPS_CONFIG) == -1)
178 WARNING ("gps plugin: gpsd seems to be down, reconnecting");
179 gps_close (&gpsd_conn);
182 // Server is responding ...
192 pthread_mutex_lock (&cgps_data_lock);
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;
198 // dilution of precision:
199 cgps_data.vdop = NAN;
200 cgps_data.hdop = NAN;
201 if (cgps_data.sats_used > 0)
203 cgps_data.hdop = gpsd_conn.dop.hdop;
204 cgps_data.vdop = gpsd_conn.dop.vdop;
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);
210 pthread_mutex_unlock (&cgps_data_lock);
215 DEBUG ("gps plugin: thread closing gpsd connection ... ");
216 gps_stream (&gpsd_conn, WATCH_DISABLE, NULL);
217 gps_close (&gpsd_conn);
219 DEBUG ("gps plugin: thread shutting down ... ");
220 cgps_thread_running = CGPS_FALSE;
221 pthread_mutex_unlock (&cgps_thread_lock);
227 * Submit a piece of the data.
229 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
232 value_list_t vl = VALUE_LIST_INIT;
234 values[0].gauge = value;
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));
243 plugin_dispatch_values (&vl);
247 * Read the data and submit by piece.
249 static int cgps_read ()
251 cgps_data_t data_copy;
253 pthread_mutex_lock (&cgps_data_lock);
254 data_copy = cgps_data;
255 pthread_mutex_unlock (&cgps_data_lock);
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");
266 * Read configuration.
268 static int cgps_config (oconfig_item_t *ci)
272 for (i = 0; i < ci->children_num; i++)
274 oconfig_item_t *child = ci->children + i;
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);
287 WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
296 static int cgps_init (void)
300 if (cgps_thread_running == CGPS_TRUE)
302 DEBUG ("gps plugin: error gps thread already running ... ");
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));
313 status = plugin_thread_create (&cgps_thread_id, NULL, cgps_thread, NULL);
316 ERROR ("gps plugin: pthread_create() failed.");
326 static int cgps_shutdown (void)
330 pthread_mutex_lock (&cgps_thread_lock);
331 cgps_thread_shutdown = CGPS_TRUE;
332 pthread_mutex_unlock (&cgps_thread_lock);
334 pthread_join(cgps_thread_id, &res);
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);
343 sfree (cgps_config_data.port);
344 sfree (cgps_config_data.host);
350 * Register the module.
352 void module_register (void)
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;
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);