1 /*
2 * Copyright (c) 2023-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 "standby_service.h"
17
18 #include <functional>
19 #include <map>
20
21 #include "accesstoken_kit.h"
22 #include "parameter.h"
23 #include "ipc_skeleton.h"
24 #include "file_ex.h"
25 #include "string_ex.h"
26
27 #include "system_ability_definition.h"
28 #include "device_standby_switch.h"
29 #include "standby_service_impl.h"
30 #include "standby_service_log.h"
31 #include "standby_config_manager.h"
32
33 namespace OHOS {
34 namespace DevStandbyMgr {
35 namespace {
36 const uint32_t COMMON_EVENT_READY = 1;
37 const uint32_t TIMER_SERVICE_READY = 2;
38 const uint32_t ABILITY_SERVICE_READY = 4;
39 const uint32_t BUNDLE_MGR_READY = 8;
40 const uint32_t POWER_SERVICE_READY = 16;
41 const uint32_t APP_MGR_SERVICE_READY = 32;
42 const uint32_t MULTIMODAL_INPUT_SERVICE_READY = 64;
43 const uint32_t ALL_DEPENDS_READY = 127;
44 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(
45 StandbyService::GetInstance().get());
46 const bool SOFTWARE_SLEEP = system::GetBoolParameter("persist.sys.standby_switch", true);
47 const std::string RSS_PROCESS_NAME = "resource_schedule_service";
48 const std::string PUSH_PROCESS_NAME = "push_manager_service";
49 const int32_t ENG_MODE = OHOS::system::GetIntParameter("const.debuggable", 0);
50 }
51
StandbyService()52 StandbyService::StandbyService() : SystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID, true) {}
53
StandbyService(const int32_t systemAbilityId,bool runOnCreate)54 StandbyService::StandbyService(const int32_t systemAbilityId, bool runOnCreate)
55 : SystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID, true) {}
56
~StandbyService()57 StandbyService::~StandbyService() {}
58
GetInstance()59 std::shared_ptr<StandbyService> StandbyService::GetInstance()
60 {
61 return DelayedSingleton<StandbyService>::GetInstance();
62 }
63
OnStart()64 void StandbyService::OnStart()
65 {
66 if (!SOFTWARE_SLEEP) {
67 return;
68 }
69 if (state_.load() == ServiceRunningState::STATE_RUNNING) {
70 STANDBYSERVICE_LOGW("device standby service has already started.");
71 return;
72 }
73 if (!StandbyServiceImpl::GetInstance()->Init()) {
74 STANDBYSERVICE_LOGE("failed to init device standby service");
75 return;
76 }
77 InitStandyMode();
78 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
79 AddSystemAbilityListener(TIME_SERVICE_ID);
80 AddSystemAbilityListener(ABILITY_MGR_SERVICE_ID);
81 AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
82 AddSystemAbilityListener(POWER_MANAGER_SERVICE_ID);
83 AddSystemAbilityListener(APP_MGR_SERVICE_ID);
84 AddSystemAbilityListener(MULTIMODAL_INPUT_SERVICE_ID);
85 if (!Publish(StandbyService::GetInstance().get())) {
86 STANDBYSERVICE_LOGE("standby service start failed!");
87 return;
88 }
89 state_.store(ServiceRunningState::STATE_RUNNING);
90 STANDBYSERVICE_LOGI("standby service start succeed!");
91 }
92
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)93 void StandbyService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
94 {
95 STANDBYSERVICE_LOGI("add system ability, systemAbilityId : %{public}d", systemAbilityId);
96 std::lock_guard<std::mutex> systemAbilityLock(systemAbilityLock_);
97 auto serviceImpl = StandbyServiceImpl::GetInstance();
98 switch (systemAbilityId) {
99 case COMMON_EVENT_SERVICE_ID:
100 STANDBYSERVICE_LOGD("common event service is ready!");
101 serviceImpl->UpdateSaDependValue(true, COMMON_EVENT_READY);
102 serviceImpl->RegisterCommEventObserver();
103 break;
104 case TIME_SERVICE_ID:
105 STANDBYSERVICE_LOGD("timer service is ready!");
106 serviceImpl->UpdateSaDependValue(true, TIMER_SERVICE_READY);
107 serviceImpl->RegisterTimeObserver();
108 break;
109 case ABILITY_MGR_SERVICE_ID:
110 STANDBYSERVICE_LOGD("ability mgr service is ready!");
111 serviceImpl->UpdateSaDependValue(true, ABILITY_SERVICE_READY);
112 break;
113 case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
114 STANDBYSERVICE_LOGD("bundle mgr service is ready!");
115 serviceImpl->UpdateSaDependValue(true, BUNDLE_MGR_READY);
116 break;
117 case POWER_MANAGER_SERVICE_ID:
118 STANDBYSERVICE_LOGD("power service is ready!");
119 serviceImpl->UpdateSaDependValue(true, POWER_SERVICE_READY);
120 break;
121 case APP_MGR_SERVICE_ID:
122 STANDBYSERVICE_LOGD("app mgr service is ready!");
123 serviceImpl->UpdateSaDependValue(true, APP_MGR_SERVICE_READY);
124 serviceImpl->RegisterAppStateObserver();
125 break;
126 case MULTIMODAL_INPUT_SERVICE_ID:
127 STANDBYSERVICE_LOGD("multi modal input service is ready!");
128 serviceImpl->UpdateSaDependValue(true, MULTIMODAL_INPUT_SERVICE_READY);
129 break;
130 default:
131 NotifySystemAbilityStatusChanged(true, systemAbilityId);
132 break;
133 }
134 if (serviceImpl->GetSaDependValue() == ALL_DEPENDS_READY) {
135 STANDBYSERVICE_LOGI("all necessary system service for standby service has been satisfied!");
136 serviceImpl->InitReadyState();
137 }
138 }
139
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)140 void StandbyService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
141 {
142 STANDBYSERVICE_LOGI("remove system ability, systemAbilityId : %{public}d", systemAbilityId);
143 std::lock_guard<std::mutex> systemAbilityLock(systemAbilityLock_);
144 auto serviceImpl = StandbyServiceImpl::GetInstance();
145 switch (systemAbilityId) {
146 case COMMON_EVENT_SERVICE_ID:
147 STANDBYSERVICE_LOGI("common event service is removed!");
148 serviceImpl->UpdateSaDependValue(false, COMMON_EVENT_READY);
149 serviceImpl->UnregisterCommEventObserver();
150 break;
151 case TIME_SERVICE_ID:
152 STANDBYSERVICE_LOGI("time service is removed!");
153 serviceImpl->UpdateSaDependValue(false, TIMER_SERVICE_READY);
154 serviceImpl->UnregisterTimeObserver();
155 break;
156 case ABILITY_MGR_SERVICE_ID:
157 STANDBYSERVICE_LOGI("ability mgr service is removed!");
158 serviceImpl->UpdateSaDependValue(false, ABILITY_SERVICE_READY);
159 break;
160 case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
161 STANDBYSERVICE_LOGI("bundle mgr service is removed!");
162 serviceImpl->UpdateSaDependValue(false, BUNDLE_MGR_READY);
163 break;
164 case POWER_MANAGER_SERVICE_ID:
165 STANDBYSERVICE_LOGI("power service is removed!");
166 serviceImpl->UpdateSaDependValue(false, POWER_SERVICE_READY);
167 break;
168 case APP_MGR_SERVICE_ID:
169 STANDBYSERVICE_LOGI("app mgr service is removed!");
170 serviceImpl->UpdateSaDependValue(false, APP_MGR_SERVICE_READY);
171 serviceImpl->UnregisterAppStateObserver();
172 break;
173 case MULTIMODAL_INPUT_SERVICE_ID:
174 STANDBYSERVICE_LOGI("multi modal input service is removed!");
175 serviceImpl->UpdateSaDependValue(false, MULTIMODAL_INPUT_SERVICE_READY);
176 break;
177 default:
178 NotifySystemAbilityStatusChanged(false, systemAbilityId);
179 break;
180 }
181 if (serviceImpl->GetSaDependValue() != ALL_DEPENDS_READY) {
182 STANDBYSERVICE_LOGI("necessary system service for standby service has been unsatisfied");
183 serviceImpl->UninitReadyState();
184 }
185 }
186
SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber,const std::string & subscriberName,const std::string & moduleName)187 ErrCode StandbyService::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber,
188 const std::string& subscriberName, const std::string& moduleName)
189 {
190 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
191 STANDBYSERVICE_LOGW("standby service is not running");
192 return ERR_STANDBY_SYS_NOT_READY;
193 }
194 subscriber->SetSubscriberName(subscriberName);
195 subscriber->SetModuleName(moduleName);
196 return StandbyServiceImpl::GetInstance()->SubscribeStandbyCallback(subscriber);
197 }
198
UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)199 ErrCode StandbyService::UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
200 {
201 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
202 STANDBYSERVICE_LOGW("standby service is not running");
203 return ERR_STANDBY_SYS_NOT_READY;
204 }
205 return StandbyServiceImpl::GetInstance()->UnsubscribeStandbyCallback(subscriber);
206 }
207
ApplyAllowResource(const ResourceRequest & resourceRequest)208 ErrCode StandbyService::ApplyAllowResource(const ResourceRequest& resourceRequest)
209 {
210 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
211 STANDBYSERVICE_LOGW("standby service is not running");
212 return ERR_STANDBY_SYS_NOT_READY;
213 }
214 ResourceRequest request(resourceRequest);
215 return StandbyServiceImpl::GetInstance()->ApplyAllowResource(request);
216 }
217
UnapplyAllowResource(const ResourceRequest & resourceRequest)218 ErrCode StandbyService::UnapplyAllowResource(const ResourceRequest& resourceRequest)
219 {
220 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
221 STANDBYSERVICE_LOGW("standby service is not running");
222 return ERR_STANDBY_SYS_NOT_READY;
223 }
224 ResourceRequest request(resourceRequest);
225 return StandbyServiceImpl::GetInstance()->UnapplyAllowResource(request);
226 }
227
GetAllowList(uint32_t allowType,std::vector<AllowInfo> & allowInfoList,uint32_t reasonCode)228 ErrCode StandbyService::GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList,
229 uint32_t reasonCode)
230 {
231 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
232 STANDBYSERVICE_LOGW("standby service is not running");
233 return ERR_STANDBY_SYS_NOT_READY;
234 }
235 return StandbyServiceImpl::GetInstance()->GetAllowList(allowType, allowInfoList, reasonCode);
236 }
237
IsDeviceInStandby(bool & isStandby)238 ErrCode StandbyService::IsDeviceInStandby(bool& isStandby)
239 {
240 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
241 STANDBYSERVICE_LOGW("standby service is not running");
242 return ERR_STANDBY_SYS_NOT_READY;
243 }
244 return StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby);
245 }
246
SetNatInterval(uint32_t type,bool enable,uint32_t interval)247 ErrCode StandbyService::SetNatInterval(uint32_t type, bool enable, uint32_t interval)
248 {
249 if (!CheckProcessNamePermission(PUSH_PROCESS_NAME)) {
250 STANDBYSERVICE_LOGE("set nat interval permission check fail");
251 return ERR_PERMISSION_DENIED;
252 }
253 StandbyMessage standbyMessage{StandbyMessageType::NAT_DETECT_INTERVAL_CHANGED};
254 standbyMessage.want_ = AAFwk::Want {};
255 standbyMessage.want_->SetParam(MESSAGE_TYPE, static_cast<int32_t>(type));
256 standbyMessage.want_->SetParam(MESSAGE_ENABLE, enable);
257 standbyMessage.want_->SetParam(MESSAGE_INTERVAL, static_cast<int32_t>(interval));
258 StandbyServiceImpl::GetInstance()->DispatchEvent(standbyMessage);
259 return ERR_OK;
260 }
261
DelayHeartBeat(int64_t timestamp)262 ErrCode StandbyService::DelayHeartBeat(int64_t timestamp)
263 {
264 if (!CheckProcessNamePermission(PUSH_PROCESS_NAME)) {
265 STANDBYSERVICE_LOGE("delay heartbeat permission check fail");
266 return ERR_PERMISSION_DENIED;
267 }
268 StandbyMessage standbyMessage{StandbyMessageType::NAT_MSG_RECV};
269 standbyMessage.want_ = AAFwk::Want {};
270 standbyMessage.want_->SetParam(MESSAGE_TIMESTAMP, static_cast<int32_t>(timestamp));
271 StandbyServiceImpl::GetInstance()->DispatchEvent(standbyMessage);
272 return ERR_OK;
273 }
274
AddPluginSysAbilityListener(int32_t systemAbilityId)275 void StandbyService::AddPluginSysAbilityListener(int32_t systemAbilityId)
276 {
277 std::lock_guard<std::mutex> pluginListenerLock(listenedSALock_);
278 STANDBYSERVICE_LOGI("add listener to system ability %{public}d", systemAbilityId);
279 AddSystemAbilityListener(systemAbilityId);
280 }
281
NotifySystemAbilityStatusChanged(bool isAdded,int32_t systemAbilityId)282 ErrCode StandbyService::NotifySystemAbilityStatusChanged(bool isAdded, int32_t systemAbilityId)
283 {
284 StandbyMessage standbyMessage{StandbyMessageType::SYS_ABILITY_STATUS_CHANGED};
285 standbyMessage.want_ = AAFwk::Want {};
286 standbyMessage.want_->SetParam(SA_STATUS, isAdded);
287 standbyMessage.want_->SetParam(SA_ID, systemAbilityId);
288 StandbyServiceImpl::GetInstance()->DispatchEvent(standbyMessage);
289 return ERR_OK;
290 }
291
OnStop()292 void StandbyService::OnStop()
293 {
294 StandbyServiceImpl::GetInstance()->UnInit();
295 state_.store(ServiceRunningState::STATE_NOT_START);
296 STANDBYSERVICE_LOGI("standby service task manager stop");
297 }
298
ReportWorkSchedulerStatus(bool started,int32_t uid,const std::string & bundleName)299 ErrCode StandbyService::ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName)
300 {
301 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
302 STANDBYSERVICE_LOGW("standby service is not running");
303 return ERR_STANDBY_SYS_NOT_READY;
304 }
305 return StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(started, uid, bundleName);
306 }
307
GetRestrictList(uint32_t restrictType,std::vector<AllowInfo> & restrictInfoList,uint32_t reasonCode)308 ErrCode StandbyService::GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
309 uint32_t reasonCode)
310 {
311 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
312 STANDBYSERVICE_LOGW("standby service is not running");
313 return ERR_STANDBY_SYS_NOT_READY;
314 }
315 return StandbyServiceImpl::GetInstance()->GetRestrictList(restrictType, restrictInfoList, reasonCode);
316 }
317
IsStrategyEnabled(const std::string & strategyName,bool & isEnabled)318 ErrCode StandbyService::IsStrategyEnabled(const std::string& strategyName, bool& isEnabled)
319 {
320 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
321 STANDBYSERVICE_LOGW("standby service is not running");
322 return ERR_STANDBY_SYS_NOT_READY;
323 }
324 return StandbyServiceImpl::GetInstance()->IsStrategyEnabled(strategyName, isEnabled);
325 }
326
ReportPowerOverused(const std::string & module,uint32_t level)327 ErrCode StandbyService::ReportPowerOverused(const std::string &module, uint32_t level)
328 {
329 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
330 STANDBYSERVICE_LOGW("standby service is not running");
331 return ERR_STANDBY_SYS_NOT_READY;
332 }
333
334 return StandbyServiceImpl::GetInstance()->ReportPowerOverused(module, level);
335 }
336
ReportDeviceStateChanged(int32_t type,bool enabled)337 ErrCode StandbyService::ReportDeviceStateChanged(int32_t type, bool enabled)
338 {
339 if (state_.load() != ServiceRunningState::STATE_RUNNING) {
340 STANDBYSERVICE_LOGW("standby service is not running");
341 return ERR_STANDBY_SYS_NOT_READY;
342 }
343 return StandbyServiceImpl::GetInstance()->ReportDeviceStateChanged(type, enabled);
344 }
345
Dump(int32_t fd,const std::vector<std::u16string> & args)346 int32_t StandbyService::Dump(int32_t fd, const std::vector<std::u16string>& args)
347 {
348 if (ENG_MODE == 0) {
349 STANDBYSERVICE_LOGE("Not Engineer mode");
350 return ERR_PERMISSION_DENIED;
351 }
352 Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
353 int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, "ohos.permission.DUMP");
354 if (ret != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
355 return ERR_PERMISSION_DENIED;
356 }
357 std::vector<std::string> argsInStr;
358 std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
359 [](const std::u16string& arg) {
360 return Str16ToStr8(arg);
361 });
362 std::string result;
363 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
364 if (!SaveStringToFd(fd, result)) {
365 STANDBYSERVICE_LOGE("StandbyService dump save string to fd failed!");
366 return ERR_STANDBY_DUMP_SAVE_DENIED;
367 }
368 return ERR_OK;
369 }
370
CheckProcessNamePermission(const std::string & processName)371 bool StandbyService::CheckProcessNamePermission(const std::string& processName)
372 {
373 Security::AccessToken::NativeTokenInfo nativeTokenInfo;
374 uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
375 auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(accessToken);
376 int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
377 if (tokenType != Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
378 result != ERR_OK || nativeTokenInfo.processName != processName) {
379 STANDBYSERVICE_LOGE("check processName failed,tokenType=%{public}d,processName=%{public}s",
380 tokenType, nativeTokenInfo.processName.c_str());
381 return false;
382 }
383 return true;
384 }
385
HandleEvent(const uint32_t resType,const int64_t value,const std::string & sceneInfo)386 ErrCode StandbyService::HandleEvent(const uint32_t resType, const int64_t value,
387 const std::string &sceneInfo)
388 {
389 if (!CheckProcessNamePermission(RSS_PROCESS_NAME)) {
390 return ERR_PERMISSION_DENIED;
391 }
392 StandbyServiceImpl::GetInstance()->HandleCommonEvent(resType, value, sceneInfo);
393 return ERR_OK;
394 }
395 } // namespace DevStandbyMgr
396 } // namespace OHOS
397