1 /*
2  * Copyright (c) 2021-2023 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 
20 #include "constants.h"
21 #include "power_mgr_client.h"
22 #include "file_operation.h"
23 #include "thermal_hisysevent.h"
24 #include "thermal_service.h"
25 #include "ffrt_utils.h"
26 #include "securec.h"
27 
28 using namespace OHOS::PowerMgr;
29 using namespace OHOS::AppExecFwk;
30 namespace OHOS {
31 namespace PowerMgr {
32 namespace {
33 constexpr const char* SHUTDOWN_REASON = "DeviceTempTooHigh";
34 constexpr const char* SHUTDOWN_PATH = "/data/service/el0/thermal/config/shut_down";
35 FFRTQueue g_queue("thermal_action_shutdown");
36 FFRTHandle g_shutdownTaskHandle;
37 const int MAX_PATH = 256;
38 }
39 
ActionShutdown(const std::string & actionName)40 ActionShutdown::ActionShutdown(const std::string& actionName)
41 {
42     actionName_ = actionName;
43 }
44 
InitParams(const std::string & params)45 void ActionShutdown::InitParams(const std::string& params)
46 {
47     (void)params;
48 }
49 
SetStrict(bool enable)50 void ActionShutdown::SetStrict(bool enable)
51 {
52     isStrict_ = enable;
53 }
54 
SetEnableEvent(bool enable)55 void ActionShutdown::SetEnableEvent(bool enable)
56 {
57     enableEvent_ = enable;
58 }
59 
AddActionValue(std::string value)60 void ActionShutdown::AddActionValue(std::string value)
61 {
62     if (value.empty()) {
63         return;
64     }
65     valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
66 }
67 
Execute()68 void ActionShutdown::Execute()
69 {
70     auto tms = ThermalService::GetInstance();
71     THERMAL_RETURN_IF (tms == nullptr);
72     uint32_t value = GetActionValue();
73     if (value != lastValue_) {
74         if (tms->GetSimulationXml()) {
75             ShutdownExecution(static_cast<bool>(value));
76         } else {
77             ShutdownRequest(static_cast<bool>(value));
78         }
79         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
80         tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
81         lastValue_ = value;
82         THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
83     }
84     valueList_.clear();
85 }
86 
GetActionValue()87 uint32_t ActionShutdown::GetActionValue()
88 {
89     uint32_t value = FALLBACK_VALUE_UINT_ZERO;
90     if (!valueList_.empty()) {
91         if (isStrict_) {
92             value = *min_element(valueList_.begin(), valueList_.end());
93         } else {
94             value = *max_element(valueList_.begin(), valueList_.end());
95         }
96     }
97     return value;
98 }
99 
ShutdownRequest(bool isShutdown)100 uint32_t ActionShutdown::ShutdownRequest(bool isShutdown)
101 {
102     if (isShutdown) {
103         THERMAL_HILOGI(COMP_SVC, "device start shutdown");
104         PowerMgrClient::GetInstance().ShutDownDevice(SHUTDOWN_REASON);
105     }
106     return ERR_OK;
107 }
108 
ShutdownExecution(bool isShutdown)109 int32_t ActionShutdown::ShutdownExecution(bool isShutdown)
110 {
111     int32_t ret = -1;
112     char shutdownBuf[MAX_PATH] = {0};
113     ret = snprintf_s(shutdownBuf, MAX_PATH, sizeof(shutdownBuf) - 1, SHUTDOWN_PATH);
114     if (ret < EOK) {
115         return ret;
116     }
117     std::string valueString = std::to_string(isShutdown) + "\n";
118     ret = FileOperation::WriteFile(shutdownBuf, valueString, valueString.length());
119     if (ret != ERR_OK) {
120         return ret;
121     }
122     return ERR_OK;
123 }
124 
DelayShutdown(bool isShutdown,int32_t temp,int32_t thresholdClr)125 uint32_t ActionShutdown::DelayShutdown(bool isShutdown, int32_t temp, int32_t thresholdClr)
126 {
127     if (g_shutdownTaskHandle && temp < thresholdClr) {
128         THERMAL_HILOGI(COMP_SVC, "shutdown canceled");
129         FFRTUtils::CancelTask(g_shutdownTaskHandle, g_queue);
130         return ERR_OK;
131     }
132 
133     uint32_t delay = 50000;
134     FFRTTask shutdownTask = [&]() {
135         THERMAL_HILOGI(COMP_SVC, "shutdown start");
136         ShutdownRequest(isShutdown);
137     };
138     g_shutdownTaskHandle = FFRTUtils::SubmitDelayTask(shutdownTask, delay, g_queue);
139     THERMAL_HILOGI(COMP_SVC, "shutdown device after %{public}u ms", delay);
140     return ERR_OK;
141 }
142 } // namespace PowerMgr
143 } // namespace OHOS
144