• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi/native_api.h"
17 #include "hiappevent.h"
18 #define FAIL (-1)
19 #define SUCCESS 0
20 #define PARAM_0 0
21 #define PARAM_1 1
22 
23 const float FLOAT_123_22 = 123.22f;
24 const float FLOAT_234_5 = 234.5f;
25 const float FLOAT_131312_46464 = 131312.46464f;
26 const double DOUBLE_123_22 = 123.22;
27 const double DOUBLE_30949_374 = 30949.374;
28 const double DOUBLE_131312_46464 = 131312.46464;
29 
OHHiAppEventWrite(napi_env env,napi_callback_info info)30 static napi_value OHHiAppEventWrite(napi_env env, napi_callback_info info)
31 {
32     ParamList list = OH_HiAppEvent_CreateParamList();
33     int32_t num = PARAM_1;
34     OH_HiAppEvent_AddInt32Param(list, "int32_key", num);
35     int32_t numArray[] = {PARAM_1, INT32_MAX, INT32_MIN};
36     OH_HiAppEvent_AddInt32ArrayParam(list, "int32_arr_key", numArray, sizeof(numArray) / sizeof(numArray[0]));
37     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
38     napi_value result = nullptr;
39     napi_create_int32(env, ret, &result);
40     return result;
41 }
42 
OHHiAppEventConfigure(napi_env env,napi_callback_info info)43 static napi_value OHHiAppEventConfigure(napi_env env, napi_callback_info info)
44 {
45     ParamList list = OH_HiAppEvent_CreateParamList();
46     int32_t num = PARAM_1;
47     OH_HiAppEvent_AddInt32Param(list, "int32_key", num);
48     int ret = -PARAM_1;
49     bool configure = OH_HiAppEvent_Configure(DISABLE, "true");
50     if (configure) {
51         ret = SUCCESS;
52     }
53     OH_HiAppEvent_Configure(DISABLE, "false");
54     napi_value result = nullptr;
55     napi_create_int32(env, ret, &result);
56     return result;
57 }
58 
OHHiAppEventAddBoolParam(napi_env env,napi_callback_info info)59 static napi_value OHHiAppEventAddBoolParam(napi_env env, napi_callback_info info)
60 {
61     ParamList list = OH_HiAppEvent_CreateParamList();
62     OH_HiAppEvent_AddBoolParam(list, "bool_key", true);
63     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
64     napi_value result = nullptr;
65     napi_create_int32(env, ret, &result);
66     return result;
67 }
68 
OHHiAppEventAddBoolArrayParam(napi_env env,napi_callback_info info)69 static napi_value OHHiAppEventAddBoolArrayParam(napi_env env, napi_callback_info info)
70 {
71     ParamList list = OH_HiAppEvent_CreateParamList();
72     bool nums[] = {true};
73     OH_HiAppEvent_AddBoolArrayParam(list, "bool_arr_key", nums, sizeof(nums) / sizeof(nums[0]));
74     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
75     napi_value result = nullptr;
76     napi_create_int32(env, ret, &result);
77     return result;
78 }
79 
OHHiAppEventAddInt8Param(napi_env env,napi_callback_info info)80 static napi_value OHHiAppEventAddInt8Param(napi_env env, napi_callback_info info)
81 {
82     ParamList list = OH_HiAppEvent_CreateParamList();
83     int8_t num = PARAM_1;
84     OH_HiAppEvent_AddInt8Param(list, "int8_key", num);
85     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
86     napi_value result = nullptr;
87     napi_create_int32(env, ret, &result);
88     return result;
89 }
90 
OHHiAppEventAddInt8ArrayParam(napi_env env,napi_callback_info info)91 static napi_value OHHiAppEventAddInt8ArrayParam(napi_env env, napi_callback_info info)
92 {
93     ParamList list = OH_HiAppEvent_CreateParamList();
94     int8_t num[] = {PARAM_1, INT8_MAX, INT8_MIN};
95     OH_HiAppEvent_AddInt8ArrayParam(list, "int8_key", num, sizeof(num) / sizeof(num[0]));
96     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
97     napi_value result = nullptr;
98     napi_create_int32(env, ret, &result);
99     return result;
100 }
101 
OHHiAppEventAddInt16Param(napi_env env,napi_callback_info info)102 static napi_value OHHiAppEventAddInt16Param(napi_env env, napi_callback_info info)
103 {
104     ParamList list = OH_HiAppEvent_CreateParamList();
105     int16_t num = PARAM_1;
106     OH_HiAppEvent_AddInt16Param(list, "int16_key", num);
107     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
108     napi_value result = nullptr;
109     napi_create_int32(env, ret, &result);
110     return result;
111 }
112 
OHHiAppEventAddInt16ArrayParam(napi_env env,napi_callback_info info)113 static napi_value OHHiAppEventAddInt16ArrayParam(napi_env env, napi_callback_info info)
114 {
115     ParamList list = OH_HiAppEvent_CreateParamList();
116     int16_t num[] = {PARAM_1, INT16_MAX, INT16_MIN};
117     OH_HiAppEvent_AddInt16ArrayParam(list, "int16_key", num, sizeof(num) / sizeof(num[0]));
118     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
119     napi_value result = nullptr;
120     napi_create_int32(env, ret, &result);
121     return result;
122 }
123 
OHHiAppEventAddInt32Param(napi_env env,napi_callback_info info)124 static napi_value OHHiAppEventAddInt32Param(napi_env env, napi_callback_info info)
125 {
126     ParamList list = OH_HiAppEvent_CreateParamList();
127     int32_t num = PARAM_1;
128     OH_HiAppEvent_AddInt32Param(list, "int32_key", num);
129     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
130     napi_value result = nullptr;
131     napi_create_int32(env, ret, &result);
132     return result;
133 }
134 
OHHiAppEventAddInt32ArrayParam(napi_env env,napi_callback_info info)135 static napi_value OHHiAppEventAddInt32ArrayParam(napi_env env, napi_callback_info info)
136 {
137     ParamList list = OH_HiAppEvent_CreateParamList();
138     int32_t num[] = {PARAM_1, INT32_MAX, INT32_MIN};
139     OH_HiAppEvent_AddInt32ArrayParam(list, "int32_key", num, sizeof(num) / sizeof(num[0]));
140     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
141     napi_value result = nullptr;
142     napi_create_int32(env, ret, &result);
143     return result;
144 }
145 
OHHiAppEventAddInt64Param(napi_env env,napi_callback_info info)146 static napi_value OHHiAppEventAddInt64Param(napi_env env, napi_callback_info info)
147 {
148     ParamList list = OH_HiAppEvent_CreateParamList();
149     int64_t num = PARAM_1;
150     OH_HiAppEvent_AddInt64Param(list, "int64_key", num);
151     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
152     napi_value result = nullptr;
153     napi_create_int32(env, ret, &result);
154     return result;
155 }
156 
OHHiAppEventAddInt64ArrayParam(napi_env env,napi_callback_info info)157 static napi_value OHHiAppEventAddInt64ArrayParam(napi_env env, napi_callback_info info)
158 {
159     ParamList list = OH_HiAppEvent_CreateParamList();
160     int64_t num[] = {PARAM_1, INT64_MAX, INT64_MIN};
161     OH_HiAppEvent_AddInt64ArrayParam(list, "int64_key", num, sizeof(num) / sizeof(num[0]));
162     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
163     napi_value result = nullptr;
164     napi_create_int32(env, ret, &result);
165     return result;
166 }
167 
OHHiAppEventAddFloatParam(napi_env env,napi_callback_info info)168 static napi_value OHHiAppEventAddFloatParam(napi_env env, napi_callback_info info)
169 {
170     ParamList list = OH_HiAppEvent_CreateParamList();
171     float num = FLOAT_234_5;
172     OH_HiAppEvent_AddFloatParam(list, "float_key", num);
173     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
174     napi_value result = nullptr;
175     napi_create_int32(env, ret, &result);
176     return result;
177 }
178 
OHHiAppEventAddFloatArrayParam(napi_env env,napi_callback_info info)179 static napi_value OHHiAppEventAddFloatArrayParam(napi_env env, napi_callback_info info)
180 {
181     ParamList list = OH_HiAppEvent_CreateParamList();
182     float num[] = {FLOAT_123_22, FLOAT_234_5, FLOAT_131312_46464};
183     OH_HiAppEvent_AddFloatArrayParam(list, "float_key", num, sizeof(num) / sizeof(num[0]));
184     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
185     napi_value result = nullptr;
186     napi_create_int32(env, ret, &result);
187     return result;
188 }
189 
OHHiAppEventAddDoubleParam(napi_env env,napi_callback_info info)190 static napi_value OHHiAppEventAddDoubleParam(napi_env env, napi_callback_info info)
191 {
192     ParamList list = OH_HiAppEvent_CreateParamList();
193     double num = DOUBLE_30949_374;
194     OH_HiAppEvent_AddDoubleParam(list, "double_key", num);
195     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
196     napi_value result = nullptr;
197     napi_create_int32(env, ret, &result);
198     return result;
199 }
200 
OHHiAppEventAddDoubleArrayParam(napi_env env,napi_callback_info info)201 static napi_value OHHiAppEventAddDoubleArrayParam(napi_env env, napi_callback_info info)
202 {
203     ParamList list = OH_HiAppEvent_CreateParamList();
204     double nums[] = {DOUBLE_123_22, DOUBLE_30949_374, DOUBLE_131312_46464};
205     OH_HiAppEvent_AddDoubleArrayParam(list, "double_arr_key", nums, sizeof(nums) / sizeof(nums[0]));
206     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
207     napi_value result = nullptr;
208     napi_create_int32(env, ret, &result);
209     return result;
210 }
211 
OHHiAppEventAddStringParam(napi_env env,napi_callback_info info)212 static napi_value OHHiAppEventAddStringParam(napi_env env, napi_callback_info info)
213 {
214     ParamList list = OH_HiAppEvent_CreateParamList();
215     char str[] = "hello";
216     OH_HiAppEvent_AddStringParam(list, "string_key", str);
217     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
218     napi_value result = nullptr;
219     napi_create_int32(env, ret, &result);
220     return result;
221 }
222 
OHHiAppEventAddStringArrayParam(napi_env env,napi_callback_info info)223 static napi_value OHHiAppEventAddStringArrayParam(napi_env env, napi_callback_info info)
224 {
225     ParamList list = OH_HiAppEvent_CreateParamList();
226     char str[] = "hello";
227     char par[] = "world";
228     char *strs[] = {str, par};
229     OH_HiAppEvent_AddStringArrayParam(list, "string_arr_key", strs, sizeof(strs) / sizeof(strs[0]));
230     int ret = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
231     napi_value result = nullptr;
232     napi_create_int32(env, ret, &result);
233     return result;
234 }
235 
OHHiAppEventCreateParamList(napi_env env,napi_callback_info info)236 static napi_value OHHiAppEventCreateParamList(napi_env env, napi_callback_info info)
237 {
238     ParamList list = OH_HiAppEvent_CreateParamList();
239     int ret = FAIL;
240     if (list != nullptr) {
241         ret = SUCCESS;
242     }
243     napi_value result = nullptr;
244     napi_create_int32(env, ret, &result);
245     return result;
246 }
247 
248 EXTERN_C_START
Init(napi_env env,napi_value exports)249 static napi_value Init(napi_env env, napi_value exports)
250 {
251     napi_property_descriptor desc[] = {
252         {"oHHiAppEventWrite", nullptr, OHHiAppEventWrite, nullptr, nullptr, nullptr, napi_default, nullptr},
253         {"oHHiAppEventConfigure", nullptr, OHHiAppEventConfigure, nullptr, nullptr, nullptr, napi_default, nullptr},
254         {"oHHiAppEventAddBoolParam", nullptr, OHHiAppEventAddBoolParam, nullptr, nullptr, nullptr,
255          napi_default, nullptr},
256         {"oHHiAppEventAddBoolArrayParam", nullptr, OHHiAppEventAddBoolArrayParam, nullptr, nullptr, nullptr,
257          napi_default, nullptr},
258         {"oHHiAppEventAddInt8Param", nullptr, OHHiAppEventAddInt8Param, nullptr, nullptr, nullptr,
259          napi_default, nullptr},
260         {"oHHiAppEventAddInt8ArrayParam", nullptr, OHHiAppEventAddInt8ArrayParam, nullptr, nullptr, nullptr,
261          napi_default, nullptr},
262         {"oHHiAppEventAddInt16Param", nullptr, OHHiAppEventAddInt16Param, nullptr, nullptr, nullptr,
263          napi_default, nullptr},
264         {"oHHiAppEventAddInt16ArrayParam", nullptr, OHHiAppEventAddInt16ArrayParam, nullptr, nullptr, nullptr,
265          napi_default, nullptr},
266         {"oHHiAppEventAddInt32Param", nullptr, OHHiAppEventAddInt32Param, nullptr, nullptr, nullptr,
267          napi_default, nullptr},
268         {"oHHiAppEventAddInt32ArrayParam", nullptr, OHHiAppEventAddInt32ArrayParam, nullptr, nullptr, nullptr,
269          napi_default, nullptr},
270         {"oHHiAppEventAddInt64Param", nullptr, OHHiAppEventAddInt64Param, nullptr, nullptr, nullptr,
271          napi_default, nullptr},
272         {"oHHiAppEventAddInt64ArrayParam", nullptr, OHHiAppEventAddInt64ArrayParam, nullptr, nullptr, nullptr,
273          napi_default, nullptr},
274         {"oHHiAppEventAddFloatParam", nullptr, OHHiAppEventAddFloatParam, nullptr, nullptr, nullptr,
275          napi_default, nullptr},
276         {"oHHiAppEventAddFloatArrayParam", nullptr, OHHiAppEventAddFloatArrayParam, nullptr, nullptr, nullptr,
277          napi_default, nullptr},
278         {"oHHiAppEventAddDoubleParam", nullptr, OHHiAppEventAddDoubleParam, nullptr, nullptr, nullptr,
279          napi_default, nullptr},
280         {"oHHiAppEventAddDoubleArrayParam", nullptr, OHHiAppEventAddDoubleArrayParam, nullptr, nullptr, nullptr,
281          napi_default, nullptr},
282         {"oHHiAppEventAddStringParam", nullptr, OHHiAppEventAddStringParam, nullptr, nullptr, nullptr,
283          napi_default, nullptr},
284         {"oHHiAppEventAddStringArrayParam", nullptr, OHHiAppEventAddStringArrayParam, nullptr, nullptr, nullptr,
285          napi_default, nullptr},
286         {"oHHiAppEventCreateParamList", nullptr, OHHiAppEventCreateParamList, nullptr, nullptr, nullptr,
287          napi_default, nullptr}};
288     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
289     return exports;
290 }
291 
292 EXTERN_C_END
293 
294 static napi_module demoModule = {
295     .nm_version = 1,
296     .nm_flags = 0,
297     .nm_filename = nullptr,
298     .nm_register_func = Init,
299     .nm_modname = "hiappevent",
300     .nm_priv = ((void *)0),
301     .reserved = {0},
302 };
303 
RegisterModule(void)304 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
305