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 | }
|
---|
183 |
|
---|
184 | + (NSDate *)wakeTime;
|
---|
185 | {
|
---|
186 | io_service_t pmuReference = [self _pmuReference];
|
---|
187 | NSNumber *autoWakeTime;
|
---|
188 | unsigned long long rawWakeTime;
|
---|
189 |
|
---|
190 | autoWakeTime = (NSNumber *)IORegistryEntryCreateCFProperty(pmuReference, CFSTR("AutoWake"), NULL, 0);
|
---|
191 | closePMUComPort(pmuReference);
|
---|
192 |
|
---|
193 | if (autoWakeTime == nil) return nil;
|
---|
194 | rawWakeTime = [autoWakeTime unsignedLongLongValue];
|
---|
195 | if (rawWakeTime == 0) return nil;
|
---|
196 | // XXX no idea what the epoch is supposed to be, but this works...
|
---|
197 | return [NSDate dateWithTimeIntervalSinceReferenceDate: rawWakeTime - 18446744072475718320LLU];
|
---|
198 | }
|
---|
199 |
|
---|
200 | + (void)setWakeTime:(NSDate *)time;
|
---|
201 | {
|
---|
202 | io_service_t pmuReference = [self _pmuReference];
|
---|
203 | unsigned long wakeTime;
|
---|
204 |
|
---|
205 | if (time == nil) wakeTime = 0;
|
---|
206 | else {
|
---|
207 | wakeTime = [time timeIntervalSinceNow];
|
---|
208 | if (wakeTime == 0) wakeTime++; // 0 will disable
|
---|
209 | }
|
---|
210 | writePMUProperty(pmuReference, CFSTR("AutoWake"), (unsigned long *)&wakeTime, sizeof(wakeTime));
|
---|
211 |
|
---|
212 | closePMUComPort(pmuReference);
|
---|
213 | }
|
---|
214 |
|
---|
215 | + (void)clearWakeTime;
|
---|
216 | {
|
---|
217 | [self setWakeTime: nil];
|
---|
218 | }
|
---|
219 |
|
---|
220 | // modified from RegisterForSleep sample code
|
---|
221 |
|
---|
222 | - (void)_messageReceived:(natural_t)messageType withArgument:(void *)messageArgument;
|
---|
223 | {
|
---|
224 | switch (messageType) {
|
---|
225 | case kIOMessageSystemWillSleep:
|
---|
226 | if ([delegate respondsToSelector: @selector(powerManagerWillDemandSleep:)]) {
|
---|
227 | [delegate powerManagerWillDemandSleep: self];
|
---|
228 | IOAllowPowerChange(root_port, (long)messageArgument);
|
---|
229 | }
|
---|
230 | break;
|
---|
231 | case kIOMessageCanSystemSleep:
|
---|
232 | if ([delegate respondsToSelector: @selector(powerManagerShouldIdleSleep:)]) {
|
---|
233 | if ([delegate powerManagerShouldIdleSleep: self]) {
|
---|
234 | IOAllowPowerChange(root_port, (long)messageArgument);
|
---|
235 | } else {
|
---|
236 | IOCancelPowerChange(root_port, (long)messageArgument);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | break;
|
---|
240 | case kIOMessageSystemHasPoweredOn:
|
---|
241 | if ([delegate respondsToSelector: @selector(powerManagerDidWake:)])
|
---|
242 | [delegate powerManagerDidWake: self];
|
---|
243 | break;
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | void
|
---|
248 | powerCallback(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
|
---|
249 | {
|
---|
250 | [(PSPowerManager *)refCon _messageReceived: messageType withArgument: messageArgument];
|
---|
251 | }
|
---|
252 |
|
---|
253 | - (id)initWithDelegate:(id)aDelegate;
|
---|
254 | {
|
---|
255 | if ( (self = [super init]) != nil) {
|
---|
256 | IONotificationPortRef notificationPort;
|
---|
257 |
|
---|
258 | delegate = [aDelegate retain];
|
---|
259 | root_port = IORegisterForSystemPower(self, ¬ificationPort, powerCallback, ¬ifier);
|
---|
260 | NSAssert(root_port != NULL, @"IORegisterForSystemPower failed");
|
---|
261 |
|
---|
262 | CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode);
|
---|
263 | }
|
---|
264 | return self;
|
---|
265 | }
|
---|
266 |
|
---|
267 | - (void)dealloc;
|
---|
268 | {
|
---|
269 | IODeregisterForSystemPower(¬ifier);
|
---|
270 | [delegate release];
|
---|
271 | [super dealloc];
|
---|
272 | }
|
---|
273 |
|
---|
274 | @end |
---|