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 "current_action.h"
16
17 #include <iosfwd>
18 #include <string>
19 #include <climits>
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 const std::string SIM_BATTERY_CURRENT_PATH = "/data/service/el0/thermal/cooling/battery/current";
29 constexpr int32_t MAX_PATH = 256;
30 }
AddActionValue(uint32_t value)31 bool CurrentAction::AddActionValue(uint32_t value)
32 {
33 THERMAL_HILOGD(FEATURE_PROTECTOR, "Enter");
34 latestvalue_ = value;
35 return true;
36 }
37
Execute()38 void CurrentAction::Execute()
39 {
40 static uint32_t value;
41 if (value != latestvalue_) {
42 if (BatteryCurrentActionRequest(latestvalue_) != ERR_OK) {
43 THERMAL_HILOGE(FEATURE_PROTECTOR, "failed to set battery current");
44 }
45 value = latestvalue_;
46 } else {
47 latestvalue_ = 0;
48 }
49 }
50
BatteryCurrentActionRequest(uint32_t current)51 int32_t CurrentAction::BatteryCurrentActionRequest(uint32_t current)
52 {
53 THERMAL_HILOGD(FEATURE_PROTECTOR, "%{public}d", current);
54 char currentBuf[MAX_PATH] = {0};
55 if (snprintf_s(currentBuf, MAX_PATH, sizeof(currentBuf) - 1, SIM_BATTERY_CURRENT_PATH.c_str()) < EOK) {
56 return ERR_INVALID_VALUE;
57 }
58 std::string currentStr = std::to_string(current);
59 return ThermalProtectorUtil::WriteFile(currentBuf, currentStr, currentStr.length());
60 }
61 } // namespace PowerMgr
62 } // namespace OHOS
63