-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathAppleSmartBattery.h
330 lines (249 loc) · 8.99 KB
/
AppleSmartBattery.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#ifndef __AppleSmartBattery__
#define __AppleSmartBattery__
#include <IOKit/IOService.h>
#include <IOKit/pwr_mgt/IOPMPowerSource.h>
#include <IOKit/acpi/IOACPIPlatformDevice.h>
#include "AppleSmartBatteryManager.h"
#define WATTS 0
#define AMPS 1
#define ACPI_MAX 0x7FFFFFFF
#define ACPI_UNKNOWN 0xFFFFFFFF
#define BATTERY_CHARGED 0
#define BATTERY_DISCHARGING 1
#define BATTERY_CHARGING 2
#define BATTERY_CRITICAL 4
#define BATTERY_PRESENT 0x10 // Bit 4 - _STA Method return
// Return package from _BIF
#define BIF_POWER_UNIT 0
#define BIF_DESIGN_CAPACITY 1
#define BIF_LAST_FULL_CAPACITY 2
#define BIF_TECHNOLOGY 3
#define BIF_DESIGN_VOLTAGE 4
#define BIF_CAPACITY_WARNING 5
#define BIF_LOW_WARNING 6
#define BIF_GRANULARITY_1 7
#define BIF_GRANULARITY_2 8
#define BIF_MODEL_NUMBER 9
#define BIF_SERIAL_NUMBER 10
#define BIF_BATTERY_TYPE 11
#define BIF_OEM 12
// Return package from _BIX
#define BIX_REVISION 0
#define BIX_POWER_UNIT 1
#define BIX_DESIGN_CAPACITY 2
#define BIX_LAST_FULL_CAPACITY 3
#define BIX_TECHNOLOGY 4
#define BIX_DESIGN_VOLTAGE 5
#define BIX_CAPACITY_WARNING 6
#define BIX_LOW_WARNING 7
#define BIX_CYCLE_COUNT 8
#define BIX_ACCURACY 9
#define BIX_MAX_SAMPLE_TIME 10
#define BIX_MIN_SAMPLE_TIME 11
#define BIX_MAX_AVG_INTERVAL 12
#define BIX_MIN_AVG_INTERVAL 13
#define BIX_GRANULARITY_1 14
#define BIX_GRANULARITY_2 15
#define BIX_MODEL_NUMBER 16
#define BIX_SERIAL_NUMBER 17
#define BIX_BATTERY_TYPE 18
#define BIX_OEM 19
// Return package from BBIX
#define BBIX_MANUF_ACCESS 0
#define BBIX_BATTERYMODE 1
#define BBIX_ATRATETIMETOFULL 2
#define BBIX_ATRATETIMETOEMPTY 3
#define BBIX_TEMPERATURE 4
#define BBIX_VOLTAGE 5
#define BBIX_CURRENT 6
#define BBIX_AVG_CURRENT 7
#define BBIX_REL_STATE_CHARGE 8
#define BBIX_ABS_STATE_CHARGE 9
#define BBIX_REMAIN_CAPACITY 10
#define BBIX_RUNTIME_TO_EMPTY 11
#define BBIX_AVG_TIME_TO_EMPTY 12
#define BBIX_AVG_TIME_TO_FULL 13
#define BBIX_MANUF_DATE 14
#define BBIX_MANUF_DATA 15
// Return package from _BIF
#define BST_STATUS 0
#define BST_RATE 1
#define BST_CAPACITY 2
#define BST_VOLTAGE 3
#define NUM_BITS 32
// Define this in Info.plist to override the default polling inverval
#define kBatteryPollingDebugKey "BatteryPollingPeriodOverride"
// Define this in Info.plist to use extended battery information (ACPI 4.0 _BIX method)
#define kUseBatteryExtendedInfoKey "UseExtendedBatteryInformationMethod"
// Define this in Info.plist to use extra battery information in non-standard BBIX method
#define kUseBatteryExtraInfoKey "UseExtraBatteryInformationMethod"
static const OSSymbol * unknownObjectKey = OSSymbol::withCString("Unknown");
UInt32 GetValueFromArray(OSArray * array, UInt8 index);
OSSymbol *GetSymbolFromArray(OSArray * array, UInt8 index);
OSData *GetDataFromArray(OSArray *array, UInt8 index);
class AppleSmartBatteryManager;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
class AppleSmartBattery : public IOPMPowerSource
{
OSDeclareDefaultStructors(AppleSmartBattery)
protected:
AppleSmartBatteryManager *fProvider;
IOWorkLoop *fWorkLoop;
IOTimerEventSource *fPollTimer;
bool fPollingNow;
bool fCancelPolling;
IOTimerEventSource *fBatteryReadAllTimer;
uint16_t fMachinePath;
uint32_t fPollingInterval;
bool fPollingOverridden;
bool fUseBatteryExtendedInformation;
bool fUseBatteryExtraInformation;
bool fBatteryPresent;
bool fACConnected;
bool fACChargeCapable;
bool fSystemSleeping;
IOService *fPowerServiceToAck;
OSArray *fCellVoltages;
uint8_t fInitialPollCountdown;;
// Accessor for MaxError reading
// Percent error in MaxCapacity reading
void setMaxErr(int error);
int maxErr(void);
// ACPIBattery reports a device name
void setDeviceName(OSSymbol *sym);
OSSymbol *deviceName(void);
// Set when battery is fully charged;
// Clear when battery starts discharging/AC is removed
void setFullyCharged(bool);
bool fullyCharged(void);
// Time remaining estimate - as measured instantaneously
void setInstantaneousTimeToEmpty(int seconds);
// Time remaining until full estimate - as measured instantaneously
void setInstantaneousTimeToFull(int seconds);
// Instantaneous amperage
void setInstantAmperage(int mA);
// Time remaining estimate - 1 minute average
void setAverageTimeToEmpty(int seconds);
int averageTimeToEmpty(void);
// Time remaining until full estimate - 1 minute average
void setAverageTimeToFull(int seconds);
int averageTimeToFull(void);
void setManufactureDate(int date);
int manufactureDate(void);
void setSerialNumber(OSSymbol *sym);
OSSymbol *serialNumber(void);
// An OSData container of manufacturer specific data
void setManufacturerData(uint8_t *buffer, uint32_t bufferSize);
void setChargeStatus(const OSSymbol *sym);
const OSSymbol *chargeStatus(void);
void oneTimeBatterySetup(void);
void constructAppleSerialNumber(void);
void setDesignCapacity(unsigned int val);
unsigned int designCapacity(void);
void setBatteryType(OSSymbol *sym);
OSSymbol *batteryType(void);
void setPermanentFailureStatus(unsigned int val);
unsigned int permanentFailureStatus(void);
void setRunTimeToEmpty(int seconds);
int runTimeToEmpty(void);
void setRelativeStateOfCharge(int percent);
int relativeStateOfCharge(void);
void setAbsoluteStateOfCharge(int percent);
int absoluteStateOfCharge(void);
void setRemainingCapacity(int mah);
int remainingCapacity(void);
void setAverageCurrent(int ma);
int averageCurrent(void);
void setCurrent(int ma);
int current(void);
void setTemperature(int temperature);
int temperature(void);
const OSSymbol *unpackDate(UInt32 packedDate);
public:
static AppleSmartBattery *smartBattery(void);
virtual bool init(void);
virtual void free(void);
virtual bool start(IOService *provider);
virtual void stop(IOService *provider);
void setPollingInterval(int milliSeconds);
bool pollBatteryState(int path = 0);
IOReturn setPowerState(unsigned long which, IOService *whom);
void handleBatteryInserted(void);
void handleBatteryRemoved(void);
IOReturn handleSystemSleepWake(IOService *powerSource, bool isSystemSleep);
protected:
void logReadError( const char *error_type,
uint16_t additional_error,
void *t);
void clearBatteryState(bool do_update);
void pollingTimeOut(void);
void incompleteReadTimeOut(void);
void rebuildLegacyIOBatteryInfo(bool do_update);
void acknowledgeSystemSleepWake(void);
private:
UInt32 fPowerUnit;
UInt32 fDesignVoltage;
UInt32 fCurrentVoltage;
UInt32 fDesignCapacity;
UInt32 fCurrentCapacity;
UInt32 fBatteryTechnology;
UInt32 fMaxCapacity;
UInt32 fCurrentRate;
UInt32 fAverageRate;
UInt32 fStatus;
UInt32 fCycleCount;
OSSymbol *fDeviceName;
OSSymbol *fType;
OSSymbol *fManufacturer;
OSSymbol *fSerialNumber;
UInt32 fMaxErr;
UInt32 fCellVoltage1;
UInt32 fCellVoltage2;
UInt32 fCellVoltage3;
UInt32 fCellVoltage4;
UInt32 fManufacturerAccess;
UInt32 fBatteryMode;
UInt32 fAtRateTimeToFull;
UInt32 fAtRateTimeToEmpty;
UInt32 fTemperature;
UInt32 fVoltage;
SInt32 fCurrent;
SInt32 fAverageCurrent;
UInt32 fRelativeStateOfCharge;
UInt32 fAbsoluteStateOfCharge;
UInt32 fRemainingCapacity;
UInt32 fRunTimeToEmpty;
UInt32 fAverageTimeToEmpty;
UInt32 fAverageTimeToFull;
UInt32 fManufactureDate;
OSData *fManufacturerData;
public:
IOReturn setBatterySTA(UInt32 battery_status);
IOReturn setBatteryBIF(OSArray *acpibat_bif);
IOReturn setBatteryBIX(OSArray *acpibat_bix);
IOReturn setBatteryBBIX(OSArray *acpibat_bbix);
IOReturn setBatteryBST(OSArray *acpibat_bst);
};
#endif