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 <stdio.h>
16 #include <string.h>
17 #include "comm_log.h"
18 #include "softbus_errcode.h"
19 #include "softbus_adapter_mem.h"
20 #include "softbus_def.h"
21 #include "softbus_hidumper.h"
22 #include "softbus_hidumper_conn.h"
23 
24 #define SOFTBUS_CONN_MODULE_NAME "conn"
25 #define SOFTBUS_CONN_MODULE_HELP "List all the dump item of conn"
26 
27 static LIST_HEAD(g_conn_var_list);
28 
SoftBusRegConnVarDump(const char * dumpVar,SoftBusVarDumpCb cb)29 int32_t SoftBusRegConnVarDump(const char *dumpVar, SoftBusVarDumpCb cb)
30 {
31     if (dumpVar == NULL || strlen(dumpVar) >= SOFTBUS_DUMP_VAR_NAME_LEN || cb == NULL) {
32         COMM_LOGE(COMM_DFX, "SoftBusRegConnVarDump invalid param");
33         return SOFTBUS_INVALID_PARAM;
34     }
35     return SoftBusAddDumpVarToList(dumpVar, cb, &g_conn_var_list);
36 }
37 
SoftBusConnDumpHander(int fd,int32_t argc,const char ** argv)38 static int32_t SoftBusConnDumpHander(int fd, int32_t argc, const char **argv)
39 {
40     if (fd < 0 || argc < 0 || argv == NULL) {
41         return SOFTBUS_INVALID_PARAM;
42     }
43 
44     if (argc == 0 || ((argc == 1) && (strcmp(argv[0], "-h") == 0))) {
45         SoftBusDumpSubModuleHelp(fd, SOFTBUS_CONN_MODULE_NAME, &g_conn_var_list);
46         return SOFTBUS_OK;
47     }
48 
49     if (argc == 1 && strcmp(argv[0], "-l") == 0) {
50         SoftBusDumpSubModuleHelp(fd, SOFTBUS_CONN_MODULE_NAME, &g_conn_var_list);
51         return SOFTBUS_OK;
52     }
53     int32_t ret = SOFTBUS_OK;
54     int32_t isModuleExist = SOFTBUS_DUMP_NOT_EXIST;
55     if (strcmp(argv[0], "-l") == 0) {
56         ListNode *item = NULL;
57         LIST_FOR_EACH(item, &g_conn_var_list) {
58             SoftBusDumpVarNode *itemNode = LIST_ENTRY(item, SoftBusDumpVarNode, node);
59             if (strcmp(itemNode->varName, argv[1]) == 0) {
60                 ret = itemNode->dumpCallback(fd);
61                 isModuleExist = SOFTBUS_DUMP_EXIST;
62                 break;
63             }
64         }
65         if (isModuleExist == SOFTBUS_DUMP_NOT_EXIST) {
66             SoftBusDumpErrInfo(fd, argv[1]);
67             SoftBusDumpSubModuleHelp(fd, SOFTBUS_CONN_MODULE_NAME, &g_conn_var_list);
68         }
69     }
70     return ret;
71 }
72 
SoftBusConnHiDumperInit(void)73 int32_t SoftBusConnHiDumperInit(void)
74 {
75     int32_t ret = SoftBusRegHiDumperHandler(SOFTBUS_CONN_MODULE_NAME, SOFTBUS_CONN_MODULE_HELP,
76         &SoftBusConnDumpHander);
77     if (ret != SOFTBUS_OK) {
78         COMM_LOGE(COMM_INIT, "SoftBusConnDumpHander regist fail");
79     }
80     return ret;
81 }
82 
SoftBusHiDumperConnDeInit(void)83 void SoftBusHiDumperConnDeInit(void)
84 {
85     SoftBusReleaseDumpVar(&g_conn_var_list);
86 }