]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOPMPowerSource.cpp
xnu-792.13.8.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPMPowerSource.cpp
CommitLineData
1c79356b 1/*
5d5c5d0d 2 * Copyright (c) 1998-2005 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30
31#include <IOKit/pwr_mgt/IOPMPowerSource.h>
5d5c5d0d
A
32#include <IOKit/pwr_mgt/IOPM.h>
33#include <IOKit/IOMessage.h>
34#include <IOKit/IOLib.h>
1c79356b 35
5d5c5d0d 36#define super IOService
1c79356b 37
5d5c5d0d 38OSDefineMetaClassAndStructors(IOPMPowerSource, IOService)
1c79356b 39
5d5c5d0d
A
40// *****************************************************************************
41// powerSource
1c79356b 42//
5d5c5d0d
A
43// Static initializer for IOPMPowerSource. Returns a new instance of the class
44// which the caller must attach to the power plane.
45// *****************************************************************************
1c79356b 46
5d5c5d0d
A
47IOPMPowerSource *IOPMPowerSource::powerSource(void)
48{
49 IOPMPowerSource *ps = new IOPMPowerSource;
8ad349bb 50
5d5c5d0d
A
51 if(ps) {
52 ps->init();
53 return ps;
54 }
55 return NULL;
1c79356b
A
56}
57
5d5c5d0d
A
58// *****************************************************************************
59// init
1c79356b 60//
5d5c5d0d
A
61// *****************************************************************************
62bool IOPMPowerSource::init (void)
1c79356b 63{
5d5c5d0d
A
64 if (!super::init()) {
65 return false;
66 }
c0fea474 67
5d5c5d0d
A
68 nextInList = NULL;
69
70 properties = OSDictionary::withCapacity(10);
71 if(!properties) return false;
72 properties->setCapacityIncrement(1);
c0fea474 73
5d5c5d0d
A
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);
93
94 return true;
1c79356b
A
95}
96
5d5c5d0d
A
97// *****************************************************************************
98// free
1c79356b 99//
5d5c5d0d
A
100// *****************************************************************************
101void IOPMPowerSource::free(void)
1c79356b 102{
5d5c5d0d
A
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();
1c79356b
A
123}
124
5d5c5d0d
A
125// *****************************************************************************
126// updateStatus
1c79356b 127//
5d5c5d0d
A
128// Update power source state in IORegistry and message interested clients
129// notifying them of our change.
130// *****************************************************************************
131void IOPMPowerSource::updateStatus (void)
1c79356b 132{
5d5c5d0d
A
133 OSCollectionIterator *iterator;
134 OSObject *iteratorKey;
135 OSObject *obj;
136
137 iterator = OSCollectionIterator::withCollection(properties);
138 if(!iterator) return;
139
140 while ((iteratorKey = iterator->getNextObject())) {
141 OSSymbol *key;
142
143 key = OSDynamicCast(OSSymbol, iteratorKey);
144 if (!key) continue;
145 obj = properties->getObject(key);
146 if(!obj) continue;
147 setProperty(key, obj);
148 }
149 iterator->release();
150
151 // And up goes the flare
152 messageClients(kIOPMMessageBatteryStatusHasChanged);
c0fea474
A
153}
154
5d5c5d0d
A
155
156/*******************************************************************************
157 *
158 * PROTECTED Accessors. All the setters! Yay!
159 *
160 ******************************************************************************/
161
162void IOPMPowerSource::setExternalConnected(bool b) {
163 properties->setObject(
164 externalConnectedKey,
165 b ? kOSBooleanTrue:kOSBooleanFalse);
c0fea474
A
166}
167
5d5c5d0d
A
168void IOPMPowerSource::setExternalChargeCapable(bool b) {
169 properties->setObject(
170 externalChargeCapableKey,
171 b ? kOSBooleanTrue:kOSBooleanFalse);
c0fea474
A
172}
173
5d5c5d0d
A
174void IOPMPowerSource::setBatteryInstalled(bool b) {
175 properties->setObject(
176 batteryInstalledKey,
177 b ? kOSBooleanTrue:kOSBooleanFalse);
c0fea474
A
178}
179
5d5c5d0d
A
180void IOPMPowerSource::setIsCharging(bool b) {
181 properties->setObject(
182 chargingKey,
183 b ? kOSBooleanTrue:kOSBooleanFalse);
c0fea474
A
184}
185
5d5c5d0d
A
186void IOPMPowerSource::setAtWarnLevel(bool b) {
187 properties->setObject(
188 warnLevelKey,
189 b ? kOSBooleanTrue:kOSBooleanFalse);
c0fea474
A
190}
191
5d5c5d0d
A
192void IOPMPowerSource::setAtCriticalLevel(bool b) {
193 properties->setObject(
194 criticalLevelKey,
195 b ? kOSBooleanTrue:kOSBooleanFalse);
c0fea474 196}
1c79356b 197
5d5c5d0d
A
198
199void IOPMPowerSource::setCurrentCapacity(unsigned int val) {
200 OSNumber *n = OSNumber::withNumber(val, 32);
201 properties->setObject(
202 currentCapacityKey,
203 n);
204 n->release();
1c79356b
A
205}
206
5d5c5d0d
A
207void IOPMPowerSource::setMaxCapacity(unsigned int val) {
208 OSNumber *n = OSNumber::withNumber(val, 32);
209 properties->setObject(
210 maxCapacityKey,
211 n);
212 n->release();
213}
1c79356b 214
5d5c5d0d
A
215void IOPMPowerSource::setTimeRemaining(int val) {
216 OSNumber *n = OSNumber::withNumber(val, 32);
217 properties->setObject(
218 timeRemainingKey,
219 n);
220 n->release();
c0fea474 221}
1c79356b 222
5d5c5d0d
A
223void IOPMPowerSource::setAmperage(int val) {
224 OSNumber *n = OSNumber::withNumber(val, 32);
225 properties->setObject(
226 amperageKey,
227 n);
228 n->release();
229}
1c79356b 230
5d5c5d0d
A
231void IOPMPowerSource::setVoltage(unsigned int val) {
232 OSNumber *n = OSNumber::withNumber(val, 32);
233 properties->setObject(
234 voltageKey,
235 n);
236 n->release();
237}
238
239void IOPMPowerSource::setCycleCount(unsigned int val) {
240 OSNumber *n = OSNumber::withNumber(val, 32);
241 properties->setObject(
242 cycleCountKey,
243 n);
244 n->release();
245}
246
247void IOPMPowerSource::setAdapterInfo(int val) {
248 OSNumber *n = OSNumber::withNumber(val, 32);
249 properties->setObject(
250 adapterInfoKey,
251 n);
252 n->release();
253}
254
255void IOPMPowerSource::setLocation(int val) {
256 OSNumber *n = OSNumber::withNumber(val, 32);
257 properties->setObject(
258 locationKey,
259 n);
260 n->release();
261}
262
263void IOPMPowerSource::setErrorCondition(OSSymbol *s) {
264 properties->setObject(errorConditionKey, s);
265}
266
267void IOPMPowerSource::setManufacturer(OSSymbol *s) {
268 properties->setObject(manufacturerKey, s);
269}
270
271void IOPMPowerSource::setModel(OSSymbol *s) {
272 properties->setObject(modelKey, s);
273}
274
275void IOPMPowerSource::setSerial(OSSymbol *s) {
276 properties->setObject(serialKey, s);
277}
278
279void IOPMPowerSource::setLegacyIOBatteryInfo(OSDictionary *d) {
280 properties->setObject(batteryInfoKey, d);
281}
282
283
284
285
286/*******************************************************************************
287 *
288 * PUBLIC Accessors. All the getters! Boo!
289 *
290 ******************************************************************************/
291
292bool IOPMPowerSource::externalConnected(void) {
293 return (kOSBooleanTrue == properties->getObject(externalConnectedKey));
294}
295
296bool IOPMPowerSource::externalChargeCapable(void) {
297 return (kOSBooleanTrue == properties->getObject(externalChargeCapableKey));
298}
299
300bool IOPMPowerSource::batteryInstalled(void) {
301 return (kOSBooleanTrue == properties->getObject(batteryInstalledKey));
302}
303
304bool IOPMPowerSource::isCharging(void) {
305 return (kOSBooleanTrue == properties->getObject(chargingKey));
306}
307
308bool IOPMPowerSource::atWarnLevel(void) {
309 return (kOSBooleanTrue == properties->getObject(warnLevelKey));
310}
311
312bool IOPMPowerSource::atCriticalLevel(void) {
313 return (kOSBooleanTrue == properties->getObject(criticalLevelKey));
314}
315
316unsigned int IOPMPowerSource::currentCapacity(void) {
317 OSNumber *n;
318 n = OSDynamicCast(OSNumber, properties->getObject(currentCapacityKey));
319 if(!n) return 0;
320 else return (unsigned int)n->unsigned32BitValue();
321}
322
323unsigned int IOPMPowerSource::maxCapacity(void) {
324 OSNumber *n;
325 n = OSDynamicCast(OSNumber, properties->getObject(maxCapacityKey));
326 if(!n) return 0;
327 else return (unsigned int)n->unsigned32BitValue();
328}
329
330unsigned int IOPMPowerSource::capacityPercentRemaining(void)
8ad349bb 331{
5d5c5d0d
A
332 unsigned int _currentCapacity = currentCapacity();
333 unsigned int _maxCapacity = maxCapacity();
334 if(0 == _maxCapacity) {
335 return 0;
336 } else {
337 return ((100*_currentCapacity) / _maxCapacity);
338 }
339}
1c79356b 340
5d5c5d0d
A
341int IOPMPowerSource::timeRemaining(void) {
342 OSNumber *n;
343 n = OSDynamicCast(OSNumber, properties->getObject(timeRemainingKey));
344 if(!n) return 0;
345 else return (int)n->unsigned32BitValue();
c0fea474
A
346}
347
5d5c5d0d
A
348int IOPMPowerSource::amperage(void) {
349 OSNumber *n;
350 n = OSDynamicCast(OSNumber, properties->getObject(amperageKey));
351 if(!n) return 0;
352 else return (int)n->unsigned32BitValue();
353}
c0fea474 354
5d5c5d0d
A
355unsigned int IOPMPowerSource::voltage(void) {
356 OSNumber *n;
357 n = OSDynamicCast(OSNumber, properties->getObject(voltageKey));
358 if(!n) return 0;
359 else return (unsigned int)n->unsigned32BitValue();
360}
c0fea474 361
5d5c5d0d
A
362unsigned int IOPMPowerSource::cycleCount(void) {
363 OSNumber *n;
364 n = OSDynamicCast(OSNumber, properties->getObject(cycleCountKey));
365 if(!n) return 0;
366 else return (unsigned int)n->unsigned32BitValue();
367}
c0fea474 368
5d5c5d0d
A
369int IOPMPowerSource::adapterInfo(void) {
370 OSNumber *n;
371 n = OSDynamicCast(OSNumber, properties->getObject(adapterInfoKey));
372 if(!n) return 0;
373 else return (int)n->unsigned32BitValue();
374}
c0fea474 375
5d5c5d0d
A
376int IOPMPowerSource::location(void) {
377 OSNumber *n;
378 n = OSDynamicCast(OSNumber, properties->getObject(locationKey));
379 if(!n) return 0;
380 else return (unsigned int)n->unsigned32BitValue();
381}
382
383OSSymbol *IOPMPowerSource::errorCondition(void) {
384 return OSDynamicCast(OSSymbol, properties->getObject(errorConditionKey));
385}
386
387OSSymbol *IOPMPowerSource::manufacturer(void) {
388 return OSDynamicCast(OSSymbol, properties->getObject(manufacturerKey));
389}
390
391OSSymbol *IOPMPowerSource::model(void) {
392 return OSDynamicCast(OSSymbol, properties->getObject(modelKey));
393}
394
395OSSymbol *IOPMPowerSource::serial(void) {
396 return OSDynamicCast(OSSymbol, properties->getObject(serialKey));
397}
398
399OSDictionary *IOPMPowerSource::legacyIOBatteryInfo(void) {
400 return OSDynamicCast(OSDictionary, properties->getObject(batteryInfoKey));
401}