1c1dac77fSApple OSS Distributions /*
2e13b1fa5SApple OSS Distributions  * Copyright (c) 1998-2005 Apple Computer, Inc. All rights reserved.
3c1dac77fSApple OSS Distributions  *
4e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5c1dac77fSApple OSS Distributions  *
6e13b1fa5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14c1dac77fSApple OSS Distributions  *
15e13b1fa5SApple OSS Distributions  * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions  *
18e13b1fa5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20c1dac77fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21c1dac77fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions  * limitations under the License.
25c1dac77fSApple OSS Distributions  *
26e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27c1dac77fSApple OSS Distributions  */
28c1dac77fSApple OSS Distributions 
29c1dac77fSApple OSS Distributions #include <IOKit/pwr_mgt/IOPMPowerSource.h>
30e13b1fa5SApple OSS Distributions #include <IOKit/pwr_mgt/IOPM.h>
31e13b1fa5SApple OSS Distributions #include <IOKit/IOMessage.h>
32e13b1fa5SApple OSS Distributions #include <IOKit/IOLib.h>
33*e6231be0SApple OSS Distributions #include <os/log.h>
34c1dac77fSApple OSS Distributions 
35e13b1fa5SApple OSS Distributions #define super IOService
36c1dac77fSApple OSS Distributions 
OSDefineMetaClassAndStructors(IOPMPowerSource,IOService)37e13b1fa5SApple OSS Distributions OSDefineMetaClassAndStructors(IOPMPowerSource, IOService)
38c1dac77fSApple OSS Distributions 
39e13b1fa5SApple OSS Distributions // *****************************************************************************
40e13b1fa5SApple OSS Distributions // powerSource
41e13b1fa5SApple OSS Distributions //
42e13b1fa5SApple OSS Distributions // Static initializer for IOPMPowerSource. Returns a new instance of the class
43e13b1fa5SApple OSS Distributions // which the caller must attach to the power plane.
44e13b1fa5SApple OSS Distributions // *****************************************************************************
45e13b1fa5SApple OSS Distributions 
46e13b1fa5SApple OSS Distributions IOPMPowerSource *IOPMPowerSource::powerSource(void)
47e13b1fa5SApple OSS Distributions {
48e13b1fa5SApple OSS Distributions 	IOPMPowerSource *ps = new IOPMPowerSource;
49e13b1fa5SApple OSS Distributions 
50e13b1fa5SApple OSS Distributions 	if (ps) {
51e13b1fa5SApple OSS Distributions 		ps->init();
52e13b1fa5SApple OSS Distributions 		return ps;
53e13b1fa5SApple OSS Distributions 	}
54e13b1fa5SApple OSS Distributions 	return NULL;
55e13b1fa5SApple OSS Distributions }
56e13b1fa5SApple OSS Distributions 
57e13b1fa5SApple OSS Distributions // *****************************************************************************
58c1dac77fSApple OSS Distributions // init
59c1dac77fSApple OSS Distributions //
60e13b1fa5SApple OSS Distributions // *****************************************************************************
61a5e72196SApple OSS Distributions bool
init(void)62a5e72196SApple OSS Distributions IOPMPowerSource::init(void)
63c1dac77fSApple OSS Distributions {
64e13b1fa5SApple OSS Distributions 	if (!super::init()) {
65c1dac77fSApple OSS Distributions 		return false;
66e13b1fa5SApple OSS Distributions 	}
67c1dac77fSApple OSS Distributions 
68e13b1fa5SApple OSS Distributions 	nextInList = NULL;
69e13b1fa5SApple OSS Distributions 
70e13b1fa5SApple OSS Distributions 	properties = OSDictionary::withCapacity(10);
71a5e72196SApple OSS Distributions 	if (!properties) {
72a5e72196SApple OSS Distributions 		return false;
73a5e72196SApple OSS Distributions 	}
74e13b1fa5SApple OSS Distributions 	properties->setCapacityIncrement(1);
75e13b1fa5SApple OSS Distributions 
76e13b1fa5SApple OSS Distributions 	externalConnectedKey = OSSymbol::withCString(kIOPMPSExternalConnectedKey);
77e13b1fa5SApple OSS Distributions 	externalChargeCapableKey = OSSymbol::withCString(kIOPMPSExternalChargeCapableKey);
78e13b1fa5SApple OSS Distributions 	batteryInstalledKey = OSSymbol::withCString(kIOPMPSBatteryInstalledKey);
79e13b1fa5SApple OSS Distributions 	chargingKey = OSSymbol::withCString(kIOPMPSIsChargingKey);
80e13b1fa5SApple OSS Distributions 	warnLevelKey = OSSymbol::withCString(kIOPMPSAtWarnLevelKey);
81e13b1fa5SApple OSS Distributions 	criticalLevelKey = OSSymbol::withCString(kIOPMPSAtCriticalLevelKey);
82e13b1fa5SApple OSS Distributions 	currentCapacityKey = OSSymbol::withCString(kIOPMPSCurrentCapacityKey);
83e13b1fa5SApple OSS Distributions 	maxCapacityKey = OSSymbol::withCString(kIOPMPSMaxCapacityKey);
84e13b1fa5SApple OSS Distributions 	timeRemainingKey = OSSymbol::withCString(kIOPMPSTimeRemainingKey);
85e13b1fa5SApple OSS Distributions 	amperageKey = OSSymbol::withCString(kIOPMPSAmperageKey);
86e13b1fa5SApple OSS Distributions 	voltageKey = OSSymbol::withCString(kIOPMPSVoltageKey);
87e13b1fa5SApple OSS Distributions 	cycleCountKey = OSSymbol::withCString(kIOPMPSCycleCountKey);
88e13b1fa5SApple OSS Distributions 	adapterInfoKey = OSSymbol::withCString(kIOPMPSAdapterInfoKey);
89e13b1fa5SApple OSS Distributions 	locationKey = OSSymbol::withCString(kIOPMPSLocationKey);
90e13b1fa5SApple OSS Distributions 	errorConditionKey = OSSymbol::withCString(kIOPMPSErrorConditionKey);
91e13b1fa5SApple OSS Distributions 	manufacturerKey = OSSymbol::withCString(kIOPMPSManufacturerKey);
92e13b1fa5SApple OSS Distributions 	modelKey = OSSymbol::withCString(kIOPMPSModelKey);
93e13b1fa5SApple OSS Distributions 	serialKey = OSSymbol::withCString(kIOPMPSSerialKey);
94e13b1fa5SApple OSS Distributions 	batteryInfoKey = OSSymbol::withCString(kIOPMPSLegacyBatteryInfoKey);
95c1dac77fSApple OSS Distributions 
96c1dac77fSApple OSS Distributions 	return true;
97c1dac77fSApple OSS Distributions }
98c1dac77fSApple OSS Distributions 
99e13b1fa5SApple OSS Distributions // *****************************************************************************
100e13b1fa5SApple OSS Distributions // free
101c1dac77fSApple OSS Distributions //
102e13b1fa5SApple OSS Distributions // *****************************************************************************
103a5e72196SApple OSS Distributions void
free(void)104a5e72196SApple OSS Distributions IOPMPowerSource::free(void)
105c1dac77fSApple OSS Distributions {
106a5e72196SApple OSS Distributions 	if (properties) {
107a5e72196SApple OSS Distributions 		properties->release();
108a5e72196SApple OSS Distributions 	}
109a5e72196SApple OSS Distributions 	if (externalConnectedKey) {
110a5e72196SApple OSS Distributions 		externalConnectedKey->release();
111a5e72196SApple OSS Distributions 	}
112a5e72196SApple OSS Distributions 	if (externalChargeCapableKey) {
113a5e72196SApple OSS Distributions 		externalChargeCapableKey->release();
114a5e72196SApple OSS Distributions 	}
115a5e72196SApple OSS Distributions 	if (batteryInstalledKey) {
116a5e72196SApple OSS Distributions 		batteryInstalledKey->release();
117a5e72196SApple OSS Distributions 	}
118a5e72196SApple OSS Distributions 	if (chargingKey) {
119a5e72196SApple OSS Distributions 		chargingKey->release();
120a5e72196SApple OSS Distributions 	}
121a5e72196SApple OSS Distributions 	if (warnLevelKey) {
122a5e72196SApple OSS Distributions 		warnLevelKey->release();
123a5e72196SApple OSS Distributions 	}
124a5e72196SApple OSS Distributions 	if (criticalLevelKey) {
125a5e72196SApple OSS Distributions 		criticalLevelKey->release();
126a5e72196SApple OSS Distributions 	}
127a5e72196SApple OSS Distributions 	if (currentCapacityKey) {
128a5e72196SApple OSS Distributions 		currentCapacityKey->release();
129a5e72196SApple OSS Distributions 	}
130a5e72196SApple OSS Distributions 	if (maxCapacityKey) {
131a5e72196SApple OSS Distributions 		maxCapacityKey->release();
132a5e72196SApple OSS Distributions 	}
133a5e72196SApple OSS Distributions 	if (timeRemainingKey) {
134a5e72196SApple OSS Distributions 		timeRemainingKey->release();
135a5e72196SApple OSS Distributions 	}
136a5e72196SApple OSS Distributions 	if (amperageKey) {
137a5e72196SApple OSS Distributions 		amperageKey->release();
138a5e72196SApple OSS Distributions 	}
139a5e72196SApple OSS Distributions 	if (voltageKey) {
140a5e72196SApple OSS Distributions 		voltageKey->release();
141a5e72196SApple OSS Distributions 	}
142a5e72196SApple OSS Distributions 	if (cycleCountKey) {
143a5e72196SApple OSS Distributions 		cycleCountKey->release();
144a5e72196SApple OSS Distributions 	}
145a5e72196SApple OSS Distributions 	if (adapterInfoKey) {
146a5e72196SApple OSS Distributions 		adapterInfoKey->release();
147a5e72196SApple OSS Distributions 	}
148a5e72196SApple OSS Distributions 	if (errorConditionKey) {
149a5e72196SApple OSS Distributions 		errorConditionKey->release();
150a5e72196SApple OSS Distributions 	}
151a5e72196SApple OSS Distributions 	if (manufacturerKey) {
152a5e72196SApple OSS Distributions 		manufacturerKey->release();
153a5e72196SApple OSS Distributions 	}
154a5e72196SApple OSS Distributions 	if (modelKey) {
155a5e72196SApple OSS Distributions 		modelKey->release();
156a5e72196SApple OSS Distributions 	}
157a5e72196SApple OSS Distributions 	if (serialKey) {
158a5e72196SApple OSS Distributions 		serialKey->release();
159a5e72196SApple OSS Distributions 	}
160a5e72196SApple OSS Distributions 	if (locationKey) {
161a5e72196SApple OSS Distributions 		locationKey->release();
162a5e72196SApple OSS Distributions 	}
163a5e72196SApple OSS Distributions 	if (batteryInfoKey) {
164a5e72196SApple OSS Distributions 		batteryInfoKey->release();
165a5e72196SApple OSS Distributions 	}
166a5e72196SApple OSS Distributions 
167a5e72196SApple OSS Distributions 	super::free();
168c1dac77fSApple OSS Distributions }
169c1dac77fSApple OSS Distributions 
170e13b1fa5SApple OSS Distributions // *****************************************************************************
171c1dac77fSApple OSS Distributions // updateStatus
172c1dac77fSApple OSS Distributions //
173e13b1fa5SApple OSS Distributions // Update power source state in IORegistry and message interested clients
174e13b1fa5SApple OSS Distributions // notifying them of our change.
175e13b1fa5SApple OSS Distributions // *****************************************************************************
176a5e72196SApple OSS Distributions void
updateStatus(void)177a5e72196SApple OSS Distributions IOPMPowerSource::updateStatus(void)
178c1dac77fSApple OSS Distributions {
179e13b1fa5SApple OSS Distributions 	OSCollectionIterator            *iterator;
180e13b1fa5SApple OSS Distributions 	OSObject                        *iteratorKey;
181e13b1fa5SApple OSS Distributions 	OSObject                        *obj;
182c1dac77fSApple OSS Distributions 
183e13b1fa5SApple OSS Distributions 	// do nothing if settings haven't changed
184a5e72196SApple OSS Distributions 	if (!settingsChangedSinceUpdate) {
185a5e72196SApple OSS Distributions 		return;
186a5e72196SApple OSS Distributions 	}
187e13b1fa5SApple OSS Distributions 
188e13b1fa5SApple OSS Distributions 	iterator = OSCollectionIterator::withCollection(properties);
189a5e72196SApple OSS Distributions 	if (!iterator) {
190a5e72196SApple OSS Distributions 		return;
191a5e72196SApple OSS Distributions 	}
192e13b1fa5SApple OSS Distributions 
193e13b1fa5SApple OSS Distributions 	while ((iteratorKey = iterator->getNextObject())) {
194e13b1fa5SApple OSS Distributions 		OSSymbol *key;
195e13b1fa5SApple OSS Distributions 
196e13b1fa5SApple OSS Distributions 		key = OSDynamicCast(OSSymbol, iteratorKey);
197a5e72196SApple OSS Distributions 		if (!key) {
198a5e72196SApple OSS Distributions 			continue;
199a5e72196SApple OSS Distributions 		}
200e13b1fa5SApple OSS Distributions 		obj = properties->getObject(key);
201a5e72196SApple OSS Distributions 		if (!obj) {
202a5e72196SApple OSS Distributions 			continue;
203a5e72196SApple OSS Distributions 		}
204e13b1fa5SApple OSS Distributions 		setProperty(key, obj);
205e13b1fa5SApple OSS Distributions 	}
206e13b1fa5SApple OSS Distributions 	iterator->release();
207e13b1fa5SApple OSS Distributions 
208e13b1fa5SApple OSS Distributions 	settingsChangedSinceUpdate = false;
209e13b1fa5SApple OSS Distributions 
210e13b1fa5SApple OSS Distributions 	// And up goes the flare
211*e6231be0SApple OSS Distributions 	IOMessage notifyMessage = kIOPMMessageBatteryStatusHasChanged;
212*e6231be0SApple OSS Distributions #if DEVELOPMENT || DEBUG
213*e6231be0SApple OSS Distributions 	os_log(OS_LOG_DEFAULT, "notify clients '%u'\n", (unsigned int)notifyMessage);
214*e6231be0SApple OSS Distributions #endif
215*e6231be0SApple OSS Distributions 	messageClients(notifyMessage);
216e13b1fa5SApple OSS Distributions }
217e13b1fa5SApple OSS Distributions 
218e13b1fa5SApple OSS Distributions 
219e13b1fa5SApple OSS Distributions /*******************************************************************************
220e13b1fa5SApple OSS Distributions  *
221e13b1fa5SApple OSS Distributions  * PROTECTED Accessors. All the setters! Yay!
222e13b1fa5SApple OSS Distributions  *
223e13b1fa5SApple OSS Distributions  ******************************************************************************/
224e13b1fa5SApple OSS Distributions 
225a5e72196SApple OSS Distributions void
setPSProperty(const OSSymbol * key,OSObject * val)226a5e72196SApple OSS Distributions IOPMPowerSource::setPSProperty(const OSSymbol *key, OSObject *val)
227e13b1fa5SApple OSS Distributions {
228e13b1fa5SApple OSS Distributions 	OSObject    *lastVal;
229e13b1fa5SApple OSS Distributions 
230a5e72196SApple OSS Distributions 	if (!key || !val) {
231a5e72196SApple OSS Distributions 		return;
232a5e72196SApple OSS Distributions 	}
233e13b1fa5SApple OSS Distributions 
234e13b1fa5SApple OSS Distributions 	// Compare new setting with existing setting; update
235e13b1fa5SApple OSS Distributions 	// 'settingsChangedSinceUpdate' if the setting has changed.
236e13b1fa5SApple OSS Distributions 	// If values are OSNumbers, do equality comparison.
237e13b1fa5SApple OSS Distributions 	// Otherwise, just compare pointers.
238e13b1fa5SApple OSS Distributions 
239e13b1fa5SApple OSS Distributions 	if ((lastVal = properties->getObject(key))) {
240855239e5SApple OSS Distributions 		if (val->isEqualTo(lastVal)) {
241e13b1fa5SApple OSS Distributions 			// settings didn't change
242e13b1fa5SApple OSS Distributions 		} else {
243e13b1fa5SApple OSS Distributions 			// num val is not equal to last val
244e13b1fa5SApple OSS Distributions 			settingsChangedSinceUpdate = true;
245e13b1fa5SApple OSS Distributions 		}
246e13b1fa5SApple OSS Distributions 	} else {
247e13b1fa5SApple OSS Distributions 		// new setting; no last value
248e13b1fa5SApple OSS Distributions 		settingsChangedSinceUpdate = true;
249e13b1fa5SApple OSS Distributions 	}
250e13b1fa5SApple OSS Distributions 
251e13b1fa5SApple OSS Distributions 	// here's the part where we go crazy.
252e13b1fa5SApple OSS Distributions 	properties->setObject(key, val);
253e13b1fa5SApple OSS Distributions }
254e13b1fa5SApple OSS Distributions 
255e13b1fa5SApple OSS Distributions 
256e13b1fa5SApple OSS Distributions 
257a5e72196SApple OSS Distributions void
setExternalConnected(bool b)258a5e72196SApple OSS Distributions IOPMPowerSource::setExternalConnected(bool b)
259a5e72196SApple OSS Distributions {
260e13b1fa5SApple OSS Distributions 	setPSProperty(externalConnectedKey,
261e13b1fa5SApple OSS Distributions 	    b ? kOSBooleanTrue : kOSBooleanFalse);
262e13b1fa5SApple OSS Distributions }
263e13b1fa5SApple OSS Distributions 
264a5e72196SApple OSS Distributions void
setExternalChargeCapable(bool b)265a5e72196SApple OSS Distributions IOPMPowerSource::setExternalChargeCapable(bool b)
266a5e72196SApple OSS Distributions {
267e13b1fa5SApple OSS Distributions 	setPSProperty(externalChargeCapableKey,
268e13b1fa5SApple OSS Distributions 	    b ? kOSBooleanTrue : kOSBooleanFalse);
269e13b1fa5SApple OSS Distributions }
270e13b1fa5SApple OSS Distributions 
271a5e72196SApple OSS Distributions void
setBatteryInstalled(bool b)272a5e72196SApple OSS Distributions IOPMPowerSource::setBatteryInstalled(bool b)
273a5e72196SApple OSS Distributions {
274e13b1fa5SApple OSS Distributions 	setPSProperty(batteryInstalledKey,
275e13b1fa5SApple OSS Distributions 	    b ? kOSBooleanTrue : kOSBooleanFalse);
276e13b1fa5SApple OSS Distributions }
277e13b1fa5SApple OSS Distributions 
278a5e72196SApple OSS Distributions void
setIsCharging(bool b)279a5e72196SApple OSS Distributions IOPMPowerSource::setIsCharging(bool b)
280a5e72196SApple OSS Distributions {
281e13b1fa5SApple OSS Distributions 	setPSProperty(chargingKey,
282e13b1fa5SApple OSS Distributions 	    b ? kOSBooleanTrue : kOSBooleanFalse);
283e13b1fa5SApple OSS Distributions }
284e13b1fa5SApple OSS Distributions 
285a5e72196SApple OSS Distributions void
setAtWarnLevel(bool b)286a5e72196SApple OSS Distributions IOPMPowerSource::setAtWarnLevel(bool b)
287a5e72196SApple OSS Distributions {
288e13b1fa5SApple OSS Distributions 	setPSProperty(warnLevelKey,
289e13b1fa5SApple OSS Distributions 	    b ? kOSBooleanTrue : kOSBooleanFalse);
290e13b1fa5SApple OSS Distributions }
291e13b1fa5SApple OSS Distributions 
292a5e72196SApple OSS Distributions void
setAtCriticalLevel(bool b)293a5e72196SApple OSS Distributions IOPMPowerSource::setAtCriticalLevel(bool b)
294a5e72196SApple OSS Distributions {
295e13b1fa5SApple OSS Distributions 	setPSProperty(criticalLevelKey,
296e13b1fa5SApple OSS Distributions 	    b ? kOSBooleanTrue : kOSBooleanFalse);
297e13b1fa5SApple OSS Distributions }
298e13b1fa5SApple OSS Distributions 
299e13b1fa5SApple OSS Distributions 
300a5e72196SApple OSS Distributions void
setCurrentCapacity(unsigned int val)301a5e72196SApple OSS Distributions IOPMPowerSource::setCurrentCapacity(unsigned int val)
302a5e72196SApple OSS Distributions {
303e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
304e13b1fa5SApple OSS Distributions 	setPSProperty(currentCapacityKey, n);
305e13b1fa5SApple OSS Distributions 	n->release();
306e13b1fa5SApple OSS Distributions }
307e13b1fa5SApple OSS Distributions 
308a5e72196SApple OSS Distributions void
setMaxCapacity(unsigned int val)309a5e72196SApple OSS Distributions IOPMPowerSource::setMaxCapacity(unsigned int val)
310a5e72196SApple OSS Distributions {
311e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
312e13b1fa5SApple OSS Distributions 	setPSProperty(maxCapacityKey, n);
313e13b1fa5SApple OSS Distributions 	n->release();
314e13b1fa5SApple OSS Distributions }
315e13b1fa5SApple OSS Distributions 
316a5e72196SApple OSS Distributions void
setTimeRemaining(int val)317a5e72196SApple OSS Distributions IOPMPowerSource::setTimeRemaining(int val)
318a5e72196SApple OSS Distributions {
319e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
320e13b1fa5SApple OSS Distributions 	setPSProperty(timeRemainingKey, n);
321e13b1fa5SApple OSS Distributions 	n->release();
322e13b1fa5SApple OSS Distributions }
323e13b1fa5SApple OSS Distributions 
324a5e72196SApple OSS Distributions void
setAmperage(int val)325a5e72196SApple OSS Distributions IOPMPowerSource::setAmperage(int val)
326a5e72196SApple OSS Distributions {
327e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
328e13b1fa5SApple OSS Distributions 	setPSProperty(amperageKey, n);
329e13b1fa5SApple OSS Distributions 	n->release();
330e13b1fa5SApple OSS Distributions }
331e13b1fa5SApple OSS Distributions 
332a5e72196SApple OSS Distributions void
setVoltage(unsigned int val)333a5e72196SApple OSS Distributions IOPMPowerSource::setVoltage(unsigned int val)
334a5e72196SApple OSS Distributions {
335e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
336e13b1fa5SApple OSS Distributions 	setPSProperty(voltageKey, n);
337e13b1fa5SApple OSS Distributions 	n->release();
338e13b1fa5SApple OSS Distributions }
339e13b1fa5SApple OSS Distributions 
340a5e72196SApple OSS Distributions void
setCycleCount(unsigned int val)341a5e72196SApple OSS Distributions IOPMPowerSource::setCycleCount(unsigned int val)
342a5e72196SApple OSS Distributions {
343e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
344e13b1fa5SApple OSS Distributions 	setPSProperty(cycleCountKey, n);
345e13b1fa5SApple OSS Distributions 	n->release();
346e13b1fa5SApple OSS Distributions }
347e13b1fa5SApple OSS Distributions 
348a5e72196SApple OSS Distributions void
setAdapterInfo(int val)349a5e72196SApple OSS Distributions IOPMPowerSource::setAdapterInfo(int val)
350a5e72196SApple OSS Distributions {
351e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
352e13b1fa5SApple OSS Distributions 	setPSProperty(adapterInfoKey, n);
353e13b1fa5SApple OSS Distributions 	n->release();
354e13b1fa5SApple OSS Distributions }
355e13b1fa5SApple OSS Distributions 
356a5e72196SApple OSS Distributions void
setLocation(int val)357a5e72196SApple OSS Distributions IOPMPowerSource::setLocation(int val)
358a5e72196SApple OSS Distributions {
359e13b1fa5SApple OSS Distributions 	OSNumber *n = OSNumber::withNumber(val, 32);
360e13b1fa5SApple OSS Distributions 	setPSProperty(locationKey, n);
361e13b1fa5SApple OSS Distributions 	n->release();
362e13b1fa5SApple OSS Distributions }
363e13b1fa5SApple OSS Distributions 
364a5e72196SApple OSS Distributions void
setErrorCondition(OSSymbol * s)365a5e72196SApple OSS Distributions IOPMPowerSource::setErrorCondition(OSSymbol *s)
366a5e72196SApple OSS Distributions {
367e13b1fa5SApple OSS Distributions 	setPSProperty(errorConditionKey, s);
368e13b1fa5SApple OSS Distributions }
369e13b1fa5SApple OSS Distributions 
370a5e72196SApple OSS Distributions void
setManufacturer(OSSymbol * s)371a5e72196SApple OSS Distributions IOPMPowerSource::setManufacturer(OSSymbol *s)
372a5e72196SApple OSS Distributions {
373e13b1fa5SApple OSS Distributions 	setPSProperty(manufacturerKey, s);
374e13b1fa5SApple OSS Distributions }
375e13b1fa5SApple OSS Distributions 
376a5e72196SApple OSS Distributions void
setModel(OSSymbol * s)377a5e72196SApple OSS Distributions IOPMPowerSource::setModel(OSSymbol *s)
378a5e72196SApple OSS Distributions {
379e13b1fa5SApple OSS Distributions 	setPSProperty(modelKey, s);
380e13b1fa5SApple OSS Distributions }
381e13b1fa5SApple OSS Distributions 
382a5e72196SApple OSS Distributions void
setSerial(OSSymbol * s)383a5e72196SApple OSS Distributions IOPMPowerSource::setSerial(OSSymbol *s)
384a5e72196SApple OSS Distributions {
385e13b1fa5SApple OSS Distributions 	setPSProperty(serialKey, s);
386e13b1fa5SApple OSS Distributions }
387e13b1fa5SApple OSS Distributions 
388a5e72196SApple OSS Distributions void
setLegacyIOBatteryInfo(OSDictionary * d)389a5e72196SApple OSS Distributions IOPMPowerSource::setLegacyIOBatteryInfo(OSDictionary *d)
390a5e72196SApple OSS Distributions {
391e13b1fa5SApple OSS Distributions 	setPSProperty(batteryInfoKey, d);
392c1dac77fSApple OSS Distributions }
393c1dac77fSApple OSS Distributions 
394c1dac77fSApple OSS Distributions 
395c1dac77fSApple OSS Distributions 
396c1dac77fSApple OSS Distributions 
397e13b1fa5SApple OSS Distributions /*******************************************************************************
398e13b1fa5SApple OSS Distributions  *
399e13b1fa5SApple OSS Distributions  * PUBLIC Accessors. All the getters! Boo!
400e13b1fa5SApple OSS Distributions  *
401e13b1fa5SApple OSS Distributions  ******************************************************************************/
402c1dac77fSApple OSS Distributions 
403a5e72196SApple OSS Distributions OSObject *
getPSProperty(const OSSymbol * symmie)404a5e72196SApple OSS Distributions IOPMPowerSource::getPSProperty(const OSSymbol *symmie)
405a5e72196SApple OSS Distributions {
406a5e72196SApple OSS Distributions 	if (!symmie) {
407a5e72196SApple OSS Distributions 		return NULL;
408a5e72196SApple OSS Distributions 	}
409e13b1fa5SApple OSS Distributions 	return properties->getObject(symmie);
410e13b1fa5SApple OSS Distributions }
411e13b1fa5SApple OSS Distributions 
412a5e72196SApple OSS Distributions bool
externalConnected(void)413a5e72196SApple OSS Distributions IOPMPowerSource::externalConnected(void)
414a5e72196SApple OSS Distributions {
415a5e72196SApple OSS Distributions 	return kOSBooleanTrue == properties->getObject(externalConnectedKey);
416e13b1fa5SApple OSS Distributions }
417e13b1fa5SApple OSS Distributions 
418a5e72196SApple OSS Distributions bool
externalChargeCapable(void)419a5e72196SApple OSS Distributions IOPMPowerSource::externalChargeCapable(void)
420a5e72196SApple OSS Distributions {
421a5e72196SApple OSS Distributions 	return kOSBooleanTrue == properties->getObject(externalChargeCapableKey);
422e13b1fa5SApple OSS Distributions }
423e13b1fa5SApple OSS Distributions 
424a5e72196SApple OSS Distributions bool
batteryInstalled(void)425a5e72196SApple OSS Distributions IOPMPowerSource::batteryInstalled(void)
426a5e72196SApple OSS Distributions {
427a5e72196SApple OSS Distributions 	return kOSBooleanTrue == properties->getObject(batteryInstalledKey);
428e13b1fa5SApple OSS Distributions }
429e13b1fa5SApple OSS Distributions 
430a5e72196SApple OSS Distributions bool
isCharging(void)431a5e72196SApple OSS Distributions IOPMPowerSource::isCharging(void)
432a5e72196SApple OSS Distributions {
433a5e72196SApple OSS Distributions 	return kOSBooleanTrue == properties->getObject(chargingKey);
434e13b1fa5SApple OSS Distributions }
435e13b1fa5SApple OSS Distributions 
436a5e72196SApple OSS Distributions bool
atWarnLevel(void)437a5e72196SApple OSS Distributions IOPMPowerSource::atWarnLevel(void)
438a5e72196SApple OSS Distributions {
439a5e72196SApple OSS Distributions 	return kOSBooleanTrue == properties->getObject(warnLevelKey);
440e13b1fa5SApple OSS Distributions }
441e13b1fa5SApple OSS Distributions 
442a5e72196SApple OSS Distributions bool
atCriticalLevel(void)443a5e72196SApple OSS Distributions IOPMPowerSource::atCriticalLevel(void)
444a5e72196SApple OSS Distributions {
445a5e72196SApple OSS Distributions 	return kOSBooleanTrue == properties->getObject(criticalLevelKey);
446e13b1fa5SApple OSS Distributions }
447e13b1fa5SApple OSS Distributions 
448a5e72196SApple OSS Distributions unsigned int
currentCapacity(void)449a5e72196SApple OSS Distributions IOPMPowerSource::currentCapacity(void)
450a5e72196SApple OSS Distributions {
451e13b1fa5SApple OSS Distributions 	OSNumber        *n;
452e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(currentCapacityKey));
453a5e72196SApple OSS Distributions 	if (!n) {
454a5e72196SApple OSS Distributions 		return 0;
455a5e72196SApple OSS Distributions 	} else {
456a5e72196SApple OSS Distributions 		return (unsigned int)n->unsigned32BitValue();
457a5e72196SApple OSS Distributions 	}
458e13b1fa5SApple OSS Distributions }
459e13b1fa5SApple OSS Distributions 
460a5e72196SApple OSS Distributions unsigned int
maxCapacity(void)461a5e72196SApple OSS Distributions IOPMPowerSource::maxCapacity(void)
462a5e72196SApple OSS Distributions {
463e13b1fa5SApple OSS Distributions 	OSNumber        *n;
464e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(maxCapacityKey));
465a5e72196SApple OSS Distributions 	if (!n) {
466a5e72196SApple OSS Distributions 		return 0;
467a5e72196SApple OSS Distributions 	} else {
468a5e72196SApple OSS Distributions 		return (unsigned int)n->unsigned32BitValue();
469a5e72196SApple OSS Distributions 	}
470e13b1fa5SApple OSS Distributions }
471e13b1fa5SApple OSS Distributions 
472a5e72196SApple OSS Distributions unsigned int
capacityPercentRemaining(void)473a5e72196SApple OSS Distributions IOPMPowerSource::capacityPercentRemaining(void)
474e13b1fa5SApple OSS Distributions {
475e13b1fa5SApple OSS Distributions 	unsigned int _currentCapacity = currentCapacity();
476e13b1fa5SApple OSS Distributions 	unsigned int _maxCapacity = maxCapacity();
477e13b1fa5SApple OSS Distributions 	if (0 == _maxCapacity) {
478e13b1fa5SApple OSS Distributions 		return 0;
479e13b1fa5SApple OSS Distributions 	} else {
480a5e72196SApple OSS Distributions 		return (100 * _currentCapacity) / _maxCapacity;
481e13b1fa5SApple OSS Distributions 	}
482e13b1fa5SApple OSS Distributions }
483e13b1fa5SApple OSS Distributions 
484a5e72196SApple OSS Distributions int
timeRemaining(void)485a5e72196SApple OSS Distributions IOPMPowerSource::timeRemaining(void)
486a5e72196SApple OSS Distributions {
487e13b1fa5SApple OSS Distributions 	OSNumber        *n;
488e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(timeRemainingKey));
489a5e72196SApple OSS Distributions 	if (!n) {
490a5e72196SApple OSS Distributions 		return 0;
491a5e72196SApple OSS Distributions 	} else {
492a5e72196SApple OSS Distributions 		return (int)n->unsigned32BitValue();
493a5e72196SApple OSS Distributions 	}
494e13b1fa5SApple OSS Distributions }
495e13b1fa5SApple OSS Distributions 
496a5e72196SApple OSS Distributions int
amperage(void)497a5e72196SApple OSS Distributions IOPMPowerSource::amperage(void)
498a5e72196SApple OSS Distributions {
499e13b1fa5SApple OSS Distributions 	OSNumber        *n;
500e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(amperageKey));
501a5e72196SApple OSS Distributions 	if (!n) {
502a5e72196SApple OSS Distributions 		return 0;
503a5e72196SApple OSS Distributions 	} else {
504a5e72196SApple OSS Distributions 		return (int)n->unsigned32BitValue();
505a5e72196SApple OSS Distributions 	}
506e13b1fa5SApple OSS Distributions }
507e13b1fa5SApple OSS Distributions 
508a5e72196SApple OSS Distributions unsigned int
voltage(void)509a5e72196SApple OSS Distributions IOPMPowerSource::voltage(void)
510a5e72196SApple OSS Distributions {
511e13b1fa5SApple OSS Distributions 	OSNumber        *n;
512e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(voltageKey));
513a5e72196SApple OSS Distributions 	if (!n) {
514a5e72196SApple OSS Distributions 		return 0;
515a5e72196SApple OSS Distributions 	} else {
516a5e72196SApple OSS Distributions 		return (unsigned int)n->unsigned32BitValue();
517a5e72196SApple OSS Distributions 	}
518e13b1fa5SApple OSS Distributions }
519e13b1fa5SApple OSS Distributions 
520a5e72196SApple OSS Distributions unsigned int
cycleCount(void)521a5e72196SApple OSS Distributions IOPMPowerSource::cycleCount(void)
522a5e72196SApple OSS Distributions {
523e13b1fa5SApple OSS Distributions 	OSNumber        *n;
524e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(cycleCountKey));
525a5e72196SApple OSS Distributions 	if (!n) {
526a5e72196SApple OSS Distributions 		return 0;
527a5e72196SApple OSS Distributions 	} else {
528a5e72196SApple OSS Distributions 		return (unsigned int)n->unsigned32BitValue();
529a5e72196SApple OSS Distributions 	}
530e13b1fa5SApple OSS Distributions }
531e13b1fa5SApple OSS Distributions 
532a5e72196SApple OSS Distributions int
adapterInfo(void)533a5e72196SApple OSS Distributions IOPMPowerSource::adapterInfo(void)
534a5e72196SApple OSS Distributions {
535e13b1fa5SApple OSS Distributions 	OSNumber        *n;
536e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(adapterInfoKey));
537a5e72196SApple OSS Distributions 	if (!n) {
538a5e72196SApple OSS Distributions 		return 0;
539a5e72196SApple OSS Distributions 	} else {
540a5e72196SApple OSS Distributions 		return (int)n->unsigned32BitValue();
541a5e72196SApple OSS Distributions 	}
542e13b1fa5SApple OSS Distributions }
543e13b1fa5SApple OSS Distributions 
544a5e72196SApple OSS Distributions int
location(void)545a5e72196SApple OSS Distributions IOPMPowerSource::location(void)
546a5e72196SApple OSS Distributions {
547e13b1fa5SApple OSS Distributions 	OSNumber        *n;
548e13b1fa5SApple OSS Distributions 	n = OSDynamicCast(OSNumber, properties->getObject(locationKey));
549a5e72196SApple OSS Distributions 	if (!n) {
550a5e72196SApple OSS Distributions 		return 0;
551a5e72196SApple OSS Distributions 	} else {
552a5e72196SApple OSS Distributions 		return (unsigned int)n->unsigned32BitValue();
553a5e72196SApple OSS Distributions 	}
554e13b1fa5SApple OSS Distributions }
555e13b1fa5SApple OSS Distributions 
556a5e72196SApple OSS Distributions OSSymbol *
errorCondition(void)557a5e72196SApple OSS Distributions IOPMPowerSource::errorCondition(void)
558a5e72196SApple OSS Distributions {
559e13b1fa5SApple OSS Distributions 	return OSDynamicCast(OSSymbol, properties->getObject(errorConditionKey));
560e13b1fa5SApple OSS Distributions }
561e13b1fa5SApple OSS Distributions 
562a5e72196SApple OSS Distributions OSSymbol *
manufacturer(void)563a5e72196SApple OSS Distributions IOPMPowerSource::manufacturer(void)
564a5e72196SApple OSS Distributions {
565e13b1fa5SApple OSS Distributions 	return OSDynamicCast(OSSymbol, properties->getObject(manufacturerKey));
566e13b1fa5SApple OSS Distributions }
567e13b1fa5SApple OSS Distributions 
568a5e72196SApple OSS Distributions OSSymbol *
model(void)569a5e72196SApple OSS Distributions IOPMPowerSource::model(void)
570a5e72196SApple OSS Distributions {
571e13b1fa5SApple OSS Distributions 	return OSDynamicCast(OSSymbol, properties->getObject(modelKey));
572e13b1fa5SApple OSS Distributions }
573e13b1fa5SApple OSS Distributions 
574a5e72196SApple OSS Distributions OSSymbol *
serial(void)575a5e72196SApple OSS Distributions IOPMPowerSource::serial(void)
576a5e72196SApple OSS Distributions {
577e13b1fa5SApple OSS Distributions 	return OSDynamicCast(OSSymbol, properties->getObject(serialKey));
578e13b1fa5SApple OSS Distributions }
579e13b1fa5SApple OSS Distributions 
580a5e72196SApple OSS Distributions OSDictionary *
legacyIOBatteryInfo(void)581a5e72196SApple OSS Distributions IOPMPowerSource::legacyIOBatteryInfo(void)
582a5e72196SApple OSS Distributions {
583e13b1fa5SApple OSS Distributions 	return OSDynamicCast(OSDictionary, properties->getObject(batteryInfoKey));
584e13b1fa5SApple OSS Distributions }
585