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_client.h"
17 
18 #include <message_parcel.h>
19 
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "standby_service_errors.h"
23 #include "standby_service_log.h"
24 #include "standby_service_proxy.h"
25 
26 namespace OHOS {
27 namespace DevStandbyMgr {
StandbyServiceClient()28 StandbyServiceClient::StandbyServiceClient() {}
29 
~StandbyServiceClient()30 StandbyServiceClient::~StandbyServiceClient() {}
31 
GetInstance()32 StandbyServiceClient& StandbyServiceClient::GetInstance()
33 {
34     static StandbyServiceClient StandbyServiceClient;
35     return StandbyServiceClient;
36 }
37 
SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)38 ErrCode StandbyServiceClient::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     if (!GetStandbyServiceProxy()) {
42         STANDBYSERVICE_LOGE("get standby service proxy failed");
43         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
44     }
45     if (subscriber == nullptr) {
46         STANDBYSERVICE_LOGE("subscriber is nullptr");
47         return ERR_STANDBY_INVALID_PARAM;
48     }
49     return standbyServiceProxy_->SubscribeStandbyCallback(subscriber);
50 }
51 
UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)52 ErrCode StandbyServiceClient::UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
53 {
54     std::lock_guard<std::mutex> lock(mutex_);
55     if (!GetStandbyServiceProxy()) {
56         STANDBYSERVICE_LOGE("get standby service proxy failed");
57         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
58     }
59     if (subscriber == nullptr) {
60         STANDBYSERVICE_LOGE("subscriber is nullptr");
61         return ERR_STANDBY_INVALID_PARAM;
62     }
63     return standbyServiceProxy_->UnsubscribeStandbyCallback(subscriber);
64 }
65 
ApplyAllowResource(const sptr<ResourceRequest> & resourceRequest)66 ErrCode StandbyServiceClient::ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
67 {
68     std::lock_guard<std::mutex> lock(mutex_);
69     if (!GetStandbyServiceProxy()) {
70         STANDBYSERVICE_LOGE("get standby service proxy failed");
71         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
72     }
73     if (resourceRequest == nullptr) {
74         STANDBYSERVICE_LOGE("resource request is nullptr");
75         return ERR_STANDBY_INVALID_PARAM;
76     }
77     return standbyServiceProxy_->ApplyAllowResource(resourceRequest);
78 }
79 
UnapplyAllowResource(const sptr<ResourceRequest> & resourceRequest)80 ErrCode StandbyServiceClient::UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
81 {
82     std::lock_guard<std::mutex> lock(mutex_);
83     if (!GetStandbyServiceProxy()) {
84         STANDBYSERVICE_LOGE("get standby service proxy failed");
85         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
86     }
87     if (resourceRequest == nullptr) {
88         STANDBYSERVICE_LOGE("resource request is nullptr");
89         return ERR_STANDBY_INVALID_PARAM;
90     }
91     return standbyServiceProxy_->UnapplyAllowResource(resourceRequest);
92 }
93 
GetAllowList(uint32_t allowType,std::vector<AllowInfo> & allowInfoArray,uint32_t reasonCode)94 ErrCode StandbyServiceClient::GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoArray,
95     uint32_t reasonCode)
96 {
97     std::lock_guard<std::mutex> lock(mutex_);
98     if (!GetStandbyServiceProxy()) {
99         STANDBYSERVICE_LOGE("get standby service proxy failed");
100         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
101     }
102     if (!allowInfoArray.empty()) {
103         STANDBYSERVICE_LOGW("allow info array is not empty");
104         allowInfoArray.clear();
105     }
106     return standbyServiceProxy_->GetAllowList(allowType, allowInfoArray, reasonCode);
107 }
108 
IsDeviceInStandby(bool & isStandby)109 ErrCode StandbyServiceClient::IsDeviceInStandby(bool& isStandby)
110 {
111     std::lock_guard<std::mutex> lock(mutex_);
112     if (!GetStandbyServiceProxy()) {
113         STANDBYSERVICE_LOGE("get standby service proxy failed");
114         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
115     }
116     return standbyServiceProxy_->IsDeviceInStandby(isStandby);
117 }
118 
SetNatInterval(uint32_t & type,bool & enable,uint32_t & interval)119 ErrCode StandbyServiceClient::SetNatInterval(uint32_t& type, bool& enable, uint32_t& interval)
120 {
121     std::lock_guard<std::mutex> lock(mutex_);
122     if (!GetStandbyServiceProxy()) {
123         STANDBYSERVICE_LOGE("get standby service proxy failed");
124         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
125     }
126     return standbyServiceProxy_->SetNatInterval(type, enable, interval);
127 }
128 
HandleEvent(const std::shared_ptr<ResData> & resData)129 ErrCode StandbyServiceClient::HandleEvent(const std::shared_ptr<ResData> &resData)
130 {
131     std::string sceneInfo = resData->payload.dump();
132     if (!GetStandbyServiceProxy()) {
133         STANDBYSERVICE_LOGE("get standby service proxy failed");
134         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
135     }
136     return standbyServiceProxy_->HandleEvent(resData->resType, resData->value, sceneInfo);
137 }
138 
ReportWorkSchedulerStatus(bool started,int32_t uid,const std::string & bundleName)139 ErrCode StandbyServiceClient::ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName)
140 {
141     std::lock_guard<std::mutex> lock(mutex_);
142     if (!GetStandbyServiceProxy()) {
143         STANDBYSERVICE_LOGE("get standby service proxy failed");
144         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
145     }
146     return standbyServiceProxy_->ReportWorkSchedulerStatus(started, uid, bundleName);
147 }
148 
GetRestrictList(uint32_t restrictType,std::vector<AllowInfo> & restrictInfoList,uint32_t reasonCode)149 ErrCode StandbyServiceClient::GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
150     uint32_t reasonCode)
151 {
152     std::lock_guard<std::mutex> lock(mutex_);
153     if (!GetStandbyServiceProxy()) {
154         STANDBYSERVICE_LOGE("get standby service proxy failed");
155         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
156     }
157     if (!restrictInfoList.empty()) {
158         STANDBYSERVICE_LOGW("restrict info array is not empty");
159         restrictInfoList.clear();
160     }
161     return standbyServiceProxy_->GetRestrictList(restrictType, restrictInfoList, reasonCode);
162 }
163 
IsStrategyEnabled(const std::string & strategyName,bool & isEnabled)164 ErrCode StandbyServiceClient::IsStrategyEnabled(const std::string& strategyName, bool& isEnabled)
165 {
166     std::lock_guard<std::mutex> lock(mutex_);
167     if (!GetStandbyServiceProxy()) {
168         STANDBYSERVICE_LOGE("get standby service proxy failed");
169         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
170     }
171     return standbyServiceProxy_->IsStrategyEnabled(strategyName, isEnabled);
172 }
173 
ReportPowerOverused(const std::string & module,uint32_t level)174 ErrCode StandbyServiceClient::ReportPowerOverused(const std::string &module, uint32_t level)
175 {
176     std::lock_guard<std::mutex> lock(mutex_);
177     STANDBYSERVICE_LOGI("[PowerOverused] StandbyClient: power overused, module name: %{public}s, "
178         "level: %{public}u.", module.c_str(), level);
179 
180     if (!GetStandbyServiceProxy()) {
181         STANDBYSERVICE_LOGE("get standby service proxy failed");
182         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
183     }
184     return standbyServiceProxy_->ReportPowerOverused(module, level);
185 }
186 
ReportDeviceStateChanged(DeviceStateType type,bool enabled)187 ErrCode StandbyServiceClient::ReportDeviceStateChanged(DeviceStateType type, bool enabled)
188 {
189     std::lock_guard<std::mutex> lock(mutex_);
190     STANDBYSERVICE_LOGI("device state changed, state type: %{public}d, enabled: %{public}d",
191         static_cast<int32_t>(type), enabled);
192     if (!GetStandbyServiceProxy()) {
193         STANDBYSERVICE_LOGE("get standby service proxy failed");
194         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
195     }
196     return standbyServiceProxy_->ReportDeviceStateChanged(type, enabled);
197 }
198 
GetStandbyServiceProxy()199 bool StandbyServiceClient::GetStandbyServiceProxy()
200 {
201     if (standbyServiceProxy_ != nullptr) {
202         return true;
203     }
204     sptr<ISystemAbilityManager> systemAbilityManager =
205         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
206     if (systemAbilityManager == nullptr) {
207         STANDBYSERVICE_LOGE("get standby service proxy failed");
208         return false;
209     }
210 
211     sptr<IRemoteObject> remoteObject =
212         systemAbilityManager->GetSystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID);
213     if (remoteObject == nullptr) {
214         STANDBYSERVICE_LOGE("get standby service system ability failed");
215         return false;
216     }
217 
218     standbyServiceProxy_ = iface_cast<IStandbyService>(remoteObject);
219     if ((standbyServiceProxy_ == nullptr) || (standbyServiceProxy_->AsObject() == nullptr)) {
220         STANDBYSERVICE_LOGE("standby service proxy iface_cast from remote Onject failed");
221         return false;
222     }
223 
224     deathRecipient_ = new (std::nothrow) StandbyServiceDeathRecipient(*this);
225     if (deathRecipient_ == nullptr) {
226         return false;
227     }
228 
229     standbyServiceProxy_->AsObject()->AddDeathRecipient(deathRecipient_);
230     return true;
231 }
232 
ResetStandbyServiceClient()233 void StandbyServiceClient::ResetStandbyServiceClient()
234 {
235     std::lock_guard<std::mutex> lock(mutex_);
236     if ((standbyServiceProxy_ != nullptr)&& (standbyServiceProxy_->AsObject() != nullptr)) {
237         standbyServiceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
238     }
239     standbyServiceProxy_ = nullptr;
240 }
241 
StandbyServiceDeathRecipient(StandbyServiceClient & standbyServiceClient)242 StandbyServiceClient::StandbyServiceDeathRecipient::StandbyServiceDeathRecipient(
243     StandbyServiceClient& standbyServiceClient)
244     : standbyServiceClient_(standbyServiceClient) {}
245 
~StandbyServiceDeathRecipient()246 StandbyServiceClient::StandbyServiceDeathRecipient::~StandbyServiceDeathRecipient() {}
247 
OnRemoteDied(const wptr<IRemoteObject> & remote)248 void StandbyServiceClient::StandbyServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
249 {
250     standbyServiceClient_.ResetStandbyServiceClient();
251 }
252 }  // namespace DevStandbyMgr
253 }  // namespace OHOS