• 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 
ReportSceneInfo(uint32_t resType,int64_t value,const std::string & sceneInfo)202 ErrCode StandbyServiceClient::ReportSceneInfo(uint32_t resType, int64_t value, const std::string &sceneInfo)
203 {
204     std::lock_guard<std::mutex> lock(mutex_);
205     if (!GetStandbyServiceProxy()) {
206         STANDBYSERVICE_LOGE("get standby service proxy failed");
207         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
208     }
209     return standbyServiceProxy_->ReportSceneInfo(resType, value, sceneInfo);
210 }
211 
ReportDeviceStateChanged(DeviceStateType type,bool enabled)212 ErrCode StandbyServiceClient::ReportDeviceStateChanged(DeviceStateType type, bool enabled)
213 {
214     std::lock_guard<std::mutex> lock(mutex_);
215     STANDBYSERVICE_LOGI("device state changed, state type: %{public}d, enabled: %{public}d",
216         static_cast<int32_t>(type), enabled);
217     if (!GetStandbyServiceProxy()) {
218         STANDBYSERVICE_LOGE("get standby service proxy failed");
219         return ERR_STANDBY_SERVICE_NOT_CONNECTED;
220     }
221     int32_t transType = static_cast<int32_t>(type);
222     return standbyServiceProxy_->ReportDeviceStateChanged(transType, enabled);
223 }
224 
GetStandbyServiceProxy()225 bool StandbyServiceClient::GetStandbyServiceProxy()
226 {
227     if (standbyServiceProxy_ != nullptr) {
228         return true;
229     }
230     sptr<ISystemAbilityManager> systemAbilityManager =
231         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
232     if (systemAbilityManager == nullptr) {
233         STANDBYSERVICE_LOGE("get standby service proxy failed");
234         return false;
235     }
236 
237     sptr<IRemoteObject> remoteObject =
238         systemAbilityManager->GetSystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID);
239     if (remoteObject == nullptr) {
240         STANDBYSERVICE_LOGE("get standby service system ability failed");
241         return false;
242     }
243 
244     standbyServiceProxy_ = iface_cast<IStandbyService>(remoteObject);
245     if ((standbyServiceProxy_ == nullptr) || (standbyServiceProxy_->AsObject() == nullptr)) {
246         STANDBYSERVICE_LOGE("standby service proxy iface_cast from remote Onject failed");
247         return false;
248     }
249 
250     deathRecipient_ = new (std::nothrow) StandbyServiceDeathRecipient(*this);
251     if (deathRecipient_ == nullptr) {
252         return false;
253     }
254 
255     standbyServiceProxy_->AsObject()->AddDeathRecipient(deathRecipient_);
256     return true;
257 }
258 
ResetStandbyServiceClient()259 void StandbyServiceClient::ResetStandbyServiceClient()
260 {
261     std::lock_guard<std::mutex> lock(mutex_);
262     if ((standbyServiceProxy_ != nullptr)&& (standbyServiceProxy_->AsObject() != nullptr)) {
263         standbyServiceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
264     }
265     standbyServiceProxy_ = nullptr;
266 }
267 
StandbyServiceDeathRecipient(StandbyServiceClient & standbyServiceClient)268 StandbyServiceClient::StandbyServiceDeathRecipient::StandbyServiceDeathRecipient(
269     StandbyServiceClient& standbyServiceClient)
270     : standbyServiceClient_(standbyServiceClient) {}
271 
~StandbyServiceDeathRecipient()272 StandbyServiceClient::StandbyServiceDeathRecipient::~StandbyServiceDeathRecipient() {}
273 
OnRemoteDied(const wptr<IRemoteObject> & remote)274 void StandbyServiceClient::StandbyServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
275 {
276     standbyServiceClient_.ResetStandbyServiceClient();
277 }
278 }  // namespace DevStandbyMgr
279 }  // namespace OHOS
280