1 /*
2 * Copyright (c) 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 "hisysevent_c_wrapper.h"
17
18 #include "securec.h"
19 #include "hisysevent_record_convertor.h"
20 #include "hisysevent_rust_manager.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
ConvertParamWrapper(const HiSysEventParamWrapper src[],HiSysEventParam dest[],unsigned int size)26 static inline void ConvertParamWrapper(const HiSysEventParamWrapper src[], HiSysEventParam dest[], unsigned int size)
27 {
28 for (size_t i = 0; i < size; i++) {
29 HiSysEventParamWrapper wrapper = src[i];
30 for (size_t j = 0; j < MAX_LENGTH_OF_PARAM_NAME && wrapper.paramName[j] != '\0'; j++) {
31 dest[i].name[j] = wrapper.paramName[j];
32 }
33 dest[i].t = HiSysEventParamType(wrapper.paramType);
34 dest[i].v = wrapper.paramValue;
35 dest[i].arraySize = wrapper.arraySize;
36 }
37 }
38
SplitStringToArray(const char src[],unsigned int srcMaxLen,char dest[][MAX_LENGTH_OF_EVENT_NAME],unsigned int destSize)39 static void SplitStringToArray(const char src[], unsigned int srcMaxLen, char dest[][MAX_LENGTH_OF_EVENT_NAME],
40 unsigned int destSize)
41 {
42 size_t curPos = 0; // The current position is initialized to be 0.
43 size_t destItemIndex = 0; // The index of array item is initialized to be 0.
44 int sliceBegin = 0; // The begin position of a slice is initialized to be 0.
45 int sliceEnd = 1; // The end position of a slice is initialized to be 1.
46 int cpyLen = 0; // The length of string to be copied is initialized to be 0.
47 while (curPos < srcMaxLen && src[curPos] != '\0') {
48 if (src[curPos] != '|') {
49 ++curPos;
50 continue;
51 }
52 sliceEnd = static_cast<int>(curPos) - 1;
53 cpyLen = sliceEnd - sliceBegin + 1;
54 if ((cpyLen <= 0) || (cpyLen > MAX_LENGTH_OF_EVENT_NAME) ||
55 (memcpy_s(dest[destItemIndex], cpyLen, src + sliceBegin, cpyLen) != EOK)) {
56 // If the length of the string to be copied is invalid or memory copy failed, skip this step.
57 sliceBegin = static_cast<int>(curPos + 1);
58 ++curPos;
59 continue;
60 }
61 sliceBegin = static_cast<int>(curPos + 1);
62 ++curPos;
63 ++destItemIndex;
64 if (destItemIndex >= destSize) {
65 break;
66 }
67 }
68 if (curPos >= srcMaxLen || src[curPos] == '\0') {
69 sliceEnd = static_cast<int>(curPos) - 1;
70 }
71 cpyLen = sliceEnd - sliceBegin + 1;
72 if ((cpyLen <= 0) || (cpyLen > MAX_LENGTH_OF_EVENT_NAME) || (destItemIndex >= destSize) ||
73 (memcpy_s(dest[destItemIndex], cpyLen, src + sliceBegin, cpyLen) != EOK)) {
74 return;
75 }
76 }
77
ConvertQueryRuleWrapper(const HiSysEventQueryRuleWrapper src[],HiSysEventQueryRule dest[],unsigned int size)78 static inline void ConvertQueryRuleWrapper(const HiSysEventQueryRuleWrapper src[], HiSysEventQueryRule dest[],
79 unsigned int size)
80 {
81 for (size_t i = 0; i < size; i++) {
82 HiSysEventQueryRuleWrapper wrapper = src[i];
83 for (size_t j = 0; (j < MAX_LENGTH_OF_EVENT_DOMAIN) && (wrapper.domain[j] != '\0'); j++) {
84 dest[i].domain[j] = wrapper.domain[j];
85 }
86 SplitStringToArray(wrapper.eventList, MAX_EVENT_LIST_LEN, dest[i].eventList,
87 wrapper.eventListSize);
88 dest[i].eventListSize = wrapper.eventListSize;
89 dest[i].condition = wrapper.condition;
90 }
91 }
92
HiSysEventWriteWrapper(const char * func,unsigned long long line,const char * domain,const char * name,int type,const HiSysEventParamWrapper paramWrappers[],unsigned int size)93 int HiSysEventWriteWrapper(const char* func, unsigned long long line, const char* domain, const char* name,
94 int type, const HiSysEventParamWrapper paramWrappers[], unsigned int size)
95 {
96 HiSysEventParam params[size];
97 ConvertParamWrapper(paramWrappers, params, size);
98 int ret = HiSysEvent_Write(func, line, domain, name, HiSysEventEventType(type), params, size);
99 for (const auto& param : params) {
100 if ((param.t == HISYSEVENT_STRING) && (param.v.s != nullptr)) {
101 delete param.v.s;
102 }
103 }
104 return ret;
105 }
106
HiSysEventAddWatcherWrapper(HiSysEventRustWatcherC * watcher,const HiSysEventWatchRule rules[],unsigned int ruleSize)107 int HiSysEventAddWatcherWrapper(HiSysEventRustWatcherC* watcher, const HiSysEventWatchRule rules[],
108 unsigned int ruleSize)
109 {
110 return OhHiSysEventAddRustWatcher(watcher, rules, ruleSize);
111 }
112
HiSysEventRemoveWatcherWrapper(HiSysEventRustWatcherC * watcher)113 int HiSysEventRemoveWatcherWrapper(HiSysEventRustWatcherC* watcher)
114 {
115 return OhHiSysEventRemoveRustWatcher(watcher);
116 }
117
HiSysEventQueryWrapper(HiSysEventQueryArg * arg,const HiSysEventQueryRuleWrapper queryRules[],unsigned int ruleSize,HiSysEventRustQuerierC * querier)118 int HiSysEventQueryWrapper(HiSysEventQueryArg* arg, const HiSysEventQueryRuleWrapper queryRules[],
119 unsigned int ruleSize, HiSysEventRustQuerierC* querier)
120 {
121 HiSysEventQueryRule rules[ruleSize];
122 ConvertQueryRuleWrapper(queryRules, rules, ruleSize);
123 return OhHiSysEventRustQuery(arg, rules, ruleSize, querier);
124 }
125
GetHiSysEventRecordByIndexWrapper(const HiSysEventRecordC records[],unsigned int total,unsigned int index)126 HiSysEventRecordC GetHiSysEventRecordByIndexWrapper(const HiSysEventRecordC records[], unsigned int total,
127 unsigned int index)
128 {
129 if (index >= total) {
130 HiSysEventRecordC record = {};
131 return record;
132 }
133 return records[index];
134 }
135
CreateRustEventWatcher(OnRustCb onEventRustCb,OnEventWrapperCb onEventWrapperCb,OnRustCb onServiceDiedRustCb,OnServiceDiedWrapperCb onServiceDiedWrapperCb)136 HiSysEventRustWatcherC* CreateRustEventWatcher(OnRustCb onEventRustCb,
137 OnEventWrapperCb onEventWrapperCb, OnRustCb onServiceDiedRustCb,
138 OnServiceDiedWrapperCb onServiceDiedWrapperCb)
139 {
140 if (onEventRustCb == nullptr || onEventWrapperCb == nullptr ||
141 onServiceDiedRustCb == nullptr || onServiceDiedWrapperCb == nullptr) {
142 return nullptr;
143 }
144 HiSysEventRustWatcherC* watcher = new(std::nothrow) HiSysEventRustWatcherC;
145 watcher->onEventRustCb = onEventRustCb;
146 watcher->onEventWrapperCb = onEventWrapperCb;
147 watcher->onServiceDiedRustCb = onServiceDiedRustCb;
148 watcher->onServiceDiedWrapperCb = onServiceDiedWrapperCb;
149 watcher->status = STATUS_NORMAL;
150 return watcher;
151 }
152
RecycleRustEventWatcher(HiSysEventRustWatcherC * watcher)153 void RecycleRustEventWatcher(HiSysEventRustWatcherC* watcher)
154 {
155 OhHiSysEventRecycleRustWatcher(watcher);
156 }
157
CreateRustEventQuerier(OnRustCb onQueryRustCb,OnQueryWrapperCb onQueryWrapperCb,OnRustCb onCompleteRustCb,OnCompleteWrapperCb onCompleteWrapperCb)158 HiSysEventRustQuerierC* CreateRustEventQuerier(OnRustCb onQueryRustCb,
159 OnQueryWrapperCb onQueryWrapperCb, OnRustCb onCompleteRustCb,
160 OnCompleteWrapperCb onCompleteWrapperCb)
161 {
162 if (onQueryRustCb == nullptr || onQueryWrapperCb == nullptr ||
163 onCompleteRustCb == nullptr || onCompleteWrapperCb == nullptr) {
164 return nullptr;
165 }
166 HiSysEventRustQuerierC* querier = new(std::nothrow) HiSysEventRustQuerierC;
167 querier->onQueryRustCb = onQueryRustCb;
168 querier->onQueryWrapperCb = onQueryWrapperCb;
169 querier->onCompleteRustCb = onCompleteRustCb;
170 querier->onCompleteWrapperCb = onCompleteWrapperCb;
171 querier->status = STATUS_NORMAL;
172 return querier;
173 }
174
RecycleRustEventQuerier(HiSysEventRustQuerierC * querier)175 void RecycleRustEventQuerier(HiSysEventRustQuerierC* querier)
176 {
177 OhHiSysEventRecycleRustQuerier(querier);
178 }
179
180 #ifdef __cplusplus
181 }
182 #endif