diff --git a/frameworks/native/power_mgr_client.cpp b/frameworks/native/power_mgr_client.cpp index 0b40463ec9f734ac35c0f69c16c12c01e0f14702..c205db8e69bf7178d31fe8af464e9d9f408f3eb6 100644 --- a/frameworks/native/power_mgr_client.cpp +++ b/frameworks/native/power_mgr_client.cpp @@ -256,5 +256,13 @@ uint32_t PowerMgrClient::GetDeviceMode() POWER_HILOGE(MODULE_INNERKIT, "%{public}s called.", __func__); return proxy_->GetDeviceMode(); } + +int PowerMgrClient::Dump(int32_t fd, const std::vector& args) +{ + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + POWER_HILOGE(MODULE_INNERKIT, "%{public}s called.", __func__); + auto serviceRemote = proxy_->AsObject(); + return serviceRemote->Dump(fd, args); +} } // namespace PowerMgr } // namespace OHOS diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 5d632df8a560500cccf4c2ef72143100f2530905..46029ddf03aaba956a56e55d5587d670c6ca4c85 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -45,6 +45,7 @@ ohos_shared_library("powermgr_client") { deps = [ "${powermgr_utils_path}:powermgr_utils", + "//foundation/aafwk/standard/tools/aa:tools_aa_source_set", "//utils/native/base:utils", ] diff --git a/interfaces/innerkits/native/include/power_mgr_client.h b/interfaces/innerkits/native/include/power_mgr_client.h index 6704b9b33ff024cb0db1d2188cf5319f83d88b2b..d62d45a0501a47335e278f76123e09a2f069d24b 100644 --- a/interfaces/innerkits/native/include/power_mgr_client.h +++ b/interfaces/innerkits/native/include/power_mgr_client.h @@ -97,6 +97,14 @@ public: */ void SetDisplaySuspend(bool enable); + /** + * Enum for set/get device mode + */ + enum { + NORMAL_MODE = 600, + POWER_SAVE_MODE = 601, + EXTREME_MODE = 602 + }; /* Set the device mode. * * @param set The mode the device. @@ -119,6 +127,7 @@ public: void UnRegisterShutdownCallback(const sptr& callback); void RegisterPowerModeCallback(const sptr& callback); void UnRegisterPowerModeCallback(const sptr& callback); + int Dump(int32_t fd, const std::vector& args); private: class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/ohos.build b/ohos.build index c48be5c0f0fcdca20ad38496b481455fa47e1cde..20502dcc8c0f2eda221232eb7d540d5439d4e4db 100644 --- a/ohos.build +++ b/ohos.build @@ -9,7 +9,8 @@ "//base/powermgr/power_manager/sa_profile:powermgr_sa_profile", "//base/powermgr/power_manager/services:powermgrservice", "//base/powermgr/power_manager/hdi:hdi_group", - "//base/powermgr/power_manager/services:power_service" + "//base/powermgr/power_manager/services:power_service", + "//base/powermgr/power_manager/utils:power-shell" ], "inner_kits": [ { diff --git a/services/native/include/power_mode_module.h b/services/native/include/power_mode_module.h index 5dcfe5587d4437757a3ab763af9bba7ee9d1487f..ba8595606d817d5204ce50f9d6b8db3c1161c768 100644 --- a/services/native/include/power_mode_module.h +++ b/services/native/include/power_mode_module.h @@ -36,10 +36,9 @@ class PowerModeModule { public: enum { POWER_MODE_MIN = 600, - DEFAULT_MODE = POWER_MODE_MIN, - EXTREAM_MODE, - NORMAL_MODE, + NORMAL_MODE = POWER_MODE_MIN, SAVE_MODE, + EXTREME_MODE, LOWPOWER_MODE, POWER_MODE_MAX = LOWPOWER_MODE }; diff --git a/services/native/profile/power_mode_config.xml b/services/native/profile/power_mode_config.xml index 0b471c365c9d5c53afd685c637f4a476bfa974aa..7ea7f1f918a7fb5c0921b783de1dc09c39983d2e 100644 --- a/services/native/profile/power_mode_config.xml +++ b/services/native/profile/power_mode_config.xml @@ -1,10 +1,9 @@ @@ -20,23 +19,31 @@ - - - - - + + + + + + + + - - - - - + + + + + + - - + + + + + + diff --git a/services/native/src/power_mgr_dumper.cpp b/services/native/src/power_mgr_dumper.cpp index 081ee57bf280e3bead1dece21e0f0fe6ec9324fe..a842c5975c5450a4a4c3be6259f6956442d997d3 100644 --- a/services/native/src/power_mgr_dumper.cpp +++ b/services/native/src/power_mgr_dumper.cpp @@ -22,10 +22,11 @@ namespace OHOS { namespace PowerMgr { namespace { +const std::string ARGS_ALL = "-a"; const std::string ARGS_HELP = "-h"; -const std::string ARGS_RUNNINGLOCK = "-runninglock"; -const std::string ARGS_STATE = "-state"; -const std::string ARGS_HDF = "-hdf"; +const std::string ARGS_RUNNINGLOCK = "-r"; +const std::string ARGS_STATE = "-s"; +const std::string ARGS_HDF = "-d"; } bool PowerMgrDumper::Dump(const std::vector& args, std::string& result) @@ -55,6 +56,20 @@ bool PowerMgrDumper::Dump(const std::vector& args, std::string& res stateMachine->DumpInfo(result); } else if (*it == ARGS_HDF) { SystemSuspendController::GetInstance().Dump(result); + } else if (*it == ARGS_ALL) { + result.clear(); + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + continue; + } + stateMachine->DumpInfo(result); + auto runningLockMgr = pms->GetRunningLockMgr(); + if (runningLockMgr == nullptr) { + continue; + } + runningLockMgr->DumpInfo(result); + SystemSuspendController::GetInstance().Dump(result); + break; } } return true; @@ -65,8 +80,11 @@ void PowerMgrDumper::ShowUsage(std::string& result) result.append("Power manager dump options:\n") .append(" [-h] [-runninglock]\n") .append(" description of the cmd option:\n") + .append(" -a: show dump info of all power modules.\n") .append(" -h: show this help.\n") - .append(" -runninglock: show the all information of runninglock.\n"); + .append(" -r: show the information of runninglock.\n") + .append(" -s: show the information of power state machine.\n") + .append(" -d: show the information of power hdf.\n"); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 126c4721d4b225890bc1f19fd39969c7acecf93f..3ca7654d07f99c6b7dbc948e207184661ff96c48 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -89,7 +89,7 @@ bool PowerMgrService::Init() POWER_HILOGE(MODULE_SERVICE, "power state machine init fail!"); } if (DelayedSpSingleton::GetInstance()) { - powerModeModule_.SetModeItem(PowerModeModule::DEFAULT_MODE); + powerModeModule_.SetModeItem(PowerModeModule::NORMAL_MODE); } else { POWER_HILOGE(MODULE_SERVICE, "power mode init fail!"); } @@ -127,6 +127,7 @@ void PowerMgrService::OnStop() int32_t PowerMgrService::Dump(int32_t fd, const std::vector& args) { std::lock_guard lock(mutex_); + POWER_HILOGI(MODULE_SERVICE, "Dump service"); std::vector argsInStr; std::transform(args.begin(), args.end(), std::back_inserter(argsInStr), [](const std::u16string &arg) { diff --git a/services/native/src/power_mode_module.cpp b/services/native/src/power_mode_module.cpp index 2b0fd696e2f8d6cbb96fa707d21f63b0e2bb086d..bbf4901e748e47285a0f5df6ae3f2c043a45e887 100644 --- a/services/native/src/power_mode_module.cpp +++ b/services/native/src/power_mode_module.cpp @@ -180,9 +180,9 @@ void PowerModeModule::PublishPowerModeEvent() IntentWant setModeWant; CommonEventData event(setModeWant); switch (mode_) { - case PowerModeModule::EXTREAM_MODE: + case PowerModeModule::EXTREME_MODE: setModeWant.SetAction(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); - event.SetCode(PowerModeModule::EXTREAM_MODE); + event.SetCode(PowerModeModule::EXTREME_MODE); break; case PowerModeModule::NORMAL_MODE: setModeWant.SetAction(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 67e80c8962b96695f2fafc5dba8b04a794129781..e37679e978a471440bec9cb7aa64cf9a1012c869 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -34,3 +34,45 @@ ohos_source_set("powermgr_utils") { part_name = "${powermgr_native_part_name}" } + +ohos_executable("power-shell") { + include_dirs = [ + "${powermgr_interfaces_path}/innerkits/native/include", + "${powermgr_utils_path}/shell/include", + "//foundation/aafwk/standard/services/common/include", + "//foundation/aafwk/standard/tools/aa/include", + "//utils/native/base/include", + ] + + sources = [ + "shell/src/main.cpp", + "shell/src/power_shell_command.cpp", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${powermgr_interfaces_path}/innerkits:powermgr_client", + "//foundation/aafwk/standard/tools/aa:tools_aa_source_set", + "//utils/native/base:utils", + ] + + configs = [ + "//foundation/aafwk/standard/tools/aa:ability_command_config", + "//foundation/aafwk/standard/services/common:common_config", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr_standard:samgr_proxy", + ] + + install_enable = true + part_name = "${powermgr_native_part_name}" +} diff --git a/utils/shell/include/power_shell_command.h b/utils/shell/include/power_shell_command.h new file mode 100644 index 0000000000000000000000000000000000000000..c356fc38307f603964b75de904d7d92f85f25f34 --- /dev/null +++ b/utils/shell/include/power_shell_command.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef POWER_SHELL_COMMAND_H +#define POWER_SHELL_COMMAND_H + +#include "shell_command.h" + +#include "power_mgr_client.h" + +namespace OHOS { +namespace PowerMgr { +class PowerShellCommand : public OHOS::AAFwk::ShellCommand { +public: + PowerShellCommand(int argc, char *argv[]); + ~PowerShellCommand() override {}; + +private: + static constexpr const char * const TEMP_DUMP_LOG_PATH = "/data/test/power_dump.log"; + static constexpr int DUMP_BUFF_SIZE = 100; + ErrCode CreateCommandMap() override; + ErrCode CreateMessageMap() override; + ErrCode init() override; + ErrCode RunAsHelpCommand(); + ErrCode RunAsDumpCommand(); + ErrCode RunAsSetModeCommand(); +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWER_SHELL_COMMAND_H \ No newline at end of file diff --git a/utils/shell/src/main.cpp b/utils/shell/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53ce8dc595a5e929f742a50a6b72b14d524e0f65 --- /dev/null +++ b/utils/shell/src/main.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "power_shell_command.h" + +int main(int argc, char *argv[]) +{ + extern int errno; + OHOS::PowerMgr::PowerShellCommand cmd(argc, argv); + std::cout << cmd.ExecCommand(); + return 0; +} \ No newline at end of file diff --git a/utils/shell/src/power_shell_command.cpp b/utils/shell/src/power_shell_command.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d19fdf02ec030c84248937cd3205192c1c91829 --- /dev/null +++ b/utils/shell/src/power_shell_command.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "power_shell_command.h" + +#include +#include +#include +#include +#include +#include + +#include "power_mgr_client.h" +#include "iservice_registry.h" +#include "singleton.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace PowerMgr { +static const struct option SET_MODE_OPTIONS[] = { + {"help", no_argument, nullptr, 'h'}, +}; + +static const std::string HELP_MSG = + "usage: power-shell\n" + "command list:\n" + " setmode : Set power mode. \n" + " dump : Dump power info. \n" + " help : Show this help menu. \n"; + +static const std::string SETMODE_HELP_MSG = + "usage: power-shell setmode []\n" + "setmode \n" + " 600 : normal mode\n" + " 601 : power save mode\n" + " 602 : extreme mode\n"; + +PowerShellCommand::PowerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "power-shell") +{} + +ErrCode PowerShellCommand::CreateCommandMap() +{ + commandMap_ = { + {"help", std::bind(&PowerShellCommand::RunAsHelpCommand, this)}, + {"setmode", std::bind(&PowerShellCommand::RunAsSetModeCommand, this)}, + {"dump", std::bind(&PowerShellCommand::RunAsDumpCommand, this)}, + }; + + return ERR_OK; +} + +ErrCode PowerShellCommand::CreateMessageMap() +{ + messageMap_ = {}; + + return ERR_OK; +} + +ErrCode PowerShellCommand::init() +{ + return OHOS::ERR_OK; +} + +ErrCode PowerShellCommand::RunAsHelpCommand() +{ + resultReceiver_.clear(); + resultReceiver_.append(HELP_MSG); + return ERR_OK; +} + +ErrCode PowerShellCommand::RunAsSetModeCommand() +{ + int ind = 0; + int option = getopt_long(argc_, argv_, "h", SET_MODE_OPTIONS, &ind); + resultReceiver_.clear(); + if (option == 'h') { + resultReceiver_.append(SETMODE_HELP_MSG); + return ERR_OK; + } + if (argList_.empty()) { + resultReceiver_.append("Error! please input your mode value. \n"); + resultReceiver_.append(SETMODE_HELP_MSG); + return ERR_OK; + } + + int mode = atoi(argList_[0].c_str()); + resultReceiver_.append("Set Mode: "); + resultReceiver_.append(argList_[0]); + resultReceiver_.append("\n"); + PowerMgrClient &client = PowerMgrClient::GetInstance(); + client.SetDeviceMode(mode); + int result = client.GetDeviceMode(); + if (result == mode) { + resultReceiver_.append("Set Mode Success!"); + } else { + resultReceiver_.append("Set Mode Failed, current mode is: "); + resultReceiver_.append(std::to_string(result)); + } + + return ERR_OK; +} + +extern "C" void PrintDumpFileError(std::string& receiver, const char* path) +{ + extern int errno; + receiver.append("Open Dump file ("); + receiver.append(path); + receiver.append(") failed: "); + receiver.append(std::to_string(errno)); +} + +ErrCode PowerShellCommand::RunAsDumpCommand() +{ + resultReceiver_.clear(); + int fd = open(TEMP_DUMP_LOG_PATH, + O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + if (fd < 0) { + PrintDumpFileError(resultReceiver_, TEMP_DUMP_LOG_PATH); + return ERR_OK; + } + std::vector args; + for (int i = 2; i < argc_; i++) { + std::string str(argv_[i]); + args.push_back(Str8ToStr16(str)); + } + PowerMgrClient &client = PowerMgrClient::GetInstance(); + int ret = client.Dump(fd, args); + resultReceiver_.append("Power Dump result: "); + resultReceiver_.append(std::to_string(ret)); + resultReceiver_.append("\n"); + char buff[DUMP_BUFF_SIZE]; + while (read(fd, buff, sizeof(buff) - 1) > 0) { + resultReceiver_.append(buff); + } + close(fd); + remove(TEMP_DUMP_LOG_PATH); + return ERR_OK; +} +} +} // namespace OHOS