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 #ifndef TRANS_AUDIT_CONVERTER_H
17 #define TRANS_AUDIT_CONVERTER_H
18 
19 #include "softbus_event_converter.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define TRANS_AUDIT_ASSIGNER(type, fieldName, field)                                                          \
26     static inline bool TransAuditAssigner##fieldName(                                                         \
27         const char *eventName, HiSysEventParamType paramType, SoftbusEventForm *form, HiSysEventParam *param) \
28     {                                                                                                         \
29         if (Assigner##type(form->transAuditExtra->field, &param) &&                                           \
30             CopyString(param->name, eventName, MAX_LENGTH_OF_PARAM_NAME)) {                                   \
31             param->t = paramType;                                                                             \
32             return true;                                                                                      \
33         }                                                                                                     \
34         return false;                                                                                         \
35     }
36 
37 TRANS_AUDIT_ASSIGNER(String, HostPkg, hostPkg)
38 TRANS_AUDIT_ASSIGNER(Int32, Result, result)
39 TRANS_AUDIT_ASSIGNER(Errcode, Errcode, errcode)
40 TRANS_AUDIT_ASSIGNER(Int32, AuditType, auditType)
41 TRANS_AUDIT_ASSIGNER(String, LocalIp, localIp)
42 TRANS_AUDIT_ASSIGNER(String, LocalPort, localPort)
43 TRANS_AUDIT_ASSIGNER(String, LocalDevId, localDevId)
44 TRANS_AUDIT_ASSIGNER(Int32, LocalDevType, localDevType)
45 TRANS_AUDIT_ASSIGNER(String, LocalSessName, localSessName)
46 TRANS_AUDIT_ASSIGNER(Int32, LocalChannelId, localChannelId)
47 TRANS_AUDIT_ASSIGNER(String, PeerIp, peerIp)
48 TRANS_AUDIT_ASSIGNER(String, PeerPort, peerPort)
49 TRANS_AUDIT_ASSIGNER(String, PeerDevId, peerDevId)
50 TRANS_AUDIT_ASSIGNER(Int32, PeerDevType, peerDevType)
51 TRANS_AUDIT_ASSIGNER(String, PeerSessName, peerSessName)
52 TRANS_AUDIT_ASSIGNER(Int32, PeerChannelId, peerChannelId)
53 TRANS_AUDIT_ASSIGNER(Int32, ChannelType, channelType)
54 TRANS_AUDIT_ASSIGNER(Int32, AuthId, authId)
55 TRANS_AUDIT_ASSIGNER(Int32, ReqId, reqId)
56 TRANS_AUDIT_ASSIGNER(Int32, LinkType, linkType)
57 TRANS_AUDIT_ASSIGNER(Int32, ConnId, connId)
58 TRANS_AUDIT_ASSIGNER(Int32, SocketFd, socketFd)
59 TRANS_AUDIT_ASSIGNER(Int32, DataType, dataType)
60 TRANS_AUDIT_ASSIGNER(Int32, DataLen, dataLen)
61 TRANS_AUDIT_ASSIGNER(Int32, DataSeq, dataSeq)
62 TRANS_AUDIT_ASSIGNER(Int32, CostTime, costTime)
63 TRANS_AUDIT_ASSIGNER(Int32, DataTraffic, dataTraffic)
64 TRANS_AUDIT_ASSIGNER(Int32, ReqCount, reqCount)
65 
66 #define TRANS_AUDIT_ASSIGNER_SIZE 28 // Size of g_transAuditAssigners
67 static const HiSysEventParamAssigner g_transAuditAssigners[] = {
68     { "HOST_PKG",         HISYSEVENT_STRING, TransAuditAssignerHostPkg        },
69     { "RESULT",           HISYSEVENT_INT32,  TransAuditAssignerResult         },
70     { "ERROR_CODE",       HISYSEVENT_INT32,  TransAuditAssignerErrcode        },
71     { "AUDIT_TYPE",       HISYSEVENT_INT32,  TransAuditAssignerAuditType      },
72     { "LOCAL_IP",         HISYSEVENT_STRING, TransAuditAssignerLocalIp        },
73     { "LOCAL_PORT",       HISYSEVENT_STRING, TransAuditAssignerLocalPort      },
74     { "LOCAL_DEV_ID",     HISYSEVENT_STRING, TransAuditAssignerLocalDevId     },
75     { "LOCAL_DEV_TYPE",   HISYSEVENT_INT32,  TransAuditAssignerLocalDevType   },
76     { "LOCAL_SESS_NAME",  HISYSEVENT_STRING, TransAuditAssignerLocalSessName  },
77     { "LOCAL_CHANNEL_ID", HISYSEVENT_INT32,  TransAuditAssignerLocalChannelId },
78     { "PEER_IP",          HISYSEVENT_STRING, TransAuditAssignerPeerIp         },
79     { "PEER_PORT",        HISYSEVENT_STRING, TransAuditAssignerPeerPort       },
80     { "PEER_DEV_ID",      HISYSEVENT_STRING, TransAuditAssignerPeerDevId      },
81     { "PEER_DEV_TYPE",    HISYSEVENT_INT32,  TransAuditAssignerPeerDevType    },
82     { "PEER_SESS_NAME",   HISYSEVENT_STRING, TransAuditAssignerPeerSessName   },
83     { "PEER_CHANNEL_ID",  HISYSEVENT_INT32,  TransAuditAssignerPeerChannelId  },
84     { "CHANNEL_TYPE",     HISYSEVENT_INT32,  TransAuditAssignerChannelType    },
85     { "AUTH_ID",          HISYSEVENT_INT32,  TransAuditAssignerAuthId         },
86     { "REQ_ID",           HISYSEVENT_INT32,  TransAuditAssignerReqId          },
87     { "LINK_TYPE",        HISYSEVENT_INT32,  TransAuditAssignerLinkType       },
88     { "CONN_ID",          HISYSEVENT_INT32,  TransAuditAssignerConnId         },
89     { "SOCKET_FD",        HISYSEVENT_INT32,  TransAuditAssignerSocketFd       },
90     { "DATA_TYPE",        HISYSEVENT_INT32,  TransAuditAssignerDataType       },
91     { "DATA_LEN",         HISYSEVENT_INT32,  TransAuditAssignerDataLen        },
92     { "DATA_SEQ",         HISYSEVENT_INT32,  TransAuditAssignerDataSeq        },
93     { "COST_TIME",        HISYSEVENT_INT32,  TransAuditAssignerCostTime       },
94     { "DATA_TRAFFIC",     HISYSEVENT_INT32,  TransAuditAssignerDataTraffic    },
95     { "REQ_COUNT",        HISYSEVENT_INT32,  TransAuditAssignerReqCount       },
96     // Modification Note: remember updating TRANS_ASSIGNER_SIZE
97 };
98 
ConvertTransAuditForm2Param(HiSysEventParam params[],SoftbusEventForm * form)99 static inline size_t ConvertTransAuditForm2Param(HiSysEventParam params[], SoftbusEventForm *form)
100 {
101     size_t validSize = 0;
102     if (form == NULL || form->transExtra == NULL) {
103         return validSize;
104     }
105     for (size_t i = 0; i < sizeof(g_transAuditAssigners) / sizeof(g_transAuditAssigners[i]); ++i) {
106         HiSysEventParamAssigner assigner = g_transAuditAssigners[i];
107         if (assigner.Assign(assigner.name, assigner.type, form, &params[validSize])) {
108             ++validSize;
109         }
110     }
111     return validSize;
112 }
113 
114 #ifdef __cplusplus
115 }
116 #endif /* __cplusplus */
117 #endif // TRANS_AUDIT_CONVERTER_H
118