• 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  * 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 "message_handler.h"
21 #include "softbus_adapter_hisysevent.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_def.h"
24 #include "softbus_error_code.h"
25 #include "softbus_hisysevt_bus_center.h"
26 #include "softbus_hisysevt_connreporter.h"
27 #include "softbus_hisysevt_discreporter.h"
28 #include "softbus_hisysevt_transreporter.h"
29 #include "softbus_log.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         return NULL;
41     }
42 
43     return g_statisticEvtReportFunc[type];
44 }
45 
SetStatisticEvtReportFunc(StatisticEvtType type,StatisticEvtReportFunc func)46 int32_t SetStatisticEvtReportFunc(StatisticEvtType type, StatisticEvtReportFunc func)
47 {
48     if (type < SOFTBUS_STATISTIC_EVT_START || type >= SOFTBUS_STATISTIC_EVT_BUTT || func == NULL) {
49         return SOFTBUS_ERR;
50     }
51 
52     g_statisticEvtReportFunc[type] = func;
53     return SOFTBUS_OK;
54 }
55 
InitStatisticEvtReportFunc(void)56 static void InitStatisticEvtReportFunc(void)
57 {
58     for (int i = SOFTBUS_STATISTIC_EVT_START; i < SOFTBUS_STATISTIC_EVT_BUTT; i++) {
59         g_statisticEvtReportFunc[i] = NULL;
60     }
61 }
62 
ReportStatisticEvent(SoftBusMessage * param)63 NO_SANITIZE("cfi") static void ReportStatisticEvent(SoftBusMessage* param)
64 {
65     (void)param;
66 
67     for (int i = SOFTBUS_STATISTIC_EVT_START; i < SOFTBUS_STATISTIC_EVT_BUTT; i++) {
68         if (g_statisticEvtReportFunc[i] != NULL) {
69             g_statisticEvtReportFunc[i]();
70         }
71     }
72 }
73 
FreeMessageFunc(SoftBusMessage * msg)74 static void FreeMessageFunc(SoftBusMessage* msg)
75 {
76     if (msg == NULL) {
77         return;
78     }
79 
80     if (msg->handler != NULL) {
81         SoftBusFree(msg->handler);
82     }
83     SoftBusFree(msg);
84 }
85 
86 typedef void (*HandleMessageFunc)(SoftBusMessage *msg);
87 
CreateHandler(SoftBusLooper * looper,HandleMessageFunc callback)88 static inline SoftBusHandler* CreateHandler(SoftBusLooper *looper, HandleMessageFunc callback)
89 {
90     SoftBusHandler *handler = SoftBusMalloc(sizeof(SoftBusHandler));
91     if (handler == NULL) {
92         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "create handler failed");
93         return NULL;
94     }
95     handler->looper = looper;
96     handler->name = "statisticEvtReportHandler";
97     handler->HandleMessage = callback;
98 
99     return handler;
100 }
101 
CreateMessage(SoftBusLooper * looper,HandleMessageFunc callback)102 static SoftBusMessage* CreateMessage(SoftBusLooper *looper, HandleMessageFunc callback)
103 {
104     SoftBusMessage* msg = SoftBusMalloc(sizeof(SoftBusMessage));
105     if (msg == NULL) {
106         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "malloc softbus message failed");
107         return NULL;
108     }
109     SoftBusHandler *handler = CreateHandler(looper, callback);
110 
111     msg->what = MSG_STATISTIC_EVT_REPORT;
112     msg->obj = NULL;
113     msg->handler = handler;
114     msg->FreeMessage = FreeMessageFunc;
115 
116     return msg;
117 }
118 
CreateAndPostMsgDelay(SoftBusLooper * looper,HandleMessageFunc callback,uint64_t delayMillis)119 NO_SANITIZE("cfi") static int32_t CreateAndPostMsgDelay(SoftBusLooper *looper, HandleMessageFunc callback,
120     uint64_t delayMillis)
121 {
122     if ((looper == NULL) || (callback == NULL)) {
123         return SOFTBUS_INVALID_PARAM;
124     }
125 
126     SoftBusMessage *message = CreateMessage(looper, callback);
127     if (message == NULL) {
128         return SOFTBUS_MEM_ERR;
129     }
130 
131     looper->PostMessageDelay(looper, message, delayMillis);
132     return SOFTBUS_OK;
133 }
134 
ReportStatisticEvtPeriod(SoftBusMessage * msg)135 static void ReportStatisticEvtPeriod(SoftBusMessage* msg)
136 {
137     ReportStatisticEvent(msg);
138     CreateAndPostMsgDelay(GetLooper(LOOP_TYPE_DEFAULT), ReportStatisticEvtPeriod, MS_OF_DAY);
139 }
140 
InitSoftbusSysEvt(void)141 int32_t InitSoftbusSysEvt(void)
142 {
143     InitStatisticEvtReportFunc();
144 
145     if (InitTransStatisticSysEvt() != SOFTBUS_OK) {
146         return SOFTBUS_ERR;
147     }
148     if (InitBusCenterDfx() != SOFTBUS_OK) {
149         return SOFTBUS_ERR;
150     }
151 
152     if (InitDiscStatisticSysEvt() != SOFTBUS_OK) {
153         return SOFTBUS_ERR;
154     }
155 
156     if (InitConnStatisticSysEvt() != SOFTBUS_OK) {
157         return SOFTBUS_ERR;
158     }
159 #ifdef FILLP_ENHANCED
160     NstackInitHiEvent();
161 #endif
162     return CreateAndPostMsgDelay(GetLooper(LOOP_TYPE_DEFAULT), ReportStatisticEvtPeriod, MS_OF_DAY);
163 }
164 
DeinitSoftbusSysEvt(void)165 void DeinitSoftbusSysEvt(void)
166 {
167     DeinitBusCenterDfx();
168     DeinitConnStatisticSysEvt();
169     DeinitDiscStatisticSysEvt();
170     DeinitTransStatisticSysEvt();
171 }