• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "system_ability.h"
17 
18 #include <cinttypes>
19 #include "datetime_ex.h"
20 #include "errors.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "local_ability_manager.h"
25 #include "safwk_log.h"
26 #include "string_ex.h"
27 
28 namespace OHOS {
29 namespace {
30 const std::string TAG = "SystemAbility";
31 }
32 
SystemAbility(bool runOnCreate)33 SystemAbility::SystemAbility(bool runOnCreate)
34 {
35     isRunning_ = false;
36     isRunOnCreate_ = runOnCreate;
37     isDistributed_ = false;
38     dumpLevel_ = 0;
39     // timeout for waiting dependency ready, which configed in xml, with DEFAULT_DEPENDENCY_TIMEOUT(6s) by default
40     dependTimeout_ = DEFAULT_DEPENDENCY_TIMEOUT;
41     capability_ = u"";
42 }
43 
SystemAbility(int32_t systemAbilityId,bool runOnCreate)44 SystemAbility::SystemAbility(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(runOnCreate)
45 {
46     saId_ = systemAbilityId;
47 }
48 
~SystemAbility()49 SystemAbility::~SystemAbility()
50 {
51     HILOGI(TAG, "SA:%{public}d destroyed", saId_);
52 }
53 
MakeAndRegisterAbility(SystemAbility * systemAbility)54 bool SystemAbility::MakeAndRegisterAbility(SystemAbility* systemAbility)
55 {
56     HILOGD(TAG, "registering system ability...");
57     return LocalAbilityManager::GetInstance().AddAbility(systemAbility);
58 }
59 
AddSystemAbilityListener(int32_t systemAbilityId)60 bool SystemAbility::AddSystemAbilityListener(int32_t systemAbilityId)
61 {
62     HILOGD(TAG, "SA:%{public}d, listenerSA:%{public}d", systemAbilityId, saId_);
63     return LocalAbilityManager::GetInstance().AddSystemAbilityListener(systemAbilityId, saId_);
64 }
65 
RemoveSystemAbilityListener(int32_t systemAbilityId)66 bool SystemAbility::RemoveSystemAbilityListener(int32_t systemAbilityId)
67 {
68     HILOGD(TAG, "SA:%{public}d, listenerSA:%{public}d", systemAbilityId, saId_);
69     return LocalAbilityManager::GetInstance().RemoveSystemAbilityListener(systemAbilityId, saId_);
70 }
71 
Publish(sptr<IRemoteObject> systemAbility)72 bool SystemAbility::Publish(sptr<IRemoteObject> systemAbility)
73 {
74     if (systemAbility == nullptr) {
75         HILOGE(TAG, "systemAbility is nullptr");
76         return false;
77     }
78     HILOGI(TAG, "[PerformanceTest] SAFWK Publish systemAbilityId:%{public}d", saId_);
79     int64_t begin = GetTickCount();
80     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
81     if (samgrProxy == nullptr) {
82         HILOGE(TAG, "failed to get samgrProxy");
83         return false;
84     }
85 
86     publishObj_ = systemAbility;
87     HILOGD(TAG, "SA:%{public}d", saId_);
88     ISystemAbilityManager::SAExtraProp saExtra(GetDistributed(), GetDumpLevel(), capability_, permission_);
89     int32_t result = samgrProxy->AddSystemAbility(saId_, publishObj_, saExtra);
90     HILOGI(TAG, "AddSystemAbility result:%{public}d", result);
91     HILOGI(TAG, "[PerformanceTest] SAFWK Publish systemAbilityId:%{public}d finished, spend:%{public}" PRId64 " ms",
92         saId_, (GetTickCount() - begin));
93     return result == ERR_OK;
94 }
95 
StopAbility(int32_t systemAbilityId)96 void SystemAbility::StopAbility(int32_t systemAbilityId)
97 {
98     HILOGD(TAG, "SA:%{public}d", systemAbilityId);
99 
100     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
101     if (samgrProxy == nullptr) {
102         HILOGE(TAG, "failed to get samgrProxy");
103         return;
104     }
105 
106     int32_t ret = samgrProxy->RemoveSystemAbility(systemAbilityId);
107     HILOGI(TAG, "%{public}s to remove ability", (ret == ERR_OK) ? "success" : "failed");
108 }
109 
Start()110 void SystemAbility::Start()
111 {
112     HILOGD(TAG, "starting system ability...");
113     if (isRunning_) {
114         return;
115     }
116     HILOGI(TAG, "[PerformanceTest] SAFWK OnStart systemAbilityId:%{public}d", saId_);
117     int64_t begin = GetTickCount();
118     OnStart();
119     isRunning_ = true;
120     HILOGI(TAG, "[PerformanceTest] SAFWK OnStart systemAbilityId:%{public}d finished, spend:%{public}" PRId64 " ms",
121         saId_, (GetTickCount() - begin));
122 }
123 
Stop()124 void SystemAbility::Stop()
125 {
126     HILOGD(TAG, "stoping system ability...");
127 
128     if (!isRunning_) {
129         return;
130     }
131 
132     OnStop();
133     isRunning_ = false;
134 
135     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
136     if (samgrProxy == nullptr) {
137         HILOGE(TAG, "failed to get samgrProxy");
138         return;
139     }
140 
141     int32_t ret = samgrProxy->RemoveSystemAbility(saId_);
142     HILOGI(TAG, "%{public}s to remove ability", (ret == ERR_OK) ? "success" : "failed");
143 }
144 
SADump()145 void SystemAbility::SADump()
146 {
147     OnDump();
148 }
149 
GetSystemAbilitId() const150 int32_t SystemAbility::GetSystemAbilitId() const
151 {
152     return saId_;
153 }
154 
SetLibPath(const std::u16string & libPath)155 void SystemAbility::SetLibPath(const std::u16string& libPath)
156 {
157     libPath_ = libPath;
158 }
159 
GetLibPath() const160 const std::u16string& SystemAbility::GetLibPath() const
161 {
162     return libPath_;
163 }
164 
SetDependSa(const std::vector<std::u16string> & dependSa)165 void SystemAbility::SetDependSa(const std::vector<std::u16string>& dependSa)
166 {
167     dependSa_ = dependSa;
168 }
169 
GetDependSa() const170 const std::vector<std::u16string>& SystemAbility::GetDependSa() const
171 {
172     return dependSa_;
173 }
174 
SetRunOnCreate(bool isRunOnCreate)175 void SystemAbility::SetRunOnCreate(bool isRunOnCreate)
176 {
177     isRunOnCreate_ = isRunOnCreate;
178 }
179 
IsRunOnCreate() const180 bool SystemAbility::IsRunOnCreate() const
181 {
182     return isRunOnCreate_;
183 }
184 
SetDistributed(bool isDistributed)185 void SystemAbility::SetDistributed(bool isDistributed)
186 {
187     isDistributed_ = isDistributed;
188 }
189 
GetDistributed() const190 bool SystemAbility::GetDistributed() const
191 {
192     return isDistributed_;
193 }
194 
GetRunningStatus() const195 bool SystemAbility::GetRunningStatus() const
196 {
197     return isRunning_;
198 }
199 
SetDumpLevel(uint32_t dumpLevel)200 void SystemAbility::SetDumpLevel(uint32_t dumpLevel)
201 {
202     dumpLevel_ = dumpLevel;
203 }
204 
GetDumpLevel() const205 uint32_t SystemAbility::GetDumpLevel() const
206 {
207     return dumpLevel_;
208 }
209 
SetDependTimeout(int32_t dependTimeout)210 void SystemAbility::SetDependTimeout(int32_t dependTimeout)
211 {
212     if (dependTimeout >= MIN_DEPENDENCY_TIMEOUT && dependTimeout <= MAX_DEPENDENCY_TIMEOUT) {
213         dependTimeout_ = dependTimeout;
214     }
215     HILOGD(TAG, "new dependTimeout:%{public}d, orignal dependTimeout:%{public}d", dependTimeout, dependTimeout_);
216 }
217 
GetDependTimeout() const218 int32_t SystemAbility::GetDependTimeout() const
219 {
220     return dependTimeout_;
221 }
222 
223 // The details should be implemented by subclass
OnDump()224 void SystemAbility::OnDump()
225 {
226 }
227 
228 // The details should be implemented by subclass
OnStart()229 void SystemAbility::OnStart()
230 {
231 }
232 
233 // The details should be implemented by subclass
OnStop()234 void SystemAbility::OnStop()
235 {
236 }
237 
238 // The details should be implemented by subclass
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)239 void SystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
240 {
241 }
242 
243 // The details should be implemented by subclass
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)244 void SystemAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
245 {
246 }
247 
GetSystemAbility(int32_t systemAbilityId)248 sptr<IRemoteObject> SystemAbility::GetSystemAbility(int32_t systemAbilityId)
249 {
250     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
251     if (samgrProxy == nullptr) {
252         HILOGE(TAG, "failed to get samgrProxy");
253         return nullptr;
254     }
255 
256     return samgrProxy->GetSystemAbility(systemAbilityId);
257 }
258 
SetCapability(const std::u16string & capability)259 void SystemAbility::SetCapability(const std::u16string& capability)
260 {
261     capability_ = capability;
262 }
263 
GetCapability() const264 const std::u16string& SystemAbility::GetCapability() const
265 {
266     return capability_;
267 }
268 
SetPermission(const std::u16string & permission)269 void SystemAbility::SetPermission(const std::u16string& permission)
270 {
271     permission_ = permission;
272 }
273 }
274