]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
0c530ab8 | 2 | * Copyright (c) 1998-2005 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 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 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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | ||
29 | #include <IOKit/pwr_mgt/IOPMPowerSource.h> | |
0c530ab8 A |
30 | #include <IOKit/pwr_mgt/IOPM.h> |
31 | #include <IOKit/IOMessage.h> | |
32 | #include <IOKit/IOLib.h> | |
1c79356b | 33 | |
0c530ab8 | 34 | #define super IOService |
1c79356b | 35 | |
0c530ab8 | 36 | OSDefineMetaClassAndStructors(IOPMPowerSource, IOService) |
1c79356b | 37 | |
0c530ab8 A |
38 | // ***************************************************************************** |
39 | // powerSource | |
1c79356b | 40 | // |
0c530ab8 A |
41 | // Static initializer for IOPMPowerSource. Returns a new instance of the class |
42 | // which the caller must attach to the power plane. | |
43 | // ***************************************************************************** | |
8ad349bb | 44 | |
0c530ab8 A |
45 | IOPMPowerSource *IOPMPowerSource::powerSource(void) |
46 | { | |
47 | IOPMPowerSource *ps = new IOPMPowerSource; | |
6601e61a | 48 | |
0c530ab8 A |
49 | if(ps) { |
50 | ps->init(); | |
51 | return ps; | |
52 | } | |
53 | return NULL; | |
1c79356b A |
54 | } |
55 | ||
0c530ab8 A |
56 | // ***************************************************************************** |
57 | // init | |
1c79356b | 58 | // |
0c530ab8 A |
59 | // ***************************************************************************** |
60 | bool IOPMPowerSource::init (void) | |
1c79356b | 61 | { |
0c530ab8 A |
62 | if (!super::init()) { |
63 | return false; | |
64 | } | |
4452a7af | 65 | |
0c530ab8 A |
66 | nextInList = NULL; |
67 | ||
68 | properties = OSDictionary::withCapacity(10); | |
69 | if(!properties) return false; | |
70 | properties->setCapacityIncrement(1); | |
4452a7af | 71 | |
0c530ab8 A |
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; | |
1c79356b A |
93 | } |
94 | ||
0c530ab8 A |
95 | // ***************************************************************************** |
96 | // free | |
1c79356b | 97 | // |
0c530ab8 A |
98 | // ***************************************************************************** |
99 | void IOPMPowerSource::free(void) | |
1c79356b | 100 | { |
0c530ab8 A |
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(); | |
1c79356b A |
121 | } |
122 | ||
0c530ab8 A |
123 | // ***************************************************************************** |
124 | // updateStatus | |
1c79356b | 125 | // |
0c530ab8 A |
126 | // Update power source state in IORegistry and message interested clients |
127 | // notifying them of our change. | |
128 | // ***************************************************************************** | |
129 | void IOPMPowerSource::updateStatus (void) | |
1c79356b | 130 | { |
0c530ab8 A |
131 | OSCollectionIterator *iterator; |
132 | OSObject *iteratorKey; | |
133 | OSObject *obj; | |
134 | ||
2d21ac55 A |
135 | // do nothing if settings haven't changed |
136 | if(!settingsChangedSinceUpdate) return; | |
137 | ||
0c530ab8 A |
138 | iterator = OSCollectionIterator::withCollection(properties); |
139 | if(!iterator) return; | |
140 | ||
141 | while ((iteratorKey = iterator->getNextObject())) { | |
142 | OSSymbol *key; | |
143 | ||
144 | key = OSDynamicCast(OSSymbol, iteratorKey); | |
145 | if (!key) continue; | |
146 | obj = properties->getObject(key); | |
147 | if(!obj) continue; | |
148 | setProperty(key, obj); | |
149 | } | |
150 | iterator->release(); | |
151 | ||
2d21ac55 A |
152 | settingsChangedSinceUpdate = false; |
153 | ||
0c530ab8 A |
154 | // And up goes the flare |
155 | messageClients(kIOPMMessageBatteryStatusHasChanged); | |
4452a7af A |
156 | } |
157 | ||
0c530ab8 A |
158 | |
159 | /******************************************************************************* | |
160 | * | |
161 | * PROTECTED Accessors. All the setters! Yay! | |
162 | * | |
163 | ******************************************************************************/ | |
164 | ||
2d21ac55 A |
165 | void IOPMPowerSource::setPSProperty(const OSSymbol *key, OSObject *val) |
166 | { | |
167 | OSObject *lastVal; | |
168 | OSNumber *newNumVal; | |
169 | ||
170 | if(!key || !val) return; | |
171 | ||
172 | // Compare new setting with existing setting; update | |
173 | // 'settingsChangedSinceUpdate' if the setting has changed. | |
174 | // If values are OSNumbers, do equality comparison. | |
175 | // Otherwise, just compare pointers. | |
176 | ||
177 | if( (lastVal = properties->getObject(key)) ) { | |
178 | newNumVal = OSDynamicCast(OSNumber, val); | |
179 | if(newNumVal) { | |
180 | if(newNumVal->isEqualTo(lastVal)) { | |
181 | // settings didn't change | |
182 | } else { | |
183 | // num val is not equal to last val | |
184 | settingsChangedSinceUpdate = true; | |
185 | } | |
186 | } else { | |
187 | // pointer compare as last resort | |
188 | if(lastVal != val) | |
189 | settingsChangedSinceUpdate = true; | |
190 | } | |
191 | } else { | |
192 | // new setting; no last value | |
193 | settingsChangedSinceUpdate = true; | |
194 | } | |
195 | ||
196 | // here's the part where we go crazy. | |
197 | properties->setObject(key, val); | |
198 | } | |
199 | ||
200 | ||
201 | ||
0c530ab8 | 202 | void IOPMPowerSource::setExternalConnected(bool b) { |
2d21ac55 A |
203 | setPSProperty(externalConnectedKey, |
204 | b ? kOSBooleanTrue : kOSBooleanFalse); | |
4452a7af A |
205 | } |
206 | ||
0c530ab8 | 207 | void IOPMPowerSource::setExternalChargeCapable(bool b) { |
2d21ac55 A |
208 | setPSProperty(externalChargeCapableKey, |
209 | b ? kOSBooleanTrue : kOSBooleanFalse); | |
4452a7af A |
210 | } |
211 | ||
0c530ab8 | 212 | void IOPMPowerSource::setBatteryInstalled(bool b) { |
2d21ac55 A |
213 | setPSProperty(batteryInstalledKey, |
214 | b ? kOSBooleanTrue : kOSBooleanFalse); | |
4452a7af A |
215 | } |
216 | ||
0c530ab8 | 217 | void IOPMPowerSource::setIsCharging(bool b) { |
2d21ac55 A |
218 | setPSProperty(chargingKey, |
219 | b ? kOSBooleanTrue : kOSBooleanFalse); | |
4452a7af A |
220 | } |
221 | ||
0c530ab8 | 222 | void IOPMPowerSource::setAtWarnLevel(bool b) { |
2d21ac55 A |
223 | setPSProperty(warnLevelKey, |
224 | b ? kOSBooleanTrue : kOSBooleanFalse); | |
4452a7af A |
225 | } |
226 | ||
0c530ab8 | 227 | void IOPMPowerSource::setAtCriticalLevel(bool b) { |
2d21ac55 A |
228 | setPSProperty(criticalLevelKey, |
229 | b ? kOSBooleanTrue : kOSBooleanFalse); | |
4452a7af | 230 | } |
5d5c5d0d | 231 | |
0c530ab8 A |
232 | |
233 | void IOPMPowerSource::setCurrentCapacity(unsigned int val) { | |
234 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 235 | setPSProperty(currentCapacityKey, n); |
0c530ab8 | 236 | n->release(); |
89b3af67 A |
237 | } |
238 | ||
0c530ab8 A |
239 | void IOPMPowerSource::setMaxCapacity(unsigned int val) { |
240 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 241 | setPSProperty(maxCapacityKey, n); |
0c530ab8 A |
242 | n->release(); |
243 | } | |
89b3af67 | 244 | |
0c530ab8 A |
245 | void IOPMPowerSource::setTimeRemaining(int val) { |
246 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 247 | setPSProperty(timeRemainingKey, n); |
0c530ab8 | 248 | n->release(); |
4452a7af | 249 | } |
89b3af67 | 250 | |
0c530ab8 A |
251 | void IOPMPowerSource::setAmperage(int val) { |
252 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 253 | setPSProperty(amperageKey, n); |
0c530ab8 A |
254 | n->release(); |
255 | } | |
89b3af67 | 256 | |
0c530ab8 A |
257 | void IOPMPowerSource::setVoltage(unsigned int val) { |
258 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 259 | setPSProperty(voltageKey, n); |
0c530ab8 A |
260 | n->release(); |
261 | } | |
262 | ||
263 | void IOPMPowerSource::setCycleCount(unsigned int val) { | |
264 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 265 | setPSProperty(cycleCountKey, n); |
0c530ab8 A |
266 | n->release(); |
267 | } | |
268 | ||
269 | void IOPMPowerSource::setAdapterInfo(int val) { | |
270 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 271 | setPSProperty(adapterInfoKey, n); |
0c530ab8 A |
272 | n->release(); |
273 | } | |
274 | ||
275 | void IOPMPowerSource::setLocation(int val) { | |
276 | OSNumber *n = OSNumber::withNumber(val, 32); | |
2d21ac55 | 277 | setPSProperty(locationKey, n); |
0c530ab8 A |
278 | n->release(); |
279 | } | |
280 | ||
281 | void IOPMPowerSource::setErrorCondition(OSSymbol *s) { | |
2d21ac55 | 282 | setPSProperty(errorConditionKey, s); |
0c530ab8 A |
283 | } |
284 | ||
285 | void IOPMPowerSource::setManufacturer(OSSymbol *s) { | |
2d21ac55 | 286 | setPSProperty(manufacturerKey, s); |
0c530ab8 A |
287 | } |
288 | ||
289 | void IOPMPowerSource::setModel(OSSymbol *s) { | |
2d21ac55 | 290 | setPSProperty(modelKey, s); |
0c530ab8 A |
291 | } |
292 | ||
293 | void IOPMPowerSource::setSerial(OSSymbol *s) { | |
2d21ac55 | 294 | setPSProperty(serialKey, s); |
0c530ab8 A |
295 | } |
296 | ||
297 | void IOPMPowerSource::setLegacyIOBatteryInfo(OSDictionary *d) { | |
2d21ac55 | 298 | setPSProperty(batteryInfoKey, d); |
0c530ab8 A |
299 | } |
300 | ||
301 | ||
302 | ||
303 | ||
304 | /******************************************************************************* | |
305 | * | |
306 | * PUBLIC Accessors. All the getters! Boo! | |
307 | * | |
308 | ******************************************************************************/ | |
309 | ||
2d21ac55 A |
310 | OSObject *IOPMPowerSource::getPSProperty(const OSSymbol *symmie) { |
311 | if(!symmie) return NULL; | |
312 | return properties->getObject(symmie); | |
313 | } | |
314 | ||
0c530ab8 A |
315 | bool IOPMPowerSource::externalConnected(void) { |
316 | return (kOSBooleanTrue == properties->getObject(externalConnectedKey)); | |
317 | } | |
318 | ||
319 | bool IOPMPowerSource::externalChargeCapable(void) { | |
320 | return (kOSBooleanTrue == properties->getObject(externalChargeCapableKey)); | |
321 | } | |
322 | ||
323 | bool IOPMPowerSource::batteryInstalled(void) { | |
324 | return (kOSBooleanTrue == properties->getObject(batteryInstalledKey)); | |
325 | } | |
326 | ||
327 | bool IOPMPowerSource::isCharging(void) { | |
328 | return (kOSBooleanTrue == properties->getObject(chargingKey)); | |
329 | } | |
330 | ||
331 | bool IOPMPowerSource::atWarnLevel(void) { | |
332 | return (kOSBooleanTrue == properties->getObject(warnLevelKey)); | |
333 | } | |
334 | ||
335 | bool IOPMPowerSource::atCriticalLevel(void) { | |
336 | return (kOSBooleanTrue == properties->getObject(criticalLevelKey)); | |
337 | } | |
338 | ||
339 | unsigned int IOPMPowerSource::currentCapacity(void) { | |
340 | OSNumber *n; | |
341 | n = OSDynamicCast(OSNumber, properties->getObject(currentCapacityKey)); | |
342 | if(!n) return 0; | |
343 | else return (unsigned int)n->unsigned32BitValue(); | |
344 | } | |
345 | ||
346 | unsigned int IOPMPowerSource::maxCapacity(void) { | |
347 | OSNumber *n; | |
348 | n = OSDynamicCast(OSNumber, properties->getObject(maxCapacityKey)); | |
349 | if(!n) return 0; | |
350 | else return (unsigned int)n->unsigned32BitValue(); | |
351 | } | |
352 | ||
353 | unsigned int IOPMPowerSource::capacityPercentRemaining(void) | |
6601e61a | 354 | { |
0c530ab8 A |
355 | unsigned int _currentCapacity = currentCapacity(); |
356 | unsigned int _maxCapacity = maxCapacity(); | |
357 | if(0 == _maxCapacity) { | |
358 | return 0; | |
359 | } else { | |
360 | return ((100*_currentCapacity) / _maxCapacity); | |
361 | } | |
362 | } | |
89b3af67 | 363 | |
0c530ab8 A |
364 | int IOPMPowerSource::timeRemaining(void) { |
365 | OSNumber *n; | |
366 | n = OSDynamicCast(OSNumber, properties->getObject(timeRemainingKey)); | |
367 | if(!n) return 0; | |
368 | else return (int)n->unsigned32BitValue(); | |
4452a7af A |
369 | } |
370 | ||
0c530ab8 A |
371 | int IOPMPowerSource::amperage(void) { |
372 | OSNumber *n; | |
373 | n = OSDynamicCast(OSNumber, properties->getObject(amperageKey)); | |
374 | if(!n) return 0; | |
375 | else return (int)n->unsigned32BitValue(); | |
376 | } | |
4452a7af | 377 | |
0c530ab8 A |
378 | unsigned int IOPMPowerSource::voltage(void) { |
379 | OSNumber *n; | |
380 | n = OSDynamicCast(OSNumber, properties->getObject(voltageKey)); | |
381 | if(!n) return 0; | |
382 | else return (unsigned int)n->unsigned32BitValue(); | |
383 | } | |
4452a7af | 384 | |
0c530ab8 A |
385 | unsigned int IOPMPowerSource::cycleCount(void) { |
386 | OSNumber *n; | |
387 | n = OSDynamicCast(OSNumber, properties->getObject(cycleCountKey)); | |
388 | if(!n) return 0; | |
389 | else return (unsigned int)n->unsigned32BitValue(); | |
390 | } | |
4452a7af | 391 | |
0c530ab8 A |
392 | int IOPMPowerSource::adapterInfo(void) { |
393 | OSNumber *n; | |
394 | n = OSDynamicCast(OSNumber, properties->getObject(adapterInfoKey)); | |
395 | if(!n) return 0; | |
396 | else return (int)n->unsigned32BitValue(); | |
397 | } | |
4452a7af | 398 | |
0c530ab8 A |
399 | int IOPMPowerSource::location(void) { |
400 | OSNumber *n; | |
401 | n = OSDynamicCast(OSNumber, properties->getObject(locationKey)); | |
402 | if(!n) return 0; | |
403 | else return (unsigned int)n->unsigned32BitValue(); | |
404 | } | |
405 | ||
406 | OSSymbol *IOPMPowerSource::errorCondition(void) { | |
407 | return OSDynamicCast(OSSymbol, properties->getObject(errorConditionKey)); | |
408 | } | |
409 | ||
410 | OSSymbol *IOPMPowerSource::manufacturer(void) { | |
411 | return OSDynamicCast(OSSymbol, properties->getObject(manufacturerKey)); | |
412 | } | |
413 | ||
414 | OSSymbol *IOPMPowerSource::model(void) { | |
415 | return OSDynamicCast(OSSymbol, properties->getObject(modelKey)); | |
416 | } | |
417 | ||
418 | OSSymbol *IOPMPowerSource::serial(void) { | |
419 | return OSDynamicCast(OSSymbol, properties->getObject(serialKey)); | |
420 | } | |
421 | ||
422 | OSDictionary *IOPMPowerSource::legacyIOBatteryInfo(void) { | |
423 | return OSDynamicCast(OSDictionary, properties->getObject(batteryInfoKey)); | |
424 | } |