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 <security_utilities/machserver.h>
32#include <IOKit/pwr_mgt/IOPMLib.h>
33#include <IOKit/pwr_mgt/IOPMLibPrivate.h>
34
35
36namespace Security {
37namespace MachPlusPlus {
38
39
40//
41// PowerWatcher embodies the ability to respond to power events.
42// By itself, it is inert - nobody will call its virtual methods.
43// Use one of it subclasses, which take care of "hooking" into an
44// event delivery mechanism.
45//
46class PowerWatcher {
47public:
48    virtual ~PowerWatcher();
49
50public:
51    virtual void systemWillSleep();
52    virtual void systemIsWaking();
53    virtual void systemWillPowerDown();
54	virtual void systemWillPowerOn();
55
56    bool inDarkWake() { return mInDarkWake; }
57
58 protected:
59    bool mInDarkWake;
60};
61
62
63//
64// A PowerWatcher that is dispatches events from an IOKit message
65//
66class IOPowerWatcher : public PowerWatcher {
67public:
68	IOPowerWatcher();
69	~IOPowerWatcher();
70
71protected:
72    io_connect_t mKernelPort;
73    IONotificationPortRef mPortRef;
74    io_object_t mHandle;
75    IOPMConnection mIOPMconn;
76    dispatch_queue_t mIOPMqueue;
77    dispatch_group_t mDarkWakeGroup;
78
79    static void ioCallback(void *refCon, io_service_t service,
80        natural_t messageType, void *argument);
81
82    static void
83    iopmcallback(void * param,  IOPMConnection connection,
84		 IOPMConnectionMessageToken token,
85		 IOPMSystemPowerStateCapabilities capabilities);
86
87    void setupDarkWake();
88
89};
90
91
92//
93// Hook into a "raw" MachServer object for event delivery
94//
95class PortPowerWatcher : public IOPowerWatcher, public MachServer::NoReplyHandler {
96public:
97    PortPowerWatcher();
98
99    boolean_t handle(mach_msg_header_t *in);
100};
101
102
103//
104// Someone should add a RunLoopPowerWatcher class here, I suppose.
105// Well, if you need one: Tag, You're It!
106//
107
108
109} // end namespace MachPlusPlus
110
111} // end namespace Security
112
113#endif //_H_POWERWATCH
114