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