Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / battery.c
index 185442c..0327d5d 100644 (file)
  **/
 
 #include "collectd.h"
+
 #include "common.h"
 #include "plugin.h"
 
-#include "utils_complain.h"
-
 #if HAVE_MACH_MACH_TYPES_H
 #  include <mach/mach_types.h>
 #endif
@@ -143,7 +142,7 @@ static void submit_capacity (char const *plugin_instance, /* {{{ */
 } /* }}} void submit_capacity */
 
 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
-static double dict_get_double (CFDictionaryRef dict, char *key_string) /* {{{ */
+static double dict_get_double (CFDictionaryRef dict, const char *key_string) /* {{{ */
 {
        double      val_double;
        long long   val_int;
@@ -203,7 +202,6 @@ static void get_via_io_power_sources (double *ret_charge, /* {{{ */
        CFTypeRef       ps_obj;
 
        double temp_double;
-       int i;
 
        ps_raw       = IOPSCopyPowerSourcesInfo ();
        ps_array     = IOPSCopyPowerSourcesList (ps_raw);
@@ -211,7 +209,7 @@ static void get_via_io_power_sources (double *ret_charge, /* {{{ */
 
        DEBUG ("ps_array_len == %i", ps_array_len);
 
-       for (i = 0; i < ps_array_len; i++)
+       for (int i = 0; i < ps_array_len; i++)
        {
                ps_obj  = CFArrayGetValueAtIndex (ps_array, i);
                ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
@@ -276,7 +274,6 @@ static void get_via_generic_iokit (double *ret_capacity_full, /* {{{ */
        CFDictionaryRef bat_root_dict;
        CFArrayRef      bat_info_arry;
        CFIndex         bat_info_arry_len;
-       CFIndex         bat_info_arry_pos;
        CFDictionaryRef bat_info_dict;
 
        double temp_double;
@@ -311,7 +308,7 @@ static void get_via_generic_iokit (double *ret_capacity_full, /* {{{ */
                }
                bat_info_arry_len = CFArrayGetCount (bat_info_arry);
 
-               for (bat_info_arry_pos = 0;
+               for (CFIndex bat_info_arry_pos = 0;
                                bat_info_arry_pos < bat_info_arry_len;
                                bat_info_arry_pos++)
                {
@@ -341,7 +338,7 @@ static void get_via_generic_iokit (double *ret_capacity_full, /* {{{ */
                                *ret_voltage = temp_double / 1000.0;
                        }
                }
-               
+
                CFRelease (bat_root_dict);
        }
 
@@ -358,7 +355,7 @@ static int battery_read (void) /* {{{ */
         * IOPowerSources. IOKit, on the other hand, only reports the full
         * capacity. We use the two to calculate the current charged capacity. */
        gauge_t charge_rel = NAN; /* Current charge in percent */
-       gauge_t capacity_charged = NAN; /* Charged capacity */
+       gauge_t capacity_charged; /* Charged capacity */
        gauge_t capacity_full = NAN; /* Total capacity */
        gauge_t capacity_design = NAN; /* Full design capacity */
 
@@ -416,10 +413,13 @@ static int sysfs_file_to_buffer(char const *dir, /* {{{ */
 
        if (fgets (buffer, buffer_size, fp) == NULL)
        {
-               char errbuf[1024];
                status = errno;
-               WARNING ("battery plugin: fgets failed: %s",
-                               sstrerror (status, errbuf, sizeof (errbuf)));
+               if (status != ENODEV)
+               {
+                       char errbuf[1024];
+                       WARNING ("battery plugin: fgets (%s) failed: %s", filename,
+                                       sstrerror (status, errbuf, sizeof (errbuf)));
+               }
                fclose (fp);
                return status;
        }
@@ -513,13 +513,15 @@ static int read_sysfs_callback (char const *dir, /* {{{ */
                        v *= -1.0;
                battery_submit (plugin_instance, "power", v * SYSFS_FACTOR);
        }
+       if (sysfs_file_to_gauge (dir, power_supply, "current_now", &v) == 0)
+       {
+               if (discharging)
+                       v *= -1.0;
+               battery_submit (plugin_instance, "current", v * SYSFS_FACTOR);
+       }
 
        if (sysfs_file_to_gauge (dir, power_supply, "voltage_now", &v) == 0)
                battery_submit (plugin_instance, "voltage", v * SYSFS_FACTOR);
-#if 0
-       if (sysfs_file_to_gauge (dir, power_supply, "voltage_min_design", &v) == 0)
-               battery_submit (plugin_instance, "voltage", v * SYSFS_FACTOR);
-#endif
 
        return (0);
 } /* }}} int read_sysfs_callback */
@@ -551,7 +553,7 @@ static int read_acpi_full_capacity (char const *dir, /* {{{ */
 
        ssnprintf (filename, sizeof (filename), "%s/%s/info", dir, power_supply);
        fh = fopen (filename, "r");
-       if ((fh = fopen (filename, "r")) == NULL)
+       if (fh == NULL)
                return (errno);
 
        /* last full capacity:      40090 mWh */
@@ -610,7 +612,7 @@ static int read_acpi_callback (char const *dir, /* {{{ */
 
        ssnprintf (filename, sizeof (filename), "%s/%s/state", dir, power_supply);
        fh = fopen (filename, "r");
-       if ((fh = fopen (filename, "r")) == NULL)
+       if (fh == NULL)
        {
                if ((errno == EAGAIN) || (errno == EINTR) || (errno == ENOENT))
                        return (0);
@@ -710,11 +712,10 @@ static int read_acpi (void) /* {{{ */
 
 static int read_pmu (void) /* {{{ */
 {
-       int i;
-
+       int i = 0;
        /* The upper limit here is just a safeguard. If there is a system with
         * more than 100 batteries, this can easily be increased. */
-       for (i = 0; i < 100; i++)
+       for (; i < 100; i++)
        {
                FILE *fh;
 
@@ -792,16 +793,14 @@ static int battery_read (void) /* {{{ */
        if (status == 0)
                return (0);
 
-       ERROR ("battery plugin: Add available input methods failed.");
+       ERROR ("battery plugin: All available input methods failed.");
        return (-1);
 } /* }}} int battery_read */
 #endif /* KERNEL_LINUX */
 
 static int battery_config (oconfig_item_t *ci)
 {
-       int i;
-
-       for (i = 0; i < ci->children_num; i++)
+       for (int i = 0; i < ci->children_num; i++)
        {
                oconfig_item_t *child = ci->children + i;