• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (data.size() > VENDOR_IOCTL_INPUT_MAX_LEN) {
197         HDF_LOGE("NfcImpl::IoctlWithResponse, dataLen is invalid!");
198         return HDF_ERR_INVALID_PARAM;
199     }
200     int ret = adaptor_.VendorIoctlWithResponse(cmd, (void*)&data[0], response);
201     if (ret == 0) {
202         status = NfcStatus::OK;
203         return HDF_SUCCESS;
204     }
205     status = NfcStatus::FAILED;
206     return HDF_FAILURE;
207 }
208 
GetVendorConfig(NfcVendorConfig & config,NfcStatus & status)209 int32_t NfcImpl::GetVendorConfig(NfcVendorConfig &config, NfcStatus &status)
210 {
211     if (adaptor_.VendorGetConfig(config) != HDF_SUCCESS) {
212         HDF_LOGE("GetConfig, fail to get vendor config!");
213         status = NfcStatus::FAILED;
214         return HDF_FAILURE;
215     }
216     status = NfcStatus::OK;
217     return HDF_SUCCESS;
218 }
219 
DoFactoryReset(NfcStatus & status)220 int32_t NfcImpl::DoFactoryReset(NfcStatus &status)
221 {
222     int ret = adaptor_.VendorFactoryReset();
223     if (ret == 0) {
224         status = NfcStatus::OK;
225         return HDF_SUCCESS;
226     }
227     status = NfcStatus::FAILED;
228     return HDF_FAILURE;
229 }
230 
Shutdown(NfcStatus & status)231 int32_t NfcImpl::Shutdown(NfcStatus &status)
232 {
233     int ret = adaptor_.VendorShutdownCase();
234     if (ret == 0) {
235         status = NfcStatus::OK;
236         return HDF_SUCCESS;
237     }
238     status = NfcStatus::FAILED;
239     return HDF_FAILURE;
240 }
241 
OnRemoteDied(const wptr<IRemoteObject> & object)242 void NfcImpl::OnRemoteDied(const wptr<IRemoteObject> &object)
243 {
244     callbacks_ = nullptr;
245     NfcStatus status = NfcStatus::FAILED;
246     int32_t ret = Close(status);
247     if (ret != HDF_SUCCESS) {
248         HDF_LOGE("OnRemoteDied, Close failed, status(%{public}d)!", status);
249     }
250 }
251 
AddNfcDeathRecipient(const sptr<INfcCallback> & callbackObj)252 int32_t NfcImpl::AddNfcDeathRecipient(const sptr<INfcCallback> &callbackObj)
253 {
254     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<INfcCallback>(callbackObj);
255     bool result = remote->AddDeathRecipient(remoteDeathRecipient_);
256     if (!result) {
257         HDF_LOGE("NfcImpl AddDeathRecipient failed!");
258         return HDF_FAILURE;
259     }
260     return HDF_SUCCESS;
261 }
262 
RemoveNfcDeathRecipient(const sptr<INfcCallback> & callbackObj)263 int32_t NfcImpl::RemoveNfcDeathRecipient(const sptr<INfcCallback> &callbackObj)
264 {
265     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<INfcCallback>(callbackObj);
266     bool result = remote->RemoveDeathRecipient(remoteDeathRecipient_);
267     if (!result) {
268         HDF_LOGE("NfcImpl RemoveDeathRecipient failed!");
269         return HDF_FAILURE;
270     }
271     return HDF_SUCCESS;
272 }
273 } // V1_1
274 } // Nfc
275 } // HDI
276 } // OHOS
277