1/*
2* Copyright (c) 2022 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 <errno.h>
16#include <stdio.h>
17
18#include "begetctl.h"
19#include "control_fd.h"
20#include "init_utils.h"
21#include "init_param.h"
22#include "securec.h"
23#include "shell_utils.h"
24
25#define MODULE_CTL_CMD_ARGS 2
26static int32_t ModuleInstallCmd(BShellHandle shell, int32_t argc, char *argv[])
27{
28    if (argc != MODULE_CTL_CMD_ARGS) {
29        BShellCmdHelp(shell, argc, argv);
30        return 0;
31    }
32    BSH_LOGV("ModuleInstallCmd %s %s \n", argv[0], argv[1]);
33    char combinedArgs[MAX_BUFFER_LEN];
34    int ret = sprintf_s(combinedArgs, sizeof(combinedArgs), "ohos.servicectrl.%s", argv[0]);
35    BSH_CHECK(ret > 0, return -1, "Invalid buffer");
36    combinedArgs[ret] = '\0';
37    SystemSetParameter(combinedArgs, argv[1]);
38    return 0;
39}
40
41static int ModuleDisplayCmd(BShellHandle shell, int argc, char **argv)
42{
43    if (argc < 1) {
44        BShellCmdHelp(shell, argc, argv);
45        return 0;
46    }
47    CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_MODULEMGR, argv[0], NULL);
48    return 0;
49}
50
51MODULE_CONSTRUCTOR(void)
52{
53    const CmdInfo infos[] = {
54        {"modulectl", ModuleDisplayCmd, "dump all modules installed",
55            "modulectl list", "modulectl list"},
56        {"modulectl", ModuleInstallCmd, "install specified module",
57            "modulectl install moduleName", "modulectl install"},
58        {"modulectl", ModuleInstallCmd, "uninstall specified module",
59            "modulectl uninstall moduleName", "modulectl uninstall"},
60    };
61    for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
62        BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
63    }
64}
65