• 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->next == NULL) {
134             // if the curNode is the last node, create a tail node
135             ParamListNode* node = OH_HiAppEvent_CreateParamList();
136             if (node == NULL) {
137                 DestroyParamValue(value);
138                 return list;
139             }
140             ParamEntry* entry = CreateParamEntry(name, value);
141             if (entry == NULL) {
142                 DestroyParamValue(value);
143                 OH_HiAppEvent_DestroyParamList(node);
144                 return list;
145             }
146             node->entry = entry;
147             node->next = NULL;
148             curNode->next = node;
149             return list;
150         } else {
151             // otherwise, find the next Node
152             curNode = curNode->next;
153         }
154     }
155     return list;
156 }
157 
OH_HiAppEvent_AddBoolParam(ParamList list,const char * name,bool boolean)158 ParamList OH_HiAppEvent_AddBoolParam(ParamList list, const char* name, bool boolean)
159 {
160     if (list == NULL || name == NULL) {
161         return list;
162     }
163     return ADD_BOOL_PARAM(list, name, boolean);
164 }
165 
OH_HiAppEvent_AddBoolArrayParam(ParamList list,const char * name,const bool * booleans,int arrSize)166 ParamList OH_HiAppEvent_AddBoolArrayParam(ParamList list, const char* name, const bool* booleans, int arrSize)
167 {
168     if (list == NULL || name == NULL || booleans == NULL) {
169         return list;
170     }
171     return ADD_BOOL_ARR_PARAM(list, name, booleans, arrSize);
172 }
173 
OH_HiAppEvent_AddInt8Param(ParamList list,const char * name,int8_t num)174 ParamList OH_HiAppEvent_AddInt8Param(ParamList list, const char* name, int8_t num)
175 {
176     if (list == NULL || name == NULL) {
177         return list;
178     }
179     return ADD_INT8_PARAM(list, name, num);
180 }
181 
OH_HiAppEvent_AddInt8ArrayParam(ParamList list,const char * name,const int8_t * nums,int arrSize)182 ParamList OH_HiAppEvent_AddInt8ArrayParam(ParamList list, const char* name, const int8_t* nums, int arrSize)
183 {
184     if (list == NULL || name == NULL || nums == NULL) {
185         return list;
186     }
187     return ADD_INT8_ARR_PARAM(list, name, nums, arrSize);
188 }
189 
OH_HiAppEvent_AddInt16Param(ParamList list,const char * name,int16_t num)190 ParamList OH_HiAppEvent_AddInt16Param(ParamList list, const char* name, int16_t num)
191 {
192     if (list == NULL || name == NULL) {
193         return list;
194     }
195     return ADD_INT16_PARAM(list, name, num);
196 }
197 
OH_HiAppEvent_AddInt16ArrayParam(ParamList list,const char * name,const int16_t * nums,int arrSize)198 ParamList OH_HiAppEvent_AddInt16ArrayParam(ParamList list, const char* name, const int16_t* nums, int arrSize)
199 {
200     if (list == NULL || name == NULL || nums == NULL) {
201         return list;
202     }
203     return ADD_INT16_ARR_PARAM(list, name, nums, arrSize);
204 }
205 
OH_HiAppEvent_AddInt32Param(ParamList list,const char * name,int32_t num)206 ParamList OH_HiAppEvent_AddInt32Param(ParamList list, const char* name, int32_t num)
207 {
208     if (list == NULL || name == NULL) {
209         return list;
210     }
211     return ADD_INT32_PARAM(list, name, num);
212 }
213 
OH_HiAppEvent_AddInt32ArrayParam(ParamList list,const char * name,const int32_t * nums,int arrSize)214 ParamList OH_HiAppEvent_AddInt32ArrayParam(ParamList list, const char* name, const int32_t* nums, int arrSize)
215 {
216     if (list == NULL || name == NULL || nums == NULL) {
217         return list;
218     }
219     return ADD_INT32_ARR_PARAM(list, name, nums, arrSize);
220 }
221 
OH_HiAppEvent_AddInt64Param(ParamList list,const char * name,int64_t num)222 ParamList OH_HiAppEvent_AddInt64Param(ParamList list, const char* name, int64_t num)
223 {
224     if (list == NULL || name == NULL) {
225         return list;
226     }
227     return ADD_INT64_PARAM(list, name, num);
228 }
229 
OH_HiAppEvent_AddInt64ArrayParam(ParamList list,const char * name,const int64_t * nums,int arrSize)230 ParamList OH_HiAppEvent_AddInt64ArrayParam(ParamList list, const char* name, const int64_t* nums, int arrSize)
231 {
232     if (list == NULL || name == NULL || nums == NULL) {
233         return list;
234     }
235     return ADD_INT64_ARR_PARAM(list, name, nums, arrSize);
236 }
237 
OH_HiAppEvent_AddFloatParam(ParamList list,const char * name,float num)238 ParamList OH_HiAppEvent_AddFloatParam(ParamList list, const char* name, float num)
239 {
240     if (list == NULL || name == NULL) {
241         return list;
242     }
243     return ADD_FLOAT_PARAM(list, name, num);
244 }
245 
OH_HiAppEvent_AddFloatArrayParam(ParamList list,const char * name,const float * nums,int arrSize)246 ParamList OH_HiAppEvent_AddFloatArrayParam(ParamList list, const char* name, const float* nums, int arrSize)
247 {
248     if (list == NULL || name == NULL || nums == NULL) {
249         return list;
250     }
251     return ADD_FLOAT_ARR_PARAM(list, name, nums, arrSize);
252 }
253 
OH_HiAppEvent_AddDoubleParam(ParamList list,const char * name,double num)254 ParamList OH_HiAppEvent_AddDoubleParam(ParamList list, const char* name, double num)
255 {
256     if (list == NULL || name == NULL) {
257         return list;
258     }
259     return ADD_DOUBLE_PARAM(list, name, num);
260 }
261 
OH_HiAppEvent_AddDoubleArrayParam(ParamList list,const char * name,const double * nums,int arrSize)262 ParamList OH_HiAppEvent_AddDoubleArrayParam(ParamList list, const char* name, const double* nums, int arrSize)
263 {
264     if (list == NULL || name == NULL || nums == NULL) {
265         return list;
266     }
267     return ADD_DOUBLE_ARR_PARAM(list, name, nums, arrSize);
268 }
269 
OH_HiAppEvent_AddStringParam(ParamList list,const char * name,const char * str)270 ParamList OH_HiAppEvent_AddStringParam(ParamList list, const char* name, const char* str)
271 {
272     if (list == NULL || name == NULL || str == NULL) {
273         return list;
274     }
275     return ADD_STRING_PARAM(list, name, str);
276 }
277 
OH_HiAppEvent_AddStringArrayParam(ParamList list,const char * name,const char * const * strs,int arrSize)278 ParamList OH_HiAppEvent_AddStringArrayParam(ParamList list, const char* name, const char* const *strs, int arrSize)
279 {
280     if (list == NULL || name == NULL || strs == NULL) {
281         return list;
282     }
283     return ADD_STRING_ARR_PARAM(list, name, strs, arrSize);
284 }
285 
OH_HiAppEvent_Configure(const char * name,const char * value)286 bool OH_HiAppEvent_Configure(const char* name, const char* value)
287 {
288     return HiAppEventInnerConfigure(name, value);
289 }
290 
OH_HiAppEvent_Write(const char * domain,const char * name,enum EventType type,const ParamList list)291 int OH_HiAppEvent_Write(const char* domain, const char* name, enum EventType type, const ParamList list)
292 {
293     return HiAppEventInnerWrite(domain, name, type, list);
294 }