1 /*
2 * Copyright (C) 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 #include "nci_tag_proxy.h"
16 #include "nci_native_selector.h"
17
18 namespace OHOS {
19 namespace NFC {
20 namespace NCI {
NciTagProxy()21 NciTagProxy::NciTagProxy()
22 {
23 nciTagInterface_ = NciNativeSelector::GetInstance().GetNciTagInterface();
24 }
25
26
27 /**
28 * @brief Set tag listener to receive tag status.
29 * @param listener The listener to receive tag status.
30 */
SetTagListener(std::weak_ptr<ITagListener> listener)31 void NciTagProxy::SetTagListener(std::weak_ptr<ITagListener> listener)
32 {
33 if (nciTagInterface_) {
34 return nciTagInterface_->SetTagListener(listener);
35 }
36 }
37
38 /**
39 * @brief Get the discovered technologies found.
40 * @param tagDiscId The tag discovered id given from nci stack.
41 * @return The technologies list.
42 */
GetTechList(uint32_t tagDiscId)43 std::vector<int> NciTagProxy::GetTechList(uint32_t tagDiscId)
44 {
45 if (nciTagInterface_) {
46 return nciTagInterface_->GetTechList(tagDiscId);
47 }
48 return std::vector<int>();
49 }
50
51 /**
52 * @brief Get the connected technology, the technology specific when call Connect(uint32_t technology).
53 * @param tagDiscId The tag discovered id given from nci stack.
54 * @return The connected technology.
55 */
GetConnectedTech(uint32_t tagDiscId)56 uint32_t NciTagProxy::GetConnectedTech(uint32_t tagDiscId)
57 {
58 if (nciTagInterface_) {
59 return nciTagInterface_->GetConnectedTech(tagDiscId);
60 }
61 return 0;
62 }
63
64 /**
65 * @brief Get the extra data of all discovered technologies, Key and value.
66 * @param tagDiscId The tag discovered id given from nci stack.
67 * @return The extra data of all discovered technologies.
68 */
GetTechExtrasData(uint32_t tagDiscId)69 std::vector<AppExecFwk::PacMap> NciTagProxy::GetTechExtrasData(uint32_t tagDiscId)
70 {
71 if (nciTagInterface_) {
72 return nciTagInterface_->GetTechExtrasData(tagDiscId);
73 }
74 return std::vector<AppExecFwk::PacMap>();
75 }
76
77 /**
78 * @brief Get the uid of discovered tag.
79 * @param tagDiscId The tag discovered id given from nci stack.
80 * @return The uid of discovered tag, such as DD236DEB.
81 */
GetTagUid(uint32_t tagDiscId)82 std::string NciTagProxy::GetTagUid(uint32_t tagDiscId)
83 {
84 if (nciTagInterface_) {
85 return nciTagInterface_->GetTagUid(tagDiscId);
86 }
87 return {};
88 }
89
90 /**
91 * @brief Connect the tag.
92 * @param tagDiscId The tag discovered id given from nci stack.
93 * @param technology The technology of the tag using to connect.
94 * @return True if success, otherwise false.
95 */
Connect(uint32_t tagDiscId,uint32_t technology)96 bool NciTagProxy::Connect(uint32_t tagDiscId, uint32_t technology)
97 {
98 if (nciTagInterface_) {
99 return nciTagInterface_->Connect(tagDiscId, technology);
100 }
101 return false;
102 }
103
104 /**
105 * @brief Disconnect the tag
106 * @param tagDiscId The tag discovered id given from nci stack.
107 * @return True if success, otherwise false.
108 */
Disconnect(uint32_t tagDiscId)109 bool NciTagProxy::Disconnect(uint32_t tagDiscId)
110 {
111 if (nciTagInterface_) {
112 return nciTagInterface_->Disconnect(tagDiscId);
113 }
114 return false;
115 }
116
117 /**
118 * @brief Reconnect the tag
119 * @param tagDiscId The tag discovered id given from nci stack.
120 * @return True if success, otherwise false.
121 */
Reconnect(uint32_t tagDiscId)122 bool NciTagProxy::Reconnect(uint32_t tagDiscId)
123 {
124 if (nciTagInterface_) {
125 return nciTagInterface_->Reconnect(tagDiscId);
126 }
127 return false;
128 }
129
130 /**
131 * @brief Send command to tag and receive response.
132 * @param tagDiscId The tag discovered id given from nci stack.
133 * @param command The command to send.
134 * @param response The response from the tag.
135 * @return The status code to transceive the command.
136 */
Transceive(uint32_t tagDiscId,const std::string & command,std::string & response)137 int NciTagProxy::Transceive(uint32_t tagDiscId, const std::string& command, std::string& response)
138 {
139 if (nciTagInterface_) {
140 return nciTagInterface_->Transceive(tagDiscId, command, response);
141 }
142 return 0;
143 }
144
145 /**
146 * @brief Read the NDEF tag.
147 * @param tagDiscId The tag discovered id given from nci stack.
148 * @return The data read from NDEF tag.
149 */
ReadNdef(uint32_t tagDiscId)150 std::string NciTagProxy::ReadNdef(uint32_t tagDiscId)
151 {
152 if (nciTagInterface_) {
153 return nciTagInterface_->ReadNdef(tagDiscId);
154 }
155 return {};
156 }
157
158 /**
159 * @brief Find the NDEF tag technology from the NDEF tag data.
160 * @param tagDiscId The tag discovered id given from nci stack.
161 * @return The data read from NDEF tag.
162 */
FindNdefTech(uint32_t tagDiscId)163 std::string NciTagProxy::FindNdefTech(uint32_t tagDiscId)
164 {
165 if (nciTagInterface_) {
166 return nciTagInterface_->FindNdefTech(tagDiscId);
167 }
168 return {};
169 }
170
171 /**
172 * @brief Write command to NDEF tag.
173 * @param tagDiscId The tag discovered id given from nci stack.
174 * @param command The command to write to NDEF tag.
175 * @return True if success, otherwise false.
176 */
WriteNdef(uint32_t tagDiscId,std::string & command)177 bool NciTagProxy::WriteNdef(uint32_t tagDiscId, std::string& command)
178 {
179 if (nciTagInterface_) {
180 return nciTagInterface_->WriteNdef(tagDiscId, command);
181 }
182 return false;
183 }
184
185 /**
186 * @brief Format NDEF tag.
187 * @param tagDiscId The tag discovered id given from nci stack.
188 * @param key The key used to format the NDEF.
189 * @return True if success, otherwise false.
190 */
FormatNdef(uint32_t tagDiscId,const std::string & key)191 bool NciTagProxy::FormatNdef(uint32_t tagDiscId, const std::string& key)
192 {
193 if (nciTagInterface_) {
194 return nciTagInterface_->FormatNdef(tagDiscId, key);
195 }
196 return false;
197 }
198
199 /**
200 * @brief Format NDEF tag.
201 * @param key The key used to format the NDEF.
202 * @return True if success, otherwise false.
203 */
CanMakeReadOnly(uint32_t ndefType)204 bool NciTagProxy::CanMakeReadOnly(uint32_t ndefType)
205 {
206 if (nciTagInterface_) {
207 return nciTagInterface_->CanMakeReadOnly(ndefType);
208 }
209 return true;
210 }
211
212 /**
213 * @brief Set the NDEF to be read only.
214 * @param tagDiscId The tag discovered id given from nci stack.
215 * @return True if success, otherwise false.
216 */
SetNdefReadOnly(uint32_t tagDiscId)217 bool NciTagProxy::SetNdefReadOnly(uint32_t tagDiscId)
218 {
219 if (nciTagInterface_) {
220 return nciTagInterface_->SetNdefReadOnly(tagDiscId);
221 }
222 return false;
223 }
224
225 /**
226 * @brief Detect the NDEF info, includes the max size and the mode.
227 * @param tagDiscId The tag discovered id given from nci stack.
228 * @param ndefInfo The output to save the detected result.
229 * @return True if success, otherwise false.
230 */
DetectNdefInfo(uint32_t tagDiscId,std::vector<int> & ndefInfo)231 bool NciTagProxy::DetectNdefInfo(uint32_t tagDiscId, std::vector<int>& ndefInfo)
232 {
233 if (nciTagInterface_) {
234 return nciTagInterface_->DetectNdefInfo(tagDiscId, ndefInfo);
235 }
236 return false;
237 }
238
239 /**
240 * @brief Check current tag is field on or not.
241 * @param tagDiscId The tag discovered id given from nci stack.
242 * @return True if current tag is field on, otherwise false.
243 */
IsTagFieldOn(uint32_t tagDiscId)244 bool NciTagProxy::IsTagFieldOn(uint32_t tagDiscId)
245 {
246 if (nciTagInterface_) {
247 return nciTagInterface_->IsTagFieldOn(tagDiscId);
248 }
249 return false;
250 }
251
252 /**
253 * @brief Start field on checking for tag. If tag lost, callback to notify.
254 * @param tagDiscId The tag discovered id given from nci stack.
255 * @param callback The callback to notify.
256 * @param delayedMs The delayed time to check the tag lost.
257 */
StartFieldOnChecking(uint32_t tagDiscId,uint32_t delayedMs)258 void NciTagProxy::StartFieldOnChecking(uint32_t tagDiscId, uint32_t delayedMs)
259 {
260 if (nciTagInterface_) {
261 return nciTagInterface_->StartFieldOnChecking(tagDiscId, delayedMs);
262 }
263 }
264
265 /**
266 * @brief Stop field on checking for tag if tag is connected.
267 */
StopFieldChecking()268 void NciTagProxy::StopFieldChecking()
269 {
270 if (nciTagInterface_) {
271 return nciTagInterface_->StopFieldChecking();
272 }
273 }
274
275 /**
276 * @brief Config the timeout value to nfc controller when read or write tag.
277 * @param tagDiscId The tag discovered id given from nci stack.
278 * @param timeout The timeout value to config.
279 * @param technology The technology to config.
280 */
SetTimeout(uint32_t tagDiscId,uint32_t timeout,uint32_t technology)281 void NciTagProxy::SetTimeout(uint32_t tagDiscId, uint32_t timeout, uint32_t technology)
282 {
283 if (nciTagInterface_) {
284 return nciTagInterface_->SetTimeout(tagDiscId, timeout, technology);
285 }
286 }
287
288 /**
289 * @brief Get the timeout value to nfc controller when read or write tag.
290 * @param tagDiscId The tag discovered id given from nci stack.
291 * @param timeout The timeout value to config.
292 * @param technology The technology to config.
293 */
GetTimeout(uint32_t tagDiscId,uint32_t & timeout,uint32_t technology)294 void NciTagProxy::GetTimeout(uint32_t tagDiscId, uint32_t &timeout, uint32_t technology)
295 {
296 if (nciTagInterface_) {
297 return nciTagInterface_->GetTimeout(tagDiscId, timeout, technology);
298 }
299 }
300
301 /**
302 * @brief Reset the timeout value to nfc controller when read or write tag.
303 * @param tagDiscId The tag discovered id given from nci stack.
304 */
ResetTimeout(uint32_t tagDiscId)305 void NciTagProxy::ResetTimeout(uint32_t tagDiscId)
306 {
307 if (nciTagInterface_) {
308 return nciTagInterface_->ResetTimeout(tagDiscId);
309 }
310 }
311
312 /**
313 * @brief Get the max transceive length of ISO-DEP technology.
314 * @return The max transceive length of ISO-DEP technology.
315 */
GetIsoDepMaxTransceiveLength()316 uint32_t NciTagProxy::GetIsoDepMaxTransceiveLength()
317 {
318 if (nciTagInterface_) {
319 return nciTagInterface_->GetIsoDepMaxTransceiveLength();
320 }
321 return 0;
322 }
323
324 /**
325 * @brief Check if the nfc controller support extended APDU or not.
326 * @return True if the nfc controller support extended APDU, otherwise false.
327 */
IsExtendedLengthApduSupported()328 bool NciTagProxy::IsExtendedLengthApduSupported()
329 {
330 if (nciTagInterface_) {
331 return nciTagInterface_->IsExtendedLengthApduSupported();
332 }
333 return true;
334 }
335
336 /**
337 * @brief Build the tech mask by all given technologies.
338 * @param discTech The given technology list.
339 * @return The technology mask.
340 */
GetTechMaskFromTechList(const std::vector<uint32_t> & discTech)341 uint16_t NciTagProxy::GetTechMaskFromTechList(const std::vector<uint32_t> &discTech)
342 {
343 if (nciTagInterface_) {
344 return nciTagInterface_->GetTechMaskFromTechList(discTech);
345 }
346 return 0;
347 }
348
349 /**
350 * @brief Try parse harPackage in vendor.
351 * @return True if vendor parsed harPackage, otherwise false.
352 */
VendorParseHarPackage(std::vector<std::string> & harPackages)353 bool NciTagProxy::VendorParseHarPackage(std::vector<std::string> &harPackages)
354 {
355 if (nciTagInterface_) {
356 return nciTagInterface_->VendorParseHarPackage(harPackages);
357 }
358 return false;
359 }
360
361 /**
362 * @brief Get browser bundle name of the vendor.
363 * @return Browser bundle name of the vendor.
364 */
GetVendorBrowserBundleName()365 std::string NciTagProxy::GetVendorBrowserBundleName()
366 {
367 if (nciTagInterface_) {
368 return nciTagInterface_->GetVendorBrowserBundleName();
369 }
370 return "";
371 }
372
373 /**
374 * @brief Get appGallery bundle name of the vendor.
375 * @return appGallery bundle name of the vendor.
376 */
GetVendorAppGalleryBundleName()377 std::string NciTagProxy::GetVendorAppGalleryBundleName()
378 {
379 if (nciTagInterface_) {
380 return nciTagInterface_->GetVendorAppGalleryBundleName();
381 }
382 return "";
383 }
384 #ifdef VENDOR_APPLICATIONS_ENABLED
385 /**
386 * @brief Check current ipc thread come from vendor process or not.
387 * @return Is Vendor Process.
388 */
IsVendorProcess()389 bool NciTagProxy::IsVendorProcess()
390 {
391 if (nciTagInterface_) {
392 return nciTagInterface_->IsVendorProcess();
393 }
394 return false;
395 }
396 #endif
397 } // namespace NCI
398 } // namespace NFC
399 } // namespace OHOS