1 /*
2 * Copyright (c) 2021 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 "hiappevent_c.h"
17
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21
22 #include "ndk_app_event_processor_service.h"
23 #include "ndk_app_event_watcher_service.h"
24
25 #define NEW(type) (type*)malloc(sizeof(type))
26 #define NEW_PARAM(uType, uName, uValue, len) ({ \
27 ParamValue* pValue = NEW(ParamValue); \
28 if (pValue != NULL) { \
29 pValue->type = uType; \
30 pValue->value.uName = uValue; \
31 pValue->arrSize = len; \
32 } \
33 pValue; \
34 })
35
36 #define ADD_BOOL_PARAM(list, name, uValue) \
37 AddParamValue(list, name, NEW_PARAM(BOOL_PARAM, bool_v, uValue, 0))
38 #define ADD_BOOL_ARR_PARAM(list, name, uValue, len) \
39 AddParamValue(list, name, NEW_PARAM(BOOL_ARR_PARAM, bool_arr_v, uValue, len))
40 #define ADD_INT8_PARAM(list, name, uValue) \
41 AddParamValue(list, name, NEW_PARAM(INT8_PARAM, int8_v, uValue, 0))
42 #define ADD_INT8_ARR_PARAM(list, name, uValue, len) \
43 AddParamValue(list, name, NEW_PARAM(INT8_ARR_PARAM, int8_arr_v, uValue, len))
44 #define ADD_INT16_PARAM(list, name, uValue) \
45 AddParamValue(list, name, NEW_PARAM(INT16_PARAM, int16_v, uValue, 0))
46 #define ADD_INT16_ARR_PARAM(list, name, uValue, len) \
47 AddParamValue(list, name, NEW_PARAM(INT16_ARR_PARAM, int16_arr_v, uValue, len))
48 #define ADD_INT32_PARAM(list, name, uValue) \
49 AddParamValue(list, name, NEW_PARAM(INT32_PARAM, int32_v, uValue, 0))
50 #define ADD_INT32_ARR_PARAM(list, name, uValue, len) \
51 AddParamValue(list, name, NEW_PARAM(INT32_ARR_PARAM, int32_arr_v, uValue, len))
52 #define ADD_INT64_PARAM(list, name, uValue) \
53 AddParamValue(list, name, NEW_PARAM(INT64_PARAM, int64_v, uValue, 0))
54 #define ADD_INT64_ARR_PARAM(list, name, uValue, len) \
55 AddParamValue(list, name, NEW_PARAM(INT64_ARR_PARAM, int64_arr_v, uValue, len))
56 #define ADD_FLOAT_PARAM(list, name, uValue) \
57 AddParamValue(list, name, NEW_PARAM(FLOAT_PARAM, float_v, uValue, 0))
58 #define ADD_FLOAT_ARR_PARAM(list, name, uValue, len) \
59 AddParamValue(list, name, NEW_PARAM(FLOAT_ARR_PARAM, float_arr_v, uValue, len))
60 #define ADD_DOUBLE_PARAM(list, name, uValue) \
61 AddParamValue(list, name, NEW_PARAM(DOUBLE_PARAM, double_v, uValue, 0))
62 #define ADD_DOUBLE_ARR_PARAM(list, name, uValue, len) \
63 AddParamValue(list, name, NEW_PARAM(DOUBLE_ARR_PARAM, double_arr_v, uValue, len))
64 #define ADD_STRING_PARAM(list, name, uValue) \
65 AddParamValue(list, name, NEW_PARAM(STRING_PARAM, str_v, uValue, 0))
66 #define ADD_STRING_ARR_PARAM(list, name, uValue, len) \
67 AddParamValue(list, name, NEW_PARAM(STRING_ARR_PARAM, str_arr_v, uValue, len))
68
CreateParamEntry(const char * name,ParamValue * value)69 ParamEntry* CreateParamEntry(const char* name, ParamValue* value)
70 {
71 ParamEntry* entry = NEW(ParamEntry);
72 if (entry != NULL) {
73 entry->name = name;
74 entry->value = value;
75 }
76 return entry;
77 }
78
OH_HiAppEvent_CreateParamList()79 ParamList OH_HiAppEvent_CreateParamList()
80 {
81 ParamList list = NEW(ParamListNode);
82 if (list != NULL) {
83 list->entry = NULL;
84 list->next = NULL;
85 }
86 return list;
87 }
88
DestroyParamValue(ParamValue * value)89 void DestroyParamValue(ParamValue* value)
90 {
91 if (value != NULL) {
92 free(value);
93 }
94 }
95
DestroyParamEntry(ParamEntry * entry)96 void DestroyParamEntry(ParamEntry* entry)
97 {
98 if (entry != NULL) {
99 DestroyParamValue(entry->value);
100 free(entry);
101 }
102 }
103
OH_HiAppEvent_DestroyParamList(ParamList list)104 void OH_HiAppEvent_DestroyParamList(ParamList list)
105 {
106 ParamList curNode = list;
107 while (curNode != NULL) {
108 ParamList nextNode = curNode->next;
109 DestroyParamEntry(curNode->entry);
110 free(curNode);
111 curNode = nextNode;
112 }
113 }
114
AddParamValue(ParamList list,const char * name,ParamValue * value)115 ParamList AddParamValue(ParamList list, const char* name, ParamValue* value)
116 {
117 // return the list if the ParamValue is null(list and name have been checked)
118 if (value == NULL) {
119 return list;
120 }
121
122 // if the param is the first one added, create the head node of the list
123 if (list->entry == NULL) {
124 ParamEntry* entry = CreateParamEntry(name, value);
125 if (entry == NULL) {
126 DestroyParamValue(value);
127 return list;
128 }
129 list->entry = entry;
130 return list;
131 }
132
133 ParamList curNode = list;
134 while (curNode != NULL) {
135 if (curNode->next == NULL) {
136 // if the curNode is the last node, create a tail node
137 ParamListNode* node = OH_HiAppEvent_CreateParamList();
138 if (node == NULL) {
139 DestroyParamValue(value);
140 return list;
141 }
142 ParamEntry* entry = CreateParamEntry(name, value);
143 if (entry == NULL) {
144 DestroyParamValue(value);
145 OH_HiAppEvent_DestroyParamList(node);
146 return list;
147 }
148 node->entry = entry;
149 node->next = NULL;
150 curNode->next = node;
151 return list;
152 } else {
153 // otherwise, find the next Node
154 curNode = curNode->next;
155 }
156 }
157 return list;
158 }
159
OH_HiAppEvent_AddBoolParam(ParamList list,const char * name,bool boolean)160 ParamList OH_HiAppEvent_AddBoolParam(ParamList list, const char* name, bool boolean)
161 {
162 if (list == NULL || name == NULL) {
163 return list;
164 }
165 return ADD_BOOL_PARAM(list, name, boolean);
166 }
167
OH_HiAppEvent_AddBoolArrayParam(ParamList list,const char * name,const bool * booleans,int arrSize)168 ParamList OH_HiAppEvent_AddBoolArrayParam(ParamList list, const char* name, const bool* booleans, int arrSize)
169 {
170 if (list == NULL || name == NULL || booleans == NULL) {
171 return list;
172 }
173 return ADD_BOOL_ARR_PARAM(list, name, booleans, arrSize);
174 }
175
OH_HiAppEvent_AddInt8Param(ParamList list,const char * name,int8_t num)176 ParamList OH_HiAppEvent_AddInt8Param(ParamList list, const char* name, int8_t num)
177 {
178 if (list == NULL || name == NULL) {
179 return list;
180 }
181 return ADD_INT8_PARAM(list, name, num);
182 }
183
OH_HiAppEvent_AddInt8ArrayParam(ParamList list,const char * name,const int8_t * nums,int arrSize)184 ParamList OH_HiAppEvent_AddInt8ArrayParam(ParamList list, const char* name, const int8_t* nums, int arrSize)
185 {
186 if (list == NULL || name == NULL || nums == NULL) {
187 return list;
188 }
189 return ADD_INT8_ARR_PARAM(list, name, nums, arrSize);
190 }
191
OH_HiAppEvent_AddInt16Param(ParamList list,const char * name,int16_t num)192 ParamList OH_HiAppEvent_AddInt16Param(ParamList list, const char* name, int16_t num)
193 {
194 if (list == NULL || name == NULL) {
195 return list;
196 }
197 return ADD_INT16_PARAM(list, name, num);
198 }
199
OH_HiAppEvent_AddInt16ArrayParam(ParamList list,const char * name,const int16_t * nums,int arrSize)200 ParamList OH_HiAppEvent_AddInt16ArrayParam(ParamList list, const char* name, const int16_t* nums, int arrSize)
201 {
202 if (list == NULL || name == NULL || nums == NULL) {
203 return list;
204 }
205 return ADD_INT16_ARR_PARAM(list, name, nums, arrSize);
206 }
207
OH_HiAppEvent_AddInt32Param(ParamList list,const char * name,int32_t num)208 ParamList OH_HiAppEvent_AddInt32Param(ParamList list, const char* name, int32_t num)
209 {
210 if (list == NULL || name == NULL) {
211 return list;
212 }
213 return ADD_INT32_PARAM(list, name, num);
214 }
215
OH_HiAppEvent_AddInt32ArrayParam(ParamList list,const char * name,const int32_t * nums,int arrSize)216 ParamList OH_HiAppEvent_AddInt32ArrayParam(ParamList list, const char* name, const int32_t* nums, int arrSize)
217 {
218 if (list == NULL || name == NULL || nums == NULL) {
219 return list;
220 }
221 return ADD_INT32_ARR_PARAM(list, name, nums, arrSize);
222 }
223
OH_HiAppEvent_AddInt64Param(ParamList list,const char * name,int64_t num)224 ParamList OH_HiAppEvent_AddInt64Param(ParamList list, const char* name, int64_t num)
225 {
226 if (list == NULL || name == NULL) {
227 return list;
228 }
229 return ADD_INT64_PARAM(list, name, num);
230 }
231
OH_HiAppEvent_AddInt64ArrayParam(ParamList list,const char * name,const int64_t * nums,int arrSize)232 ParamList OH_HiAppEvent_AddInt64ArrayParam(ParamList list, const char* name, const int64_t* nums, int arrSize)
233 {
234 if (list == NULL || name == NULL || nums == NULL) {
235 return list;
236 }
237 return ADD_INT64_ARR_PARAM(list, name, nums, arrSize);
238 }
239
OH_HiAppEvent_AddFloatParam(ParamList list,const char * name,float num)240 ParamList OH_HiAppEvent_AddFloatParam(ParamList list, const char* name, float num)
241 {
242 if (list == NULL || name == NULL) {
243 return list;
244 }
245 return ADD_FLOAT_PARAM(list, name, num);
246 }
247
OH_HiAppEvent_AddFloatArrayParam(ParamList list,const char * name,const float * nums,int arrSize)248 ParamList OH_HiAppEvent_AddFloatArrayParam(ParamList list, const char* name, const float* nums, int arrSize)
249 {
250 if (list == NULL || name == NULL || nums == NULL) {
251 return list;
252 }
253 return ADD_FLOAT_ARR_PARAM(list, name, nums, arrSize);
254 }
255
OH_HiAppEvent_AddDoubleParam(ParamList list,const char * name,double num)256 ParamList OH_HiAppEvent_AddDoubleParam(ParamList list, const char* name, double num)
257 {
258 if (list == NULL || name == NULL) {
259 return list;
260 }
261 return ADD_DOUBLE_PARAM(list, name, num);
262 }
263
OH_HiAppEvent_AddDoubleArrayParam(ParamList list,const char * name,const double * nums,int arrSize)264 ParamList OH_HiAppEvent_AddDoubleArrayParam(ParamList list, const char* name, const double* nums, int arrSize)
265 {
266 if (list == NULL || name == NULL || nums == NULL) {
267 return list;
268 }
269 return ADD_DOUBLE_ARR_PARAM(list, name, nums, arrSize);
270 }
271
OH_HiAppEvent_AddStringParam(ParamList list,const char * name,const char * str)272 ParamList OH_HiAppEvent_AddStringParam(ParamList list, const char* name, const char* str)
273 {
274 if (list == NULL || name == NULL || str == NULL) {
275 return list;
276 }
277 return ADD_STRING_PARAM(list, name, str);
278 }
279
OH_HiAppEvent_AddStringArrayParam(ParamList list,const char * name,const char * const * strs,int arrSize)280 ParamList OH_HiAppEvent_AddStringArrayParam(ParamList list, const char* name, const char* const *strs, int arrSize)
281 {
282 if (list == NULL || name == NULL || strs == NULL) {
283 return list;
284 }
285 return ADD_STRING_ARR_PARAM(list, name, strs, arrSize);
286 }
287
OH_HiAppEvent_Configure(const char * name,const char * value)288 bool OH_HiAppEvent_Configure(const char* name, const char* value)
289 {
290 return HiAppEventInnerConfigure(name, value);
291 }
292
OH_HiAppEvent_CreateWatcher(const char * name)293 struct HiAppEvent_Watcher* OH_HiAppEvent_CreateWatcher(const char *name)
294 {
295 return CreateWatcher(name);
296 }
297
OH_HiAppEvent_SetAppEventFilter(struct HiAppEvent_Watcher * watcher,const char * domain,uint8_t eventTypes,const char * const * names,int namesLen)298 int OH_HiAppEvent_SetAppEventFilter(struct HiAppEvent_Watcher *watcher, const char *domain, uint8_t eventTypes,
299 const char *const *names, int namesLen)
300 {
301 return SetAppEventFilter(watcher, domain, eventTypes, names, namesLen);
302 }
303
OH_HiAppEvent_SetTriggerCondition(struct HiAppEvent_Watcher * watcher,int row,int size,int timeOut)304 int OH_HiAppEvent_SetTriggerCondition(struct HiAppEvent_Watcher* watcher, int row, int size, int timeOut)
305 {
306 return SetTriggerCondition(watcher, row, size, timeOut);
307 }
308
OH_HiAppEvent_SetWatcherOnTrigger(struct HiAppEvent_Watcher * watcher,OH_HiAppEvent_OnTrigger onTrigger)309 int OH_HiAppEvent_SetWatcherOnTrigger(struct HiAppEvent_Watcher *watcher, OH_HiAppEvent_OnTrigger onTrigger)
310 {
311 return SetWatcherOnTrigger(watcher, onTrigger);
312 }
313
OH_HiAppEvent_SetWatcherOnReceive(struct HiAppEvent_Watcher * watcher,OH_HiAppEvent_OnReceive onReceiver)314 int OH_HiAppEvent_SetWatcherOnReceive(struct HiAppEvent_Watcher *watcher, OH_HiAppEvent_OnReceive onReceiver)
315 {
316 return SetWatcherOnReceiver(watcher, onReceiver);
317 }
318
OH_HiAppEvent_AddWatcher(struct HiAppEvent_Watcher * watcher)319 int OH_HiAppEvent_AddWatcher(struct HiAppEvent_Watcher* watcher)
320 {
321 return AddWatcher(watcher);
322 }
323
OH_HiAppEvent_TakeWatcherData(struct HiAppEvent_Watcher * watcher,uint32_t size,OH_HiAppEvent_OnTake onTake)324 int OH_HiAppEvent_TakeWatcherData(struct HiAppEvent_Watcher *watcher, uint32_t size, OH_HiAppEvent_OnTake onTake)
325 {
326 return TakeWatcherData(watcher, size, onTake);
327 }
328
OH_HiAppEvent_ClearData()329 void OH_HiAppEvent_ClearData()
330 {
331 ClearData();
332 }
333
OH_HiAppEvent_RemoveWatcher(struct HiAppEvent_Watcher * watcher)334 int OH_HiAppEvent_RemoveWatcher(struct HiAppEvent_Watcher *watcher)
335 {
336 return RemoveWatcher(watcher);
337 }
338
OH_HiAppEvent_DestroyWatcher(struct HiAppEvent_Watcher * watcher)339 void OH_HiAppEvent_DestroyWatcher(struct HiAppEvent_Watcher *watcher)
340 {
341 DestroyWatcher(watcher);
342 }
343
OH_HiAppEvent_Write(const char * domain,const char * name,enum EventType type,const ParamList list)344 int OH_HiAppEvent_Write(const char* domain, const char* name, enum EventType type, const ParamList list)
345 {
346 return HiAppEventInnerWrite(domain, name, type, list);
347 }
348
OH_HiAppEvent_CreateProcessor(const char * name)349 struct HiAppEvent_Processor* OH_HiAppEvent_CreateProcessor(const char* name)
350 {
351 return CreateProcessor(name);
352 }
353
OH_HiAppEvent_SetReportRoute(struct HiAppEvent_Processor * processor,const char * appId,const char * routeInfo)354 int OH_HiAppEvent_SetReportRoute(struct HiAppEvent_Processor* processor, const char* appId, const char* routeInfo)
355 {
356 return SetReportRoute(processor, appId, routeInfo);
357 }
358
OH_HiAppEvent_SetReportPolicy(struct HiAppEvent_Processor * processor,int periodReport,int batchReport,bool onStartReport,bool onBackgroundReport)359 int OH_HiAppEvent_SetReportPolicy(struct HiAppEvent_Processor* processor, int periodReport, int batchReport,
360 bool onStartReport, bool onBackgroundReport)
361 {
362 return SetReportPolicy(processor, periodReport, batchReport, onStartReport, onBackgroundReport);
363 }
364
OH_HiAppEvent_SetReportEvent(struct HiAppEvent_Processor * processor,const char * domain,const char * name,bool isRealTime)365 int OH_HiAppEvent_SetReportEvent(struct HiAppEvent_Processor* processor, const char* domain, const char* name,
366 bool isRealTime)
367 {
368 return SetReportEvent(processor, domain, name, isRealTime);
369 }
370
OH_HiAppEvent_SetCustomConfig(struct HiAppEvent_Processor * processor,const char * key,const char * value)371 int OH_HiAppEvent_SetCustomConfig(struct HiAppEvent_Processor* processor, const char* key, const char* value)
372 {
373 return SetCustomConfig(processor, key, value);
374 }
375
OH_HiAppEvent_SetConfigId(struct HiAppEvent_Processor * processor,int configId)376 int OH_HiAppEvent_SetConfigId(struct HiAppEvent_Processor* processor, int configId)
377 {
378 return SetConfigId(processor, configId);
379 }
380
OH_HiAppEvent_SetReportUserId(struct HiAppEvent_Processor * processor,const char * const * userIdNames,int size)381 int OH_HiAppEvent_SetReportUserId(struct HiAppEvent_Processor* processor, const char* const * userIdNames, int size)
382 {
383 return SetReportUserId(processor, userIdNames, size);
384 }
385
OH_HiAppEvent_SetReportUserProperty(struct HiAppEvent_Processor * processor,const char * const * userPropertyNames,int size)386 int OH_HiAppEvent_SetReportUserProperty(struct HiAppEvent_Processor* processor, const char* const * userPropertyNames,
387 int size)
388 {
389 return SetReportUserProperty(processor, userPropertyNames, size);
390 }
391
OH_HiAppEvent_AddProcessor(struct HiAppEvent_Processor * processor)392 int64_t OH_HiAppEvent_AddProcessor(struct HiAppEvent_Processor* processor)
393 {
394 return AddProcessor(processor);
395 }
396
OH_HiAppEvent_DestoryProcessor(struct HiAppEvent_Processor * processor)397 void OH_HiAppEvent_DestoryProcessor(struct HiAppEvent_Processor* processor)
398 {
399 DestoryProcessor(processor);
400 }
401
OH_HiAppEvent_RemoveProcessor(int64_t processorId)402 int OH_HiAppEvent_RemoveProcessor(int64_t processorId)
403 {
404 return RemoveProcessor(processorId);
405 }