Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / ted.c
index cccb3db..ff650a8 100644 (file)
--- a/src/ted.c
+++ b/src/ted.c
 
 #include "collectd.h"
 
-#include "common.h"
 #include "plugin.h"
+#include "utils/common/common.h"
 
-#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
-#include <math.h>
+#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #include <termios.h>
 #else
@@ -54,8 +53,8 @@
 
 #define DEFAULT_DEVICE "/dev/ttyUSB0"
 
-static char *conf_device = NULL;
-static int conf_retries = 0;
+static char *conf_device;
+static int conf_retries;
 
 static int fd = -1;
 
@@ -89,8 +88,8 @@ static int ted_read_value(double *ret_power, double *ret_voltage) {
 
   status = write(fd, pkt_request, sizeof(pkt_request));
   if (status <= 0) {
-    ERROR("ted plugin: swrite failed.");
-    return (-1);
+    ERROR("ted plugin: write failed.");
+    return -1;
   }
 
   /* Loop until we find the end of the package */
@@ -105,33 +104,29 @@ static int ted_read_value(double *ret_power, double *ret_voltage) {
     {
       WARNING("ted plugin: Timeout while waiting for file descriptor "
               "to become ready.");
-      return (-1);
+      return -1;
     } else if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR))) {
       /* Some signal or something. Start over.. */
       continue;
     } else if (status < 0) {
-      char errbuf[1024];
-      ERROR("ted plugin: select failed: %s",
-            sstrerror(errno, errbuf, sizeof(errbuf)));
-      return (-1);
+      ERROR("ted plugin: select failed: %s", STRERRNO);
+      return -1;
     }
 
     receive_buffer_length = read(fd, receive_buffer, sizeof(receive_buffer));
     if (receive_buffer_length < 0) {
-      char errbuf[1024];
       if ((errno == EAGAIN) || (errno == EINTR))
         continue;
-      ERROR("ted plugin: read(2) failed: %s",
-            sstrerror(errno, errbuf, sizeof(errbuf)));
-      return (-1);
+      ERROR("ted plugin: read(2) failed: %s", STRERRNO);
+      return -1;
     } else if (receive_buffer_length == 0) {
       /* Should we close the FD in this case? */
       WARNING("ted plugin: Received EOF from file descriptor.");
-      return (-1);
+      return -1;
     } else if (((size_t)receive_buffer_length) > sizeof(receive_buffer)) {
       ERROR("ted plugin: read(2) returned invalid value %zi.",
             receive_buffer_length);
-      return (-1);
+      return -1;
     }
 
     /*
@@ -176,7 +171,7 @@ static int ted_read_value(double *ret_power, double *ret_voltage) {
 
   /* Check for errors inside the loop. */
   if ((end_flag == 0) || (package_buffer_pos != EXPECTED_PACKAGE_LENGTH))
-    return (-1);
+    return -1;
 
   /*
    * Power is at positions 247 and 248 (LSB first) in [10kW].
@@ -191,7 +186,7 @@ static int ted_read_value(double *ret_power, double *ret_voltage) {
                                 ((int)package_buffer[251]));
 
   /* success */
-  return (0);
+  return 0;
 } /* int ted_read_value */
 
 static int ted_open_device(void) {
@@ -199,7 +194,7 @@ static int ted_open_device(void) {
   struct termios options;
 
   if (fd >= 0)
-    return (0);
+    return 0;
 
   dev = DEFAULT_DEVICE;
   if (conf_device != NULL)
@@ -208,7 +203,7 @@ static int ted_open_device(void) {
   fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
   if (fd < 0) {
     ERROR("ted plugin: Unable to open device %s.", dev);
-    return (-1);
+    return -1;
   }
 
   /* Get the current options for the port... */
@@ -225,7 +220,7 @@ static int ted_open_device(void) {
   tcsetattr(fd, TCSANOW, &options);
 
   INFO("ted plugin: Successfully opened %s.", dev);
-  return (0);
+  return 0;
 } /* int ted_open_device */
 
 static void ted_submit(const char *type, double value) {
@@ -248,16 +243,16 @@ static int ted_config(const char *key, const char *value) {
 
     tmp = atoi(value);
     if (tmp < 0) {
-      WARNING("ted plugin: Invalid retry count: %i", tmp);
-      return (1);
+      ERROR("ted plugin: Invalid retry count: %i", tmp);
+      return 1;
     }
     conf_retries = tmp;
   } else {
     ERROR("ted plugin: Unknown config option: %s", key);
-    return (-1);
+    return -1;
   }
 
-  return (0);
+  return 0;
 } /* int ted_config */
 
 static int ted_read(void) {
@@ -267,7 +262,7 @@ static int ted_read(void) {
 
   status = ted_open_device();
   if (status != 0)
-    return (-1);
+    return -1;
 
   power = NAN;
   voltage = NAN;
@@ -278,12 +273,12 @@ static int ted_read(void) {
   }
 
   if (status != 0)
-    return (-1);
+    return -1;
 
   ted_submit("power", power);
   ted_submit("voltage", voltage);
 
-  return (0);
+  return 0;
 } /* int ted_read */
 
 static int ted_shutdown(void) {
@@ -292,7 +287,7 @@ static int ted_shutdown(void) {
     fd = -1;
   }
 
-  return (0);
+  return 0;
 } /* int ted_shutdown */
 
 void module_register(void) {
@@ -300,5 +295,3 @@ void module_register(void) {
   plugin_register_read("ted", ted_read);
   plugin_register_shutdown("ted", ted_shutdown);
 } /* void module_register */
-
-/* vim: set sw=4 et : */