]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/powerwatch.h
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / powerwatch.h
1 /*
2 * Copyright (c) 2000-2001,2003-2004,2011-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // powerwatch - hook into system notifications of power events
27 //
28 #ifndef _H_POWERWATCH
29 #define _H_POWERWATCH
30
31 #include <TargetConditionals.h>
32
33 #if TARGET_OS_OSX
34
35 #include <security_utilities/machserver.h>
36 #include <IOKit/pwr_mgt/IOPMLib.h>
37 #include <IOKit/pwr_mgt/IOPMLibPrivate.h>
38
39
40 namespace Security {
41 namespace MachPlusPlus {
42
43
44 //
45 // PowerWatcher embodies the ability to respond to power events.
46 // By itself, it is inert - nobody will call its virtual methods.
47 // Use one of it subclasses, which take care of "hooking" into an
48 // event delivery mechanism.
49 //
50 class PowerWatcher {
51 public:
52 virtual ~PowerWatcher();
53
54 public:
55 virtual void systemWillSleep();
56 virtual void systemIsWaking();
57 virtual void systemWillPowerDown();
58 virtual void systemWillPowerOn();
59
60 bool inDarkWake() { return mInDarkWake; }
61
62 protected:
63 bool mInDarkWake;
64 };
65
66
67 //
68 // A PowerWatcher that is dispatches events from an IOKit message
69 //
70 class IOPowerWatcher : public PowerWatcher {
71 public:
72 IOPowerWatcher();
73 ~IOPowerWatcher();
74
75 protected:
76 io_connect_t mKernelPort;
77 IONotificationPortRef mPortRef;
78 io_object_t mHandle;
79 IOPMConnection mIOPMconn;
80 dispatch_queue_t mIOPMqueue;
81 dispatch_group_t mDarkWakeGroup;
82 IOPMNotificationHandle mUserActiveHandle;
83
84 static void ioCallback(void *refCon, io_service_t service,
85 natural_t messageType, void *argument);
86
87 static void
88 iopmcallback(void * param, IOPMConnection connection,
89 IOPMConnectionMessageToken token,
90 IOPMSystemPowerStateCapabilities capabilities);
91
92 void setupDarkWake();
93
94 };
95
96
97 //
98 // Hook into a "raw" MachServer object for event delivery
99 //
100 class PortPowerWatcher : public IOPowerWatcher, public MachServer::NoReplyHandler {
101 public:
102 PortPowerWatcher();
103
104 boolean_t handle(mach_msg_header_t *in);
105 };
106
107
108 //
109 // Someone should add a RunLoopPowerWatcher class here, I suppose.
110 // Well, if you need one: Tag, You're It!
111 //
112
113
114 } // end namespace MachPlusPlus
115
116 } // end namespace Security
117
118 #endif //TARGET_OS_OSX
119
120 #endif //_H_POWERWATCH