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