1 /*
2  * Copyright (c) 2024 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 
16 #include <string.h>
17 #include <unistd.h>
18 
19 #include "hnp_base.h"
20 #include "hnp_installer.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 extern int HnpShowHelp(int argc, char *argv[]);
27 
28 typedef int (*HNP_CMD_PROCESS_FUNC)(int argc, char *argv[]);
29 
30 typedef struct NativeManagerCmdInfoStru {
31     char *cmd;
32     HNP_CMD_PROCESS_FUNC process;
33 } NativeManagerCmdInfo;
34 
35 NativeManagerCmdInfo g_nativeManagerCmd[] = {
36     {"help", HnpShowHelp},
37     {"-h", HnpShowHelp},
38     {"install", HnpCmdInstall},
39     {"uninstall", HnpCmdUnInstall}
40 };
41 
HnpShowHelp(int argc,char * argv[])42 int HnpShowHelp(int argc, char *argv[])
43 {
44     (void)argc;
45     (void)argv;
46 
47     HNP_LOGI("\r\nusage:hnp <command> <args> [-u <user id>][-p <hap package name>][-i <hap install path>][-f]"
48             "[-s <hap source path>][-a <system abi>]\r\n"
49         "\r\nThese are common hnp commands used in various situations:\r\n"
50         "\r\ninstall: install one hap package"
51         "\r\n           hnp install <-u [user id]> <-p [hap package name]> <-i [hap install path]> <-f>"
52         "\r\n           -u    : [required]    user id"
53         "\r\n           -p    : [required]    hap package name"
54         "\r\n           -i    : [required]    hap install path"
55         "\r\n           -s    : [required]    hap source path"
56         "\r\n           -a    : [required]    system abi"
57         "\r\n           -f    : [optional]    if provided, the hnp package will be installed forcely, ignoring old"
58             " versions of the hnp package\r\n"
59         "\r\nuninstall: uninstall one hap package"
60         "\r\n           hnp uninstall <-u [user id]> <-p [hap package name]>"
61         "\r\n           -u    : [required]    user id"
62         "\r\n           -p    : [required]    hap package name"
63         "\r\nfor example:\r\n"
64         "\r\n    hnp install -u 1000 -p app_sample -i /data/app_sample/ -s /data/app_hap/demo.hap -a arm64 -f"
65         "\r\n    hnp uninstall -u 1000 -p app_sample\r\n");
66 
67     return 0;
68 }
69 
HnpCmdCheck(const char * cmd)70 static NativeManagerCmdInfo* HnpCmdCheck(const char *cmd)
71 {
72     int i;
73     int cmdNum = sizeof(g_nativeManagerCmd) / sizeof(NativeManagerCmdInfo);
74 
75     for (i = 0; i < cmdNum; i++) {
76         if (!strcmp(cmd, g_nativeManagerCmd[i].cmd)) {
77             return &g_nativeManagerCmd[i];
78         }
79     }
80     return NULL;
81 }
82 
main(int argc,char * argv[])83 int main(int argc, char *argv[])
84 {
85     int ret;
86     NativeManagerCmdInfo *cmdInfo = NULL;
87 
88     if (argc < HNP_INDEX_2) {
89         HnpShowHelp(argc, argv);
90         return HNP_ERRNO_PARAM_INVALID;
91     }
92 
93     HNP_LOGI("native manager process start.");
94 
95     /* 检验用户命令,获取对应的处理函数 */
96     cmdInfo = HnpCmdCheck(argv[HNP_INDEX_1]);
97     if (cmdInfo == NULL) {
98         HNP_LOGE("invalid cmd!. cmd:%{public}s\r\n", argv[HNP_INDEX_1]);
99         return HNP_ERRNO_OPERATOR_TYPE_INVALID;
100     }
101 
102     /* 执行命令 */
103     ret = cmdInfo->process(argc, argv);
104     if (ret == HNP_ERRNO_OPERATOR_ARGV_MISS) {
105         HnpShowHelp(argc, argv);
106     }
107 
108     /* 返回值依赖此条log打印,切勿随意修改 */
109     HNP_LOGI("native manager process exit. ret=%{public}d \r\n", ret);
110     return ret;
111 }
112 
113 #ifdef __cplusplus
114 }
115 #endif