]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOPMPowerSource.cpp
281377d3f1d99cbe21f6137720cefd543b5fc3eb
[apple/xnu.git] / iokit / Kernel / IOPMPowerSource.cpp
1 /*
2 * Copyright (c) 1998-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <IOKit/pwr_mgt/IOPMPowerSource.h>
30 #include <IOKit/pwr_mgt/IOPM.h>
31 #include <IOKit/IOMessage.h>
32 #include <IOKit/IOLib.h>
33
34 #define super IOService
35
36 OSDefineMetaClassAndStructors(IOPMPowerSource, IOService)
37
38 // *****************************************************************************
39 // powerSource
40 //
41 // Static initializer for IOPMPowerSource. Returns a new instance of the class
42 // which the caller must attach to the power plane.
43 // *****************************************************************************
44
45 IOPMPowerSource *IOPMPowerSource::powerSource(void)
46 {
47 IOPMPowerSource *ps = new IOPMPowerSource;
48
49 if(ps) {
50 ps->init();
51 return ps;
52 }
53 return NULL;
54 }
55
56 // *****************************************************************************
57 // init
58 //
59 // *****************************************************************************
60 bool IOPMPowerSource::init (void)
61 {
62 if (!super::init()) {
63 return false;
64 }
65
66 nextInList = NULL;
67
68 properties = OSDictionary::withCapacity(10);
69 if(!properties) return false;
70 properties->setCapacityIncrement(1);
71
72 externalConnectedKey = OSSymbol::withCString(kIOPMPSExternalConnectedKey);
73 externalChargeCapableKey = OSSymbol::withCString(kIOPMPSExternalChargeCapableKey);
74 batteryInstalledKey = OSSymbol::withCString(kIOPMPSBatteryInstalledKey);
75 chargingKey = OSSymbol::withCString(kIOPMPSIsChargingKey);
76 warnLevelKey = OSSymbol::withCString(kIOPMPSAtWarnLevelKey);
77 criticalLevelKey = OSSymbol::withCString(kIOPMPSAtCriticalLevelKey);
78 currentCapacityKey = OSSymbol::withCString(kIOPMPSCurrentCapacityKey);
79 maxCapacityKey = OSSymbol::withCString(kIOPMPSMaxCapacityKey);
80 timeRemainingKey = OSSymbol::withCString(kIOPMPSTimeRemainingKey);
81 amperageKey = OSSymbol::withCString(kIOPMPSAmperageKey);
82 voltageKey = OSSymbol::withCString(kIOPMPSVoltageKey);
83 cycleCountKey = OSSymbol::withCString(kIOPMPSCycleCountKey);
84 adapterInfoKey = OSSymbol::withCString(kIOPMPSAdapterInfoKey);
85 locationKey = OSSymbol::withCString(kIOPMPSLocationKey);
86 errorConditionKey = OSSymbol::withCString(kIOPMPSErrorConditionKey);
87 manufacturerKey = OSSymbol::withCString(kIOPMPSManufacturerKey);
88 modelKey = OSSymbol::withCString(kIOPMPSModelKey);
89 serialKey = OSSymbol::withCString(kIOPMPSSerialKey);
90 batteryInfoKey = OSSymbol::withCString(kIOPMPSLegacyBatteryInfoKey);
91
92 return true;
93 }
94
95 // *****************************************************************************
96 // free
97 //
98 // *****************************************************************************
99 void IOPMPowerSource::free(void)
100 {
101 if(properties) properties->release();
102 if(externalConnectedKey) externalConnectedKey->release();
103 if(externalChargeCapableKey) externalChargeCapableKey->release();
104 if(batteryInstalledKey) batteryInstalledKey->release();
105 if(chargingKey) chargingKey->release();
106 if(warnLevelKey) warnLevelKey->release();
107 if(criticalLevelKey) criticalLevelKey->release();
108 if(currentCapacityKey) currentCapacityKey->release();
109 if(maxCapacityKey) maxCapacityKey->release();
110 if(timeRemainingKey) timeRemainingKey->release();
111 if(amperageKey) amperageKey->release();
112 if(voltageKey) voltageKey->release();
113 if(cycleCountKey) cycleCountKey->release();
114 if(adapterInfoKey) adapterInfoKey->release();
115 if(errorConditionKey) errorConditionKey->release();
116 if(manufacturerKey) manufacturerKey->release();
117 if(modelKey) modelKey->release();
118 if(serialKey) serialKey->release();
119 if(locationKey) locationKey->release();
120 if(batteryInfoKey) batteryInfoKey->release();
121 }
122
123 // *****************************************************************************
124 // updateStatus
125 //
126 // Update power source state in IORegistry and message interested clients
127 // notifying them of our change.
128 // *****************************************************************************
129 void IOPMPowerSource::updateStatus (void)
130 {
131 OSCollectionIterator *iterator;
132 OSObject *iteratorKey;
133 OSObject *obj;
134
135 iterator = OSCollectionIterator::withCollection(properties);
136 if(!iterator) return;
137
138 while ((iteratorKey = iterator->getNextObject())) {
139 OSSymbol *key;
140
141 key = OSDynamicCast(OSSymbol, iteratorKey);
142 if (!key) continue;
143 obj = properties->getObject(key);
144 if(!obj) continue;
145 setProperty(key, obj);
146 }
147 iterator->release();
148
149 // And up goes the flare
150 messageClients(kIOPMMessageBatteryStatusHasChanged);
151 }
152
153
154 /*******************************************************************************
155 *
156 * PROTECTED Accessors. All the setters! Yay!
157 *
158 ******************************************************************************/
159
160 void IOPMPowerSource::setExternalConnected(bool b) {
161 properties->setObject(
162 externalConnectedKey,
163 b ? kOSBooleanTrue:kOSBooleanFalse);
164 }
165
166 void IOPMPowerSource::setExternalChargeCapable(bool b) {
167 properties->setObject(
168 externalChargeCapableKey,
169 b ? kOSBooleanTrue:kOSBooleanFalse);
170 }
171
172 void IOPMPowerSource::setBatteryInstalled(bool b) {
173 properties->setObject(
174 batteryInstalledKey,
175 b ? kOSBooleanTrue:kOSBooleanFalse);
176 }
177
178 void IOPMPowerSource::setIsCharging(bool b) {
179 properties->setObject(
180 chargingKey,
181 b ? kOSBooleanTrue:kOSBooleanFalse);
182 }
183
184 void IOPMPowerSource::setAtWarnLevel(bool b) {
185 properties->setObject(
186 warnLevelKey,
187 b ? kOSBooleanTrue:kOSBooleanFalse);
188 }
189
190 void IOPMPowerSource::setAtCriticalLevel(bool b) {
191 properties->setObject(
192 criticalLevelKey,
193 b ? kOSBooleanTrue:kOSBooleanFalse);
194 }
195
196
197 void IOPMPowerSource::setCurrentCapacity(unsigned int val) {
198 OSNumber *n = OSNumber::withNumber(val, 32);
199 properties->setObject(
200 currentCapacityKey,
201 n);
202 n->release();
203 }
204
205 void IOPMPowerSource::setMaxCapacity(unsigned int val) {
206 OSNumber *n = OSNumber::withNumber(val, 32);
207 properties->setObject(
208 maxCapacityKey,
209 n);
210 n->release();
211 }
212
213 void IOPMPowerSource::setTimeRemaining(int val) {
214 OSNumber *n = OSNumber::withNumber(val, 32);
215 properties->setObject(
216 timeRemainingKey,
217 n);
218 n->release();
219 }
220
221 void IOPMPowerSource::setAmperage(int val) {
222 OSNumber *n = OSNumber::withNumber(val, 32);
223 properties->setObject(
224 amperageKey,
225 n);
226 n->release();
227 }
228
229 void IOPMPowerSource::setVoltage(unsigned int val) {
230 OSNumber *n = OSNumber::withNumber(val, 32);
231 properties->setObject(
232 voltageKey,
233 n);
234 n->release();
235 }
236
237 void IOPMPowerSource::setCycleCount(unsigned int val) {
238 OSNumber *n = OSNumber::withNumber(val, 32);
239 properties->setObject(
240 cycleCountKey,
241 n);
242 n->release();
243 }
244
245 void IOPMPowerSource::setAdapterInfo(int val) {
246 OSNumber *n = OSNumber::withNumber(val, 32);
247 properties->setObject(
248 adapterInfoKey,
249 n);
250 n->release();
251 }
252
253 void IOPMPowerSource::setLocation(int val) {
254 OSNumber *n = OSNumber::withNumber(val, 32);
255 properties->setObject(
256 locationKey,
257 n);
258 n->release();
259 }
260
261 void IOPMPowerSource::setErrorCondition(OSSymbol *s) {
262 properties->setObject(errorConditionKey, s);
263 }
264
265 void IOPMPowerSource::setManufacturer(OSSymbol *s) {
266 properties->setObject(manufacturerKey, s);
267 }
268
269 void IOPMPowerSource::setModel(OSSymbol *s) {
270 properties->setObject(modelKey, s);
271 }
272
273 void IOPMPowerSource::setSerial(OSSymbol *s) {
274 properties->setObject(serialKey, s);
275 }
276
277 void IOPMPowerSource::setLegacyIOBatteryInfo(OSDictionary *d) {
278 properties->setObject(batteryInfoKey, d);
279 }
280
281
282
283
284 /*******************************************************************************
285 *
286 * PUBLIC Accessors. All the getters! Boo!
287 *
288 ******************************************************************************/
289
290 bool IOPMPowerSource::externalConnected(void) {
291 return (kOSBooleanTrue == properties->getObject(externalConnectedKey));
292 }
293
294 bool IOPMPowerSource::externalChargeCapable(void) {
295 return (kOSBooleanTrue == properties->getObject(externalChargeCapableKey));
296 }
297
298 bool IOPMPowerSource::batteryInstalled(void) {
299 return (kOSBooleanTrue == properties->getObject(batteryInstalledKey));
300 }
301
302 bool IOPMPowerSource::isCharging(void) {
303 return (kOSBooleanTrue == properties->getObject(chargingKey));
304 }
305
306 bool IOPMPowerSource::atWarnLevel(void) {
307 return (kOSBooleanTrue == properties->getObject(warnLevelKey));
308 }
309
310 bool IOPMPowerSource::atCriticalLevel(void) {
311 return (kOSBooleanTrue == properties->getObject(criticalLevelKey));
312 }
313
314 unsigned int IOPMPowerSource::currentCapacity(void) {
315 OSNumber *n;
316 n = OSDynamicCast(OSNumber, properties->getObject(currentCapacityKey));
317 if(!n) return 0;
318 else return (unsigned int)n->unsigned32BitValue();
319 }
320
321 unsigned int IOPMPowerSource::maxCapacity(void) {
322 OSNumber *n;
323 n = OSDynamicCast(OSNumber, properties->getObject(maxCapacityKey));
324 if(!n) return 0;
325 else return (unsigned int)n->unsigned32BitValue();
326 }
327
328 unsigned int IOPMPowerSource::capacityPercentRemaining(void)
329 {
330 unsigned int _currentCapacity = currentCapacity();
331 unsigned int _maxCapacity = maxCapacity();
332 if(0 == _maxCapacity) {
333 return 0;
334 } else {
335 return ((100*_currentCapacity) / _maxCapacity);
336 }
337 }
338
339 int IOPMPowerSource::timeRemaining(void) {
340 OSNumber *n;
341 n = OSDynamicCast(OSNumber, properties->getObject(timeRemainingKey));
342 if(!n) return 0;
343 else return (int)n->unsigned32BitValue();
344 }
345
346 int IOPMPowerSource::amperage(void) {
347 OSNumber *n;
348 n = OSDynamicCast(OSNumber, properties->getObject(amperageKey));
349 if(!n) return 0;
350 else return (int)n->unsigned32BitValue();
351 }
352
353 unsigned int IOPMPowerSource::voltage(void) {
354 OSNumber *n;
355 n = OSDynamicCast(OSNumber, properties->getObject(voltageKey));
356 if(!n) return 0;
357 else return (unsigned int)n->unsigned32BitValue();
358 }
359
360 unsigned int IOPMPowerSource::cycleCount(void) {
361 OSNumber *n;
362 n = OSDynamicCast(OSNumber, properties->getObject(cycleCountKey));
363 if(!n) return 0;
364 else return (unsigned int)n->unsigned32BitValue();
365 }
366
367 int IOPMPowerSource::adapterInfo(void) {
368 OSNumber *n;
369 n = OSDynamicCast(OSNumber, properties->getObject(adapterInfoKey));
370 if(!n) return 0;
371 else return (int)n->unsigned32BitValue();
372 }
373
374 int IOPMPowerSource::location(void) {
375 OSNumber *n;
376 n = OSDynamicCast(OSNumber, properties->getObject(locationKey));
377 if(!n) return 0;
378 else return (unsigned int)n->unsigned32BitValue();
379 }
380
381 OSSymbol *IOPMPowerSource::errorCondition(void) {
382 return OSDynamicCast(OSSymbol, properties->getObject(errorConditionKey));
383 }
384
385 OSSymbol *IOPMPowerSource::manufacturer(void) {
386 return OSDynamicCast(OSSymbol, properties->getObject(manufacturerKey));
387 }
388
389 OSSymbol *IOPMPowerSource::model(void) {
390 return OSDynamicCast(OSSymbol, properties->getObject(modelKey));
391 }
392
393 OSSymbol *IOPMPowerSource::serial(void) {
394 return OSDynamicCast(OSSymbol, properties->getObject(serialKey));
395 }
396
397 OSDictionary *IOPMPowerSource::legacyIOBatteryInfo(void) {
398 return OSDynamicCast(OSDictionary, properties->getObject(batteryInfoKey));
399 }