• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdbool.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "hiappevent_c.h"
22 
23 #define NEW(type) (type*)malloc(sizeof(type))
24 #define NEW_PARAM(uType, uName, uValue, len) ({ \
25     ParamValue* pValue = NEW(ParamValue);  \
26     if (pValue != NULL) {   \
27         pValue->type = uType;    \
28         pValue->value.uName = uValue; \
29         pValue->arrSize = len;  \
30     }   \
31     pValue; \
32 })
33 
34 #define ADD_BOOL_PARAM(list, name, uValue)  \
35     AddParamValue(list, name, NEW_PARAM(BOOL_PARAM, bool_v, uValue, 0))
36 #define ADD_BOOL_ARR_PARAM(list, name, uValue, len) \
37     AddParamValue(list, name, NEW_PARAM(BOOL_ARR_PARAM, bool_arr_v, uValue, len))
38 #define ADD_INT8_PARAM(list, name, uValue)  \
39     AddParamValue(list, name, NEW_PARAM(INT8_PARAM, int8_v, uValue, 0))
40 #define ADD_INT8_ARR_PARAM(list, name, uValue, len) \
41     AddParamValue(list, name, NEW_PARAM(INT8_ARR_PARAM, int8_arr_v, uValue, len))
42 #define ADD_INT16_PARAM(list, name, uValue)  \
43     AddParamValue(list, name, NEW_PARAM(INT16_PARAM, int16_v, uValue, 0))
44 #define ADD_INT16_ARR_PARAM(list, name, uValue, len) \
45     AddParamValue(list, name, NEW_PARAM(INT16_ARR_PARAM, int16_arr_v, uValue, len))
46 #define ADD_INT32_PARAM(list, name, uValue)  \
47     AddParamValue(list, name, NEW_PARAM(INT32_PARAM, int32_v, uValue, 0))
48 #define ADD_INT32_ARR_PARAM(list, name, uValue, len) \
49     AddParamValue(list, name, NEW_PARAM(INT32_ARR_PARAM, int32_arr_v, uValue, len))
50 #define ADD_INT64_PARAM(list, name, uValue)  \
51     AddParamValue(list, name, NEW_PARAM(INT64_PARAM, int64_v, uValue, 0))
52 #define ADD_INT64_ARR_PARAM(list, name, uValue, len) \
53     AddParamValue(list, name, NEW_PARAM(INT64_ARR_PARAM, int64_arr_v, uValue, len))
54 #define ADD_FLOAT_PARAM(list, name, uValue)  \
55     AddParamValue(list, name, NEW_PARAM(FLOAT_PARAM, float_v, uValue, 0))
56 #define ADD_FLOAT_ARR_PARAM(list, name, uValue, len) \
57     AddParamValue(list, name, NEW_PARAM(FLOAT_ARR_PARAM, float_arr_v, uValue, len))
58 #define ADD_DOUBLE_PARAM(list, name, uValue)  \
59     AddParamValue(list, name, NEW_PARAM(DOUBLE_PARAM, double_v, uValue, 0))
60 #define ADD_DOUBLE_ARR_PARAM(list, name, uValue, len) \
61     AddParamValue(list, name, NEW_PARAM(DOUBLE_ARR_PARAM, double_arr_v, uValue, len))
62 #define ADD_STRING_PARAM(list, name, uValue)  \
63     AddParamValue(list, name, NEW_PARAM(STRING_PARAM, str_v, uValue, 0))
64 #define ADD_STRING_ARR_PARAM(list, name, uValue, len) \
65     AddParamValue(list, name, NEW_PARAM(STRING_ARR_PARAM, str_arr_v, uValue, len))
66 
CreateParamEntry(const char * name,ParamValue * value)67 ParamEntry* CreateParamEntry(const char* name, ParamValue* value)
68 {
69     ParamEntry* entry = NEW(ParamEntry);
70     if (entry != NULL) {
71         entry->name = name;
72         entry->value = value;
73     }
74     return entry;
75 }
76 
OH_HiAppEvent_CreateParamList()77 ParamList OH_HiAppEvent_CreateParamList()
78 {
79     ParamList list = NEW(ParamListNode);
80     if (list != NULL) {
81         list->entry = NULL;
82         list->next = NULL;
83     }
84     return list;
85 }
86 
DestroyParamValue(ParamValue * value)87 void DestroyParamValue(ParamValue* value)
88 {
89     if (value != NULL) {
90         free(value);
91     }
92 }
93 
DestroyParamEntry(ParamEntry * entry)94 void DestroyParamEntry(ParamEntry* entry)
95 {
96     if (entry != NULL) {
97         DestroyParamValue(entry->value);
98         free(entry);
99     }
100 }
101 
OH_HiAppEvent_DestroyParamList(ParamList list)102 void OH_HiAppEvent_DestroyParamList(ParamList list)
103 {
104     ParamList curNode = list;
105     while (curNode != NULL) {
106         ParamList nextNode = curNode->next;
107         DestroyParamEntry(curNode->entry);
108         free(curNode);
109         curNode = nextNode;
110     }
111 }
112 
AddParamValue(ParamList list,const char * name,ParamValue * value)113 ParamList AddParamValue(ParamList list, const char* name, ParamValue* value)
114 {
115     // return the list if the ParamValue is null(list and name have been checked)
116     if (value == NULL) {
117         return list;
118     }
119 
120     // if the param is the first one added, create the head node of the list
121     if (list->entry == NULL) {
122         ParamEntry* entry = CreateParamEntry(name, value);
123         if (entry == NULL) {
124             DestroyParamValue(value);
125             return list;
126         }
127         list->entry = entry;
128         return list;
129     }
130 
131     ParamList curNode = list;
132     while (curNode != NULL) {
133         if (curNode->entry != NULL && (strcmp(name, curNode->entry->name) == 0)) {
134             // if the names of the params are same, overwrite it
135             ParamValue* oldValue = curNode->entry->value;
136             curNode->entry->value = value;
137             DestroyParamValue(oldValue);
138             return list;
139         } else if (curNode->next == NULL) {
140             // if the curNode is the last node, create a tail node
141             ParamListNode* node = OH_HiAppEvent_CreateParamList();
142             if (node == NULL) {
143                 DestroyParamValue(value);
144                 return list;
145             }
146             ParamEntry* entry = CreateParamEntry(name, value);
147             if (entry == NULL) {
148                 DestroyParamValue(value);
149                 OH_HiAppEvent_DestroyParamList(node);
150                 return list;
151             }
152             node->entry = entry;
153             node->next = NULL;
154             curNode->next = node;
155             return list;
156         } else {
157             // otherwise, find the next Node
158             curNode = curNode->next;
159         }
160     }
161     return list;
162 }
163 
OH_HiAppEvent_AddBoolParam(ParamList list,const char * name,bool boolean)164 ParamList OH_HiAppEvent_AddBoolParam(ParamList list, const char* name, bool boolean)
165 {
166     if (list == NULL || name == NULL) {
167         return list;
168     }
169     return ADD_BOOL_PARAM(list, name, boolean);
170 }
171 
OH_HiAppEvent_AddBoolArrayParam(ParamList list,const char * name,const bool * booleans,int arrSize)172 ParamList OH_HiAppEvent_AddBoolArrayParam(ParamList list, const char* name, const bool* booleans, int arrSize)
173 {
174     if (list == NULL || name == NULL || booleans == NULL) {
175         return list;
176     }
177     return ADD_BOOL_ARR_PARAM(list, name, booleans, arrSize);
178 }
179 
OH_HiAppEvent_AddInt8Param(ParamList list,const char * name,int8_t num)180 ParamList OH_HiAppEvent_AddInt8Param(ParamList list, const char* name, int8_t num)
181 {
182     if (list == NULL || name == NULL) {
183         return list;
184     }
185     return ADD_INT8_PARAM(list, name, num);
186 }
187 
OH_HiAppEvent_AddInt8ArrayParam(ParamList list,const char * name,const int8_t * nums,int arrSize)188 ParamList OH_HiAppEvent_AddInt8ArrayParam(ParamList list, const char* name, const int8_t* nums, int arrSize)
189 {
190     if (list == NULL || name == NULL || nums == NULL) {
191         return list;
192     }
193     return ADD_INT8_ARR_PARAM(list, name, nums, arrSize);
194 }
195 
OH_HiAppEvent_AddInt16Param(ParamList list,const char * name,int16_t num)196 ParamList OH_HiAppEvent_AddInt16Param(ParamList list, const char* name, int16_t num)
197 {
198     if (list == NULL || name == NULL) {
199         return list;
200     }
201     return ADD_INT16_PARAM(list, name, num);
202 }
203 
OH_HiAppEvent_AddInt16ArrayParam(ParamList list,const char * name,const int16_t * nums,int arrSize)204 ParamList OH_HiAppEvent_AddInt16ArrayParam(ParamList list, const char* name, const int16_t* nums, int arrSize)
205 {
206     if (list == NULL || name == NULL || nums == NULL) {
207         return list;
208     }
209     return ADD_INT16_ARR_PARAM(list, name, nums, arrSize);
210 }
211 
OH_HiAppEvent_AddInt32Param(ParamList list,const char * name,int32_t num)212 ParamList OH_HiAppEvent_AddInt32Param(ParamList list, const char* name, int32_t num)
213 {
214     if (list == NULL || name == NULL) {
215         return list;
216     }
217     return ADD_INT32_PARAM(list, name, num);
218 }
219 
OH_HiAppEvent_AddInt32ArrayParam(ParamList list,const char * name,const int32_t * nums,int arrSize)220 ParamList OH_HiAppEvent_AddInt32ArrayParam(ParamList list, const char* name, const int32_t* nums, int arrSize)
221 {
222     if (list == NULL || name == NULL || nums == NULL) {
223         return list;
224     }
225     return ADD_INT32_ARR_PARAM(list, name, nums, arrSize);
226 }
227 
OH_HiAppEvent_AddInt64Param(ParamList list,const char * name,int64_t num)228 ParamList OH_HiAppEvent_AddInt64Param(ParamList list, const char* name, int64_t num)
229 {
230     if (list == NULL || name == NULL) {
231         return list;
232     }
233     return ADD_INT64_PARAM(list, name, num);
234 }
235 
OH_HiAppEvent_AddInt64ArrayParam(ParamList list,const char * name,const int64_t * nums,int arrSize)236 ParamList OH_HiAppEvent_AddInt64ArrayParam(ParamList list, const char* name, const int64_t* nums, int arrSize)
237 {
238     if (list == NULL || name == NULL || nums == NULL) {
239         return list;
240     }
241     return ADD_INT64_ARR_PARAM(list, name, nums, arrSize);
242 }
243 
OH_HiAppEvent_AddFloatParam(ParamList list,const char * name,float num)244 ParamList OH_HiAppEvent_AddFloatParam(ParamList list, const char* name, float num)
245 {
246     if (list == NULL || name == NULL) {
247         return list;
248     }
249     return ADD_FLOAT_PARAM(list, name, num);
250 }
251 
OH_HiAppEvent_AddFloatArrayParam(ParamList list,const char * name,const float * nums,int arrSize)252 ParamList OH_HiAppEvent_AddFloatArrayParam(ParamList list, const char* name, const float* nums, int arrSize)
253 {
254     if (list == NULL || name == NULL || nums == NULL) {
255         return list;
256     }
257     return ADD_FLOAT_ARR_PARAM(list, name, nums, arrSize);
258 }
259 
OH_HiAppEvent_AddDoubleParam(ParamList list,const char * name,double num)260 ParamList OH_HiAppEvent_AddDoubleParam(ParamList list, const char* name, double num)
261 {
262     if (list == NULL || name == NULL) {
263         return list;
264     }
265     return ADD_DOUBLE_PARAM(list, name, num);
266 }
267 
OH_HiAppEvent_AddDoubleArrayParam(ParamList list,const char * name,const double * nums,int arrSize)268 ParamList OH_HiAppEvent_AddDoubleArrayParam(ParamList list, const char* name, const double* nums, int arrSize)
269 {
270     if (list == NULL || name == NULL || nums == NULL) {
271         return list;
272     }
273     return ADD_DOUBLE_ARR_PARAM(list, name, nums, arrSize);
274 }
275 
OH_HiAppEvent_AddStringParam(ParamList list,const char * name,const char * str)276 ParamList OH_HiAppEvent_AddStringParam(ParamList list, const char* name, const char* str)
277 {
278     if (list == NULL || name == NULL || str == NULL) {
279         return list;
280     }
281     return ADD_STRING_PARAM(list, name, str);
282 }
283 
OH_HiAppEvent_AddStringArrayParam(ParamList list,const char * name,const char * const * strs,int arrSize)284 ParamList OH_HiAppEvent_AddStringArrayParam(ParamList list, const char* name, const char* const *strs, int arrSize)
285 {
286     if (list == NULL || name == NULL || strs == NULL) {
287         return list;
288     }
289     return ADD_STRING_ARR_PARAM(list, name, strs, arrSize);
290 }
291 
OH_HiAppEvent_Configure(const char * name,const char * value)292 bool OH_HiAppEvent_Configure(const char* name, const char* value)
293 {
294     return HiAppEventInnerConfigure(name, value);
295 }
296 
OH_HiAppEvent_Write(const char * domain,const char * name,enum EventType type,const ParamList list)297 int OH_HiAppEvent_Write(const char* domain, const char* name, enum EventType type, const ParamList list)
298 {
299     return HiAppEventInnerWrite(domain, name, type, list);
300 }