1 /*
2  * Copyright (c) 2024 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_server_client.h"
17 
18 #include "hilog/log.h"
19 #include "iremote_broker.h"
20 #include "iservice_registry.h"
21 #include "test_server_interface_proxy.h"
22 #include "system_ability_definition.h"
23 #include "session_token.h"
24 #include "test_server_error_code.h"
25 
26 namespace OHOS::testserver {
27     using namespace OHOS::HiviewDFX;
28     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD003110, "TestServerClient"};
29     static constexpr int32_t TESTSERVER_LOAD_TIMEOUT_MS = 1000; // ms
30 
GetInstance()31     TestServerClient &TestServerClient::GetInstance()
32     {
33         HiLog::Info(LABEL, "%{public}s called. ", __func__);
34         static TestServerClient testServerClient;
35         return testServerClient;
36     }
37 
38     class TestServerLoadCallback : public SystemAbilityLoadCallbackStub {
39     public:
TestServerLoadCallback(int32_t systemAbilityId)40         explicit TestServerLoadCallback(int32_t systemAbilityId) : systemAbilityId_(systemAbilityId){};
41 
InitLoadState()42         void InitLoadState()
43         {
44             std::unique_lock<std::mutex> lock(locatorMutex_);
45             loadState_ = false;
46         }
47 
WaitLoadStateChange(int32_t systemAbilityId)48         bool WaitLoadStateChange(int32_t systemAbilityId)
49         {
50             std::unique_lock<std::mutex> lock(locatorMutex_);
51             auto wait = locatorCond_.wait_for(lock, std::chrono::milliseconds(TESTSERVER_LOAD_TIMEOUT_MS), [this] {
52                 return loadState_ == true;
53             });
54             if (!wait) {
55                 HiLog::Error(LABEL, "%{public}s. Locator SystemAbility [%{public}d] time out.",
56                              __func__, systemAbilityId);
57                 return false;
58             }
59             return true;
60         }
61 
GetTestServerObject()62         sptr<IRemoteObject> GetTestServerObject()
63         {
64             return remoteObject_;
65         }
66 
67     private:
68         void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override
69         {
70             HiLog::Info(LABEL, "%{public}s. Load SystemAbility success, systemAbilityId = [%{public}d]",
71                         __func__, systemAbilityId);
72             if (systemAbilityId == systemAbilityId_) {
73                 std::unique_lock<std::mutex> lock(locatorMutex_);
74                 loadState_ = true;
75                 remoteObject_ = remoteObject;
76                 locatorCond_.notify_one();
77             }
78         }
79 
80         void OnLoadSystemAbilityFail(int32_t systemAbilityId) override
81         {
82             HiLog::Info(LABEL, "%{public}s. Load SystemAbility failed, systemAbilityId = [%{public}d]",
83                         __func__, systemAbilityId);
84             std::unique_lock<std::mutex> lock(locatorMutex_);
85             loadState_ = false;
86             locatorCond_.notify_one();
87         }
88 
89         int32_t systemAbilityId_;
90         std::condition_variable locatorCond_;
91         std::mutex locatorMutex_;
92         bool loadState_ = false;
93         sptr<IRemoteObject> remoteObject_ = nullptr;
94     };
95 
LoadTestServer()96     sptr<ITestServerInterface> TestServerClient::LoadTestServer()
97     {
98         const int32_t systemAbilityId = TEST_SERVER_SA_ID;
99         HiLog::Info(LABEL, "%{public}s called. SystemAbility [%{public}d] loading", __func__, systemAbilityId);
100         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
101         if (samgr == nullptr) {
102             HiLog::Error(LABEL, "%{public}s. Get SystemAbility Manager failed!", __func__);
103             return nullptr;
104         }
105         auto object = samgr->CheckSystemAbility(systemAbilityId);
106         if (object != nullptr) {
107             HiLog::Info(LABEL, "%{public}s. CheckSystemAbility [%{public}d] SUCCESS", __func__, systemAbilityId);
108             remoteObject_ = object;
109         } else {
110             auto testServerLoadCallback = sptr<TestServerLoadCallback>(new TestServerLoadCallback(systemAbilityId));
111             testServerLoadCallback->InitLoadState();
112             int32_t ret = samgr->LoadSystemAbility(systemAbilityId, testServerLoadCallback);
113             if (ret != ERR_NONE) {
114                 HiLog::Error(LABEL, "%{public}s. LoadSystemAbility [%{public}d] FAILED, ret %{public}d",
115                              __func__, systemAbilityId, ret);
116                 return nullptr;
117             }
118             if (testServerLoadCallback->WaitLoadStateChange(systemAbilityId)) {
119                 HiLog::Info(LABEL, "%{public}s. LoadSystemAbility [%{public}d] SUCCESS", __func__, systemAbilityId);
120                 remoteObject_ = testServerLoadCallback->GetTestServerObject();
121             }
122         }
123         if (remoteObject_ == nullptr) {
124             HiLog::Error(LABEL, "%{public}s. Get SystemAbility [%{public}d] remoteObject FAILED",
125                          __func__, systemAbilityId);
126             return nullptr;
127         }
128         sptr<ITestServerInterface> iTestServerInterface = iface_cast<TestServerInterfaceProxy>(remoteObject_);
129         if (iTestServerInterface == nullptr) {
130             HiLog::Error(LABEL, "%{public}s. Get SystemAbility [%{public}d] proxy FAILED", __func__, systemAbilityId);
131             return nullptr;
132         }
133         sptr<SessionToken> sessionToken = new (std::nothrow) SessionToken();
134         if (iTestServerInterface->CreateSession(*sessionToken) != TEST_SERVER_OK) {
135             HiLog::Error(LABEL, "%{public}s. Create session FAILED", __func__);
136             return nullptr;
137         }
138         return iTestServerInterface;
139     }
140 
SetPasteData(std::string text)141     int32_t TestServerClient::SetPasteData(std::string text)
142     {
143         HiLog::Info(LABEL, "%{public}s called.", __func__);
144         auto iTestServerInterface = LoadTestServer();
145         if (iTestServerInterface == nullptr) {
146             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
147             return TEST_SERVER_GET_INTERFACE_FAILED;
148         }
149         return iTestServerInterface->SetPasteData(text);
150     }
151 
PublishCommonEvent(const EventFwk::CommonEventData &event)152     bool TestServerClient::PublishCommonEvent(const EventFwk::CommonEventData &event)
153     {
154         HiLog::Info(LABEL, "%{public}s called.", __func__);
155         auto iTestServerInterface = LoadTestServer();
156         if (iTestServerInterface == nullptr) {
157             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
158             return TEST_SERVER_GET_INTERFACE_FAILED;
159         }
160         bool result = false;
161         iTestServerInterface->PublishCommonEvent(event, result);
162         return result;
163     }
164 } // namespace OHOS::testserver