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 = (HiEvent *)HIVIEW_MemAlloc(MEM_POOL_HIVIEW_ID, sizeof(HiEvent));
79 if (event == NULL) {
80 return NULL;
81 }
82 event->payload = (uint8 *)HIVIEW_MemAlloc(MEM_POOL_HIVIEW_ID, SINGLE_VALUE_MAX_LEN * num);
83 if (event->payload == NULL) {
84 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, (void *)event);
85 return NULL;
86 }
87 // Number of values temporarily stored in mark.
88 event->common.mark = num;
89 event->common.eventId = eventId;
90 event->common.time = (uint32)(HIVIEW_GetCurrentTime() / MS_PER_SECOND);
91 event->common.len = 0;
92 event->type = type;
93
94 return event;
95 }
96
HiEventPutInteger(HiEvent * event,int8 key,int32 value)97 void HiEventPutInteger(HiEvent *event, int8 key, int32 value)
98 {
99 if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_OFF || event == NULL || event->payload == NULL ||
100 key < 0 || event->common.mark == 0) {
101 return;
102 }
103
104 uint8 encodeLen;
105 if (event->common.mark <= 1) {
106 encodeLen = HiEventEncode((uint8)key, value, 1, event->payload + event->common.len);
107 } else {
108 encodeLen = HiEventEncode((uint8)key, value, 0, event->payload + event->common.len);
109 }
110 event->common.len += encodeLen;
111 event->common.mark -= 1;
112 }
113
HiEventReport(HiEvent * event)114 void HiEventReport(HiEvent *event)
115 {
116 if (g_hiviewConfig.eventSwitch == HIVIEW_FEATURE_OFF || event == NULL || event->payload == NULL) {
117 return;
118 }
119
120 // All data has been added.
121 if (event->common.mark == 0) {
122 event->common.mark = EVENT_INFO_HEAD;
123 OutputEvent((uint8 *)event);
124 }
125 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, event->payload);
126 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, (void *)event);
127 }
128
HiEventEncode(uint8 k,int32 v,uint8 last,uint8 * encodeOut)129 static uint8 HiEventEncode(uint8 k, int32 v, uint8 last, uint8 *encodeOut)
130 {
131 HiEventTag tag;
132
133 if (encodeOut == NULL) {
134 return 0;
135 }
136 tag.last = last;
137 tag.id = k;
138 if (v >= 0 && v <= 0xFF) {
139 tag.len = ENCODE_VALUE_LEN1;
140 } else if (v >= 0 && v <= 0xFFFF) {
141 tag.len = ENCODE_VALUE_LEN2;
142 } else if (v >= 0 && v <= 0x00FFFFFF) {
143 tag.len = ENCODE_VALUE_LEN3;
144 } else {
145 tag.len = ENCODE_VALUE_LEN4;
146 }
147 (void)memcpy_s(encodeOut, sizeof(HiEventTag), (void *)&tag, sizeof(HiEventTag));
148
149 switch (tag.len) {
150 case ENCODE_VALUE_LEN1:
151 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
152 break;
153 case ENCODE_VALUE_LEN2:
154 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
155 *(encodeOut + ENCODE_VALUE_LEN2) = GET_UINT32_BYTE2(v);
156 break;
157 case ENCODE_VALUE_LEN3:
158 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
159 *(encodeOut + ENCODE_VALUE_LEN2) = GET_UINT32_BYTE2(v);
160 *(encodeOut + ENCODE_VALUE_LEN3) = GET_UINT32_BYTE3(v);
161 break;
162 default:
163 *(encodeOut + ENCODE_VALUE_LEN1) = GET_UINT32_BYTE1(v);
164 *(encodeOut + ENCODE_VALUE_LEN2) = GET_UINT32_BYTE2(v);
165 *(encodeOut + ENCODE_VALUE_LEN3) = GET_UINT32_BYTE3(v);
166 *(encodeOut + ENCODE_VALUE_LEN4) = GET_UINT32_BYTE4(v);
167 break;
168 }
169
170 return tag.len + 1;
171 }
172
HiEventFlush(boolean syncFlag)173 void HiEventFlush(boolean syncFlag)
174 {
175 FlushEvent(syncFlag);
176 }
177
HiEventRegisterProc(HieventProc func)178 void HiEventRegisterProc(HieventProc func)
179 {
180 HiviewRegisterHieventProc(func);
181 }
182
HiEventUnRegisterProc(HieventProc func)183 void HiEventUnRegisterProc(HieventProc func)
184 {
185 HiviewUnRegisterHieventProc(func);
186 }
187
HiEventFileAddWatcher(uint8 type,FileProc func,const char * path)188 void HiEventFileAddWatcher(uint8 type, FileProc func, const char *path)
189 {
190 HiviewRegisterHieventFileWatcher(type, func, path);
191 }
192
HiEventFileRemoveWatcher(uint8 type,FileProc func)193 void HiEventFileRemoveWatcher(uint8 type, FileProc func)
194 {
195 HiviewUnRegisterHieventFileWatcher(type, func);
196 }
197
HiEventFileProc(uint8 type,const char * dest,uint8 mode)198 int HiEventFileProc(uint8 type, const char *dest, uint8 mode)
199 {
200 return HiEventFileProcImp(type, dest, mode);
201 }
202
HiEventOutputFileLock(void)203 void HiEventOutputFileLock(void)
204 {
205 HiEventOutputFileLockImp();
206 }
207
HiEventOutputFileUnLock(void)208 void HiEventOutputFileUnLock(void)
209 {
210 HiEventOutputFileUnLockImp();
211 }
212