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