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 "unistd.h"
17 
18 #include "napi/native_api.h"
19 #include "hilog/log.h"
20 #include "BasicServicesKit/oh_commonevent.h"
21 #include "BasicServicesKit/oh_commonevent_support.h"
22 #include <cstdio>
23 #include <cwchar>
24 
OnReceive(const CommonEvent_RcvData *data)25 static void OnReceive(const CommonEvent_RcvData *data) {
26     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "Onreceive event!");
27     const char *event = OH_CommonEvent_GetEventFromRcvData(data);
28     int code = OH_CommonEvent_GetCodeFromRcvData(data);
29     const char *retData = OH_CommonEvent_GetDataStrFromRcvData(data);
30     const char *bundle = OH_CommonEvent_GetBundleNameFromRcvData(data);
31     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "event: %{public}s, code: %{public}d, data: %{public}s, bundle: %{public}s\n", event, code, retData, bundle);
32     const CommonEvent_Parameters *parameters = OH_CommonEvent_GetParametersFromRcvData(data);
33     int intValue = OH_CommonEvent_GetIntFromParameters(parameters, "wh1", 10);
34     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "intValue = %{public}d", intValue);
35     bool boolValue = OH_CommonEvent_GetBoolFromParameters(parameters, "wh4", false);
36     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "boolValue = %{public}d", boolValue);
37     bool** boolarray = new bool*;
38     int32_t boolarraysize = OH_CommonEvent_GetBoolArrayFromParameters(parameters, "wh6",boolarray);
39     printf("int arr size is %d:\n", boolarraysize);
40     for (int i = 0; i < boolarraysize; i++) {
41         printf("%d |", *((*boolarray) + i));
42     }
43     printf("\n");
44     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "boolarraysize = %{public}d", boolarraysize);
45 
46     char charValue = OH_CommonEvent_GetCharFromParameters(parameters, "wh2", 'A');
47     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "charValue = %{public}c", charValue);
48     char** chararray = new char*;
49     int32_t chararraysize = OH_CommonEvent_GetCharArrayFromParameters(parameters, "wh2",chararray);
50     printf("int arr size is %d:\n", chararraysize);
51     for (int i = 0; i < chararraysize; i++) {
52         printf("%c |", *((*chararray) + i));
53     }
54     printf("\n");
55     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "chararraysize = %{public}d, %{public}s", chararraysize, *chararray);
56 
57     double doubleValue = OH_CommonEvent_GetDoubleFromParameters(parameters, "wh3", 33.33);
58     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "doubleValue = %{public}f", doubleValue);
59     double** doublearray = new double*;
60     int32_t doublearraysize = OH_CommonEvent_GetDoubleArrayFromParameters(parameters, "wh8",doublearray);
61     printf("int arr size is %d:\n", doublearraysize);
62     for (int i = 0; i < doublearraysize; i++) {
63         printf("%f |", *((*doublearray) + i));
64     }
65     printf("\n");
66     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "doublearraysize = %{public}d", doublearraysize);
67 
68     int** arr = new int*;
69     int32_t intarrarysize = OH_CommonEvent_GetIntArrayFromParameters(parameters, "wh5",arr);
70     printf("int arr size is %d:\n", intarrarysize);
71     for (int i = 0; i < intarrarysize; i++) {
72         printf("%d |", *((*arr) + i));
73     }
74     printf("\n");
75     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "intarrarysize = %{public}d", intarrarysize);
76 
77     bool exists = OH_CommonEvent_HasKeyInParameters(parameters, "wh4");
78     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "exists = %{public}d", exists);
79 
80     long longValue = OH_CommonEvent_GetLongFromParameters(parameters, "wh9", 2147483650);
81     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "longValue = %{public}ld", longValue);
82     long** longarrary = new long*;
83     int32_t longarrarysize = OH_CommonEvent_GetLongArrayFromParameters(parameters, "wh10",longarrary);
84     printf("int arr size is %d:\n", longarrarysize);
85     for (int i = 0; i < longarrarysize; i++) {
86         printf("%ld |", *((*longarrary) + i));
87     }
88     printf("\n");
89     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "ces_test", "longarrarysize = %{public}d", longarrarysize);
90 }
91 
92 static CommonEvent_Subscriber *subscriber = {};
93 
94 
95 static CommonEvent_SubscribeInfo* information = nullptr;
96 
CreateSubscribeInfo(napi_env env, napi_callback_info info)97 static napi_value CreateSubscribeInfo(napi_env env, napi_callback_info info)
98 {
99     size_t argc = 1;
100     napi_value args[1] = {nullptr};
101     napi_get_cb_info(env, info, &argc, args, NULL, NULL);
102     if (argc != 1) {
103         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "CreateSubscribeInfo wrong number of arguments.");
104         return 0;
105     }
106 
107     size_t strLen = 0;
108     napi_value result = args[0];
109     bool isArray = false;
110     napi_valuetype valuetype = napi_undefined;
111     napi_is_array(env, result, &isArray);
112     if (!isArray) {
113         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "Create subscribe is expected to be an array.");
114         napi_value res = nullptr;
115         napi_create_int32(env, 403, &res);
116         return res;
117     }
118 
119     uint32_t length = 0;
120 
121     napi_get_array_length(env, result, &length);
122     if (length == 0) {
123         OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "The array is empty.");
124         information = OH_CommonEvent_CreateSubscribeInfo(nullptr, 0);
125         if (information == nullptr) {
126             OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "Create subscribe failed.");
127             napi_value res = nullptr;
128             napi_create_int32(env, 401, &res);
129             return res;
130         } else {
131             OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Create subscribe successful.");
132             napi_value res = nullptr;
133             napi_create_int32(env, 402, &res);
134             return res;
135         }
136         return 0;
137     }
138 
139     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "The array size is %{public}u.", length);
140     const char *events[length];
141     for (size_t i = 0; i < length; i++) {
142         napi_value event = nullptr;
143         napi_get_element(env, result, i, &event);
144         napi_typeof(env, event, &valuetype);
145         if (valuetype != napi_string) {
146             OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "Wrong argument type. String expected.");
147             return 0;
148         }
149         char* str = new char[1024];
150         napi_get_value_string_utf8(env, event, str, 1024 - 1, &strLen);
151         events[i] = str;
152         OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Get event %{public}s.", str);
153     }
154     information = OH_CommonEvent_CreateSubscribeInfo(events, length);
155     if (information == nullptr) {
156         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "Create subscribe info failed.");
157     } else {
158         OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Create subscribe info successful.");
159     }
160     return 0;
161 }
162 
SetPublisherPermission(napi_env env, napi_callback_info info)163 static napi_value SetPublisherPermission(napi_env env, napi_callback_info info)
164 {
165     size_t argc = 1;
166     napi_value args[1] = {nullptr};
167     napi_get_cb_info(env, info, &argc, args, NULL, NULL);
168     if (argc != 1) {
169         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "SetPublisherPermission wrong number of arguments.");
170         return 0;
171     }
172     size_t strLen = 0;
173     napi_value result = args[0];
174     napi_valuetype valuetype = napi_undefined;
175     char str[1024] = {0};
176     napi_get_named_property(env, args[0], "permission", &result);
177     napi_typeof(env, result, &valuetype);
178     if (valuetype != napi_string) {
179         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "Wrong argument type. String expected.");
180         return 0;
181     }
182 
183     napi_get_value_string_utf8(env, result, str, 1024 - 1, &strLen);
184     int32_t ret = OH_CommonEvent_SetPublisherPermission(information, str);
185     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Get permission %{public}s.", str);
186     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Set publisher permission, result %{public}d.", ret);
187     return 0;
188 }
189 
SetPublisherBundleName(napi_env env, napi_callback_info info)190 static napi_value SetPublisherBundleName(napi_env env, napi_callback_info info)
191 {
192     size_t argc = 1;
193     napi_value args[1] = {nullptr};
194     napi_get_cb_info(env, info, &argc, args, NULL, NULL);
195     if (argc != 1) {
196         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "PublisherBundleName wrong number of arguments.");
197         return 0;
198     }
199     size_t strLen = 0;
200     napi_value result = args[0];
201     napi_valuetype valuetype = napi_undefined;
202     napi_get_named_property(env, args[0], "bundleName", &result);
203     napi_typeof(env, result, &valuetype);
204     if (valuetype != napi_string) {
205         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "Wrong argument type. String expected.");
206         return 0;
207     }
208     char str[1024] = {0};
209     napi_get_value_string_utf8(env, result, str, 1024 - 1, &strLen);
210     int32_t ret = OH_CommonEvent_SetPublisherBundleName(information, str);
211     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Get bundleName %{public}s.", str);
212     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Set publisher bundle name, result %{public}d.", ret);
213     return 0;
214 }
215 
DestroySubscribeInfo(napi_env env, napi_callback_info info)216 static napi_value DestroySubscribeInfo(napi_env env, napi_callback_info info)
217 {
218     OH_CommonEvent_DestroySubscribeInfo(information);
219     information = nullptr;
220     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Destroy subscribe info successful.");
221     napi_value res = nullptr;
222     napi_create_int32(env, 400, &res);
223     return res;
224 }
225 
CreateSubscriber(napi_env env, napi_callback_info info)226 static napi_value CreateSubscriber(napi_env env, napi_callback_info info)
227 {
228     subscriber = OH_CommonEvent_CreateSubscriber(information, OnReceive);
229     if (subscriber == nullptr) {
230         OH_LOG_Print(LOG_APP, LOG_WARN, 1, "CES_TEST", "Create subscriber failed.");
231         napi_value res = nullptr;
232         napi_create_int32(env, 401, &res);
233         return res;
234     } else {
235         OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Create subscriber successful.");
236     }
237     return 0;
238 }
239 
DestroySubscriber(napi_env env, napi_callback_info info)240 static napi_value DestroySubscriber(napi_env env, napi_callback_info info)
241 {
242     OH_CommonEvent_DestroySubscriber(subscriber);
243     subscriber = nullptr;
244     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Destroy subscriber successful.");
245     napi_value res = nullptr;
246     napi_create_int32(env, 400, &res);
247     return res;
248 }
249 
CommonEventSubscribe(napi_env env, napi_callback_info info)250 static napi_value CommonEventSubscribe(napi_env env, napi_callback_info info)
251 {
252     int32_t ret = OH_CommonEvent_Subscribe(subscriber);
253     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Subscribe result %{public}d", ret);
254     napi_value res = nullptr;
255     napi_create_int32(env, ret, &res);
256     return res;
257 }
258 
CommonEventUnSubscribe(napi_env env, napi_callback_info info)259 static napi_value CommonEventUnSubscribe(napi_env env, napi_callback_info info)
260 {
261     int32_t ret = OH_CommonEvent_UnSubscribe(subscriber);
262     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "unSubscribe result %{public}d", ret);
263     napi_value res = nullptr;
264     napi_create_int32(env, ret, &res);
265     return res;
266 }
267 
ReleaseSubscribeInfo(napi_env env, napi_callback_info info)268 static napi_value ReleaseSubscribeInfo(napi_env env, napi_callback_info info)
269 {
270     information = nullptr;
271     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Assign subscriber info nullptr");
272     return 0;
273 }
274 
ReleaseSubscriber(napi_env env, napi_callback_info info)275 static napi_value ReleaseSubscriber(napi_env env, napi_callback_info info)
276 {
277     subscriber = nullptr;
278     OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "Assign subscriber nullptr");
279     return 0;
280 }
281 
282 EXTERN_C_START
Init(napi_env env, napi_value exports)283 static napi_value Init(napi_env env, napi_value exports)
284 {
285     napi_property_descriptor desc[] = {
286         { "create_subscribe_info", nullptr, CreateSubscribeInfo, nullptr, nullptr, nullptr, napi_default, nullptr },
287         { "set_publisher_permission", nullptr, SetPublisherPermission, nullptr, nullptr, nullptr, napi_default, nullptr },
288         { "set_publisher_bundleName", nullptr, SetPublisherBundleName, nullptr, nullptr, nullptr, napi_default, nullptr },
289         { "destroy_subscribe_info", nullptr, DestroySubscribeInfo, nullptr, nullptr, nullptr, napi_default, nullptr },
290         { "create_subscriber", nullptr, CreateSubscriber, nullptr, nullptr, nullptr, napi_default, nullptr },
291         { "destroy_subscriber", nullptr, DestroySubscriber, nullptr, nullptr, nullptr, napi_default, nullptr },
292         { "common_event_subscribe", nullptr, CommonEventSubscribe, nullptr, nullptr, nullptr, napi_default, nullptr },
293         { "common_event_unsubscribe", nullptr, CommonEventUnSubscribe, nullptr, nullptr, nullptr, napi_default, nullptr },
294         { "release_subscribe_info", nullptr, ReleaseSubscribeInfo, nullptr, nullptr, nullptr, napi_default, nullptr },
295         { "release_subscriber", nullptr, ReleaseSubscriber, nullptr, nullptr, nullptr, napi_default, nullptr },
296     };
297     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
298     return exports;
299 }
300 EXTERN_C_END
301 
302 static napi_module demoModule = {
303     .nm_version = 1,
304     .nm_flags = 0,
305     .nm_filename = nullptr,
306     .nm_register_func = Init,
307     .nm_modname = "subscribe",
308     .nm_priv = ((void*)0),
309     .reserved = { 0 },
310 };
311 
RegisterEntryModule(void)312 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
313 {
314     napi_module_register(&demoModule);
315 }