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