169570cc8Sopenharmony_ci/*
269570cc8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License.
569570cc8Sopenharmony_ci * You may obtain a copy of the License at
669570cc8Sopenharmony_ci *
769570cc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
869570cc8Sopenharmony_ci *
969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and
1369570cc8Sopenharmony_ci * limitations under the License.
1469570cc8Sopenharmony_ci */
1569570cc8Sopenharmony_ci#include <stdio.h>
1669570cc8Sopenharmony_ci#include <string.h>
1769570cc8Sopenharmony_ci#include <unistd.h>
1869570cc8Sopenharmony_ci
1969570cc8Sopenharmony_ci#include "devicedebug_base.h"
2069570cc8Sopenharmony_ci#include "devicedebug_kill.h"
2169570cc8Sopenharmony_ci
2269570cc8Sopenharmony_ci#ifdef __cplusplus
2369570cc8Sopenharmony_ciextern "C" {
2469570cc8Sopenharmony_ci#endif
2569570cc8Sopenharmony_ci
2669570cc8Sopenharmony_citypedef int (*DEVICEDEBUG_CMD_PROCESS_FUNC)(int argc, char *argv[]);
2769570cc8Sopenharmony_ci
2869570cc8Sopenharmony_citypedef struct DeviceDebugManagerCmdInfoStru {
2969570cc8Sopenharmony_ci    char *cmd;
3069570cc8Sopenharmony_ci    DEVICEDEBUG_CMD_PROCESS_FUNC process;
3169570cc8Sopenharmony_ci} DeviceDebugManagerCmdInfo;
3269570cc8Sopenharmony_ci
3369570cc8Sopenharmony_ciAPPSPAWN_STATIC int DeviceDebugShowHelp(int argc, char *argv[])
3469570cc8Sopenharmony_ci{
3569570cc8Sopenharmony_ci    (void)argc;
3669570cc8Sopenharmony_ci    (void)argv;
3769570cc8Sopenharmony_ci
3869570cc8Sopenharmony_ci    printf("\r\nusage: devicedebug <command> <options>\r\n"
3969570cc8Sopenharmony_ci        "\r\nThese are common devicedebug commands list:\r\n"
4069570cc8Sopenharmony_ci        "\r\n         help              list available commands"
4169570cc8Sopenharmony_ci        "\r\n         kill              send a signal to a process\r\n");
4269570cc8Sopenharmony_ci
4369570cc8Sopenharmony_ci    return 0;
4469570cc8Sopenharmony_ci}
4569570cc8Sopenharmony_ci
4669570cc8Sopenharmony_ciDeviceDebugManagerCmdInfo g_deviceDebugManagerCmd[] = {
4769570cc8Sopenharmony_ci    {"help", DeviceDebugShowHelp},
4869570cc8Sopenharmony_ci    {"-h", DeviceDebugShowHelp},
4969570cc8Sopenharmony_ci    {"kill", DeviceDebugCmdKill},
5069570cc8Sopenharmony_ci};
5169570cc8Sopenharmony_ci
5269570cc8Sopenharmony_ciAPPSPAWN_STATIC DeviceDebugManagerCmdInfo* DeviceDebugCmdCheck(const char *cmd)
5369570cc8Sopenharmony_ci{
5469570cc8Sopenharmony_ci    int cmdNum = sizeof(g_deviceDebugManagerCmd) / sizeof(DeviceDebugManagerCmdInfo);
5569570cc8Sopenharmony_ci
5669570cc8Sopenharmony_ci    for (int i = 0; i < cmdNum; i++) {
5769570cc8Sopenharmony_ci        if (!strcmp(cmd, g_deviceDebugManagerCmd[i].cmd)) {
5869570cc8Sopenharmony_ci            return &g_deviceDebugManagerCmd[i];
5969570cc8Sopenharmony_ci        }
6069570cc8Sopenharmony_ci    }
6169570cc8Sopenharmony_ci    return NULL;
6269570cc8Sopenharmony_ci}
6369570cc8Sopenharmony_ci
6469570cc8Sopenharmony_ciint main(int argc, char *argv[])
6569570cc8Sopenharmony_ci{
6669570cc8Sopenharmony_ci    int ret = 0;
6769570cc8Sopenharmony_ci    DeviceDebugManagerCmdInfo *cmdInfo = NULL;
6869570cc8Sopenharmony_ci
6969570cc8Sopenharmony_ci    if (argc < DEVICEDEBUG_NUM_2) {
7069570cc8Sopenharmony_ci        DeviceDebugShowHelp(argc, argv);
7169570cc8Sopenharmony_ci        return DEVICEDEBUG_ERRNO_PARAM_INVALID;
7269570cc8Sopenharmony_ci    }
7369570cc8Sopenharmony_ci
7469570cc8Sopenharmony_ci    DEVICEDEBUG_LOGI("devicedebug manager process start.");
7569570cc8Sopenharmony_ci
7669570cc8Sopenharmony_ci    /* 检验用户命令,获取对应的处理函数 */
7769570cc8Sopenharmony_ci    cmdInfo = DeviceDebugCmdCheck(argv[DEVICEDEBUG_NUM_1]);
7869570cc8Sopenharmony_ci    if (cmdInfo == NULL) {
7969570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("invalid cmd!. cmd:%{public}s\r\n", argv[DEVICEDEBUG_NUM_1]);
8069570cc8Sopenharmony_ci        return DEVICEDEBUG_ERRNO_OPERATOR_TYPE_INVALID;
8169570cc8Sopenharmony_ci    }
8269570cc8Sopenharmony_ci
8369570cc8Sopenharmony_ci    /* 执行命令 */
8469570cc8Sopenharmony_ci    ret = cmdInfo->process(argc, argv);
8569570cc8Sopenharmony_ci
8669570cc8Sopenharmony_ci    /* 返回值依赖此条log打印,切勿随意修改 */
8769570cc8Sopenharmony_ci    DEVICEDEBUG_LOGI("devicedebug manager process exit. ret=%{public}d \r\n", ret);
8869570cc8Sopenharmony_ci    return ret;
8969570cc8Sopenharmony_ci}
9069570cc8Sopenharmony_ci
9169570cc8Sopenharmony_ci#ifdef __cplusplus
9269570cc8Sopenharmony_ci}
9369570cc8Sopenharmony_ci#endif