1 /*
2 * Copyright (c) 2025 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 <map>
17 #include "ohos.brightness.proj.hpp"
18 #include "taihe/runtime.hpp"
19 #include "stdexcept"
20 #include "display_power_mgr_client.h"
21 #include "display_log.h"
22 #include "display_mgr_errors.h"
23 #include "ohos.brightness.impl.hpp"
24
25 using namespace taihe;
26 using namespace OHOS::DisplayPowerMgr;
27
28 namespace {
29 const uint32_t MAX_BRIGHTNESS = DisplayPowerMgrClient::GetInstance().GetMaxBrightness();
30 const uint32_t MIN_BRIGHTNESS = DisplayPowerMgrClient::GetInstance().GetMinBrightness();
31
32 std::map<DisplayErrors, std::string> g_errorTable = {
33 {DisplayErrors::ERR_CONNECTION_FAIL, "Failed to connect to the service."},
34 {DisplayErrors::ERR_PERMISSION_DENIED, "Permission is denied" },
35 {DisplayErrors::ERR_SYSTEM_API_DENIED, "System permission is denied" },
36 {DisplayErrors::ERR_PARAM_INVALID, "Invalid input parameter." }
37 };
38
SetValueContinuous(int32_t value,bool continuous)39 void SetValueContinuous(int32_t value, bool continuous)
40 {
41 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "ets brightness interface");
42 DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness: %{public}d, %{public}d", value, continuous);
43 value = value > static_cast<int32_t>(MAX_BRIGHTNESS) ? static_cast<int32_t>(MAX_BRIGHTNESS) : value;
44 value = value < static_cast<int32_t>(MIN_BRIGHTNESS) ? static_cast<int32_t>(MIN_BRIGHTNESS) : value;
45 bool isSucc = DisplayPowerMgrClient::GetInstance().SetBrightness(value, 0, continuous);
46 if (!isSucc) {
47 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Failed to set brightness: %{public}d", value);
48 DisplayErrors error = DisplayPowerMgrClient::GetInstance().GetError();
49 if (error != DisplayErrors::ERR_OK) {
50 taihe::set_business_error(static_cast<int32_t>(error), g_errorTable[error]);
51 }
52 }
53 }
54
SetValueInt(int32_t value)55 void SetValueInt(int32_t value)
56 {
57 bool continuous = false;
58 SetValueContinuous(value, continuous);
59 }
60 } // namespace
61
62 // Since these macros are auto-generate, lint will cause false positive
63 // NOLINTBEGIN
64 TH_EXPORT_CPP_API_SetValueContinuous(SetValueContinuous);
65 TH_EXPORT_CPP_API_SetValueInt(SetValueInt);
66 // NOLINTEND