Fixed time to ms instead of us, added a possibility to reconnect if gpsd server resta...
[collectd.git] / src / gps.c
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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_time.h"
31 #include "configfile.h"
32
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"
39
40 #include <gps.h>
41 #include <pthread.h>
42
43 typedef struct {
44   char *host;
45   char *port;
46   cdtime_t timeout;
47   cdtime_t pause;
48 } cgps_config_t;
49
50 typedef struct {
51   gauge_t sats_used;
52   gauge_t sats_visible;
53   gauge_t hdop;
54   gauge_t vdop;
55 } cgps_data_t;
56
57 // Thread items:
58 static pthread_t connector = (pthread_t) 0;
59
60 static cgps_config_t config;
61
62 static cgps_data_t      data = {NAN, NAN, NAN, NAN};
63 static pthread_mutex_t  data_lock = PTHREAD_MUTEX_INITIALIZER;
64
65 /**
66  * Thread reading from gpsd.
67  */
68 static void * cgps_thread (void * pData)
69 {
70   struct gps_data_t conn;
71   int err_count;
72
73   while (1)
74   {
75     err_count = 0;
76
77     int status = gps_open (config.host, config.port, &conn);
78     if (status < 0)
79     {
80       WARNING ("gps plugin: Connecting to %s:%s failed: %s",
81                config.host, config.port, gps_errstr (status));
82       sleep (60);
83       continue;
84     }
85
86     gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
87     gps_send (&conn, GPS_CONFIG);
88
89     while (1)
90     {
91       long timeout_ms = CDTIME_T_TO_MS (config.timeout);
92       if (!gps_waiting (&conn, (int) timeout_ms))
93       {
94         struct timespec pause_ns;
95         CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns);
96         nanosleep (&pause_ns, NULL);
97         continue;
98       }
99
100       if (gps_read (&conn) == -1)
101       {
102         WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count);
103         err_count++;
104
105         if (err_count > GPS_MAX_ERROR)
106         {
107           // Server is not responding ...
108           if (gps_send (&conn, GPS_CONFIG) == -1)
109           {
110             WARNING ("gps plugin: gpsd seems to be done, reconnecting");
111             break;
112           }
113           // Server is responding ...
114           else
115           {
116             err_count = 0;
117           }
118         }
119
120         continue;
121       }
122
123       pthread_mutex_lock (&data_lock);
124
125       // Number of sats in view:
126       data.sats_used = (gauge_t) conn.satellites_used;
127       data.sats_visible = (gauge_t) conn.satellites_visible;
128
129       // dilution of precision:
130       data.vdop = NAN; data.hdop = NAN;
131       if (data.sats_used > 0)
132       {
133         data.hdop = conn.dop.hdop;
134         data.vdop = conn.dop.vdop;
135       }
136
137
138       DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
139              data.sats_used, data.sats_visible, data.hdop, data.vdop);
140
141       pthread_mutex_unlock (&data_lock);
142     }
143   }
144
145   gps_stream (&conn, WATCH_DISABLE, /* data = */ NULL);
146   gps_close (&conn);
147
148   pthread_exit ((void *) 0);
149 }
150
151 /**
152  * Submit a piece of the data.
153  */
154 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
155 {
156   value_t values[1];
157   value_list_t vl = VALUE_LIST_INIT;
158
159   values[0].gauge = value;
160
161   vl.values = values;
162   vl.values_len = 1;
163   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164   sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
165   sstrncpy (vl.type, type, sizeof (vl.type));
166   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
167
168   plugin_dispatch_values (&vl);
169 }
170
171 /**
172  * Read the data and submit by piece.
173  */
174 static int cgps_read ()
175 {
176   cgps_data_t data_copy;
177
178   pthread_mutex_lock (&data_lock);
179   data_copy = data;
180   pthread_mutex_unlock (&data_lock);
181
182   cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
183   cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
184   cgps_submit ("satellites", data_copy.sats_used, "used");
185   cgps_submit ("satellites", data_copy.sats_visible, "visible");
186
187   return (0);
188 }
189
190 /**
191  * Read configuration.
192  */
193 static int cgps_config (oconfig_item_t *ci)
194 {
195   int i;
196
197   for (i = 0; i < ci->children_num; i++)
198   {
199     oconfig_item_t *child = ci->children + i;
200
201     if (strcasecmp ("Host", child->key) == 0)
202       cf_util_get_string (child, &config.host);
203     else if (strcasecmp ("Port", child->key) == 0)
204       cf_util_get_service (child, &config.port);
205     else if (strcasecmp ("Timeout", child->key) == 0)
206       cf_util_get_cdtime (child, &config.timeout);
207     else if (strcasecmp ("Pause", child->key) == 0)
208       cf_util_get_cdtime (child, &config.pause);
209     else
210       WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
211   }
212
213   return 0;
214 }
215
216 /**
217  * Init.
218  */
219 static int cgps_init (void)
220 {
221   int status;
222
223   DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %.3f, pause: %.3f}",
224          config.host, config.port,
225          CDTIME_T_TO_DOUBLE (config.timeout), CDTIME_T_TO_DOUBLE (config.pause));
226
227   status = plugin_thread_create (&connector, NULL, cgps_thread, NULL);
228   if (status != 0)
229   {
230     ERROR ("gps plugin: pthread_create() failed.");
231     return (-1);
232   }
233
234   return (0);
235 }
236
237 /**
238  * Shutdown.
239  */
240 static int cgps_shutdown (void)
241 {
242   if (connector != ((pthread_t) 0))
243   {
244     pthread_kill (connector, SIGTERM);
245     connector = (pthread_t) 0;
246   }
247
248   sfree (config.port);
249   sfree (config.host);
250
251   return (0);
252 }
253
254 /**
255  * Register the module.
256  */
257 void module_register (void)
258 {
259   config.host = sstrdup (GPS_DEFAULT_HOST);
260   config.port = sstrdup (GPS_DEFAULT_PORT);
261   config.timeout = GPS_DEFAULT_TIMEOUT;
262   config.pause = GPS_DEFAULT_PAUSE;
263
264   plugin_register_complex_config ("gps", cgps_config);
265   plugin_register_init ("gps", cgps_init);
266   plugin_register_read ("gps", cgps_read);
267   plugin_register_shutdown ("gps", cgps_shutdown);
268 }