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_ = atoi(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(std::string value)59 void ActionNode::AddActionValue(std::string value)
60 {
61 if (value.empty()) {
62 return;
63 }
64 valueList_.push_back(atoi(value.c_str()));
65 }
66
Execute()67 void ActionNode::Execute()
68 {
69 int32_t value = fallbackValue_;
70 if (!valueList_.empty()) {
71 if (isStrict_) {
72 value = *min_element(valueList_.begin(), valueList_.end());
73 } else {
74 value = *max_element(valueList_.begin(), valueList_.end());
75 }
76 }
77 if (value != lastValue_) {
78 std::string valStr = std::to_string(value);
79 if (nodePath_.size() > MIN_PATH_LENGTH) {
80 FileOperation::WriteFile(nodePath_, valStr, valStr.length());
81 }
82 auto tms = ThermalService::GetInstance();
83 tms->GetObserver()->SetDecisionValue(actionName_, valStr);
84 lastValue_ = value;
85 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}d}", actionName_.c_str(), lastValue_);
86 }
87 valueList_.clear();
88 }
89 } // namespace PowerMgr
90 } // namespace OHOS
91