• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             reply.WriteString(GetFooName());
32             break;
33         }
34         case SEND_ASYNC_REPLY: {
35             int32_t replyData = data.ReadInt32();
36             SendAsyncReply(replyData);
37             break;
38         }
39         case SEND_WRONG_REPLY: {
40             return TestNestingSend(data.ReadInt32());
41         }
42         default:
43             result = ERR_TRANSACTION_FAILED;
44             break;
45     }
46 
47     return result;
48 }
49 
GetFooName()50 std::string FooStub::GetFooName()
51 {
52     return "ReallFoo";
53 }
54 
WaitForAsyncReply(int timeout)55 int FooStub::WaitForAsyncReply(int timeout)
56 {
57     asyncReply_ = 0;
58     std::unique_lock<std::mutex> lck(mutex_);
59     cv_.wait_for(lck, std::chrono::milliseconds(timeout), [this]() {
60         return this->asyncReply_ != 0;
61     });
62     return asyncReply_;
63 }
64 
SendAsyncReply(int & replyValue)65 void FooStub::SendAsyncReply(int &replyValue)
66 {
67     std::unique_lock<std::mutex> lck(mutex_);
68     asyncReply_ = replyValue;
69     cv_.notify_all();
70 }
71 
~FooStub()72 FooStub::~FooStub()
73 {
74     std::unique_lock<std::mutex> lck(decTimeMutex_);
75     decTimes_++;
76 }
77 
CleanDecTimes()78 void FooStub::CleanDecTimes()
79 {
80     std::unique_lock<std::mutex> lck(decTimeMutex_);
81     decTimes_ = 0;
82 }
83 
GetDecTimes()84 int FooStub::GetDecTimes()
85 {
86     std::unique_lock<std::mutex> lck(decTimeMutex_);
87     return decTimes_;
88 }
89 
TestNestingSend(int sendCode)90 int FooStub::TestNestingSend(int sendCode)
91 {
92     return sendCode;
93 }
FooProxy(const sptr<IRemoteObject> & impl)94 FooProxy::FooProxy(const sptr<IRemoteObject> &impl)
95     : IRemoteProxy<IFoo>(impl)
96 {
97 }
98 
GetFooName()99 std::string FooProxy::GetFooName()
100 {
101     ZLOGI(LABEL, "Start");
102     MessageParcel data, reply;
103     MessageOption option;
104     Remote()->SendRequest(GET_FOO_NAME, data, reply, option);
105     return reply.ReadString();
106 }
107 
SendAsyncReply(int & replyValue)108 void FooProxy::SendAsyncReply(int &replyValue)
109 {
110     ZLOGI(LABEL, "Start");
111     MessageParcel data, reply;
112     MessageOption option = { MessageOption::TF_ASYNC };
113     data.WriteInt32(replyValue);
114     Remote()->SendRequest(SEND_ASYNC_REPLY, data, reply, option);
115 }
116 
TestNestingSend(int sendCode)117 int FooProxy::TestNestingSend(int sendCode)
118 {
119     MessageOption option;
120     MessageParcel dataParcel, replyParcel;
121     if (!dataParcel.WriteInt32(sendCode)) {
122         return -1;
123     }
124     int error = Remote()->SendRequest(SEND_WRONG_REPLY, dataParcel, replyParcel, option);
125     ZLOGE(LABEL, "Send foo result = %{public}d", error);
126     return error;
127 }
128 } // namespace OHOS