1 /*
2 * Copyright (C) 2021 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 "foo_service.h"
17 #include "ipc_debug.h"
18 #include "ipc_types.h"
19
20 namespace OHOS {
21 std::mutex FooStub::decTimeMutex_;
22 int FooStub::decTimes_ = 0;
23
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int FooStub::OnRemoteRequest(uint32_t code,
25 MessageParcel& data, MessageParcel& reply, MessageOption& option)
26 {
27 int result = ERR_NONE;
28
29 switch (code) {
30 case GET_FOO_NAME: {
31 ZLOGD(LABEL, "%{public}s:called\n", __func__);
32 reply.WriteString(GetFooName());
33 break;
34 }
35 case SEND_ASYNC_REPLY: {
36 int32_t replyData = data.ReadInt32();
37 SendAsyncReply(replyData);
38 break;
39 }
40 case SEND_WRONG_REPLY: {
41 return TestNestingSend(data.ReadInt32());
42 }
43 default:
44 result = ERR_TRANSACTION_FAILED;
45 break;
46 }
47
48 return result;
49 }
50
GetFooName()51 std::string FooStub::GetFooName()
52 {
53 return "ReallFoo";
54 }
55
WaitForAsyncReply(int timeout)56 int FooStub::WaitForAsyncReply(int timeout)
57 {
58 asyncReply_ = 0;
59 std::unique_lock<std::mutex> lck(mutex_);
60 cv_.wait_for(lck, std::chrono::milliseconds(timeout), [&]() {
61 return asyncReply_ != 0;
62 });
63 return asyncReply_;
64 }
65
SendAsyncReply(int & replyValue)66 void FooStub::SendAsyncReply(int &replyValue)
67 {
68 std::unique_lock<std::mutex> lck(mutex_);
69 asyncReply_ = replyValue;
70 cv_.notify_all();
71 }
72
~FooStub()73 FooStub::~FooStub()
74 {
75 std::unique_lock<std::mutex> lck(decTimeMutex_);
76 decTimes_++;
77 }
78
CleanDecTimes()79 void FooStub::CleanDecTimes()
80 {
81 std::unique_lock<std::mutex> lck(decTimeMutex_);
82 decTimes_ = 0;
83 }
84
GetDecTimes()85 int FooStub::GetDecTimes()
86 {
87 std::unique_lock<std::mutex> lck(decTimeMutex_);
88 return decTimes_;
89 }
90
TestNestingSend(int sendCode)91 int FooStub::TestNestingSend(int sendCode)
92 {
93 return sendCode;
94 }
FooProxy(const sptr<IRemoteObject> & impl)95 FooProxy::FooProxy(const sptr<IRemoteObject> &impl)
96 : IRemoteProxy<IFoo>(impl)
97 {
98 }
99
GetFooName()100 std::string FooProxy::GetFooName()
101 {
102 ZLOGD(LABEL, "%{public}s:called\n", __func__);
103 MessageParcel data, reply;
104 MessageOption option;
105 Remote()->SendRequest(GET_FOO_NAME, data, reply, option);
106 return reply.ReadString();
107 }
108
SendAsyncReply(int & replyValue)109 void FooProxy::SendAsyncReply(int &replyValue)
110 {
111 ZLOGD(LABEL, "%{public}s:called\n", __func__);
112 MessageParcel data, reply;
113 MessageOption option = { MessageOption::TF_ASYNC };
114 data.WriteInt32(replyValue);
115 Remote()->SendRequest(SEND_ASYNC_REPLY, data, reply, option);
116 }
117
TestNestingSend(int sendCode)118 int FooProxy::TestNestingSend(int sendCode)
119 {
120 MessageOption option;
121 MessageParcel dataParcel, replyParcel;
122 if (!dataParcel.WriteInt32(sendCode)) {
123 return -1;
124 }
125 int error = Remote()->SendRequest(SEND_WRONG_REPLY, dataParcel, replyParcel, option);
126 ZLOGE(LABEL, "send foo result = %{public}d", error);
127 return error;
128 }
129 } // namespace OHOS