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