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 #include "voltage_action.h"
16 
17 #include <iosfwd>
18 #include <climits>
19 #include <string>
20 #include "errors.h"
21 #include "securec.h"
22 #include "thermal_log.h"
23 #include "thermal_protector_util.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 namespace {
28 constexpr const char* BATTERY_VOLTAGE_PATH = "/data/service/el0/thermal/cooling/battery/voltage";
29 constexpr int32_t MAX_PATH = 256;
30 }
AddActionValue(uint32_t value)31 bool VoltageAction::AddActionValue(uint32_t value)
32 {
33     THERMAL_HILOGD(FEATURE_PROTECTOR, "Enter");
34     latestvalue_ = value;
35     return true;
36 }
37 
Execute()38 void VoltageAction::Execute()
39 {
40     static uint32_t value;
41     if (value != latestvalue_) {
42         if (BatteryVoltageActionRequest(latestvalue_) != ERR_OK) {
43             THERMAL_HILOGW(FEATURE_PROTECTOR, "failed to set battery volatage");
44         }
45         value = latestvalue_;
46     } else {
47         latestvalue_ = 0;
48     }
49 }
50 
BatteryVoltageActionRequest(uint32_t voltage)51 int32_t VoltageAction::BatteryVoltageActionRequest(uint32_t voltage)
52 {
53     char voltageBuf[MAX_PATH] = {0};
54     if (snprintf_s(voltageBuf, MAX_PATH, sizeof(voltageBuf) - 1, BATTERY_VOLTAGE_PATH) < EOK) {
55         return ERR_INVALID_VALUE;
56     }
57     std::string voltageStr = std::to_string(voltage);
58     return ThermalProtectorUtil::WriteFile(voltageBuf, voltageStr, voltageStr.length());
59 }
60 } // namespace PowerMgr
61 } // namespace OHOS
62