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 "softbus_event.h"
17 
18 #include "comm_log.h"
19 #include "convert/conn_audit_converter.h"
20 #include "convert/conn_event_converter.h"
21 #include "convert/disc_audit_converter.h"
22 #include "convert/disc_event_converter.h"
23 #include "convert/lnn_audit_converter.h"
24 #include "convert/lnn_event_converter.h"
25 #include "convert/trans_audit_converter.h"
26 #include "convert/trans_event_converter.h"
27 #include "convert/stats_event_converter.h"
28 #include "hisysevent_c.h"
29 
30 #define HISYSEVENT_WRITE_SUCCESS 0
31 
32 typedef void (*WriteEventAndFree)(HiSysEventParam *params, size_t size, SoftbusEventForm *form);
33 
ConstructHiSysEventParams(HiSysEventParam * eventParams,const HiSysEventParam * params,size_t size,const HiSysEventParam * extraParams,size_t extraSize)34 static void ConstructHiSysEventParams(HiSysEventParam *eventParams, const HiSysEventParam *params, size_t size,
35     const HiSysEventParam *extraParams, size_t extraSize)
36 {
37     size_t index = 0;
38     for (size_t i = 0; i < size; ++i) {
39         eventParams[index++] = params[i];
40     }
41     for (size_t j = 0; j < extraSize; ++j) {
42         eventParams[index++] = extraParams[j];
43     }
44 }
45 
WriteHiSysEvent(HiSysEventParam params[],size_t size,HiSysEventParam extraParams[],size_t extraSize,SoftbusEventForm * form)46 static void WriteHiSysEvent(
47     HiSysEventParam params[], size_t size, HiSysEventParam extraParams[], size_t extraSize, SoftbusEventForm *form)
48 {
49     size_t validParamSize = size + extraSize;
50     HiSysEventParam eventParams[validParamSize];
51     ConstructHiSysEventParams(eventParams, params, size, extraParams, extraSize);
52     int32_t ret = HiSysEvent_Write(
53         form->func, form->line, form->domain, form->eventName, form->eventType, eventParams, validParamSize);
54     if (ret != HISYSEVENT_WRITE_SUCCESS) {
55         COMM_LOGD(COMM_DFX, "write to hisysevent failed, ret=%{public}d", ret);
56     }
57 }
58 
HiSysEventParamsFree(HiSysEventParam params[],size_t size)59 static void HiSysEventParamsFree(HiSysEventParam params[], size_t size)
60 {
61     for (size_t i = 0; i < size; ++i) {
62         if (params[i].t == HISYSEVENT_STRING) {
63             free(params[i].v.s);
64         }
65     }
66 }
67 
WriteConnEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)68 static void WriteConnEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
69 {
70     HiSysEventParam connParams[CONN_ASSIGNER_SIZE] = { 0 };
71     size_t connSize = ConvertConnForm2Param(connParams, CONN_ASSIGNER_SIZE, form);
72     WriteHiSysEvent(params, size, connParams, connSize, form);
73     HiSysEventParamsFree(connParams, connSize);
74 }
75 
WriteDiscEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)76 static void WriteDiscEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
77 {
78     HiSysEventParam discParams[DISC_ASSIGNER_SIZE] = { 0 };
79     size_t discSize = ConvertDiscForm2Param(discParams, DISC_ASSIGNER_SIZE, form);
80     WriteHiSysEvent(params, size, discParams, discSize, form);
81     HiSysEventParamsFree(discParams, discSize);
82 }
83 
WriteLnnEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)84 static void WriteLnnEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
85 {
86     HiSysEventParam lnnParams[LNN_ASSIGNER_SIZE] = { 0 };
87     size_t lnnSize = ConvertLnnForm2Param(lnnParams, LNN_ASSIGNER_SIZE, form);
88     WriteHiSysEvent(params, size, lnnParams, lnnSize, form);
89     HiSysEventParamsFree(lnnParams, lnnSize);
90 }
91 
WriteTransEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)92 static void WriteTransEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
93 {
94     HiSysEventParam transParams[TRANS_ASSIGNER_SIZE] = { 0 };
95     size_t transSize = ConvertTransForm2Param(transParams, TRANS_ASSIGNER_SIZE, form);
96     WriteHiSysEvent(params, size, transParams, transSize, form);
97     HiSysEventParamsFree(transParams, transSize);
98 }
99 
WriteTransAlarmEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)100 static void WriteTransAlarmEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
101 {
102     HiSysEventParam alarmParams[TRANS_ALARM_ASSIGNER_SIZE] = { 0 };
103     size_t alarmSize = ConvertTransAlarmForm2Param(alarmParams, TRANS_ALARM_ASSIGNER_SIZE, form);
104     WriteHiSysEvent(params, size, alarmParams, alarmSize, form);
105     HiSysEventParamsFree(alarmParams, alarmSize);
106 }
107 
WriteConnAlarmEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)108 static void WriteConnAlarmEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
109 {
110     HiSysEventParam alarmParams[CONN_ALARM_ASSIGNER_SIZE] = { 0 };
111     size_t alarmSize = ConvertConnAlarmForm2Param(alarmParams, CONN_ALARM_ASSIGNER_SIZE, form);
112     WriteHiSysEvent(params, size, alarmParams, alarmSize, form);
113     HiSysEventParamsFree(alarmParams, alarmSize);
114 }
115 
WriteLnnAlarmEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)116 static void WriteLnnAlarmEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
117 {
118     HiSysEventParam alarmParams[LNN_ALARM_ASSIGNER_SIZE] = { 0 };
119     size_t alarmSize = ConvertLnnAlarmForm2Param(alarmParams, LNN_ALARM_ASSIGNER_SIZE, form);
120     WriteHiSysEvent(params, size, alarmParams, alarmSize, form);
121     HiSysEventParamsFree(alarmParams, alarmSize);
122 }
123 
WriteDiscAlarmEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)124 static void WriteDiscAlarmEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
125 {
126     HiSysEventParam alarmParams[DISC_ALARM_ASSIGNER_SIZE] = { 0 };
127     size_t alarmSize = ConvertDiscAlarmForm2Param(alarmParams, DISC_ALARM_ASSIGNER_SIZE, form);
128     WriteHiSysEvent(params, size, alarmParams, alarmSize, form);
129     HiSysEventParamsFree(alarmParams, alarmSize);
130 }
131 
WriteStateEventAndFree(HiSysEventParam * params,size_t size,SoftbusEventForm * form)132 static void WriteStateEventAndFree(HiSysEventParam *params, size_t size, SoftbusEventForm *form)
133 {
134     HiSysEventParam statsParams[STATS_ASSIGNER_SIZE] = { 0 };
135     size_t statsSize = ConvertStatsForm2Param(statsParams, STATS_ASSIGNER_SIZE, form);
136     WriteHiSysEvent(params, size, statsParams, statsSize, form);
137     HiSysEventParamsFree(statsParams, statsSize);
138 }
139 
140 static WriteEventAndFree g_eventFunc[] = {
141     [EVENT_MODULE_CONN] = WriteConnEventAndFree,
142     [EVENT_MODULE_DISC] = WriteDiscEventAndFree,
143     [EVENT_MODULE_LNN] = WriteLnnEventAndFree,
144     [EVENT_MODULE_TRANS] = WriteTransEventAndFree,
145     [EVENT_MODULE_TRANS_ALARM] = WriteTransAlarmEventAndFree,
146     [EVENT_MODULE_CONN_ALARM] = WriteConnAlarmEventAndFree,
147     [EVENT_MODULE_LNN_ALARM] = WriteLnnAlarmEventAndFree,
148     [EVENT_MODULE_DISC_ALARM] = WriteDiscAlarmEventAndFree,
149     [EVENT_MODULE_STATS] = WriteStateEventAndFree
150 };
151 
WriteSoftbusEvent(SoftbusEventModule module,SoftbusEventForm * form)152 static void WriteSoftbusEvent(SoftbusEventModule module, SoftbusEventForm *form)
153 {
154     HiSysEventParam params[SOFTBUS_ASSIGNER_SIZE] = { 0 };
155     size_t size = ConvertSoftbusForm2Param(params, SOFTBUS_ASSIGNER_SIZE, form);
156     if (module >= 0 && module < EVENT_MODULE_MAX) {
157         g_eventFunc[module](params, size, form);
158     } else {
159         COMM_LOGW(COMM_DFX, "invalid module. module=%{public}d", (int32_t)module);
160     }
161     HiSysEventParamsFree(params, size);
162 }
163 
SoftbusEventInner(SoftbusEventModule module,SoftbusEventForm * form)164 void SoftbusEventInner(SoftbusEventModule module, SoftbusEventForm *form)
165 {
166     if (form == NULL) {
167         return;
168     }
169     form->domain = SOFTBUS_EVENT_DOMAIN;
170     form->eventType = SOFTBUS_EVENT_TYPE_BEHAVIOR;
171     form->orgPkg = SOFTBUS_EVENT_PKG_NAME;
172     WriteSoftbusEvent(module, form);
173 }
174 
WriteSoftbusAudit(SoftbusEventModule module,SoftbusEventForm * form)175 static void WriteSoftbusAudit(SoftbusEventModule module, SoftbusEventForm *form)
176 {
177     HiSysEventParam params[SOFTBUS_ASSIGNER_SIZE] = { 0 };
178     size_t size = ConvertSoftbusForm2Param(params, SOFTBUS_ASSIGNER_SIZE, form);
179     switch (module) {
180         case EVENT_MODULE_CONN: {
181             HiSysEventParam connParams[CONN_AUDIT_ASSIGNER_SIZE] = { 0 };
182             size_t connSize = ConvertConnAuditForm2Param(connParams, form);
183             WriteHiSysEvent(params, size, connParams, connSize, form);
184             HiSysEventParamsFree(connParams, connSize);
185             break;
186         }
187         case EVENT_MODULE_DISC: {
188             HiSysEventParam discParams[DISC_AUDIT_ASSIGNER_SIZE] = { 0 };
189             size_t discSize = ConvertDiscAuditForm2Param(discParams, form);
190             WriteHiSysEvent(params, size, discParams, discSize, form);
191             HiSysEventParamsFree(discParams, discSize);
192             break;
193         }
194         case EVENT_MODULE_LNN: {
195             HiSysEventParam lnnParams[LNN_AUDIT_ASSIGNER_SIZE] = { 0 };
196             size_t lnnSize = ConvertLnnAuditForm2Param(lnnParams, form);
197             WriteHiSysEvent(params, size, lnnParams, lnnSize, form);
198             HiSysEventParamsFree(lnnParams, lnnSize);
199             break;
200         }
201         case EVENT_MODULE_TRANS: {
202             HiSysEventParam transParams[TRANS_AUDIT_ASSIGNER_SIZE] = { 0 };
203             size_t transSize = ConvertTransAuditForm2Param(transParams, form);
204             WriteHiSysEvent(params, size, transParams, transSize, form);
205             HiSysEventParamsFree(transParams, transSize);
206             break;
207         }
208         default: {
209             COMM_LOGW(COMM_DFX, "invalid module. module=%{public}d", (int32_t)module);
210             break;
211         }
212     }
213     HiSysEventParamsFree(params, size);
214 }
215 
SoftbusAuditInner(SoftbusEventModule module,SoftbusEventForm * form)216 void SoftbusAuditInner(SoftbusEventModule module, SoftbusEventForm *form)
217 {
218     if (form == NULL) {
219         return;
220     }
221     form->domain = SOFTBUS_EVENT_DOMAIN;
222     form->eventType = SOFTBUS_EVENT_TYPE_SECURITY;
223     form->orgPkg = SOFTBUS_EVENT_PKG_NAME;
224     WriteSoftbusAudit(module, form);
225 }
226