• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "v1_2/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 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
33 }
34 std::vector<ChargingLimit> ActionCharger::chargeLimitList_;
35 
ActionCharger(const std::string & actionName)36 ActionCharger::ActionCharger(const std::string& actionName)
37 {
38     actionName_ = actionName;
39 }
40 
InitParams(const std::string & protocol)41 void ActionCharger::InitParams(const std::string& protocol)
42 {
43     protocol_ = protocol;
44 }
45 
SetStrict(bool enable)46 void ActionCharger::SetStrict(bool enable)
47 {
48     isStrict_ = enable;
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     if (value.empty()) {
59         return;
60     }
61     valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
62 }
63 
Execute()64 void ActionCharger::Execute()
65 {
66     THERMAL_RETURN_IF (g_service == nullptr);
67     uint32_t value = GetActionValue();
68     if (value != lastValue_) {
69         ChargerRequest(value);
70         WriteSimValue(value);
71         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
72         g_service->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     std::string scene = g_service->GetScene();
82     auto iter = g_sceneMap.find(scene);
83     if (iter != g_sceneMap.end()) {
84         return static_cast<uint32_t>(strtol(iter->second.c_str(), nullptr, STRTOL_FORMART_DEC));
85     }
86     uint32_t value = FALLBACK_VALUE_UINT_ZERO;
87     if (!valueList_.empty()) {
88         if (isStrict_) {
89             value = *min_element(valueList_.begin(), valueList_.end());
90         } else {
91             value = *max_element(valueList_.begin(), valueList_.end());
92         }
93     }
94     return value;
95 }
96 
ChargerRequest(int32_t current)97 int32_t ActionCharger::ChargerRequest(int32_t current)
98 {
99     ChargingLimit chargingLimit;
100     chargingLimit.type = TYPE_CURRENT;
101     chargingLimit.protocol = protocol_;
102     chargingLimit.value = current;
103     chargeLimitList_.push_back(chargingLimit);
104 
105     auto thermalInterface = g_service->GetThermalInterface();
106     if (thermalInterface != nullptr) {
107         int32_t ret = thermalInterface->SetBatteryCurrent(current);
108         if (ret != ERR_OK) {
109             THERMAL_HILOGE(COMP_SVC, "failed to set charger current to thermal hdf");
110             return ret;
111         }
112     }
113     return ERR_OK;
114 }
115 
ExecuteCurrentLimit()116 void ActionCharger::ExecuteCurrentLimit()
117 {
118     if (chargeLimitList_.empty()) {
119         return;
120     }
121     sptr<IBatteryInterface> iBatteryInterface = IBatteryInterface::Get();
122     if (iBatteryInterface == nullptr) {
123         THERMAL_HILOGE(COMP_SVC, "iBatteryInterface_ is nullptr");
124         return;
125     }
126     int32_t result = iBatteryInterface->SetChargingLimit(chargeLimitList_);
127     if (result != ERR_OK) {
128         THERMAL_HILOGE(COMP_SVC, "failed to set charge limit");
129         return;
130     }
131     chargeLimitList_.clear();
132 }
133 
WriteSimValue(int32_t simValue)134 int32_t ActionCharger::WriteSimValue(int32_t simValue)
135 {
136     int32_t ret = -1;
137     char buf[MAX_PATH] = {0};
138     if (protocol_ == SC_PROTOCOL) {
139         ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, SC_CURRENT_PATH);
140         if (ret < EOK) {
141             return ret;
142         }
143     } else if (protocol_ == BUCK_PROTOCOL) {
144         ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, BUCK_CURRENT_PATH);
145         if (ret < EOK) {
146             return ret;
147         }
148     }
149     std::string valueString = std::to_string(simValue) + "\n";
150     ret = FileOperation::WriteFile(buf, valueString, valueString.length());
151     if (ret != ERR_OK) {
152         return ret;
153     }
154     return ERR_OK;
155 }
156 } // namespace PowerMgr
157 } // namespace OHOS
158