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