1 /*
2 * Copyright (c) 2022 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 "test_client.h"
17
18 #include "if_system_ability_manager.h"
19 #include "ipc_debug.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
ConnectService()25 int TestClient::ConnectService()
26 {
27 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
28 if (saMgr == nullptr) {
29 ZLOGE(LABEL, "get registry fail");
30 return -1;
31 }
32
33 sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
34 if (object != nullptr) {
35 ZLOGE(LABEL, "Got test Service object");
36 testService_ = (new (std::nothrow) IdlTestServiceProxy(object));
37 }
38
39 if (testService_ == nullptr) {
40 ZLOGE(LABEL, "Could not find Test Service!");
41 return -1;
42 }
43
44 return 0;
45 }
46
StartIntTransaction()47 void TestClient::StartIntTransaction()
48 {
49 if (testService_ != nullptr) {
50 ZLOGE(LABEL, "StartIntTransaction");
51 [[maybe_unused]] int result = 0;
52 testService_->TestIntTransaction(1234); // 1234 : test number
53 ZLOGE(LABEL, "Rec result from server %{public}d.", result);
54 }
55 }
56
StartStringTransaction()57 void TestClient::StartStringTransaction()
58 {
59 if (testService_ != nullptr) {
60 ZLOGI(LABEL, "StartIntTransaction");
61 testService_->TestStringTransaction("IDL Test");
62 }
63 }
64 } // namespace OHOS
65