• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "devhost_dump.h"
17 #include "devhost_dump_reg.h"
18 #include "hdf_base.h"
19 #include "hdf_cstring.h"
20 #include "hdf_dlist.h"
21 #include "hdf_log.h"
22 #include "osal_mem.h"
23 #include "osal_mutex.h"
24 
25 #define HDF_LOG_TAG devhost_dump
26 
27 struct DumpServiceNode {
28     DevHostDumpFunc dumpService;
29     char *servName;
30     struct DListHead node;
31 };
32 
33 struct DumpHostNode {
34     struct DListHead list;
35     struct OsalMutex mutex;
36     DevHostDumpFunc dumpHost;
37 };
38 
39 static struct DumpHostNode g_dumpHostNode;
40 
DevHostDumpInit(void)41 void DevHostDumpInit(void)
42 {
43     OsalMutexInit(&g_dumpHostNode.mutex);
44     DListHeadInit(&g_dumpHostNode.list);
45     g_dumpHostNode.dumpHost = NULL;
46 }
47 
DevHostDumpDeInit(void)48 void DevHostDumpDeInit(void)
49 {
50     struct DumpServiceNode *pos = NULL;
51     struct DumpServiceNode *tmp = NULL;
52 
53     OsalMutexLock(&g_dumpHostNode.mutex);
54     DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_dumpHostNode.list, struct DumpServiceNode, node) {
55         DListRemove(&pos->node);
56         OsalMemFree(pos->servName);
57         OsalMemFree(pos);
58     }
59     OsalMutexUnlock(&g_dumpHostNode.mutex);
60 
61     OsalMutexDestroy(&g_dumpHostNode.mutex);
62     DListHeadInit(&g_dumpHostNode.list);
63     g_dumpHostNode.dumpHost = NULL;
64 }
65 
DevHostCheckDumpExist(const char * servName)66 static bool DevHostCheckDumpExist(const char *servName)
67 {
68     struct DumpServiceNode *pos = NULL;
69     bool findFlag = false;
70     OsalMutexLock(&g_dumpHostNode.mutex);
71     DLIST_FOR_EACH_ENTRY(pos, &g_dumpHostNode.list, struct DumpServiceNode, node) {
72         if (strcmp(pos->servName, servName) == 0) {
73             findFlag = true;
74             break;
75         }
76     }
77     OsalMutexUnlock(&g_dumpHostNode.mutex);
78     return findFlag;
79 }
80 
DevHostRegisterDumpService(const char * servName,DevHostDumpFunc dump)81 int32_t DevHostRegisterDumpService(const char *servName, DevHostDumpFunc dump)
82 {
83     if (dump == NULL || servName == NULL) {
84         return HDF_FAILURE;
85     }
86 
87     if (DevHostCheckDumpExist(servName)) {
88         HDF_LOGE("%{public}s service %{public}s dump function exist", __func__, servName);
89         return HDF_FAILURE;
90     }
91 
92     struct DumpServiceNode *node = (struct DumpServiceNode *)OsalMemCalloc(sizeof(*node));
93     if (node == NULL) {
94         return HDF_FAILURE;
95     }
96 
97     HDF_LOGI("%{public}s enter", __func__);
98     node->dumpService = dump;
99     node->servName = HdfStringCopy(servName);
100     if (node->servName == NULL) {
101         OsalMemFree(node);
102         return HDF_FAILURE;
103     }
104     OsalMutexLock(&g_dumpHostNode.mutex);
105     DListInsertTail(&node->node, &g_dumpHostNode.list);
106     OsalMutexUnlock(&g_dumpHostNode.mutex);
107 
108     return HDF_SUCCESS;
109 }
110 
DevHostRegisterDumpHost(DevHostDumpFunc dump)111 int32_t DevHostRegisterDumpHost(DevHostDumpFunc dump)
112 {
113     if (dump == NULL) {
114         return HDF_FAILURE;
115     }
116 
117     g_dumpHostNode.dumpHost = dump;
118 
119     return HDF_SUCCESS;
120 }
121 
DevHostDump(struct HdfSBuf * data,struct HdfSBuf * reply)122 void DevHostDump(struct HdfSBuf *data, struct HdfSBuf *reply)
123 {
124     HDF_LOGI("%{public}s enter", __func__);
125     if (data == NULL || reply == NULL) {
126         return;
127     }
128 
129     const char *option = HdfSbufReadString(data);
130     if (option == NULL) {
131         return;
132     }
133 
134     if (strcmp(option, "dumpHost") == 0) {
135         if (g_dumpHostNode.dumpHost == NULL) {
136             HDF_LOGE("%{public}s no dumpHost function", __func__);
137             (void)HdfSbufWriteString(reply, "The host does not register dump function\n");
138             return;
139         }
140         g_dumpHostNode.dumpHost(data, reply);
141     } else if (strcmp(option, "dumpService") == 0) {
142         const char *servName = HdfSbufReadString(data);
143         struct DumpServiceNode *pos = NULL;
144         bool dumpFlag = false;
145         OsalMutexLock(&g_dumpHostNode.mutex);
146         DLIST_FOR_EACH_ENTRY(pos, &g_dumpHostNode.list, struct DumpServiceNode, node) {
147             if (strcmp(pos->servName, servName) != 0) {
148                 continue;
149             }
150             if (pos->dumpService) {
151                 pos->dumpService(data, reply);
152                 dumpFlag = true;
153                 break;
154             }
155         }
156         OsalMutexUnlock(&g_dumpHostNode.mutex);
157         if (!dumpFlag) {
158             (void)HdfSbufWriteString(reply, "The service does not register dump function\n");
159         }
160     } else {
161         HDF_LOGE("%{public}s invalid parameter %{public}s", __func__, option);
162     }
163 
164     return;
165 }
166