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