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>
31 #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 pthread_cond_t cgps_thread_cond = PTHREAD_COND_INITIALIZER;
68 static int cgps_thread_shutdown = CGPS_FALSE;
69 static int cgps_thread_running = CGPS_FALSE;
72 * Non blocking pause for the thread.
74 static int cgps_thread_pause(cdtime_t pTime) {
75 cdtime_t until = cdtime() + pTime;
77 pthread_mutex_lock(&cgps_thread_lock);
78 pthread_cond_timedwait(&cgps_thread_cond, &cgps_thread_lock,
79 &CDTIME_T_TO_TIMESPEC(until));
81 int ret = !cgps_thread_shutdown;
83 pthread_mutex_lock(&cgps_thread_lock);
88 * Thread reading from gpsd.
90 static void *cgps_thread(void *pData) {
91 struct gps_data_t gpsd_conn;
92 unsigned int err_count;
93 cgps_thread_running = CGPS_TRUE;
96 pthread_mutex_lock(&cgps_thread_lock);
97 if (cgps_thread_shutdown == CGPS_TRUE) {
100 pthread_mutex_unlock(&cgps_thread_lock);
104 #if GPSD_API_MAJOR_VERSION > 4
106 gps_open(cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
109 gps_open_r(cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
112 WARNING("gps plugin: connecting to %s:%s failed: %s",
113 cgps_config_data.host, cgps_config_data.port, gps_errstr(status));
115 // Here we make a pause until a new tentative to connect, we check also if
116 // the thread does not need to stop.
117 if (cgps_thread_pause(cgps_config_data.pause_connect) == CGPS_FALSE) {
124 gps_stream(&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
125 gps_send(&gpsd_conn, CGPS_CONFIG);
128 pthread_mutex_lock(&cgps_thread_lock);
129 if (cgps_thread_shutdown == CGPS_TRUE) {
132 pthread_mutex_unlock(&cgps_thread_lock);
134 #if GPSD_API_MAJOR_VERSION > 4
135 long timeout_us = CDTIME_T_TO_US(cgps_config_data.timeout);
136 if (!gps_waiting(&gpsd_conn, (int)timeout_us))
138 if (!gps_waiting(&gpsd_conn))
144 if (gps_read(&gpsd_conn) == -1) {
145 WARNING("gps plugin: incorrect data! (err_count: %d)", err_count);
148 if (err_count > CGPS_MAX_ERROR) {
149 // Server is not responding ...
150 if (gps_send(&gpsd_conn, CGPS_CONFIG) == -1) {
151 WARNING("gps plugin: gpsd seems to be down, reconnecting");
152 gps_close(&gpsd_conn);
155 // Server is responding ...
164 pthread_mutex_lock(&cgps_data_lock);
166 // Number of sats in view:
167 cgps_data.sats_used = (gauge_t)gpsd_conn.satellites_used;
168 cgps_data.sats_visible = (gauge_t)gpsd_conn.satellites_visible;
170 // dilution of precision:
171 cgps_data.vdop = NAN;
172 cgps_data.hdop = NAN;
173 if (cgps_data.sats_used > 0) {
174 cgps_data.hdop = gpsd_conn.dop.hdop;
175 cgps_data.vdop = gpsd_conn.dop.vdop;
178 DEBUG("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = "
180 cgps_data.sats_used, cgps_data.sats_visible, cgps_data.hdop,
183 pthread_mutex_unlock(&cgps_data_lock);
188 DEBUG("gps plugin: thread closing gpsd connection ... ");
189 gps_stream(&gpsd_conn, WATCH_DISABLE, NULL);
190 gps_close(&gpsd_conn);
192 DEBUG("gps plugin: thread shutting down ... ");
193 cgps_thread_running = CGPS_FALSE;
194 pthread_mutex_unlock(&cgps_thread_lock);
199 * Submit a piece of the data.
201 static void cgps_submit(const char *type, gauge_t value,
202 const char *type_instance) {
203 value_list_t vl = VALUE_LIST_INIT;
205 vl.values = &(value_t){.gauge = value};
207 sstrncpy(vl.plugin, "gps", sizeof(vl.plugin));
208 sstrncpy(vl.type, type, sizeof(vl.type));
209 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
211 plugin_dispatch_values(&vl);
215 * Read the data and submit by piece.
217 static int cgps_read(void) {
218 cgps_data_t data_copy;
220 pthread_mutex_lock(&cgps_data_lock);
221 data_copy = cgps_data;
222 pthread_mutex_unlock(&cgps_data_lock);
224 cgps_submit("dilution_of_precision", data_copy.hdop, "horizontal");
225 cgps_submit("dilution_of_precision", data_copy.vdop, "vertical");
226 cgps_submit("satellites", data_copy.sats_used, "used");
227 cgps_submit("satellites", data_copy.sats_visible, "visible");
233 * Read configuration.
235 static int cgps_config(oconfig_item_t *ci) {
238 for (i = 0; i < ci->children_num; i++) {
239 oconfig_item_t *child = ci->children + i;
241 if (strcasecmp("Host", child->key) == 0)
242 cf_util_get_string(child, &cgps_config_data.host);
243 else if (strcasecmp("Port", child->key) == 0)
244 cf_util_get_service(child, &cgps_config_data.port);
245 else if (strcasecmp("Timeout", child->key) == 0)
246 cf_util_get_cdtime(child, &cgps_config_data.timeout);
247 else if (strcasecmp("PauseConnect", child->key) == 0)
248 cf_util_get_cdtime(child, &cgps_config_data.pause_connect);
250 WARNING("gps plugin: Ignoring unknown config option \"%s\".", child->key);
253 // Controlling the value for timeout:
254 // If set too high it blocks the reading (> 5 s), too low it gets not reading
256 // To avoid any issues we replace "out of range" value by the default value.
257 if (cgps_config_data.timeout > TIME_T_TO_CDTIME_T(5) ||
258 cgps_config_data.timeout < US_TO_CDTIME_T(500)) {
259 WARNING("gps plugin: timeout set to %.6f sec. setting to default (%.6f).",
260 CDTIME_T_TO_DOUBLE(cgps_config_data.timeout),
261 CDTIME_T_TO_DOUBLE(CGPS_DEFAULT_TIMEOUT));
262 cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
271 static int cgps_init(void) {
274 if (cgps_thread_running == CGPS_TRUE) {
275 DEBUG("gps plugin: error gps thread already running ... ");
279 DEBUG("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.6f sec., "
280 "pause connect: %.3f sec.}",
281 cgps_config_data.host, cgps_config_data.port,
282 CDTIME_T_TO_DOUBLE(cgps_config_data.timeout),
283 CDTIME_T_TO_DOUBLE(cgps_config_data.pause_connect));
286 plugin_thread_create(&cgps_thread_id, NULL, cgps_thread, NULL, "gps");
288 ERROR("gps plugin: pthread_create() failed.");
298 static int cgps_shutdown(void) {
301 pthread_mutex_lock(&cgps_thread_lock);
302 cgps_thread_shutdown = CGPS_TRUE;
303 pthread_cond_broadcast(&cgps_thread_cond);
304 pthread_mutex_unlock(&cgps_thread_lock);
306 pthread_join(cgps_thread_id, &res);
310 pthread_mutex_unlock(&cgps_thread_lock);
311 pthread_mutex_destroy(&cgps_thread_lock);
312 pthread_mutex_unlock(&cgps_data_lock);
313 pthread_mutex_destroy(&cgps_data_lock);
315 sfree(cgps_config_data.port);
316 sfree(cgps_config_data.host);
322 * Register the module.
324 void module_register(void) {
325 cgps_config_data.host = sstrdup(CGPS_DEFAULT_HOST);
326 cgps_config_data.port = sstrdup(CGPS_DEFAULT_PORT);
327 cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
328 cgps_config_data.pause_connect = CGPS_DEFAULT_PAUSE_CONNECT;
330 plugin_register_complex_config("gps", cgps_config);
331 plugin_register_init("gps", cgps_init);
332 plugin_register_read("gps", cgps_read);
333 plugin_register_shutdown("gps", cgps_shutdown);