Debian Codegraph @

src/gps.c

raw
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 * Florian octo Forster <octo at collectd.org>
26 * Marc Fournier <marc.fournier at camptocamp.com>
27 **/
28
29 #include "collectd.h"
30 #include "plugin.h"
31 #include "utils/common/common.h"
32 #include "utils_time.h"
33
34 #define CGPS_TRUE 1
35 #define CGPS_FALSE 0
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"
42
43 #include <gps.h>
44 #include <pthread.h>
45
46 typedef struct {
47 char *host;
48 char *port;
49 cdtime_t timeout;
50 cdtime_t pause_connect;
51 } cgps_config_t;
52
53 typedef struct {
54 gauge_t sats_used;
55 gauge_t sats_visible;
56 gauge_t hdop;
57 gauge_t vdop;
58 } cgps_data_t;
59
60 static cgps_config_t cgps_config_data;
61
62 static cgps_data_t cgps_data = {NAN, NAN, NAN, NAN};
63
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;
70
71 /**
72 * Non blocking pause for the thread.
73 */
74 static int cgps_thread_pause(cdtime_t pTime) {
75 cdtime_t until = cdtime() + pTime;
76
77 pthread_mutex_lock(&cgps_thread_lock);
78 pthread_cond_timedwait(&cgps_thread_cond, &cgps_thread_lock,
79 &CDTIME_T_TO_TIMESPEC(until));
80
81 int ret = !cgps_thread_shutdown;
82
83 pthread_mutex_unlock(&cgps_thread_lock);
84 return ret;
85 }
86
87 /**
88 * Thread reading from gpsd.
89 */
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;
94
95 while (CGPS_TRUE) {
96 pthread_mutex_lock(&cgps_thread_lock);
97 if (cgps_thread_shutdown == CGPS_TRUE) {
98 goto quit;
99 }
100 pthread_mutex_unlock(&cgps_thread_lock);
101
102 err_count = 0;
103
104 #if GPSD_API_MAJOR_VERSION > 4
105 int status =
106 gps_open(cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
107 #else
108 int status =
109 gps_open_r(cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
110 #endif
111 if (status < 0) {
112 WARNING("gps plugin: connecting to %s:%s failed: %s",
113 cgps_config_data.host, cgps_config_data.port, gps_errstr(status));
114
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) {
118 goto quit;
119 }
120
121 continue;
122 }
123
124 gps_stream(&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
125 gps_send(&gpsd_conn, CGPS_CONFIG);
126
127 while (CGPS_TRUE) {
128 pthread_mutex_lock(&cgps_thread_lock);
129 if (cgps_thread_shutdown == CGPS_TRUE) {
130 goto stop;
131 }
132 pthread_mutex_unlock(&cgps_thread_lock);
133
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))
137 #else
138 if (!gps_waiting(&gpsd_conn))
139 #endif
140 {
141 continue;
142 }
143
144 #if GPSD_API_MAJOR_VERSION > 6
145 if (gps_read(&gpsd_conn, NULL, 0) == -1)
146 #else
147 if (gps_read(&gpsd_conn) == -1)
148 #endif
149 {
150 WARNING("gps plugin: incorrect data! (err_count: %d)", err_count);
151 err_count++;
152
153 if (err_count > CGPS_MAX_ERROR) {
154 // Server is not responding ...
155 if (gps_send(&gpsd_conn, CGPS_CONFIG) == -1) {
156 WARNING("gps plugin: gpsd seems to be down, reconnecting");
157 gps_close(&gpsd_conn);
158 break;
159 }
160 // Server is responding ...
161 else {
162 err_count = 0;
163 }
164 }
165
166 continue;
167 }
168
169 pthread_mutex_lock(&cgps_data_lock);
170
171 // Number of sats in view:
172 cgps_data.sats_used = (gauge_t)gpsd_conn.satellites_used;
173 cgps_data.sats_visible = (gauge_t)gpsd_conn.satellites_visible;
174
175 // dilution of precision:
176 cgps_data.vdop = NAN;
177 cgps_data.hdop = NAN;
178 if (cgps_data.sats_used > 0) {
179 cgps_data.hdop = gpsd_conn.dop.hdop;
180 cgps_data.vdop = gpsd_conn.dop.vdop;
181 }
182
183 DEBUG("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = "
184 "%.3f",
185 cgps_data.sats_used, cgps_data.sats_visible, cgps_data.hdop,
186 cgps_data.vdop);
187
188 pthread_mutex_unlock(&cgps_data_lock);
189 }
190 }
191
192 stop:
193 DEBUG("gps plugin: thread closing gpsd connection ... ");
194 gps_stream(&gpsd_conn, WATCH_DISABLE, NULL);
195 gps_close(&gpsd_conn);
196 quit:
197 DEBUG("gps plugin: thread shutting down ... ");
198 cgps_thread_running = CGPS_FALSE;
199 pthread_mutex_unlock(&cgps_thread_lock);
200 pthread_exit(NULL);
201 }
202
203 /**
204 * Submit a piece of the data.
205 */
206 static void cgps_submit(const char *type, gauge_t value,
207 const char *type_instance) {
208 value_list_t vl = VALUE_LIST_INIT;
209
210 vl.values = &(value_t){.gauge = value};
211 vl.values_len = 1;
212 sstrncpy(vl.plugin, "gps", sizeof(vl.plugin));
213 sstrncpy(vl.type, type, sizeof(vl.type));
214 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
215
216 plugin_dispatch_values(&vl);
217 }
218
219 /**
220 * Read the data and submit by piece.
221 */
222 static int cgps_read(void) {
223 cgps_data_t data_copy;
224
225 pthread_mutex_lock(&cgps_data_lock);
226 data_copy = cgps_data;
227 pthread_mutex_unlock(&cgps_data_lock);
228
229 cgps_submit("dilution_of_precision", data_copy.hdop, "horizontal");
230 cgps_submit("dilution_of_precision", data_copy.vdop, "vertical");
231 cgps_submit("satellites", data_copy.sats_used, "used");
232 cgps_submit("satellites", data_copy.sats_visible, "visible");
233
234 return 0;
235 }
236
237 /**
238 * Read configuration.
239 */
240 static int cgps_config(oconfig_item_t *ci) {
241 int i;
242
243 for (i = 0; i < ci->children_num; i++) {
244 oconfig_item_t *child = ci->children + i;
245
246 if (strcasecmp("Host", child->key) == 0)
247 cf_util_get_string(child, &cgps_config_data.host);
248 else if (strcasecmp("Port", child->key) == 0)
249 cf_util_get_service(child, &cgps_config_data.port);
250 else if (strcasecmp("Timeout", child->key) == 0)
251 cf_util_get_cdtime(child, &cgps_config_data.timeout);
252 else if (strcasecmp("PauseConnect", child->key) == 0)
253 cf_util_get_cdtime(child, &cgps_config_data.pause_connect);
254 else
255 WARNING("gps plugin: Ignoring unknown config option \"%s\".", child->key);
256 }
257
258 // Controlling the value for timeout:
259 // If set too high it blocks the reading (> 5 s), too low it gets not reading
260 // (< 500 us).
261 // To avoid any issues we replace "out of range" value by the default value.
262 if (cgps_config_data.timeout > TIME_T_TO_CDTIME_T(5) ||
263 cgps_config_data.timeout < US_TO_CDTIME_T(500)) {
264 WARNING("gps plugin: timeout set to %.6f sec. setting to default (%.6f).",
265 CDTIME_T_TO_DOUBLE(cgps_config_data.timeout),
266 CDTIME_T_TO_DOUBLE(CGPS_DEFAULT_TIMEOUT));
267 cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
268 }
269
270 return 0;
271 }
272
273 /**
274 * Init.
275 */
276 static int cgps_init(void) {
277 int status;
278
279 if (cgps_thread_running == CGPS_TRUE) {
280 DEBUG("gps plugin: error gps thread already running ... ");
281 return 0;
282 }
283
284 DEBUG("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.6f sec., "
285 "pause connect: %.3f sec.}",
286 cgps_config_data.host, cgps_config_data.port,
287 CDTIME_T_TO_DOUBLE(cgps_config_data.timeout),
288 CDTIME_T_TO_DOUBLE(cgps_config_data.pause_connect));
289
290 status = plugin_thread_create(&cgps_thread_id, cgps_thread, NULL, "gps");
291 if (status != 0) {
292 ERROR("gps plugin: pthread_create() failed.");
293 return -1;
294 }
295
296 return 0;
297 }
298
299 /**
300 * Shutdown.
301 */
302 static int cgps_shutdown(void) {
303 void *res;
304
305 pthread_mutex_lock(&cgps_thread_lock);
306 cgps_thread_shutdown = CGPS_TRUE;
307 pthread_cond_broadcast(&cgps_thread_cond);
308 pthread_mutex_unlock(&cgps_thread_lock);
309
310 pthread_join(cgps_thread_id, &res);
311 free(res);
312
313 // Clean mutex:
314 pthread_mutex_destroy(&cgps_thread_lock);
315 pthread_mutex_unlock(&cgps_data_lock);
316 pthread_mutex_destroy(&cgps_data_lock);
317
318 sfree(cgps_config_data.port);
319 sfree(cgps_config_data.host);
320
321 return 0;
322 }
323
324 /**
325 * Register the module.
326 */
327 void module_register(void) {
328 cgps_config_data.host = sstrdup(CGPS_DEFAULT_HOST);
329 cgps_config_data.port = sstrdup(CGPS_DEFAULT_PORT);
330 cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
331 cgps_config_data.pause_connect = CGPS_DEFAULT_PAUSE_CONNECT;
332
333 plugin_register_complex_config("gps", cgps_config);
334 plugin_register_init("gps", cgps_init);
335 plugin_register_read("gps", cgps_read);
336 plugin_register_shutdown("gps", cgps_shutdown);
337 }