diff --git a/src/battery.c b/src/battery.c
index d467ecc8614148181cd945941f45d616915a6f26..2141a58c725f3515963f71e29566ac0315b0fca8 100644 (file)
--- a/src/battery.c
+++ b/src/battery.c
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#include "collectd.h"
#include "common.h"
#include "plugin.h"
+#include "utils_debug.h"
#define MODULE_NAME "battery"
#define BUFSIZE 512
#define MODULE_NAME "battery"
#define BUFSIZE 512
-#if defined(KERNEL_LINUX)
+#if HAVE_MACH_MACH_TYPES_H
+# include <mach/mach_types.h>
+#endif
+#if HAVE_MACH_MACH_INIT_H
+# include <mach/mach_init.h>
+#endif
+#if HAVE_MACH_MACH_ERROR_H
+# include <mach/mach_error.h>
+#endif
+#if HAVE_COREFOUNDATION_COREFOUNDATION_H
+# include <CoreFoundation/CoreFoundation.h>
+#endif
+#if HAVE_IOKIT_IOKITLIB_H
+# include <IOKit/IOKitLib.h>
+#endif
+#if HAVE_IOKIT_IOTYPES_H
+# include <IOKit/IOTypes.h>
+#endif
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+# include <IOKit/ps/IOPowerSources.h>
+#endif
+#if HAVE_IOKIT_PS_IOPSKEYS_H
+# include <IOKit/ps/IOPSKeys.h>
+#endif
+
+#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H || KERNEL_LINUX
# define BATTERY_HAVE_READ 1
#else
# define BATTERY_HAVE_READ 0
# define BATTERY_HAVE_READ 1
#else
# define BATTERY_HAVE_READ 0
static char *ds_def_current[] =
{
static char *ds_def_current[] =
{
- "DS:current:GAUGE:25:U:U",
+ "DS:current:GAUGE:"COLLECTD_HEARTBEAT":U:U",
NULL
};
static int ds_num_current = 1;
static char *ds_def_voltage[] =
{
NULL
};
static int ds_num_current = 1;
static char *ds_def_voltage[] =
{
- "DS:voltage:GAUGE:25:U:U",
+ "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
NULL
};
static int ds_num_voltage = 1;
static char *ds_def_charge[] =
{
NULL
};
static int ds_num_voltage = 1;
static char *ds_def_charge[] =
{
- "DS:charge:GAUGE:25:0:U",
+ "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
NULL
};
static int ds_num_charge = 1;
NULL
};
static int ds_num_charge = 1;
+#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
+ /* No global variables */
+/* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#elif KERNEL_LINUX
static int battery_pmu_num = 0;
static char *battery_pmu_file = "/proc/pmu/battery_%i";
static int battery_pmu_num = 0;
static char *battery_pmu_file = "/proc/pmu/battery_%i";
+#endif /* KERNEL_LINUX */
static void battery_init (void)
{
static void battery_init (void)
{
-#if BATTERY_HAVE_READ
+#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
+ /* No init neccessary */
+/* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#elif KERNEL_LINUX
int len;
char filename[BUFSIZE];
int len;
char filename[BUFSIZE];
if (access (filename, R_OK))
break;
}
if (access (filename, R_OK))
break;
}
-#endif
+#endif /* KERNEL_LINUX */
return;
}
return;
}
@@ -166,9 +202,240 @@ static void battery_submit (char *inst, double current, double voltage, double c
}
}
}
}
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
+double dict_get_double (CFDictionaryRef dict, char *key_string)
+{
+ double val_double;
+ long long val_int;
+ CFNumberRef val_obj;
+ CFStringRef key_obj;
+
+ key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
+ kCFStringEncodingASCII);
+ if (key_obj == NULL)
+ {
+ DBG ("CFStringCreateWithCString (%s) failed.\n", key_string);
+ return (INVALID_VALUE);
+ }
+
+ if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
+ {
+ DBG ("CFDictionaryGetValue (%s) failed.", key_string);
+ CFRelease (key_obj);
+ return (INVALID_VALUE);
+ }
+ CFRelease (key_obj);
+
+ if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
+ {
+ if (CFNumberIsFloatType (val_obj))
+ {
+ CFNumberGetValue (val_obj,
+ kCFNumberDoubleType,
+ &val_double);
+ }
+ else
+ {
+ CFNumberGetValue (val_obj,
+ kCFNumberLongLongType,
+ &val_int);
+ val_double = val_int;
+ }
+ }
+ else
+ {
+ DBG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
+ return (INVALID_VALUE);
+ }
+
+ return (val_double);
+}
+#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
+
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+static void get_via_io_power_sources (double *ret_charge,
+ double *ret_current,
+ double *ret_voltage)
+{
+ CFTypeRef ps_raw;
+ CFArrayRef ps_array;
+ int ps_array_len;
+ CFDictionaryRef ps_dict;
+ CFTypeRef ps_obj;
+
+ double temp_double;
+ int i;
+
+ ps_raw = IOPSCopyPowerSourcesInfo ();
+ ps_array = IOPSCopyPowerSourcesList (ps_raw);
+ ps_array_len = CFArrayGetCount (ps_array);
+
+ DBG ("ps_array_len == %i", ps_array_len);
+
+ for (i = 0; i < ps_array_len; i++)
+ {
+ ps_obj = CFArrayGetValueAtIndex (ps_array, i);
+ ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
+
+ if (ps_dict == NULL)
+ {
+ DBG ("IOPSGetPowerSourceDescription failed.");
+ continue;
+ }
+
+ if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
+ {
+ DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
+ continue;
+ }
+
+ /* FIXME: Check if this is really an internal battery */
+
+ if (*ret_charge == INVALID_VALUE)
+ {
+ /* This is the charge in percent. */
+ temp_double = dict_get_double (ps_dict,
+ kIOPSCurrentCapacityKey);
+ if ((temp_double != INVALID_VALUE)
+ && (temp_double >= 0.0)
+ && (temp_double <= 100.0))
+ *ret_charge = temp_double;
+ }
+
+ if (*ret_current == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (ps_dict,
+ kIOPSCurrentKey);
+ if (temp_double != INVALID_VALUE)
+ *ret_current = temp_double / 1000.0;
+ }
+
+ if (*ret_voltage == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (ps_dict,
+ kIOPSVoltageKey);
+ if (temp_double != INVALID_VALUE)
+ *ret_voltage = temp_double / 1000.0;
+ }
+ }
+
+ CFRelease(ps_array);
+ CFRelease(ps_raw);
+}
+#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#if HAVE_IOKIT_IOKITLIB_H
+static void get_via_generic_iokit (double *ret_charge,
+ double *ret_current,
+ double *ret_voltage)
+{
+ kern_return_t status;
+ io_iterator_t iterator;
+ io_object_t io_obj;
+
+ 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;
+
+ status = IOServiceGetMatchingServices (kIOMasterPortDefault,
+ IOServiceNameMatching ("battery"),
+ &iterator);
+ if (status != kIOReturnSuccess)
+ {
+ DBG ("IOServiceGetMatchingServices failed.");
+ return;
+ }
+
+ while ((io_obj = IOIteratorNext (iterator)))
+ {
+ status = IORegistryEntryCreateCFProperties (io_obj,
+ (CFMutableDictionaryRef *) &bat_root_dict,
+ kCFAllocatorDefault,
+ kNilOptions);
+ if (status != kIOReturnSuccess)
+ {
+ DBG ("IORegistryEntryCreateCFProperties failed.");
+ continue;
+ }
+
+ bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
+ CFSTR ("IOBatteryInfo"));
+ if (bat_info_arry == NULL)
+ {
+ CFRelease (bat_root_dict);
+ continue;
+ }
+ bat_info_arry_len = CFArrayGetCount (bat_info_arry);
+
+ for (bat_info_arry_pos = 0;
+ bat_info_arry_pos < bat_info_arry_len;
+ bat_info_arry_pos++)
+ {
+ bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
+
+ if (*ret_charge == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (bat_info_dict,
+ "Capacity");
+ if (temp_double != INVALID_VALUE)
+ *ret_charge = temp_double / 1000.0;
+ }
+
+ if (*ret_current == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (bat_info_dict,
+ "Current");
+ if (temp_double != INVALID_VALUE)
+ *ret_current = temp_double / 1000.0;
+ }
+
+ if (*ret_voltage == INVALID_VALUE)
+ {
+ temp_double = dict_get_double (bat_info_dict,
+ "Voltage");
+ if (temp_double != INVALID_VALUE)
+ *ret_voltage = temp_double / 1000.0;
+ }
+ }
+
+ CFRelease (bat_root_dict);
+ }
+
+ IOObjectRelease (iterator);
+}
+#endif /* HAVE_IOKIT_IOKITLIB_H */
+
static void battery_read (void)
{
static void battery_read (void)
{
-#ifdef KERNEL_LINUX
+#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
+ double charge = INVALID_VALUE; /* Current charge in Ah */
+ double current = INVALID_VALUE; /* Current in A */
+ double voltage = INVALID_VALUE; /* Voltage in V */
+
+ double charge_rel = INVALID_VALUE; /* Current charge in percent */
+ double charge_abs = INVALID_VALUE; /* Total capacity */
+
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+ get_via_io_power_sources (&charge_rel, ¤t, &voltage);
+#endif
+#if HAVE_IOKIT_IOKITLIB_H
+ get_via_generic_iokit (&charge_abs, ¤t, &voltage);
+#endif
+
+ if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
+ charge = charge_abs * charge_rel / 100.0;
+
+ if ((charge != INVALID_VALUE)
+ || (current != INVALID_VALUE)
+ || (voltage != INVALID_VALUE))
+ battery_submit ("0", current, voltage, charge);
+/* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#elif KERNEL_LINUX
FILE *fh;
char buffer[BUFSIZE];
char filename[BUFSIZE];
FILE *fh;
char buffer[BUFSIZE];
char filename[BUFSIZE];