1/*
2* Copyright (c) 2021 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 "service_control.h"
16
17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20
21#include "begetctl.h"
22#include "init_utils.h"
23
24#define SERVICE_START_NUMBER 2
25#define SERVICE_CONTROL_NUMBER 3
26#define CONTROL_SERVICE_POS 2
27#define SERVICE_CONTROL_MAX_SIZE 50
28
29static void ServiceControlUsage(BShellHandle shell, int argc, char **argv)
30{
31    BShellCmdHelp(shell, argc, argv);
32    return;
33}
34
35static int main_cmd(BShellHandle shell, int argc, char **argv)
36{
37    if (argc < SERVICE_START_NUMBER) {
38        ServiceControlUsage(shell, argc, argv);
39        return 0;
40    }
41    if (strcmp(argv[0], "start_service") == 0) {
42        ServiceControlWithExtra(argv[1], 0, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
43    } else if (strcmp(argv[0], "stop_service") == 0) {
44        ServiceControlWithExtra(argv[1], 1, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
45    } else if (strcmp(argv[0], "start") == 0) {
46        ServiceControlWithExtra(argv[1], 0, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
47    } else if (strcmp(argv[0], "stop") == 0) {
48        ServiceControlWithExtra(argv[1], 1, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
49    } else if (strcmp(argv[0], "timer_start") == 0) {
50        if (argc <= SERVICE_START_NUMBER) {
51            ServiceControlUsage(shell, argc, argv);
52            return 0;
53        }
54        char *timeBuffer = argv[SERVICE_START_NUMBER];
55        errno = 0;
56        uint64_t timeout = strtoull(timeBuffer, NULL, DECIMAL_BASE);
57        if (errno != 0) {
58            return -1;
59        }
60        StartServiceByTimer(argv[1], timeout);
61    } else if (strcmp(argv[0], "timer_stop") == 0) {
62        StopServiceTimer(argv[1]);
63    } else {
64        ServiceControlUsage(shell, argc, argv);
65    }
66    return 0;
67}
68
69MODULE_CONSTRUCTOR(void)
70{
71    const CmdInfo infos[] = {
72        {"service_control", main_cmd, "stop service", "service_control stop servicename", "service_control stop"},
73        {"service_control", main_cmd, "start service", "service_control start servicename", "service_control start"},
74        {"stop_service", main_cmd, "stop service", "stop_service servicename", ""},
75        {"start_service", main_cmd, "start service", "start_service servicename", ""},
76        {"timer_start", main_cmd, "start service by timer", "timer_start servicename timeout", ""},
77        {"timer_stop", main_cmd, "stop service timer", "timer_stop servicename", ""},
78    };
79    for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
80        BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
81    }
82}
83