• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOGI("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_LOGI("common event service is ready!");
91             dependsReady_ |= COMMON_EVENT_READY;
92             StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
93             break;
94         case TIME_SERVICE_ID:
95             STANDBYSERVICE_LOGI("timer service is ready!");
96             dependsReady_ |= TIMER_SERVICE_READY;
97             StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
98             break;
99         case ABILITY_MGR_SERVICE_ID:
100             STANDBYSERVICE_LOGI("ability mgr service is ready!");
101             dependsReady_ |= ABILITY_SERVICE_READY;
102             break;
103         case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
104             STANDBYSERVICE_LOGI("bundle mgr service is ready!");
105             dependsReady_ |= BUNDLE_MGR_READY;
106             break;
107         case POWER_MANAGER_SERVICE_ID:
108             STANDBYSERVICE_LOGI("power service is ready!");
109             dependsReady_ |= POWER_SERVICE_READY;
110             break;
111         case APP_MGR_SERVICE_ID:
112             STANDBYSERVICE_LOGI("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_LOGI("multi modal input service is ready!");
118             dependsReady_ |= MULTIMODAL_INPUT_SERVICE_READY;
119             break;
120         default:
121             NotifySystemAbilityStatusChanged(true, systemAbilityId);
122             break;
123     }
124     STANDBYSERVICE_LOGI("after add system ability, ready state : %{public}u", dependsReady_);
125     if (dependsReady_ == ALL_DEPENDS_READY) {
126         STANDBYSERVICE_LOGI("all necessary system service for standby service has been satisfied!");
127         StandbyServiceImpl::GetInstance()->InitReadyState();
128     }
129 }
130 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)131 void StandbyService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
132 {
133     STANDBYSERVICE_LOGI("remove system ability, systemAbilityId : %{public}d", systemAbilityId);
134     std::lock_guard<std::mutex> systemAbilityLock(systemAbilityLock_);
135     switch (systemAbilityId) {
136         case COMMON_EVENT_SERVICE_ID:
137             STANDBYSERVICE_LOGI("common event service is removed!");
138             dependsReady_ &= (~COMMON_EVENT_READY);
139             StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver();
140             break;
141         case TIME_SERVICE_ID:
142             STANDBYSERVICE_LOGI("time service is removed!");
143             dependsReady_ &= (~TIMER_SERVICE_READY);
144             StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
145             break;
146         case ABILITY_MGR_SERVICE_ID:
147             STANDBYSERVICE_LOGI("ability mgr service is removed!");
148             dependsReady_ &= (~ABILITY_SERVICE_READY);
149             break;
150         case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
151             STANDBYSERVICE_LOGI("bundle mgr service is removed!");
152             dependsReady_ &= (~BUNDLE_MGR_READY);
153             break;
154         case POWER_MANAGER_SERVICE_ID:
155             STANDBYSERVICE_LOGI("power service is removed!");
156             dependsReady_ &= (~POWER_SERVICE_READY);
157             break;
158         case APP_MGR_SERVICE_ID:
159             STANDBYSERVICE_LOGI("app mgr service is removed!");
160             dependsReady_ &= (~APP_MGR_SERVICE_READY);
161             StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
162             break;
163         case MULTIMODAL_INPUT_SERVICE_ID:
164             STANDBYSERVICE_LOGI("multi modal input service  is removed!");
165             dependsReady_ &= (~MULTIMODAL_INPUT_SERVICE_READY);
166             break;
167         default:
168             NotifySystemAbilityStatusChanged(false, systemAbilityId);
169             break;
170     }
171     if (dependsReady_ != ALL_DEPENDS_READY) {
172         STANDBYSERVICE_LOGI("necessary system service for standby service has been unsatisfied");
173         StandbyServiceImpl::GetInstance()->UninitReadyState();
174     }
175 }
176 
SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)177 ErrCode StandbyService::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
178 {
179     if (state_ != ServiceRunningState::STATE_RUNNING) {
180         STANDBYSERVICE_LOGW("standby service is not running");
181         return ERR_STANDBY_SYS_NOT_READY;
182     }
183     return StandbyServiceImpl::GetInstance()->SubscribeStandbyCallback(subscriber);
184 }
185 
UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)186 ErrCode StandbyService::UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
187 {
188     if (state_ != ServiceRunningState::STATE_RUNNING) {
189         STANDBYSERVICE_LOGW("standby service is not running");
190         return ERR_STANDBY_SYS_NOT_READY;
191     }
192     return StandbyServiceImpl::GetInstance()->UnsubscribeStandbyCallback(subscriber);
193 }
194 
ApplyAllowResource(const sptr<ResourceRequest> & resourceRequest)195 ErrCode StandbyService::ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
196 {
197     if (state_ != ServiceRunningState::STATE_RUNNING) {
198         STANDBYSERVICE_LOGW("standby service is not running");
199         return ERR_STANDBY_SYS_NOT_READY;
200     }
201     return StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
202 }
203 
UnapplyAllowResource(const sptr<ResourceRequest> & resourceRequest)204 ErrCode StandbyService::UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
205 {
206     if (state_ != ServiceRunningState::STATE_RUNNING) {
207         STANDBYSERVICE_LOGW("standby service is not running");
208         return ERR_STANDBY_SYS_NOT_READY;
209     }
210     return StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
211 }
212 
GetAllowList(uint32_t allowType,std::vector<AllowInfo> & allowInfoList,uint32_t reasonCode)213 ErrCode StandbyService::GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList,
214     uint32_t reasonCode)
215 {
216     if (state_ != ServiceRunningState::STATE_RUNNING) {
217         STANDBYSERVICE_LOGW("standby service is not running");
218         return ERR_STANDBY_SYS_NOT_READY;
219     }
220     return StandbyServiceImpl::GetInstance()->GetAllowList(allowType, allowInfoList, reasonCode);
221 }
222 
IsDeviceInStandby(bool & isStandby)223 ErrCode StandbyService::IsDeviceInStandby(bool& isStandby)
224 {
225     if (state_ != ServiceRunningState::STATE_RUNNING) {
226         STANDBYSERVICE_LOGW("standby service is not running");
227         return ERR_STANDBY_SYS_NOT_READY;
228     }
229     return StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby);
230 }
231 
AddPluginSysAbilityListener(int32_t systemAbilityId)232 void StandbyService::AddPluginSysAbilityListener(int32_t systemAbilityId)
233 {
234     std::lock_guard<std::mutex> pluginListenerLock(listenedSALock_);
235     STANDBYSERVICE_LOGI("add listener to system ability %{public}d", systemAbilityId);
236     AddSystemAbilityListener(systemAbilityId);
237 }
238 
NotifySystemAbilityStatusChanged(bool isAdded,int32_t systemAbilityId)239 ErrCode StandbyService::NotifySystemAbilityStatusChanged(bool isAdded, int32_t systemAbilityId)
240 {
241     StandbyMessage standbyMessage{StandbyMessageType::SYS_ABILITY_STATUS_CHANGED};
242     standbyMessage.want_ = AAFwk::Want {};
243     standbyMessage.want_->SetParam(SA_STATUS, isAdded);
244     standbyMessage.want_->SetParam(SA_ID, systemAbilityId);
245     StandbyServiceImpl::GetInstance()->DispatchEvent(standbyMessage);
246     return ERR_OK;
247 }
248 
OnStop()249 void StandbyService::OnStop()
250 {
251     StandbyServiceImpl::GetInstance()->UnInit();
252     state_ = ServiceRunningState::STATE_NOT_START;
253     STANDBYSERVICE_LOGI("standby service task manager stop");
254 }
255 
ReportWorkSchedulerStatus(bool started,int32_t uid,const std::string & bundleName)256 ErrCode StandbyService::ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName)
257 {
258     if (state_ != ServiceRunningState::STATE_RUNNING) {
259         STANDBYSERVICE_LOGW("standby service is not running");
260         return ERR_STANDBY_SYS_NOT_READY;
261     }
262     return StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(started, uid, bundleName);
263 }
264 
GetRestrictList(uint32_t restrictType,std::vector<AllowInfo> & restrictInfoList,uint32_t reasonCode)265 ErrCode StandbyService::GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
266     uint32_t reasonCode)
267 {
268     if (state_ != ServiceRunningState::STATE_RUNNING) {
269         STANDBYSERVICE_LOGW("standby service is not running");
270         return ERR_STANDBY_SYS_NOT_READY;
271     }
272     return StandbyServiceImpl::GetInstance()->GetRestrictList(restrictType, restrictInfoList, reasonCode);
273 }
274 
IsStrategyEnabled(const std::string & strategyName,bool & isEnabled)275 ErrCode StandbyService::IsStrategyEnabled(const std::string& strategyName, bool& isEnabled)
276 {
277     if (state_ != ServiceRunningState::STATE_RUNNING) {
278         STANDBYSERVICE_LOGW("standby service is not running");
279         return ERR_STANDBY_SYS_NOT_READY;
280     }
281     return StandbyServiceImpl::GetInstance()->IsStrategyEnabled(strategyName, isEnabled);
282 }
283 
ReportDeviceStateChanged(DeviceStateType type,bool enabled)284 ErrCode StandbyService::ReportDeviceStateChanged(DeviceStateType type, bool enabled)
285 {
286     if (state_ != ServiceRunningState::STATE_RUNNING) {
287         STANDBYSERVICE_LOGW("standby service is not running");
288         return ERR_STANDBY_SYS_NOT_READY;
289     }
290     return StandbyServiceImpl::GetInstance()->ReportDeviceStateChanged(type, enabled);
291 }
292 
Dump(int32_t fd,const std::vector<std::u16string> & args)293 int32_t StandbyService::Dump(int32_t fd, const std::vector<std::u16string>& args)
294 {
295     std::vector<std::string> argsInStr;
296     std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
297         [](const std::u16string& arg) {
298         return Str16ToStr8(arg);
299     });
300     std::string result;
301     StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
302     if (!SaveStringToFd(fd, result)) {
303         STANDBYSERVICE_LOGE("StandbyService dump save string to fd failed!");
304         return ERR_STANDBY_DUMP_SAVE_DENIED;
305     }
306     return ERR_OK;
307 }
308 }  // namespace DevStandbyMgr
309 }  // namespace OHOS
310