1 /*
2  * Copyright (C) 2021-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 "nstackx_dfinder_hievent.h"
17 #include "nstackx_dfinder_log.h"
18 #include "nstackx_statistics.h"
19 #include "nstackx_error.h"
20 #include "nstackx_common.h"
21 #include "nstackx_event.h"
22 #include "securec.h"
23 
24 #define TAG "nStackXDFinder"
25 #define STAT_EVT_PARA_NUM 4
26 #define STAT_EVT_NAME "DFINDER_STATS"
27 
28 static void *g_softObj;
29 static DFinderEventFunc g_eventFunc;
30 
31 static int g_statisticsIdx[STAT_EVT_PARA_NUM] = {
32     STATS_INVALID_OPT_AND_PAYLOAD,
33     STATS_BUILD_PKT_FAILED,
34     STATS_INVALID_RESPONSE_MSG,
35     STATS_OVER_DEVICE_LIMIT
36 };
37 
38 static DFinderEventParam g_statisticsPara[STAT_EVT_PARA_NUM] = {
39     {
40         .type = DFINDER_PARAM_TYPE_UINT64,
41         .name = "INVALID_OPTION_CNT"
42     },
43     {
44         .type = DFINDER_PARAM_TYPE_UINT64,
45         .name = "BUILD_PKT_FAIL_CNT"
46     },
47     {
48         .type = DFINDER_PARAM_TYPE_UINT64,
49         .name = "INVALID_RSP_CNT"
50     },
51     {
52         .type = DFINDER_PARAM_TYPE_UINT64,
53         .name = "OVER_DEVICE_LIMIT_CNT"
54     }
55 };
56 
CreateStatisticsEventParams(void)57 static DFinderEventParam *CreateStatisticsEventParams(void)
58 {
59     int i;
60     const uint64_t *stat = GetStatistics();
61     DFinderEventParam *para = (DFinderEventParam *)calloc(STAT_EVT_PARA_NUM, sizeof(DFinderEventParam));
62     if (para == NULL) {
63         return NULL;
64     }
65 
66     for (i = 0; i < STAT_EVT_PARA_NUM; i++) {
67         para[i] = g_statisticsPara[i];
68         para[i].value.u64v = stat[g_statisticsIdx[i]];
69     }
70     return para;
71 }
72 
NotifyStatisticsEvent(void)73 void NotifyStatisticsEvent(void)
74 {
75     int ret;
76     DFinderEvent evt;
77 
78     if (g_eventFunc == NULL) {
79         return;
80     }
81 
82     (void)memset_s(&evt, sizeof(evt), 0, sizeof(evt));
83     evt.type = DFINDER_EVENT_TYPE_STATISTIC;
84     evt.level = DFINDER_EVENT_LEVEL_MINOR;
85     evt.paramNum = STAT_EVT_PARA_NUM;
86     ret = memcpy_s(evt.eventName, DFINDER_EVENT_NAME_LEN, STAT_EVT_NAME, strlen(STAT_EVT_NAME));
87     if (ret != EOK) {
88         DFINDER_LOGE(TAG, "memcpy_s eventName failed");
89         return;
90     }
91 
92     evt.params = CreateStatisticsEventParams();
93     if (evt.params == NULL) {
94         DFINDER_LOGE(TAG, "create statistics params failed");
95         return;
96     }
97     g_eventFunc(g_softObj, &evt);
98     free(evt.params);
99     DFINDER_LOGD(TAG, "report statistics event");
100 }
101 
102 typedef struct {
103     void *softobj;
104     DFinderEventFunc func;
105 } DFinderEventMsg;
106 
SetEventFuncInner(void * arg)107 static void SetEventFuncInner(void *arg)
108 {
109     DFinderEventMsg *msg = (DFinderEventMsg *)arg;
110     g_softObj = msg->softobj;
111     g_eventFunc = msg->func;
112     free(msg);
113 }
114 
SetEventFunc(void * softobj,DFinderEventFunc func)115 int SetEventFunc(void *softobj, DFinderEventFunc func)
116 {
117     DFinderEventMsg *msg = (DFinderEventMsg *)malloc(sizeof(DFinderEventMsg));
118     if (msg == NULL) {
119         return NSTACKX_EFAILED;
120     }
121 
122     msg->softobj = softobj;
123     msg->func = func;
124     if (PostEvent(GetEventNodeChain(), GetEpollFD(), SetEventFuncInner, msg) != NSTACKX_EOK) {
125         free(msg);
126         return NSTACKX_EFAILED;
127     }
128 
129     return NSTACKX_EOK;
130 }
131 
ResetEventFunc(void)132 void ResetEventFunc(void)
133 {
134     g_softObj = NULL;
135     g_eventFunc = NULL;
136 }
137