1 /*
2 * Copyright (c) 2021-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 "display_power_mgr_client.h"
17
18 #include <if_system_ability_manager.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 #include "new"
22 #include "refbase.h"
23 #include "iremote_broker.h"
24 #include "iremote_object.h"
25 #include "display_log.h"
26 #include "display_common.h"
27 #include "display_power_info.h"
28 #include "idisplay_power_callback.h"
29 #include "idisplay_power_mgr.h"
30 #include "power_state_machine_info.h"
31
32 namespace OHOS {
33 namespace DisplayPowerMgr {
34 DisplayPowerMgrClient::DisplayPowerMgrClient() = default;
35 DisplayPowerMgrClient::~DisplayPowerMgrClient() = default;
36
GetProxy()37 sptr<IDisplayPowerMgr> DisplayPowerMgrClient::GetProxy()
38 {
39 std::lock_guard lock(mutex_);
40 if (proxy_ != nullptr) {
41 return proxy_;
42 }
43
44 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 if (sam == nullptr) {
46 DISPLAY_HILOGE(COMP_FWK, "Failed to get system ability manager");
47 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
48 return nullptr;
49 }
50 sptr<IRemoteObject> obj = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID);
51 if (obj == nullptr) {
52 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
53 static uint32_t count = 0;
54 DISPLAY_HILOGE(COMP_FWK, "Failed to get display manager service, count=%{public}u", ++count);
55 return nullptr;
56 }
57 sptr<IRemoteObject::DeathRecipient> dr = new DisplayDeathRecipient(*this);
58 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
59 DISPLAY_HILOGE(COMP_FWK, "Failed to add death recipient");
60 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
61 return nullptr;
62 }
63
64 proxy_ = iface_cast<IDisplayPowerMgr>(obj);
65 deathRecipient_ = dr;
66 DISPLAY_HILOGI(COMP_FWK, "Succeed to connect display manager service, pid=%{public}d", getpid());
67 return proxy_;
68 }
69
OnRemoteDied(const wptr<IRemoteObject> & remote)70 void DisplayPowerMgrClient::OnRemoteDied(const wptr<IRemoteObject>& remote)
71 {
72 if (remote == nullptr) {
73 DISPLAY_HILOGE(COMP_FWK, "remote is nullptr");
74 return;
75 }
76
77 std::lock_guard lock(mutex_);
78 RETURN_IF(proxy_ == nullptr);
79
80 auto serviceRemote = proxy_->AsObject();
81 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
82 serviceRemote->RemoveDeathRecipient(deathRecipient_);
83 proxy_ = nullptr;
84 }
85 }
86
SetDisplayState(DisplayState state,PowerMgr::StateChangeReason reason,uint32_t id)87 bool DisplayPowerMgrClient::SetDisplayState(DisplayState state,
88 PowerMgr::StateChangeReason reason, uint32_t id)
89 {
90 auto proxy = GetProxy();
91 RETURN_IF_WITH_RET(proxy == nullptr, false);
92 return proxy->SetDisplayState(id, state, static_cast<uint32_t>(reason));
93 }
94
GetDisplayState(uint32_t id)95 DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id)
96 {
97 auto proxy = GetProxy();
98 if (proxy == nullptr) {
99 return DisplayState::DISPLAY_UNKNOWN;
100 }
101 return proxy->GetDisplayState(id);
102 }
103
GetDisplayIds()104 std::vector<uint32_t> DisplayPowerMgrClient::GetDisplayIds()
105 {
106 auto proxy = GetProxy();
107 if (proxy == nullptr) {
108 return {};
109 }
110 return proxy->GetDisplayIds();
111 }
112
GetMainDisplayId()113 int32_t DisplayPowerMgrClient::GetMainDisplayId()
114 {
115 auto proxy = GetProxy();
116 RETURN_IF_WITH_RET(proxy == nullptr, INVALID_DISPLAY_ID);
117 return static_cast<int32_t>(proxy->GetMainDisplayId());
118 }
119
SetBrightness(uint32_t value,uint32_t displayId,bool continuous)120 bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId, bool continuous)
121 {
122 auto proxy = GetProxy();
123 RETURN_IF_WITH_RET(proxy == nullptr, false);
124 return proxy->SetBrightness(value, displayId, continuous);
125 }
126
DiscountBrightness(double discount,uint32_t displayId)127 bool DisplayPowerMgrClient::DiscountBrightness(double discount, uint32_t displayId)
128 {
129 auto proxy = GetProxy();
130 RETURN_IF_WITH_RET(proxy == nullptr, false);
131 return proxy->DiscountBrightness(discount, displayId);
132 }
133
OverrideBrightness(uint32_t value,uint32_t displayId,uint32_t duration)134 bool DisplayPowerMgrClient::OverrideBrightness(uint32_t value, uint32_t displayId, uint32_t duration)
135 {
136 auto proxy = GetProxy();
137 RETURN_IF_WITH_RET(proxy == nullptr, false);
138 return proxy->OverrideBrightness(value, displayId, duration);
139 }
140
OverrideDisplayOffDelay(uint32_t delayMs)141 bool DisplayPowerMgrClient::OverrideDisplayOffDelay(uint32_t delayMs)
142 {
143 auto proxy = GetProxy();
144 RETURN_IF_WITH_RET(proxy == nullptr, false);
145 return proxy->OverrideDisplayOffDelay(delayMs);
146 }
147
RestoreBrightness(uint32_t displayId,uint32_t duration)148 bool DisplayPowerMgrClient::RestoreBrightness(uint32_t displayId, uint32_t duration)
149 {
150 auto proxy = GetProxy();
151 RETURN_IF_WITH_RET(proxy == nullptr, false);
152 return proxy->RestoreBrightness(displayId, duration);
153 }
154
GetBrightness(uint32_t displayId)155 uint32_t DisplayPowerMgrClient::GetBrightness(uint32_t displayId)
156 {
157 auto proxy = GetProxy();
158 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_OFF);
159 return proxy->GetBrightness(displayId);
160 }
161
GetDefaultBrightness()162 uint32_t DisplayPowerMgrClient::GetDefaultBrightness()
163 {
164 auto proxy = GetProxy();
165 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_DEFAULT);
166 return proxy->GetDefaultBrightness();
167 }
168
GetMaxBrightness()169 uint32_t DisplayPowerMgrClient::GetMaxBrightness()
170 {
171 auto proxy = GetProxy();
172 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MAX);
173 return proxy->GetMaxBrightness();
174 }
175
GetMinBrightness()176 uint32_t DisplayPowerMgrClient::GetMinBrightness()
177 {
178 auto proxy = GetProxy();
179 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MIN);
180 return proxy->GetMinBrightness();
181 }
182
AdjustBrightness(uint32_t value,uint32_t duration,uint32_t id)183 bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id)
184 {
185 auto proxy = GetProxy();
186 RETURN_IF_WITH_RET(proxy == nullptr, false);
187 return proxy->AdjustBrightness(id, value, duration);
188 }
189
AutoAdjustBrightness(bool enable)190 bool DisplayPowerMgrClient::AutoAdjustBrightness(bool enable)
191 {
192 auto proxy = GetProxy();
193 RETURN_IF_WITH_RET(proxy == nullptr, false);
194 return proxy->AutoAdjustBrightness(enable);
195 }
196
IsAutoAdjustBrightness()197 bool DisplayPowerMgrClient::IsAutoAdjustBrightness()
198 {
199 auto proxy = GetProxy();
200 RETURN_IF_WITH_RET(proxy == nullptr, false);
201 return proxy->IsAutoAdjustBrightness();
202 }
203
RegisterCallback(sptr<IDisplayPowerCallback> callback)204 bool DisplayPowerMgrClient::RegisterCallback(sptr<IDisplayPowerCallback> callback)
205 {
206 if (callback == nullptr) {
207 DISPLAY_HILOGE(COMP_FWK, "callback is nullptr");
208 return false;
209 }
210
211 auto proxy = GetProxy();
212 RETURN_IF_WITH_RET(proxy == nullptr, false);
213 return proxy->RegisterCallback(callback);
214 }
215
BoostBrightness(int32_t timeoutMs,uint32_t displayId)216 bool DisplayPowerMgrClient::BoostBrightness(int32_t timeoutMs, uint32_t displayId)
217 {
218 auto proxy = GetProxy();
219 RETURN_IF_WITH_RET(proxy == nullptr, false);
220 return proxy->BoostBrightness(timeoutMs, displayId);
221 }
222
CancelBoostBrightness(uint32_t displayId)223 bool DisplayPowerMgrClient::CancelBoostBrightness(uint32_t displayId)
224 {
225 auto proxy = GetProxy();
226 RETURN_IF_WITH_RET(proxy == nullptr, false);
227 return proxy->CancelBoostBrightness(displayId);
228 }
229
GetDeviceBrightness(uint32_t displayId)230 uint32_t DisplayPowerMgrClient::GetDeviceBrightness(uint32_t displayId)
231 {
232 auto proxy = GetProxy();
233 RETURN_IF_WITH_RET(proxy == nullptr, 0);
234 return proxy->GetDeviceBrightness(displayId);
235 }
236
SetCoordinated(bool coordinated,uint32_t displayId)237 bool DisplayPowerMgrClient::SetCoordinated(bool coordinated, uint32_t displayId)
238 {
239 auto proxy = GetProxy();
240 RETURN_IF_WITH_RET(proxy == nullptr, 0);
241 return proxy->SetCoordinated(coordinated, displayId);
242 }
243
SetLightBrightnessThreshold(std::vector<int32_t> threshold,sptr<IDisplayBrightnessCallback> callback)244 uint32_t DisplayPowerMgrClient::SetLightBrightnessThreshold(
245 std::vector<int32_t> threshold, sptr<IDisplayBrightnessCallback> callback)
246 {
247 auto proxy = GetProxy();
248 RETURN_IF_WITH_RET(proxy == nullptr, 0);
249 return proxy->SetLightBrightnessThreshold(threshold, callback);
250 }
251
GetError()252 DisplayErrors DisplayPowerMgrClient::GetError()
253 {
254 if (lastError_ != DisplayErrors::ERR_OK) {
255 DisplayErrors tmpError = lastError_;
256 lastError_ = DisplayErrors::ERR_OK;
257 return tmpError;
258 }
259 auto proxy = GetProxy();
260 RETURN_IF_WITH_RET(proxy == nullptr, DisplayErrors::ERR_CONNECTION_FAIL);
261 return proxy->GetError();
262 }
263
SetMaxBrightness(double value,uint32_t enterTestMode)264 bool DisplayPowerMgrClient::SetMaxBrightness(double value, uint32_t enterTestMode)
265 {
266 auto proxy = GetProxy();
267 RETURN_IF_WITH_RET(proxy == nullptr, false);
268 return proxy->SetMaxBrightness(value, enterTestMode);
269 }
270
SetMaxBrightnessNit(uint32_t maxNit,uint32_t enterTestMode)271 bool DisplayPowerMgrClient::SetMaxBrightnessNit(uint32_t maxNit, uint32_t enterTestMode)
272 {
273 auto proxy = GetProxy();
274 RETURN_IF_WITH_RET(proxy == nullptr, false);
275 return proxy->SetMaxBrightnessNit(maxNit, enterTestMode);
276 }
277 } // namespace DisplayPowerMgr
278 } // namespace OHOS
279