09073abab2d7449804e764151d45b028238e671a
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 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
30 #include <termios.h>
31 #include <sys/ioctl.h>
32 #include <math.h>
34 #define MODULE_NAME "multimeter"
36 static char *multimeter_file = "multimeter.rrd";
38 static char *ds_def[] =
39 {
40 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
41 NULL
42 };
43 static int ds_num = 1;
45 static int fd = -1;
47 static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2,
48 struct timeval *res)
49 {
50 if ((tv1->tv_sec < tv2->tv_sec) ||
51 ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec)))
52 return (-1);
54 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
55 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
57 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
59 while (res->tv_usec < 0)
60 {
61 res->tv_usec += 1000000;
62 res->tv_sec--;
63 }
64 return (0);
65 }
66 #define LINE_LENGTH 14
67 static int multimeter_read_value(double *value)
68 {
69 int retry = 3; /* sometimes we receive garbadge */
71 do
72 {
73 struct timeval time_end;
75 tcflush(fd, TCIFLUSH);
77 if (gettimeofday (&time_end, NULL) < 0)
78 {
79 syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s",
80 strerror (errno));
81 return (-1);
82 }
83 time_end.tv_sec++;
85 while (1)
86 {
87 char buf[LINE_LENGTH];
88 char *range;
89 int status;
90 fd_set rfds;
91 struct timeval timeout;
92 struct timeval time_now;
94 write(fd, "D", 1);
96 FD_ZERO(&rfds);
97 FD_SET(fd, &rfds);
99 if (gettimeofday (&time_now, NULL) < 0)
100 {
101 syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s",
102 strerror (errno));
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 syslog (LOG_ERR, MODULE_NAME": select failed: %s",
155 strerror (errno));
156 break;
157 }
158 }
159 } while (--retry);
161 return (-2); /* no value received */
162 }
164 static void multimeter_init (void)
165 {
166 int i;
167 char device[] = "/dev/ttyS ";
169 for (i = 0; i < 10; i++)
170 {
171 device[strlen(device)-1] = i + '0';
173 if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0)
174 {
175 struct termios tios;
176 int rts = TIOCM_RTS;
177 double value;
179 tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
180 tios.c_iflag = IGNBRK | IGNPAR;
181 tios.c_oflag = 0;
182 tios.c_lflag = 0;
183 tios.c_cc[VTIME] = 3;
184 tios.c_cc[VMIN] = LINE_LENGTH;
186 tcflush(fd, TCIFLUSH);
187 tcsetattr(fd, TCSANOW, &tios);
188 ioctl(fd, TIOCMBIC, &rts);
190 if (multimeter_read_value(&value) < -1)
191 {
192 close(fd);
193 fd = -1;
194 }
195 else
196 {
197 syslog (LOG_INFO, MODULE_NAME" found (%s)", device);
198 return;
199 }
200 }
201 }
202 syslog (LOG_ERR, MODULE_NAME" not found");
203 }
204 #undef LINE_LENGTH
206 static void multimeter_write (char *host, char *inst, char *val)
207 {
208 rrd_update_file (host, multimeter_file, val, ds_def, ds_num);
209 }
210 #define BUFSIZE 128
211 static void multimeter_submit (double *value)
212 {
213 char buf[BUFSIZE];
215 if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, *value) >= BUFSIZE)
216 return;
218 plugin_submit (MODULE_NAME, "-", buf);
219 }
220 #undef BUFSIZE
222 static void multimeter_read (void)
223 {
224 double value;
226 if (fd > -1 && !(multimeter_read_value(&value)))
227 multimeter_submit (&value);
228 }
230 void module_register (void)
231 {
232 plugin_register (MODULE_NAME, multimeter_init, multimeter_read, multimeter_write);
233 }
235 #undef MODULE_NAME