1 /*
2 * Copyright (c) 2022-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 "softbus_adapter_hisysevent.h"
16
17 #include <securec.h>
18 #include <sstream>
19
20 #include "comm_log.h"
21 #include "hisysevent_c.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_adapter_thread.h"
24 #include "softbus_error_code.h"
25
26 static const char *g_domain = "DSOFTBUS";
27 static bool g_init_lock = false;
28 static SoftBusMutex g_dfx_lock;
29 static HiSysEventParam g_dstParam[SOFTBUS_EVT_PARAM_BUTT];
30
ConvertToHisEventString(SoftBusEvtParam * srcParam,HiSysEventParam * dstParam)31 static int32_t ConvertToHisEventString(SoftBusEvtParam *srcParam, HiSysEventParam *dstParam)
32 {
33 dstParam->t = HISYSEVENT_STRING;
34 dstParam->v.s = reinterpret_cast<char *>(SoftBusCalloc(sizeof(char) * SOFTBUS_HISYSEVT_PARAM_LEN));
35 if (dstParam->v.s == nullptr) {
36 COMM_LOGE(COMM_ADAPTER, "ConvertEventParam: SoftBusMalloc fail");
37 return SOFTBUS_MALLOC_ERR;
38 }
39 if (strcpy_s(dstParam->v.s, SOFTBUS_HISYSEVT_PARAM_LEN, srcParam->paramValue.str) != EOK) {
40 SoftBusFree(dstParam->v.s);
41 COMM_LOGE(COMM_ADAPTER, "ConvertEventParam:copy string var fail");
42 return SOFTBUS_STRCPY_ERR;
43 }
44 return SOFTBUS_OK;
45 }
46
ConvertToHisEventUint32Array(SoftBusEvtParam * srcParam,HiSysEventParam * dstParam,uint32_t arraySize)47 static int32_t ConvertToHisEventUint32Array(SoftBusEvtParam *srcParam, HiSysEventParam *dstParam, uint32_t arraySize)
48 {
49 dstParam->t = HISYSEVENT_UINT32_ARRAY;
50 dstParam->v.array = reinterpret_cast<uint32_t *>(SoftBusCalloc(arraySize));
51 if (dstParam->v.array == nullptr) {
52 COMM_LOGE(COMM_ADAPTER, "ConvertEventParam: SoftBusMalloc fail");
53 return SOFTBUS_MALLOC_ERR;
54 }
55 if (memcpy_s(dstParam->v.array, arraySize, srcParam->paramValue.u32a, arraySize) != EOK) {
56 SoftBusFree(dstParam->v.array);
57 COMM_LOGE(COMM_ADAPTER, "ConvertEventParam:copy uint32 array var fail");
58 return SOFTBUS_MEM_ERR;
59 }
60 return SOFTBUS_OK;
61 }
62
ConvertEventParam(SoftBusEvtParam * srcParam,HiSysEventParam * dstParam)63 static int32_t ConvertEventParam(SoftBusEvtParam *srcParam, HiSysEventParam *dstParam)
64 {
65 uint32_t arraySize = sizeof(uint32_t) * SOFTBUS_HISYSEVT_PARAM_UINT32_ARRAY_SIZE;
66 switch (srcParam->paramType) {
67 case SOFTBUS_EVT_PARAMTYPE_BOOL:
68 dstParam->t = HISYSEVENT_BOOL;
69 dstParam->v.b = srcParam->paramValue.b;
70 break;
71 case SOFTBUS_EVT_PARAMTYPE_UINT8:
72 dstParam->t = HISYSEVENT_UINT8;
73 dstParam->v.ui8 = srcParam->paramValue.u8v;
74 break;
75 case SOFTBUS_EVT_PARAMTYPE_UINT16:
76 dstParam->t = HISYSEVENT_UINT16;
77 dstParam->v.ui16 = srcParam->paramValue.u16v;
78 break;
79 case SOFTBUS_EVT_PARAMTYPE_INT32:
80 dstParam->t = HISYSEVENT_INT32;
81 dstParam->v.i32 = srcParam->paramValue.i32v;
82 break;
83 case SOFTBUS_EVT_PARAMTYPE_UINT32:
84 dstParam->t = HISYSEVENT_UINT32;
85 dstParam->v.ui32 = srcParam->paramValue.u32v;
86 break;
87 case SOFTBUS_EVT_PARAMTYPE_INT64:
88 dstParam->t = HISYSEVENT_INT64;
89 dstParam->v.i64 = srcParam->paramValue.i64v;
90 break;
91 case SOFTBUS_EVT_PARAMTYPE_UINT64:
92 dstParam->t = HISYSEVENT_UINT64;
93 dstParam->v.ui64 = srcParam->paramValue.u64v;
94 break;
95 case SOFTBUS_EVT_PARAMTYPE_FLOAT:
96 dstParam->t = HISYSEVENT_FLOAT;
97 dstParam->v.f = srcParam->paramValue.f;
98 break;
99 case SOFTBUS_EVT_PARAMTYPE_DOUBLE:
100 dstParam->t = HISYSEVENT_DOUBLE;
101 dstParam->v.d = srcParam->paramValue.d;
102 break;
103 case SOFTBUS_EVT_PARAMTYPE_STRING:
104 return ConvertToHisEventString(srcParam, dstParam);
105 case SOFTBUS_EVT_PARAMTYPE_UINT32_ARRAY:
106 return ConvertToHisEventUint32Array(srcParam, dstParam, arraySize);
107 default:
108 break;
109 }
110 return SOFTBUS_OK;
111 }
112
ConvertMsgToHiSysEvent(SoftBusEvtReportMsg * msg)113 static int32_t ConvertMsgToHiSysEvent(SoftBusEvtReportMsg *msg)
114 {
115 if (memset_s(g_dstParam, sizeof(HiSysEventParam) * SOFTBUS_EVT_PARAM_BUTT, 0,
116 sizeof(HiSysEventParam) * SOFTBUS_EVT_PARAM_BUTT) != EOK) {
117 COMM_LOGE(COMM_ADAPTER, "init g_dstParam fail");
118 return SOFTBUS_ERR;
119 }
120 for (uint32_t i = 0; i < msg->paramNum; i++) {
121 if (strcpy_s(g_dstParam[i].name, MAX_LENGTH_OF_PARAM_NAME, msg->paramArray[i].paramName) != EOK) {
122 COMM_LOGE(COMM_ADAPTER, "copy param fail");
123 return SOFTBUS_ERR;
124 }
125 if (ConvertEventParam(&msg->paramArray[i], &g_dstParam[i]) != SOFTBUS_OK) {
126 COMM_LOGE(COMM_ADAPTER, "ConvertMsgToHiSysEvent:convert param fail");
127 return SOFTBUS_ERR;
128 }
129 }
130 return SOFTBUS_OK;
131 }
132
HiSysEventParamDeInit(uint32_t size)133 static void HiSysEventParamDeInit(uint32_t size)
134 {
135 for (uint32_t i = 0; i < size; i++) {
136 if (g_dstParam[i].t == HISYSEVENT_STRING && g_dstParam[i].v.s != nullptr) {
137 SoftBusFree(g_dstParam[i].v.s);
138 g_dstParam[i].v.s = nullptr;
139 }
140 }
141 }
142
ConvertMsgType(SoftBusEvtType type)143 static HiSysEventEventType ConvertMsgType(SoftBusEvtType type)
144 {
145 HiSysEventEventType hiSysEvtType;
146 switch (type) {
147 case SOFTBUS_EVT_TYPE_FAULT:
148 hiSysEvtType = HISYSEVENT_FAULT;
149 break;
150 case SOFTBUS_EVT_TYPE_STATISTIC:
151 hiSysEvtType = HISYSEVENT_STATISTIC;
152 break;
153 case SOFTBUS_EVT_TYPE_SECURITY:
154 hiSysEvtType = HISYSEVENT_SECURITY;
155 break;
156 case SOFTBUS_EVT_TYPE_BEHAVIOR:
157 hiSysEvtType = HISYSEVENT_BEHAVIOR;
158 break;
159 default:
160 hiSysEvtType = HISYSEVENT_STATISTIC;
161 break;
162 }
163 return hiSysEvtType;
164 }
165
InitHisEvtMutexLock()166 static void InitHisEvtMutexLock()
167 {
168 if (SoftBusMutexInit(&g_dfx_lock, nullptr) != SOFTBUS_OK) {
169 COMM_LOGE(COMM_ADAPTER, "init HisEvtMutexLock fail");
170 return;
171 }
172 }
173
174 #ifdef __cplusplus
175 #if __cplusplus
176 extern "C" {
177 #endif
178 #endif
179
SoftbusWriteHisEvt(SoftBusEvtReportMsg * reportMsg)180 int32_t SoftbusWriteHisEvt(SoftBusEvtReportMsg *reportMsg)
181 {
182 if (reportMsg == nullptr) {
183 return SOFTBUS_ERR;
184 }
185 if (!g_init_lock) {
186 InitHisEvtMutexLock();
187 g_init_lock = true;
188 }
189 if (SoftBusMutexLock(&g_dfx_lock) != 0) {
190 COMM_LOGE(COMM_ADAPTER, "lock failed");
191 return SOFTBUS_LOCK_ERR;
192 }
193 ConvertMsgToHiSysEvent(reportMsg);
194 OH_HiSysEvent_Write(
195 g_domain, reportMsg->evtName, ConvertMsgType(reportMsg->evtType), g_dstParam, reportMsg->paramNum);
196 HiSysEventParamDeInit(reportMsg->paramNum);
197 (void)SoftBusMutexUnlock(&g_dfx_lock);
198 return SOFTBUS_OK;
199 }
200
SoftbusFreeEvtReportMsg(SoftBusEvtReportMsg * msg)201 void SoftbusFreeEvtReportMsg(SoftBusEvtReportMsg *msg)
202 {
203 if (msg == nullptr) {
204 return;
205 }
206 if (msg->paramArray != nullptr) {
207 SoftBusFree(msg->paramArray);
208 msg->paramArray = nullptr;
209 }
210 SoftBusFree(msg);
211 }
212
SoftbusCreateEvtReportMsg(int32_t paramNum)213 SoftBusEvtReportMsg *SoftbusCreateEvtReportMsg(int32_t paramNum)
214 {
215 if (paramNum <= SOFTBUS_EVT_PARAM_ZERO || paramNum >= SOFTBUS_EVT_PARAM_BUTT) {
216 COMM_LOGE(COMM_ADAPTER, "param is invalid");
217 return nullptr;
218 }
219 SoftBusEvtReportMsg *msg = reinterpret_cast<SoftBusEvtReportMsg *>(SoftBusCalloc(sizeof(SoftBusEvtReportMsg)));
220 if (msg == nullptr) {
221 COMM_LOGE(COMM_ADAPTER, "report msg is null");
222 return nullptr;
223 }
224 msg->paramArray = reinterpret_cast<SoftBusEvtParam *>(SoftBusCalloc(sizeof(SoftBusEvtParam) * paramNum));
225 if (msg->paramArray == nullptr) {
226 SoftbusFreeEvtReportMsg(msg);
227 return nullptr;
228 }
229 return msg;
230 }
231
232 #ifdef __cplusplus
233 #if __cplusplus
234 }
235 #endif /* __cplusplus */
236 #endif /* __cplusplus */