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