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_voltage.h"
17
18 #include <map>
19 #include "v2_0/ibattery_interface.h"
20 #include "thermal_hisysevent.h"
21 #include "thermal_service.h"
22 #include "file_operation.h"
23 #include "securec.h"
24 #include "constants.h"
25
26 namespace OHOS {
27 namespace PowerMgr {
28 namespace {
29 constexpr const char* SC_VOLTAGE_PATH = "/data/service/el0/thermal/config/sc_voltage";
30 constexpr const char* BUCK_VOLTAGE_PATH = "/data/service/el0/thermal/config/buck_voltage";
31 const int32_t MAX_PATH = 256;
32 }
33
34 std::vector<ChargingLimit> ActionVoltage::chargeLimitList_;
35
ActionVoltage(const std::string & actionName)36 ActionVoltage::ActionVoltage(const std::string& actionName)
37 {
38 actionName_ = actionName;
39 }
40
InitParams(const std::string & protocol)41 void ActionVoltage::InitParams(const std::string& protocol)
42 {
43 protocol_ = protocol;
44 }
45
SetStrict(bool enable)46 void ActionVoltage::SetStrict(bool enable)
47 {
48 isStrict_ = enable;
49 }
50
SetEnableEvent(bool enable)51 void ActionVoltage::SetEnableEvent(bool enable)
52 {
53 enableEvent_ = enable;
54 }
55
AddActionValue(std::string value)56 void ActionVoltage::AddActionValue(std::string value)
57 {
58 if (value.empty()) {
59 return;
60 }
61 valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
62 }
63
Execute()64 void ActionVoltage::Execute()
65 {
66 auto tms = ThermalService::GetInstance();
67 THERMAL_RETURN_IF (tms == nullptr);
68 uint32_t value = GetActionValue();
69 if (value != lastValue_) {
70 SetVoltage(value);
71 WriteMockNode(value);
72 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
73 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
74 lastValue_ = value;
75 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
76 }
77 valueList_.clear();
78 }
79
GetActionValue()80 uint32_t ActionVoltage::GetActionValue()
81 {
82 uint32_t value = FALLBACK_VALUE_UINT_ZERO;
83 if (!valueList_.empty()) {
84 if (isStrict_) {
85 value = *min_element(valueList_.begin(), valueList_.end());
86 } else {
87 value = *max_element(valueList_.begin(), valueList_.end());
88 }
89 }
90 return value;
91 }
92
SetVoltage(int32_t voltage)93 int32_t ActionVoltage::SetVoltage(int32_t voltage)
94 {
95 sptr<IBatteryInterface> iBatteryInterface = IBatteryInterface::Get();
96 if (iBatteryInterface == nullptr) {
97 THERMAL_HILOGE(COMP_SVC, "iBatteryInterface_ is nullptr");
98 return ERR_FLATTEN_OBJECT;
99 }
100 ChargingLimit chargingLimit;
101 chargingLimit.type = TYPE_VOLTAGE;
102 chargingLimit.protocol = protocol_;
103 chargingLimit.value = voltage;
104 chargeLimitList_.push_back(chargingLimit);
105 return ERR_OK;
106 }
107
ExecuteVoltageLimit()108 void ActionVoltage::ExecuteVoltageLimit()
109 {
110 if (chargeLimitList_.empty()) {
111 return;
112 }
113 sptr<IBatteryInterface> iBatteryInterface = IBatteryInterface::Get();
114 if (iBatteryInterface == nullptr) {
115 THERMAL_HILOGE(COMP_SVC, "iBatteryInterface_ is nullptr");
116 return;
117 }
118 int32_t result = iBatteryInterface->SetChargingLimit(chargeLimitList_);
119 if (result != ERR_OK) {
120 THERMAL_HILOGE(COMP_SVC, "failed to set charge limit");
121 return;
122 }
123 chargeLimitList_.clear();
124 }
125
WriteMockNode(int32_t mockValue)126 int32_t ActionVoltage::WriteMockNode(int32_t mockValue)
127 {
128 int32_t ret = -1;
129 char buf[MAX_PATH] = {0};
130 if (protocol_ == SC_PROTOCOL) {
131 ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, SC_VOLTAGE_PATH);
132 if (ret < EOK) {
133 return ret;
134 }
135 } else if (protocol_ == BUCK_PROTOCOL) {
136 ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, BUCK_VOLTAGE_PATH);
137 if (ret < EOK) {
138 return ret;
139 }
140 }
141
142 std::string valueString = std::to_string(mockValue) + "\n";
143 ret = FileOperation::WriteFile(buf, valueString, valueString.length());
144 if (ret != ERR_OK) {
145 return ret;
146 }
147 return ERR_OK;
148 }
149 } // namespace PowerMgr
150 } // namespace OHOS
151