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 } // namespace 59 60 struct PublishCmdInfo { 61 bool isSticky = false; 62 bool isOrdered = false; 63 int32_t code = 0; 64 int32_t userId = ALL_USER; 65 std::string action; 66 std::string data; 67 }; 68 69 struct DumpCmdInfo { 70 std::string action; 71 int32_t userId = ALL_USER; 72 DumpEventType eventType = DumpEventType::ALL; 73 }; 74 75 class CommonEventCommand : public OHOS::EventFwk::ShellCommand { 76 public: CommonEventCommand(int argc,char * argv[])77 CommonEventCommand(int argc, char *argv[]) : ShellCommand(argc, argv, TOOL_NAME) {} 78 ~CommonEventCommand() override = default; 79 80 private: 81 ErrCode CreateCommandMap() override; 82 ErrCode Init() override; 83 ErrCode RunAsHelpCommand(); 84 ErrCode RunAsPublishCommand(); 85 ErrCode RunAsDumpCommand(); 86 void CheckPublishOpt(); 87 void SetPublishCmdInfo(PublishCmdInfo &cmdInfo, ErrCode &result, bool &hasOption); 88 void CheckDumpOpt(); 89 void SetDumpCmdInfo(DumpCmdInfo &cmdInfo, ErrCode &result, bool &hasOption); 90 void CheckDumpEventType(DumpCmdInfo &cmdInfo, ErrCode &result); 91 92 std::shared_ptr<CommonEvent> commonEventPtr_ = nullptr; 93 }; 94 } // namespace EventFwk 95 } // namespace OHOS 96 97 #endif // BASE_NOTIFICATION_CES_STANDARD_CESFWK_TOOLS_CEM_INCLUDE_COMMON_EVENT_COMMAND_H 98