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 <iproxy_broker.h>
20 #include <vector>
21 #include "nfc_vendor_adaptions.h"
22
23 #define HDF_LOG_TAG hdf_nfc_dal
24
25 #ifdef LOG_DOMAIN
26 #undef LOG_DOMAIN
27 #endif
28
29 #define LOG_DOMAIN 0xD000306
30
31 namespace OHOS {
32 namespace HDI {
33 namespace Nfc {
34 namespace V1_1 {
35 static sptr<INfcCallback> g_callbackV1_1 = nullptr;
36 static std::mutex g_callbacksMutex {};
37
EventCallback(unsigned char event,unsigned char status)38 static void EventCallback(unsigned char event, unsigned char status)
39 {
40 std::lock_guard<std::mutex> guard(g_callbacksMutex);
41 if (g_callbackV1_1 != nullptr) {
42 g_callbackV1_1->OnEvent((NfcEvent)event, (NfcStatus)status);
43 }
44 }
45
DataCallback(uint16_t len,uint8_t * data)46 static void DataCallback(uint16_t len, uint8_t *data)
47 {
48 std::lock_guard<std::mutex> guard(g_callbacksMutex);
49 if (g_callbackV1_1 != nullptr) {
50 std::vector<uint8_t> vec(data, data + len / sizeof(uint8_t));
51 g_callbackV1_1->OnData(vec);
52 }
53 }
54
NfcInterfaceImplGetInstance(void)55 extern "C" INfcInterface *NfcInterfaceImplGetInstance(void)
56 {
57 using OHOS::HDI::Nfc::V1_1::NfcImpl;
58 NfcImpl *service = new (std::nothrow) NfcImpl();
59 if (service == nullptr) {
60 return nullptr;
61 }
62 return service;
63 }
64
NfcImpl()65 NfcImpl::NfcImpl()
66 {
67 remoteDeathRecipient_ =
68 new RemoteDeathRecipient(std::bind(&NfcImpl::OnRemoteDied, this, std::placeholders::_1));
69 }
70
~NfcImpl()71 NfcImpl::~NfcImpl()
72 {
73 HDF_LOGI("~NfcImpl");
74 std::lock_guard<std::mutex> guard(g_callbacksMutex);
75 if (callbacks_ != nullptr) {
76 RemoveNfcDeathRecipient(callbacks_);
77 callbacks_ = nullptr;
78 }
79 }
80
Open(const sptr<INfcCallback> & callbackObj,NfcStatus & status)81 int32_t NfcImpl::Open(const sptr<INfcCallback> &callbackObj, NfcStatus &status)
82 {
83 HDF_LOGI("NfcImpl::Open");
84 {
85 std::lock_guard<std::mutex> guard(g_callbacksMutex);
86 if (callbackObj == nullptr) {
87 HDF_LOGE("Open, callback is nullptr!");
88 return HDF_ERR_INVALID_PARAM;
89 }
90 g_callbackV1_1 = callbackObj;
91 callbacks_ = callbackObj;
92 AddNfcDeathRecipient(callbacks_);
93 }
94 int ret = adaptor_.VendorOpen(EventCallback, DataCallback);
95 if (ret == 0) {
96 status = NfcStatus::OK;
97 return HDF_SUCCESS;
98 }
99 status = NfcStatus::FAILED;
100 return HDF_FAILURE;
101 }
102
CoreInitialized(const std::vector<uint8_t> & data,NfcStatus & status)103 int32_t NfcImpl::CoreInitialized(const std::vector<uint8_t> &data, NfcStatus &status)
104 {
105 if (data.empty()) {
106 HDF_LOGE("CoreInitialized, data is nullptr!");
107 return HDF_ERR_INVALID_PARAM;
108 }
109 int ret = adaptor_.VendorCoreInitialized(data.size(), (uint8_t *)&data[0]);
110 if (ret == 0) {
111 status = NfcStatus::OK;
112 return HDF_SUCCESS;
113 }
114 status = NfcStatus::FAILED;
115 return HDF_FAILURE;
116 }
117
Prediscover(NfcStatus & status)118 int32_t NfcImpl::Prediscover(NfcStatus &status)
119 {
120 int ret = adaptor_.VendorPrediscover();
121 if (ret == 0) {
122 status = NfcStatus::OK;
123 return HDF_SUCCESS;
124 }
125 status = NfcStatus::FAILED;
126 return HDF_FAILURE;
127 }
128
Write(const std::vector<uint8_t> & data,NfcStatus & status)129 int32_t NfcImpl::Write(const std::vector<uint8_t> &data, NfcStatus &status)
130 {
131 if (data.empty()) {
132 HDF_LOGE("Write, data is nullptr!");
133 return HDF_ERR_INVALID_PARAM;
134 }
135 int ret = adaptor_.VendorWrite(data.size(), (uint8_t *)&data[0]);
136 if (ret == 0) {
137 status = NfcStatus::OK;
138 return HDF_SUCCESS;
139 }
140 status = NfcStatus::FAILED;
141 return HDF_FAILURE;
142 }
143
ControlGranted(NfcStatus & status)144 int32_t NfcImpl::ControlGranted(NfcStatus &status)
145 {
146 int ret = adaptor_.VendorControlGranted();
147 if (ret == 0) {
148 status = NfcStatus::OK;
149 return HDF_SUCCESS;
150 }
151 status = NfcStatus::FAILED;
152 return HDF_FAILURE;
153 }
154
PowerCycle(NfcStatus & status)155 int32_t NfcImpl::PowerCycle(NfcStatus &status)
156 {
157 int ret = adaptor_.VendorPowerCycle();
158 if (ret == 0) {
159 status = NfcStatus::OK;
160 return HDF_SUCCESS;
161 }
162 status = NfcStatus::FAILED;
163 return HDF_FAILURE;
164 }
165
Close(NfcStatus & status)166 int32_t NfcImpl::Close(NfcStatus &status)
167 {
168 HDF_LOGI("NfcImpl::Close");
169 int ret = adaptor_.VendorClose(false);
170 std::lock_guard<std::mutex> guard(g_callbacksMutex);
171 g_callbackV1_1 = nullptr;
172 if (callbacks_ != nullptr) {
173 RemoveNfcDeathRecipient(callbacks_);
174 callbacks_ = nullptr;
175 }
176 if (ret == 0) {
177 status = NfcStatus::OK;
178 return HDF_SUCCESS;
179 }
180 status = NfcStatus::FAILED;
181 return HDF_FAILURE;
182 }
183
Ioctl(NfcCommand cmd,const std::vector<uint8_t> & data,NfcStatus & status)184 int32_t NfcImpl::Ioctl(NfcCommand cmd, const std::vector<uint8_t> &data, NfcStatus &status)
185 {
186 if (data.empty()) {
187 HDF_LOGE("Ioctl, data is nullptr!");
188 return HDF_ERR_INVALID_PARAM;
189 }
190 int ret = adaptor_.VendorIoctl(data.size(), (uint8_t *)&data[0]);
191 if (ret == 0) {
192 status = NfcStatus::OK;
193 return HDF_SUCCESS;
194 }
195 status = NfcStatus::FAILED;
196 return HDF_FAILURE;
197 }
198
IoctlWithResponse(NfcCommand cmd,const std::vector<uint8_t> & data,std::vector<uint8_t> & response,NfcStatus & status)199 int32_t NfcImpl::IoctlWithResponse(NfcCommand cmd, const std::vector<uint8_t> &data,
200 std::vector<uint8_t> &response, NfcStatus &status)
201 {
202 if (data.empty()) {
203 HDF_LOGE("NfcImpl::IoctlWithResponse, data is nullptr!");
204 return HDF_ERR_INVALID_PARAM;
205 }
206 int ret = adaptor_.VendorIoctlWithResponse(cmd, (void*)&data[0], data.size(), response);
207 if (ret == 0) {
208 status = NfcStatus::OK;
209 return HDF_SUCCESS;
210 }
211 status = NfcStatus::FAILED;
212 return HDF_FAILURE;
213 }
214
GetVendorConfig(NfcVendorConfig & config,NfcStatus & status)215 int32_t NfcImpl::GetVendorConfig(NfcVendorConfig &config, NfcStatus &status)
216 {
217 if (adaptor_.VendorGetConfig(config) != HDF_SUCCESS) {
218 HDF_LOGE("GetConfig, fail to get vendor config!");
219 status = NfcStatus::FAILED;
220 return HDF_FAILURE;
221 }
222 status = NfcStatus::OK;
223 return HDF_SUCCESS;
224 }
225
DoFactoryReset(NfcStatus & status)226 int32_t NfcImpl::DoFactoryReset(NfcStatus &status)
227 {
228 int ret = adaptor_.VendorFactoryReset();
229 if (ret == 0) {
230 status = NfcStatus::OK;
231 return HDF_SUCCESS;
232 }
233 status = NfcStatus::FAILED;
234 return HDF_FAILURE;
235 }
236
Shutdown(NfcStatus & status)237 int32_t NfcImpl::Shutdown(NfcStatus &status)
238 {
239 int ret = adaptor_.VendorShutdownCase();
240 if (ret == 0) {
241 status = NfcStatus::OK;
242 return HDF_SUCCESS;
243 }
244 status = NfcStatus::FAILED;
245 return HDF_FAILURE;
246 }
247
OnRemoteDied(const wptr<IRemoteObject> & object)248 void NfcImpl::OnRemoteDied(const wptr<IRemoteObject> &object)
249 {
250 HDF_LOGW("NfcImpl::OnRemoteDied");
251 {
252 std::lock_guard<std::mutex> guard(g_callbacksMutex);
253 callbacks_ = nullptr;
254 }
255 NfcStatus status = NfcStatus::FAILED;
256 int32_t ret = Close(status);
257 if (ret != HDF_SUCCESS) {
258 HDF_LOGE("OnRemoteDied, Close failed, status(%{public}d)!", status);
259 }
260 }
261
AddNfcDeathRecipient(const sptr<INfcCallback> & callbackObj)262 int32_t NfcImpl::AddNfcDeathRecipient(const sptr<INfcCallback> &callbackObj)
263 {
264 if (callbackObj == nullptr) {
265 HDF_LOGE("AddNfcDeathRecipient callbackobj nullptr");
266 return HDF_FAILURE;
267 }
268 const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<INfcCallback>(callbackObj);
269 if (remote == nullptr) {
270 HDF_LOGE("AddNfcDeathRecipient remote nullptr");
271 return HDF_FAILURE;
272 }
273 bool result = remote->AddDeathRecipient(remoteDeathRecipient_);
274 if (!result) {
275 HDF_LOGE("NfcImpl AddDeathRecipient failed!");
276 return HDF_FAILURE;
277 }
278 return HDF_SUCCESS;
279 }
280
RemoveNfcDeathRecipient(const sptr<INfcCallback> & callbackObj)281 int32_t NfcImpl::RemoveNfcDeathRecipient(const sptr<INfcCallback> &callbackObj)
282 {
283 if (callbackObj == nullptr) {
284 HDF_LOGE("RemoveNfcDeathRecipient callbackobj nullptr");
285 return HDF_FAILURE;
286 }
287 const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<INfcCallback>(callbackObj);
288 if (remote == nullptr) {
289 HDF_LOGE("RemoveNfcDeathRecipient remote nullptr");
290 return HDF_FAILURE;
291 }
292 bool result = remote->RemoveDeathRecipient(remoteDeathRecipient_);
293 if (!result) {
294 HDF_LOGE("NfcImpl RemoveDeathRecipient failed!");
295 return HDF_FAILURE;
296 }
297 return HDF_SUCCESS;
298 }
299 } // V1_1
300 } // Nfc
301 } // HDI
302 } // OHOS
303