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