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 "action_display.h"
17
18 #include <cmath>
19
20 #include "constants.h"
21 #include "display_power_mgr_client.h"
22 #include "file_operation.h"
23 #include "thermal_hisysevent.h"
24 #include "thermal_service.h"
25
26 using namespace OHOS::DisplayPowerMgr;
27 namespace OHOS {
28 namespace PowerMgr {
29 namespace {
30 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
31 const std::string LCD_MOCK_PATH = "/data/service/el0/thermal/config/lcd";
32 }
33
ActionDisplay(const std::string & actionName)34 ActionDisplay::ActionDisplay(const std::string& actionName)
35 {
36 actionName_ = actionName;
37 }
38
InitParams(const std::string & params)39 void ActionDisplay::InitParams(const std::string& params)
40 {
41 (void)params;
42 }
43
SetStrict(bool enable)44 void ActionDisplay::SetStrict(bool enable)
45 {
46 isStrict_ = enable;
47 }
48
SetEnableEvent(bool enable)49 void ActionDisplay::SetEnableEvent(bool enable)
50 {
51 enableEvent_ = enable;
52 }
53
AddActionValue(std::string value)54 void ActionDisplay::AddActionValue(std::string value)
55 {
56 if (value.empty()) {
57 return;
58 }
59 valueList_.push_back(static_cast<float>(strtof(value.c_str(), nullptr)));
60 }
61
Execute()62 void ActionDisplay::Execute()
63 {
64 THERMAL_RETURN_IF (g_service == nullptr);
65 float value = GetActionValue();
66 if (fabs(value - lastValue_) > FLOAT_ACCURACY) {
67 if (!g_service->GetSimulationXml()) {
68 RequestDisplay(value);
69 } else {
70 ExecuteMock(value);
71 }
72 WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
73 g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
74 lastValue_ = value;
75 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}f}", actionName_.c_str(), lastValue_);
76 }
77 valueList_.clear();
78 }
79
GetActionValue()80 float ActionDisplay::GetActionValue()
81 {
82 std::string scene = g_service->GetScene();
83 auto iter = g_sceneMap.find(scene);
84 if (iter != g_sceneMap.end()) {
85 return static_cast<float>(strtof(iter->second.c_str(), nullptr));
86 }
87 float value = FALLBACK_VALUE_FLOAT;
88 if (!valueList_.empty()) {
89 if (isStrict_) {
90 value = *min_element(valueList_.begin(), valueList_.end());
91 } else {
92 value = *max_element(valueList_.begin(), valueList_.end());
93 }
94 }
95 return value;
96 }
97
RequestDisplay(float factor)98 void ActionDisplay::RequestDisplay(float factor)
99 {
100 if (!DisplayPowerMgrClient::GetInstance().DiscountBrightness(factor)) {
101 THERMAL_HILOGE(COMP_SVC, "failed to discount brightness");
102 return;
103 }
104 THERMAL_HILOGI(COMP_SVC, "action execute: {%{public}s = %{public}f}", actionName_.c_str(), factor);
105 }
106
ExecuteMock(float factor)107 void ActionDisplay::ExecuteMock(float factor)
108 {
109 std::string valueString = std::to_string(factor) + "\n";
110 FileOperation::WriteFile(LCD_MOCK_PATH, valueString, valueString.length());
111 }
112 } // namespace PowerMgr
113 } // namespace OHOS
114