]>
Commit | Line | Data |
---|---|---|
83f6dbe8 A |
1 | |
2 | #include "disk_power.h" | |
3 | ||
4 | #include <IOKit/IOKitLib.h> | |
5 | #include <IOKit/pwr_mgt/IOPMLib.h> | |
6 | #include <IOKit/pwr_mgt/IOPM.h> | |
7 | ||
8 | #include <CoreFoundation/CFNumber.h> | |
9 | ||
10 | /* | |
11 | -- | |
12 | */ | |
13 | ||
14 | int on_battery_power( void ) | |
15 | { | |
16 | int result = 0; // Assume we're not running on battery power. | |
17 | kern_return_t kr; | |
18 | mach_port_t master_device_port; | |
19 | ||
20 | CFArrayRef cfarray = NULL; | |
21 | ||
22 | kr = IOMasterPort( bootstrap_port, & master_device_port); | |
23 | if ( KERN_SUCCESS != kr) | |
24 | { | |
25 | fprintf(stderr,"ERROR: IOMasterPort() failed\n"); | |
26 | goto Return; | |
27 | } | |
28 | ||
29 | kr = IOPMCopyBatteryInfo( master_device_port, & cfarray ); | |
30 | if ( kIOReturnSuccess != kr ) | |
31 | { | |
32 | // This case handles desktop machines in addition to error cases. | |
33 | result = 0; | |
34 | goto Return; | |
35 | } | |
36 | ||
37 | { | |
38 | CFDictionaryRef dict; | |
39 | CFNumberRef cfnum; | |
40 | int flags; | |
41 | ||
42 | dict = CFArrayGetValueAtIndex( cfarray, 0 ); | |
43 | cfnum = CFDictionaryGetValue( dict, CFSTR(kIOBatteryFlagsKey) ); | |
44 | ||
45 | if ( CFNumberGetTypeID() != CFGetTypeID( cfnum ) ) | |
46 | { | |
47 | fprintf(stderr, "ERROR: on_battery_power(): battery flags not a CFNumber!!!\n"); | |
48 | result = 0; | |
49 | goto Return; | |
50 | } | |
51 | ||
52 | CFNumberGetValue( cfnum, kCFNumberLongType, & flags ); | |
53 | ||
54 | #if 0 | |
55 | printf( "BatteryFlags = %#08x\n", flags ); | |
56 | ||
57 | printf( "BatteryChargerConnect : %s\n", ( 0 != ( flags & kIOBatteryChargerConnect ) ) ? "TRUE" : "FALSE" ); | |
58 | printf( "BatteryCharge : %s\n", ( 0 != ( flags & kIOBatteryCharge ) ) ? "TRUE" : "FALSE" ); | |
59 | printf( "BatteryInstalled : %s\n", ( 0 != ( flags & kIOBatteryInstalled ) ) ? "TRUE" : "FALSE" ); | |
60 | #endif | |
61 | ||
62 | result = ( 0 == ( flags & kIOPMACInstalled ) ); | |
63 | } | |
64 | ||
65 | Return: | |
66 | if (cfarray) | |
67 | CFRelease(cfarray); | |
68 | return result; | |
69 | ||
70 | } // on_battery_power() | |
71 | ||
72 | /* | |
73 | -- | |
74 | */ | |
75 | ||
76 | int is_disk_awake( void ) | |
77 | { | |
78 | int result = 1; // Go ahead and sync by default. | |
79 | int err; | |
80 | DiskDevice diskDevice; | |
81 | IOATAPowerState powerState; | |
82 | ||
83 | bzero(&diskDevice, sizeof(diskDevice)); | |
84 | ||
85 | err = GetATADeviceInfoWithRetry( & diskDevice ); | |
86 | if ( err ) | |
87 | { | |
88 | result = 1; // Go ahead and sync. | |
89 | goto Return; | |
90 | } | |
91 | ||
92 | powerState = PowerStatesMax( & diskDevice.powerStates ); | |
93 | ||
94 | // printf( "%s: power state = %s\n", __FUNCTION__, PowerStateString( powerState, /* opt_summary = */ 0 ) ); | |
95 | ||
96 | result = ( powerState >= kIOATAPowerStateStandby ); | |
97 | ||
98 | Return: | |
99 | if (diskDevice.name) | |
100 | free(diskDevice.name); | |
101 | if (diskDevice.location) | |
102 | free(diskDevice.location); | |
103 | if (diskDevice.interconnect) | |
104 | free(diskDevice.interconnect); | |
105 | ||
106 | // printf( "%s => %s\n", __FUNCTION__, result ? "TRUE" : "FALSE" ); | |
107 | return result; | |
108 | ||
109 | } // is_disk_awake() | |
110 | ||
111 | /* | |
112 | -- | |
113 | */ | |
114 |