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 static data_source_t data_source[1] =
40 {
41 {"value", DS_TYPE_GAUGE, NAN, NAN}
42 };
44 static data_set_t data_set =
45 {
46 "multimeter", 1, data_source
47 };
49 #if MULTIMETER_HAVE_READ
50 static int fd = -1;
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);
59 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
60 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
62 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
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 */
76 do
77 {
78 struct timeval time_end;
80 tcflush(fd, TCIFLUSH);
82 if (gettimeofday (&time_end, NULL) < 0)
83 {
84 syslog (LOG_ERR, "multimeter plugin: gettimeofday failed: %s",
85 strerror (errno));
86 return (-1);
87 }
88 time_end.tv_sec++;
90 while (1)
91 {
92 char buf[LINE_LENGTH];
93 char *range;
94 int status;
95 fd_set rfds;
96 struct timeval timeout;
97 struct timeval time_now;
99 write(fd, "D", 1);
101 FD_ZERO(&rfds);
102 FD_SET(fd, &rfds);
104 if (gettimeofday (&time_now, NULL) < 0)
105 {
106 syslog (LOG_ERR, "multimeter plugin: "
107 "gettimeofday failed: %s",
108 strerror (errno));
109 return (-1);
110 }
111 if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1)
112 break;
114 status = select(fd+1, &rfds, NULL, NULL, &timeout);
116 if (status > 0) /* usually we succeed */
117 {
118 status = read(fd, buf, LINE_LENGTH);
120 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
121 continue;
123 /* Format: "DC 00.000mV \r" */
124 if (status > 0 && status == LINE_LENGTH)
125 {
126 *value = strtod(buf + 2, &range);
128 if ( range > (buf + 6) )
129 {
130 range = buf + 9;
132 switch ( *range )
133 {
134 case 'p': *value *= 1.0E-12; break;
135 case 'n': *value *= 1.0E-9; break;
136 case 'u': *value *= 1.0E-6; break;
137 case 'm': *value *= 1.0E-3; break;
138 case 'k': *value *= 1.0E3; break;
139 case 'M': *value *= 1.0E6; break;
140 case 'G': *value *= 1.0E9; break;
141 }
142 }
143 else
144 return (-1); /* Overflow */
146 return (0); /* value received */
147 }
148 else break;
149 }
150 else if (!status) /* Timeout */
151 {
152 break;
153 }
154 else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
155 {
156 continue;
157 }
158 else /* status == -1 */
159 {
160 syslog (LOG_ERR, "multimeter plugin: "
161 "select failed: %s",
162 strerror (errno));
163 break;
164 }
165 }
166 } while (--retry);
168 return (-2); /* no value received */
169 } /* int multimeter_read_value */
171 static int multimeter_init (void)
172 {
173 int i;
174 char device[] = "/dev/ttyS ";
176 for (i = 0; i < 10; i++)
177 {
178 device[strlen(device)-1] = i + '0';
180 if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0)
181 {
182 struct termios tios;
183 int rts = TIOCM_RTS;
184 double value;
186 tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
187 tios.c_iflag = IGNBRK | IGNPAR;
188 tios.c_oflag = 0;
189 tios.c_lflag = 0;
190 tios.c_cc[VTIME] = 3;
191 tios.c_cc[VMIN] = LINE_LENGTH;
193 tcflush(fd, TCIFLUSH);
194 tcsetattr(fd, TCSANOW, &tios);
195 ioctl(fd, TIOCMBIC, &rts);
197 if (multimeter_read_value (&value) < -1)
198 {
199 close (fd);
200 fd = -1;
201 }
202 else
203 {
204 syslog (LOG_INFO, "multimeter plugin: Device "
205 "found at %s", device);
206 return (0);
207 }
208 }
209 }
211 syslog (LOG_ERR, "multimeter plugin: No device found");
212 return (-1);
213 }
214 #undef LINE_LENGTH
216 static void multimeter_submit (double value)
217 {
218 value_t values[1];
219 value_list_t vl = VALUE_LIST_INIT;
221 values[0].gauge = value;
223 vl.values = values;
224 vl.values_len = 1;
225 vl.time = time (NULL);
226 strcpy (vl.host, hostname);
227 strcpy (vl.plugin, "multimeter");
229 plugin_dispatch_values ("multimeter", &vl);
230 }
232 static int multimeter_read (void)
233 {
234 double value;
236 if (fd < 0)
237 return (-1);
239 if (multimeter_read_value (&value) != 0)
240 return (-1);
242 multimeter_submit (value);
243 return (0);
244 } /* int multimeter_read */
246 static int multimeter_shutdown (void)
247 {
248 if (fd >= 0)
249 {
250 close (fd);
251 fd = -1;
252 }
254 return (0);
255 }
256 #endif /* MULTIMETER_HAVE_READ */
258 void module_register (void)
259 {
260 plugin_register_data_set (&data_set);
262 #if MULTIMETER_HAVE_READ
263 plugin_register_init ("multimeter", multimeter_init);
264 plugin_register_read ("multimeter", multimeter_read);
265 plugin_register_shutdown ("multimeter", multimeter_shutdown);
266 #endif /* MULTIMETER_HAVE_READ */
267 }