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