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 return NfccNciAdapter::GetInstance().SendRawFrame(rawData);
87 }
88
SetScreenStatus(unsigned char screenStateMask)89 bool NfccHost::SetScreenStatus(unsigned char screenStateMask)
90 {
91 DebugLog("NfccHost::SetScreenStatus");
92 if (!NfcChipTypeParser::IsSn110()) {
93 WarnLog("NfccHost::SetScreenStatus(): unsupported chip type");
94 return true;
95 }
96 NfccNciAdapter::GetInstance().SetScreenStatus(screenStateMask);
97 return true;
98 }
99
GetNciVersion()100 int NfccHost::GetNciVersion()
101 {
102 DebugLog("NfccHost::GetNciVersion");
103 if (!NfcChipTypeParser::IsSn110()) {
104 WarnLog("NfccHost::GetNciVersion(): unsupported chip type");
105 return 0;
106 }
107 return NfccNciAdapter::GetInstance().GetNciVersion();
108 }
109
SetSecureNfc(bool secure)110 bool NfccHost::SetSecureNfc(bool secure)
111 {
112 DebugLog("NfccHost::SetSecureNfc");
113 #ifdef _NFC_SERVICE_HCE_
114 NciBalCe::GetInstance().SetSecureNfc(secure);
115 #endif
116 return true;
117 }
118
GetIsoDepMaxTransceiveLength()119 int NfccHost::GetIsoDepMaxTransceiveLength()
120 {
121 DebugLog("NfccHost::GetIsoDepMaxTransceiveLength");
122 return NfccNciAdapter::GetInstance().GetIsoDepMaxTransceiveLength();
123 }
124
RegisterT3tIdentifier(std::string & t3tIdentifier)125 int NfccHost::RegisterT3tIdentifier(std::string& t3tIdentifier)
126 {
127 DebugLog("NfccHost::RegisterT3tIdentifier");
128 return NfccNciAdapter::GetInstance().RegisterT3tIdentifier(t3tIdentifier);
129 }
130
DeregisterT3tIdentifier(std::string & t3tIdentifier)131 void NfccHost::DeregisterT3tIdentifier(std::string& t3tIdentifier)
132 {
133 DebugLog("NfccHost::DeregisterT3tIdentifier");
134 // get handle from mT3tIdentifiers
135 if (!t3tIdentifier.empty()) {
136 int handle = -1;
137 NfccNciAdapter::GetInstance().DeregisterT3tIdentifier(handle);
138 }
139 }
140
ClearT3tIdentifiersCache()141 void NfccHost::ClearT3tIdentifiersCache()
142 {
143 DebugLog("NfccHost::ClearT3tIdentifiersCache");
144 NfccNciAdapter::GetInstance().ClearT3tIdentifiersCache();
145 }
146
GetLfT3tMax()147 int NfccHost::GetLfT3tMax()
148 {
149 DebugLog("NfccHost::GetLfT3tMax");
150 return NfccNciAdapter::GetInstance().GetLfT3tMax();
151 }
152
GetLastError()153 int NfccHost::GetLastError()
154 {
155 DebugLog("NfccHost::GetLastError");
156 return NfccNciAdapter::GetInstance().GetLastError();
157 }
158
Abort()159 void NfccHost::Abort()
160 {
161 DebugLog("NfccHost::Abort");
162 NfccNciAdapter::GetInstance().Abort();
163 }
164
CheckFirmware()165 bool NfccHost::CheckFirmware()
166 {
167 DebugLog("NfccHost::CheckFirmware");
168 return NfccNciAdapter::GetInstance().CheckFirmware();
169 }
170
Dump(int fd)171 void NfccHost::Dump(int fd)
172 {
173 DebugLog("NfccHost::Dump");
174 NfccNciAdapter::GetInstance().Dump(fd);
175 }
176
FactoryReset()177 void NfccHost::FactoryReset()
178 {
179 DebugLog("NfccHost::FactoryReset");
180 NfccNciAdapter::GetInstance().FactoryReset();
181 }
182
Shutdown()183 void NfccHost::Shutdown()
184 {
185 DebugLog("NfccHost::Shutdown");
186 NfccNciAdapter::GetInstance().Shutdown();
187 }
188
AddAidRouting(std::string & aid,int route,int aidInfo)189 bool NfccHost::AddAidRouting(std::string& aid, int route, int aidInfo)
190 {
191 DebugLog("NfccHost::AddAidRouting");
192 #ifdef _NFC_SERVICE_HCE_
193 return NciBalCe::GetInstance().AddAidRouting(aid, route, aidInfo);
194 #else
195 return true;
196 #endif
197 }
198
RemoveAidRouting(std::string & aid)199 bool NfccHost::RemoveAidRouting(std::string& aid)
200 {
201 DebugLog("NfccHost::RemoveAidRouting");
202 #ifdef _NFC_SERVICE_HCE_
203 return NciBalCe::GetInstance().RemoveAidRouting(aid);
204 #else
205 return true;
206 #endif
207 }
208
CommitRouting()209 bool NfccHost::CommitRouting()
210 {
211 DebugLog("NfccHost::CommitRouting");
212 #ifdef _NFC_SERVICE_HCE_
213 bool restart = NfccNciAdapter::GetInstance().IsRfEbabled();
214 if (restart) {
215 NfccNciAdapter::GetInstance().StartRfDiscovery(false);
216 }
217 bool commitResult = NciBalCe::GetInstance().CommitRouting();
218 if (restart) {
219 NfccNciAdapter::GetInstance().StartRfDiscovery(true);
220 }
221 return commitResult;
222 #else
223 return true;
224 #endif
225 }
226
GetAidRoutingTableSize()227 int NfccHost::GetAidRoutingTableSize()
228 {
229 DebugLog("NfccHost::GetAidRoutingTableSize");
230 #ifdef _NFC_SERVICE_HCE_
231 return NciBalCe::GetInstance().GetAidRoutingTableSize();
232 #endif
233 return 0;
234 }
235
GetDefaultRoute()236 int NfccHost::GetDefaultRoute()
237 {
238 DebugLog("NfccHost::GetDefaultRoute");
239 #ifdef _NFC_SERVICE_HCE_
240 return NciBalCe::GetInstance().GetDefaultRoute();
241 #endif
242 return 0;
243 }
244
GetDefaultOffHostRoute()245 int NfccHost::GetDefaultOffHostRoute()
246 {
247 DebugLog("NfccHost::GetDefaultOffHostRoute");
248 #ifdef _NFC_SERVICE_HCE_
249 return NciBalCe::GetInstance().GetDefaultOffHostRoute();
250 #endif
251 return 0;
252 }
253
GetOffHostUiccRoute()254 std::vector<int> NfccHost::GetOffHostUiccRoute()
255 {
256 DebugLog("NfccHost::GetOffHostUiccRoute");
257 #ifdef _NFC_SERVICE_HCE_
258 return NciBalCe::GetInstance().GetOffHostUiccRoute();
259 #endif
260 return {};
261 }
262
GetOffHostEseRoute()263 std::vector<int> NfccHost::GetOffHostEseRoute()
264 {
265 DebugLog("NfccHost::GetOffHostEseRoute");
266 #ifdef _NFC_SERVICE_HCE_
267 return NciBalCe::GetInstance().GetOffHostEseRoute();
268 #else
269 return {};
270 #endif
271 }
272
GetAidMatchingMode()273 int NfccHost::GetAidMatchingMode()
274 {
275 DebugLog("NfccHost::GetAidMatchingMode");
276 #ifdef _NFC_SERVICE_HCE_
277 return NciBalCe::GetInstance().GetAidMatchingMode();
278 #else
279 return 0;
280 #endif
281 }
282
GetDefaultIsoDepRouteDestination()283 int NfccHost::GetDefaultIsoDepRouteDestination()
284 {
285 DebugLog("NfccHost::GetDefaultIsoDepRouteDestination");
286 #ifdef _NFC_SERVICE_HCE_
287 return NciBalCe::GetInstance().GetDefaultIsoDepRouteDestination();
288 #else
289 return 0;
290 #endif
291 }
292
CanMakeReadOnly(int ndefType)293 bool NfccHost::CanMakeReadOnly(int ndefType)
294 {
295 return ndefType == KITS::NdefTag::EmNfcForumType::NFC_FORUM_TYPE_1 ||
296 ndefType == KITS::NdefTag::EmNfcForumType::NFC_FORUM_TYPE_2;
297 }
298
GetExtendedLengthApdusSupported()299 bool NfccHost::GetExtendedLengthApdusSupported()
300 {
301 if (NfccNciAdapter::GetInstance().GetIsoDepMaxTransceiveLength() > ISO_DEP_FRAME_MAX_LEN) {
302 return true;
303 }
304 return false;
305 }
306
SetNciAdaptation(std::shared_ptr<INfcNci> nciAdaptation)307 void NfccHost::SetNciAdaptation(std::shared_ptr<INfcNci> nciAdaptation)
308 {
309 NfccNciAdapter::GetInstance().SetNciAdaptation(nciAdaptation);
310 #ifdef _NFC_SERVICE_HCE_
311 NciBalCe::GetInstance().SetNciAdaptation(nciAdaptation);
312 HciManager::GetInstance().SetNciAdaptation(nciAdaptation);
313 #endif
314 }
315
RemoteFieldActivated()316 void NfccHost::RemoteFieldActivated()
317 {
318 DebugLog("NfccHost::RemoteFieldActivated");
319 if (nfccHostListener_.expired()) {
320 ErrorLog("Nfcc host listener is null");
321 return;
322 }
323 }
324
RemoteFieldDeactivated()325 void NfccHost::RemoteFieldDeactivated()
326 {
327 DebugLog("NfccHost::RemoteFieldDeactivated");
328 if (nfccHostListener_.expired()) {
329 ErrorLog("Nfcc host listener is null");
330 return;
331 }
332 }
333
HostCardEmulationActivated(int technology)334 void NfccHost::HostCardEmulationActivated(int technology)
335 {
336 DebugLog("NfccHost::HostCardEmulationActivated");
337 if (nfccHostListener_.expired()) {
338 ErrorLog("Nfcc host listener is null");
339 return;
340 }
341 }
342
HostCardEmulationDeactivated(int technology)343 void NfccHost::HostCardEmulationDeactivated(int technology)
344 {
345 DebugLog("NfccHost::HostCardEmulationDeactivated");
346 if (nfccHostListener_.expired()) {
347 ErrorLog("Nfcc host listener is null");
348 return;
349 }
350 }
351
HostCardEmulationDataReceived(int technology,std::string & data)352 void NfccHost::HostCardEmulationDataReceived(int technology, std::string& data)
353 {
354 DebugLog("NfccHost::HostCardEmulationDataReceived");
355 if (nfccHostListener_.expired()) {
356 ErrorLog("Nfcc host listener is null");
357 return;
358 }
359 }
360
TagDiscovered(std::shared_ptr<NCI::ITagHost> tagHost)361 void NfccHost::TagDiscovered(std::shared_ptr<NCI::ITagHost> tagHost)
362 {
363 DebugLog("NfccHost::TagDiscovered");
364 if (nfccHostListener_.expired()) {
365 ErrorLog("Nfcc host listener is null");
366 return;
367 }
368 nfccHostListener_.lock()->OnTagDiscovered(tagHost);
369 }
370
OffHostTransactionEvent(std::string & aid,std::string & data,std::string & seName)371 void NfccHost::OffHostTransactionEvent(std::string& aid, std::string& data, std::string& seName)
372 {
373 DebugLog("NfccHost::OffHostTransactionEvent");
374 if (nfccHostListener_.expired()) {
375 ErrorLog("Nfcc host listener is null");
376 return;
377 }
378 }
379
EeUpdate()380 void NfccHost::EeUpdate()
381 {
382 DebugLog("NfccHost::EeUpdate");
383 if (nfccHostListener_.expired()) {
384 ErrorLog("Nfcc host listener is null");
385 return;
386 }
387 }
388
ClearAidTable()389 bool NfccHost::ClearAidTable()
390 {
391 DebugLog("NfccHost::ClearAidTable");
392 #ifdef _NFC_SERVICE_HCE_
393 return NciBalCe::GetInstance().ClearAidTable();
394 #else
395 return true;
396 #endif
397 }
398
GetRemainRoutingTableSize()399 int NfccHost::GetRemainRoutingTableSize()
400 {
401 DebugLog("NfccHost::GetRemainRoutingTableSize");
402 #ifdef _NFC_SERVICE_HCE_
403 return NciBalCe::GetInstance().GetRemainRoutingTableSize();
404 #endif
405 return 0;
406 }
407 } // namespace NCI
408 } // namespace NFC
409 } // namespace OHOS
410