gps plugin: Coding style cleanup.
[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 15
36 #define GPS_DEFAULT_PAUSE   1
37
38 #include <gps.h>
39 #include <pthread.h>
40
41 typedef struct {
42   char *host;
43   char *port;
44   int timeout;
45   int pause;
46 } cgps_config_t;
47
48 typedef struct {
49   gauge_t sats_used;
50   gauge_t sats_visible;
51   gauge_t hdop;
52   gauge_t vdop;
53 } cgps_data_t;
54
55 static const char *config_keys[] =
56 {
57   "Host",
58   "Port",
59   "Timeout",
60   "Pause"
61 };
62 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63
64 // Thread items:
65 static pthread_t connector = (pthread_t) 0;
66
67 static cgps_config_t config;
68
69 static cgps_data_t      data = {NAN, NAN, NAN, NAN};
70 static pthread_mutex_t  data_lock = PTHREAD_MUTEX_INITIALIZER;
71
72 /**
73  * Thread reading from gpsd.
74  */
75 static void * gps_collectd_thread (void * pData)
76 {
77   struct gps_data_t conn;
78
79   while (1)
80   {
81     int status = gps_open (config.host, config.port, &conn);
82     if (status < 0)
83     {
84       WARNING ("gps plugin: Connecting to %s:%s failed: %s",
85                config.host, config.port, gps_errstr (status));
86       sleep (60);
87       continue;
88     }
89
90     gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
91     gps_send (&conn, "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n");
92
93     while (1)
94     {
95       if (!gps_waiting (&conn, config.timeout))
96       {
97         sleep (config.pause);
98         continue;
99       }
100
101       if (gps_read (&conn) == -1)
102       {
103         WARNING ("gps plugin: incorrect data!");
104         continue;
105       }
106
107       pthread_mutex_lock (&data_lock);
108
109       // Number of sats in view:
110       data.sats_used = (gauge_t) conn.satellites_used;
111       data.sats_visible = (gauge_t) conn.satellites_visible;
112
113       // dilution of precision:
114       data.vdop = NAN; data.hdop = NAN;
115       if (data.sats_used > 0)
116       {
117         data.hdop = conn.dop.hdop;
118         data.vdop = conn.dop.vdop;
119       }
120
121
122       DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
123              data.sats_used, data.sats_visible, data.hdop, data.vdop);
124
125       pthread_mutex_unlock (&data_lock);
126     }
127   }
128
129   gps_stream (&conn, WATCH_DISABLE, /* data = */ NULL);
130   gps_close (&conn);
131
132   pthread_exit ((void *) 0);
133 }
134
135 /**
136  * Submit a piece of the data.
137  */
138 static void cgps_submit (const char *type, gauge_t value, const char *type_instance)
139 {
140   value_t values[1];
141   value_list_t vl = VALUE_LIST_INIT;
142
143   values[0].gauge = value;
144
145   vl.values = values;
146   vl.values_len = 1;
147   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
148   sstrncpy (vl.plugin, "gps", sizeof (vl.plugin));
149   sstrncpy (vl.type, type, sizeof (vl.type));
150   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
151
152   plugin_dispatch_values (&vl);
153 }
154
155 /**
156  * Read the data and submit by piece.
157  */
158 static int cgps_read ()
159 {
160   cgps_data_t data_copy;
161
162   pthread_mutex_lock (&data_lock);
163   data_copy = data;
164   pthread_mutex_unlock (&data_lock);
165
166   cgps_submit ("dilution_of_precision", data_copy.hdop, "horizontal");
167   cgps_submit ("dilution_of_precision", data_copy.vdop, "vertical");
168   cgps_submit ("satellites", data_copy.sats_used, "used");
169   cgps_submit ("satellites", data_copy.sats_visible, "visible");
170
171   return (0);
172 }
173
174 /**
175  * Read configuration.
176  */
177 static int cgps_config (const char *key, const char *value)
178 {
179   char *endptr = NULL;
180
181   if (strcasecmp (key, "Host") == 0)
182   {
183     free (config.host);
184     config.host = sstrdup (value);
185   }
186   else if (strcasecmp (key, "Port") == 0)
187   {
188     free (config.port);
189     config.port = sstrdup (value);
190   }
191   else if (strcasecmp (key, "Timeout") == 0)
192   {
193     config.timeout = (int) (strtod(value, &endptr) * 1000);
194   }
195   else if (strcasecmp (key, "Pause") == 0)
196   {
197     config.pause = (int) (strtod (value, &endptr));
198   }
199
200   return (0);
201 }
202
203 /**
204  * Init.
205  */
206 static int cgps_init (void)
207 {
208   int status;
209
210   DEBUG ("gps plugin: config{host: \"%s\", port: \"%s\", timeout: %d, pause: %d}",
211          config.host, config.port, config.timeout, config.pause);
212
213   status = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL);
214   if (status != 0)
215   {
216     ERROR ("gps plugin: pthread_create() failed.");
217     return (-1);
218   }
219
220   return (0);
221 }
222
223 /**
224  * Shutdown.
225  */
226 static int cgps_shutdown (void)
227 {
228   if (connector != ((pthread_t) 0))
229   {
230     pthread_kill (connector, SIGTERM);
231     connector = (pthread_t) 0;
232   }
233
234   sfree (config.port);
235   sfree (config.host);
236
237   return (0);
238 }
239
240 /**
241  * Register the module.
242  */
243 void module_register (void)
244 {
245   config.host = sstrdup (GPS_DEFAULT_HOST);
246   config.port = sstrdup (GPS_DEFAULT_PORT);
247   config.timeout = GPS_DEFAULT_TIMEOUT;
248   config.pause = GPS_DEFAULT_PAUSE;
249
250   plugin_register_config ("gps", cgps_config, config_keys, config_keys_num);
251   plugin_register_init ("gps", cgps_init);
252   plugin_register_read ("gps", cgps_read);
253   plugin_register_shutdown ("gps", cgps_shutdown);
254 }