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 "action_display.h"
17
18 #include <map>
19 #include "display_power_mgr_client.h"
20 #include "file_operation.h"
21 #include "thermal_hisysevent.h"
22 #include "thermal_service.h"
23 #include "securec.h"
24
25 using namespace OHOS::DisplayPowerMgr;
26 namespace OHOS {
27 namespace PowerMgr {
28 namespace {
29 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
30 constexpr const char* LCD_PATH = "/data/service/el0/thermal/config/lcd";
31 const int MAX_PATH = 256;
32 }
ActionDisplay(const std::string & actionName)33 ActionDisplay::ActionDisplay(const std::string& actionName)
34 {
35 actionName_ = actionName;
36 }
37
InitParams(const std::string & params)38 void ActionDisplay::InitParams(const std::string& params)
39 {
40 }
41
SetStrict(bool flag)42 void ActionDisplay::SetStrict(bool flag)
43 {
44 flag_ = flag;
45 }
46
SetEnableEvent(bool enable)47 void ActionDisplay::SetEnableEvent(bool enable)
48 {
49 enableEvent_ = enable;
50 }
51
AddActionValue(std::string value)52 void ActionDisplay::AddActionValue(std::string value)
53 {
54 THERMAL_HILOGD(COMP_SVC, "value=%{public}s", value.c_str());
55 if (value.empty()) return;
56 valueList_.push_back(atof(value.c_str()));
57 }
58
Execute()59 void ActionDisplay::Execute()
60 {
61 THERMAL_HILOGD(COMP_SVC, "Enter");
62 float value;
63 THERMAL_RETURN_IF (g_service == nullptr);
64 std::string scene = g_service->GetScene();
65 auto iter = g_sceneMap.find(scene);
66 if (iter != g_sceneMap.end()) {
67 value = static_cast<float>(atof(iter->second.c_str()));
68 if ((value != lastValue_) && (!g_service->GetSimulationXml())) {
69 DisplayRequest(value);
70 } else if (value != lastValue_) {
71 DisplayExecution(value);
72 } else {
73 THERMAL_HILOGD(COMP_SVC, "value is not change");
74 }
75 WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
76 g_service->GetObserver()->SetDecisionValue(actionName_, iter->second);
77 lastValue_ = value;
78 return;
79 }
80
81 if (valueList_.empty()) {
82 value = 0;
83 } else {
84 if (flag_) {
85 value = *max_element(valueList_.begin(), valueList_.end());
86 } else {
87 value = *min_element(valueList_.begin(), valueList_.end());
88 }
89 valueList_.clear();
90 }
91
92 if (value != lastValue_) {
93 if (!g_service->GetSimulationXml()) {
94 DisplayRequest(value);
95 } else {
96 DisplayExecution(value);
97 }
98 WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
99 g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
100 lastValue_ = value;
101 }
102 }
103
DisplayRequest(float brightness)104 uint32_t ActionDisplay::DisplayRequest(float brightness)
105 {
106 THERMAL_HILOGD(COMP_SVC, "Enter");
107 if (!DisplayPowerMgrClient::GetInstance().DiscountBrightness(brightness)) {
108 THERMAL_HILOGE(COMP_SVC, "failed to discount brightness");
109 return ERR_INVALID_VALUE;
110 }
111 return ERR_OK;
112 }
113
DisplayExecution(float brightness)114 int32_t ActionDisplay::DisplayExecution(float brightness)
115 {
116 THERMAL_HILOGD(COMP_SVC, "Enter");
117 int32_t ret = -1;
118 char lcdBuf[MAX_PATH] = {0};
119 ret = snprintf_s(lcdBuf, MAX_PATH, sizeof(lcdBuf) - 1, LCD_PATH);
120 if (ret < EOK) {
121 return ret;
122 }
123 std::string valueString = std::to_string(brightness) + "\n";
124 ret = FileOperation::WriteFile(lcdBuf, valueString, valueString.length());
125 if (ret != ERR_OK) {
126 return ret;
127 }
128 return ERR_OK;
129 }
130 } // namespace PowerMgr
131 } // namespace OHOS
132