Merge branch 'collectd-4.4' into collectd-4.5
[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 #else
34 # error "No applicable input method."
35 #endif
36
37 static int fd = -1;
38
39 static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2,
40                 struct timeval *res)
41 {
42         if ((tv1->tv_sec < tv2->tv_sec) ||
43             ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec)))
44                 return (-1);
45
46         res->tv_sec  = tv1->tv_sec  - tv2->tv_sec;
47         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
48
49         assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
50
51         while (res->tv_usec < 0)
52         {
53                 res->tv_usec += 1000000;
54                 res->tv_sec--;
55         }
56         return (0);
57 }
58
59 #define LINE_LENGTH 14
60 static int multimeter_read_value(double *value)
61 {
62         int retry = 3; /* sometimes we receive garbadge */
63
64         do
65         {
66                 struct timeval time_end;
67
68                 tcflush(fd, TCIFLUSH);
69
70                 if (gettimeofday (&time_end, NULL) < 0)
71                 {
72                         char errbuf[1024];
73                         ERROR ("multimeter plugin: gettimeofday failed: %s",
74                                         sstrerror (errno, errbuf,
75                                                 sizeof (errbuf)));
76                         return (-1);
77                 }
78                 time_end.tv_sec++;      
79
80                 while (1)
81                 {
82                         char buf[LINE_LENGTH];
83                         char *range;
84                         int status;
85                         fd_set rfds;
86                         struct timeval timeout;
87                         struct timeval time_now;
88
89                         status = swrite (fd, "D", 1);
90                         if (status < 0)
91                         {
92                                 ERROR ("multimeter plugin: swrite failed.");
93                                 return (-1);
94                         }
95
96                         FD_ZERO(&rfds);
97                         FD_SET(fd, &rfds);
98
99                         if (gettimeofday (&time_now, NULL) < 0)
100                         {
101                                 char errbuf[1024];
102                                 ERROR ("multimeter plugin: "
103                                                 "gettimeofday failed: %s",
104                                                 sstrerror (errno, errbuf,
105                                                         sizeof (errbuf)));
106                                 return (-1);
107                         }
108                         if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1)
109                                 break;
110
111                         status = select(fd+1, &rfds, NULL, NULL, &timeout);
112
113                         if (status > 0) /* usually we succeed */
114                         {
115                                 status = read(fd, buf, LINE_LENGTH);
116
117                                 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
118                                         continue;
119
120                                 /* Format: "DC 00.000mV  \r" */
121                                 if (status > 0 && status == LINE_LENGTH)
122                                 {
123                                         *value = strtod(buf + 2, &range);
124
125                                         if ( range > (buf + 6) )
126                                         {
127                                                 range = buf + 9;
128
129                                                 switch ( *range )
130                                                 {
131                                                         case 'p': *value *= 1.0E-12; break;
132                                                         case 'n': *value *= 1.0E-9; break;
133                                                         case 'u': *value *= 1.0E-6; break;
134                                                         case 'm': *value *= 1.0E-3; break;
135                                                         case 'k': *value *= 1.0E3; break;
136                                                         case 'M': *value *= 1.0E6; break;
137                                                         case 'G': *value *= 1.0E9; break;
138                                                 }
139                                         }
140                                         else
141                                                 return (-1); /* Overflow */
142
143                                         return (0); /* value received */
144                                 }
145                                 else break;
146                         }
147                         else if (!status) /* Timeout */
148                         {
149                                 break;
150                         }
151                         else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
152                         {
153                                 continue;
154                         }
155                         else /* status == -1 */
156                         {
157                                 char errbuf[1024];
158                                 ERROR ("multimeter plugin: "
159                                                 "select failed: %s",
160                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
161                                 break;
162                         }
163                 }
164         } while (--retry);
165
166         return (-2);  /* no value received */
167 } /* int multimeter_read_value */
168
169 static int multimeter_init (void)
170 {
171         int i;
172         char device[] = "/dev/ttyS ";
173
174         for (i = 0; i < 10; i++)
175         {
176                 device[strlen(device)-1] = i + '0'; 
177
178                 if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0)
179                 {
180                         struct termios tios;
181                         int rts = TIOCM_RTS;
182                         double value;
183
184                         tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
185                         tios.c_iflag = IGNBRK | IGNPAR;
186                         tios.c_oflag = 0;
187                         tios.c_lflag = 0;
188                         tios.c_cc[VTIME] = 3;
189                         tios.c_cc[VMIN]  = LINE_LENGTH;
190
191                         tcflush(fd, TCIFLUSH);
192                         tcsetattr(fd, TCSANOW, &tios);
193                         ioctl(fd, TIOCMBIC, &rts);
194                         
195                         if (multimeter_read_value (&value) < -1)
196                         {
197                                 close (fd);
198                                 fd = -1;
199                         }
200                         else
201                         {
202                                 INFO ("multimeter plugin: Device "
203                                                 "found at %s", device);
204                                 return (0);
205                         }
206                 }
207         }
208
209         ERROR ("multimeter plugin: No device found");
210         return (-1);
211 }
212 #undef LINE_LENGTH
213
214 static void multimeter_submit (double value)
215 {
216         value_t values[1];
217         value_list_t vl = VALUE_LIST_INIT;
218
219         values[0].gauge = value;
220
221         vl.values = values;
222         vl.values_len = 1;
223         vl.time = time (NULL);
224         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225         sstrncpy (vl.plugin, "multimeter", sizeof (vl.plugin));
226         sstrncpy (vl.type, "multimeter", sizeof (vl.type));
227
228         plugin_dispatch_values (&vl);
229 }
230
231 static int multimeter_read (void)
232 {
233         double value;
234
235         if (fd < 0)
236                 return (-1);
237
238         if (multimeter_read_value (&value) != 0)
239                 return (-1);
240
241         multimeter_submit (value);
242         return (0);
243 } /* int multimeter_read */
244
245 static int multimeter_shutdown (void)
246 {
247         if (fd >= 0)
248         {
249                 close (fd);
250                 fd = -1;
251         }
252
253         return (0);
254 }
255
256 void module_register (void)
257 {
258         plugin_register_init ("multimeter", multimeter_init);
259         plugin_register_read ("multimeter", multimeter_read);
260         plugin_register_shutdown ("multimeter", multimeter_shutdown);
261 } /* void module_register */