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>
30 #include "utils_time.h"
31 #include "configfile.h"
33 #define GPS_DEFAULT_HOST "localhost"
34 #define GPS_DEFAULT_PORT "2947"
35 #define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (15)
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"
58 static pthread_t connector = (pthread_t) 0;
60 static cgps_config_t config;
62 static cgps_data_t data = {NAN, NAN, NAN, NAN};
63 static pthread_mutex_t data_lock = PTHREAD_MUTEX_INITIALIZER;
66 * Thread reading from gpsd.
68 static void * cgps_thread (void * pData)
70 struct gps_data_t conn;
77 #if GPSD_API_MAJOR_VERSION > 4
78 int status = gps_open (config.host, config.port, &conn);
80 int status = gps_open_r (config.host, config.port, &conn);
84 WARNING ("gps plugin: Connecting to %s:%s failed: %s",
85 config.host, config.port, gps_errstr (status));
90 gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
91 gps_send (&conn, GPS_CONFIG);
95 #if GPSD_API_MAJOR_VERSION > 4
96 long timeout_ms = CDTIME_T_TO_MS (config.timeout);
97 if (!gps_waiting (&conn, (int) timeout_ms))
99 if (!gps_waiting (&conn))
102 struct timespec pause_ns;
103 CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns);
104 nanosleep (&pause_ns, NULL);
108 if (gps_read (&conn) == -1)
110 WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count);
113 if (err_count > GPS_MAX_ERROR)
115 // Server is not responding ...
116 if (gps_send (&conn, GPS_CONFIG) == -1)
118 WARNING ("gps plugin: gpsd seems to be done, reconnecting");
121 // Server is responding ...
131 pthread_mutex_lock (&data_lock);
133 // Number of sats in view:
134 data.sats_used = (gauge_t) conn.satellites_used;
135 data.sats_visible = (gauge_t) conn.satellites_visible;
137 // dilution of precision:
138 data.vdop = NAN; data.hdop = NAN;
139 if (data.sats_used > 0)
141 data.hdop = conn.dop.hdop;
142 data.vdop = conn.dop.vdop;
146 DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
147 data.sats_used, data.sats_visible, data.hdop, data.vdop);
149 pthread_mutex_unlock (&data_lock);
153 gps_stream (&conn, WATCH_DISABLE, /* data = */ NULL);
156 pthread_exit ((void *) 0);
160 * Submit a piece of the data.
162 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
165 value_list_t vl = VALUE_LIST_INIT;
167 values[0].gauge = value;
171 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
172 sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
173 sstrncpy (vl.type, type, sizeof (vl.type));
174 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
176 plugin_dispatch_values (&vl);
180 * Read the data and submit by piece.
182 static int cgps_read ()
184 cgps_data_t data_copy;
186 pthread_mutex_lock (&data_lock);
188 pthread_mutex_unlock (&data_lock);
190 cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
191 cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
192 cgps_submit ("satellites", data_copy.sats_used, "used");
193 cgps_submit ("satellites", data_copy.sats_visible, "visible");
199 * Read configuration.
201 static int cgps_config (oconfig_item_t *ci)
205 for (i = 0; i < ci->children_num; i++)
207 oconfig_item_t *child = ci->children + i;
209 if (strcasecmp ("Host", child->key) == 0)
210 cf_util_get_string (child, &config.host);
211 else if (strcasecmp ("Port", child->key) == 0)
212 cf_util_get_service (child, &config.port);
213 else if (strcasecmp ("Timeout", child->key) == 0)
214 cf_util_get_cdtime (child, &config.timeout);
215 else if (strcasecmp ("Pause", child->key) == 0)
216 cf_util_get_cdtime (child, &config.pause);
218 WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
227 static int cgps_init (void)
231 DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.3f, pause: %.3f}",
232 config.host, config.port,
233 CDTIME_T_TO_DOUBLE (config.timeout), CDTIME_T_TO_DOUBLE (config.pause));
235 status = plugin_thread_create (&connector, NULL, cgps_thread, NULL);
238 ERROR ("gps plugin: pthread_create() failed.");
248 static int cgps_shutdown (void)
250 if (connector != ((pthread_t) 0))
252 pthread_kill (connector, SIGTERM);
253 connector = (pthread_t) 0;
263 * Register the module.
265 void module_register (void)
267 config.host = sstrdup (GPS_DEFAULT_HOST);
268 config.port = sstrdup (GPS_DEFAULT_PORT);
269 config.timeout = GPS_DEFAULT_TIMEOUT;
270 config.pause = GPS_DEFAULT_PAUSE;
272 plugin_register_complex_config ("gps", cgps_config);
273 plugin_register_init ("gps", cgps_init);
274 plugin_register_read ("gps", cgps_read);
275 plugin_register_shutdown ("gps", cgps_shutdown);