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