• 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 "napi/native_api.h"
17 #include "hiappevent/hiappevent.h"
18 #include <cstring>
19 #include <vector>
20 #include <string>
21 
22 #define FAIL (-1)
23 #define SUCCESS 0
24 #define PARAM_0 0
25 #define PARAM_1 1
26 
27 #undef LOG_TAG
28 #define LOG_TAG "testTag"
29 
30 const float FLOAT_123_22 = 123.22f;
31 const float FLOAT_234_5 = 234.5f;
32 const float FLOAT_131312_46464 = 131312.46464f;
33 const double DOUBLE_123_22 = 123.22;
34 const double DOUBLE_30949_374 = 30949.374;
35 const double DOUBLE_131312_46464 = 131312.46464;
36 
37 static struct HiAppEvent_Watcher *g_watcher;
38 
OnReceiver(const char * domain,const struct HiAppEvent_AppEventGroup * appEventGroups,uint32_t groupSize)39 static void OnReceiver(const char *domain, const struct HiAppEvent_AppEventGroup *appEventGroups, uint32_t groupSize)
40 {
41 }
42 
OnTrigger(int row,int size)43 static void OnTrigger(int row, int size)
44 {
45 };
46 
OnTake(const char * const * events,uint32_t eventLen)47 static void OnTake(const char *const *events, uint32_t eventLen)
48 {
49 }
50 
OHHiAppEventWrite(napi_env env,napi_callback_info info)51 static napi_value OHHiAppEventWrite(napi_env env, napi_callback_info info)
52 {
53     ParamList list = OH_HiAppEvent_CreateParamList();
54     int32_t num = PARAM_1;
55     OH_HiAppEvent_AddInt32Param(list, "int32_key", num);
56     int32_t numArray[] = {PARAM_1, INT32_MAX, INT32_MIN};
57     OH_HiAppEvent_AddInt32ArrayParam(list, "int32_arr_key", numArray, sizeof(numArray) / sizeof(numArray[0]));
58     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
59     napi_value result = nullptr;
60     napi_create_int32(env, ret, &result);
61     return result;
62 }
63 
OHHiAppEventConfigure(napi_env env,napi_callback_info info)64 static napi_value OHHiAppEventConfigure(napi_env env, napi_callback_info info)
65 {
66     ParamList list = OH_HiAppEvent_CreateParamList();
67     int32_t num = PARAM_1;
68     OH_HiAppEvent_AddInt32Param(list, "int32_key", num);
69     int ret = -PARAM_1;
70     bool configure = OH_HiAppEvent_Configure(DISABLE, "true");
71     if (configure) {
72         ret = SUCCESS;
73     }
74     OH_HiAppEvent_Configure(DISABLE, "false");
75     napi_value result = nullptr;
76     napi_create_int32(env, ret, &result);
77     return result;
78 }
79 
OHHiAppEventAddBoolParam(napi_env env,napi_callback_info info)80 static napi_value OHHiAppEventAddBoolParam(napi_env env, napi_callback_info info)
81 {
82     ParamList list = OH_HiAppEvent_CreateParamList();
83     OH_HiAppEvent_AddBoolParam(list, "bool_key", true);
84     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
85     napi_value result = nullptr;
86     napi_create_int32(env, ret, &result);
87     return result;
88 }
89 
OHHiAppEventAddBoolArrayParam(napi_env env,napi_callback_info info)90 static napi_value OHHiAppEventAddBoolArrayParam(napi_env env, napi_callback_info info)
91 {
92     ParamList list = OH_HiAppEvent_CreateParamList();
93     bool nums[] = {true};
94     OH_HiAppEvent_AddBoolArrayParam(list, "bool_arr_key", nums, sizeof(nums) / sizeof(nums[0]));
95     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
96     napi_value result = nullptr;
97     napi_create_int32(env, ret, &result);
98     return result;
99 }
100 
OHHiAppEventAddInt8Param(napi_env env,napi_callback_info info)101 static napi_value OHHiAppEventAddInt8Param(napi_env env, napi_callback_info info)
102 {
103     ParamList list = OH_HiAppEvent_CreateParamList();
104     int8_t num = PARAM_1;
105     OH_HiAppEvent_AddInt8Param(list, "int8_key", num);
106     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
107     napi_value result = nullptr;
108     napi_create_int32(env, ret, &result);
109     return result;
110 }
111 
OHHiAppEventAddInt8ArrayParam(napi_env env,napi_callback_info info)112 static napi_value OHHiAppEventAddInt8ArrayParam(napi_env env, napi_callback_info info)
113 {
114     ParamList list = OH_HiAppEvent_CreateParamList();
115     int8_t num[] = {PARAM_1, INT8_MAX, INT8_MIN};
116     OH_HiAppEvent_AddInt8ArrayParam(list, "int8_key", num, sizeof(num) / sizeof(num[0]));
117     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
118     napi_value result = nullptr;
119     napi_create_int32(env, ret, &result);
120     return result;
121 }
122 
OHHiAppEventAddInt16Param(napi_env env,napi_callback_info info)123 static napi_value OHHiAppEventAddInt16Param(napi_env env, napi_callback_info info)
124 {
125     ParamList list = OH_HiAppEvent_CreateParamList();
126     int16_t num = PARAM_1;
127     OH_HiAppEvent_AddInt16Param(list, "int16_key", num);
128     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
129     napi_value result = nullptr;
130     napi_create_int32(env, ret, &result);
131     return result;
132 }
133 
OHHiAppEventAddInt16ArrayParam(napi_env env,napi_callback_info info)134 static napi_value OHHiAppEventAddInt16ArrayParam(napi_env env, napi_callback_info info)
135 {
136     ParamList list = OH_HiAppEvent_CreateParamList();
137     int16_t num[] = {PARAM_1, INT16_MAX, INT16_MIN};
138     OH_HiAppEvent_AddInt16ArrayParam(list, "int16_key", num, sizeof(num) / sizeof(num[0]));
139     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
140     napi_value result = nullptr;
141     napi_create_int32(env, ret, &result);
142     return result;
143 }
144 
OHHiAppEventAddInt32Param(napi_env env,napi_callback_info info)145 static napi_value OHHiAppEventAddInt32Param(napi_env env, napi_callback_info info)
146 {
147     ParamList list = OH_HiAppEvent_CreateParamList();
148     int32_t num = PARAM_1;
149     OH_HiAppEvent_AddInt32Param(list, "int32_key", num);
150     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
151     napi_value result = nullptr;
152     napi_create_int32(env, ret, &result);
153     return result;
154 }
155 
OHHiAppEventAddInt32ArrayParam(napi_env env,napi_callback_info info)156 static napi_value OHHiAppEventAddInt32ArrayParam(napi_env env, napi_callback_info info)
157 {
158     ParamList list = OH_HiAppEvent_CreateParamList();
159     int32_t num[] = {PARAM_1, INT32_MAX, INT32_MIN};
160     OH_HiAppEvent_AddInt32ArrayParam(list, "int32_key", num, sizeof(num) / sizeof(num[0]));
161     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
162     napi_value result = nullptr;
163     napi_create_int32(env, ret, &result);
164     return result;
165 }
166 
OHHiAppEventAddInt64Param(napi_env env,napi_callback_info info)167 static napi_value OHHiAppEventAddInt64Param(napi_env env, napi_callback_info info)
168 {
169     ParamList list = OH_HiAppEvent_CreateParamList();
170     int64_t num = PARAM_1;
171     OH_HiAppEvent_AddInt64Param(list, "int64_key", num);
172     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
173     napi_value result = nullptr;
174     napi_create_int32(env, ret, &result);
175     return result;
176 }
177 
OHHiAppEventAddInt64ArrayParam(napi_env env,napi_callback_info info)178 static napi_value OHHiAppEventAddInt64ArrayParam(napi_env env, napi_callback_info info)
179 {
180     ParamList list = OH_HiAppEvent_CreateParamList();
181     int64_t num[] = {PARAM_1, INT64_MAX, INT64_MIN};
182     OH_HiAppEvent_AddInt64ArrayParam(list, "int64_key", num, sizeof(num) / sizeof(num[0]));
183     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
184     napi_value result = nullptr;
185     napi_create_int32(env, ret, &result);
186     return result;
187 }
188 
OHHiAppEventAddFloatParam(napi_env env,napi_callback_info info)189 static napi_value OHHiAppEventAddFloatParam(napi_env env, napi_callback_info info)
190 {
191     ParamList list = OH_HiAppEvent_CreateParamList();
192     float num = FLOAT_234_5;
193     OH_HiAppEvent_AddFloatParam(list, "float_key", num);
194     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
195     napi_value result = nullptr;
196     napi_create_int32(env, ret, &result);
197     return result;
198 }
199 
OHHiAppEventAddFloatArrayParam(napi_env env,napi_callback_info info)200 static napi_value OHHiAppEventAddFloatArrayParam(napi_env env, napi_callback_info info)
201 {
202     ParamList list = OH_HiAppEvent_CreateParamList();
203     float num[] = {FLOAT_123_22, FLOAT_234_5, FLOAT_131312_46464};
204     OH_HiAppEvent_AddFloatArrayParam(list, "float_key", num, sizeof(num) / sizeof(num[0]));
205     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
206     napi_value result = nullptr;
207     napi_create_int32(env, ret, &result);
208     return result;
209 }
210 
OHHiAppEventAddDoubleParam(napi_env env,napi_callback_info info)211 static napi_value OHHiAppEventAddDoubleParam(napi_env env, napi_callback_info info)
212 {
213     ParamList list = OH_HiAppEvent_CreateParamList();
214     double num = DOUBLE_30949_374;
215     OH_HiAppEvent_AddDoubleParam(list, "double_key", num);
216     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
217     napi_value result = nullptr;
218     napi_create_int32(env, ret, &result);
219     return result;
220 }
221 
OHHiAppEventAddDoubleArrayParam(napi_env env,napi_callback_info info)222 static napi_value OHHiAppEventAddDoubleArrayParam(napi_env env, napi_callback_info info)
223 {
224     ParamList list = OH_HiAppEvent_CreateParamList();
225     double nums[] = {DOUBLE_123_22, DOUBLE_30949_374, DOUBLE_131312_46464};
226     OH_HiAppEvent_AddDoubleArrayParam(list, "double_arr_key", nums, sizeof(nums) / sizeof(nums[0]));
227     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
228     napi_value result = nullptr;
229     napi_create_int32(env, ret, &result);
230     return result;
231 }
232 
OHHiAppEventAddStringParam(napi_env env,napi_callback_info info)233 static napi_value OHHiAppEventAddStringParam(napi_env env, napi_callback_info info)
234 {
235     ParamList list = OH_HiAppEvent_CreateParamList();
236     char str[] = "hello";
237     OH_HiAppEvent_AddStringParam(list, "string_key", str);
238     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
239     napi_value result = nullptr;
240     napi_create_int32(env, ret, &result);
241     return result;
242 }
243 
OHHiAppEventAddStringArrayParam(napi_env env,napi_callback_info info)244 static napi_value OHHiAppEventAddStringArrayParam(napi_env env, napi_callback_info info)
245 {
246     ParamList list = OH_HiAppEvent_CreateParamList();
247     char str[] = "hello";
248     char par[] = "world";
249     char *strs[] = {str, par};
250     OH_HiAppEvent_AddStringArrayParam(list, "string_arr_key", strs, sizeof(strs) / sizeof(strs[0]));
251     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
252     napi_value result = nullptr;
253     napi_create_int32(env, ret, &result);
254     return result;
255 }
256 
OHHiAppEventCreateParamList(napi_env env,napi_callback_info info)257 static napi_value OHHiAppEventCreateParamList(napi_env env, napi_callback_info info)
258 {
259     ParamList list = OH_HiAppEvent_CreateParamList();
260     int ret = FAIL;
261     if (list != nullptr) {
262         ret = SUCCESS;
263     }
264     napi_value result = nullptr;
265     napi_create_int32(env, ret, &result);
266     return result;
267 }
268 
SetAppEventFilter(napi_env env,napi_callback_info info)269 static napi_value SetAppEventFilter(napi_env env, napi_callback_info info)
270 {
271     napi_value ret;
272     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
273     int32_t code = OH_HiAppEvent_SetAppEventFilter(g_watcher, "testDomain", 0b1000, {}, 0);
274     OH_HiAppEvent_DestroyWatcher(g_watcher);
275     napi_create_int32(env, code, &ret);
276     return ret;
277 }
278 
SetAppEventFilterInvName(napi_env env,napi_callback_info info)279 static napi_value SetAppEventFilterInvName(napi_env env, napi_callback_info info)
280 {
281     napi_value ret;
282     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
283     int32_t code = OH_HiAppEvent_SetAppEventFilter(g_watcher, "nullptr", 0b1000, {}, 1);
284     OH_HiAppEvent_DestroyWatcher(g_watcher);
285     napi_create_int32(env, code, &ret);
286     return ret;
287 }
288 
SetAppEventFilterInvDomain(napi_env env,napi_callback_info info)289 static napi_value SetAppEventFilterInvDomain(napi_env env, napi_callback_info info)
290 {
291     napi_value ret;
292     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
293     int32_t code = OH_HiAppEvent_SetAppEventFilter(g_watcher, nullptr, 0b1000, {}, 0);
294     OH_HiAppEvent_DestroyWatcher(g_watcher);
295     napi_create_int32(env, code, &ret);
296     return ret;
297 }
298 
SetAppEventFilterInvWatcher(napi_env env,napi_callback_info info)299 static napi_value SetAppEventFilterInvWatcher(napi_env env, napi_callback_info info)
300 {
301     napi_value ret;
302     g_watcher = OH_HiAppEvent_CreateWatcher(nullptr);
303     int32_t code = OH_HiAppEvent_SetAppEventFilter(g_watcher, "testDomain", 0b1000, {}, 0);
304     OH_HiAppEvent_DestroyWatcher(g_watcher);
305     napi_create_int32(env, code, &ret);
306     return ret;
307 }
308 
SetTriggerCondition(napi_env env,napi_callback_info info)309 static napi_value SetTriggerCondition(napi_env env, napi_callback_info info)
310 {
311     napi_value ret;
312     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
313     int32_t code = OH_HiAppEvent_SetTriggerCondition(g_watcher, 1, 0, 0);
314     OH_HiAppEvent_DestroyWatcher(g_watcher);
315     napi_create_int32(env, code, &ret);
316     return ret;
317 }
318 
SetTriggerConditionInvWatcher(napi_env env,napi_callback_info info)319 static napi_value SetTriggerConditionInvWatcher(napi_env env, napi_callback_info info)
320 {
321     napi_value ret;
322     g_watcher = OH_HiAppEvent_CreateWatcher(nullptr);
323     int32_t code = OH_HiAppEvent_SetTriggerCondition(g_watcher, 1, 0, 0);
324     OH_HiAppEvent_DestroyWatcher(g_watcher);
325     napi_create_int32(env, code, &ret);
326     return ret;
327 }
328 
SetWatcherOnTrigger(napi_env env,napi_callback_info info)329 static napi_value SetWatcherOnTrigger(napi_env env, napi_callback_info info)
330 {
331     napi_value ret;
332     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
333     int32_t code = OH_HiAppEvent_SetWatcherOnTrigger(g_watcher, OnTrigger);
334     OH_HiAppEvent_DestroyWatcher(g_watcher);
335     napi_create_int32(env, code, &ret);
336     return ret;
337 }
338 
SetWatcherOnTriggerInvWatcher(napi_env env,napi_callback_info info)339 static napi_value SetWatcherOnTriggerInvWatcher(napi_env env, napi_callback_info info)
340 {
341     napi_value ret;
342     g_watcher = OH_HiAppEvent_CreateWatcher(nullptr);
343     int32_t code = OH_HiAppEvent_SetWatcherOnTrigger(g_watcher, OnTrigger);
344     OH_HiAppEvent_DestroyWatcher(g_watcher);
345     napi_create_int32(env, code, &ret);
346     return ret;
347 }
348 
SetWatcherOnReceive(napi_env env,napi_callback_info info)349 static napi_value SetWatcherOnReceive(napi_env env, napi_callback_info info)
350 {
351     napi_value ret;
352     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
353     int32_t code = OH_HiAppEvent_SetWatcherOnReceive(g_watcher, OnReceiver);
354     OH_HiAppEvent_DestroyWatcher(g_watcher);
355     napi_create_int32(env, code, &ret);
356     return ret;
357 }
358 
SetWatcherOnReceiveInvWatcher(napi_env env,napi_callback_info info)359 static napi_value SetWatcherOnReceiveInvWatcher(napi_env env, napi_callback_info info)
360 {
361     napi_value ret;
362     g_watcher = OH_HiAppEvent_CreateWatcher(nullptr);
363     int32_t code = OH_HiAppEvent_SetWatcherOnReceive(g_watcher, OnReceiver);
364     OH_HiAppEvent_DestroyWatcher(g_watcher);
365     napi_create_int32(env, code, &ret);
366     return ret;
367 }
368 
TakeWatcherData(napi_env env,napi_callback_info info)369 static napi_value TakeWatcherData(napi_env env, napi_callback_info info)
370 {
371     napi_value ret;
372     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
373     OH_HiAppEvent_AddWatcher(g_watcher);
374     int32_t code = OH_HiAppEvent_TakeWatcherData(g_watcher, 1, OnTake);
375     OH_HiAppEvent_DestroyWatcher(g_watcher);
376     napi_create_int32(env, code, &ret);
377     return ret;
378 }
379 
TakeWatcherDataInvWatcher(napi_env env,napi_callback_info info)380 static napi_value TakeWatcherDataInvWatcher(napi_env env, napi_callback_info info)
381 {
382     napi_value ret;
383     g_watcher = OH_HiAppEvent_CreateWatcher(nullptr);
384     OH_HiAppEvent_AddWatcher(g_watcher);
385     int32_t code = OH_HiAppEvent_TakeWatcherData(g_watcher, 1, OnTake);
386     OH_HiAppEvent_DestroyWatcher(g_watcher);
387     napi_create_int32(env, code, &ret);
388     return ret;
389 }
TakeWatcherDataInvOperation(napi_env env,napi_callback_info info)390 static napi_value TakeWatcherDataInvOperation(napi_env env, napi_callback_info info)
391 {
392     napi_value ret;
393     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
394     int32_t code = OH_HiAppEvent_TakeWatcherData(g_watcher, 1, OnTake);
395     OH_HiAppEvent_DestroyWatcher(g_watcher);
396     napi_create_int32(env, code, &ret);
397     return ret;
398 }
399 
AddWatcher(napi_env env,napi_callback_info info)400 static napi_value AddWatcher(napi_env env, napi_callback_info info)
401 {
402     napi_value ret;
403     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
404     int32_t code = OH_HiAppEvent_AddWatcher(g_watcher);
405     OH_HiAppEvent_DestroyWatcher(g_watcher);
406     napi_create_int32(env, code, &ret);
407     return ret;
408 }
409 
AddWatcherInvWatcher(napi_env env,napi_callback_info info)410 static napi_value AddWatcherInvWatcher(napi_env env, napi_callback_info info)
411 {
412     napi_value ret;
413     g_watcher = OH_HiAppEvent_CreateWatcher(nullptr);
414     int32_t code = OH_HiAppEvent_AddWatcher(g_watcher);
415     OH_HiAppEvent_DestroyWatcher(g_watcher);
416     napi_create_int32(env, code, &ret);
417     return ret;
418 }
419 
RemoveWatcher(napi_env env,napi_callback_info info)420 static napi_value RemoveWatcher(napi_env env, napi_callback_info info)
421 {
422     napi_value ret;
423     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
424     OH_HiAppEvent_AddWatcher(g_watcher);
425     int32_t code = OH_HiAppEvent_RemoveWatcher(g_watcher);
426     OH_HiAppEvent_DestroyWatcher(g_watcher);
427     napi_create_int32(env, code, &ret);
428     return ret;
429 }
430 
RemoveWatcherInvWatcher(napi_env env,napi_callback_info info)431 static napi_value RemoveWatcherInvWatcher(napi_env env, napi_callback_info info)
432 {
433     napi_value ret;
434     g_watcher = OH_HiAppEvent_CreateWatcher(nullptr);
435     OH_HiAppEvent_AddWatcher(g_watcher);
436     int32_t code = OH_HiAppEvent_RemoveWatcher(g_watcher);
437     OH_HiAppEvent_DestroyWatcher(g_watcher);
438     napi_create_int32(env, code, &ret);
439     return ret;
440 }
441 
RemoveWatcherInvOperation(napi_env env,napi_callback_info info)442 static napi_value RemoveWatcherInvOperation(napi_env env, napi_callback_info info)
443 {
444     napi_value ret;
445     g_watcher = OH_HiAppEvent_CreateWatcher("testWatcher");
446     int32_t code = OH_HiAppEvent_RemoveWatcher(g_watcher);
447     OH_HiAppEvent_DestroyWatcher(g_watcher);
448     OH_HiAppEvent_ClearData();
449     napi_create_int32(env, code, &ret);
450     return ret;
451 }
452 
CreateProcessor(napi_env env,napi_callback_info info)453 static napi_value CreateProcessor(napi_env env, napi_callback_info info)
454 {
455     size_t argc = 1;
456     napi_value args[1];
457     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
458     size_t strSize;
459     napi_get_value_string_utf8(env, args[0], nullptr, 0, &strSize);
460     char*  name = new char[strSize + 1];
461     napi_get_value_string_utf8(env, args[0], name, strSize + 1, nullptr);
462     napi_valuetype valueType;
463     napi_typeof(env, args[0], &valueType);
464     HiAppEvent_Processor* processor = OH_HiAppEvent_CreateProcessor(name);
465     delete[] name;
466     napi_value retVal;
467     napi_create_external(env, processor, nullptr, nullptr, &retVal);
468     return retVal;
469 }
470 
SetReportRoute(napi_env env,napi_callback_info info)471 napi_value SetReportRoute(napi_env env, napi_callback_info info)
472 {
473     size_t argc = 3;
474     napi_value args[3];
475     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
476     napi_valuetype processorType;
477     napi_valuetype appIdType;
478     napi_valuetype routeInfoType;
479     napi_typeof(env, args[0], &processorType);
480     napi_typeof(env, args[1], &appIdType);
481     napi_typeof(env, args[2], &routeInfoType);
482     HiAppEvent_Processor* processor;
483     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
484     size_t appIdSize;
485     size_t routeInfoSize;
486     napi_get_value_string_utf8(env, args[1], nullptr, 0, &appIdSize);
487     napi_get_value_string_utf8(env, args[2], nullptr, 0, &routeInfoSize);
488     char* appId = new char[appIdSize + 1];
489     char* routeInfo = new char[routeInfoSize + 1];
490     napi_get_value_string_utf8(env, args[1], appId, appIdSize + 1, nullptr);
491     napi_get_value_string_utf8(env, args[2], routeInfo, routeInfoSize + 1, nullptr);
492     int result = OH_HiAppEvent_SetReportRoute(processor, appId, routeInfo);
493     delete[] appId;
494     delete[] routeInfo;
495     napi_value retVal;
496     napi_create_int32(env, result, &retVal);
497     return retVal;
498 }
499 
SetReportPolicy(napi_env env,napi_callback_info info)500 napi_value SetReportPolicy(napi_env env, napi_callback_info info)
501 {
502     size_t argc = 5;
503     napi_value args[5];
504     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
505     napi_valuetype processorType;
506     napi_valuetype periodReportType;
507     napi_valuetype batchReportType;
508     napi_valuetype onStartReportType;
509     napi_valuetype onBackgroundReportType;
510     napi_typeof(env, args[0], &processorType);
511     napi_typeof(env, args[1], &periodReportType);
512     napi_typeof(env, args[2], &batchReportType);
513     napi_typeof(env, args[3], &onStartReportType);
514     napi_typeof(env, args[4], &onBackgroundReportType);
515     HiAppEvent_Processor* processor;
516     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
517     int32_t periodReport;
518     int32_t batchReport;
519     napi_get_value_int32(env, args[1], &periodReport);
520     napi_get_value_int32(env, args[2], &batchReport);
521     bool onStartReport;
522     bool onBackgroundReport;
523     napi_get_value_bool(env, args[3], &onStartReport);
524     napi_get_value_bool(env, args[4], &onBackgroundReport);
525     size_t periodSize = sizeof(int32_t);
526     size_t batchSize = sizeof(int32_t);
527     size_t startReportSize = sizeof(bool);
528     size_t backgroundReportSize = sizeof(bool);
529     std::fill(reinterpret_cast<uint8_t*>(&periodReport), reinterpret_cast<uint8_t*>(&periodReport) + periodSize, 0);
530     std::fill(reinterpret_cast<uint8_t*>(&batchReport), reinterpret_cast<uint8_t*>(&batchReport) + batchSize, 0);
531     std::fill(
532         reinterpret_cast<uint8_t*>(&onStartReport),
533         reinterpret_cast<uint8_t*>(&onStartReport) + startReportSize, 0
534         );
535     std::fill(
536         reinterpret_cast<uint8_t*>(&onBackgroundReport),
537         reinterpret_cast<uint8_t*>(&onBackgroundReport) + backgroundReportSize, 0
538         );
539     int result = OH_HiAppEvent_SetReportPolicy(
540         processor, periodReport, batchReport, onStartReport, onBackgroundReport);
541     napi_value napiResult;
542     napi_create_int32(env, result, &napiResult);
543     return napiResult;
544 }
545 
SetReportEvent(napi_env env,napi_callback_info info)546 napi_value SetReportEvent(napi_env env, napi_callback_info info)
547 {
548     size_t argc = 4;
549     napi_value args[4];
550     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
551     napi_valuetype processorType;
552     napi_valuetype domainType;
553     napi_valuetype nameType;
554     napi_valuetype isRealTimeType;
555     napi_typeof(env, args[0], &processorType);
556     napi_typeof(env, args[1], &domainType);
557     napi_typeof(env, args[2], &nameType);
558     napi_typeof(env, args[3], &isRealTimeType);
559     HiAppEvent_Processor* processor;
560     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
561     size_t domainSize;
562     size_t nameSize;
563     napi_get_value_string_utf8(env, args[1], nullptr, 0, &domainSize);
564     napi_get_value_string_utf8(env, args[2], nullptr, 0, &nameSize);
565     char* domain = new char[domainSize + 1];
566     char* name = new char[nameSize + 1];
567     napi_get_value_string_utf8(env, args[1], domain, domainSize + 1, nullptr);
568     napi_get_value_string_utf8(env, args[2], name, nameSize + 1, nullptr);
569     bool isRealTime;
570     napi_get_value_bool(env, args[3], &isRealTime);
571     int result = OH_HiAppEvent_SetReportEvent(processor, domain, name, isRealTime);
572     delete[] domain;
573     delete[] name;
574     napi_value napiResult;
575     napi_create_int32(env, result, &napiResult);
576     return napiResult;
577 }
578 
SetCustomConfig(napi_env env,napi_callback_info info)579 napi_value SetCustomConfig(napi_env env, napi_callback_info info)
580 {
581     size_t argc = 3;
582     napi_value args[3];
583     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
584     napi_valuetype processorType;
585     napi_valuetype keyType;
586     napi_valuetype valueType;
587     napi_typeof(env, args[0], &processorType);
588     napi_typeof(env, args[1], &keyType);
589     napi_typeof(env, args[2], &valueType);
590     HiAppEvent_Processor* processor;
591     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
592     size_t keySize;
593     size_t valueSize;
594     napi_get_value_string_utf8(env, args[1], nullptr, 0, &keySize);
595     napi_get_value_string_utf8(env, args[2], nullptr, 0, &valueSize);
596     char* key = new char[keySize + 1];
597     char* value = new char[valueSize + 1];
598     napi_get_value_string_utf8(env, args[1], key, keySize + 1, nullptr);
599     napi_get_value_string_utf8(env, args[2], value, valueSize + 1, nullptr);
600     int result = OH_HiAppEvent_SetCustomConfig(processor, key, value);
601     delete[] key;
602     delete[] value;
603     napi_value retVal;
604     napi_create_int32(env, result, &retVal);
605     return retVal;
606 }
607 
SetConfigId(napi_env env,napi_callback_info info)608 napi_value SetConfigId(napi_env env, napi_callback_info info)
609 {
610     size_t argc = 2;
611     napi_value args[2];
612     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
613     napi_valuetype processorType;
614     napi_valuetype configIdType;
615     napi_typeof(env, args[0], &processorType);
616     napi_typeof(env, args[1], &configIdType);
617     HiAppEvent_Processor* processor;
618     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
619     int32_t configId;
620     napi_get_value_int32(env, args[1], &configId);
621     size_t configIdSize = sizeof(int32_t);
622     std::fill(reinterpret_cast<uint8_t*>(&configId), reinterpret_cast<uint8_t*>(&configId) + configIdSize, 0);
623     int result = OH_HiAppEvent_SetConfigId(processor, configId);
624     napi_value napiResult;
625     napi_create_int32(env, result, &napiResult);
626     return napiResult;
627 }
628 
SetReportUserId(napi_env env,napi_callback_info info)629 napi_value SetReportUserId(napi_env env, napi_callback_info info)
630 {
631     size_t argc = 3;
632     napi_value args[3];
633     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
634     napi_valuetype processorType;
635     napi_valuetype userIdNamesType;
636     napi_valuetype sizeType;
637     napi_typeof(env, args[0], &processorType);
638     napi_typeof(env, args[1], &userIdNamesType);
639     napi_typeof(env, args[2], &sizeType);
640     HiAppEvent_Processor* processor;
641     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
642     int size;
643     napi_get_value_int32(env, args[2], &size);
644     std::vector<std::string> userIdStrings;
645     std::vector<const char*> userIdNames;
646     for (uint32_t i = 0; i < size; i++) {
647         napi_value element;
648         napi_get_element(env, args[1], i, &element);
649         size_t strSize;
650         napi_get_value_string_utf8(env, element, nullptr, 0, &strSize);
651         std::string str(strSize, '\0');
652         napi_get_value_string_utf8(env, element, &str[0], strSize + 1, &strSize);
653         userIdStrings.push_back(str);
654         userIdNames.push_back(userIdStrings.back().c_str());
655         }
656     int result = OH_HiAppEvent_SetReportUserId(processor, userIdNames.data(), size);
657     napi_value retVal;
658     napi_create_int32(env, result, &retVal);
659     return retVal;
660 }
661 
SetReportUserProperty(napi_env env,napi_callback_info info)662 napi_value SetReportUserProperty(napi_env env, napi_callback_info info)
663 {
664     size_t argc = 3;
665     napi_value args[3];
666     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
667     napi_valuetype processorType;
668     napi_valuetype userPropertyNamesType;
669     napi_valuetype sizeType;
670     napi_typeof(env, args[0], &processorType);
671     napi_typeof(env, args[1], &userPropertyNamesType);
672     napi_typeof(env, args[2], &sizeType);
673     HiAppEvent_Processor* processor;
674     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
675     int size;
676     napi_get_value_int32(env, args[2], &size);
677     std::vector<std::string> userPropertyNamesStrings;
678     std::vector<const char*> userPropertyNames;
679     for (uint32_t i = 0; i < size; i++) {
680         napi_value element;
681         napi_get_element(env, args[1], i, &element);
682         size_t strSize;
683         napi_get_value_string_utf8(env, element, nullptr, 0, &strSize);
684         std::string str(strSize, '\0');
685         napi_get_value_string_utf8(env, element, &str[0], strSize + 1, &strSize);
686         userPropertyNamesStrings.push_back(str);
687         userPropertyNames.push_back(userPropertyNamesStrings.back().c_str());
688         }
689     int result = OH_HiAppEvent_SetReportUserId(processor, userPropertyNames.data(), size);
690     napi_value retVal;
691     napi_create_int32(env, result, &retVal);
692     return retVal;
693 }
694 
AddProcessor(napi_env env,napi_callback_info info)695 napi_value AddProcessor(napi_env env, napi_callback_info info)
696 {
697     size_t argc = 1;
698     napi_value args[1];
699     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
700     napi_valuetype processorType;
701     napi_typeof(env, args[0], &processorType);
702     HiAppEvent_Processor* processor;
703     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
704     int result = OH_HiAppEvent_AddProcessor(processor);
705     napi_value napiResult;
706     napi_create_int32(env, result, &napiResult);
707     return napiResult;
708 }
709 
RemoveProcessor(napi_env env,napi_callback_info info)710 napi_value RemoveProcessor(napi_env env, napi_callback_info info)
711 {
712     size_t argc = 1;
713     napi_value args[1];
714     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
715     napi_valuetype processorType;
716     napi_typeof(env, args[0], &processorType);
717     int64_t processorId;
718     napi_get_value_int64(env, args[0], &processorId);
719     int result = OH_HiAppEvent_RemoveProcessor(processorId);
720     napi_value napiResult;
721     napi_create_int32(env, result, &napiResult);
722     return napiResult;
723 }
724 
DestroyProcessor(napi_env env,napi_callback_info info)725 napi_value DestroyProcessor(napi_env env, napi_callback_info info)
726 {
727     size_t argc = 1;
728     napi_value args[1];
729     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
730     napi_valuetype processorType;
731     napi_typeof(env, args[0], &processorType);
732     HiAppEvent_Processor* processor;
733     napi_get_value_external(env, args[0], reinterpret_cast<void**>(&processor));
734     OH_HiAppEvent_DestroyProcessor(processor);
735     napi_value Result;
736     napi_get_undefined(env, &Result);
737     return Result;
738 }
739 
Add(napi_env env,napi_callback_info info)740 static napi_value Add(napi_env env, napi_callback_info info)
741 {
742     size_t argc = 2;
743     napi_value args[2] = {nullptr};
744 
745     napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
746 
747     napi_valuetype valuetype0;
748     napi_typeof(env, args[0], &valuetype0);
749 
750     napi_valuetype valuetype1;
751     napi_typeof(env, args[1], &valuetype1);
752 
753     double value0;
754     napi_get_value_double(env, args[0], &value0);
755 
756     double value1;
757     napi_get_value_double(env, args[1], &value1);
758 
759     napi_value sum;
760     napi_create_double(env, value0 + value1, &sum);
761 
762     return sum;
763 }
764 
NAPI_Global_add(napi_env env,napi_callback_info info)765 static napi_value NAPI_Global_add(napi_env env, napi_callback_info info) {
766 }
767 EXTERN_C_START
Init(napi_env env,napi_value exports)768 static napi_value Init(napi_env env, napi_value exports)
769 {
770     napi_property_descriptor desc[] = {
771         {"oHHiAppEventWrite", nullptr, OHHiAppEventWrite, nullptr, nullptr, nullptr, napi_default, nullptr},
772         {"oHHiAppEventConfigure", nullptr, OHHiAppEventConfigure, nullptr, nullptr, nullptr, napi_default, nullptr},
773         {"oHHiAppEventAddBoolParam", nullptr, OHHiAppEventAddBoolParam, nullptr, nullptr, nullptr, napi_default,
774          nullptr},
775         {"oHHiAppEventAddBoolArrayParam", nullptr, OHHiAppEventAddBoolArrayParam, nullptr, nullptr, nullptr,
776          napi_default, nullptr},
777         {"oHHiAppEventAddInt8Param", nullptr, OHHiAppEventAddInt8Param, nullptr, nullptr, nullptr, napi_default,
778          nullptr},
779         {"oHHiAppEventAddInt8ArrayParam", nullptr, OHHiAppEventAddInt8ArrayParam, nullptr, nullptr, nullptr,
780          napi_default, nullptr},
781         {"oHHiAppEventAddInt16Param", nullptr, OHHiAppEventAddInt16Param, nullptr, nullptr, nullptr, napi_default,
782          nullptr},
783         {"oHHiAppEventAddInt16ArrayParam", nullptr, OHHiAppEventAddInt16ArrayParam, nullptr, nullptr, nullptr,
784          napi_default, nullptr},
785         {"oHHiAppEventAddInt32Param", nullptr, OHHiAppEventAddInt32Param, nullptr, nullptr, nullptr, napi_default,
786          nullptr},
787         {"oHHiAppEventAddInt32ArrayParam", nullptr, OHHiAppEventAddInt32ArrayParam, nullptr, nullptr, nullptr,
788          napi_default, nullptr},
789         {"oHHiAppEventAddInt64Param", nullptr, OHHiAppEventAddInt64Param, nullptr, nullptr, nullptr, napi_default,
790          nullptr},
791         {"oHHiAppEventAddInt64ArrayParam", nullptr, OHHiAppEventAddInt64ArrayParam, nullptr, nullptr, nullptr,
792          napi_default, nullptr},
793         {"oHHiAppEventAddFloatParam", nullptr, OHHiAppEventAddFloatParam, nullptr, nullptr, nullptr, napi_default,
794          nullptr},
795         {"oHHiAppEventAddFloatArrayParam", nullptr, OHHiAppEventAddFloatArrayParam, nullptr, nullptr, nullptr,
796          napi_default, nullptr},
797         {"oHHiAppEventAddDoubleParam", nullptr, OHHiAppEventAddDoubleParam, nullptr, nullptr, nullptr, napi_default,
798          nullptr},
799         {"oHHiAppEventAddDoubleArrayParam", nullptr, OHHiAppEventAddDoubleArrayParam, nullptr, nullptr, nullptr,
800          napi_default, nullptr},
801         {"oHHiAppEventAddStringParam", nullptr, OHHiAppEventAddStringParam, nullptr, nullptr, nullptr, napi_default,
802          nullptr},
803         {"oHHiAppEventAddStringArrayParam", nullptr, OHHiAppEventAddStringArrayParam, nullptr, nullptr, nullptr,
804          napi_default, nullptr},
805         {"oHHiAppEventCreateParamList", nullptr, OHHiAppEventCreateParamList, nullptr, nullptr, nullptr, napi_default,
806          nullptr},
807         {"setAppEventFilter", nullptr, SetAppEventFilter, nullptr, nullptr, nullptr, napi_default, nullptr},
808         {"setAppEventFilterInvName", nullptr, SetAppEventFilterInvName, nullptr, nullptr, nullptr, napi_default,
809          nullptr},
810         {"setAppEventFilterInvDomain", nullptr, SetAppEventFilterInvDomain, nullptr, nullptr, nullptr, napi_default,
811          nullptr},
812         {"setAppEventFilterInvWatcher", nullptr, SetAppEventFilterInvWatcher, nullptr, nullptr, nullptr, napi_default,
813          nullptr},
814         {"setTriggerCondition", nullptr, SetTriggerCondition, nullptr, nullptr, nullptr, napi_default, nullptr},
815         {"setTriggerConditionInvWatcher", nullptr, SetTriggerConditionInvWatcher, nullptr, nullptr, nullptr,
816          napi_default, nullptr},
817         {"setWatcherOnTrigger", nullptr, SetWatcherOnTrigger, nullptr, nullptr, nullptr, napi_default, nullptr},
818         {"setWatcherOnTriggerInvWatcher", nullptr, SetWatcherOnTriggerInvWatcher, nullptr, nullptr, nullptr,
819          napi_default, nullptr},
820         {"setWatcherOnReceive", nullptr, SetWatcherOnReceive, nullptr, nullptr, nullptr, napi_default, nullptr},
821         {"setWatcherOnReceiveInvWatcher", nullptr, SetWatcherOnReceiveInvWatcher, nullptr, nullptr, nullptr,
822          napi_default, nullptr},
823         {"takeWatcherData", nullptr, TakeWatcherData, nullptr, nullptr, nullptr, napi_default, nullptr},
824         {"takeWatcherDataInvWatcher", nullptr, TakeWatcherDataInvWatcher, nullptr, nullptr, nullptr, napi_default,
825          nullptr},
826         {"takeWatcherDataInvOperation", nullptr, TakeWatcherDataInvOperation, nullptr, nullptr, nullptr, napi_default,
827          nullptr},
828         {"addWatcher", nullptr, AddWatcher, nullptr, nullptr, nullptr, napi_default, nullptr},
829         {"addWatcherInvWatcher", nullptr, AddWatcherInvWatcher, nullptr, nullptr, nullptr, napi_default, nullptr},
830         {"removeWatcher", nullptr, RemoveWatcher, nullptr, nullptr, nullptr, napi_default, nullptr},
831         {"removeWatcherInvWatcher", nullptr, RemoveWatcherInvWatcher, nullptr, nullptr, nullptr, napi_default, nullptr},
832         {"removeWatcherInvOperation", nullptr, RemoveWatcherInvOperation, nullptr, nullptr, nullptr, napi_default,
833          nullptr},
834         { "SetReportRoute", nullptr, SetReportRoute, nullptr, nullptr, nullptr, napi_default, nullptr },
835         { "CreateProcessor", nullptr, CreateProcessor, nullptr, nullptr, nullptr, napi_default, nullptr },
836         { "SetReportPolicy", nullptr, SetReportPolicy, nullptr, nullptr, nullptr, napi_default, nullptr },
837         { "SetReportEvent", nullptr, SetReportEvent, nullptr, nullptr, nullptr, napi_default, nullptr },
838         { "SetCustomConfig", nullptr, SetCustomConfig, nullptr, nullptr, nullptr, napi_default, nullptr },
839         { "SetConfigId", nullptr, SetConfigId, nullptr, nullptr, nullptr, napi_default, nullptr },
840         { "SetReportUserId", nullptr, SetReportUserId, nullptr, nullptr, nullptr, napi_default, nullptr },
841         { "SetReportUserProperty", nullptr, SetReportUserId, nullptr, nullptr, nullptr, napi_default, nullptr },
842         { "AddProcessor", nullptr, AddProcessor, nullptr, nullptr, nullptr, napi_default, nullptr },
843         { "RemoveProcessor", nullptr, RemoveProcessor, nullptr, nullptr, nullptr, napi_default, nullptr },
844         { "DestroyProcessor", nullptr, DestroyProcessor, nullptr, nullptr, nullptr, napi_default, nullptr },
845         {"add", nullptr, NAPI_Global_add, nullptr, nullptr, nullptr, napi_default, nullptr}};
846     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
847     return exports;
848 }
849 EXTERN_C_END
850 
851 static napi_module demoModule = {
852     .nm_version = 1,
853     .nm_flags = 0,
854     .nm_filename = nullptr,
855     .nm_register_func = Init,
856     .nm_modname = "entry",
857     .nm_priv = ((void*)0),
858     .reserved = { 0 },
859 };
860 
RegisterEntryModule(void)861 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
862 {
863     napi_module_register(&demoModule);
864 }
865