IOPowerSourcesPrivate.c [plain text]
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SCValidation.h>
#include <IOKit/pwr_mgt/IOPMLibPrivate.h>
#include "IOPowerSources.h"
#include "IOPowerSourcesPrivate.h"
#include "IOPSKeys.h"
extern CFArrayRef
IOPSCopyInternalBatteriesArray(CFTypeRef power_sources)
{
CFArrayRef array = isA_CFArray(IOPSCopyPowerSourcesList(power_sources));
CFMutableArrayRef ret_arr;
CFTypeRef name = NULL;
CFDictionaryRef ps;
CFStringRef transport_type;
int i, count;
if(!array) return NULL;
count = CFArrayGetCount(array);
name = NULL;
ret_arr = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
if(!ret_arr) goto exit;
for(i=0; i<count; i++) {
name = CFArrayGetValueAtIndex(array, i);
ps = isA_CFDictionary(IOPSGetPowerSourceDescription(power_sources, name));
if(ps) {
transport_type = isA_CFString(CFDictionaryGetValue(ps, CFSTR(kIOPSTransportTypeKey)));
if(transport_type && CFEqual(transport_type, CFSTR(kIOPSInternalType)))
{
CFArrayAppendValue(ret_arr, name);
}
}
}
if(0 == CFArrayGetCount(ret_arr)) {
CFRelease(ret_arr);
ret_arr = NULL;
}
exit:
CFRelease(array);
return ret_arr;
}
extern CFArrayRef
IOPSCopyUPSArray(CFTypeRef power_sources)
{
CFArrayRef array = isA_CFArray(IOPSCopyPowerSourcesList(power_sources));
CFMutableArrayRef ret_arr;
CFTypeRef name = NULL;
CFDictionaryRef ps;
CFStringRef transport_type;
int i, count;
if(!array) return NULL;
count = CFArrayGetCount(array);
name = NULL;
ret_arr = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
if(!ret_arr) goto exit;
for(i=0; i<count; i++) {
name = CFArrayGetValueAtIndex(array, i);
ps = isA_CFDictionary(IOPSGetPowerSourceDescription(power_sources, name));
if(ps) {
transport_type = isA_CFString(CFDictionaryGetValue(ps, CFSTR(kIOPSTransportTypeKey)));
if(transport_type && ( CFEqual(transport_type, CFSTR(kIOPSSerialTransportType)) ||
CFEqual(transport_type, CFSTR(kIOPSUSBTransportType)) ||
CFEqual(transport_type, CFSTR(kIOPSNetworkTransportType)) ) )
{
CFArrayAppendValue(ret_arr, name);
}
}
}
if(0 == CFArrayGetCount(ret_arr)) {
CFRelease(ret_arr);
ret_arr = NULL;
}
exit:
CFRelease(array);
return ret_arr;
}
extern CFTypeRef
IOPSGetActiveUPS(CFTypeRef ps_blob)
{
CFTypeRef ret_ups;
CFArrayRef ups_arr;
ups_arr = IOPSCopyUPSArray(ps_blob);
if(!ups_arr)
{
return NULL;
}
ret_ups = CFArrayGetValueAtIndex(ups_arr, 0);
CFRelease(ups_arr);
return ret_ups;
}
extern CFTypeRef
IOPSGetActiveBattery(CFTypeRef ps_blob)
{
CFTypeRef ret_ups;
CFArrayRef ups_arr;
ups_arr = IOPSCopyInternalBatteriesArray(ps_blob);
if(!ups_arr)
{
return NULL;
}
ret_ups = CFArrayGetValueAtIndex(ups_arr, 0);
CFRelease(ups_arr);
return ret_ups;
}
static CFStringRef getPowerSourceState(CFTypeRef blob, CFTypeRef id)
{
CFDictionaryRef the_dict = IOPSGetPowerSourceDescription(blob, id);
return CFDictionaryGetValue(the_dict, CFSTR(kIOPSPowerSourceStateKey));
}
extern CFStringRef IOPSGetProvidingPowerSourceType(CFTypeRef ps_blob)
{
CFArrayRef batt_arr = NULL;
CFArrayRef ups_arr = NULL;
CFTypeRef the_ups = NULL;
CFTypeRef the_batt = NULL;
CFStringRef ps_state = NULL;
if(kCFBooleanFalse == IOPSPowerSourceSupported(ps_blob, CFSTR(kIOPMBatteryPowerKey)))
{
if(kCFBooleanFalse == IOPSPowerSourceSupported(ps_blob, CFSTR(kIOPMUPSPowerKey))) {
return CFSTR(kIOPMACPowerKey);
} else {
the_ups = IOPSGetActiveUPS(ps_blob);
if(!the_ups) return CFSTR(kIOPMACPowerKey);
ps_state = getPowerSourceState(ps_blob, the_ups);
if(ps_state && CFEqual(ps_state, CFSTR(kIOPSACPowerValue)))
{
return CFSTR(kIOPMACPowerKey);
} else if(ps_state && CFEqual(ps_state, CFSTR(kIOPSBatteryPowerValue)))
{
return CFSTR(kIOPMUPSPowerKey);
}
}
return CFSTR(kIOPMACPowerKey);
} else {
the_batt = IOPSGetActiveBattery(ps_blob);
if(!the_batt) return CFSTR(kIOPMACPowerKey);
ps_state = getPowerSourceState(ps_blob, the_batt);
if(ps_state && CFEqual(ps_state, CFSTR(kIOPSBatteryPowerValue)))
{
return CFSTR(kIOPMBatteryPowerKey);
} else {
if(kCFBooleanFalse == IOPSPowerSourceSupported(ps_blob, CFSTR(kIOPMUPSPowerKey)))
{
return CFSTR(kIOPMACPowerKey);
} else {
the_ups = IOPSGetActiveUPS(ps_blob);
if(!the_ups) return CFSTR(kIOPMACPowerKey);
ps_state = getPowerSourceState(ps_blob, the_ups);
if(ps_state && CFEqual(ps_state, CFSTR(kIOPSBatteryPowerValue)))
{
return CFSTR(kIOPMUPSPowerKey);
} else if(ps_state && CFEqual(ps_state, CFSTR(kIOPSACPowerValue)))
{
return CFSTR(kIOPMACPowerKey);
}
}
}
}
return CFSTR(kIOPMACPowerKey);
}
extern CFBooleanRef IOPSPowerSourceSupported(CFTypeRef ps_blob, CFStringRef ps_type)
{
CFBooleanRef ret = kCFBooleanFalse;
if(!ps_blob) return kCFBooleanFalse;
if(!isA_CFString(ps_type))
{
ret = kCFBooleanFalse;
} else if(CFEqual(ps_type, CFSTR(kIOPMACPowerKey)))
{
ret = kCFBooleanTrue;
} else if(CFEqual(ps_type, CFSTR(kIOPMBatteryPowerKey)))
{
if(IOPSGetActiveBattery(ps_blob))
{
ret = kCFBooleanTrue;
} else {
ret = kCFBooleanFalse;
}
} else if(CFEqual(ps_type, CFSTR(kIOPMUPSPowerKey)))
{
if(IOPSGetActiveUPS(ps_blob))
{
ret = kCFBooleanTrue;
} else {
ret = kCFBooleanFalse;
}
}
return ret;
}