Code

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