]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/powerwatch.cpp
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / powerwatch.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // powerwatch - hook into system notifications of power events
21 //
22 #include "powerwatch.h"
23 #include <IOKit/IOMessage.h>
24
25
26 namespace Security {
27 namespace MachPlusPlus {
28
29
30 //
31 // The obligatory empty virtual destructor
32 //
33 PowerWatcher::PowerWatcher()
34 {
35 if (!(mKernelPort = IORegisterForSystemPower(this, &mPortRef, ioCallback, &mHandle)))
36 UnixError::throwMe(EINVAL); // no clue
37 }
38
39 PowerWatcher::~PowerWatcher()
40 {
41 if (mKernelPort)
42 IODeregisterForSystemPower(&mHandle);
43 }
44
45
46 //
47 // The callback dispatcher
48 //
49 void PowerWatcher::ioCallback(void *refCon, io_service_t service,
50 natural_t messageType, void *argument)
51 {
52 PowerWatcher *me = (PowerWatcher *)refCon;
53 enum { allow, refuse, ignore } reaction;
54 switch (messageType) {
55 case kIOMessageSystemWillSleep:
56 debug("powerwatch", "system will sleep");
57 me->systemWillSleep();
58 reaction = allow;
59 break;
60 case kIOMessageSystemHasPoweredOn:
61 debug("powerwatch", "system has powered on");
62 me->systemIsWaking();
63 reaction = ignore;
64 break;
65 case kIOMessageSystemWillPowerOff:
66 debug("powerwatch", "system will power off");
67 me->systemWillPowerDown();
68 reaction = allow;
69 break;
70 case kIOMessageSystemWillNotPowerOff:
71 debug("powerwatch", "system will not power off");
72 reaction = ignore;
73 break;
74 case kIOMessageCanSystemSleep:
75 debug("powerwatch", "can system sleep");
76 reaction = allow;
77 break;
78 case kIOMessageSystemWillNotSleep:
79 debug("powerwatch", "system will not sleep");
80 reaction = ignore;
81 break;
82 case kIOMessageCanSystemPowerOff:
83 debug("powerwatch", "can system power off");
84 reaction = allow;
85 break;
86 default:
87 debug("powerwatch",
88 "type 0x%x message received (ignored)", messageType);
89 reaction = ignore;
90 break;
91 }
92
93 // handle acknowledgments
94 switch (reaction) {
95 case allow:
96 debug("powerwatch", "calling IOAllowPowerChange");
97 IOAllowPowerChange(me->mKernelPort, long(argument));
98 break;
99 case refuse:
100 debug("powerwatch", "calling IOCancelPowerChange");
101 IOCancelPowerChange(me->mKernelPort, long(argument));
102 break;
103 case ignore:
104 debug("powerwatch", "sending no response");
105 break;
106 }
107 }
108
109
110 //
111 // The default NULL implementations of the callback virtuals.
112 // We define these (rather than leaving them abstract) since
113 // many users want only one of these events.
114 //
115 void PowerWatcher::systemWillSleep()
116 { }
117
118 void PowerWatcher::systemIsWaking()
119 { }
120
121 void PowerWatcher::systemWillPowerDown()
122 { }
123
124
125 //
126 // The MachServer hookup
127 //
128 PortPowerWatcher::PortPowerWatcher()
129 {
130 port(IONotificationPortGetMachPort(mPortRef));
131 }
132
133 PortPowerWatcher::~PortPowerWatcher()
134 {
135 }
136
137 boolean_t PortPowerWatcher::handle(mach_msg_header_t *in)
138 {
139 IODispatchCalloutFromMessage(NULL, in, mPortRef);
140 return TRUE;
141 }
142
143
144 } // end namespace MachPlusPlus
145
146 } // end namespace Security