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 "device_power_action.h"
17
18 #include <string>
19 #include <init_reboot.h>
20 #include <list.h>
21 #include "power_hookmgr.h"
22 #include "power_log.h"
23
24 namespace {
25 const std::string INVALID_CMD = "invalid_cmd";
26 const std::string UPDATER_CMD = "updater";
27 const std::string REBOOT_CMD = "";
28 const std::string SHUTDOWN_CMD = "shutdown";
29 const std::string FLASH_CMD = "flash";
30 }
31
32 namespace OHOS {
33 namespace PowerMgr {
Reboot(const std::string & reason)34 void DevicePowerAction::Reboot(const std::string& reason)
35 {
36 std::string rebootCmd;
37 RebootCmdInfo rebootInfo = {.rebootReason = reason, .rebootCmd = INVALID_CMD};
38 HOOK_EXEC_OPTIONS options {TRAVERSE_STOP_WHEN_ERROR, nullptr, nullptr};
39 HOOK_MGR* hookMgr = GetPowerHookMgr();
40 if (HookMgrExecute(hookMgr, static_cast<int32_t>(PowerHookStage::POWER_PRE_DO_REBOOT),
41 &rebootInfo, &options) == 0) {
42 rebootCmd = rebootInfo.rebootCmd;
43 } else {
44 rebootCmd = Updater(reason);
45 }
46 POWER_KHILOGI(FEATURE_SHUTDOWN,
47 "Reboot reason: %{public}s, command: %{public}s", reason.c_str(), rebootCmd.c_str());
48 DoRebootExt(rebootCmd.c_str(), reason.c_str());
49 }
50
Shutdown(const std::string & reason)51 void DevicePowerAction::Shutdown(const std::string& reason)
52 {
53 #ifdef POWER_MANAGER_POWEROFF_CHARGE
54 HOOK_MGR* hookMgr = GetPowerHookMgr();
55 std::string hookContext = reason;
56 HookMgrExecute(hookMgr, static_cast<int32_t>(PowerHookStage::POWER_PRE_DO_SHUTDOWN), &hookContext, nullptr);
57 #endif
58 POWER_KHILOGI(FEATURE_SHUTDOWN, "Shutdown executing");
59 DoRebootExt(SHUTDOWN_CMD.c_str(), reason.c_str());
60 }
61
Updater(const std::string & reason)62 std::string DevicePowerAction::Updater(const std::string& reason)
63 {
64 return (reason.find(UPDATER_CMD) != std::string::npos) ? UPDATER_CMD : REBOOT_CMD;
65 }
66 } // namespace PowerMgr
67 } // namespace OHOS
68