Merge branch 'pull/collectd-4' into collectd-4
[collectd.git] / src / multimeter.c
1 /**
2  * collectd - src/multimeter.c
3  * Copyright (C) 2005,2006  Peter Holik
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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Peter Holik <peter at holik.at>
21  *
22  * Used multimeter: Metex M-4650CR
23  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28
29 #if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
30 # include <termios.h>
31 # include <sys/ioctl.h>
32 # include <math.h>
33 # define MULTIMETER_HAVE_READ 1
34 #else
35 # define MULTIMETER_HAVE_READ 0
36 # error "multimeter cannot read!"
37 #endif
38
39 static data_source_t data_source[1] =
40 {
41         {"value", DS_TYPE_GAUGE, NAN, NAN}
42 };
43
44 static data_set_t data_set =
45 {
46         "multimeter", 1, data_source
47 };
48
49 #if MULTIMETER_HAVE_READ
50 static int fd = -1;
51
52 static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2,
53                 struct timeval *res)
54 {
55         if ((tv1->tv_sec < tv2->tv_sec) ||
56             ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec)))
57                 return (-1);
58
59         res->tv_sec  = tv1->tv_sec  - tv2->tv_sec;
60         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
61
62         assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
63
64         while (res->tv_usec < 0)
65         {
66                 res->tv_usec += 1000000;
67                 res->tv_sec--;
68         }
69         return (0);
70 }
71 #define LINE_LENGTH 14
72 static int multimeter_read_value(double *value)
73 {
74         int retry = 3; /* sometimes we receive garbadge */
75
76         do
77         {
78                 struct timeval time_end;
79
80                 tcflush(fd, TCIFLUSH);
81
82                 if (gettimeofday (&time_end, NULL) < 0)
83                 {
84                         char errbuf[1024];
85                         ERROR ("multimeter plugin: gettimeofday failed: %s",
86                                         sstrerror (errno, errbuf,
87                                                 sizeof (errbuf)));
88                         return (-1);
89                 }
90                 time_end.tv_sec++;      
91
92                 while (1)
93                 {
94                         char buf[LINE_LENGTH];
95                         char *range;
96                         int status;
97                         fd_set rfds;
98                         struct timeval timeout;
99                         struct timeval time_now;
100
101                         write(fd, "D", 1);
102
103                         FD_ZERO(&rfds);
104                         FD_SET(fd, &rfds);
105
106                         if (gettimeofday (&time_now, NULL) < 0)
107                         {
108                                 char errbuf[1024];
109                                 ERROR ("multimeter plugin: "
110                                                 "gettimeofday failed: %s",
111                                                 sstrerror (errno, errbuf,
112                                                         sizeof (errbuf)));
113                                 return (-1);
114                         }
115                         if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1)
116                                 break;
117
118                         status = select(fd+1, &rfds, NULL, NULL, &timeout);
119
120                         if (status > 0) /* usually we succeed */
121                         {
122                                 status = read(fd, buf, LINE_LENGTH);
123
124                                 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
125                                         continue;
126
127                                 /* Format: "DC 00.000mV  \r" */
128                                 if (status > 0 && status == LINE_LENGTH)
129                                 {
130                                         *value = strtod(buf + 2, &range);
131
132                                         if ( range > (buf + 6) )
133                                         {
134                                                 range = buf + 9;
135
136                                                 switch ( *range )
137                                                 {
138                                                         case 'p': *value *= 1.0E-12; break;
139                                                         case 'n': *value *= 1.0E-9; break;
140                                                         case 'u': *value *= 1.0E-6; break;
141                                                         case 'm': *value *= 1.0E-3; break;
142                                                         case 'k': *value *= 1.0E3; break;
143                                                         case 'M': *value *= 1.0E6; break;
144                                                         case 'G': *value *= 1.0E9; break;
145                                                 }
146                                         }
147                                         else
148                                                 return (-1); /* Overflow */
149
150                                         return (0); /* value received */
151                                 }
152                                 else break;
153                         }
154                         else if (!status) /* Timeout */
155                         {
156                                 break;
157                         }
158                         else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
159                         {
160                                 continue;
161                         }
162                         else /* status == -1 */
163                         {
164                                 char errbuf[1024];
165                                 ERROR ("multimeter plugin: "
166                                                 "select failed: %s",
167                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
168                                 break;
169                         }
170                 }
171         } while (--retry);
172
173         return (-2);  /* no value received */
174 } /* int multimeter_read_value */
175
176 static int multimeter_init (void)
177 {
178         int i;
179         char device[] = "/dev/ttyS ";
180
181         for (i = 0; i < 10; i++)
182         {
183                 device[strlen(device)-1] = i + '0'; 
184
185                 if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0)
186                 {
187                         struct termios tios;
188                         int rts = TIOCM_RTS;
189                         double value;
190
191                         tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
192                         tios.c_iflag = IGNBRK | IGNPAR;
193                         tios.c_oflag = 0;
194                         tios.c_lflag = 0;
195                         tios.c_cc[VTIME] = 3;
196                         tios.c_cc[VMIN]  = LINE_LENGTH;
197
198                         tcflush(fd, TCIFLUSH);
199                         tcsetattr(fd, TCSANOW, &tios);
200                         ioctl(fd, TIOCMBIC, &rts);
201                         
202                         if (multimeter_read_value (&value) < -1)
203                         {
204                                 close (fd);
205                                 fd = -1;
206                         }
207                         else
208                         {
209                                 INFO ("multimeter plugin: Device "
210                                                 "found at %s", device);
211                                 return (0);
212                         }
213                 }
214         }
215
216         ERROR ("multimeter plugin: No device found");
217         return (-1);
218 }
219 #undef LINE_LENGTH
220
221 static void multimeter_submit (double value)
222 {
223         value_t values[1];
224         value_list_t vl = VALUE_LIST_INIT;
225
226         values[0].gauge = value;
227
228         vl.values = values;
229         vl.values_len = 1;
230         vl.time = time (NULL);
231         strcpy (vl.host, hostname_g);
232         strcpy (vl.plugin, "multimeter");
233
234         plugin_dispatch_values ("multimeter", &vl);
235 }
236
237 static int multimeter_read (void)
238 {
239         double value;
240
241         if (fd < 0)
242                 return (-1);
243
244         if (multimeter_read_value (&value) != 0)
245                 return (-1);
246
247         multimeter_submit (value);
248         return (0);
249 } /* int multimeter_read */
250
251 static int multimeter_shutdown (void)
252 {
253         if (fd >= 0)
254         {
255                 close (fd);
256                 fd = -1;
257         }
258
259         return (0);
260 }
261 #endif /* MULTIMETER_HAVE_READ */
262
263 void module_register (modreg_e load)
264 {
265         if (load & MR_DATASETS)
266                 plugin_register_data_set (&data_set);
267
268 #if MULTIMETER_HAVE_READ
269         if (load & MR_READ)
270         {
271                 plugin_register_init ("multimeter", multimeter_init);
272                 plugin_register_read ("multimeter", multimeter_read);
273                 plugin_register_shutdown ("multimeter", multimeter_shutdown);
274         }
275 #endif /* MULTIMETER_HAVE_READ */
276 } /* void module_register */