• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_node.h"
17 
18 #include "file_operation.h"
19 #include "string_operation.h"
20 #include "thermal_common.h"
21 #include "thermal_service.h"
22 
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 constexpr int32_t PATH_IDX = 0;
27 constexpr int32_t FALLBACK_IDX = 1;
28 constexpr size_t MIN_PATH_LENGTH = 9;
29 }
30 
ActionNode(const std::string & actionName)31 ActionNode::ActionNode(const std::string& actionName)
32 {
33     actionName_ = actionName;
34 }
35 
InitParams(const std::string & params)36 void ActionNode::InitParams(const std::string& params)
37 {
38     std::vector<std::string> paramList;
39     StringOperation::SplitString(params, paramList, "|");
40     int32_t paramNum = static_cast<int32_t>(paramList.size());
41     if (paramNum > FALLBACK_IDX) {
42         nodePath_ = paramList[PATH_IDX];
43         fallbackValue_ = atol(paramList[FALLBACK_IDX].c_str());
44     } else if (paramNum > PATH_IDX) {
45         nodePath_ = paramList[PATH_IDX];
46     }
47 }
48 
SetStrict(bool enable)49 void ActionNode::SetStrict(bool enable)
50 {
51     isStrict_ = enable;
52 }
53 
SetEnableEvent(bool enable)54 void ActionNode::SetEnableEvent(bool enable)
55 {
56     enableEvent_ = enable;
57 }
58 
AddActionValue(uint32_t actionId,std::string value)59 void ActionNode::AddActionValue(uint32_t actionId, std::string value)
60 {
61     if (value.empty()) {
62         return;
63     }
64     if (actionId > 0) {
65         auto iter = policyActionMap_.find(actionId);
66         if (iter != policyActionMap_.end()) {
67             iter->second.intDelayValue = atol(value.c_str());
68         }
69     } else {
70         valueList_.push_back(atol(value.c_str()));
71     }
72 }
73 
ExecuteInner()74 void ActionNode::ExecuteInner()
75 {
76     for (auto &policyAction : policyActionMap_) {
77         if (policyAction.second.isCompleted) {
78             valueList_.push_back(policyAction.second.intDelayValue);
79         }
80     }
81 
82     int64_t value = GetActionValue();
83     if (value != lastValue_) {
84         std::string valStr = std::to_string(value);
85         if (nodePath_.size() > MIN_PATH_LENGTH) {
86             FileOperation::WriteFile(nodePath_, valStr, valStr.length());
87         }
88         auto tms = ThermalService::GetInstance();
89         tms->GetObserver()->SetDecisionValue(actionName_, valStr);
90         lastValue_ = value;
91         THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}" PRId64 "}", actionName_.c_str(), lastValue_);
92     }
93     valueList_.clear();
94 }
95 
ResetActionValue()96 void ActionNode::ResetActionValue()
97 {
98     lastValue_ = INT_MAX;
99 }
100 
GetActionValue()101 int64_t ActionNode::GetActionValue()
102 {
103     int64_t value = fallbackValue_;
104     if (!valueList_.empty()) {
105         if (isStrict_) {
106             value = *min_element(valueList_.begin(), valueList_.end());
107         } else {
108             value = *max_element(valueList_.begin(), valueList_.end());
109         }
110     }
111 
112     return value;
113 }
114 } // namespace PowerMgr
115 } // namespace OHOS
116