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