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