2 * Copyright (c) 1998-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <IOKit/pwr_mgt/IOPMPowerSource.h>
32 #include <IOKit/pwr_mgt/IOPM.h>
33 #include <IOKit/IOMessage.h>
34 #include <IOKit/IOLib.h>
36 #define super IOService
38 OSDefineMetaClassAndStructors(IOPMPowerSource
, IOService
)
40 // *****************************************************************************
43 // Static initializer for IOPMPowerSource. Returns a new instance of the class
44 // which the caller must attach to the power plane.
45 // *****************************************************************************
47 IOPMPowerSource
*IOPMPowerSource::powerSource(void)
49 IOPMPowerSource
*ps
= new IOPMPowerSource
;
58 // *****************************************************************************
61 // *****************************************************************************
62 bool IOPMPowerSource::init (void)
70 properties
= OSDictionary::withCapacity(10);
71 if(!properties
) return false;
72 properties
->setCapacityIncrement(1);
74 externalConnectedKey
= OSSymbol::withCString(kIOPMPSExternalConnectedKey
);
75 externalChargeCapableKey
= OSSymbol::withCString(kIOPMPSExternalChargeCapableKey
);
76 batteryInstalledKey
= OSSymbol::withCString(kIOPMPSBatteryInstalledKey
);
77 chargingKey
= OSSymbol::withCString(kIOPMPSIsChargingKey
);
78 warnLevelKey
= OSSymbol::withCString(kIOPMPSAtWarnLevelKey
);
79 criticalLevelKey
= OSSymbol::withCString(kIOPMPSAtCriticalLevelKey
);
80 currentCapacityKey
= OSSymbol::withCString(kIOPMPSCurrentCapacityKey
);
81 maxCapacityKey
= OSSymbol::withCString(kIOPMPSMaxCapacityKey
);
82 timeRemainingKey
= OSSymbol::withCString(kIOPMPSTimeRemainingKey
);
83 amperageKey
= OSSymbol::withCString(kIOPMPSAmperageKey
);
84 voltageKey
= OSSymbol::withCString(kIOPMPSVoltageKey
);
85 cycleCountKey
= OSSymbol::withCString(kIOPMPSCycleCountKey
);
86 adapterInfoKey
= OSSymbol::withCString(kIOPMPSAdapterInfoKey
);
87 locationKey
= OSSymbol::withCString(kIOPMPSLocationKey
);
88 errorConditionKey
= OSSymbol::withCString(kIOPMPSErrorConditionKey
);
89 manufacturerKey
= OSSymbol::withCString(kIOPMPSManufacturerKey
);
90 modelKey
= OSSymbol::withCString(kIOPMPSModelKey
);
91 serialKey
= OSSymbol::withCString(kIOPMPSSerialKey
);
92 batteryInfoKey
= OSSymbol::withCString(kIOPMPSLegacyBatteryInfoKey
);
97 // *****************************************************************************
100 // *****************************************************************************
101 void IOPMPowerSource::free(void)
103 if(properties
) properties
->release();
104 if(externalConnectedKey
) externalConnectedKey
->release();
105 if(externalChargeCapableKey
) externalChargeCapableKey
->release();
106 if(batteryInstalledKey
) batteryInstalledKey
->release();
107 if(chargingKey
) chargingKey
->release();
108 if(warnLevelKey
) warnLevelKey
->release();
109 if(criticalLevelKey
) criticalLevelKey
->release();
110 if(currentCapacityKey
) currentCapacityKey
->release();
111 if(maxCapacityKey
) maxCapacityKey
->release();
112 if(timeRemainingKey
) timeRemainingKey
->release();
113 if(amperageKey
) amperageKey
->release();
114 if(voltageKey
) voltageKey
->release();
115 if(cycleCountKey
) cycleCountKey
->release();
116 if(adapterInfoKey
) adapterInfoKey
->release();
117 if(errorConditionKey
) errorConditionKey
->release();
118 if(manufacturerKey
) manufacturerKey
->release();
119 if(modelKey
) modelKey
->release();
120 if(serialKey
) serialKey
->release();
121 if(locationKey
) locationKey
->release();
122 if(batteryInfoKey
) batteryInfoKey
->release();
125 // *****************************************************************************
128 // Update power source state in IORegistry and message interested clients
129 // notifying them of our change.
130 // *****************************************************************************
131 void IOPMPowerSource::updateStatus (void)
133 OSCollectionIterator
*iterator
;
134 OSObject
*iteratorKey
;
137 iterator
= OSCollectionIterator::withCollection(properties
);
138 if(!iterator
) return;
140 while ((iteratorKey
= iterator
->getNextObject())) {
143 key
= OSDynamicCast(OSSymbol
, iteratorKey
);
145 obj
= properties
->getObject(key
);
147 setProperty(key
, obj
);
151 // And up goes the flare
152 messageClients(kIOPMMessageBatteryStatusHasChanged
);
156 /*******************************************************************************
158 * PROTECTED Accessors. All the setters! Yay!
160 ******************************************************************************/
162 void IOPMPowerSource::setExternalConnected(bool b
) {
163 properties
->setObject(
164 externalConnectedKey
,
165 b
? kOSBooleanTrue
:kOSBooleanFalse
);
168 void IOPMPowerSource::setExternalChargeCapable(bool b
) {
169 properties
->setObject(
170 externalChargeCapableKey
,
171 b
? kOSBooleanTrue
:kOSBooleanFalse
);
174 void IOPMPowerSource::setBatteryInstalled(bool b
) {
175 properties
->setObject(
177 b
? kOSBooleanTrue
:kOSBooleanFalse
);
180 void IOPMPowerSource::setIsCharging(bool b
) {
181 properties
->setObject(
183 b
? kOSBooleanTrue
:kOSBooleanFalse
);
186 void IOPMPowerSource::setAtWarnLevel(bool b
) {
187 properties
->setObject(
189 b
? kOSBooleanTrue
:kOSBooleanFalse
);
192 void IOPMPowerSource::setAtCriticalLevel(bool b
) {
193 properties
->setObject(
195 b
? kOSBooleanTrue
:kOSBooleanFalse
);
199 void IOPMPowerSource::setCurrentCapacity(unsigned int val
) {
200 OSNumber
*n
= OSNumber::withNumber(val
, 32);
201 properties
->setObject(
207 void IOPMPowerSource::setMaxCapacity(unsigned int val
) {
208 OSNumber
*n
= OSNumber::withNumber(val
, 32);
209 properties
->setObject(
215 void IOPMPowerSource::setTimeRemaining(int val
) {
216 OSNumber
*n
= OSNumber::withNumber(val
, 32);
217 properties
->setObject(
223 void IOPMPowerSource::setAmperage(int val
) {
224 OSNumber
*n
= OSNumber::withNumber(val
, 32);
225 properties
->setObject(
231 void IOPMPowerSource::setVoltage(unsigned int val
) {
232 OSNumber
*n
= OSNumber::withNumber(val
, 32);
233 properties
->setObject(
239 void IOPMPowerSource::setCycleCount(unsigned int val
) {
240 OSNumber
*n
= OSNumber::withNumber(val
, 32);
241 properties
->setObject(
247 void IOPMPowerSource::setAdapterInfo(int val
) {
248 OSNumber
*n
= OSNumber::withNumber(val
, 32);
249 properties
->setObject(
255 void IOPMPowerSource::setLocation(int val
) {
256 OSNumber
*n
= OSNumber::withNumber(val
, 32);
257 properties
->setObject(
263 void IOPMPowerSource::setErrorCondition(OSSymbol
*s
) {
264 properties
->setObject(errorConditionKey
, s
);
267 void IOPMPowerSource::setManufacturer(OSSymbol
*s
) {
268 properties
->setObject(manufacturerKey
, s
);
271 void IOPMPowerSource::setModel(OSSymbol
*s
) {
272 properties
->setObject(modelKey
, s
);
275 void IOPMPowerSource::setSerial(OSSymbol
*s
) {
276 properties
->setObject(serialKey
, s
);
279 void IOPMPowerSource::setLegacyIOBatteryInfo(OSDictionary
*d
) {
280 properties
->setObject(batteryInfoKey
, d
);
286 /*******************************************************************************
288 * PUBLIC Accessors. All the getters! Boo!
290 ******************************************************************************/
292 bool IOPMPowerSource::externalConnected(void) {
293 return (kOSBooleanTrue
== properties
->getObject(externalConnectedKey
));
296 bool IOPMPowerSource::externalChargeCapable(void) {
297 return (kOSBooleanTrue
== properties
->getObject(externalChargeCapableKey
));
300 bool IOPMPowerSource::batteryInstalled(void) {
301 return (kOSBooleanTrue
== properties
->getObject(batteryInstalledKey
));
304 bool IOPMPowerSource::isCharging(void) {
305 return (kOSBooleanTrue
== properties
->getObject(chargingKey
));
308 bool IOPMPowerSource::atWarnLevel(void) {
309 return (kOSBooleanTrue
== properties
->getObject(warnLevelKey
));
312 bool IOPMPowerSource::atCriticalLevel(void) {
313 return (kOSBooleanTrue
== properties
->getObject(criticalLevelKey
));
316 unsigned int IOPMPowerSource::currentCapacity(void) {
318 n
= OSDynamicCast(OSNumber
, properties
->getObject(currentCapacityKey
));
320 else return (unsigned int)n
->unsigned32BitValue();
323 unsigned int IOPMPowerSource::maxCapacity(void) {
325 n
= OSDynamicCast(OSNumber
, properties
->getObject(maxCapacityKey
));
327 else return (unsigned int)n
->unsigned32BitValue();
330 unsigned int IOPMPowerSource::capacityPercentRemaining(void)
332 unsigned int _currentCapacity
= currentCapacity();
333 unsigned int _maxCapacity
= maxCapacity();
334 if(0 == _maxCapacity
) {
337 return ((100*_currentCapacity
) / _maxCapacity
);
341 int IOPMPowerSource::timeRemaining(void) {
343 n
= OSDynamicCast(OSNumber
, properties
->getObject(timeRemainingKey
));
345 else return (int)n
->unsigned32BitValue();
348 int IOPMPowerSource::amperage(void) {
350 n
= OSDynamicCast(OSNumber
, properties
->getObject(amperageKey
));
352 else return (int)n
->unsigned32BitValue();
355 unsigned int IOPMPowerSource::voltage(void) {
357 n
= OSDynamicCast(OSNumber
, properties
->getObject(voltageKey
));
359 else return (unsigned int)n
->unsigned32BitValue();
362 unsigned int IOPMPowerSource::cycleCount(void) {
364 n
= OSDynamicCast(OSNumber
, properties
->getObject(cycleCountKey
));
366 else return (unsigned int)n
->unsigned32BitValue();
369 int IOPMPowerSource::adapterInfo(void) {
371 n
= OSDynamicCast(OSNumber
, properties
->getObject(adapterInfoKey
));
373 else return (int)n
->unsigned32BitValue();
376 int IOPMPowerSource::location(void) {
378 n
= OSDynamicCast(OSNumber
, properties
->getObject(locationKey
));
380 else return (unsigned int)n
->unsigned32BitValue();
383 OSSymbol
*IOPMPowerSource::errorCondition(void) {
384 return OSDynamicCast(OSSymbol
, properties
->getObject(errorConditionKey
));
387 OSSymbol
*IOPMPowerSource::manufacturer(void) {
388 return OSDynamicCast(OSSymbol
, properties
->getObject(manufacturerKey
));
391 OSSymbol
*IOPMPowerSource::model(void) {
392 return OSDynamicCast(OSSymbol
, properties
->getObject(modelKey
));
395 OSSymbol
*IOPMPowerSource::serial(void) {
396 return OSDynamicCast(OSSymbol
, properties
->getObject(serialKey
));
399 OSDictionary
*IOPMPowerSource::legacyIOBatteryInfo(void) {
400 return OSDynamicCast(OSDictionary
, properties
->getObject(batteryInfoKey
));