1 /*
2 * Copyright (c) 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 "wakeup_action_controller.h"
17
18 #include <ipc_skeleton.h>
19 #include "power_log.h"
20 #include "power_mgr_service.h"
21 #include "system_suspend_controller.h"
22
23 namespace OHOS {
24 namespace PowerMgr {
25
26 /** WakeupActionController Implement */
WakeupActionController(std::shared_ptr<ShutdownController> & shutdownController,std::shared_ptr<PowerStateMachine> & stateMachine)27 WakeupActionController::WakeupActionController(
28 std::shared_ptr<ShutdownController>& shutdownController, std::shared_ptr<PowerStateMachine>& stateMachine)
29 {
30 shutdownController_ = shutdownController;
31 stateMachine_ = stateMachine;
32 }
33
~WakeupActionController()34 WakeupActionController::~WakeupActionController()
35 {
36 }
37
Init()38 void WakeupActionController::Init()
39 {
40 std::lock_guard lock(mutex_);
41 std::shared_ptr<WakeupActionSources> sources = WakeupActionSourceParser::ParseSources();
42 sourceMap_ = sources->GetSourceMap();
43 if (sourceMap_.empty()) {
44 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "InputManager is null");
45 }
46 }
47
IsLowCapacityWakeup()48 bool WakeupActionController::IsLowCapacityWakeup()
49 {
50 std::string reason;
51 SystemSuspendController::GetInstance().GetWakeupReason(reason);
52 if (reason.empty()) {
53 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "WakeupAction reason is empty");
54 return false;
55 }
56 reason.erase(reason.end() - 1);
57 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "WakeupAction reason %{public}s", reason.c_str());
58 if (sourceMap_.find(reason) != sourceMap_.end()) {
59 return true;
60 }
61 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "WakeupAction reason %{public}s doesn't exist", reason.c_str());
62 return false;
63 }
64
ExecuteByGetReason()65 bool WakeupActionController::ExecuteByGetReason()
66 {
67 std::string reason;
68 SystemSuspendController::GetInstance().GetWakeupReason(reason);
69 if (reason.empty()) {
70 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "WakeupAction reason is empty");
71 return false;
72 }
73 reason.erase(reason.end() - 1);
74 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "WakeupAction reason %{public}s", reason.c_str());
75 if (sourceMap_.find(reason) != sourceMap_.end()) {
76 pid_t pid = IPCSkeleton::GetCallingPid();
77 auto uid = IPCSkeleton::GetCallingUid();
78 POWER_HILOGI(FEATURE_WAKEUP_ACTION,
79 "WakeupAction device, pid=%{public}d, uid=%{public}d, reason=%{public}s, scene=%{public}s, "
80 "action=%{public}u",
81 pid, uid, reason.c_str(), sourceMap_[reason]->GetScene().c_str(), sourceMap_[reason]->GetAction());
82 HandleAction(reason);
83 return true;
84 }
85 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "WakeupAction reason %{public}s doesn't exist", reason.c_str());
86 return false;
87 }
88
HandleAction(const std::string & reason)89 void WakeupActionController::HandleAction(const std::string& reason)
90 {
91 switch (static_cast<WakeupAction>(sourceMap_[reason]->GetAction())) {
92 case WakeupAction::ACTION_HIBERNATE:
93 HandleHibernate(WakeupActionSources::mapSuspendDeviceType(reason));
94 break;
95 case WakeupAction::ACTION_SHUTDOWN:
96 HandleShutdown(sourceMap_[reason]->GetScene());
97 break;
98 case WakeupAction::ACTION_NONE:
99 default:
100 break;
101 }
102 }
103
HandleHibernate(SuspendDeviceType reason)104 void WakeupActionController::HandleHibernate(SuspendDeviceType reason)
105 {
106 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
107 if (pms == nullptr) {
108 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "pms is nullptr");
109 return;
110 }
111 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "low capacity, hibernate begin, %{public}d", static_cast<int>(reason));
112 if (pms->Hibernate(false) != PowerErrors::ERR_OK) {
113 POWER_HILOGE(FEATURE_WAKEUP_ACTION, "hibernate failed.");
114 }
115 }
116
HandleShutdown(const std::string & scene)117 void WakeupActionController::HandleShutdown(const std::string& scene)
118 {
119 POWER_HILOGI(FEATURE_WAKEUP_ACTION, "shutdown by reason=%{public}s", scene.c_str());
120 shutdownController_->Shutdown(scene);
121 }
122
123 } // namespace PowerMgr
124 } // namespace OHOS
125