1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci* Copyright (c) 2021 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci* you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci* You may obtain a copy of the License at
6d9f0492fSopenharmony_ci*
7d9f0492fSopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci*
9d9f0492fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci* See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci* limitations under the License.
14d9f0492fSopenharmony_ci*/
15d9f0492fSopenharmony_ci#include "service_control.h"
16d9f0492fSopenharmony_ci
17d9f0492fSopenharmony_ci#include <errno.h>
18d9f0492fSopenharmony_ci#include <stdio.h>
19d9f0492fSopenharmony_ci#include <string.h>
20d9f0492fSopenharmony_ci
21d9f0492fSopenharmony_ci#include "begetctl.h"
22d9f0492fSopenharmony_ci#include "init_utils.h"
23d9f0492fSopenharmony_ci
24d9f0492fSopenharmony_ci#define SERVICE_START_NUMBER 2
25d9f0492fSopenharmony_ci#define SERVICE_CONTROL_NUMBER 3
26d9f0492fSopenharmony_ci#define CONTROL_SERVICE_POS 2
27d9f0492fSopenharmony_ci#define SERVICE_CONTROL_MAX_SIZE 50
28d9f0492fSopenharmony_ci
29d9f0492fSopenharmony_cistatic void ServiceControlUsage(BShellHandle shell, int argc, char **argv)
30d9f0492fSopenharmony_ci{
31d9f0492fSopenharmony_ci    BShellCmdHelp(shell, argc, argv);
32d9f0492fSopenharmony_ci    return;
33d9f0492fSopenharmony_ci}
34d9f0492fSopenharmony_ci
35d9f0492fSopenharmony_cistatic int main_cmd(BShellHandle shell, int argc, char **argv)
36d9f0492fSopenharmony_ci{
37d9f0492fSopenharmony_ci    if (argc < SERVICE_START_NUMBER) {
38d9f0492fSopenharmony_ci        ServiceControlUsage(shell, argc, argv);
39d9f0492fSopenharmony_ci        return 0;
40d9f0492fSopenharmony_ci    }
41d9f0492fSopenharmony_ci    if (strcmp(argv[0], "start_service") == 0) {
42d9f0492fSopenharmony_ci        ServiceControlWithExtra(argv[1], 0, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
43d9f0492fSopenharmony_ci    } else if (strcmp(argv[0], "stop_service") == 0) {
44d9f0492fSopenharmony_ci        ServiceControlWithExtra(argv[1], 1, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
45d9f0492fSopenharmony_ci    } else if (strcmp(argv[0], "start") == 0) {
46d9f0492fSopenharmony_ci        ServiceControlWithExtra(argv[1], 0, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
47d9f0492fSopenharmony_ci    } else if (strcmp(argv[0], "stop") == 0) {
48d9f0492fSopenharmony_ci        ServiceControlWithExtra(argv[1], 1, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
49d9f0492fSopenharmony_ci    } else if (strcmp(argv[0], "timer_start") == 0) {
50d9f0492fSopenharmony_ci        if (argc <= SERVICE_START_NUMBER) {
51d9f0492fSopenharmony_ci            ServiceControlUsage(shell, argc, argv);
52d9f0492fSopenharmony_ci            return 0;
53d9f0492fSopenharmony_ci        }
54d9f0492fSopenharmony_ci        char *timeBuffer = argv[SERVICE_START_NUMBER];
55d9f0492fSopenharmony_ci        errno = 0;
56d9f0492fSopenharmony_ci        uint64_t timeout = strtoull(timeBuffer, NULL, DECIMAL_BASE);
57d9f0492fSopenharmony_ci        if (errno != 0) {
58d9f0492fSopenharmony_ci            return -1;
59d9f0492fSopenharmony_ci        }
60d9f0492fSopenharmony_ci        StartServiceByTimer(argv[1], timeout);
61d9f0492fSopenharmony_ci    } else if (strcmp(argv[0], "timer_stop") == 0) {
62d9f0492fSopenharmony_ci        StopServiceTimer(argv[1]);
63d9f0492fSopenharmony_ci    } else {
64d9f0492fSopenharmony_ci        ServiceControlUsage(shell, argc, argv);
65d9f0492fSopenharmony_ci    }
66d9f0492fSopenharmony_ci    return 0;
67d9f0492fSopenharmony_ci}
68d9f0492fSopenharmony_ci
69d9f0492fSopenharmony_ciMODULE_CONSTRUCTOR(void)
70d9f0492fSopenharmony_ci{
71d9f0492fSopenharmony_ci    const CmdInfo infos[] = {
72d9f0492fSopenharmony_ci        {"service_control", main_cmd, "stop service", "service_control stop servicename", "service_control stop"},
73d9f0492fSopenharmony_ci        {"service_control", main_cmd, "start service", "service_control start servicename", "service_control start"},
74d9f0492fSopenharmony_ci        {"stop_service", main_cmd, "stop service", "stop_service servicename", ""},
75d9f0492fSopenharmony_ci        {"start_service", main_cmd, "start service", "start_service servicename", ""},
76d9f0492fSopenharmony_ci        {"timer_start", main_cmd, "start service by timer", "timer_start servicename timeout", ""},
77d9f0492fSopenharmony_ci        {"timer_stop", main_cmd, "stop service timer", "timer_stop servicename", ""},
78d9f0492fSopenharmony_ci    };
79d9f0492fSopenharmony_ci    for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
80d9f0492fSopenharmony_ci        BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
81d9f0492fSopenharmony_ci    }
82d9f0492fSopenharmony_ci}
83