1 /*
2 * Copyright (c) 2021-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 "thermal_action_manager.h"
17
18 #include "constants.h"
19 #include "file_operation.h"
20 #include "thermal_action_factory.h"
21 #include "securec.h"
22 #include "string_operation.h"
23
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 const int MAX_PATH = 256;
28 }
Init()29 bool ThermalActionManager::Init()
30 {
31 THERMAL_HILOGD(COMP_SVC, "Enter");
32 ThermalActionFactory::InitFactory();
33 for (auto item = vActionItem_.begin(); item != vActionItem_.end(); ++item) {
34 THERMAL_HILOGI(COMP_SVC, "ThermalActionManager name = %{public}s", item->name.c_str());
35 if (!item->protocol.empty()) {
36 std::vector<std::string> protocolList;
37 StringOperation::SplitString(item->protocol, protocolList, ",");
38 if (protocolList.empty()) {
39 THERMAL_HILOGW(COMP_SVC, "protocolList is empty");
40 continue;
41 }
42
43 for (auto& iter : protocolList) {
44 std::string str = item->name;
45 std::string combinedActionName = str.append("_").append(iter.c_str());
46 InsertActionMap(combinedActionName, iter, item->strict, item->enableEvent);
47 }
48 } else {
49 InsertActionMap(item->name, item->protocol, item->strict, item->enableEvent);
50 }
51 }
52
53 if (actionThermalLevel_ == nullptr) {
54 actionThermalLevel_ = std::make_shared<ActionThermalLevel>(THERMAL_LEVEL_NAME);
55 if (!actionThermalLevel_->Init()) {
56 THERMAL_HILOGE(COMP_SVC, "failed to create level action");
57 }
58 }
59 CreateActionMockFile();
60 return true;
61 }
62
InsertActionMap(const std::string & actionName,const std::string & protocol,bool strict,bool enableEvent)63 void ThermalActionManager::InsertActionMap(const std::string& actionName, const std::string& protocol, bool strict,
64 bool enableEvent)
65 {
66 std::shared_ptr<IThermalAction> thermalAction = ThermalActionFactory::Create(actionName);
67 if (thermalAction == nullptr) {
68 THERMAL_HILOGE(COMP_SVC, "failed to create action");
69 return;
70 }
71 thermalAction->InitParams(protocol);
72 thermalAction->SetStrict(strict);
73 thermalAction->SetEnableEvent(enableEvent);
74 actionMap_.emplace(actionName, thermalAction);
75 }
76
SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)77 void ThermalActionManager::SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> &callback)
78 {
79 THERMAL_HILOGD(COMP_SVC, "Enter");
80 if (actionThermalLevel_ != nullptr) {
81 actionThermalLevel_->SubscribeThermalLevelCallback(callback);
82 }
83 }
84
UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)85 void ThermalActionManager::UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> &callback)
86 {
87 THERMAL_HILOGD(COMP_SVC, "Enter");
88 if (actionThermalLevel_ != nullptr) {
89 actionThermalLevel_->UnSubscribeThermalLevelCallback(callback);
90 }
91 }
92
GetThermalLevel()93 uint32_t ThermalActionManager::GetThermalLevel()
94 {
95 THERMAL_HILOGD(COMP_SVC, "Enter");
96 return actionThermalLevel_->GetThermalLevel();
97 }
98
CreateActionMockFile()99 int32_t ThermalActionManager::CreateActionMockFile()
100 {
101 THERMAL_HILOGD(COMP_SVC, "Enter");
102 std::string configDir = "/data/service/el0/thermal/config/%s";
103 std::string stateDir = "/data/service/el0/thermal/state/%s";
104 char fileBuf[MAX_PATH] = {0};
105 char stateFileBuf[MAX_PATH] = {0};
106 std::vector<std::string> actionValueList = {"lcd", "process_ctrl", "configLevel", "shut_down", "sc_current",
107 "buck_current", "sc_voltage", "buck_voltage"};
108 std::vector<std::string> stateValueList = {"scene", "screen", "charge"};
109 int32_t ret;
110 for (auto iter : actionValueList) {
111 THERMAL_HILOGD(COMP_SVC, "start create file");
112 ret = snprintf_s(fileBuf, MAX_PATH, sizeof(fileBuf) - ARG_1, configDir.c_str(), iter.c_str());
113 if (ret < EOK) {
114 return ret;
115 }
116 FileOperation::CreateNodeFile(static_cast<std::string>(fileBuf));
117 }
118
119 for (auto iter : stateValueList) {
120 THERMAL_HILOGD(COMP_SVC, "start create file");
121 ret = snprintf_s(stateFileBuf, MAX_PATH, sizeof(stateFileBuf) - ARG_1, stateDir.c_str(), iter.c_str());
122 if (ret < EOK) {
123 return ret;
124 }
125 FileOperation::CreateNodeFile(static_cast<std::string>(stateFileBuf));
126 }
127 return ERR_OK;
128 }
129
DumpAction(std::string & result)130 void ThermalActionManager::DumpAction(std::string& result)
131 {
132 for (auto iter = vActionItem_.begin(); iter != vActionItem_.end(); ++iter) {
133 result.append("name: ");
134 result.append(iter->name);
135 if (!iter->params.empty()) {
136 result.append("\t");
137 result.append("params: ");
138 result.append(iter->params);
139 }
140 if (!iter->protocol.empty()) {
141 result.append("\t");
142 result.append("protocol: ");
143 result.append(iter->protocol);
144 }
145 if (!iter->uid.empty()) {
146 result.append("\t");
147 result.append("uid: ");
148 result.append(iter->uid);
149 }
150 result.append("\t");
151 result.append("strict: ");
152 result.append(std::to_string(iter->strict));
153 result.append("\t");
154 result.append("enableEvent: ");
155 result.append(std::to_string(iter->enableEvent));
156 result.append("\n");
157 }
158 }
159 } // namespace PowerMgr
160 } // namespace OHOS
161