1 /*
2 * Copyright (c) 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 "workscheduler_shell_command.h"
17
18 #include <iostream>
19
20 #include <getopt.h>
21
22 #include "event_publisher.h"
23 #include "iservice_registry.h"
24 #include "singleton.h"
25
26 namespace OHOS {
27 namespace WorkScheduler {
28 namespace {
29 auto& client_ = WorkSchedulerSrvClient::GetInstance();
30
31 static const struct option OPTIONS[] = {
32 {"help", no_argument, nullptr, 'h'},
33 {"all", no_argument, nullptr, 'A'},
34 };
35
36 static const std::string DUMP_HELP_MSG =
37 "usage: workscheduler dump [<options>]\n"
38 "options list:\n"
39 " -h help menu\n"
40 " -A All dump all infos\n"
41 " -A WorkQueue dump work queue infos\n"
42 " -A WorkPolicy dump work policy infos\n"
43 " -A DebugInfo dump debug info\n"
44 " -A CheckBundle (bool) set debug of checking bundle, default false\n"
45 " -A SetMemory (number) set the available memory\n"
46 " -A SetWatchdogTime (number) set watch dog time, default 120000\n"
47 " -A SetRepeatCycleTimeMin (number) set min repeat cycle time, default 1200000\n"
48 " -E help show publish common event help menu\n";
49 } // namespace
50
WorkSchedulerShellCommand(int argc,char * argv[])51 WorkSchedulerShellCommand::WorkSchedulerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "workscheduler")
52 {}
53
CreateCommandMap()54 ErrCode WorkSchedulerShellCommand::CreateCommandMap()
55 {
56 commandMap_ = {
57 {"dump", std::bind(&WorkSchedulerShellCommand::RunAsDumpCommand, this)},
58 };
59 return ERR_OK;
60 }
61
CreateMessageMap()62 ErrCode WorkSchedulerShellCommand::CreateMessageMap()
63 {
64 messageMap_ = {};
65 return ERR_OK;
66 }
67
init()68 ErrCode WorkSchedulerShellCommand::init()
69 {
70 return ERR_OK;
71 }
72
RunAsDumpCommand()73 ErrCode WorkSchedulerShellCommand::RunAsDumpCommand()
74 {
75 int ind = 0;
76 int option = getopt_long(argc_, argv_, "hAE", OPTIONS, &ind);
77 std::vector<std::string> infos;
78 switch (option) {
79 case 'h':
80 resultReceiver_.append(DUMP_HELP_MSG);
81 break;
82 case 'A':
83 if (!client_.ShellDump(argList_, infos)) {
84 resultReceiver_.append("ErrNo: " + std::to_string(ERR_INVALID_VALUE) + "\n");
85 return ERR_INVALID_VALUE;
86 }
87 break;
88 case 'E':
89 EventPublisher eventPublisher;
90 eventPublisher.PublishEvent(argList_, infos);
91 break;
92 default:
93 resultReceiver_.append("please add right options.\n");
94 resultReceiver_.append(DUMP_HELP_MSG);
95 break;
96 }
97 for (auto info : infos) {
98 resultReceiver_.append(info);
99 resultReceiver_.append("\n");
100 }
101 return ERR_OK;
102 }
103 } // namespace WorkScheduler
104 } // namespace OHOS