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