1 /*
2 * Copyright (C) 2022 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 "nfcc_host.h"
16
17 #include "infcc_host.h"
18 #include "loghelper.h"
19 #include "ndef_tag.h"
20 #include "nfcc_nci_adapter.h"
21 #include "nfc_chip_type_parser.h"
22
23 namespace OHOS {
24 namespace NFC {
25 namespace NCI {
26 std::weak_ptr<NfccHost::INfccHostListener> NfccHost::nfccHostListener_;
27
NfccHost(std::weak_ptr<INfccHostListener> listener)28 NfccHost::NfccHost(std::weak_ptr<INfccHostListener> listener)
29 {
30 nfccHostListener_ = listener;
31 }
32
SetNfccHostListener(std::weak_ptr<INfccHostListener> listener)33 void NfccHost::SetNfccHostListener(std::weak_ptr<INfccHostListener> listener)
34 {
35 nfccHostListener_ = listener;
36 }
37
~NfccHost()38 NfccHost::~NfccHost()
39 {
40 this->Deinitialize();
41 }
42
Initialize()43 bool NfccHost::Initialize()
44 {
45 DebugLog("NfccHost::Initialize");
46 if (!NfcChipTypeParser::IsSn110()) {
47 WarnLog("NfccHost::Initialize(): unsupported chip type");
48 return true;
49 }
50 return NfccNciAdapter::GetInstance().Initialize();
51 }
52
Deinitialize()53 bool NfccHost::Deinitialize()
54 {
55 DebugLog("NfccHost::Deinitialize");
56 if (!NfcChipTypeParser::IsSn110()) {
57 WarnLog("NfccHost::Deinitialize(): unsupported chip type");
58 return true;
59 }
60 return NfccNciAdapter::GetInstance().Deinitialize();
61 }
62
EnableDiscovery(uint16_t techMask,bool enableReaderMode,bool enableHostRouting,bool restart)63 void NfccHost::EnableDiscovery(uint16_t techMask, bool enableReaderMode, bool enableHostRouting, bool restart)
64 {
65 DebugLog("NfccHost::EnableDiscovery");
66 if (!NfcChipTypeParser::IsSn110()) {
67 WarnLog("NfccHost::EnableDiscovery(): unsupported chip type");
68 return;
69 }
70 NfccNciAdapter::GetInstance().EnableDiscovery(techMask, enableReaderMode, enableHostRouting, restart);
71 }
72
DisableDiscovery()73 void NfccHost::DisableDiscovery()
74 {
75 DebugLog("NfccHost::DisableDiscovery");
76 if (!NfcChipTypeParser::IsSn110()) {
77 WarnLog("NfccHost::DisableDiscovery(): unsupported chip type");
78 return;
79 }
80 NfccNciAdapter::GetInstance().DisableDiscovery();
81 }
82
SendRawFrame(std::string & rawData)83 bool NfccHost::SendRawFrame(std::string& rawData)
84 {
85 DebugLog("NfccHost::SendRawFrame");
86 if (!NfcChipTypeParser::IsSn110()) {
87 WarnLog("NfccHost::SendRawFrame(): unsupported chip type");
88 return true;
89 }
90 return NfccNciAdapter::GetInstance().SendRawFrame(rawData);
91 }
92
SetScreenStatus(unsigned char screenStateMask)93 bool NfccHost::SetScreenStatus(unsigned char screenStateMask)
94 {
95 DebugLog("NfccHost::SetScreenStatus");
96 if (!NfcChipTypeParser::IsSn110()) {
97 WarnLog("NfccHost::SetScreenStatus(): unsupported chip type");
98 return true;
99 }
100 NfccNciAdapter::GetInstance().SetScreenStatus(screenStateMask);
101 return true;
102 }
103
GetNciVersion()104 int NfccHost::GetNciVersion()
105 {
106 DebugLog("NfccHost::GetNciVersion");
107 if (!NfcChipTypeParser::IsSn110()) {
108 WarnLog("NfccHost::GetNciVersion(): unsupported chip type");
109 return 0;
110 }
111 return NfccNciAdapter::GetInstance().GetNciVersion();
112 }
113
SetSecureNfc(bool secure)114 bool NfccHost::SetSecureNfc(bool secure)
115 {
116 DebugLog("NfccHost::SetSecureNfc");
117 #ifdef _NFC_SERVICE_HCE_
118 NciBalCe::GetInstance().SetSecureNfc(secure);
119 #endif
120 return true;
121 }
122
GetIsoDepMaxTransceiveLength()123 int NfccHost::GetIsoDepMaxTransceiveLength()
124 {
125 DebugLog("NfccHost::GetIsoDepMaxTransceiveLength");
126 if (!NfcChipTypeParser::IsSn110()) {
127 WarnLog("NfccHost::GetIsoDepMaxTransceiveLength(): unsupported chip type");
128 return 0;
129 }
130 return NfccNciAdapter::GetInstance().GetIsoDepMaxTransceiveLength();
131 }
132
RegisterT3tIdentifier(std::string & t3tIdentifier)133 int NfccHost::RegisterT3tIdentifier(std::string& t3tIdentifier)
134 {
135 DebugLog("NfccHost::RegisterT3tIdentifier");
136 if (!NfcChipTypeParser::IsSn110()) {
137 WarnLog("NfccHost::RegisterT3tIdentifier(): unsupported chip type");
138 return 0;
139 }
140 return NfccNciAdapter::GetInstance().RegisterT3tIdentifier(t3tIdentifier);
141 }
142
DeregisterT3tIdentifier(std::string & t3tIdentifier)143 void NfccHost::DeregisterT3tIdentifier(std::string& t3tIdentifier)
144 {
145 DebugLog("NfccHost::DeregisterT3tIdentifier");
146 if (!NfcChipTypeParser::IsSn110()) {
147 WarnLog("NfccHost::DeregisterT3tIdentifier(): unsupported chip type");
148 return;
149 }
150 if (!t3tIdentifier.empty()) {
151 int handle = -1;
152 NfccNciAdapter::GetInstance().DeregisterT3tIdentifier(handle);
153 }
154 }
155
ClearT3tIdentifiersCache()156 void NfccHost::ClearT3tIdentifiersCache()
157 {
158 DebugLog("NfccHost::ClearT3tIdentifiersCache");
159 if (!NfcChipTypeParser::IsSn110()) {
160 WarnLog("NfccHost::ClearT3tIdentifiersCache(): unsupported chip type");
161 return;
162 }
163 NfccNciAdapter::GetInstance().ClearT3tIdentifiersCache();
164 }
165
GetLfT3tMax()166 int NfccHost::GetLfT3tMax()
167 {
168 DebugLog("NfccHost::GetLfT3tMax");
169 if (!NfcChipTypeParser::IsSn110()) {
170 WarnLog("NfccHost::GetLfT3tMax(): unsupported chip type");
171 return 0;
172 }
173 return NfccNciAdapter::GetInstance().GetLfT3tMax();
174 }
175
GetLastError()176 int NfccHost::GetLastError()
177 {
178 DebugLog("NfccHost::GetLastError");
179 if (!NfcChipTypeParser::IsSn110()) {
180 WarnLog("NfccHost::GetLastError(): unsupported chip type");
181 return 0;
182 }
183 return NfccNciAdapter::GetInstance().GetLastError();
184 }
185
Abort()186 void NfccHost::Abort()
187 {
188 DebugLog("NfccHost::Abort");
189 if (!NfcChipTypeParser::IsSn110()) {
190 WarnLog("NfccHost::Abort(): unsupported chip type");
191 return;
192 }
193 NfccNciAdapter::GetInstance().Abort();
194 }
195
CheckFirmware()196 bool NfccHost::CheckFirmware()
197 {
198 DebugLog("NfccHost::CheckFirmware");
199 if (!NfcChipTypeParser::IsSn110()) {
200 WarnLog("NfccHost::CheckFirmware(): unsupported chip type");
201 return true;
202 }
203 return NfccNciAdapter::GetInstance().CheckFirmware();
204 }
205
Dump(int fd)206 void NfccHost::Dump(int fd)
207 {
208 DebugLog("NfccHost::Dump");
209 if (!NfcChipTypeParser::IsSn110()) {
210 WarnLog("NfccHost::Dump(): unsupported chip type");
211 return;
212 }
213 NfccNciAdapter::GetInstance().Dump(fd);
214 }
215
FactoryReset()216 void NfccHost::FactoryReset()
217 {
218 DebugLog("NfccHost::FactoryReset");
219 if (!NfcChipTypeParser::IsSn110()) {
220 WarnLog("NfccHost::FactoryReset(): unsupported chip type");
221 return;
222 }
223 NfccNciAdapter::GetInstance().FactoryReset();
224 }
225
Shutdown()226 void NfccHost::Shutdown()
227 {
228 DebugLog("NfccHost::Shutdown");
229 if (!NfcChipTypeParser::IsSn110()) {
230 WarnLog("NfccHost::Shutdown(): unsupported chip type");
231 return;
232 }
233 NfccNciAdapter::GetInstance().Shutdown();
234 }
235
AddAidRouting(std::string & aid,int route,int aidInfo)236 bool NfccHost::AddAidRouting(std::string& aid, int route, int aidInfo)
237 {
238 DebugLog("NfccHost::AddAidRouting");
239 #ifdef _NFC_SERVICE_HCE_
240 return NciBalCe::GetInstance().AddAidRouting(aid, route, aidInfo);
241 #else
242 return true;
243 #endif
244 }
245
RemoveAidRouting(std::string & aid)246 bool NfccHost::RemoveAidRouting(std::string& aid)
247 {
248 DebugLog("NfccHost::RemoveAidRouting");
249 #ifdef _NFC_SERVICE_HCE_
250 return NciBalCe::GetInstance().RemoveAidRouting(aid);
251 #else
252 return true;
253 #endif
254 }
255
CommitRouting()256 bool NfccHost::CommitRouting()
257 {
258 DebugLog("NfccHost::CommitRouting");
259 if (!NfcChipTypeParser::IsSn110()) {
260 WarnLog("NfccHost::CommitRouting(): unsupported chip type");
261 return true;
262 }
263 bool restart = NfccNciAdapter::GetInstance().IsRfEbabled();
264 if (restart) {
265 NfccNciAdapter::GetInstance().StartRfDiscovery(false);
266 }
267 bool commitResult = NfccNciAdapter::GetInstance().CommitRouting();
268 if (restart) {
269 NfccNciAdapter::GetInstance().StartRfDiscovery(true);
270 }
271 return commitResult;
272 }
273
ComputeRoutingParams()274 bool NfccHost::ComputeRoutingParams()
275 {
276 DebugLog("NfccHost::ComputeRoutingParams");
277 if (!NfcChipTypeParser::IsSn110()) {
278 WarnLog("NfccHost::ComputeRoutingParams(): unsupported chip type");
279 return true;
280 }
281 return NfccNciAdapter::GetInstance().ComputeRoutingParams();
282 }
283
GetAidRoutingTableSize()284 int NfccHost::GetAidRoutingTableSize()
285 {
286 DebugLog("NfccHost::GetAidRoutingTableSize");
287 #ifdef _NFC_SERVICE_HCE_
288 return NciBalCe::GetInstance().GetAidRoutingTableSize();
289 #endif
290 return 0;
291 }
292
GetDefaultRoute()293 int NfccHost::GetDefaultRoute()
294 {
295 DebugLog("NfccHost::GetDefaultRoute");
296 #ifdef _NFC_SERVICE_HCE_
297 return NciBalCe::GetInstance().GetDefaultRoute();
298 #endif
299 return 0;
300 }
301
GetDefaultOffHostRoute()302 int NfccHost::GetDefaultOffHostRoute()
303 {
304 DebugLog("NfccHost::GetDefaultOffHostRoute");
305 #ifdef _NFC_SERVICE_HCE_
306 return NciBalCe::GetInstance().GetDefaultOffHostRoute();
307 #endif
308 return 0;
309 }
310
GetOffHostUiccRoute()311 std::vector<int> NfccHost::GetOffHostUiccRoute()
312 {
313 DebugLog("NfccHost::GetOffHostUiccRoute");
314 #ifdef _NFC_SERVICE_HCE_
315 return NciBalCe::GetInstance().GetOffHostUiccRoute();
316 #endif
317 return {};
318 }
319
GetOffHostEseRoute()320 std::vector<int> NfccHost::GetOffHostEseRoute()
321 {
322 DebugLog("NfccHost::GetOffHostEseRoute");
323 #ifdef _NFC_SERVICE_HCE_
324 return NciBalCe::GetInstance().GetOffHostEseRoute();
325 #else
326 return {};
327 #endif
328 }
329
GetAidMatchingMode()330 int NfccHost::GetAidMatchingMode()
331 {
332 DebugLog("NfccHost::GetAidMatchingMode");
333 #ifdef _NFC_SERVICE_HCE_
334 return NciBalCe::GetInstance().GetAidMatchingMode();
335 #else
336 return 0;
337 #endif
338 }
339
GetDefaultIsoDepRouteDestination()340 int NfccHost::GetDefaultIsoDepRouteDestination()
341 {
342 DebugLog("NfccHost::GetDefaultIsoDepRouteDestination");
343 #ifdef _NFC_SERVICE_HCE_
344 return NciBalCe::GetInstance().GetDefaultIsoDepRouteDestination();
345 #else
346 return 0;
347 #endif
348 }
349
CanMakeReadOnly(int ndefType)350 bool NfccHost::CanMakeReadOnly(int ndefType)
351 {
352 return ndefType == KITS::NdefTag::EmNfcForumType::NFC_FORUM_TYPE_1 ||
353 ndefType == KITS::NdefTag::EmNfcForumType::NFC_FORUM_TYPE_2;
354 }
355
GetExtendedLengthApdusSupported()356 bool NfccHost::GetExtendedLengthApdusSupported()
357 {
358 if (!NfcChipTypeParser::IsSn110()) {
359 WarnLog("NfccHost::GetExtendedLengthApdusSupported(): unsupported chip type");
360 return true;
361 }
362 if (NfccNciAdapter::GetInstance().GetIsoDepMaxTransceiveLength() > ISO_DEP_FRAME_MAX_LEN) {
363 return true;
364 }
365 return false;
366 }
367
SetNciAdaptation(std::shared_ptr<INfcNci> nciAdaptation)368 void NfccHost::SetNciAdaptation(std::shared_ptr<INfcNci> nciAdaptation)
369 {
370 if (!NfcChipTypeParser::IsSn110()) {
371 WarnLog("NfccHost::SetNciAdaptation(): unsupported chip type");
372 return;
373 }
374 NfccNciAdapter::GetInstance().SetNciAdaptation(nciAdaptation);
375 #ifdef _NFC_SERVICE_HCE_
376 NciBalCe::GetInstance().SetNciAdaptation(nciAdaptation);
377 HciManager::GetInstance().SetNciAdaptation(nciAdaptation);
378 #endif
379 }
380
RemoteFieldActivated()381 void NfccHost::RemoteFieldActivated()
382 {
383 DebugLog("NfccHost::RemoteFieldActivated");
384 if (nfccHostListener_.expired()) {
385 ErrorLog("Nfcc host listener is null");
386 return;
387 }
388 nfccHostListener_.lock()->FieldActivated();
389 }
390
RemoteFieldDeactivated()391 void NfccHost::RemoteFieldDeactivated()
392 {
393 DebugLog("NfccHost::RemoteFieldDeactivated");
394 if (nfccHostListener_.expired()) {
395 ErrorLog("Nfcc host listener is null");
396 return;
397 }
398 nfccHostListener_.lock()->FieldDeactivated();
399 }
400
HostCardEmulationActivated(int technology)401 void NfccHost::HostCardEmulationActivated(int technology)
402 {
403 DebugLog("NfccHost::HostCardEmulationActivated");
404 if (nfccHostListener_.expired()) {
405 ErrorLog("Nfcc host listener is null");
406 return;
407 }
408 }
409
HostCardEmulationDeactivated(int technology)410 void NfccHost::HostCardEmulationDeactivated(int technology)
411 {
412 DebugLog("NfccHost::HostCardEmulationDeactivated");
413 if (nfccHostListener_.expired()) {
414 ErrorLog("Nfcc host listener is null");
415 return;
416 }
417 }
418
HostCardEmulationDataReceived(int technology,std::string & data)419 void NfccHost::HostCardEmulationDataReceived(int technology, std::string& data)
420 {
421 DebugLog("NfccHost::HostCardEmulationDataReceived");
422 if (nfccHostListener_.expired()) {
423 ErrorLog("Nfcc host listener is null");
424 return;
425 }
426 }
427
TagDiscovered(std::shared_ptr<NCI::ITagHost> tagHost)428 void NfccHost::TagDiscovered(std::shared_ptr<NCI::ITagHost> tagHost)
429 {
430 DebugLog("NfccHost::TagDiscovered");
431 if (nfccHostListener_.expired()) {
432 ErrorLog("Nfcc host listener is null");
433 return;
434 }
435 nfccHostListener_.lock()->OnTagDiscovered(tagHost);
436 }
437
OffHostTransactionEvent(std::string & aid,std::string & data,std::string & seName)438 void NfccHost::OffHostTransactionEvent(std::string& aid, std::string& data, std::string& seName)
439 {
440 DebugLog("NfccHost::OffHostTransactionEvent");
441 if (nfccHostListener_.expired()) {
442 ErrorLog("Nfcc host listener is null");
443 return;
444 }
445 }
446
EeUpdate()447 void NfccHost::EeUpdate()
448 {
449 DebugLog("NfccHost::EeUpdate");
450 if (nfccHostListener_.expired()) {
451 ErrorLog("Nfcc host listener is null");
452 return;
453 }
454 }
455
ClearAidTable()456 bool NfccHost::ClearAidTable()
457 {
458 DebugLog("NfccHost::ClearAidTable");
459 #ifdef _NFC_SERVICE_HCE_
460 return NciBalCe::GetInstance().ClearAidTable();
461 #else
462 return true;
463 #endif
464 }
465
GetRemainRoutingTableSize()466 int NfccHost::GetRemainRoutingTableSize()
467 {
468 DebugLog("NfccHost::GetRemainRoutingTableSize");
469 #ifdef _NFC_SERVICE_HCE_
470 return NciBalCe::GetInstance().GetRemainRoutingTableSize();
471 #endif
472 return 0;
473 }
474 } // namespace NCI
475 } // namespace NFC
476 } // namespace OHOS
477