]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOPMPowerSource.cpp
xnu-792.13.8.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPMPowerSource.cpp
1 /*
2 * Copyright (c) 1998-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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@
29 */
30
31 #include <IOKit/pwr_mgt/IOPMPowerSource.h>
32 #include <IOKit/pwr_mgt/IOPM.h>
33 #include <IOKit/IOMessage.h>
34 #include <IOKit/IOLib.h>
35
36 #define super IOService
37
38 OSDefineMetaClassAndStructors(IOPMPowerSource, IOService)
39
40 // *****************************************************************************
41 // powerSource
42 //
43 // Static initializer for IOPMPowerSource. Returns a new instance of the class
44 // which the caller must attach to the power plane.
45 // *****************************************************************************
46
47 IOPMPowerSource *IOPMPowerSource::powerSource(void)
48 {
49 IOPMPowerSource *ps = new IOPMPowerSource;
50
51 if(ps) {
52 ps->init();
53 return ps;
54 }
55 return NULL;
56 }
57
58 // *****************************************************************************
59 // init
60 //
61 // *****************************************************************************
62 bool IOPMPowerSource::init (void)
63 {
64 if (!super::init()) {
65 return false;
66 }
67
68 nextInList = NULL;
69
70 properties = OSDictionary::withCapacity(10);
71 if(!properties) return false;
72 properties->setCapacityIncrement(1);
73
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;
95 }
96
97 // *****************************************************************************
98 // free
99 //
100 // *****************************************************************************
101 void IOPMPowerSource::free(void)
102 {
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();
123 }
124
125 // *****************************************************************************
126 // updateStatus
127 //
128 // Update power source state in IORegistry and message interested clients
129 // notifying them of our change.
130 // *****************************************************************************
131 void IOPMPowerSource::updateStatus (void)
132 {
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);
153 }
154
155
156 /*******************************************************************************
157 *
158 * PROTECTED Accessors. All the setters! Yay!
159 *
160 ******************************************************************************/
161
162 void IOPMPowerSource::setExternalConnected(bool b) {
163 properties->setObject(
164 externalConnectedKey,
165 b ? kOSBooleanTrue:kOSBooleanFalse);
166 }
167
168 void IOPMPowerSource::setExternalChargeCapable(bool b) {
169 properties->setObject(
170 externalChargeCapableKey,
171 b ? kOSBooleanTrue:kOSBooleanFalse);
172 }
173
174 void IOPMPowerSource::setBatteryInstalled(bool b) {
175 properties->setObject(
176 batteryInstalledKey,
177 b ? kOSBooleanTrue:kOSBooleanFalse);
178 }
179
180 void IOPMPowerSource::setIsCharging(bool b) {
181 properties->setObject(
182 chargingKey,
183 b ? kOSBooleanTrue:kOSBooleanFalse);
184 }
185
186 void IOPMPowerSource::setAtWarnLevel(bool b) {
187 properties->setObject(
188 warnLevelKey,
189 b ? kOSBooleanTrue:kOSBooleanFalse);
190 }
191
192 void IOPMPowerSource::setAtCriticalLevel(bool b) {
193 properties->setObject(
194 criticalLevelKey,
195 b ? kOSBooleanTrue:kOSBooleanFalse);
196 }
197
198
199 void IOPMPowerSource::setCurrentCapacity(unsigned int val) {
200 OSNumber *n = OSNumber::withNumber(val, 32);
201 properties->setObject(
202 currentCapacityKey,
203 n);
204 n->release();
205 }
206
207 void IOPMPowerSource::setMaxCapacity(unsigned int val) {
208 OSNumber *n = OSNumber::withNumber(val, 32);
209 properties->setObject(
210 maxCapacityKey,
211 n);
212 n->release();
213 }
214
215 void IOPMPowerSource::setTimeRemaining(int val) {
216 OSNumber *n = OSNumber::withNumber(val, 32);
217 properties->setObject(
218 timeRemainingKey,
219 n);
220 n->release();
221 }
222
223 void IOPMPowerSource::setAmperage(int val) {
224 OSNumber *n = OSNumber::withNumber(val, 32);
225 properties->setObject(
226 amperageKey,
227 n);
228 n->release();
229 }
230
231 void IOPMPowerSource::setVoltage(unsigned int val) {
232 OSNumber *n = OSNumber::withNumber(val, 32);
233 properties->setObject(
234 voltageKey,
235 n);
236 n->release();
237 }
238
239 void IOPMPowerSource::setCycleCount(unsigned int val) {
240 OSNumber *n = OSNumber::withNumber(val, 32);
241 properties->setObject(
242 cycleCountKey,
243 n);
244 n->release();
245 }
246
247 void IOPMPowerSource::setAdapterInfo(int val) {
248 OSNumber *n = OSNumber::withNumber(val, 32);
249 properties->setObject(
250 adapterInfoKey,
251 n);
252 n->release();
253 }
254
255 void IOPMPowerSource::setLocation(int val) {
256 OSNumber *n = OSNumber::withNumber(val, 32);
257 properties->setObject(
258 locationKey,
259 n);
260 n->release();
261 }
262
263 void IOPMPowerSource::setErrorCondition(OSSymbol *s) {
264 properties->setObject(errorConditionKey, s);
265 }
266
267 void IOPMPowerSource::setManufacturer(OSSymbol *s) {
268 properties->setObject(manufacturerKey, s);
269 }
270
271 void IOPMPowerSource::setModel(OSSymbol *s) {
272 properties->setObject(modelKey, s);
273 }
274
275 void IOPMPowerSource::setSerial(OSSymbol *s) {
276 properties->setObject(serialKey, s);
277 }
278
279 void 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
292 bool IOPMPowerSource::externalConnected(void) {
293 return (kOSBooleanTrue == properties->getObject(externalConnectedKey));
294 }
295
296 bool IOPMPowerSource::externalChargeCapable(void) {
297 return (kOSBooleanTrue == properties->getObject(externalChargeCapableKey));
298 }
299
300 bool IOPMPowerSource::batteryInstalled(void) {
301 return (kOSBooleanTrue == properties->getObject(batteryInstalledKey));
302 }
303
304 bool IOPMPowerSource::isCharging(void) {
305 return (kOSBooleanTrue == properties->getObject(chargingKey));
306 }
307
308 bool IOPMPowerSource::atWarnLevel(void) {
309 return (kOSBooleanTrue == properties->getObject(warnLevelKey));
310 }
311
312 bool IOPMPowerSource::atCriticalLevel(void) {
313 return (kOSBooleanTrue == properties->getObject(criticalLevelKey));
314 }
315
316 unsigned 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
323 unsigned 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
330 unsigned int IOPMPowerSource::capacityPercentRemaining(void)
331 {
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 }
340
341 int IOPMPowerSource::timeRemaining(void) {
342 OSNumber *n;
343 n = OSDynamicCast(OSNumber, properties->getObject(timeRemainingKey));
344 if(!n) return 0;
345 else return (int)n->unsigned32BitValue();
346 }
347
348 int 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 }
354
355 unsigned 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 }
361
362 unsigned 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 }
368
369 int 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 }
375
376 int 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
383 OSSymbol *IOPMPowerSource::errorCondition(void) {
384 return OSDynamicCast(OSSymbol, properties->getObject(errorConditionKey));
385 }
386
387 OSSymbol *IOPMPowerSource::manufacturer(void) {
388 return OSDynamicCast(OSSymbol, properties->getObject(manufacturerKey));
389 }
390
391 OSSymbol *IOPMPowerSource::model(void) {
392 return OSDynamicCast(OSSymbol, properties->getObject(modelKey));
393 }
394
395 OSSymbol *IOPMPowerSource::serial(void) {
396 return OSDynamicCast(OSSymbol, properties->getObject(serialKey));
397 }
398
399 OSDictionary *IOPMPowerSource::legacyIOBatteryInfo(void) {
400 return OSDynamicCast(OSDictionary, properties->getObject(batteryInfoKey));
401 }