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