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