nut/drivers/macosx-ups.c

463 lines
13 KiB
C
Raw Permalink Normal View History

2012-08-12 21:39:31 +00:00
/* Bridge driver to read Mac OS X UPS status (as displayed in Energy Saver control panel)
*
2016-07-18 00:11:41 +00:00
* Copyright (C) 2011-2012, 2015 Charles Lepple <clepple+nut@gmail.com>
2012-08-12 21:39:31 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "main.h"
#include <regex.h>
#include "CoreFoundation/CoreFoundation.h"
#include "IOKit/ps/IOPowerSources.h"
#include "IOKit/ps/IOPSKeys.h"
#define DRIVER_NAME "Mac OS X UPS meta-driver"
2016-07-18 00:11:41 +00:00
#define DRIVER_VERSION "1.2"
2012-08-12 21:39:31 +00:00
/* driver description structure */
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"Charles Lepple <clepple+nut@gmail.com>",
DRV_EXPERIMENTAL,
{ NULL }
};
#if 0
#define CFRelease(ref) do { upsdebugx(3, "%s:%d: CFRelease(%p)", __FILE__, __LINE__, ref); CFRelease(ref); } while(0)
#endif
2016-07-18 00:11:41 +00:00
static CFStringRef g_power_source_name = NULL;
2012-08-12 21:39:31 +00:00
static double max_capacity_value = 100.0;
/*! Copy the current power dictionary.
*
* Caller must release power dictionary when finished with it.
*/
2016-07-18 00:11:41 +00:00
static CFDictionaryRef copy_power_dictionary(CFStringRef power_source_name)
2012-08-12 21:39:31 +00:00
{
2016-07-18 00:11:41 +00:00
CFTypeRef power_sources_info, power_source;
CFArrayRef sources_list;
CFDictionaryRef this_power_dictionary, power_dictionary = NULL;
CFStringRef this_power_source_name;
CFIndex num_keys, index;
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
power_sources_info = IOPSCopyPowerSourcesInfo();
assert(power_sources_info);
upsdebugx(6, "%s: Got power_sources_info:", __func__);
if(nut_debug_level >= 6) CFShow(power_sources_info);
upsdebugx(5, "power_source_name = ");
if(nut_debug_level >= 5) CFShow(power_source_name);
upsdebugx(6, "end power_source_name");
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
sources_list = IOPSCopyPowerSourcesList(power_sources_info);
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
num_keys = CFArrayGetCount(sources_list);
for(index=0; index < num_keys; index++) {
upsdebugx(6, "%s: Getting power source %ld/%ld...", __func__, index+1, num_keys);
power_source = CFArrayGetValueAtIndex(sources_list, index);
assert(power_source);
upsdebugx(6, "%s: power source %ld = ", __func__, index+1);
if(nut_debug_level >= 6) CFShow(power_source);
this_power_dictionary = IOPSGetPowerSourceDescription(power_sources_info, power_source);
assert(this_power_dictionary);
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
this_power_source_name = CFDictionaryGetValue(this_power_dictionary, CFSTR(kIOPSNameKey));
assert(this_power_source_name);
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
if(!CFStringCompare(this_power_source_name, power_source_name, 0)) {
power_dictionary = this_power_dictionary;
CFRetain(power_dictionary);
break;
}
}
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
if(power_dictionary) {
upsdebugx(5, "CFShowing 'power_dictionary'");
if(nut_debug_level >= 5) CFShow(power_dictionary);
}
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
/* Get a new power_sources_info next time: */
CFRelease(power_sources_info);
CFRelease(sources_list);
2012-08-12 21:39:31 +00:00
return power_dictionary;
}
void upsdrv_initinfo(void)
{
/* try to detect the UPS here - call fatal_with_errno(EXIT_FAILURE, ) if it fails */
char device_name[80] = "";
CFStringRef device_type_cfstr, device_name_cfstr;
CFPropertyListRef power_dictionary;
CFNumberRef max_capacity;
upsdebugx(1, "upsdrv_initinfo()");
dstate_setinfo("device.mfr", "(unknown)");
2016-07-18 00:11:41 +00:00
power_dictionary = copy_power_dictionary(g_power_source_name);
2012-08-12 21:39:31 +00:00
device_type_cfstr = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSTypeKey));
if(device_type_cfstr && !CFStringCompare(device_type_cfstr, CFSTR(kIOPSInternalBatteryType), 0)) {
dstate_setinfo("device.type", "battery");
}
upsdebugx(2, "Getting 'Name' key");
device_name_cfstr = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSNameKey));
if (!device_name_cfstr) {
fatalx(EXIT_FAILURE, "Couldn't retrieve 'Name' key from power dictionary.");
}
CFRetain(device_name_cfstr);
CFStringGetCString(device_name_cfstr, device_name, sizeof(device_name), kCFStringEncodingUTF8);
upsdebugx(2, "Got name: %s", device_name);
CFRelease(device_name_cfstr);
dstate_setinfo("device.model", "%s", device_name);
max_capacity = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSMaxCapacityKey));
if(max_capacity) {
CFRetain(max_capacity);
CFNumberGetValue(max_capacity, kCFNumberDoubleType, &max_capacity_value);
CFRelease(max_capacity);
upsdebugx(3, "Max Capacity = %.f units (usually 100)", max_capacity_value);
if(max_capacity_value != 100) {
upsdebugx(1, "Max Capacity: %f != 100", max_capacity_value);
}
}
/* upsh.instcmd = instcmd; */
CFRelease(power_dictionary);
}
void upsdrv_updateinfo(void)
{
CFPropertyListRef power_dictionary;
CFStringRef power_source_state;
CFNumberRef battery_voltage, battery_runtime;
CFNumberRef current_capacity;
CFBooleanRef is_charging;
double max_capacity_value = 100.0, current_capacity_value;
upsdebugx(1, "upsdrv_updateinfo()");
2016-07-18 00:11:41 +00:00
power_dictionary = copy_power_dictionary( g_power_source_name );
if(!power_dictionary) {
dstate_datastale();
return;
}
2012-08-12 21:39:31 +00:00
status_init();
/* Retrieve OL/OB state */
power_source_state = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSPowerSourceStateKey));
assert(power_source_state);
CFRetain(power_source_state);
upsdebugx(3, "Power Source State:");
if(nut_debug_level >= 3) CFShow(power_source_state);
if(!CFStringCompare(power_source_state, CFSTR(kIOPSACPowerValue), 0)) {
status_set("OL");
} else {
status_set("OB");
}
CFRelease(power_source_state);
/* Retrieve CHRG state */
is_charging = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSIsChargingKey));
if(is_charging) {
Boolean is_charging_value;
is_charging_value = CFBooleanGetValue(is_charging);
if(is_charging_value) {
status_set("CHRG");
}
}
status_commit();
/* Retrieve battery voltage */
battery_voltage = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSVoltageKey));
if(battery_voltage) {
int battery_voltage_value;
CFNumberGetValue(battery_voltage, kCFNumberIntType, &battery_voltage_value);
upsdebugx(2, "battery_voltage = %d mV", battery_voltage_value);
dstate_setinfo("battery.voltage", "%.3f", battery_voltage_value/1000.0);
}
/* Retrieve battery runtime */
battery_runtime = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSTimeToEmptyKey));
if(battery_runtime) {
double battery_runtime_value;
CFNumberGetValue(battery_runtime, kCFNumberDoubleType, &battery_runtime_value);
upsdebugx(2, "battery_runtime = %.f minutes", battery_runtime_value);
if(battery_runtime_value > 0) {
dstate_setinfo("battery.runtime", "%d", (int)(battery_runtime_value*60));
} else {
dstate_delinfo("battery.runtime");
}
} else {
dstate_delinfo("battery.runtime");
}
/* Retrieve current capacity */
current_capacity = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSCurrentCapacityKey));
if(current_capacity) {
CFNumberGetValue(current_capacity, kCFNumberDoubleType, &current_capacity_value);
upsdebugx(2, "Current Capacity = %.f/%.f units", current_capacity_value, max_capacity_value);
if(max_capacity_value > 0) {
dstate_setinfo("battery.charge", "%.f", 100.0 * current_capacity_value / max_capacity_value);
}
}
/* TODO: it should be possible to set poll_interval (and maxage in the
* server) to an absurdly large value, and use notify(3) to get
* updates.
*/
/*
* poll_interval = 2;
*/
dstate_dataok();
CFRelease(power_dictionary);
}
void upsdrv_shutdown(void)
{
/* tell the UPS to shut down, then return - DO NOT SLEEP HERE */
/* maybe try to detect the UPS here, but try a shutdown even if
it doesn't respond at first if possible */
/* NOTE: Mac OS X already has shutdown routines - this driver is more
for monitoring and notification purposes. Still, there is a key that
might be useful to set in SystemConfiguration land. */
fatalx(EXIT_FAILURE, "shutdown not supported");
/* you may have to check the line status since the commands
for toggling power are frequently different for OL vs. OB */
/* OL: this must power cycle the load if possible */
/* OB: the load must remain off until the power returns */
}
/*
static int instcmd(const char *cmdname, const char *extra)
{
if (!strcasecmp(cmdname, "test.battery.stop")) {
ser_send_buf(upsfd, ...);
return STAT_INSTCMD_HANDLED;
}
upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
return STAT_INSTCMD_UNKNOWN;
}
*/
/* TODO:
There is a configuration file here:
/Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist
with several keys under UPSDefaultThresholds:
* UPSShutdownAfterMinutes
* UPSShutdownAtLevel
* UPSShutdownAtMinutesLeft
It is not likely that these keys can be written, but they might be good values for NUT variables.
In conjunction with 'ignorelb' and a delta, this could be used to synthesize a
LB status right before the computer shuts down.
*/
/*
static int setvar(const char *varname, const char *val)
{
if (!strcasecmp(varname, "ups.test.interval")) {
ser_send_buf(upsfd, ...);
return STAT_SET_HANDLED;
}
upslogx(LOG_NOTICE, "setvar: unknown variable [%s]", varname);
return STAT_SET_UNKNOWN;
}
*/
void upsdrv_help(void)
{
}
/* list flags and values that you want to receive via -x */
void upsdrv_makevartable(void)
{
/* allow '-x xyzzy' */
/* addvar(VAR_FLAG, "xyzzy", "Enable xyzzy mode"); */
/* allow '-x foo=<some value>' */
/* addvar(VAR_VALUE, "foo", "Override foo setting"); */
addvar(VAR_VALUE, "model", "Regular Expression to match power source model name");
}
void upsdrv_initups(void)
{
CFArrayRef power_source_key_list;
CFIndex num_keys, index;
CFDictionaryRef power_dictionary;
CFTypeRef power_blob;
CFStringRef potential_key, potential_model;
2016-07-18 00:11:41 +00:00
char *model_name; /* regex(3) */
char potential_model_name[256];
regex_t model_regex;
2012-08-12 21:39:31 +00:00
int ret;
upsdebugx(3, "upsdrv_initups(): Power Sources blob:");
/* upsfd = ser_open(device_path); */
/* ser_set_speed(upsfd, device_path, B1200); */
power_blob = IOPSCopyPowerSourcesInfo();
if(!power_blob) {
fatalx(EXIT_FAILURE, "Couldn't retrieve Power Sources blob");
}
if(nut_debug_level >= 3) CFShow(power_blob);
2016-07-18 00:11:41 +00:00
/* The CFDictionary through 10.9 has changed to a CFArray, so this part is no longer applicable: */
#if 0
2012-08-12 21:39:31 +00:00
if(!strcmp(device_name, "auto")) {
device_name = "/UPS";
}
upsdebugx(2, "Matching power supply key names against regex '%s'", device_name);
ret = regcomp(&name_regex, device_name, REG_EXTENDED|REG_NOSUB|REG_ICASE);
if(ret) {
fatalx(EXIT_FAILURE,
"Failed to compile regex from 'port' parameter: '%s'.",
device_name);
}
2016-07-18 00:11:41 +00:00
#endif
2012-08-12 21:39:31 +00:00
if((model_name = getval("model"))) {
upsdebugx(2, "Matching power supply model names against regex '%s'", model_name);
ret = regcomp(&model_regex, model_name, REG_EXTENDED|REG_NOSUB|REG_ICASE);
if(ret) {
fatalx(EXIT_FAILURE,
"Failed to compile regex from 'model' parameter: '%s'.",
model_name);
}
}
power_source_key_list = IOPSCopyPowerSourcesList(power_blob);
num_keys = CFArrayGetCount(power_source_key_list);
upsdebugx(1, "Number of power supplies found: %d", (int)num_keys);
if(nut_debug_level >= 3) CFShow(power_source_key_list);
if(num_keys < 1) {
/* bail */
2016-07-18 00:11:41 +00:00
fatalx(EXIT_FAILURE, "Couldn't find any UPS or battery. Is your UPS shown in the 'Energy Saver' control panel?");
2012-08-12 21:39:31 +00:00
}
for(index=0; index < num_keys; index++) {
2016-07-18 00:11:41 +00:00
upsdebugx(2, "Retrieving power source #%ld", index+1);
2012-08-12 21:39:31 +00:00
potential_key = CFArrayGetValueAtIndex(power_source_key_list, index);
2016-07-18 00:11:41 +00:00
upsdebugx(3, "Power source key/index:");
if(nut_debug_level >= 3) CFShow(potential_key);
2012-08-12 21:39:31 +00:00
2016-07-18 00:11:41 +00:00
power_dictionary = IOPSGetPowerSourceDescription(power_blob, potential_key);
assert(power_dictionary);
CFRetain(power_dictionary);
2012-08-12 21:39:31 +00:00
upsdebugx(2, "Getting 'Name' key (UPS model)");
potential_model = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSNameKey));
2016-07-18 00:11:41 +00:00
CFRetain(potential_model);
if(CFStringGetCString(potential_model, potential_model_name, sizeof(potential_model_name), kCFStringEncodingUTF8)) {
upsdebugx(1, " model name: %s", potential_model_name);
} else {
upsdebugx(1, " (failed to retrieve 'Name')");
}
2012-08-12 21:39:31 +00:00
CFRelease(power_dictionary);
2016-07-18 00:11:41 +00:00
if(model_name) {
ret = regexec(&model_regex, potential_model_name, 0,0,0);
if(!ret) {
upsdebugx(2, "Matched model name");
2012-08-12 21:39:31 +00:00
break;
}
}
2016-07-18 00:11:41 +00:00
CFRelease(potential_model);
2012-08-12 21:39:31 +00:00
}
if(model_name) {
regfree(&model_regex);
}
if(ret) {
2016-07-18 00:11:41 +00:00
if(model_name) {
fatalx(EXIT_FAILURE, "Couldn't find UPS or battery matching 'model' (%s)",
model_name);
} else {
fatalx(EXIT_FAILURE, "Couldn't find an UPS or battery.");
}
2012-08-12 21:39:31 +00:00
}
2016-07-18 00:11:41 +00:00
g_power_source_name = potential_model;
2012-08-12 21:39:31 +00:00
/* the upsh handlers can't be done here, as they get initialized
* shortly after upsdrv_initups returns to main.
*/
/* don't try to detect the UPS here */
/* do stuff */
}
void upsdrv_cleanup(void)
{
upsdebugx(1, "Cleanup: release references");
2016-07-18 00:11:41 +00:00
CFRelease(g_power_source_name);
2012-08-12 21:39:31 +00:00
/* free(dynamic_mem); */
/* ser_close(upsfd, device_path); */
}