• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "event_dump.h"
17 
18 #include <getopt.h>
19 
20 #include <climits>
21 #include <cstdarg>
22 #include <cstring>
23 
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 
28 #include "event_interceptor_handler.h"
29 #include "event_monitor_handler.h"
30 #include "input_device_manager.h"
31 #include "input_event_handler.h"
32 #include "input_windows_manager.h"
33 #include "key_subscriber_handler.h"
34 #include "mouse_event_normalize.h"
35 #include "switch_subscriber_handler.h"
36 #include "securec.h"
37 #include "util_ex.h"
38 #include "util.h"
39 
40 namespace OHOS {
41 namespace MMI {
42 namespace {
43 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "EventDump" };
44 constexpr size_t MAX_COMMAND_COUNT { 32 };
45 } // namespace
46 
EventDump()47 EventDump::EventDump() {}
~EventDump()48 EventDump::~EventDump() {}
49 
ChkConfig(int32_t fd)50 void ChkConfig(int32_t fd)
51 {
52     mprintf(fd, "ChkMMIConfig: ");
53     mprintf(fd, "DEF_MMI_DATA_ROOT: %s\n", DEF_MMI_DATA_ROOT);
54     mprintf(fd, "EXP_CONFIG: %s\n", DEF_EXP_CONFIG);
55     mprintf(fd, "EXP_SOPATH: %s\n", DEF_EXP_SOPATH);
56 }
57 
CheckCount(int32_t fd,const std::vector<std::string> & args,size_t & count)58 void EventDump::CheckCount(int32_t fd, const std::vector<std::string> &args, size_t &count)
59 {
60     CALL_DEBUG_ENTER;
61     for (const auto &str : args) {
62         if (str.find("--") == 0) {
63             ++count;
64             continue;
65         }
66         if (str.find("-") == 0) {
67             count += str.size() - 1;
68             continue;
69         }
70     }
71 }
72 
ParseCommand(int32_t fd,const std::vector<std::string> & args)73 void EventDump::ParseCommand(int32_t fd, const std::vector<std::string> &args)
74 {
75     CALL_DEBUG_ENTER;
76     size_t count = 0;
77     CheckCount(fd, args, count);
78     if (count > MAX_COMMAND_COUNT) {
79         MMI_HILOGE("cmd param number not more than 32");
80         mprintf(fd, "cmd param number not more than 32\n");
81         return;
82     }
83     int32_t optionIndex = 0;
84     struct option dumpOptions[] = {
85         { "help", no_argument, 0, 'h' },
86         { "device", no_argument, 0, 'd' },
87         { "devicelist", no_argument, 0, 'l' },
88         { "windows", no_argument, 0, 'w' },
89         { "udsserver", no_argument, 0, 'u' },
90         { "subscriber", no_argument, 0, 's' },
91         { "monitor", no_argument, 0, 'o' },
92         { "interceptor", no_argument, 0, 'i' },
93         { "filter", no_argument, 0, 'f' },
94         { "mouse", no_argument, 0, 'm' },
95         { nullptr, 0, 0, 0 }
96     };
97     if (args.empty()) {
98         MMI_HILOGE("size of args can't be zero");
99         return;
100     }
101     char **argv = new (std::nothrow) char *[args.size()];
102     CHKPV(argv);
103     if (memset_s(argv, args.size() * sizeof(char*), 0, args.size() * sizeof(char*)) != EOK) {
104         MMI_HILOGE("Call memset_s failed");
105         delete[] argv;
106         return;
107     }
108     for (size_t i = 0; i < args.size(); ++i) {
109         argv[i] = new (std::nothrow) char[args[i].size() + 1];
110         if (argv[i] == nullptr) {
111             MMI_HILOGE("Failed to allocate memory");
112             goto RELEASE_RES;
113         }
114         if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) {
115             MMI_HILOGE("strcpy_s error");
116             goto RELEASE_RES;
117         }
118     }
119     optind = 1;
120     int32_t c;
121     while ((c = getopt_long (args.size(), argv, "hdlwusoifmc", dumpOptions, &optionIndex)) != -1) {
122         switch (c) {
123             case 'h': {
124                 DumpEventHelp(fd, args);
125                 break;
126             }
127             case 'd': {
128                 InputDevMgr->Dump(fd, args);
129                 break;
130             }
131             case 'l': {
132                 InputDevMgr->DumpDeviceList(fd, args);
133                 break;
134             }
135             case 'w': {
136                 WinMgr->Dump(fd, args);
137                 break;
138             }
139             case 'u': {
140                 auto udsServer = InputHandler->GetUDSServer();
141                 CHKPV(udsServer);
142                 udsServer->Dump(fd, args);
143                 break;
144             }
145             case 's': {
146 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
147                 auto subscriberHandler = InputHandler->GetSubscriberHandler();
148                 CHKPV(subscriberHandler);
149                 subscriberHandler->Dump(fd, args);
150 #else
151                 mprintf(fd, "Keyboard device does not support");
152 #endif // OHOS_BUILD_ENABLE_KEYBOARD
153                 break;
154             }
155             case 'o': {
156 #ifdef OHOS_BUILD_ENABLE_MONITOR
157                 auto monitorHandler = InputHandler->GetMonitorHandler();
158                 CHKPV(monitorHandler);
159                 monitorHandler->Dump(fd, args);
160 #else
161                 mprintf(fd, "Monitor function does not support");
162 #endif // OHOS_BUILD_ENABLE_MONITOR
163                 break;
164             }
165             case 'i': {
166 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
167                 auto interceptorHandler = InputHandler->GetInterceptorHandler();
168                 CHKPV(interceptorHandler);
169                 interceptorHandler->Dump(fd, args);
170 #else
171                 mprintf(fd, "Interceptor function does not support");
172 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
173                 break;
174             }
175             case 'f': {
176                 auto filterHandler = InputHandler->GetFilterHandler();
177                 CHKPV(filterHandler);
178                 filterHandler->Dump(fd, args);
179                 break;
180             }
181             case 'm': {
182 #ifdef OHOS_BUILD_ENABLE_POINTER
183                 MouseEventHdr->Dump(fd, args);
184 #else
185                 mprintf(fd, "Pointer device does not support");
186 #endif // OHOS_BUILD_ENABLE_POINTER
187                 break;
188             }
189             default: {
190                 mprintf(fd, "cmd param is error\n");
191                 DumpHelp(fd);
192                 break;
193             }
194         }
195     }
196     RELEASE_RES:
197     for (size_t i = 0; i < args.size(); ++i) {
198         if (argv[i] != nullptr) {
199             delete[] argv[i];
200         }
201     }
202     delete[] argv;
203 }
204 
DumpEventHelp(int32_t fd,const std::vector<std::string> & args)205 void EventDump::DumpEventHelp(int32_t fd, const std::vector<std::string> &args)
206 {
207     DumpHelp(fd);
208 }
209 
DumpHelp(int32_t fd)210 void EventDump::DumpHelp(int32_t fd)
211 {
212     mprintf(fd, "Usage:\t");
213     mprintf(fd, "      -h, --help: dump help\t");
214     mprintf(fd, "      -d, --device: dump the device information\t");
215     mprintf(fd, "      -l, --devicelist: dump the device list information\t");
216     mprintf(fd, "      -w, --windows: dump the windows information\t");
217     mprintf(fd, "      -u, --udsserver: dump the uds_server information\t");
218     mprintf(fd, "      -o, --monitor: dump the monitor information\t");
219     mprintf(fd, "      -s, --subscriber: dump the subscriber information\t");
220     mprintf(fd, "      -i, --interceptor: dump the interceptor information\t");
221     mprintf(fd, "      -f, --filter: dump the filter information\t");
222     mprintf(fd, "      -m, --mouse: dump the mouse information\t");
223 }
224 } // namespace MMI
225 } // namespace OHOS
226