1 /*
2 * Copyright (c) 2021-2022 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 "brightness.h"
17
18 #include <utility>
19
20 #include "napi/native_common.h"
21 #include "power_state_machine_info.h"
22
23 #include "display_common.h"
24 #include "display_log.h"
25 #include "display_power_mgr_client.h"
26
27 using namespace OHOS::PowerMgr;
28
29 namespace OHOS {
30 namespace DisplayPowerMgr {
31 namespace {
32 const uint32_t MAX_ARGC = 1;
33 const uint32_t ARGV_ONE = 0;
34 const uint32_t MAX_FAIL_ARGC = 2;
35 const int32_t MAX_BRIGHTNESS = 255;
36 const int32_t MIN_BRIGHTNESS = 1;
37 const int32_t BRIGHTNESS_OFF = 0;
38
39 const std::string FUNC_SUCEESS_NAME = "success";
40 const std::string FUNC_FAIL_NAME = "fail";
41 const std::string FUNC_COMPLETE_NAME = "complete";
42
43 const int32_t COMMON_ERROR_COED = 200;
44 const int32_t INPUT_ERROR_CODE = 202;
45
46 const std::string SET_VALUE_ERROR_MGR = "value is not an available number";
47 const std::string GET_VALUE_ERROR_MGR = "get system screen brightness fail";
48 const std::string SET_MODE_ERROR_MGR = "value is not an available number";
49 const std::string SET_MODE_NOT_SUPPORTED_ERROR_MGR = "Auto adjusting brightness is not supported";
50 const std::string SET_KEEP_SCREENON_ERROR_MGR = "value is not an available boolean";
51 } // namespace
52
53 std::map<DisplayErrors, std::string> Brightness::Result::errorTable_ = {
54 {DisplayErrors::ERR_CONNECTION_FAIL, "Connecting to the service failed."},
55 {DisplayErrors::ERR_PERMISSION_DENIED, "Permission is denied" },
56 {DisplayErrors::ERR_PARAM_INVALID, "Invalid input parameter." }
57 };
58
Brightness(napi_env env,std::shared_ptr<RunningLock> runningLock)59 Brightness::Brightness(napi_env env, std::shared_ptr<RunningLock> runningLock) : env_(env), runningLock_(runningLock) {}
60
~Brightness()61 Brightness::~Brightness()
62 {
63 ReleaseReference(successRef_);
64 ReleaseReference(failRef_);
65 ReleaseReference(completeRef_);
66 ReleaseReference(napiValRef_);
67 }
68
GetValue()69 void Brightness::GetValue()
70 {
71 uint32_t brightness = brightnessInfo_.GetBrightness();
72 if (BRIGHTNESS_OFF >= brightness || brightness > MAX_BRIGHTNESS) {
73 result_.Error(COMMON_ERROR_COED, GET_VALUE_ERROR_MGR);
74 } else {
75 result_.SetResult(BRIGHTNESS_VALUE, brightness);
76 }
77 ExecuteCallback();
78 }
79
SetValue(napi_callback_info & info)80 void Brightness::SetValue(napi_callback_info& info)
81 {
82 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Brightness interface");
83 napi_value napiBrightness = GetCallbackInfo(info, napi_number);
84 if (napiBrightness == nullptr) {
85 result_.ThrowError(env_, DisplayErrors::ERR_PARAM_INVALID);
86 return;
87 }
88
89 int32_t value = MIN_BRIGHTNESS;
90 if (napi_ok != napi_get_value_int32(env_, napiBrightness, &value)) {
91 DISPLAY_HILOGW(COMP_FWK, "Failed to get the input number");
92 result_.ThrowError(env_, DisplayErrors::ERR_PARAM_INVALID);
93 return;
94 }
95 if (!brightnessInfo_.SetBrightness(value)) {
96 DisplayErrors error = brightnessInfo_.GetServiceError();
97 if (error != DisplayErrors::ERR_OK) {
98 result_.ThrowError(env_, error);
99 }
100 }
101 }
102
SystemSetValue()103 void Brightness::SystemSetValue()
104 {
105 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "System brightness interface");
106 if (napiValRef_ == nullptr) {
107 result_.Error(INPUT_ERROR_CODE, SET_VALUE_ERROR_MGR);
108 } else {
109 int32_t brightness = MIN_BRIGHTNESS;
110 napi_value napiVal = nullptr;
111 napi_get_reference_value(env_, napiValRef_, &napiVal);
112 napi_get_value_int32(env_, napiVal, &brightness);
113 brightnessInfo_.SetBrightness(brightness);
114 ReleaseReference(napiValRef_);
115 }
116 ExecuteCallback();
117 }
118
GetMode()119 void Brightness::GetMode()
120 {
121 int32_t isAuto = brightnessInfo_.GetAutoMode();
122 result_.SetResult(BRIGHTNESS_MODE, isAuto);
123 ExecuteCallback();
124 }
125
SetMode()126 void Brightness::SetMode()
127 {
128 DISPLAY_HILOGD(COMP_FWK, "Set auto brightness");
129 if (napiValRef_ == nullptr) {
130 result_.Error(INPUT_ERROR_CODE, SET_MODE_ERROR_MGR);
131 } else {
132 int32_t mode = 0;
133 napi_value napiMode = nullptr;
134 napi_get_reference_value(env_, napiValRef_, &napiMode);
135 napi_get_value_int32(env_, napiMode, &mode);
136 if (!brightnessInfo_.SetAutoMode(static_cast<bool>(mode))) {
137 result_.Error(COMMON_ERROR_COED, SET_MODE_NOT_SUPPORTED_ERROR_MGR);
138 }
139 ReleaseReference(napiValRef_);
140 }
141 ExecuteCallback();
142 }
143
SetKeepScreenOn()144 void Brightness::SetKeepScreenOn()
145 {
146 DISPLAY_HILOGD(COMP_FWK, "Set keep screen on");
147 if (napiValRef_ == nullptr) {
148 result_.Error(INPUT_ERROR_CODE, SET_KEEP_SCREENON_ERROR_MGR);
149 } else {
150 napi_value napiKeep = nullptr;
151 napi_get_reference_value(env_, napiValRef_, &napiKeep);
152 bool screenOn = false;
153 napi_get_value_bool(env_, napiKeep, &screenOn);
154 brightnessInfo_.ScreenOn(screenOn, runningLock_);
155 ReleaseReference(napiValRef_);
156 }
157 ExecuteCallback();
158 }
159
GetCallbackInfo(napi_callback_info & info,napi_valuetype checkType)160 napi_value Brightness::GetCallbackInfo(napi_callback_info& info, napi_valuetype checkType)
161 {
162 size_t argc = MAX_ARGC;
163 napi_value argv[argc];
164 napi_value thisVar = nullptr;
165 void* data = nullptr;
166 if (napi_ok != napi_get_cb_info(env_, info, &argc, argv, &thisVar, &data)) {
167 DISPLAY_HILOGW(COMP_FWK, "Failed to get the input parameter");
168 return nullptr;
169 }
170
171 if (argc != MAX_ARGC) {
172 DISPLAY_HILOGW(COMP_FWK, "Lack of parameter");
173 return nullptr;
174 }
175
176 napi_value options = argv[ARGV_ONE];
177 RETURN_IF_WITH_RET(!CheckValueType(options, checkType), nullptr);
178 return options;
179 }
180
CreateCallbackRef(napi_value & options)181 bool Brightness::CreateCallbackRef(napi_value& options)
182 {
183 RETURN_IF_WITH_RET(!CheckValueType(options, napi_object), false);
184
185 napi_value succCallBack = GetOptions(options, FUNC_SUCEESS_NAME, napi_function);
186 if (succCallBack != nullptr) {
187 napi_create_reference(env_, succCallBack, 1, &successRef_);
188 }
189
190 napi_value failCallBack = GetOptions(options, FUNC_FAIL_NAME, napi_function);
191 if (failCallBack != nullptr) {
192 napi_create_reference(env_, failCallBack, 1, &failRef_);
193 }
194
195 napi_value completeCallBack = GetOptions(options, FUNC_COMPLETE_NAME, napi_function);
196 if (completeCallBack != nullptr) {
197 napi_create_reference(env_, completeCallBack, 1, &completeRef_);
198 }
199 return true;
200 }
201
CreateValueRef(napi_value & options,const std::string & valName,napi_valuetype checkType)202 void Brightness::CreateValueRef(napi_value& options, const std::string& valName, napi_valuetype checkType)
203 {
204 napi_value value = GetOptions(options, valName, checkType);
205 if (value != nullptr) {
206 napi_create_reference(env_, value, 1, &napiValRef_);
207 }
208 }
209
Error(int32_t code,const std::string & msg)210 void Brightness::Result::Error(int32_t code, const std::string& msg)
211 {
212 code_ = code;
213 msg_ = msg;
214 DISPLAY_HILOGW(COMP_FWK, "Error message, code: %{public}d, msg: %{public}s", code_, msg_.c_str());
215 }
216
GetError(napi_env env,napi_value * error,size_t & size) const217 void Brightness::Result::GetError(napi_env env, napi_value* error, size_t& size) const
218 {
219 if (!error) {
220 DISPLAY_HILOGW(COMP_FWK, "error is null");
221 return;
222 }
223 napi_value data = nullptr;
224 napi_value code = nullptr;
225 napi_create_string_utf8(env, msg_.c_str(), msg_.size(), &data);
226 napi_create_int32(env, code_, &code);
227 size = MAX_FAIL_ARGC;
228 error[ARGV_ONE] = data;
229 error[MAX_ARGC] = code;
230 }
231
GetError(napi_env & env)232 napi_value Brightness::Result::GetError(napi_env& env)
233 {
234 napi_value napiError = nullptr;
235 if (!IsError()) {
236 napi_get_undefined(env, &napiError);
237 return napiError;
238 }
239
240 std::string msg;
241 auto item = errorTable_.find(static_cast<DisplayErrors>(code_));
242 if (item != errorTable_.end()) {
243 msg = item->second;
244 }
245 napi_value napiMsg;
246 NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.size(), &napiMsg));
247 NAPI_CALL(env, napi_create_error(env, nullptr, napiMsg, &napiError));
248
249 napi_value napiCode;
250 NAPI_CALL(env, napi_create_int32(env, code_, &napiCode));
251
252 napi_set_named_property(env, napiError, "code", napiCode);
253 napi_set_named_property(env, napiError, "message", napiMsg);
254
255 DISPLAY_HILOGW(COMP_FWK, "throw error code: %{public}d, msg: %{public}s,", code_, msg.c_str());
256 return napiError;
257 }
258
ThrowError(napi_env & env,DisplayErrors code)259 napi_value Brightness::Result::ThrowError(napi_env& env, DisplayErrors code)
260 {
261 Error(static_cast<int32_t>(code));
262 napi_value error = GetError(env);
263 RETURN_IF_WITH_RET(error == nullptr, nullptr);
264 napi_throw(env, error);
265 return nullptr;
266 }
267
GetResult(napi_env env)268 napi_value Brightness::Result::GetResult(napi_env env)
269 {
270 napi_value result = nullptr;
271 NAPI_CALL(env, napi_create_object(env, &result));
272 for (const auto& it : mapResult_) {
273 napi_value napiValue = 0;
274 NAPI_CALL(env, napi_create_int32(env, it.second, &napiValue));
275 NAPI_CALL(env, napi_set_named_property(env, result, it.first.c_str(), napiValue));
276 }
277 return result;
278 }
279
GetBrightness() const280 uint32_t Brightness::BrightnessInfo::GetBrightness() const
281 {
282 uint32_t brightness = DisplayPowerMgrClient::GetInstance().GetBrightness();
283 DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Get brightness: %{public}d", brightness);
284 return brightness;
285 }
286
SetBrightness(int32_t value)287 bool Brightness::BrightnessInfo::SetBrightness(int32_t value)
288 {
289 DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness: %{public}d", value);
290 value = value > MAX_BRIGHTNESS ? MAX_BRIGHTNESS : value;
291 value = value < MIN_BRIGHTNESS ? MIN_BRIGHTNESS : value;
292 bool isSucc = DisplayPowerMgrClient::GetInstance().SetBrightness(value);
293 if (!isSucc) {
294 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Failed to set brightness: %{public}d", value);
295 }
296 return isSucc;
297 }
298
GetAutoMode() const299 int32_t Brightness::BrightnessInfo::GetAutoMode() const
300 {
301 bool isAuto = DisplayPowerMgrClient::GetInstance().IsAutoAdjustBrightness();
302 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Automatic brightness adjustment: %{public}d", isAuto);
303 return static_cast<int32_t>(isAuto);
304 }
305
SetAutoMode(bool mode)306 bool Brightness::BrightnessInfo::SetAutoMode(bool mode)
307 {
308 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "AutoAdjustBrightness begin");
309 bool isSucc = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(mode);
310 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "set auto brightness mode: %{public}d, succ: %{public}d", mode, isSucc);
311 return isSucc;
312 }
313
ScreenOn(bool keep,std::shared_ptr<RunningLock> & runningLock)314 void Brightness::BrightnessInfo::ScreenOn(bool keep, std::shared_ptr<RunningLock>& runningLock)
315 {
316 if (runningLock != nullptr) {
317 DISPLAY_HILOGD(COMP_FWK, "Keep screen on, keep: %{public}d, isUsed: %{public}d", keep, runningLock->IsUsed());
318 keep ? runningLock->Lock() : runningLock->UnLock();
319 }
320 }
321
GetServiceError() const322 DisplayErrors Brightness::BrightnessInfo::GetServiceError() const
323 {
324 return DisplayPowerMgrClient::GetInstance().GetError();
325 }
326
ExecuteCallback()327 void Brightness::ExecuteCallback()
328 {
329 bool error = result_.IsError();
330 if (!error) {
331 DISPLAY_HILOGI(COMP_FWK, "Call the js success method");
332 napi_value result = result_.GetResult(env_);
333 size_t argc = result ? MAX_ARGC : 0;
334 CallFunction(successRef_, argc, result ? &result : nullptr);
335 }
336
337 if (error) {
338 DISPLAY_HILOGI(COMP_FWK, "Call the js fail method");
339 size_t argc = MAX_FAIL_ARGC;
340 napi_value argv[argc];
341 result_.GetError(env_, argv, argc);
342 CallFunction(failRef_, argc, argv);
343 }
344 DISPLAY_HILOGI(COMP_FWK, "Call the js complete method");
345 CallFunction(completeRef_, 0, nullptr);
346 }
347
CheckValueType(napi_value & value,napi_valuetype checkType)348 bool Brightness::CheckValueType(napi_value& value, napi_valuetype checkType)
349 {
350 napi_valuetype valueType = napi_undefined;
351 napi_typeof(env_, value, &valueType);
352 if (valueType != checkType) {
353 DISPLAY_HILOGW(COMP_FWK, "Check input parameter error");
354 return false;
355 }
356 return true;
357 }
358
GetOptions(napi_value & options,const std::string & name,napi_valuetype checkType)359 napi_value Brightness::GetOptions(napi_value& options, const std::string& name, napi_valuetype checkType)
360 {
361 napi_value property = nullptr;
362 napi_status status = napi_get_named_property(env_, options, name.c_str(), &property);
363 if (status != napi_ok) {
364 DISPLAY_HILOGW(COMP_FWK, "Failed to get the %{public}s Options property", name.c_str());
365 return nullptr;
366 }
367 if (!CheckValueType(property, checkType)) {
368 DISPLAY_HILOGW(COMP_FWK, "Get %{public}s Options property type mismatch", name.c_str());
369 return nullptr;
370 }
371 return property;
372 }
373
CallFunction(napi_ref & callbackRef,size_t argc,napi_value * response)374 void Brightness::CallFunction(napi_ref& callbackRef, size_t argc, napi_value* response)
375 {
376 RETURN_IF(callbackRef == nullptr);
377
378 napi_value callResult = 0;
379 napi_value callback = nullptr;
380 napi_get_reference_value(env_, callbackRef, &callback);
381 napi_status status = napi_call_function(env_, nullptr, callback, argc, response, &callResult);
382 if (status != napi_ok) {
383 DISPLAY_HILOGW(COMP_FWK, "Failed to call the callback function");
384 }
385 ReleaseReference(callbackRef);
386 }
387
ReleaseReference(napi_ref & ref)388 void Brightness::ReleaseReference(napi_ref& ref)
389 {
390 if (ref != nullptr) {
391 napi_delete_reference(env_, ref);
392 ref = nullptr;
393 }
394 }
395 } // namespace DisplayPowerMgr
396 } // namespace OHOS