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 "dbinder_test_service.h"
17 #include <unistd.h>
18 #include <string>
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "log_tags.h"
24
25 static std::string g_dbinderTestServerName = "dbinderTestServer";
26
27 namespace OHOS {
28 using namespace OHOS::HiviewDFX;
29 int DBinderTestService::destructTimes_ = 0;
30 std::mutex DBinderTestService::destructTimesMutex_;
31
32 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "DbinderTest" };
33
Reverse(int x)34 static int Reverse(int x)
35 {
36 int result = 0;
37 while (x != 0) {
38 result = result * 10 + x % 10;
39 x = x / 10;
40 }
41 return result;
42 }
43
~DBinderTestService()44 DBinderTestService::~DBinderTestService()
45 {
46 DBINDER_LOGI(LOG_LABEL, "DBinderTestService finish");
47 std::lock_guard<std::mutex> lockGuard(destructTimesMutex_);
48
49 destructTimes_++;
50 }
51
Instantiate()52 int DBinderTestService::Instantiate()
53 {
54 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
55 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56 if (saMgr == nullptr) {
57 DBINDER_LOGE(LOG_LABEL, "%{public}s:fail to instantiate", __func__);
58 return -ENODEV;
59 }
60
61 ISystemAbilityManager::SAExtraProp saExtra;
62 saExtra.isDistributed = true;
63 #ifdef DBINDER_TEST_SECOND
64 int result = saMgr->AddSystemAbility(RPC_TEST_SERVICE2, new DBinderTestService(), saExtra);
65 #else
66 int result = saMgr->AddSystemAbility(RPC_TEST_SERVICE, new DBinderTestService(), saExtra);
67 #endif
68 DBINDER_LOGE(LOG_LABEL, "%{public}s: add TestService result=%{public}d", __func__, result);
69
70 return result;
71 }
72
ReverseInt(int data,int & rep)73 int DBinderTestService::ReverseInt(int data, int &rep)
74 {
75 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
76 rep = Reverse(data);
77 DBINDER_LOGI(LOG_LABEL, "%{public}s:read from client data = %{public}d", __func__, data);
78 return ERR_NONE;
79 }
80
ReverseIntDelay(int data,int & rep)81 int DBinderTestService::ReverseIntDelay(int data, int &rep)
82 {
83 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
84 rep = Reverse(data);
85 DBINDER_LOGI(LOG_LABEL, "%{public}s:read from client data = %{public}d", __func__, data);
86 return ERR_NONE;
87 }
88
Delay(int data,int & rep)89 int DBinderTestService::Delay(int data, int &rep)
90 {
91 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
92 rep = data;
93 int i = 1;
94 while (i <= data) {
95 sleep(1);
96 DBINDER_LOGI(LOG_LABEL, "sleep loop : %{public}d", i);
97 i++;
98 }
99 DBINDER_LOGE(LOG_LABEL, "%{public}s:read from client data = %{public}d", __func__, data);
100 return ERR_NONE;
101 }
102
PingService(std::u16string & serviceName)103 int DBinderTestService::PingService(std::u16string &serviceName)
104 {
105 std::u16string localServiceName = GetDescriptor();
106 if (localServiceName.compare(serviceName) != 0) {
107 DBINDER_LOGE(LOG_LABEL, "ServiceName is not equal");
108 return -1;
109 }
110 return ERR_NONE;
111 }
112
TransProxyObject(int data,sptr<IRemoteObject> & transObject,int operation,int & rep,int & withdrawRes)113 int DBinderTestService::TransProxyObject(int data, sptr<IRemoteObject> &transObject, int operation, int &rep,
114 int &withdrawRes)
115 {
116 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
117 return 0;
118 }
119
TransProxyObjectAgain(int data,sptr<IRemoteObject> & transObject,int operation,int & rep,int & withdrawRes)120 int DBinderTestService::TransProxyObjectAgain(int data, sptr<IRemoteObject> &transObject, int operation, int &rep,
121 int &withdrawRes)
122 {
123 DBINDER_LOGI(LOG_LABEL, "enter");
124 return 0;
125 }
126
TransStubObject(int data,sptr<IRemoteObject> & transObject,int & rep,int & stubRep)127 int DBinderTestService::TransStubObject(int data, sptr<IRemoteObject> &transObject, int &rep, int &stubRep)
128 {
129 (void)transObject;
130 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
131 return 0;
132 }
133
TransOversizedPkt(const std::string & dataStr,std::string & repStr)134 int DBinderTestService::TransOversizedPkt(const std::string &dataStr, std::string &repStr)
135 {
136 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
137 return 0;
138 }
139
ProxyTransRawData(int length)140 int DBinderTestService::ProxyTransRawData(int length)
141 {
142 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
143 return 0;
144 }
145
StubTransRawData(int length)146 int DBinderTestService::StubTransRawData(int length)
147 {
148 (void)length;
149 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
150 return 0;
151 }
152
GetChildId(uint64_t & rep)153 int DBinderTestService::GetChildId(uint64_t &rep)
154 {
155 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
156 return 0;
157 }
158
FlushAsyncCommands(int count,int length)159 int DBinderTestService::FlushAsyncCommands(int count, int length)
160 {
161 DBINDER_LOGI(LOG_LABEL, "enter %{public}s", __func__);
162 return 0;
163 }
164
GetRemoteObject(int type)165 sptr<IRemoteObject> DBinderTestService::GetRemoteObject(int type)
166 {
167 DBINDER_LOGI(LOG_LABEL, "DBinderTestService GetRemoteObject");
168 if (type == IDBinderTestService::FIRST_OBJECT) {
169 return new DBinderTestService();
170 }
171
172 if (object_ == nullptr) {
173 object_ = new DBinderTestService();
174 return object_;
175 } else {
176 sptr<IRemoteObject> node = object_;
177 object_ = nullptr;
178 return node;
179 }
180 }
181
GetRemoteDecTimes()182 int DBinderTestService::GetRemoteDecTimes()
183 {
184 std::lock_guard<std::mutex> lockGuard(destructTimesMutex_);
185
186 DBINDER_LOGI(LOG_LABEL, "DBinderTestService GetDestructTimes");
187 return destructTimes_;
188 }
189
ClearRemoteDecTimes()190 void DBinderTestService::ClearRemoteDecTimes()
191 {
192 std::lock_guard<std::mutex> lockGuard(destructTimesMutex_);
193
194 DBINDER_LOGI(LOG_LABEL, "DBinderTestService ClearRemoteDecTimes");
195 destructTimes_ = 0;
196 }
197 } // namespace OHOS
198