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