1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef POWERMGR_SERVICES_ASYNC_REPLY_H
17 #define POWERMGR_SERVICES_ASYNC_REPLY_H
18 
19 #include <mutex>
20 #include <condition_variable>
21 #include <nocopyable.h>
22 #include "iremote_broker.h"
23 #include "iremote_stub.h"
24 #include "iremote_proxy.h"
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 class IPowerMgrAsync : public IRemoteBroker {
29 public:
30     enum PowerInterfaceId {
31         SEND_ASYNC_REPLY = 0,
32     };
33     virtual void SendAsyncReply(int reply) = 0;
34 
35     DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IPowerMgrAsync");
36 };
37 
38 class PowerMgrStubAsync : public IRemoteStub<IPowerMgrAsync> {
39 public:
40     DISALLOW_COPY_AND_MOVE(PowerMgrStubAsync);
41     PowerMgrStubAsync() = default;
42     ~PowerMgrStubAsync() override = default;
43     int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
44     void SendAsyncReply(int reply) override;
45     int WaitForAsyncReply(int timeout);
46 private:
47     std::mutex mutex_;
48     std::condition_variable cv_;
49     int asyncReply_ = { 0 };
50     bool notified {false};
51 };
52 
53 class PowerMgrProxyAsync : public IRemoteProxy<IPowerMgrAsync> {
54 public:
PowerMgrProxyAsync(const sptr<IRemoteObject> & impl)55     explicit PowerMgrProxyAsync(const sptr<IRemoteObject>& impl)
56         : IRemoteProxy<IPowerMgrAsync>(impl) {}
57     ~PowerMgrProxyAsync() = default;
58     DISALLOW_COPY_AND_MOVE(PowerMgrProxyAsync);
59 
60     void SendAsyncReply(int reply) override;
61 private:
62     static inline BrokerDelegator<PowerMgrProxyAsync> delegator_;
63 };
64 } // namespace PowerMgr
65 } // namespace OHOS
66 #endif // POWERMGR_SERVICES_ASYNC_REPLY_H