1/*
2 * Copyright (c) 2024 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#include <stdio.h>
16#include <string.h>
17#include <unistd.h>
18
19#include "devicedebug_base.h"
20#include "devicedebug_kill.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26typedef int (*DEVICEDEBUG_CMD_PROCESS_FUNC)(int argc, char *argv[]);
27
28typedef struct DeviceDebugManagerCmdInfoStru {
29    char *cmd;
30    DEVICEDEBUG_CMD_PROCESS_FUNC process;
31} DeviceDebugManagerCmdInfo;
32
33APPSPAWN_STATIC int DeviceDebugShowHelp(int argc, char *argv[])
34{
35    (void)argc;
36    (void)argv;
37
38    printf("\r\nusage: devicedebug <command> <options>\r\n"
39        "\r\nThese are common devicedebug commands list:\r\n"
40        "\r\n         help              list available commands"
41        "\r\n         kill              send a signal to a process\r\n");
42
43    return 0;
44}
45
46DeviceDebugManagerCmdInfo g_deviceDebugManagerCmd[] = {
47    {"help", DeviceDebugShowHelp},
48    {"-h", DeviceDebugShowHelp},
49    {"kill", DeviceDebugCmdKill},
50};
51
52APPSPAWN_STATIC DeviceDebugManagerCmdInfo* DeviceDebugCmdCheck(const char *cmd)
53{
54    int cmdNum = sizeof(g_deviceDebugManagerCmd) / sizeof(DeviceDebugManagerCmdInfo);
55
56    for (int i = 0; i < cmdNum; i++) {
57        if (!strcmp(cmd, g_deviceDebugManagerCmd[i].cmd)) {
58            return &g_deviceDebugManagerCmd[i];
59        }
60    }
61    return NULL;
62}
63
64int main(int argc, char *argv[])
65{
66    int ret = 0;
67    DeviceDebugManagerCmdInfo *cmdInfo = NULL;
68
69    if (argc < DEVICEDEBUG_NUM_2) {
70        DeviceDebugShowHelp(argc, argv);
71        return DEVICEDEBUG_ERRNO_PARAM_INVALID;
72    }
73
74    DEVICEDEBUG_LOGI("devicedebug manager process start.");
75
76    /* 检验用户命令,获取对应的处理函数 */
77    cmdInfo = DeviceDebugCmdCheck(argv[DEVICEDEBUG_NUM_1]);
78    if (cmdInfo == NULL) {
79        DEVICEDEBUG_LOGE("invalid cmd!. cmd:%{public}s\r\n", argv[DEVICEDEBUG_NUM_1]);
80        return DEVICEDEBUG_ERRNO_OPERATOR_TYPE_INVALID;
81    }
82
83    /* 执行命令 */
84    ret = cmdInfo->process(argc, argv);
85
86    /* 返回值依赖此条log打印,切勿随意修改 */
87    DEVICEDEBUG_LOGI("devicedebug manager process exit. ret=%{public}d \r\n", ret);
88    return ret;
89}
90
91#ifdef __cplusplus
92}
93#endif