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 "softbus_hisysevt_common.h"
17
18 #include "securec.h"
19
20 #include "comm_log.h"
21 #include "message_handler.h"
22 #include "softbus_adapter_hisysevent.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_def.h"
25 #include "softbus_error_code.h"
26 #include "softbus_hisysevt_bus_center.h"
27 #include "softbus_hisysevt_connreporter.h"
28 #include "softbus_hisysevt_discreporter.h"
29 #include "softbus_hisysevt_transreporter.h"
30 #include "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) || (callback == NULL)) {
125 COMM_LOGE(COMM_EVENT, "invalid param");
126 return SOFTBUS_INVALID_PARAM;
127 }
128
129 SoftBusMessage *message = CreateMessage(looper, callback);
130 if (message == NULL) {
131 COMM_LOGE(COMM_EVENT, "create message fail");
132 return SOFTBUS_MEM_ERR;
133 }
134
135 looper->PostMessageDelay(looper, message, delayMillis);
136 return SOFTBUS_OK;
137 }
138
ReportStatisticEvtPeriod(SoftBusMessage * msg)139 static void ReportStatisticEvtPeriod(SoftBusMessage* msg)
140 {
141 ReportStatisticEvent(msg);
142 CreateAndPostMsgDelay(GetLooper(LOOP_TYPE_DEFAULT), ReportStatisticEvtPeriod, MS_OF_DAY);
143 }
144
InitSoftbusSysEvt(void)145 int32_t InitSoftbusSysEvt(void)
146 {
147 InitStatisticEvtReportFunc();
148
149 int32_t ret = InitTransStatisticSysEvt();
150 if (ret != SOFTBUS_OK) {
151 COMM_LOGE(COMM_INIT, "init trans statistic sys evt fail");
152 return ret;
153 }
154 ret = InitBusCenterDfx();
155 if (ret != SOFTBUS_OK) {
156 COMM_LOGE(COMM_INIT, "init bus center dfx fail");
157 return ret;
158 }
159 ret = InitDiscStatisticSysEvt();
160 if (ret != SOFTBUS_OK) {
161 COMM_LOGE(COMM_INIT, "init disc statistic fail");
162 return ret;
163 }
164 ret = InitConnStatisticSysEvt();
165 if (ret != SOFTBUS_OK) {
166 return ret;
167 }
168 #ifdef FILLP_ENHANCED
169 NstackInitHiEvent();
170 #endif
171 return CreateAndPostMsgDelay(GetLooper(LOOP_TYPE_DEFAULT), ReportStatisticEvtPeriod, MS_OF_DAY);
172 }
173
DeinitSoftbusSysEvt(void)174 void DeinitSoftbusSysEvt(void)
175 {
176 DeinitBusCenterDfx();
177 DeinitConnStatisticSysEvt();
178 DeinitDiscStatisticSysEvt();
179 DeinitTransStatisticSysEvt();
180 }
181
GetErrorCodeEx(int32_t errorCode)182 int32_t GetErrorCodeEx(int32_t errorCode)
183 {
184 if (errorCode < 0) {
185 return -errorCode;
186 }
187 return errorCode;
188 }