1 /*
2 * Copyright (c) 2020 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 "hiview_event.h"
17 #include "hiview_config.h"
18 #include "hiview_def.h"
19 #include "hiview_file.h"
20 #include "hiview_output_event.h"
21 #include "hiview_service.h"
22 #include "hiview_util.h"
23 #include "ohos_init.h"
24 #include "securec.h"
25 #include "string.h"
26
27 #define SINGLE_VALUE_MAX_LEN 5
28 #define EVENT_VALUE_MAX_NUM 16
29 #define ENCODE_VALUE_LEN1 1
30 #define ENCODE_VALUE_LEN2 2
31 #define ENCODE_VALUE_LEN3 3
32 #define ENCODE_VALUE_LEN4 4
33 #define GET_UINT32_BYTE1(v) (uint8)(((uint32)(v)) & 0x000000FF)
34 #define GET_UINT32_BYTE2(v) (uint8)((((uint32)(v)) & 0x0000FF00) >> 8)
35 #define GET_UINT32_BYTE3(v) (uint8)((((uint32)(v)) & 0x00FF0000) >> 16)
36 #define GET_UINT32_BYTE4(v) (uint8)((((uint32)(v)) & 0xFF000000) >> 24)
37
38 static uint8 HiEventEncode(uint8 k, int32 v, uint8 last, uint8 *encodeOut);
39
HiEventInit(void)40 static void HiEventInit(void)
41 {
42 HIVIEW_UartPrint("hievent will init.\n");
43 if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_ON && HIEVENT_COMPILE_TYPE > HIEVENT_NONE) {
44 InitCoreEventOutput();
45 HiviewRegisterInitFunc(HIVIEW_CMP_TYPE_EVENT, InitEventOutput);
46 HIVIEW_UartPrint("hievent init success.");
47 }
48 }
49 CORE_INIT_PRI(HiEventInit, 1);
50
HiEventPrintf(uint8 type,uint16 eventId,int8 key,int32 value)51 void HiEventPrintf(uint8 type, uint16 eventId, int8 key, int32 value)
52 {
53 if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_OFF) {
54 return;
55 }
56 HiEvent e = { 0 };
57 uint8 encodeBuffer[SINGLE_VALUE_MAX_LEN] = {0};
58 e.common.mark = EVENT_INFO_HEAD;
59 e.common.eventId = eventId;
60 e.common.time = (uint32)(HIVIEW_GetCurrentTime() / MS_PER_SECOND);
61 e.type = type;
62 if (key < 0) {
63 e.common.len = 0;
64 e.payload = NULL;
65 } else {
66 e.common.len = HiEventEncode((uint8)key, value, 1, encodeBuffer);
67 e.payload = encodeBuffer;
68 }
69
70 OutputEvent((uint8 *)&e);
71 }
72
HiEventCreate(uint8 type,uint16 eventId,uint8 num)73 HiEvent *HiEventCreate(uint8 type, uint16 eventId, uint8 num)
74 {
75 if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_OFF || num > EVENT_VALUE_MAX_NUM) {
76 return NULL;
77 }
78 HiEvent *event = NULL;
79 event = (HiEvent *)HIVIEW_MemAlloc(MEM_POOL_HIVIEW_ID, sizeof(HiEvent));
80 if (event == NULL) {
81 return NULL;
82 }
83 event->payload = (uint8 *)HIVIEW_MemAlloc(MEM_POOL_HIVIEW_ID, SINGLE_VALUE_MAX_LEN * num);
84 if (event->payload == NULL) {
85 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, (void *)event);
86 return NULL;
87 }
88 // Number of values temporarily stored in mark.
89 event->common.mark = num;
90 event->common.eventId = eventId;
91 event->common.time = (uint32)(HIVIEW_GetCurrentTime() / MS_PER_SECOND);
92 event->common.len = 0;
93 event->type = type;
94
95 return event;
96 }
97
HiEventPutInteger(HiEvent * event,int8 key,int32 value)98 void HiEventPutInteger(HiEvent *event, int8 key, int32 value)
99 {
100 if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_OFF || event == NULL || event->payload == NULL ||
101 key < 0 || event->common.mark == 0) {
102 return;
103 }
104
105 uint8 encodeLen;
106 if (event->common.mark <= 1) {
107 encodeLen = HiEventEncode((uint8)key, value, 1, event->payload + event->common.len);
108 } else {
109 encodeLen = HiEventEncode((uint8)key, value, 0, event->payload + event->common.len);
110 }
111 event->common.len += encodeLen;
112 event->common.mark -= 1;
113 }
114
HiEventReport(HiEvent * event)115 void HiEventReport(HiEvent *event)
116 {
117 if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_OFF || event == NULL || event->payload == NULL) {
118 return;
119 }
120
121 // All data has been added.
122 if (event->common.mark == 0) {
123 event->common.mark = EVENT_INFO_HEAD;
124 OutputEvent((uint8 *)event);
125 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, event->payload);
126 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, (void *)event);
127 } else {
128 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, event->payload);
129 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, (void *)event);
130 }
131 }
132
HiEventEncode(uint8 k,int32 v,uint8 last,uint8 * encodeOut)133 static uint8 HiEventEncode(uint8 k, int32 v, uint8 last, uint8 *encodeOut)
134 {
135 HiEventTag tag;
136
137 if (encodeOut == NULL) {
138 return 0;
139 }
140 tag.last = last;
141 tag.id = k;
142 if (v >= 0 && v <= 0xFF) {
143 tag.len = ENCODE_VALUE_LEN1;
144 } else if (v >= 0 && v <= 0xFFFF) {
145 tag.len = ENCODE_VALUE_LEN2;
146 } else if (v >= 0 && v <= 0x00FFFFFF) {
147 tag.len = ENCODE_VALUE_LEN3;
148 } else {
149 tag.len = ENCODE_VALUE_LEN4;
150 }
151 memcpy_s(encodeOut, sizeof(HiEventTag), (void *)&tag, sizeof(HiEventTag));
152
153 switch (tag.len) {
154 case ENCODE_VALUE_LEN1:
155 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
156 break;
157 case ENCODE_VALUE_LEN2:
158 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
159 *(encodeOut + ENCODE_VALUE_LEN2) = GET_UINT32_BYTE2(v);
160 break;
161 case ENCODE_VALUE_LEN3:
162 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
163 *(encodeOut + ENCODE_VALUE_LEN2) = GET_UINT32_BYTE2(v);
164 *(encodeOut + ENCODE_VALUE_LEN3) = GET_UINT32_BYTE3(v);
165 break;
166 default:
167 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
168 *(encodeOut + ENCODE_VALUE_LEN2) = GET_UINT32_BYTE2(v);
169 *(encodeOut + ENCODE_VALUE_LEN3) = GET_UINT32_BYTE3(v);
170 *(encodeOut + ENCODE_VALUE_LEN4) = GET_UINT32_BYTE4(v);
171 break;
172 }
173
174 return tag.len + 1;
175 }
176
HiEventFlush(boolean syncFlag)177 void HiEventFlush(boolean syncFlag)
178 {
179 FlushEvent(syncFlag);
180 }
181
HiEventRegisterProc(HieventProc func)182 void HiEventRegisterProc(HieventProc func)
183 {
184 HiviewRegisterHieventProc(func);
185 }
186
HiEventUnRegisterProc(HieventProc func)187 void HiEventUnRegisterProc(HieventProc func)
188 {
189 HiviewUnRegisterHieventProc(func);
190 }
191
HiEventFileAddWatcher(uint8 type,FileProc func,const char * path)192 void HiEventFileAddWatcher(uint8 type, FileProc func, const char *path)
193 {
194 HiviewRegisterHieventFileWatcher(type, func, path);
195 }
196
HiEventFileRemoveWatcher(uint8 type,FileProc func)197 void HiEventFileRemoveWatcher(uint8 type, FileProc func)
198 {
199 HiviewUnRegisterHieventFileWatcher(type, func);
200 }
201
HiEventFileProc(uint8 type,const char * dest,uint8 mode)202 int HiEventFileProc(uint8 type, const char *dest, uint8 mode)
203 {
204 return HiEventFileProcImp(type, dest, mode);
205 }
206
HiEventOutputFileLock(void)207 void HiEventOutputFileLock(void)
208 {
209 HiEventOutputFileLockImp();
210 }
211
HiEventOutputFileUnLock(void)212 void HiEventOutputFileUnLock(void)
213 {
214 HiEventOutputFileUnLockImp();
215 }
216