1 /*
2 * Copyright (c) 2022-2023 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 "nfc_impl.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <vector>
20 #include "nfc_vendor_adaptions.h"
21
22 #define HDF_LOG_TAG hdf_nfc_dal
23
24 namespace OHOS {
25 namespace HDI {
26 namespace Nfc {
27 namespace V1_0 {
28 static sptr<V1_0::INfcCallback> g_callbackV1_0 = nullptr;
29
EventCallback(unsigned char event,unsigned char status)30 static void EventCallback(unsigned char event, unsigned char status)
31 {
32 if (g_callbackV1_0 != nullptr) {
33 g_callbackV1_0->OnEvent((NfcEvent)event, (NfcStatus)status);
34 }
35 }
36
DataCallback(uint16_t len,uint8_t * data)37 static void DataCallback(uint16_t len, uint8_t *data)
38 {
39 if (g_callbackV1_0 != nullptr) {
40 std::vector<uint8_t> vec(data, data + len / sizeof(uint8_t));
41 g_callbackV1_0->OnData(vec);
42 }
43 }
44
NfcInterfaceImplGetInstance(void)45 extern "C" INfcInterface *NfcInterfaceImplGetInstance(void)
46 {
47 using OHOS::HDI::Nfc::V1_0::NfcImpl;
48 NfcImpl *service = new (std::nothrow) NfcImpl();
49 if (service == nullptr) {
50 return nullptr;
51 }
52 return service;
53 }
54
Open(const sptr<INfcCallback> & callbackObj,NfcStatus & status)55 int32_t NfcImpl::Open(const sptr<INfcCallback> &callbackObj, NfcStatus &status)
56 {
57 if (callbackObj == nullptr) {
58 HDF_LOGE("Open, callback is nullptr!");
59 return HDF_ERR_INVALID_PARAM;
60 }
61 g_callbackV1_0 = callbackObj;
62
63 int ret = adaptor_.VendorOpen(EventCallback, DataCallback);
64 if (ret == 0) {
65 status = NfcStatus::OK;
66 return HDF_SUCCESS;
67 }
68 status = NfcStatus::FAILED;
69 return HDF_FAILURE;
70 }
71
CoreInitialized(const std::vector<uint8_t> & data,NfcStatus & status)72 int32_t NfcImpl::CoreInitialized(const std::vector<uint8_t> &data, NfcStatus &status)
73 {
74 if (data.empty()) {
75 HDF_LOGE("CoreInitialized, data is nullptr!");
76 return HDF_ERR_INVALID_PARAM;
77 }
78 int ret = adaptor_.VendorCoreInitialized(data.size(), (uint8_t *)&data[0]);
79 if (ret == 0) {
80 status = NfcStatus::OK;
81 return HDF_SUCCESS;
82 }
83 status = NfcStatus::FAILED;
84 return HDF_FAILURE;
85 }
86
Prediscover(NfcStatus & status)87 int32_t NfcImpl::Prediscover(NfcStatus &status)
88 {
89 int ret = adaptor_.VendorPrediscover();
90 if (ret == 0) {
91 status = NfcStatus::OK;
92 return HDF_SUCCESS;
93 }
94 status = NfcStatus::FAILED;
95 return HDF_FAILURE;
96 }
97
Write(const std::vector<uint8_t> & data,NfcStatus & status)98 int32_t NfcImpl::Write(const std::vector<uint8_t> &data, NfcStatus &status)
99 {
100 if (data.empty()) {
101 HDF_LOGE("Write, data is nullptr!");
102 return HDF_ERR_INVALID_PARAM;
103 }
104 int ret = adaptor_.VendorWrite(data.size(), (uint8_t *)&data[0]);
105 if (ret == 0) {
106 status = NfcStatus::OK;
107 return HDF_SUCCESS;
108 }
109 status = NfcStatus::FAILED;
110 return HDF_FAILURE;
111 }
112
ControlGranted(NfcStatus & status)113 int32_t NfcImpl::ControlGranted(NfcStatus &status)
114 {
115 int ret = adaptor_.VendorControlGranted();
116 if (ret == 0) {
117 status = NfcStatus::OK;
118 return HDF_SUCCESS;
119 }
120 status = NfcStatus::FAILED;
121 return HDF_FAILURE;
122 }
123
PowerCycle(NfcStatus & status)124 int32_t NfcImpl::PowerCycle(NfcStatus &status)
125 {
126 int ret = adaptor_.VendorPowerCycle();
127 if (ret == 0) {
128 status = NfcStatus::OK;
129 return HDF_SUCCESS;
130 }
131 status = NfcStatus::FAILED;
132 return HDF_FAILURE;
133 }
134
Close(NfcStatus & status)135 int32_t NfcImpl::Close(NfcStatus &status)
136 {
137 g_callbackV1_0 = nullptr;
138 int ret = adaptor_.VendorClose(true);
139 if (ret == 0) {
140 status = NfcStatus::OK;
141 return HDF_SUCCESS;
142 }
143 status = NfcStatus::FAILED;
144 return HDF_FAILURE;
145 }
146
Ioctl(NfcCommand cmd,const std::vector<uint8_t> & data,NfcStatus & status)147 int32_t NfcImpl::Ioctl(NfcCommand cmd, const std::vector<uint8_t> &data, NfcStatus &status)
148 {
149 if (data.empty()) {
150 HDF_LOGE("Ioctl, data is nullptr!");
151 return HDF_ERR_INVALID_PARAM;
152 }
153 int ret = adaptor_.VendorIoctl(data.size(), (uint8_t *)&data[0]);
154 if (ret == 0) {
155 status = NfcStatus::OK;
156 return HDF_SUCCESS;
157 }
158 status = NfcStatus::FAILED;
159 return HDF_FAILURE;
160 }
161 } // V1_0
162 } // Nfc
163 } // HDI
164 } // OHOS
165