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 "action_shutdown.h"
17
18 #include <map>
19 #include <thread>
20 #include "event_runner.h"
21 #include "event_handler.h"
22 #include "event_queue.h"
23 #include "power_mgr_client.h"
24 #include "file_operation.h"
25 #include "thermal_hisysevent.h"
26 #include "thermal_service.h"
27 #include "securec.h"
28
29 using namespace OHOS::PowerMgr;
30 using namespace OHOS::AppExecFwk;
31 namespace OHOS {
32 namespace PowerMgr {
33 namespace {
34 constexpr const char* SHUTDOWN_REASON = "DeviceTempTooHigh";
35 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
36 constexpr const char* SHUTDOWN_PATH = "/data/service/el0/thermal/config/shut_down";
37 const int MAX_PATH = 256;
38 }
ActionShutdown(const std::string & actionName)39 ActionShutdown::ActionShutdown(const std::string& actionName)
40 {
41 actionName_ = actionName;
42 }
43
InitParams(const std::string & params)44 void ActionShutdown::InitParams(const std::string& params)
45 {
46 }
47
SetStrict(bool flag)48 void ActionShutdown::SetStrict(bool flag)
49 {
50 flag_ = flag;
51 }
52
SetEnableEvent(bool enable)53 void ActionShutdown::SetEnableEvent(bool enable)
54 {
55 enableEvent_ = enable;
56 }
57
AddActionValue(std::string value)58 void ActionShutdown::AddActionValue(std::string value)
59 {
60 THERMAL_HILOGD(COMP_SVC, "value=%{public}s", value.c_str());
61 valuesList_.push_back(atoi(value.c_str()));
62 }
63
Execute()64 void ActionShutdown::Execute()
65 {
66 THERMAL_HILOGD(COMP_SVC, "Enter");
67 int32_t value;
68 THERMAL_RETURN_IF (g_service == nullptr);
69 std::string scene = g_service->GetScene();
70 auto iter = g_sceneMap.find(scene);
71 if (iter != g_sceneMap.end()) {
72 value = static_cast<int32_t>(atoi(iter->second.c_str()));
73 if ((value != lastValue_) && (!g_service->GetSimulationXml())) {
74 ShutdownRequest(static_cast<bool>(value));
75 } else if (value != lastValue_) {
76 ShutdownExecution(static_cast<bool>(value));
77 } else {
78 THERMAL_HILOGD(COMP_SVC, "value is not change");
79 }
80 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
81 g_service->GetObserver()->SetDecisionValue(actionName_, iter->second);
82 lastValue_ = value;
83 valuesList_.clear();
84 return;
85 }
86
87 if (valuesList_.empty()) {
88 value = 0;
89 } else {
90 if (flag_) {
91 value = *max_element(valuesList_.begin(), valuesList_.end());
92 } else {
93 value = *min_element(valuesList_.begin(), valuesList_.end());
94 }
95 valuesList_.clear();
96 }
97
98 if (value != lastValue_) {
99 if (!g_service->GetFlag()) {
100 ShutdownExecution(static_cast<bool>(value));
101 } else {
102 ShutdownRequest(static_cast<bool>(value));
103 }
104 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
105 g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
106 lastValue_ = value;
107 }
108 }
109
ShutdownRequest(bool isShutdown)110 uint32_t ActionShutdown::ShutdownRequest(bool isShutdown)
111 {
112 THERMAL_HILOGD(COMP_SVC, "Enter");
113 if (isShutdown) {
114 PowerMgrClient::GetInstance().ShutDownDevice(SHUTDOWN_REASON);
115 THERMAL_HILOGI(COMP_SVC, "device start shutdown");
116 }
117 return ERR_OK;
118 }
119
ShutdownExecution(bool isShutdown)120 int32_t ActionShutdown::ShutdownExecution(bool isShutdown)
121 {
122 THERMAL_HILOGD(COMP_SVC, "Enter");
123 int32_t ret = -1;
124 char shutdownBuf[MAX_PATH] = {0};
125 ret = snprintf_s(shutdownBuf, MAX_PATH, sizeof(shutdownBuf) - 1, SHUTDOWN_PATH);
126 if (ret < EOK) {
127 return ret;
128 }
129 std::string valueString = std::to_string(isShutdown) + "\n";
130 ret = FileOperation::WriteFile(shutdownBuf, valueString, valueString.length());
131 if (ret != ERR_OK) {
132 return ret;
133 }
134 return ERR_OK;
135 }
136
DelayShutdown(bool isShutdown,int32_t temp,int32_t thresholdClr)137 uint32_t ActionShutdown::DelayShutdown(bool isShutdown, int32_t temp, int32_t thresholdClr)
138 {
139 THERMAL_HILOGD(COMP_SVC, "Enter");
140 uint32_t delay = 50000;
141 auto handler = g_service->GetHandler();
142 if (handler == nullptr) {
143 return ERR_INVALID_VALUE;
144 }
145 auto runner = g_service->GetEventRunner();
146 if (runner == nullptr) {
147 return ERR_INVALID_VALUE;
148 }
149 auto shutDownTask = [&]() {
150 THERMAL_HILOGI(COMP_SVC, "shutdown run");
151 ShutdownRequest(isShutdown);
152 runner->Stop();
153 };
154
155 /**
156 * @steps: post delay task to start runner to shutdown,
157 * then new a thread to start the same runner cancell shutdown.
158 * @expected: return runner is already running error.
159 */
160 auto f = [&]() {
161 if (temp < thresholdClr) {
162 auto runResult = runner->Stop();
163 if (!runResult) {
164 THERMAL_HILOGE(COMP_SVC, "HandleShutdownActionHUb: success to cancell");
165 }
166 }
167 };
168 handler->PostTask(shutDownTask, delay, EventQueue::Priority::HIGH);
169 std::thread newThread(f);
170 newThread.detach();
171 runner->Run();
172 return ERR_OK;
173 }
174 } // namespace PowerMgr
175 } // namespace OHOS
176