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(std::string value)57 void ActionDisplay::AddActionValue(std::string value)
58 {
59     if (value.empty()) {
60         return;
61     }
62     valueList_.push_back(static_cast<float>(strtof(value.c_str(), nullptr)));
63 }
64 
Execute()65 void ActionDisplay::Execute()
66 {
67     auto tms = ThermalService::GetInstance();
68     THERMAL_RETURN_IF (tms == nullptr);
69     float value = GetActionValue();
70     if (fabs(value - lastValue_) > FLOAT_ACCURACY) {
71         if (!tms->GetSimulationXml()) {
72             RequestDisplay(value);
73         } else {
74             ExecuteMock(value);
75         }
76         WriteActionTriggeredHiSysEventWithRatio(enableEvent_, actionName_, value);
77         tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
78         lastValue_ = value;
79         THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}f}", actionName_.c_str(), lastValue_);
80     }
81     valueList_.clear();
82 }
83 
GetActionValue()84 float ActionDisplay::GetActionValue()
85 {
86     float value = FALLBACK_VALUE_FLOAT;
87     if (!valueList_.empty()) {
88         if (isStrict_) {
89             value = *min_element(valueList_.begin(), valueList_.end());
90         } else {
91             value = *max_element(valueList_.begin(), valueList_.end());
92         }
93     }
94     return value;
95 }
96 
RequestDisplay(float factor)97 void ActionDisplay::RequestDisplay(float factor)
98 {
99 #ifdef HAS_THERMAL_DISPLAY_MANAGER_PART
100     if (!DisplayPowerMgrClient::GetInstance().DiscountBrightness(factor)) {
101         THERMAL_HILOGE(COMP_SVC, "failed to discount brightness");
102         return;
103     }
104 #endif
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