]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/powerwatch.cpp
Security-28.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 switch (messageType) {
54 case kIOMessageSystemWillSleep:
55 debug("powerwatch", "system will sleep");
56 me->systemWillSleep();
57 break;
58 case kIOMessageSystemHasPoweredOn:
59 debug("powerwatch", "system has powered on");
60 me->systemIsWaking();
61 break;
62 case kIOMessageSystemWillPowerOff:
63 debug("powerwatch", "system will power off");
64 me->systemWillPowerDown();
65 break;
66
67 #if !defined(NDEBUG)
68 case kIOMessageSystemWillNotPowerOff:
69 debug("powerwatch", "system will not power off");
70 break;
71 case kIOMessageCanSystemSleep:
72 debug("powerwatch", "can system sleep");
73 break;
74 case kIOMessageSystemWillNotSleep:
75 debug("powerwatch", "system will not sleep");
76 break;
77 case kIOMessageCanSystemPowerOff:
78 debug("powerwatch", "can system power off");
79 break;
80 default:
81 debug("powerwatch",
82 "type 0x%x message received (ignored)", messageType);
83 break;
84 #endif //NDEBUG
85 }
86
87 // always confirm
88 IOAllowPowerChange(me->mKernelPort, long(argument));
89 }
90
91
92 //
93 // The default NULL implementations of the callback virtuals.
94 // We define these (rather than leaving them abstract) since
95 // many users want only one of these events.
96 //
97 void PowerWatcher::systemWillSleep()
98 { }
99
100 void PowerWatcher::systemIsWaking()
101 { }
102
103 void PowerWatcher::systemWillPowerDown()
104 { }
105
106
107 //
108 // The MachServer hookup
109 //
110 PortPowerWatcher::PortPowerWatcher()
111 {
112 port(IONotificationPortGetMachPort(mPortRef));
113 }
114
115 PortPowerWatcher::~PortPowerWatcher()
116 {
117 }
118
119 boolean_t PortPowerWatcher::handle(mach_msg_header_t *in)
120 {
121 IODispatchCalloutFromMessage(NULL, in, mPortRef);
122 return TRUE;
123 }
124
125
126 } // end namespace MachPlusPlus
127
128 } // end namespace Security