1 | //
|
---|
2 | // PSPowerManager.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Mon Dec 23 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "PSPowerManager.h"
|
---|
10 |
|
---|
11 | #import <IOKit/pwr_mgt/IOPMLib.h>
|
---|
12 | #import <IOKit/IOMessage.h>
|
---|
13 | #import <CoreFoundation/CoreFoundation.h>
|
---|
14 |
|
---|
15 | /*
|
---|
16 | * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
|
---|
17 | *
|
---|
18 | * @APPLE_LICENSE_HEADER_START@
|
---|
19 | *
|
---|
20 | * The contents of this file constitute Original Code as defined in and
|
---|
21 | * are subject to the Apple Public Source License Version 1.1 (the
|
---|
22 | * "License"). You may not use this file except in compliance with the
|
---|
23 | * License. Please obtain a copy of the License at
|
---|
24 | * http://www.apple.com/publicsource and read it before using this file.
|
---|
25 | *
|
---|
26 | * This Original Code and all software distributed under the License are
|
---|
27 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
|
---|
28 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
|
---|
29 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
|
---|
30 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
|
---|
31 | * License for the specific language governing rights and limitations
|
---|
32 | * under the License.
|
---|
33 | *
|
---|
34 | * @APPLE_LICENSE_HEADER_END@
|
---|
35 | */
|
---|
36 |
|
---|
37 | /* Sample code to set an automatic wakeup timer to wake machines from sleep.
|
---|
38 |
|
---|
39 | When a machine is asleep, most hardware (including the processor) is
|
---|
40 | powered off. The PMU chip is one of the few things left powered on, and it's
|
---|
41 | able to generate a wakeup event on a timer.
|
---|
42 | This code shows how to set the wakeup timer within the PMU.
|
---|
43 | */
|
---|
44 |
|
---|
45 | // From autowake.cpp:
|
---|
46 |
|
---|
47 | // #define kAppleVIAUserClientMagicCookie 0x101face // or 0x101beef -- for PMU
|
---|
48 | // #define kAppleVIAUserClientMagicCookie 0x101beef // or 0x101face -- for PMU
|
---|
49 |
|
---|
50 | // The difference is 101beef is only for superusers and 101face works for
|
---|
51 | // non-privileged users. I have not determined which calls are only available
|
---|
52 | // for superusers
|
---|
53 |
|
---|
54 | #define PMU_MAGIC_PASSWORD 0x0101FACE // BEEF
|
---|
55 |
|
---|
56 | /* ==========================================
|
---|
57 | * Close a device user client
|
---|
58 | * =========================================== */
|
---|
59 | static kern_return_t
|
---|
60 | closeDevice(io_connect_t con)
|
---|
61 | {
|
---|
62 | kern_return_t ret = IOServiceClose(con);
|
---|
63 |
|
---|
64 | NSCAssert1(ret == kIOReturnSuccess, @"closeDevice: IOServiceClose returned an error of type %08lx", (unsigned long)ret);
|
---|
65 |
|
---|
66 | return ret;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* ==========================================
|
---|
70 | * Open an IORegistry device user client
|
---|
71 | * =========================================== */
|
---|
72 | static void
|
---|
73 | openDevice(io_object_t obj, unsigned int type, io_connect_t * con)
|
---|
74 | {
|
---|
75 | kern_return_t ret = IOServiceOpen(obj, mach_task_self(), type, con);
|
---|
76 |
|
---|
77 | NSCAssert1(ret == kIOReturnSuccess, @"openDevice: IOServiceOpen returned an error of type %08lx", (unsigned long)ret);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /* ===========================================
|
---|
81 | * Changes the string for a registry
|
---|
82 | * property.
|
---|
83 | * =========================================== */
|
---|
84 | void
|
---|
85 | writeDataProperty(io_object_t handle, CFStringRef name,
|
---|
86 | unsigned char * bytes, unsigned int size)
|
---|
87 | {
|
---|
88 | kern_return_t kr = kIOReturnNoMemory;
|
---|
89 | CFDataRef data;
|
---|
90 | CFMutableDictionaryRef dict = 0;
|
---|
91 |
|
---|
92 | data = CFDataCreate(kCFAllocatorDefault, bytes, size);
|
---|
93 | NSCAssert(data != NULL, @"writeDataProperty: CFDataCreate failed");
|
---|
94 | [(NSData *)data autorelease];
|
---|
95 |
|
---|
96 | dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
---|
97 | NSCAssert(data != NULL, @"writeDataProperty: CFDictionaryCreateMutable failed");
|
---|
98 | [(NSMutableDictionary *)dict autorelease];
|
---|
99 |
|
---|
100 | CFDictionarySetValue(dict, name, data);
|
---|
101 | kr = IOConnectSetCFProperties(handle, dict);
|
---|
102 | NSCAssert1(kr == KERN_SUCCESS, @"writeDataProperty: IOConnectSetCFProperties returned an error of type %08lx", (unsigned long)kr);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* ==========================================
|
---|
106 | * Write a data property to the PMU driver
|
---|
107 | * Arguments
|
---|
108 | * pmuReference - the IORegistry device to write to
|
---|
109 | * propertyName - Name of the property to write to
|
---|
110 | * data - Data to write
|
---|
111 | * dataSize - Data size
|
---|
112 | * =========================================== */
|
---|
113 | void
|
---|
114 | writePMUProperty(io_object_t pmuReference, CFStringRef propertyName, void *data, size_t dataSize)
|
---|
115 | {
|
---|
116 | io_connect_t conObj;
|
---|
117 | openDevice(pmuReference, PMU_MAGIC_PASSWORD, &conObj);
|
---|
118 | writeDataProperty(conObj, propertyName, (unsigned char *)data, dataSize);
|
---|
119 | closeDevice(conObj);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /* ==========================================
|
---|
124 | * Look through the registry and search for an
|
---|
125 | * IONetworkInterface objects with the given
|
---|
126 | * name.
|
---|
127 | * If a match is found, the object is returned.
|
---|
128 | * =========================================== */
|
---|
129 |
|
---|
130 | io_service_t
|
---|
131 | getInterfaceWithName(mach_port_t masterPort, char *className)
|
---|
132 | {
|
---|
133 | io_service_t obj;
|
---|
134 |
|
---|
135 | obj = IOServiceGetMatchingService(masterPort, IOServiceMatching(className));
|
---|
136 |
|
---|
137 | NSCAssert(obj != NULL, @"getInterfaceWithName: IOServiceGetMatchingService returned NULL");
|
---|
138 |
|
---|
139 | return obj;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* ==========================================
|
---|
143 | * Find the PMU in the IORegistry
|
---|
144 | * =========================================== */
|
---|
145 | io_service_t
|
---|
146 | openPMUComPort(void)
|
---|
147 | {
|
---|
148 | static mach_port_t masterPort;
|
---|
149 | kern_return_t kr;
|
---|
150 |
|
---|
151 | // Get a master port to talk with the mach_kernel
|
---|
152 | kr = IOMasterPort(bootstrap_port, &masterPort);
|
---|
153 | NSCAssert1(kr == KERN_SUCCESS, @"openPMUComPort: IOMasterPort returned an error of type %08lx", (unsigned long)kr);
|
---|
154 |
|
---|
155 | return getInterfaceWithName(masterPort, "ApplePMU");
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | /* ==========================================
|
---|
160 | * Release our reference to the PMU in the IORegistry
|
---|
161 | * =========================================== */
|
---|
162 | void
|
---|
163 | closePMUComPort(io_object_t pmuRef)
|
---|
164 | {
|
---|
165 | IOObjectRelease(pmuRef);
|
---|
166 | }
|
---|
167 |
|
---|
168 | @implementation PSPowerManager
|
---|
169 |
|
---|
170 | + (BOOL)autoWakeSupported;
|
---|
171 | {
|
---|
172 | io_service_t pmuReference = openPMUComPort();
|
---|
173 | if (pmuReference == NULL) return NO;
|
---|
174 | closePMUComPort(pmuReference);
|
---|
175 | return YES;
|
---|
176 | }
|
---|
177 |
|
---|
178 | + (io_service_t)_pmuReference;
|
---|
179 | {
|
---|
180 | io_service_t pmuReference = openPMUComPort();
|
---|
181 | NSAssert(pmuReference != NULL, NSLocalizedString(@"Couldn't find PMU in IORegistry. This computer may not support automatic wake from sleep.", "Assertion message: couldn't open ApplePMU"));
|
---|
182 | return pmuReference;
|
---|
183 | }
|
---|
184 |
|
---|
185 | + (NSDate *)wakeTime;
|
---|
186 | {
|
---|
187 | io_service_t pmuReference = [self _pmuReference];
|
---|
188 | NSNumber *autoWakeTime;
|
---|
189 | unsigned long long rawWakeTime;
|
---|
190 |
|
---|
191 | autoWakeTime = (NSNumber *)IORegistryEntryCreateCFProperty(pmuReference, CFSTR("AutoWake"), NULL, 0);
|
---|
192 | closePMUComPort(pmuReference);
|
---|
193 |
|
---|
194 | if (autoWakeTime == nil) return nil;
|
---|
195 | rawWakeTime = [autoWakeTime unsignedLongLongValue];
|
---|
196 | if (rawWakeTime == 0) return nil;
|
---|
197 | // XXX no idea what the epoch is supposed to be, but this works...
|
---|
198 | return [NSDate dateWithTimeIntervalSinceReferenceDate: rawWakeTime - 18446744072475718320LLU];
|
---|
199 | }
|
---|
200 |
|
---|
201 | + (void)setWakeTime:(NSDate *)time;
|
---|
202 | {
|
---|
203 | io_service_t pmuReference = [self _pmuReference];
|
---|
204 | unsigned long wakeTime;
|
---|
205 |
|
---|
206 | if (time == nil) wakeTime = 0;
|
---|
207 | else {
|
---|
208 | wakeTime = [time timeIntervalSinceNow];
|
---|
209 | if (wakeTime == 0) wakeTime++; // 0 will disable
|
---|
210 | }
|
---|
211 | writePMUProperty(pmuReference, CFSTR("AutoWake"), (unsigned long *)&wakeTime, sizeof(wakeTime));
|
---|
212 |
|
---|
213 | closePMUComPort(pmuReference);
|
---|
214 | }
|
---|
215 |
|
---|
216 | + (void)clearWakeTime;
|
---|
217 | {
|
---|
218 | [self setWakeTime: nil];
|
---|
219 | }
|
---|
220 |
|
---|
221 | // modified from RegisterForSleep sample code
|
---|
222 |
|
---|
223 | - (void)_messageReceived:(natural_t)messageType withArgument:(void *)messageArgument;
|
---|
224 | {
|
---|
225 | switch (messageType) {
|
---|
226 | case kIOMessageSystemWillSleep:
|
---|
227 | if ([delegate respondsToSelector: @selector(powerManagerWillDemandSleep:)]) {
|
---|
228 | [delegate powerManagerWillDemandSleep: self];
|
---|
229 | IOAllowPowerChange(root_port, (long)messageArgument);
|
---|
230 | }
|
---|
231 | break;
|
---|
232 | case kIOMessageCanSystemSleep:
|
---|
233 | if ([delegate respondsToSelector: @selector(powerManagerShouldIdleSleep:)]) {
|
---|
234 | if ([delegate powerManagerShouldIdleSleep: self]) {
|
---|
235 | IOAllowPowerChange(root_port, (long)messageArgument);
|
---|
236 | } else {
|
---|
237 | IOCancelPowerChange(root_port, (long)messageArgument);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | break;
|
---|
241 | case kIOMessageSystemHasPoweredOn:
|
---|
242 | if ([delegate respondsToSelector: @selector(powerManagerDidWake:)])
|
---|
243 | [delegate powerManagerDidWake: self];
|
---|
244 | break;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | void
|
---|
249 | powerCallback(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
|
---|
250 | {
|
---|
251 | [(PSPowerManager *)refCon _messageReceived: messageType withArgument: messageArgument];
|
---|
252 | }
|
---|
253 |
|
---|
254 | - (id)initWithDelegate:(id)aDelegate;
|
---|
255 | {
|
---|
256 | if ( (self = [super init]) != nil) {
|
---|
257 | IONotificationPortRef notificationPort;
|
---|
258 |
|
---|
259 | delegate = [aDelegate retain];
|
---|
260 | root_port = IORegisterForSystemPower(self, ¬ificationPort, powerCallback, ¬ifier);
|
---|
261 | NSAssert(root_port != NULL, @"IORegisterForSystemPower failed");
|
---|
262 |
|
---|
263 | CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode);
|
---|
264 | }
|
---|
265 | return self;
|
---|
266 | }
|
---|
267 |
|
---|
268 | - (void)dealloc;
|
---|
269 | {
|
---|
270 | IODeregisterForSystemPower(¬ifier);
|
---|
271 | [delegate release];
|
---|
272 | [super dealloc];
|
---|
273 | }
|
---|
274 |
|
---|
275 | @end |
---|