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