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_charger.h"
17
18 #include <map>
19 #include "constants.h"
20 #include "file_operation.h"
21 #include "securec.h"
22 #include "thermal_hisysevent.h"
23 #include "thermal_service.h"
24 #ifdef DRIVERS_INTERFACE_BATTERY_ENABLE
25 #include "v2_0/ibattery_interface.h"
26 #endif
27
28 namespace OHOS {
29 namespace PowerMgr {
30 namespace {
31 constexpr const char* SC_CURRENT_PATH = "/data/service/el0/thermal/config/sc_current";
32 constexpr const char* BUCK_CURRENT_PATH = "/data/service/el0/thermal/config/buck_current";
33 const int MAX_PATH = 256;
34 }
35 #ifdef DRIVERS_INTERFACE_BATTERY_ENABLE
36 std::vector<ChargingLimit> ActionCharger::chargeLimitList_;
37 #endif
38
ActionCharger(const std::string & actionName)39 ActionCharger::ActionCharger(const std::string& actionName)
40 {
41 actionName_ = actionName;
42 }
43
InitParams(const std::string & protocol)44 void ActionCharger::InitParams(const std::string& protocol)
45 {
46 protocol_ = protocol;
47 }
48
SetStrict(bool enable)49 void ActionCharger::SetStrict(bool enable)
50 {
51 isStrict_ = enable;
52 }
53
SetEnableEvent(bool enable)54 void ActionCharger::SetEnableEvent(bool enable)
55 {
56 enableEvent_ = enable;
57 }
58
AddActionValue(uint32_t actionId,std::string value)59 void ActionCharger::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.uintDelayValue = static_cast<uint32_t>(static_cast<uint32_t>(strtol(value.c_str(),
68 nullptr, STRTOL_FORMART_DEC)));
69 }
70 } else {
71 valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
72 }
73 }
74
ExecuteInner()75 void ActionCharger::ExecuteInner()
76 {
77 auto tms = ThermalService::GetInstance();
78 THERMAL_RETURN_IF (tms == nullptr);
79 for (auto &policyAction : policyActionMap_) {
80 if (policyAction.second.isCompleted) {
81 valueList_.push_back(policyAction.second.uintDelayValue);
82 }
83 }
84
85 uint32_t value = GetActionValue();
86 if (value != lastValue_) {
87 ChargerRequest(value);
88 WriteSimValue(value);
89 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
90 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
91 lastValue_ = value;
92 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
93 }
94 valueList_.clear();
95 }
96
ResetActionValue()97 void ActionCharger::ResetActionValue()
98 {
99 lastValue_ = 0;
100 }
101
GetActionValue()102 uint32_t ActionCharger::GetActionValue()
103 {
104 uint32_t value = FALLBACK_VALUE_UINT_ZERO;
105 if (!valueList_.empty()) {
106 if (isStrict_) {
107 value = *min_element(valueList_.begin(), valueList_.end());
108 } else {
109 value = *max_element(valueList_.begin(), valueList_.end());
110 }
111 }
112 return value;
113 }
114
ChargerRequest(int32_t current)115 int32_t ActionCharger::ChargerRequest(int32_t current)
116 {
117 #ifdef DRIVERS_INTERFACE_BATTERY_ENABLE
118 ChargingLimit chargingLimit;
119 chargingLimit.type = TYPE_CURRENT;
120 chargingLimit.protocol = protocol_;
121 chargingLimit.value = current;
122 chargeLimitList_.push_back(chargingLimit);
123 #endif
124
125 auto tms = ThermalService::GetInstance();
126 auto thermalInterface = tms->GetThermalInterface();
127 if (thermalInterface != nullptr) {
128 int32_t ret = thermalInterface->SetBatteryCurrent(current);
129 if (ret != ERR_OK) {
130 THERMAL_HILOGE(COMP_SVC, "failed to set charger current to thermal hdf");
131 return ret;
132 }
133 }
134 return ERR_OK;
135 }
136
ExecuteCurrentLimit()137 void ActionCharger::ExecuteCurrentLimit()
138 {
139 #ifdef DRIVERS_INTERFACE_BATTERY_ENABLE
140 if (chargeLimitList_.empty()) {
141 return;
142 }
143 sptr<IBatteryInterface> iBatteryInterface = IBatteryInterface::Get();
144 if (iBatteryInterface == nullptr) {
145 THERMAL_HILOGE(COMP_SVC, "iBatteryInterface_ is nullptr");
146 return;
147 }
148 int32_t result = iBatteryInterface->SetChargingLimit(chargeLimitList_);
149 if (result != ERR_OK) {
150 THERMAL_HILOGE(COMP_SVC, "failed to set charge limit");
151 return;
152 }
153 chargeLimitList_.clear();
154 #endif
155 }
156
WriteSimValue(int32_t simValue)157 int32_t ActionCharger::WriteSimValue(int32_t simValue)
158 {
159 int32_t ret = -1;
160 char buf[MAX_PATH] = {0};
161 if (protocol_ == SC_PROTOCOL) {
162 ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, SC_CURRENT_PATH);
163 if (ret < EOK) {
164 return ret;
165 }
166 } else if (protocol_ == BUCK_PROTOCOL) {
167 ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, BUCK_CURRENT_PATH);
168 if (ret < EOK) {
169 return ret;
170 }
171 }
172 std::string valueString = std::to_string(simValue) + "\n";
173 ret = FileOperation::WriteFile(buf, valueString, valueString.length());
174 if (ret != ERR_OK) {
175 return ret;
176 }
177 return ERR_OK;
178 }
179 } // namespace PowerMgr
180 } // namespace OHOS
181