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