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 #include "power_mgr_async_reply.h"
17 #include "power_common.h"
18 
19 namespace OHOS {
20 namespace PowerMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)21 int PowerMgrStubAsync::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
22 {
23     POWER_HILOGI(COMP_FWK, "code=%{public}u, flags=%{public}d", code, option.GetFlags());
24     int result = ERR_OK;
25     switch (code) {
26         case SEND_ASYNC_REPLY: {
27             int32_t replyData = data.ReadInt32();
28             SendAsyncReply(replyData);
29             break;
30         }
31         default:
32             result = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
33             break;
34     }
35 
36     return result;
37 }
38 
SendAsyncReply(int replyValue)39 void PowerMgrStubAsync::SendAsyncReply(int replyValue)
40 {
41     std::unique_lock<std::mutex> lck(mutex_);
42     asyncReply_ = replyValue;
43     notified = true;
44     lck.unlock();
45     cv_.notify_all();
46 }
47 
WaitForAsyncReply(int timeout)48 int PowerMgrStubAsync::WaitForAsyncReply(int timeout)
49 {
50     std::unique_lock<std::mutex> lck(mutex_);
51     cv_.wait_for(lck, std::chrono::milliseconds(timeout), [this]() {
52         return notified;
53     });
54     return asyncReply_;
55 }
56 
SendAsyncReply(int replyValue)57 void PowerMgrProxyAsync::SendAsyncReply(int replyValue)
58 {
59     sptr<IRemoteObject> remote = Remote();
60     RETURN_IF(remote == nullptr);
61     MessageParcel data;
62     MessageParcel reply;
63     MessageOption option = { MessageOption::TF_ASYNC };
64     data.WriteInt32(replyValue);
65     remote->SendRequest(SEND_ASYNC_REPLY, data, reply, option);
66 }
67 } // namespace PowerMgr
68 } // namespace OHOS