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