• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "system_ability_load_callback_stub.h"
26 
27 namespace OHOS::testserver {
28     using namespace OHOS::HiviewDFX;
29     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD003110, "TestServerClient"};
30     static constexpr int32_t TESTSERVER_LOAD_TIMEOUT_MS = 4000; // ms
31 
GetInstance()32     TestServerClient &TestServerClient::GetInstance()
33     {
34         HiLog::Debug(LABEL, "%{public}s called. ", __func__);
35         static TestServerClient testServerClient;
36         return testServerClient;
37     }
38 
39     class TestServerLoadCallback : public SystemAbilityLoadCallbackStub {
40     public:
TestServerLoadCallback(int32_t systemAbilityId)41         explicit TestServerLoadCallback(int32_t systemAbilityId) : systemAbilityId_(systemAbilityId){};
42 
InitLoadState()43         void InitLoadState()
44         {
45             std::unique_lock<std::mutex> lock(locatorMutex_);
46             loadState_ = false;
47         }
48 
WaitLoadStateChange(int32_t systemAbilityId)49         bool WaitLoadStateChange(int32_t systemAbilityId)
50         {
51             std::unique_lock<std::mutex> lock(locatorMutex_);
52             auto wait = locatorCond_.wait_for(lock, std::chrono::milliseconds(TESTSERVER_LOAD_TIMEOUT_MS), [this] {
53                 return loadState_ == true;
54             });
55             if (!wait) {
56                 HiLog::Error(LABEL, "%{public}s. Locator SystemAbility [%{public}d] time out.",
57                              __func__, systemAbilityId);
58                 return false;
59             }
60             return true;
61         }
62 
GetTestServerObject()63         sptr<IRemoteObject> GetTestServerObject()
64         {
65             return remoteObject_;
66         }
67 
68     private:
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)69         void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override
70         {
71             HiLog::Debug(LABEL, "%{public}s. Load SystemAbility success, systemAbilityId = [%{public}d]",
72                          __func__, systemAbilityId);
73             if (systemAbilityId == systemAbilityId_) {
74                 std::unique_lock<std::mutex> lock(locatorMutex_);
75                 loadState_ = true;
76                 remoteObject_ = remoteObject;
77                 locatorCond_.notify_one();
78             }
79         }
80 
OnLoadSystemAbilityFail(int32_t systemAbilityId)81         void OnLoadSystemAbilityFail(int32_t systemAbilityId) override
82         {
83             HiLog::Debug(LABEL, "%{public}s. Load SystemAbility failed, systemAbilityId = [%{public}d]",
84                          __func__, systemAbilityId);
85             std::unique_lock<std::mutex> lock(locatorMutex_);
86             loadState_ = false;
87             locatorCond_.notify_one();
88         }
89 
90         int32_t systemAbilityId_;
91         std::condition_variable locatorCond_;
92         std::mutex locatorMutex_;
93         bool loadState_ = false;
94         sptr<IRemoteObject> remoteObject_ = nullptr;
95     };
96 
TestServerClient()97     TestServerClient::TestServerClient()
98     {
99         iTestServerInterface_ = LoadTestServer();
100     }
101 
LoadTestServer()102     sptr<ITestServerInterface> TestServerClient::LoadTestServer()
103     {
104         const int32_t systemAbilityId = TEST_SERVER_SA_ID;
105         HiLog::Debug(LABEL, "%{public}s called. SystemAbility [%{public}d] loading", __func__, systemAbilityId);
106         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
107         if (samgr == nullptr) {
108             HiLog::Error(LABEL, "%{public}s. Get SystemAbility Manager failed!", __func__);
109             return nullptr;
110         }
111         auto object = samgr->CheckSystemAbility(systemAbilityId);
112         if (object != nullptr) {
113             HiLog::Debug(LABEL, "%{public}s. CheckSystemAbility [%{public}d] SUCCESS", __func__, systemAbilityId);
114             remoteObject_ = object;
115         } else {
116             auto testServerLoadCallback = sptr<TestServerLoadCallback>(new TestServerLoadCallback(systemAbilityId));
117             testServerLoadCallback->InitLoadState();
118             int32_t ret = samgr->LoadSystemAbility(systemAbilityId, testServerLoadCallback);
119             if (ret != ERR_NONE) {
120                 HiLog::Error(LABEL, "%{public}s. LoadSystemAbility [%{public}d] FAILED, ret %{public}d",
121                              __func__, systemAbilityId, ret);
122                 return nullptr;
123             }
124             if (testServerLoadCallback->WaitLoadStateChange(systemAbilityId)) {
125                 HiLog::Debug(LABEL, "%{public}s. LoadSystemAbility [%{public}d] SUCCESS", __func__, systemAbilityId);
126                 remoteObject_ = testServerLoadCallback->GetTestServerObject();
127             }
128         }
129         if (remoteObject_ == nullptr) {
130             HiLog::Error(LABEL, "%{public}s. Get SystemAbility [%{public}d] remoteObject FAILED",
131                          __func__, systemAbilityId);
132             return nullptr;
133         }
134         sptr<ITestServerInterface> iTestServerInterface = iface_cast<TestServerInterfaceProxy>(remoteObject_);
135         if (iTestServerInterface == nullptr) {
136             HiLog::Error(LABEL, "%{public}s. Get SystemAbility [%{public}d] proxy FAILED", __func__, systemAbilityId);
137             return nullptr;
138         }
139         sptr<SessionToken> sessionToken = new (std::nothrow) SessionToken();
140         if (sessionToken == nullptr || iTestServerInterface->CreateSession(*sessionToken) != TEST_SERVER_OK) {
141             HiLog::Error(LABEL, "%{public}s. Create session FAILED", __func__);
142             return nullptr;
143         }
144         return iTestServerInterface;
145     }
146 
SetPasteData(std::string text)147     int32_t TestServerClient::SetPasteData(std::string text)
148     {
149         HiLog::Info(LABEL, "%{public}s called.", __func__);
150         if (iTestServerInterface_ == nullptr) {
151             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
152             return TEST_SERVER_GET_INTERFACE_FAILED;
153         }
154         return iTestServerInterface_->SetPasteData(text);
155     }
156 
ChangeWindowMode(int windowId,uint32_t mode)157     int32_t TestServerClient::ChangeWindowMode(int windowId, uint32_t mode)
158     {
159         HiLog::Info(LABEL, "%{public}s called.", __func__);
160         if (iTestServerInterface_ == nullptr) {
161             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
162             return TEST_SERVER_GET_INTERFACE_FAILED;
163         }
164         return iTestServerInterface_->ChangeWindowMode(windowId, mode);
165     }
166 
TerminateWindow(int windowId)167     int32_t TestServerClient::TerminateWindow(int windowId)
168     {
169         HiLog::Info(LABEL, "%{public}s called.", __func__);
170         if (iTestServerInterface_ == nullptr) {
171             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
172             return TEST_SERVER_GET_INTERFACE_FAILED;
173         }
174         return iTestServerInterface_->TerminateWindow(windowId);
175     }
176 
MinimizeWindow(int windowId)177     int32_t TestServerClient::MinimizeWindow(int windowId)
178     {
179         HiLog::Info(LABEL, "%{public}s called.", __func__);
180         if (iTestServerInterface_ == nullptr) {
181             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
182             return TEST_SERVER_GET_INTERFACE_FAILED;
183         }
184         return iTestServerInterface_->MinimizeWindow(windowId);
185     }
186 
PublishCommonEvent(const EventFwk::CommonEventData & event)187     bool TestServerClient::PublishCommonEvent(const EventFwk::CommonEventData &event)
188     {
189         HiLog::Info(LABEL, "%{public}s called.", __func__);
190         if (iTestServerInterface_ == nullptr) {
191             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
192             return TEST_SERVER_GET_INTERFACE_FAILED;
193         }
194         bool result = false;
195         auto ret = iTestServerInterface_->PublishCommonEvent(event, result);
196         HiLog::Info(LABEL, "%{public}s ipc ret = %{public}d.", __func__, ret);
197         return result;
198     }
199 
FrequencyLock()200     void TestServerClient::FrequencyLock()
201     {
202         if (iTestServerInterface_ == nullptr) {
203             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
204             return;
205         }
206         iTestServerInterface_->FrequencyLock();
207         return;
208     }
209 
SpDaemonProcess(int daemonCommand,std::string extraInfo)210     int32_t TestServerClient::SpDaemonProcess(int daemonCommand, std::string extraInfo)
211     {
212         if (iTestServerInterface_ == nullptr) {
213             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
214             return TEST_SERVER_GET_INTERFACE_FAILED;
215         }
216 
217         return iTestServerInterface_->SpDaemonProcess(daemonCommand, extraInfo);
218     }
219 
CollectProcessMemory(int32_t & pid,ProcessMemoryInfo & processMemoryInfo)220     int32_t TestServerClient::CollectProcessMemory(int32_t &pid, ProcessMemoryInfo &processMemoryInfo)
221     {
222         HiLog::Debug(LABEL, "%{public}s called.", __func__);
223         if (iTestServerInterface_ == nullptr) {
224             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
225             return TEST_SERVER_GET_INTERFACE_FAILED;
226         }
227         return iTestServerInterface_->CollectProcessMemory(pid, processMemoryInfo);
228     }
229 
CollectProcessCpu(int32_t & pid,bool isNeedUpdate,ProcessCpuInfo & processCpuInfo)230     int32_t TestServerClient::CollectProcessCpu(int32_t &pid, bool isNeedUpdate, ProcessCpuInfo &processCpuInfo)
231     {
232         HiLog::Debug(LABEL, "%{public}s called.", __func__);
233         if (iTestServerInterface_ == nullptr) {
234             HiLog::Error(LABEL, "%{public}s. Get iTestServerInterface FAILED", __func__);
235             return TEST_SERVER_GET_INTERFACE_FAILED;
236         }
237         return iTestServerInterface_->CollectProcessCpu(pid, isNeedUpdate, processCpuInfo);
238     }
239 } // namespace OHOS::testserver
240 
FrequencyLockPlugin()241 void FrequencyLockPlugin()
242 {
243     OHOS::testserver::TestServerClient::GetInstance().FrequencyLock();
244 }