1 /*
2 * Copyright (c) 2022 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 #include "loghelper.h"
16 #include "napi/native_api.h"
17 #include "napi/native_node_api.h"
18 #include "nfc_napi_cardEmulation_adapter.h"
19 #include "nfc_sdk_common.h"
20
21 namespace OHOS {
22 namespace NFC {
23 namespace KITS {
24 const std::string FEATURE_TYPE = "FeatureType";
25 const std::string CARD_TYPE = "CardType";
26
27 /*
28 * Module initialization function
29 */
CreateEnumConstructor(napi_env env,napi_callback_info info)30 static napi_value CreateEnumConstructor(napi_env env, napi_callback_info info)
31 {
32 napi_value thisArg = nullptr;
33 void *data = nullptr;
34
35 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
36 napi_value global = nullptr;
37 napi_get_global(env, &global);
38 return thisArg;
39 }
40
41 // @deprecated since 9
CreateEnumFeatureType(napi_env env,napi_value exports)42 static napi_value CreateEnumFeatureType(napi_env env, napi_value exports)
43 {
44 napi_value hce = nullptr;
45 napi_value uicc = nullptr;
46 napi_value ese = nullptr;
47 napi_create_int32(env, static_cast<int32_t>(FeatureType::HCE), &hce);
48 napi_create_int32(env, static_cast<int32_t>(FeatureType::UICC), &uicc);
49 napi_create_int32(env, static_cast<int32_t>(FeatureType::ESE), &ese);
50 napi_property_descriptor desc[] = {
51 DECLARE_NAPI_STATIC_PROPERTY("HCE", hce),
52 DECLARE_NAPI_STATIC_PROPERTY("UICC", uicc),
53 DECLARE_NAPI_STATIC_PROPERTY("ESE", ese),
54 };
55 napi_value result = nullptr;
56 napi_define_class(env, FEATURE_TYPE.c_str(), NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
57 sizeof(desc) / sizeof(*desc), desc, &result);
58 napi_set_named_property(env, exports, FEATURE_TYPE.c_str(), result);
59 return exports;
60 }
61
CreateEnumCardType(napi_env env,napi_value exports)62 static napi_value CreateEnumCardType(napi_env env, napi_value exports)
63 {
64 napi_value payment = nullptr;
65 napi_value other = nullptr;
66 napi_create_string_utf8(env, KITS::TYPE_PAYMENT.c_str(), KITS::TYPE_PAYMENT.length(), &payment);
67 napi_create_string_utf8(env, KITS::TYPE_OHTER.c_str(), KITS::TYPE_OHTER.length(), &other);
68 napi_property_descriptor desc[] = {
69 DECLARE_NAPI_STATIC_PROPERTY("PAYMENT", payment),
70 DECLARE_NAPI_STATIC_PROPERTY("OTHER", other),
71 };
72 napi_value result = nullptr;
73 napi_define_class(env, CARD_TYPE.c_str(), NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
74 sizeof(desc) / sizeof(desc[0]), desc, &result);
75 napi_set_named_property(env, exports, CARD_TYPE.c_str(), result);
76 return exports;
77 }
78
InitJs(napi_env env,napi_value exports)79 static napi_value InitJs(napi_env env, napi_value exports)
80 {
81 DebugLog("Init, nfc_napi_cardEmulation");
82 napi_property_descriptor desc[] = {
83 DECLARE_NAPI_FUNCTION("isSupported", IsSupported),
84 DECLARE_NAPI_FUNCTION("hasHceCapability", HasHceCapability),
85 DECLARE_NAPI_FUNCTION("isDefaultService", IsDefaultService),
86 };
87
88 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
89 CreateEnumFeatureType(env, exports);
90 CreateEnumCardType(env, exports);
91 return exports;
92 }
93
94 static napi_module cardEmulationModule = {
95 .nm_version = 1,
96 .nm_flags = 0,
97 .nm_filename = NULL,
98 .nm_register_func = InitJs,
99 .nm_modname = "nfc.cardEmulation",
100 .nm_priv = ((void *)0),
101 .reserved = { 0 }
102 };
103
RegisterModule(void)104 extern "C" __attribute__((constructor)) void RegisterModule(void)
105 {
106 napi_module_register(&cardEmulationModule);
107 }
108
109 } // namespace KITS
110 } // namespace NFC
111 } // namespace OHOS
112
113