/* * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include #include #include #include "IOPowerSources.h" #include "IOPowerSourcesPrivate.h" #include "IOPSKeys.h" /* IOPSCopyInternalBatteriesArray * * Argument: * CFTypeRef power_sources: The return value from IOPSCoyPowerSourcesInfo() * Return value: * CFArrayRef: all the batteries we found * NULL if none are found */ 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; // Iterate through power_sources for(i=0; i AC Power return CFSTR(kIOPMACPowerKey); } else { // optimization opportunity: needless loops inside IOPSGetActiveUPS 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))) { // no batteries, yes UPS, UPS is running off of AC power -> AC Power return CFSTR(kIOPMACPowerKey); } else if(ps_state && CFEqual(ps_state, CFSTR(kIOPSBatteryPowerValue))) { // no batteries, yes UPS, UPS is running off of Battery power -> UPS Power return CFSTR(kIOPMUPSPowerKey); } } // Error in the data we were passed return CFSTR(kIOPMACPowerKey); } else { // Optimization opportunity: needless loops inside IOPSGetActiveBattery 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))) { // Yes batteries, yes running on battery power -> Battery power return CFSTR(kIOPMBatteryPowerKey); } else { // batteries are on AC power. let's check UPS. // optimize. if(kCFBooleanFalse == IOPSPowerSourceSupported(ps_blob, CFSTR(kIOPMUPSPowerKey))) { // yes batteries on AC power, no UPS -> AC Power 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))) { // yes batteries on AC power, UPS is on battery power -> UPS Power return CFSTR(kIOPMUPSPowerKey); } else if(ps_state && CFEqual(ps_state, CFSTR(kIOPSACPowerValue))) { // yes batteries on AC Power, UPS is on AC Power -> AC Power return CFSTR(kIOPMACPowerKey); } } } // Error in the data we were passed. // syslog(LOG_INFO, "PowerManagement: unexpected point 3 reached in _getProvidingPowerSourceType\n"); } // Should not reach this point. Return something safe. return CFSTR(kIOPMACPowerKey); } /*** * int powerSourceSupported(CFStringRef) * takes: CFSTR of kIOPMACPowerKey, kIOPMBatteryPowerKey, kIOPMUPSPowerKey * returns true if this machine supports (has) that power type. */ 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; }