Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / onewire.c
1 /**
2  * collectd - src/owfs.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at noris.net>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
26
27 #include <owcapi.h>
28
29 #define OW_FAMILY_LENGTH 8
30 #define OW_FAMILY_MAX_FEATURES 2
31 struct ow_family_features_s
32 {
33   char family[OW_FAMILY_LENGTH];
34   struct
35   {
36     char filename[DATA_MAX_NAME_LEN];
37     char type[DATA_MAX_NAME_LEN];
38     char type_instance[DATA_MAX_NAME_LEN];
39   } features[OW_FAMILY_MAX_FEATURES];
40   size_t features_num;
41 };
42 typedef struct ow_family_features_s ow_family_features_t;
43
44 /* see http://owfs.sourceforge.net/ow_table.html for a list of families */
45 static ow_family_features_t ow_family_features[] =
46 {
47   {
48     /* family = */ "10.",
49     {
50       {
51         /* filename = */ "temperature",
52         /* type = */ "temperature",
53         /* type_instance = */ ""
54       }
55     },
56     /* features_num = */ 1
57   }
58 };
59 static int ow_family_features_num = STATIC_ARRAY_SIZE (ow_family_features);
60
61 static char *device_g = NULL;
62
63 static const char *config_keys[] =
64 {
65   "Device",
66   "IgnoreSelected",
67   "Sensor",
68 };
69 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
70
71 static ignorelist_t *sensor_list;
72
73 static int cow_load_config (const char *key, const char *value)
74 {
75   if (sensor_list == NULL)
76     sensor_list = ignorelist_create (1);
77
78   if (strcasecmp (key, "Sensor") == 0)
79   {
80     if (ignorelist_add (sensor_list, value))
81     {
82       ERROR ("sensors plugin: "
83           "Cannot add value to ignorelist.");
84       return (1);
85     }
86   }
87   else if (strcasecmp (key, "IgnoreSelected") == 0)
88   {
89     ignorelist_set_invert (sensor_list, 1);
90     if ((strcasecmp (value, "True") == 0)
91         || (strcasecmp (value, "Yes") == 0)
92         || (strcasecmp (value, "On") == 0))
93       ignorelist_set_invert (sensor_list, 0);
94   }
95   else if (strcasecmp (key, "Device") == 0)
96   {
97     char *temp;
98     temp = strdup (value);
99     if (temp == NULL)
100     {
101       ERROR ("onewire plugin: strdup failed.");
102       return (1);
103     }
104     sfree (device_g);
105     device_g = temp;
106   }
107   else
108   {
109     return (-1);
110   }
111
112   return (0);
113 }
114
115 static int cow_init (void)
116 {
117   int status;
118
119   if (device_g == NULL)
120   {
121     ERROR ("onewire plugin: cow_init: No device configured.");
122     return (-1);
123   }
124
125   status = (int) OW_init (device_g);
126   if (status != 0)
127   {
128     ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
129     return (1);
130   }
131
132   return (0);
133 } /* int cow_init */
134
135 static int cow_read_values (const char *path, const char *name,
136     const ow_family_features_t *family_info)
137 {
138   value_t values[1];
139   value_list_t vl = VALUE_LIST_INIT;
140   int success = 0;
141   size_t i;
142
143   if (sensor_list != NULL)
144   {
145     DEBUG ("onewire plugin: Checking ignorelist for `%s'", name);
146     if (ignorelist_match (sensor_list, name) != 0)
147       return 0;
148   }
149
150   vl.values = values;
151   vl.values_len = 1;
152
153   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
154   sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
155   sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
156
157   for (i = 0; i < family_info->features_num; i++)
158   {
159     char *buffer;
160     size_t buffer_size;
161     int status;
162
163     char file[4096];
164     char *endptr;
165
166     snprintf (file, sizeof (file), "%s/%s",
167         path, family_info->features[i].filename);
168     file[sizeof (file) - 1] = 0;
169
170     buffer = NULL;
171     buffer_size = 0;
172     status = OW_get (file, &buffer, &buffer_size);
173     if (status < 0)
174     {
175       ERROR ("onewire plugin: OW_get (%s/%s) failed. status = %#x;",
176           path, family_info->features[i].filename, status);
177       return (-1);
178     }
179
180     endptr = NULL;
181     values[0].gauge = strtod (buffer, &endptr);
182     if (endptr == NULL)
183     {
184       ERROR ("onewire plugin: Buffer is not a number: %s", buffer);
185       status = -1;
186       continue;
187     }
188
189     sstrncpy (vl.type, family_info->features[i].type, sizeof (vl.type));
190     sstrncpy (vl.type_instance, family_info->features[i].type_instance,
191         sizeof (vl.type_instance));
192
193     plugin_dispatch_values (&vl);
194     success++;
195
196     free (buffer);
197   } /* for (i = 0; i < features_num; i++) */
198
199   return ((success > 0) ? 0 : -1);
200 } /* int cow_read_values */
201
202 /* Forward declaration so the recursion below works */
203 static int cow_read_bus (const char *path);
204
205 /*
206  * cow_read_ds2409
207  *
208  * Handles:
209  * - DS2409 - MicroLAN Coupler
210  */
211 static int cow_read_ds2409 (const char *path)
212 {
213   char subpath[4096];
214   int status;
215
216   status = ssnprintf (subpath, sizeof (subpath), "%s/main", path);
217   if ((status > 0) && (status < sizeof (subpath)))
218     cow_read_bus (subpath);
219
220   status = ssnprintf (subpath, sizeof (subpath), "%s/aux", path);
221   if ((status > 0) && (status < sizeof (subpath)))
222     cow_read_bus (subpath);
223
224   return (0);
225 } /* int cow_read_ds2409 */
226
227 static int cow_read_bus (const char *path)
228 {
229   char *buffer;
230   size_t buffer_size;
231   int status;
232
233   char *buffer_ptr;
234   char *dummy;
235   char *saveptr;
236   char subpath[4096];
237
238   status = OW_get (path, &buffer, &buffer_size);
239   if (status < 0)
240   {
241     ERROR ("onewire plugin: OW_get (%s) failed. status = %#x;",
242         path, status);
243     return (-1);
244   }
245   DEBUG ("onewire plugin: OW_get (%s) returned: %s",
246       path, buffer);
247
248   dummy = buffer;
249   saveptr = NULL;
250   while ((buffer_ptr = strtok_r (dummy, ",/", &saveptr)) != NULL)
251   {
252     int i;
253
254     dummy = NULL;
255
256     if (strcmp ("/", path) == 0)
257       status = ssnprintf (subpath, sizeof (subpath), "/%s", buffer_ptr);
258     else
259       status = ssnprintf (subpath, sizeof (subpath), "%s/%s",
260           path, buffer_ptr);
261     if ((status <= 0) || (status >= sizeof (subpath)))
262       continue;
263
264     for (i = 0; i < ow_family_features_num; i++)
265     {
266       if (strncmp (ow_family_features[i].family, buffer_ptr,
267             strlen (ow_family_features[i].family)) != 0)
268         continue;
269
270       cow_read_values (subpath,
271           buffer_ptr + strlen (ow_family_features[i].family),
272           ow_family_features + i);
273       break;
274     }
275     if (i < ow_family_features_num)
276       continue;
277
278     /* DS2409 */
279     if (strncmp ("1F.", buffer_ptr, strlen ("1F.")) == 0)
280     {
281       cow_read_ds2409 (subpath);
282       continue;
283     }
284   } /* while (strtok_r) */
285
286   free (buffer);
287   return (0);
288 } /* int cow_read_bus */
289
290 static int cow_read (void)
291 {
292   return (cow_read_bus ("/"));
293 } /* int cow_read */
294
295 static int cow_shutdown (void)
296 {
297   OW_finish ();
298   ignorelist_free (sensor_list);
299   return (0);
300 } /* int cow_shutdown */
301
302 void module_register (void)
303 {
304   plugin_register_init ("onewire", cow_init);
305   plugin_register_read ("onewire", cow_read);
306   plugin_register_shutdown ("onewire", cow_shutdown);
307   plugin_register_config ("onewire", cow_load_config,
308     config_keys, config_keys_num);
309 }
310
311 /* vim: set sw=2 sts=2 ts=8 et fdm=marker cindent : */