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