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 #ifndef BASE_NOTIFICATION_CES_STANDARD_CESFWK_TOOLS_CEM_INCLUDE_COMMON_EVENT_COMMAND_H
17 #define BASE_NOTIFICATION_CES_STANDARD_CESFWK_TOOLS_CEM_INCLUDE_COMMON_EVENT_COMMAND_H
18 
19 #include "common_event.h"
20 #include "common_event_constant.h"
21 #include "shell_command.h"
22 
23 namespace OHOS {
24 namespace EventFwk {
25 namespace {
26 constexpr char TOOL_NAME[] = "cem";
27 constexpr char HELP_MSG[] =
28     "usage: cem <command> [<options>]\n"
29     "These are common cem commands list:\n"
30     "  help                         list available commands\n"
31     "  publish                      publish a common event with options\n"
32     "  dump                         dump the info of events\n";
33 
34 constexpr char HELP_MSG_PUBLISH[] =
35     "usage: cem publish [<options>]\n"
36     "options list:\n"
37     "  -h, --help                                       list available commands\n"
38     "  -e, --event <name> [-s, --sticky] [-o, --ordered] [-c, --code <code>] [-d, --data <data>]\n"
39     "                                                   publish a common event\n";
40 
41 constexpr char HELP_MSG_DUMP[] =
42     "usage: cem dump [<options>]\n"
43     "options list:\n"
44     "  -h, --help                   list available commands\n"
45     "  -a, --all                    dump the info of all events\n"
46     "  -e, --event <name>           dump the info filter by the specified event\n"
47     "  -u, --user-id <userId>       dump the info filter by the specified userId\n"
48     "  -p, --part <name>            dump the info of part events\n"
49     "       subscriber              all subscribers\n"
50     "       sticky                  sticky events\n"
51     "       pending                 pending events\n"
52     "       history                 history events\n";
53 
54 constexpr char HELP_MSG_NO_EVENT_OPTION[] = "error: you must specify an event name with '-e' or '--event'.\n";
55 constexpr char STRING_PUBLISH_COMMON_EVENT_OK[] = "publish the common event successfully.\n";
56 constexpr char STRING_PUBLISH_COMMON_EVENT_NG[] = "error: failed to publish the common event.\n";
57 constexpr char STRING_DUMP_COMMON_EVENT_NG[] = "error: failed to dump the common event(s).\n";
58 constexpr char USER_PUBLISH_COMMON_EVENT_NG[] = "error: user version cannot publish common events.\n";
59 constexpr char USER_DUMP_COMMON_EVENT_NG[] = "error: user version cannot use dump.\n";
60 }  // namespace
61 
62 struct PublishCmdInfo {
63     bool isSticky = false;
64     bool isOrdered = false;
65     int32_t code = 0;
66     int32_t userId = UNDEFINED_USER;
67     std::string action;
68     std::string data;
69 };
70 
71 struct DumpCmdInfo {
72     std::string action;
73     int32_t userId = ALL_USER;
74     DumpEventType eventType = DumpEventType::ALL;
75 };
76 
77 class CommonEventCommand : public OHOS::EventFwk::ShellCommand {
78 public:
CommonEventCommand(int argc,char * argv[])79     CommonEventCommand(int argc, char *argv[]) : ShellCommand(argc, argv, TOOL_NAME) {}
80     ~CommonEventCommand() override = default;
81 
82 private:
83     ErrCode CreateCommandMap() override;
84     ErrCode RunAsHelpCommand();
85     ErrCode RunAsPublishCommand();
86     ErrCode RunAsDumpCommand();
87     void CheckPublishOpt();
88     void SetPublishCmdInfo(PublishCmdInfo &cmdInfo, ErrCode &result, bool &hasOption);
89     void CheckDumpOpt();
90     void SetDumpCmdInfo(DumpCmdInfo &cmdInfo, ErrCode &result, bool &hasOption);
91     void CheckDumpEventType(DumpCmdInfo &cmdInfo, ErrCode &result);
92 };
93 }  // namespace EventFwk
94 }  // namespace OHOS
95 
96 #endif  // BASE_NOTIFICATION_CES_STANDARD_CESFWK_TOOLS_CEM_INCLUDE_COMMON_EVENT_COMMAND_H
97