1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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_controller_adapter.h"
19 #include "nfc_napi_controller_event.h"
20 #include "nfc_sdk_common.h"
21 
22 namespace OHOS {
23 namespace NFC {
24 namespace KITS {
25 /*
26  * Module initialization function
27  */
CreateEnumConstructor(napi_env env,napi_callback_info info)28 static napi_value CreateEnumConstructor(napi_env env, napi_callback_info info)
29 {
30     napi_value thisArg = nullptr;
31     void *data = nullptr;
32 
33     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
34     napi_value global = nullptr;
35     napi_get_global(env, &global);
36     return thisArg;
37 }
38 
CreateEnumNfcState(napi_env env,napi_value exports)39 static napi_value CreateEnumNfcState(napi_env env, napi_value exports)
40 {
41     napi_value state_off = nullptr;
42     napi_value state_turning_on = nullptr;
43     napi_value state_on = nullptr;
44     napi_value state_turning_off = nullptr;
45     napi_create_int32(env, static_cast<int32_t>(NfcState::STATE_OFF), &state_off);
46     napi_create_int32(env, static_cast<int32_t>(NfcState::STATE_TURNING_OFF), &state_turning_off);
47     napi_create_int32(env, static_cast<int32_t>(NfcState::STATE_ON), &state_on);
48     napi_create_int32(env, static_cast<int32_t>(NfcState::STATE_TURNING_ON), &state_turning_on);
49     napi_property_descriptor desc[] = {
50         DECLARE_NAPI_STATIC_PROPERTY("STATE_OFF", state_off),
51         DECLARE_NAPI_STATIC_PROPERTY("STATE_TURNING_OFF", state_turning_off),
52         DECLARE_NAPI_STATIC_PROPERTY("STATE_ON", state_on),
53         DECLARE_NAPI_STATIC_PROPERTY("STATE_TURNING_ON", state_turning_on),
54     };
55     napi_value result = nullptr;
56     napi_define_class(env, "NfcState", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
57         sizeof(desc) / sizeof(*desc), desc, &result);
58     napi_set_named_property(env, exports, "NfcState", result);
59     return exports;
60 }
61 
InitJs(napi_env env,napi_value exports)62 static napi_value InitJs(napi_env env, napi_value exports)
63 {
64     DebugLog("Init, nfc_napi_controller");
65     napi_property_descriptor desc[] = {
66         DECLARE_NAPI_FUNCTION("openNfc", OpenNfc), // @deprecated since 9
67         DECLARE_NAPI_FUNCTION("enableNfc", EnableNfc),
68         DECLARE_NAPI_FUNCTION("closeNfc", CloseNfc), // @deprecated since 9
69         DECLARE_NAPI_FUNCTION("disableNfc", DisableNfc),
70         DECLARE_NAPI_FUNCTION("getNfcState", GetNfcState),
71         DECLARE_NAPI_FUNCTION("isNfcAvailable", IsNfcAvailable),
72         DECLARE_NAPI_FUNCTION("isNfcOpen", IsNfcOpen),
73         DECLARE_NAPI_FUNCTION("on", On),
74         DECLARE_NAPI_FUNCTION("off", Off),
75     };
76 
77     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
78     CreateEnumNfcState(env, exports);
79     return exports;
80 }
81 
82 static napi_module nfcControllerModule = {
83     .nm_version = 1,
84     .nm_flags = 0,
85     .nm_filename = NULL,
86     .nm_register_func = InitJs,
87     .nm_modname = "nfc.controller",
88     .nm_priv = ((void *)0),
89     .reserved = { 0 }
90 };
91 
RegisterModule(void)92 extern "C" __attribute__((constructor)) void RegisterModule(void)
93 {
94     napi_module_register(&nfcControllerModule);
95 }
96 }  // namespace KITS
97 }  // namespace NFC
98 }  // namespace OHOS
99