• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
35 constexpr const char* SHUTDOWN_PATH = "/data/service/el0/thermal/config/shut_down";
36 FFRTQueue g_queue("thermal_action_shutdown");
37 FFRTHandle g_shutdownTaskHandle;
38 const int MAX_PATH = 256;
39 }
40 
ActionShutdown(const std::string & actionName)41 ActionShutdown::ActionShutdown(const std::string& actionName)
42 {
43     actionName_ = actionName;
44 }
45 
InitParams(const std::string & params)46 void ActionShutdown::InitParams(const std::string& params)
47 {
48     (void)params;
49 }
50 
SetStrict(bool enable)51 void ActionShutdown::SetStrict(bool enable)
52 {
53     isStrict_ = enable;
54 }
55 
SetEnableEvent(bool enable)56 void ActionShutdown::SetEnableEvent(bool enable)
57 {
58     enableEvent_ = enable;
59 }
60 
AddActionValue(std::string value)61 void ActionShutdown::AddActionValue(std::string value)
62 {
63     if (value.empty()) {
64         return;
65     }
66     valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
67 }
68 
Execute()69 void ActionShutdown::Execute()
70 {
71     THERMAL_RETURN_IF (g_service == nullptr);
72     uint32_t value = GetActionValue();
73     if (value != lastValue_) {
74         if (!g_service->GetFlag()) {
75             ShutdownExecution(static_cast<bool>(value));
76         } else {
77             ShutdownRequest(static_cast<bool>(value));
78         }
79         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
80         g_service->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     std::string scene = g_service->GetScene();
90     auto iter = g_sceneMap.find(scene);
91     if (iter != g_sceneMap.end()) {
92         return static_cast<uint32_t>(strtol(iter->second.c_str(), nullptr, STRTOL_FORMART_DEC));
93     }
94     uint32_t value = FALLBACK_VALUE_UINT_ZERO;
95     if (!valueList_.empty()) {
96         if (isStrict_) {
97             value = *min_element(valueList_.begin(), valueList_.end());
98         } else {
99             value = *max_element(valueList_.begin(), valueList_.end());
100         }
101     }
102     return value;
103 }
104 
ShutdownRequest(bool isShutdown)105 uint32_t ActionShutdown::ShutdownRequest(bool isShutdown)
106 {
107     if (isShutdown) {
108         THERMAL_HILOGI(COMP_SVC, "device start shutdown");
109         PowerMgrClient::GetInstance().ShutDownDevice(SHUTDOWN_REASON);
110     }
111     return ERR_OK;
112 }
113 
ShutdownExecution(bool isShutdown)114 int32_t ActionShutdown::ShutdownExecution(bool isShutdown)
115 {
116     int32_t ret = -1;
117     char shutdownBuf[MAX_PATH] = {0};
118     ret = snprintf_s(shutdownBuf, MAX_PATH, sizeof(shutdownBuf) - 1, SHUTDOWN_PATH);
119     if (ret < EOK) {
120         return ret;
121     }
122     std::string valueString = std::to_string(isShutdown) + "\n";
123     ret = FileOperation::WriteFile(shutdownBuf, valueString, valueString.length());
124     if (ret != ERR_OK) {
125         return ret;
126     }
127     return ERR_OK;
128 }
129 
DelayShutdown(bool isShutdown,int32_t temp,int32_t thresholdClr)130 uint32_t ActionShutdown::DelayShutdown(bool isShutdown, int32_t temp, int32_t thresholdClr)
131 {
132     if (g_shutdownTaskHandle && temp < thresholdClr) {
133         THERMAL_HILOGI(COMP_SVC, "shutdown canceled");
134         FFRTUtils::CancelTask(g_shutdownTaskHandle, g_queue);
135         return ERR_OK;
136     }
137 
138     uint32_t delay = 50000;
139     FFRTTask shutdownTask = [&]() {
140         THERMAL_HILOGI(COMP_SVC, "shutdown start");
141         ShutdownRequest(isShutdown);
142     };
143     g_shutdownTaskHandle = FFRTUtils::SubmitDelayTask(shutdownTask, delay, g_queue);
144     THERMAL_HILOGI(COMP_SVC, "shutdown device after %{public}u ms", delay);
145     return ERR_OK;
146 }
147 } // namespace PowerMgr
148 } // namespace OHOS
149