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 * miscservices under the License is miscservices 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 "legacy/softbus_hisysevt_common.h"
17
18 #include "securec.h"
19
20 #include "comm_log.h"
21 #include "message_handler.h"
22 #include "legacy/softbus_adapter_hisysevent.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_def.h"
25 #include "softbus_error_code.h"
26 #include "legacy/softbus_hisysevt_bus_center.h"
27 #include "legacy/softbus_hisysevt_connreporter.h"
28 #include "legacy/softbus_hisysevt_discreporter.h"
29 #include "legacy/softbus_hisysevt_transreporter.h"
30 #include "legacy/softbus_hisysevt_nstack.h"
31
32 #define MS_OF_DAY (24 * 3600 * 1000)
33 #define MSG_STATISTIC_EVT_REPORT 0
34
35 StatisticEvtReportFunc g_statisticEvtReportFunc[SOFTBUS_STATISTIC_EVT_BUTT] = {NULL};
36
GetStatisticEvtReportFunc(StatisticEvtType type)37 StatisticEvtReportFunc GetStatisticEvtReportFunc(StatisticEvtType type)
38 {
39 if (type < SOFTBUS_STATISTIC_EVT_START || type >= SOFTBUS_STATISTIC_EVT_BUTT) {
40 COMM_LOGE(COMM_EVENT, "invalid param");
41 return NULL;
42 }
43
44 return g_statisticEvtReportFunc[type];
45 }
46
SetStatisticEvtReportFunc(StatisticEvtType type,StatisticEvtReportFunc func)47 int32_t SetStatisticEvtReportFunc(StatisticEvtType type, StatisticEvtReportFunc func)
48 {
49 if (type < SOFTBUS_STATISTIC_EVT_START || type >= SOFTBUS_STATISTIC_EVT_BUTT || func == NULL) {
50 COMM_LOGE(COMM_EVENT, "invalid param");
51 return SOFTBUS_INVALID_PARAM;
52 }
53
54 g_statisticEvtReportFunc[type] = func;
55 return SOFTBUS_OK;
56 }
57
InitStatisticEvtReportFunc(void)58 static void InitStatisticEvtReportFunc(void)
59 {
60 for (int i = SOFTBUS_STATISTIC_EVT_START; i < SOFTBUS_STATISTIC_EVT_BUTT; i++) {
61 g_statisticEvtReportFunc[i] = NULL;
62 }
63 }
64
ReportStatisticEvent(SoftBusMessage * param)65 static void ReportStatisticEvent(SoftBusMessage* param)
66 {
67 (void)param;
68
69 for (int i = SOFTBUS_STATISTIC_EVT_START; i < SOFTBUS_STATISTIC_EVT_BUTT; i++) {
70 if (g_statisticEvtReportFunc[i] != NULL) {
71 g_statisticEvtReportFunc[i]();
72 }
73 }
74 }
75
FreeMessageFunc(SoftBusMessage * msg)76 static void FreeMessageFunc(SoftBusMessage* msg)
77 {
78 if (msg == NULL) {
79 return;
80 }
81
82 if (msg->handler != NULL) {
83 SoftBusFree(msg->handler);
84 }
85 SoftBusFree(msg);
86 }
87
88 typedef void (*HandleMessageFunc)(SoftBusMessage *msg);
89
CreateHandler(SoftBusLooper * looper,HandleMessageFunc callback)90 static inline SoftBusHandler* CreateHandler(SoftBusLooper *looper, HandleMessageFunc callback)
91 {
92 SoftBusHandler *handler = SoftBusMalloc(sizeof(SoftBusHandler));
93 if (handler == NULL) {
94 COMM_LOGE(COMM_EVENT, "create handler failed");
95 return NULL;
96 }
97 handler->looper = looper;
98 handler->name = "statisticEvtReportHandler";
99 handler->HandleMessage = callback;
100
101 return handler;
102 }
103
CreateMessage(SoftBusLooper * looper,HandleMessageFunc callback)104 static SoftBusMessage* CreateMessage(SoftBusLooper *looper, HandleMessageFunc callback)
105 {
106 SoftBusMessage* msg = SoftBusMalloc(sizeof(SoftBusMessage));
107 if (msg == NULL) {
108 COMM_LOGE(COMM_EVENT, "malloc softbus message failed");
109 return NULL;
110 }
111 SoftBusHandler *handler = CreateHandler(looper, callback);
112
113 msg->what = MSG_STATISTIC_EVT_REPORT;
114 msg->obj = NULL;
115 msg->handler = handler;
116 msg->FreeMessage = FreeMessageFunc;
117
118 return msg;
119 }
120
CreateAndPostMsgDelay(SoftBusLooper * looper,HandleMessageFunc callback,uint64_t delayMillis)121 static int32_t CreateAndPostMsgDelay(SoftBusLooper *looper, HandleMessageFunc callback,
122 uint64_t delayMillis)
123 {
124 if (looper == NULL) {
125 COMM_LOGE(COMM_EVENT, "invalid param looper");
126 return SOFTBUS_INVALID_PARAM;
127 }
128 if (callback == NULL) {
129 COMM_LOGE(COMM_EVENT, "invalid param callback");
130 return SOFTBUS_INVALID_PARAM;
131 }
132
133 SoftBusMessage *message = CreateMessage(looper, callback);
134 if (message == NULL) {
135 COMM_LOGE(COMM_EVENT, "create message fail");
136 return SOFTBUS_MEM_ERR;
137 }
138
139 looper->PostMessageDelay(looper, message, delayMillis);
140 return SOFTBUS_OK;
141 }
142
ReportStatisticEvtPeriod(SoftBusMessage * msg)143 static void ReportStatisticEvtPeriod(SoftBusMessage* msg)
144 {
145 ReportStatisticEvent(msg);
146 CreateAndPostMsgDelay(GetLooper(LOOP_TYPE_DEFAULT), ReportStatisticEvtPeriod, MS_OF_DAY);
147 }
148
InitSoftbusSysEvt(void)149 int32_t InitSoftbusSysEvt(void)
150 {
151 InitStatisticEvtReportFunc();
152
153 int32_t ret = InitTransStatisticSysEvt();
154 if (ret != SOFTBUS_OK) {
155 COMM_LOGE(COMM_INIT, "init trans statistic sys evt fail");
156 return ret;
157 }
158 ret = InitBusCenterDfx();
159 if (ret != SOFTBUS_OK) {
160 COMM_LOGE(COMM_INIT, "init bus center dfx fail");
161 return ret;
162 }
163 ret = InitDiscStatisticSysEvt();
164 if (ret != SOFTBUS_OK) {
165 COMM_LOGE(COMM_INIT, "init disc statistic fail");
166 return ret;
167 }
168 ret = InitConnStatisticSysEvt();
169 if (ret != SOFTBUS_OK) {
170 return ret;
171 }
172 #ifdef FILLP_ENHANCED
173 NstackInitHiEvent();
174 #endif
175 return CreateAndPostMsgDelay(GetLooper(LOOP_TYPE_DEFAULT), ReportStatisticEvtPeriod, MS_OF_DAY);
176 }
177
DeinitSoftbusSysEvt(void)178 void DeinitSoftbusSysEvt(void)
179 {
180 DeinitBusCenterDfx();
181 DeinitConnStatisticSysEvt();
182 DeinitDiscStatisticSysEvt();
183 DeinitTransStatisticSysEvt();
184 }
185
GetErrorCodeEx(int32_t errorCode)186 int32_t GetErrorCodeEx(int32_t errorCode)
187 {
188 if (errorCode < 0) {
189 return -errorCode;
190 }
191 return errorCode;
192 }