1 /*
2 * Copyright (c) 2021 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 "thermal_shell_command.h"
17
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <getopt.h>
21 #include <iostream>
22 #include <string_ex.h>
23 #include <unistd.h>
24
25 #include "iservice_registry.h"
26 #include "singleton.h"
27 #include "system_ability_definition.h"
28
29 namespace OHOS {
30 namespace PowerMgr {
31 static const std::string HELP_MSG =
32 "usage: thermal-shell\n"
33 "command list:\n"
34 " dump : Dump thermal info. \n"
35 " help : Show this help menu. \n";
36
ThermalShellCommand(int argc,char * argv[])37 ThermalShellCommand::ThermalShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "thermal-shell")
38 {}
39
CreateCommandMap()40 ErrCode ThermalShellCommand::CreateCommandMap()
41 {
42 commandMap_ = {
43 {"help", std::bind(&ThermalShellCommand::RunAsHelpCommand, this)},
44 {"dump", std::bind(&ThermalShellCommand::RunAsDumpCommand, this)},
45 };
46
47 return ERR_OK;
48 }
49
CreateMessageMap()50 ErrCode ThermalShellCommand::CreateMessageMap()
51 {
52 messageMap_ = {};
53
54 return ERR_OK;
55 }
56
init()57 ErrCode ThermalShellCommand::init()
58 {
59 return OHOS::ERR_OK;
60 }
61
RunAsHelpCommand()62 ErrCode ThermalShellCommand::RunAsHelpCommand()
63 {
64 resultReceiver_.clear();
65 resultReceiver_.append(HELP_MSG);
66 return ERR_OK;
67 }
68
RunAsDumpCommand()69 ErrCode ThermalShellCommand::RunAsDumpCommand()
70 {
71 resultReceiver_.clear();
72
73 ThermalMgrClient &client = ThermalMgrClient::GetInstance();
74 std::string ret = client.Dump(argList_);
75 resultReceiver_.append("Thermal Dump result: \n");
76 resultReceiver_.append(ret);
77
78 return ERR_OK;
79 }
80
PrintDumpFileError(std::string & receiver,const char * path)81 extern "C" void PrintDumpFileError(std::string& receiver, const char* path)
82 {
83 receiver.append("Open Dump file (");
84 receiver.append(path);
85 receiver.append(") failed: ");
86 receiver.append(std::to_string(errno));
87 receiver.append("\n");
88 }
89 } // namespace Powermgr
90 } // namespace OHOS