• 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_service.h"
17 
18 #include "iremote_object.h"
19 #include "system_ability_definition.h"
20 #include "hilog/log.h"
21 #include "parameters.h"
22 #include "iservice_registry.h"
23 #include "test_server_error_code.h"
24 #include "pasteboard_client.h"
25 #include "socperf_client.h"
26 
27 namespace OHOS::testserver {
28     // TEST_SERVER_SA_ID
29     REGISTER_SYSTEM_ABILITY_BY_ID(TestServerService, TEST_SERVER_SA_ID, false); // SA run on demand
30 
31     using namespace std;
32     using namespace OHOS::HiviewDFX;
33     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_SERVICE = {LOG_CORE, 0xD003110, "TestServerService"};
34     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_TIMER = {LOG_CORE, 0xD003110, "CallerDetectTimer"};
35     static const int CALLER_DETECT_DURING = 10000;
36 
TestServerService(int32_t saId,bool runOnCreate)37     TestServerService::TestServerService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
38     {
39         HiLog::Info(LABEL_SERVICE, "%{public}s called. saId=%{public}d, runOnCreate=%{public}d",
40             __func__, saId, runOnCreate);
41         StartCallerDetectTimer();
42     }
43 
~TestServerService()44     TestServerService::~TestServerService()
45     {
46         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
47         if (callerDetectTimer_ == nullptr) {
48             HiLog::Error(LABEL_SERVICE, "%{public}s. callerDetectTimer_ is nullptr.", __func__);
49             return;
50         }
51         callerDetectTimer_->Cancel();
52     }
53 
OnStart()54     void TestServerService::OnStart()
55     {
56         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
57         if (!IsRootVersion() && !IsDeveloperMode()) {
58             HiLog::Error(LABEL_SERVICE, "%{public}s. System mode is unsatisfied.", __func__);
59             return;
60         }
61         bool res = Publish(this);
62         if (!res) {
63             HiLog::Error(LABEL_SERVICE, "%{public}s. Publish failed", __func__);
64         }
65     }
66 
OnStop()67     void TestServerService::OnStop()
68     {
69         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
70         IsDeveloperMode();
71     }
72 
IsRootVersion()73     bool TestServerService::IsRootVersion()
74     {
75         bool debugmode = OHOS::system::GetBoolParameter("const.debuggable", false);
76         HiLog::Info(LABEL_SERVICE, "%{public}s. debugmode=%{public}d", __func__, debugmode);
77         return debugmode;
78     }
79 
IsDeveloperMode()80     bool TestServerService::IsDeveloperMode()
81     {
82         bool developerMode = OHOS::system::GetBoolParameter("const.security.developermode.state", false);
83         HiLog::Info(LABEL_SERVICE, "%{public}s. developerMode=%{public}d", __func__, developerMode);
84         return developerMode;
85     }
86 
StartCallerDetectTimer()87     void TestServerService::StartCallerDetectTimer()
88     {
89         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
90         callerDetectTimer_ = new CallerDetectTimer(this);
91         callerDetectTimer_->Start();
92     }
93 
CreateSession(const SessionToken & sessionToken)94     ErrCode TestServerService::CreateSession(const SessionToken &sessionToken)
95     {
96         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
97         bool result = true;
98         try {
99             result = sessionToken.AddDeathRecipient(
100                 sptr<TestServerProxyDeathRecipient>(new TestServerProxyDeathRecipient(this)));
101         } catch(...) {
102             result = false;
103         }
104         if (!result) {
105             HiLog::Error(LABEL_SERVICE, "%{public}s. AddDeathRecipient FAILD.", __func__);
106             DestorySession();
107             return TEST_SERVER_ADD_DEATH_RECIPIENT_FAILED;
108         }
109         AddCaller();
110         HiLog::Info(LABEL_SERVICE, "%{public}s. Create session SUCCESS. callerCount=%{public}d",
111             __func__, GetCallerCount());
112         return TEST_SERVER_OK;
113     }
114 
SetPasteData(const std::string & text)115     ErrCode TestServerService::SetPasteData(const std::string& text)
116     {
117         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
118         auto pasteBoardMgr = MiscServices::PasteboardClient::GetInstance();
119         pasteBoardMgr->Clear();
120         auto pasteData = pasteBoardMgr->CreatePlainTextData(text);
121         if (pasteData == nullptr) {
122             return TEST_SERVER_CREATE_PASTE_DATA_FAILED;
123         }
124         int32_t ret = pasteBoardMgr->SetPasteData(*pasteData);
125         int32_t successErrCode = 27787264;
126         return ret == successErrCode ? TEST_SERVER_OK : TEST_SERVER_SET_PASTE_DATA_FAILED;
127     }
128 
PublishCommonEvent(const EventFwk::CommonEventData & event,bool & re)129     ErrCode TestServerService::PublishCommonEvent(const EventFwk::CommonEventData &event, bool &re)
130     {
131         if (!EventFwk::CommonEventManager::PublishCommonEvent(event)) {
132             HiLog::Info(LABEL_SERVICE, "%{public}s Pulbish commonEvent.", __func__);
133             re = false;
134         }
135         re = true;
136         int32_t ret = re ? TEST_SERVER_OK : TEST_SERVER_PUBLISH_EVENT_FAILED;
137         return ret;
138     }
139 
AddCaller()140     void TestServerService::AddCaller()
141     {
142         callerCount_++;
143     }
144 
RemoveCaller()145     void TestServerService::RemoveCaller()
146     {
147         callerCount_--;
148     }
149 
GetCallerCount()150     int TestServerService::GetCallerCount()
151     {
152         return callerCount_.load();
153     }
154 
DestorySession()155     void TestServerService::DestorySession()
156     {
157         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
158         if (callerCount_ == 0) {
159             HiLog::Info(LABEL_SERVICE, "%{public}s. No proxy exists. Remove the TestServer", __func__);
160             RemoveTestServer();
161         } else {
162             HiLog::Info(LABEL_SERVICE, "%{public}s. Other proxys exist. Can not remove the TestServer", __func__);
163         }
164     }
165 
RemoveTestServer()166     bool TestServerService::RemoveTestServer()
167     {
168         HiLog::Info(LABEL_SERVICE, "%{public}s called. ", __func__);
169         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
170         if (samgr == nullptr) {
171             HiLog::Error(LABEL_SERVICE, "%{public}s. Get SystemAbility Manager failed!", __func__);
172             return false;
173         }
174         auto res = samgr->UnloadSystemAbility(TEST_SERVER_SA_ID);
175         return res == ERR_OK;
176     }
177 
OnRemoteDied(const wptr<IRemoteObject> & object)178     void TestServerService::TestServerProxyDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
179     {
180         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
181         if (object == nullptr) {
182             HiLog::Error(LABEL_SERVICE, "%{public}s. IRemoteObject is NULL.", __func__);
183             return;
184         }
185         testServerService_->RemoveCaller();
186         testServerService_->DestorySession();
187     }
188 
Start()189     void TestServerService::CallerDetectTimer::Start()
190     {
191         HiLog::Info(LABEL_TIMER, "%{public}s called.", __func__);
192         thread_ = thread([this] {
193             this_thread::sleep_for(chrono::milliseconds(CALLER_DETECT_DURING));
194             HiLog::Info(LABEL_TIMER, "%{public}s. Timer is done.", __func__);
195             if (!testServerExit_) {
196                 testServerService_->DestorySession();
197             }
198         });
199         thread_.detach();
200     }
201 
Cancel()202     void TestServerService::CallerDetectTimer::Cancel()
203     {
204         HiLog::Info(LABEL_TIMER, "%{public}s called.", __func__);
205         testServerExit_ = true;
206     }
207 
FrequencyLock()208     ErrCode TestServerService::FrequencyLock()
209     {
210         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
211         int performanceModeId = 9100;
212         OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(performanceModeId, "");
213         return TEST_SERVER_OK;
214     }
215 
216 } // namespace OHOS::testserver