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