1 /*
2 * Copyright (c) 2023 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 "ipc_skeleton.h"
23 #include "file_ex.h"
24 #include "string_ex.h"
25
26 #include "device_standby_switch.h"
27 #include "standby_service_impl.h"
28 #include "standby_service_log.h"
29 #include "standby_config_manager.h"
30
31 namespace OHOS {
32 namespace DevStandbyMgr {
33 namespace {
34 const uint32_t COMMON_EVENT_READY = 1;
35 const uint32_t TIMER_SERVICE_READY = 2;
36 const uint32_t ABILITY_SERVICE_READY = 4;
37 const uint32_t BUNDLE_MGR_READY = 8;
38 const uint32_t POWER_SERVICE_READY = 16;
39 const uint32_t APP_MGR_SERVICE_READY = 32;
40 const uint32_t MULTIMODAL_INPUT_SERVICE_READY = 64;
41 const uint32_t ALL_DEPENDS_READY = 127;
42 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(
43 StandbyService::GetInstance().get());
44 const bool SOFTWARE_SLEEP = system::GetBoolParameter("persist.sys.standby_switch", true);
45 }
46
47 IMPLEMENT_SINGLE_INSTANCE(StandbyService);
48
StandbyService()49 StandbyService::StandbyService() : SystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID, true) {}
50
StandbyService(const int32_t systemAbilityId,bool runOnCreate)51 StandbyService::StandbyService(const int32_t systemAbilityId, bool runOnCreate)
52 : SystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID, true) {}
53
~StandbyService()54 StandbyService::~StandbyService() {}
55
OnStart()56 void StandbyService::OnStart()
57 {
58 if (!SOFTWARE_SLEEP) {
59 return;
60 }
61 if (state_ == ServiceRunningState::STATE_RUNNING) {
62 STANDBYSERVICE_LOGW("device standby service has already started.");
63 return;
64 }
65 if (!StandbyServiceImpl::GetInstance()->Init()) {
66 STANDBYSERVICE_LOGE("failed to init device standby service");
67 return;
68 }
69 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
70 AddSystemAbilityListener(TIME_SERVICE_ID);
71 AddSystemAbilityListener(ABILITY_MGR_SERVICE_ID);
72 AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
73 AddSystemAbilityListener(POWER_MANAGER_SERVICE_ID);
74 AddSystemAbilityListener(APP_MGR_SERVICE_ID);
75 AddSystemAbilityListener(MULTIMODAL_INPUT_SERVICE_ID);
76 if (!Publish(StandbyService::GetInstance().get())) {
77 STANDBYSERVICE_LOGE("standby service start failed!");
78 return;
79 }
80 state_ = ServiceRunningState::STATE_RUNNING;
81 STANDBYSERVICE_LOGI("standby service start succeed!");
82 }
83
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)84 void StandbyService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
85 {
86 STANDBYSERVICE_LOGD("add system ability, systemAbilityId : %{public}d", systemAbilityId);
87 std::lock_guard<std::mutex> systemAbilityLock(systemAbilityLock_);
88 switch (systemAbilityId) {
89 case COMMON_EVENT_SERVICE_ID:
90 STANDBYSERVICE_LOGD("common event service is ready!");
91 dependsReady_ |= COMMON_EVENT_READY;
92 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
93 break;
94 case TIME_SERVICE_ID:
95 STANDBYSERVICE_LOGD("timer service is ready!");
96 dependsReady_ |= TIMER_SERVICE_READY;
97 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
98 break;
99 case ABILITY_MGR_SERVICE_ID:
100 STANDBYSERVICE_LOGD("ability mgr service is ready!");
101 dependsReady_ |= ABILITY_SERVICE_READY;
102 break;
103 case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
104 STANDBYSERVICE_LOGD("bundle mgr service is ready!");
105 dependsReady_ |= BUNDLE_MGR_READY;
106 break;
107 case POWER_MANAGER_SERVICE_ID:
108 STANDBYSERVICE_LOGD("power service is ready!");
109 dependsReady_ |= POWER_SERVICE_READY;
110 break;
111 case APP_MGR_SERVICE_ID:
112 STANDBYSERVICE_LOGD("app mgr service is ready!");
113 dependsReady_ |= APP_MGR_SERVICE_READY;
114 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
115 break;
116 case MULTIMODAL_INPUT_SERVICE_ID:
117 STANDBYSERVICE_LOGD("multi modal input service is ready!");
118 dependsReady_ |= MULTIMODAL_INPUT_SERVICE_READY;
119 break;
120 default:
121 NotifySystemAbilityStatusChanged(true, systemAbilityId);
122 break;
123 }
124 if (dependsReady_ == ALL_DEPENDS_READY) {
125 STANDBYSERVICE_LOGD("all necessary system service for standby service has been satisfied!");
126 StandbyServiceImpl::GetInstance()->InitReadyState();
127 }
128 }
129
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)130 void StandbyService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
131 {
132 STANDBYSERVICE_LOGI("remove system ability, systemAbilityId : %{public}d", systemAbilityId);
133 std::lock_guard<std::mutex> systemAbilityLock(systemAbilityLock_);
134 switch (systemAbilityId) {
135 case COMMON_EVENT_SERVICE_ID:
136 STANDBYSERVICE_LOGI("common event service is removed!");
137 dependsReady_ &= (~COMMON_EVENT_READY);
138 StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver();
139 break;
140 case TIME_SERVICE_ID:
141 STANDBYSERVICE_LOGI("time service is removed!");
142 dependsReady_ &= (~TIMER_SERVICE_READY);
143 StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
144 break;
145 case ABILITY_MGR_SERVICE_ID:
146 STANDBYSERVICE_LOGI("ability mgr service is removed!");
147 dependsReady_ &= (~ABILITY_SERVICE_READY);
148 break;
149 case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
150 STANDBYSERVICE_LOGI("bundle mgr service is removed!");
151 dependsReady_ &= (~BUNDLE_MGR_READY);
152 break;
153 case POWER_MANAGER_SERVICE_ID:
154 STANDBYSERVICE_LOGI("power service is removed!");
155 dependsReady_ &= (~POWER_SERVICE_READY);
156 break;
157 case APP_MGR_SERVICE_ID:
158 STANDBYSERVICE_LOGI("app mgr service is removed!");
159 dependsReady_ &= (~APP_MGR_SERVICE_READY);
160 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
161 break;
162 case MULTIMODAL_INPUT_SERVICE_ID:
163 STANDBYSERVICE_LOGI("multi modal input service is removed!");
164 dependsReady_ &= (~MULTIMODAL_INPUT_SERVICE_READY);
165 break;
166 default:
167 NotifySystemAbilityStatusChanged(false, systemAbilityId);
168 break;
169 }
170 if (dependsReady_ != ALL_DEPENDS_READY) {
171 STANDBYSERVICE_LOGI("necessary system service for standby service has been unsatisfied");
172 StandbyServiceImpl::GetInstance()->UninitReadyState();
173 }
174 }
175
SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)176 ErrCode StandbyService::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
177 {
178 if (state_ != ServiceRunningState::STATE_RUNNING) {
179 STANDBYSERVICE_LOGW("standby service is not running");
180 return ERR_STANDBY_SYS_NOT_READY;
181 }
182 return StandbyServiceImpl::GetInstance()->SubscribeStandbyCallback(subscriber);
183 }
184
UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)185 ErrCode StandbyService::UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
186 {
187 if (state_ != ServiceRunningState::STATE_RUNNING) {
188 STANDBYSERVICE_LOGW("standby service is not running");
189 return ERR_STANDBY_SYS_NOT_READY;
190 }
191 return StandbyServiceImpl::GetInstance()->UnsubscribeStandbyCallback(subscriber);
192 }
193
ApplyAllowResource(const sptr<ResourceRequest> & resourceRequest)194 ErrCode StandbyService::ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
195 {
196 if (state_ != ServiceRunningState::STATE_RUNNING) {
197 STANDBYSERVICE_LOGW("standby service is not running");
198 return ERR_STANDBY_SYS_NOT_READY;
199 }
200 return StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
201 }
202
UnapplyAllowResource(const sptr<ResourceRequest> & resourceRequest)203 ErrCode StandbyService::UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
204 {
205 if (state_ != ServiceRunningState::STATE_RUNNING) {
206 STANDBYSERVICE_LOGW("standby service is not running");
207 return ERR_STANDBY_SYS_NOT_READY;
208 }
209 return StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
210 }
211
GetAllowList(uint32_t allowType,std::vector<AllowInfo> & allowInfoList,uint32_t reasonCode)212 ErrCode StandbyService::GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList,
213 uint32_t reasonCode)
214 {
215 if (state_ != ServiceRunningState::STATE_RUNNING) {
216 STANDBYSERVICE_LOGW("standby service is not running");
217 return ERR_STANDBY_SYS_NOT_READY;
218 }
219 return StandbyServiceImpl::GetInstance()->GetAllowList(allowType, allowInfoList, reasonCode);
220 }
221
IsDeviceInStandby(bool & isStandby)222 ErrCode StandbyService::IsDeviceInStandby(bool& isStandby)
223 {
224 if (state_ != ServiceRunningState::STATE_RUNNING) {
225 STANDBYSERVICE_LOGW("standby service is not running");
226 return ERR_STANDBY_SYS_NOT_READY;
227 }
228 return StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby);
229 }
230
AddPluginSysAbilityListener(int32_t systemAbilityId)231 void StandbyService::AddPluginSysAbilityListener(int32_t systemAbilityId)
232 {
233 std::lock_guard<std::mutex> pluginListenerLock(listenedSALock_);
234 STANDBYSERVICE_LOGI("add listener to system ability %{public}d", systemAbilityId);
235 AddSystemAbilityListener(systemAbilityId);
236 }
237
NotifySystemAbilityStatusChanged(bool isAdded,int32_t systemAbilityId)238 ErrCode StandbyService::NotifySystemAbilityStatusChanged(bool isAdded, int32_t systemAbilityId)
239 {
240 StandbyMessage standbyMessage{StandbyMessageType::SYS_ABILITY_STATUS_CHANGED};
241 standbyMessage.want_ = AAFwk::Want {};
242 standbyMessage.want_->SetParam(SA_STATUS, isAdded);
243 standbyMessage.want_->SetParam(SA_ID, systemAbilityId);
244 StandbyServiceImpl::GetInstance()->DispatchEvent(standbyMessage);
245 return ERR_OK;
246 }
247
OnStop()248 void StandbyService::OnStop()
249 {
250 StandbyServiceImpl::GetInstance()->UnInit();
251 state_ = ServiceRunningState::STATE_NOT_START;
252 STANDBYSERVICE_LOGI("standby service task manager stop");
253 }
254
ReportWorkSchedulerStatus(bool started,int32_t uid,const std::string & bundleName)255 ErrCode StandbyService::ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName)
256 {
257 if (state_ != ServiceRunningState::STATE_RUNNING) {
258 STANDBYSERVICE_LOGW("standby service is not running");
259 return ERR_STANDBY_SYS_NOT_READY;
260 }
261 return StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(started, uid, bundleName);
262 }
263
GetRestrictList(uint32_t restrictType,std::vector<AllowInfo> & restrictInfoList,uint32_t reasonCode)264 ErrCode StandbyService::GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
265 uint32_t reasonCode)
266 {
267 if (state_ != ServiceRunningState::STATE_RUNNING) {
268 STANDBYSERVICE_LOGW("standby service is not running");
269 return ERR_STANDBY_SYS_NOT_READY;
270 }
271 return StandbyServiceImpl::GetInstance()->GetRestrictList(restrictType, restrictInfoList, reasonCode);
272 }
273
IsStrategyEnabled(const std::string & strategyName,bool & isEnabled)274 ErrCode StandbyService::IsStrategyEnabled(const std::string& strategyName, bool& isEnabled)
275 {
276 if (state_ != ServiceRunningState::STATE_RUNNING) {
277 STANDBYSERVICE_LOGW("standby service is not running");
278 return ERR_STANDBY_SYS_NOT_READY;
279 }
280 return StandbyServiceImpl::GetInstance()->IsStrategyEnabled(strategyName, isEnabled);
281 }
282
ReportDeviceStateChanged(DeviceStateType type,bool enabled)283 ErrCode StandbyService::ReportDeviceStateChanged(DeviceStateType type, bool enabled)
284 {
285 if (state_ != ServiceRunningState::STATE_RUNNING) {
286 STANDBYSERVICE_LOGW("standby service is not running");
287 return ERR_STANDBY_SYS_NOT_READY;
288 }
289 return StandbyServiceImpl::GetInstance()->ReportDeviceStateChanged(type, enabled);
290 }
291
Dump(int32_t fd,const std::vector<std::u16string> & args)292 int32_t StandbyService::Dump(int32_t fd, const std::vector<std::u16string>& args)
293 {
294 std::vector<std::string> argsInStr;
295 std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
296 [](const std::u16string& arg) {
297 return Str16ToStr8(arg);
298 });
299 std::string result;
300 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
301 if (!SaveStringToFd(fd, result)) {
302 STANDBYSERVICE_LOGE("StandbyService dump save string to fd failed!");
303 return ERR_STANDBY_DUMP_SAVE_DENIED;
304 }
305 return ERR_OK;
306 }
HandleEvent(const uint32_t resType,const int64_t value,const std::string & sceneInfo)307 ErrCode StandbyService::HandleEvent(const uint32_t resType, const int64_t value,
308 const std::string &sceneInfo)
309 {
310 StandbyServiceImpl::GetInstance()->HandleCommonEvent(resType, value, sceneInfo);
311 return ERR_OK;
312 }
313 } // namespace DevStandbyMgr
314 } // namespace OHOS
315