• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "hiview_lite_command.h"
17 
18 #include <ctype.h>
19 
20 #include "securec.h"
21 #include "ohos_types.h"
22 #include "hiview_util.h"
23 #include "hiview_config.h"
24 #include "hiview_log.h"
25 
26 #define CMD_MIN_LEN     2
27 #define CMD_MAX_LEN     32
28 #define CMD_HIEVENT     "hievent"
29 #define OPTION_TAG      '-'
30 #define OPTION_SET      'c'
31 #define OPTION_HELP     'h'
32 #define STR_MAX_LEN     128
33 
34 static boolean CheckCmdStr(const char *cmd);
35 static void HieventHelpProc(void);
36 static void HieventSetProc(const char *cmd);
37 
38 /* Command does not contain the "hievent". */
HieventCmdProc(const char * cmd)39 void HieventCmdProc(const char *cmd)
40 {
41     if (cmd == NULL) {
42         return;
43     }
44 
45     int32 len = strnlen(cmd, CMD_MAX_LEN + 1);
46     if ((len < CMD_MIN_LEN) || (len > CMD_MAX_LEN) || (CheckCmdStr(cmd) == FALSE)) {
47         HIVIEW_UartPrint("Invalid command.\n");
48         return;
49     }
50 
51     if (*cmd != OPTION_TAG) {
52         HIVIEW_UartPrint("Invalid command.\n");
53         return;
54     }
55 
56     switch (*(++cmd)) {
57         case OPTION_HELP:
58             HieventHelpProc();
59             break;
60         case OPTION_SET:
61             HieventSetProc(++cmd);
62             break;
63         default:
64             HIVIEW_UartPrint("Invalid command.\n");
65             break;
66     }
67 }
68 
HieventHelpProc(void)69 static void HieventHelpProc(void)
70 {
71     HIVIEW_UartPrint("hievent [-h] [-c]\n");
72     HIVIEW_UartPrint(" -h            Help\n");
73     HIVIEW_UartPrint(" -c            Enable or disable event function\n");
74 }
75 
HieventSetProc(const char * cmd)76 static void HieventSetProc(const char *cmd)
77 {
78     if (*cmd != '\0') {
79         HIVIEW_UartPrint("Invalid command.\n");
80         return;
81     }
82 
83     if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_ON) {
84         SwitchEvent(HIVIEW_FEATURE_OFF);
85         HIVIEW_UartPrint("Close event function success.\n");
86     } else {
87         SwitchEvent(HIVIEW_FEATURE_ON);
88         HIVIEW_UartPrint("Open event function success.\n");
89     }
90 }
91 
CheckCmdStr(const char * cmd)92 static boolean CheckCmdStr(const char *cmd)
93 {
94     while (*cmd != '\0') {
95         if (!(isalnum(*cmd) || (*cmd == ' ') || (*cmd == '\n') ||
96             (*cmd == '=') || (*cmd == '-'))) {
97             return FALSE;
98         }
99         cmd++;
100     }
101     return TRUE;
102 }
103 
SwitchEvent(uint8 flag)104 void SwitchEvent(uint8 flag)
105 {
106     g_hiviewConfig.eventSwitch = (flag == HIVIEW_FEATURE_ON) ? HIVIEW_FEATURE_ON : HIVIEW_FEATURE_OFF
107 }
108