X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmultimeter.c;h=2ffcc8f2ca97d2de8743418e051610dd54f22651;hb=e14e8b7f5c5bf9d0fe5cc632c6383f304d4ac2ad;hp=994747f94f4167c352febc69c8ca4eb884c28650;hpb=92445ff3363441d0f515de4a3ab92a504cfc0366;p=collectd.git diff --git a/src/multimeter.c b/src/multimeter.c index 994747f9..2ffcc8f2 100644 --- a/src/multimeter.c +++ b/src/multimeter.c @@ -23,6 +23,7 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" @@ -36,26 +37,6 @@ static int fd = -1; -static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2, - struct timeval *res) -{ - if ((tv1->tv_sec < tv2->tv_sec) || - ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec))) - return (-1); - - res->tv_sec = tv1->tv_sec - tv2->tv_sec; - res->tv_usec = tv1->tv_usec - tv2->tv_usec; - - assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0))); - - while (res->tv_usec < 0) - { - res->tv_usec += 1000000; - res->tv_sec--; - } - return (0); -} - #define LINE_LENGTH 14 static int multimeter_read_value(double *value) { @@ -86,7 +67,12 @@ static int multimeter_read_value(double *value) struct timeval timeout; struct timeval time_now; - write(fd, "D", 1); + status = swrite (fd, "D", 1); + if (status < 0) + { + ERROR ("multimeter plugin: swrite failed."); + return (-1); + } FD_ZERO(&rfds); FD_SET(fd, &rfds); @@ -100,7 +86,7 @@ static int multimeter_read_value(double *value) sizeof (errbuf))); return (-1); } - if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1) + if (timeval_cmp (time_end, time_now, &timeout) < 0) break; status = select(fd+1, &rfds, NULL, NULL, &timeout); @@ -163,22 +149,21 @@ static int multimeter_read_value(double *value) static int multimeter_init (void) { - int i; char device[] = "/dev/ttyS "; - for (i = 0; i < 10; i++) + for (int i = 0; i < 10; i++) { - device[strlen(device)-1] = i + '0'; + device[strlen(device)-1] = i + '0'; - if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0) + if ((fd = open(device, O_RDWR | O_NOCTTY)) != -1) { - struct termios tios; + struct termios tios = { 0 }; int rts = TIOCM_RTS; double value; tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL; tios.c_iflag = IGNBRK | IGNPAR; - tios.c_oflag = 0; + tios.c_oflag = 0; tios.c_lflag = 0; tios.c_cc[VTIME] = 3; tios.c_cc[VMIN] = LINE_LENGTH; @@ -186,7 +171,7 @@ static int multimeter_init (void) tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &tios); ioctl(fd, TIOCMBIC, &rts); - + if (multimeter_read_value (&value) < -1) { close (fd); @@ -208,14 +193,10 @@ static int multimeter_init (void) static void multimeter_submit (double value) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = value; - - vl.values = values; + vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; - vl.time = time (NULL); sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "multimeter", sizeof (vl.plugin)); sstrncpy (vl.type, "multimeter", sizeof (vl.type));