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