15ccb8f90Sopenharmony_ci/*
25ccb8f90Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License.
55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at
65ccb8f90Sopenharmony_ci *
75ccb8f90Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ccb8f90Sopenharmony_ci *
95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and
135ccb8f90Sopenharmony_ci * limitations under the License.
145ccb8f90Sopenharmony_ci */
155ccb8f90Sopenharmony_ci
165ccb8f90Sopenharmony_ci#include "power_mgr_dumper.h"
175ccb8f90Sopenharmony_ci
185ccb8f90Sopenharmony_ci#include "system_suspend_controller.h"
195ccb8f90Sopenharmony_ci
205ccb8f90Sopenharmony_cinamespace OHOS {
215ccb8f90Sopenharmony_cinamespace PowerMgr {
225ccb8f90Sopenharmony_cinamespace {
235ccb8f90Sopenharmony_ciconst std::string ARGS_ALL = "-a";
245ccb8f90Sopenharmony_ciconst std::string ARGS_HELP = "-h";
255ccb8f90Sopenharmony_ciconst std::string ARGS_RUNNINGLOCK = "-r";
265ccb8f90Sopenharmony_ciconst std::string ARGS_STATE = "-s";
275ccb8f90Sopenharmony_ciconst std::string ARGS_DIALOG = "-d";
285ccb8f90Sopenharmony_ciconst std::string ARGS_REG_KEY = "-k";
295ccb8f90Sopenharmony_ciconst std::string ARGS_On = "-t";
305ccb8f90Sopenharmony_ciconst std::string ARGS_Off = "-f";
315ccb8f90Sopenharmony_ci}
325ccb8f90Sopenharmony_ci
335ccb8f90Sopenharmony_cibool PowerMgrDumper::Dump(const std::vector<std::string>& args, std::string& result)
345ccb8f90Sopenharmony_ci{
355ccb8f90Sopenharmony_ci    result.clear();
365ccb8f90Sopenharmony_ci    auto argc = args.size();
375ccb8f90Sopenharmony_ci    if ((argc == 0) || (args[0] == ARGS_HELP)) {
385ccb8f90Sopenharmony_ci        ShowUsage(result);
395ccb8f90Sopenharmony_ci        return true;
405ccb8f90Sopenharmony_ci    }
415ccb8f90Sopenharmony_ci    auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
425ccb8f90Sopenharmony_ci    if (pms == nullptr) {
435ccb8f90Sopenharmony_ci        return true;
445ccb8f90Sopenharmony_ci    }
455ccb8f90Sopenharmony_ci    if (DumpArg(pms, args[0])) {
465ccb8f90Sopenharmony_ci        return true;
475ccb8f90Sopenharmony_ci    }
485ccb8f90Sopenharmony_ci    for (auto it = args.begin(); it != args.end(); it++) {
495ccb8f90Sopenharmony_ci        if (*it == ARGS_RUNNINGLOCK) {
505ccb8f90Sopenharmony_ci            auto runningLockMgr = pms->GetRunningLockMgr();
515ccb8f90Sopenharmony_ci            if (runningLockMgr == nullptr) {
525ccb8f90Sopenharmony_ci                continue;
535ccb8f90Sopenharmony_ci            }
545ccb8f90Sopenharmony_ci            runningLockMgr->DumpInfo(result);
555ccb8f90Sopenharmony_ci        } else if (*it == ARGS_STATE) {
565ccb8f90Sopenharmony_ci            auto stateMachine = pms->GetPowerStateMachine();
575ccb8f90Sopenharmony_ci            if (stateMachine == nullptr) {
585ccb8f90Sopenharmony_ci                continue;
595ccb8f90Sopenharmony_ci            }
605ccb8f90Sopenharmony_ci            stateMachine->DumpInfo(result);
615ccb8f90Sopenharmony_ci        } else if (*it == ARGS_ALL) {
625ccb8f90Sopenharmony_ci            result.clear();
635ccb8f90Sopenharmony_ci            auto stateMachine = pms->GetPowerStateMachine();
645ccb8f90Sopenharmony_ci            if (stateMachine == nullptr) {
655ccb8f90Sopenharmony_ci                continue;
665ccb8f90Sopenharmony_ci            }
675ccb8f90Sopenharmony_ci            stateMachine->DumpInfo(result);
685ccb8f90Sopenharmony_ci            auto runningLockMgr = pms->GetRunningLockMgr();
695ccb8f90Sopenharmony_ci            if (runningLockMgr == nullptr) {
705ccb8f90Sopenharmony_ci                continue;
715ccb8f90Sopenharmony_ci            }
725ccb8f90Sopenharmony_ci            runningLockMgr->DumpInfo(result);
735ccb8f90Sopenharmony_ci            break;
745ccb8f90Sopenharmony_ci        }
755ccb8f90Sopenharmony_ci    }
765ccb8f90Sopenharmony_ci    return true;
775ccb8f90Sopenharmony_ci}
785ccb8f90Sopenharmony_ci
795ccb8f90Sopenharmony_cibool PowerMgrDumper::DumpArg(const sptr<PowerMgrService>& pms, const std::string& arg)
805ccb8f90Sopenharmony_ci{
815ccb8f90Sopenharmony_ci    if (arg == ARGS_DIALOG) {
825ccb8f90Sopenharmony_ci        pms->GetShutdownDialog().ConnectSystemUi();
835ccb8f90Sopenharmony_ci        return true;
845ccb8f90Sopenharmony_ci    }
855ccb8f90Sopenharmony_ci    if (arg == ARGS_REG_KEY) {
865ccb8f90Sopenharmony_ci        pms->GetShutdownDialog().KeyMonitorInit();
875ccb8f90Sopenharmony_ci        return true;
885ccb8f90Sopenharmony_ci    }
895ccb8f90Sopenharmony_ci    if (arg == ARGS_On) {
905ccb8f90Sopenharmony_ci        pms->KeepScreenOn(true);
915ccb8f90Sopenharmony_ci        return true;
925ccb8f90Sopenharmony_ci    }
935ccb8f90Sopenharmony_ci    if (arg == ARGS_Off) {
945ccb8f90Sopenharmony_ci        pms->KeepScreenOn(false);
955ccb8f90Sopenharmony_ci        return true;
965ccb8f90Sopenharmony_ci    }
975ccb8f90Sopenharmony_ci    return false;
985ccb8f90Sopenharmony_ci}
995ccb8f90Sopenharmony_ci
1005ccb8f90Sopenharmony_civoid PowerMgrDumper::ShowUsage(std::string& result)
1015ccb8f90Sopenharmony_ci{
1025ccb8f90Sopenharmony_ci    result.append("Power manager dump options:\n")
1035ccb8f90Sopenharmony_ci        .append("  [-h] [-runninglock]\n")
1045ccb8f90Sopenharmony_ci        .append("  description of the cmd option:\n")
1055ccb8f90Sopenharmony_ci        .append("    -a: show dump info of all power modules.\n")
1065ccb8f90Sopenharmony_ci        .append("    -h: show this help.\n")
1075ccb8f90Sopenharmony_ci        .append("    -r: show the information of runninglock.\n")
1085ccb8f90Sopenharmony_ci        .append("    -s: show the information of power state machine.\n")
1095ccb8f90Sopenharmony_ci        .append("    -d: show power off dialog.\n");
1105ccb8f90Sopenharmony_ci}
1115ccb8f90Sopenharmony_ci} // namespace PowerMgr
1125ccb8f90Sopenharmony_ci} // namespace OHOS
113