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
16 #include "nfc_napi_controller_adapter.h"
17 #include <vector>
18 #include "loghelper.h"
19 #include "nfc_controller.h"
20 #include "nfc_sdk_common.h"
21 #include "nfc_napi_ctrl_utils.h"
22 #include "nfc_ha_event_report.h"
23
24 namespace OHOS {
25 namespace NFC {
26 namespace KITS {
OpenNfc(napi_env env,napi_callback_info info)27 napi_value OpenNfc(napi_env env, napi_callback_info info)
28 {
29 NfcHaEventReport eventReport(SDK_NAME, "OpenNfc");
30 NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
31 int status = nfcCtrl.TurnOn();
32 if (status == ERR_NONE) {
33 eventReport.ReportSdkEvent(RESULT_SUCCESS, status);
34 } else {
35 eventReport.ReportSdkEvent(RESULT_FAIL, status);
36 }
37 napi_value result;
38 napi_get_boolean(env, (status == KITS::ERR_NONE), &result);
39 return result;
40 }
41
EnableNfc(napi_env env,napi_callback_info info)42 napi_value EnableNfc(napi_env env, napi_callback_info info)
43 {
44 NfcHaEventReport eventReport(SDK_NAME, "EnableNfc");
45 NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
46 int status = nfcCtrl.TurnOn();
47 if (status == ERR_NONE) {
48 eventReport.ReportSdkEvent(RESULT_SUCCESS, status);
49 } else {
50 eventReport.ReportSdkEvent(RESULT_FAIL, status);
51 }
52 CheckNfcStatusCodeAndThrow(env, status, "enableNfc");
53 return CreateUndefined(env);
54 }
55
CloseNfc(napi_env env,napi_callback_info info)56 napi_value CloseNfc(napi_env env, napi_callback_info info)
57 {
58 NfcHaEventReport eventReport(SDK_NAME, "CloseNfc");
59 NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
60 int status = nfcCtrl.TurnOff();
61 if (status == ERR_NONE) {
62 eventReport.ReportSdkEvent(RESULT_SUCCESS, status);
63 } else {
64 eventReport.ReportSdkEvent(RESULT_FAIL, status);
65 }
66 napi_value result;
67 napi_get_boolean(env, (status == KITS::ERR_NONE), &result);
68 return result;
69 }
70
DisableNfc(napi_env env,napi_callback_info info)71 napi_value DisableNfc(napi_env env, napi_callback_info info)
72 {
73 NfcHaEventReport eventReport(SDK_NAME, "DisableNfc");
74 NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
75 int status = nfcCtrl.TurnOff();
76 if (status == ERR_NONE) {
77 eventReport.ReportSdkEvent(RESULT_SUCCESS, status);
78 } else {
79 eventReport.ReportSdkEvent(RESULT_FAIL, status);
80 }
81 CheckNfcStatusCodeAndThrow(env, status, "disableNfc");
82 return CreateUndefined(env);
83 }
84
GetNfcState(napi_env env,napi_callback_info info)85 napi_value GetNfcState(napi_env env, napi_callback_info info)
86 {
87 NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
88 napi_value result;
89 napi_create_int32(env, nfcCtrl.GetNfcState(), &result);
90 return result;
91 }
92
IsNfcAvailable(napi_env env,napi_callback_info info)93 napi_value IsNfcAvailable(napi_env env, napi_callback_info info)
94 {
95 NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
96 napi_value result;
97 napi_get_boolean(env, nfcCtrl.IsNfcAvailable(), &result);
98 return result;
99 }
100
IsNfcOpen(napi_env env,napi_callback_info info)101 napi_value IsNfcOpen(napi_env env, napi_callback_info info)
102 {
103 NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
104 bool isOpen = false;
105 int statusCode = nfcCtrl.IsNfcOpen(isOpen);
106 if (statusCode != KITS::ERR_NONE) {
107 ErrorLog("IsNfcOpen, statusCode = %{public}d", statusCode);
108 }
109 napi_value result;
110 napi_get_boolean(env, isOpen, &result);
111 return result;
112 }
113 } // namespace KITS
114 } // namespace NFC
115 } // namespace OHOS
116