1/*
2 * Copyright (c) 2019-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _IOKIT_UIOSERVICESTATEDISPATCHSOURCE_H
30#define _IOKIT_UIOSERVICESTATEDISPATCHSOURCE_H
31
32#include <DriverKit/IODispatchQueue.iig>
33#include <DriverKit/OSAction.iig>
34#include <DriverKit/IOService.iig>
35
36
37class NATIVE KERNEL IOServiceStateNotificationDispatchSource : public IODispatchSource
38{
39public:
40
41	virtual bool
42	init() override;
43
44	virtual void
45	free() override;
46
47    /*!
48     * @brief       Create an IOServiceStateNotificationDispatchSource for notification of IOService state events sent by the StateNotificationSet() api.
49     * @param       service The object hosting state, typically returned by IOService::CopySystemStateNotificationService().
50     * @param       items Array of state item names to be notified about.
51     * @param       queue IODispatchQueue the source is attached to. Note that the StateNotificationReady
52     *              handler is invoked on the queue set for the target method
53     *              of the OSAction, not this queue.
54     * @param       source Created source with +1 retain count to be released by the caller.
55     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
56     */
57	static kern_return_t
58	Create(IOService * service, OSArray * items, IODispatchQueue * queue, IOServiceStateNotificationDispatchSource ** source) LOCAL;
59
60
61    /*!
62     * @brief       Control the enable state of the notification.
63     * @param       enable Pass true to enable the source or false to disable.
64     * @param       handler Optional block to be executed after the interrupt has been disabled and any pending
65     *              interrupt handlers completed.
66     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
67     */
68	virtual kern_return_t
69	SetEnableWithCompletion(
70		bool enable,
71		IODispatchSourceCancelHandler handler) override LOCAL;
72
73    /*!
74     * @brief       Cancel all callbacks from the event source.
75     * @discussion  After cancellation, the source can only be freed. It cannot be reactivated.
76     * @param       handler Handler block to be invoked after any callbacks have completed.
77     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
78     */
79	virtual kern_return_t
80	Cancel(IODispatchSourceCancelHandler handler) override LOCAL;
81
82    /*!
83     * @brief       Set the handler block to run when the notification has become ready.
84     * @param       action OSAction instance specifying the callback method. The OSAction object will be retained
85     *              until SetHandler is called again or the event source is cancelled.
86     *              The StateNotificationReady handler is invoked on the queue set for the target method of the
87     *              OSAction.
88     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
89     */
90	virtual kern_return_t
91	SetHandler(
92	OSAction * action TYPE(StateNotificationReady));
93
94    /*!
95     * @brief       Rearm the notification.
96     * @discussion  Call this method in the implementation of your StateNotificationReady action
97     *              before reading any item values.
98     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
99     */
100	virtual kern_return_t
101	StateNotificationBegin(void);
102
103private:
104	virtual void
105	StateNotificationReady(
106		OSAction  * action TARGET) LOCAL = 0;
107};
108
109#endif /* ! _IOKIT_UIOSERVICESTATEDISPATCHSOURCE_H */
110