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
1669570cc8Sopenharmony_ci#include <string.h>
1769570cc8Sopenharmony_ci#include <unistd.h>
1869570cc8Sopenharmony_ci
1969570cc8Sopenharmony_ci#include "hnp_base.h"
2069570cc8Sopenharmony_ci#include "hnp_installer.h"
2169570cc8Sopenharmony_ci
2269570cc8Sopenharmony_ci#ifdef __cplusplus
2369570cc8Sopenharmony_ciextern "C" {
2469570cc8Sopenharmony_ci#endif
2569570cc8Sopenharmony_ci
2669570cc8Sopenharmony_ciextern int HnpShowHelp(int argc, char *argv[]);
2769570cc8Sopenharmony_ci
2869570cc8Sopenharmony_citypedef int (*HNP_CMD_PROCESS_FUNC)(int argc, char *argv[]);
2969570cc8Sopenharmony_ci
3069570cc8Sopenharmony_citypedef struct NativeManagerCmdInfoStru {
3169570cc8Sopenharmony_ci    char *cmd;
3269570cc8Sopenharmony_ci    HNP_CMD_PROCESS_FUNC process;
3369570cc8Sopenharmony_ci} NativeManagerCmdInfo;
3469570cc8Sopenharmony_ci
3569570cc8Sopenharmony_ciNativeManagerCmdInfo g_nativeManagerCmd[] = {
3669570cc8Sopenharmony_ci    {"help", HnpShowHelp},
3769570cc8Sopenharmony_ci    {"-h", HnpShowHelp},
3869570cc8Sopenharmony_ci    {"install", HnpCmdInstall},
3969570cc8Sopenharmony_ci    {"uninstall", HnpCmdUnInstall}
4069570cc8Sopenharmony_ci};
4169570cc8Sopenharmony_ci
4269570cc8Sopenharmony_ciint HnpShowHelp(int argc, char *argv[])
4369570cc8Sopenharmony_ci{
4469570cc8Sopenharmony_ci    (void)argc;
4569570cc8Sopenharmony_ci    (void)argv;
4669570cc8Sopenharmony_ci
4769570cc8Sopenharmony_ci    HNP_LOGI("\r\nusage:hnp <command> <args> [-u <user id>][-p <hap package name>][-i <hap install path>][-f]"
4869570cc8Sopenharmony_ci            "[-s <hap source path>][-a <system abi>]\r\n"
4969570cc8Sopenharmony_ci        "\r\nThese are common hnp commands used in various situations:\r\n"
5069570cc8Sopenharmony_ci        "\r\ninstall: install one hap package"
5169570cc8Sopenharmony_ci        "\r\n           hnp install <-u [user id]> <-p [hap package name]> <-i [hap install path]> <-f>"
5269570cc8Sopenharmony_ci        "\r\n           -u    : [required]    user id"
5369570cc8Sopenharmony_ci        "\r\n           -p    : [required]    hap package name"
5469570cc8Sopenharmony_ci        "\r\n           -i    : [required]    hap install path"
5569570cc8Sopenharmony_ci        "\r\n           -s    : [required]    hap source path"
5669570cc8Sopenharmony_ci        "\r\n           -a    : [required]    system abi"
5769570cc8Sopenharmony_ci        "\r\n           -f    : [optional]    if provided, the hnp package will be installed forcely, ignoring old"
5869570cc8Sopenharmony_ci            " versions of the hnp package\r\n"
5969570cc8Sopenharmony_ci        "\r\nuninstall: uninstall one hap package"
6069570cc8Sopenharmony_ci        "\r\n           hnp uninstall <-u [user id]> <-p [hap package name]>"
6169570cc8Sopenharmony_ci        "\r\n           -u    : [required]    user id"
6269570cc8Sopenharmony_ci        "\r\n           -p    : [required]    hap package name"
6369570cc8Sopenharmony_ci        "\r\nfor example:\r\n"
6469570cc8Sopenharmony_ci        "\r\n    hnp install -u 1000 -p app_sample -i /data/app_sample/ -s /data/app_hap/demo.hap -a arm64 -f"
6569570cc8Sopenharmony_ci        "\r\n    hnp uninstall -u 1000 -p app_sample\r\n");
6669570cc8Sopenharmony_ci
6769570cc8Sopenharmony_ci    return 0;
6869570cc8Sopenharmony_ci}
6969570cc8Sopenharmony_ci
7069570cc8Sopenharmony_cistatic NativeManagerCmdInfo* HnpCmdCheck(const char *cmd)
7169570cc8Sopenharmony_ci{
7269570cc8Sopenharmony_ci    int i;
7369570cc8Sopenharmony_ci    int cmdNum = sizeof(g_nativeManagerCmd) / sizeof(NativeManagerCmdInfo);
7469570cc8Sopenharmony_ci
7569570cc8Sopenharmony_ci    for (i = 0; i < cmdNum; i++) {
7669570cc8Sopenharmony_ci        if (!strcmp(cmd, g_nativeManagerCmd[i].cmd)) {
7769570cc8Sopenharmony_ci            return &g_nativeManagerCmd[i];
7869570cc8Sopenharmony_ci        }
7969570cc8Sopenharmony_ci    }
8069570cc8Sopenharmony_ci    return NULL;
8169570cc8Sopenharmony_ci}
8269570cc8Sopenharmony_ci
8369570cc8Sopenharmony_ciint main(int argc, char *argv[])
8469570cc8Sopenharmony_ci{
8569570cc8Sopenharmony_ci    int ret;
8669570cc8Sopenharmony_ci    NativeManagerCmdInfo *cmdInfo = NULL;
8769570cc8Sopenharmony_ci
8869570cc8Sopenharmony_ci    if (argc < HNP_INDEX_2) {
8969570cc8Sopenharmony_ci        HnpShowHelp(argc, argv);
9069570cc8Sopenharmony_ci        return HNP_ERRNO_PARAM_INVALID;
9169570cc8Sopenharmony_ci    }
9269570cc8Sopenharmony_ci
9369570cc8Sopenharmony_ci    HNP_LOGI("native manager process start.");
9469570cc8Sopenharmony_ci
9569570cc8Sopenharmony_ci    /* 检验用户命令,获取对应的处理函数 */
9669570cc8Sopenharmony_ci    cmdInfo = HnpCmdCheck(argv[HNP_INDEX_1]);
9769570cc8Sopenharmony_ci    if (cmdInfo == NULL) {
9869570cc8Sopenharmony_ci        HNP_LOGE("invalid cmd!. cmd:%{public}s\r\n", argv[HNP_INDEX_1]);
9969570cc8Sopenharmony_ci        return HNP_ERRNO_OPERATOR_TYPE_INVALID;
10069570cc8Sopenharmony_ci    }
10169570cc8Sopenharmony_ci
10269570cc8Sopenharmony_ci    /* 执行命令 */
10369570cc8Sopenharmony_ci    ret = cmdInfo->process(argc, argv);
10469570cc8Sopenharmony_ci    if (ret == HNP_ERRNO_OPERATOR_ARGV_MISS) {
10569570cc8Sopenharmony_ci        HnpShowHelp(argc, argv);
10669570cc8Sopenharmony_ci    }
10769570cc8Sopenharmony_ci
10869570cc8Sopenharmony_ci    /* 返回值依赖此条log打印,切勿随意修改 */
10969570cc8Sopenharmony_ci    HNP_LOGI("native manager process exit. ret=%{public}d \r\n", ret);
11069570cc8Sopenharmony_ci    return ret;
11169570cc8Sopenharmony_ci}
11269570cc8Sopenharmony_ci
11369570cc8Sopenharmony_ci#ifdef __cplusplus
11469570cc8Sopenharmony_ci}
11569570cc8Sopenharmony_ci#endif