• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tag_session.h"
16 #include "loghelper.h"
17 #include "app_state_observer.h"
18 #include "external_deps_proxy.h"
19 
20 namespace OHOS {
21 namespace NFC {
22 namespace TAG {
23 using OHOS::AppExecFwk::ElementName;
24 const std::string DUMP_LINE = "---------------------------";
25 const std::string DUMP_END = "\n";
26 
27 // NFC_A = 1 ~ NDEF_FORMATABLE = 10
28 const int MAX_TECH = 12;
29 int g_techTimeout[MAX_TECH] = {0};
30 int g_maxTransLength[MAX_TECH] = {0, 253, 253, 261, 255, 253, 0, 0, 253, 253, 0, 0};
31 std::shared_ptr<AppStateObserver> g_appStateObserver = nullptr;
32 
TagSession(std::shared_ptr<NfcService> service)33 TagSession::TagSession(std::shared_ptr<NfcService> service)
34     : nfcService_(service)
35 {
36     if (service) {
37         nciTagProxy_ = service->GetNciTagProxy();
38         nfcPollingManager_ = service->GetNfcPollingManager();
39     }
40     g_appStateObserver = std::make_shared<AppStateObserver>(this);
41 }
42 
~TagSession()43 TagSession::~TagSession()
44 {
45 }
46 
47 /**
48  * @brief To connect the tagRfDiscId by technology.
49  * @param tagRfDiscId the rf disc id of tag
50  * @param technology the tag technology
51  * @return the result to connect the tag
52  */
Connect(int tagRfDiscId,int technology)53 int TagSession::Connect(int tagRfDiscId, int technology)
54 {
55     if (technology < 0 || technology >= MAX_TECH) {
56         ErrorLog("Connect, invalid technology %{public}d", technology);
57         return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
58     }
59     if (nfcService_.expired() || nciTagProxy_.expired()) {
60         ErrorLog("Connect, expired");
61         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
62     }
63     if (!nfcService_.lock()->IsNfcEnabled()) {
64         ErrorLog("Connect, IsNfcEnabled error");
65         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
66     }
67     if (!nciTagProxy_.lock()->IsTagFieldOn(tagRfDiscId)) {
68         ErrorLog("Connect, IsTagFieldOn error");
69         return NFC::KITS::ErrorCode::ERR_TAG_STATE_LOST;
70     }
71 
72     if (nciTagProxy_.lock()->Connect(tagRfDiscId, technology)) {
73         return NFC::KITS::ErrorCode::ERR_NONE;
74     } else {
75         ErrorLog("Connect, unallowd call error");
76         return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
77     }
78 }
79 
80 /**
81  * @brief To get connection status of tag.
82  * @param tagRfDiscId the rf disc id of tag
83  * @param isConnected the connection status of tag
84  * @return the result to get connection status of the tag
85  */
IsConnected(int tagRfDiscId,bool & isConnected)86 int TagSession::IsConnected(int tagRfDiscId, bool &isConnected)
87 {
88     if (nfcService_.expired() || nciTagProxy_.expired()) {
89         ErrorLog("Connect, expired");
90         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
91     }
92     if (!nfcService_.lock()->IsNfcEnabled()) {
93         ErrorLog("Connect, IsNfcEnabled error");
94         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
95     }
96     isConnected = nciTagProxy_.lock()->IsTagFieldOn(tagRfDiscId);
97     return NFC::KITS::ErrorCode::ERR_NONE;
98 }
99 
100 /**
101  * @brief To reconnect the tagRfDiscId.
102  * @param tagRfDiscId the rf disc id of tag
103  * @return the result to reconnect the tag
104  */
Reconnect(int tagRfDiscId)105 int TagSession::Reconnect(int tagRfDiscId)
106 {
107     // Check if NFC is enabled
108     if (nfcService_.expired() || nciTagProxy_.expired()) {
109         ErrorLog("Reconnect, expired");
110         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
111     }
112     if (!nfcService_.lock()->IsNfcEnabled()) {
113         ErrorLog("Reconnect, IsNfcEnabled error");
114         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
115     }
116 
117     if (nciTagProxy_.lock()->Reconnect(tagRfDiscId)) {
118         return NFC::KITS::ErrorCode::ERR_NONE;
119     } else {
120         ErrorLog("Reconnect, unallowd call error");
121         return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
122     }
123 }
124 /**
125  * @brief To disconnect the tagRfDiscId.
126  * @param tagRfDiscId the rf disc id of tag
127  */
Disconnect(int tagRfDiscId)128 void TagSession::Disconnect(int tagRfDiscId)
129 {
130     // Check if NFC is enabled
131     if (nfcService_.expired() || nciTagProxy_.expired() || !nfcService_.lock()->IsNfcEnabled()) {
132         ErrorLog("Disconnect, IsTagFieldOn error");
133         return;
134     }
135 
136     nciTagProxy_.lock()->Disconnect(tagRfDiscId);
137 }
138 
SetTimeout(int tagRfDiscId,int timeout,int technology)139 int TagSession::SetTimeout(int tagRfDiscId, int timeout, int technology)
140 {
141     if (technology < 0 || technology >= MAX_TECH) {
142         ErrorLog("SetTimeout, invalid technology %{public}d", technology);
143         return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
144     }
145     // Check if NFC is enabled
146     if (nfcService_.expired() || nciTagProxy_.expired()) {
147         ErrorLog("SetTimeout, expired");
148         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
149     }
150     if (!nfcService_.lock()->IsNfcEnabled()) {
151         ErrorLog("SetTimeout, IsNfcEnabled error");
152         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
153     }
154 
155     nciTagProxy_.lock()->SetTimeout(tagRfDiscId, timeout, technology);
156     return NFC::KITS::ErrorCode::ERR_NONE;
157 }
158 
GetTimeout(int tagRfDiscId,int technology,int & timeout)159 int TagSession::GetTimeout(int tagRfDiscId, int technology, int &timeout)
160 {
161     if (technology < 0 || technology >= MAX_TECH) {
162         ErrorLog("GetTimeout, invalid technology %{public}d", technology);
163         return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
164     }
165     // Check if NFC is enabled
166     if (nfcService_.expired() || nciTagProxy_.expired()) {
167         ErrorLog("GetTimeout, expired");
168         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
169     }
170     if (!nfcService_.lock()->IsNfcEnabled()) {
171         ErrorLog("GetTimeout, IsNfcEnabled error");
172         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
173     }
174 
175     uint32_t timeoutTemp = 0;
176     nciTagProxy_.lock()->GetTimeout(tagRfDiscId, timeoutTemp, technology);
177     timeout = static_cast<int>(timeoutTemp);
178     return NFC::KITS::ErrorCode::ERR_NONE;
179 }
180 
ResetTimeout(int tagRfDiscId)181 void TagSession::ResetTimeout(int tagRfDiscId)
182 {
183     if (nfcService_.expired() || nciTagProxy_.expired()) {
184         ErrorLog("ResetTimeout, expired");
185         return;
186     }
187     if (!nfcService_.lock()->IsNfcEnabled()) {
188         ErrorLog("ResetTimeout, IsNfcEnabled error");
189         return;
190     }
191     nciTagProxy_.lock()->ResetTimeout(tagRfDiscId);
192     return;
193 }
194 
195 /**
196  * @brief Get the TechList of the tagRfDiscId.
197  * @param tagRfDiscId the rf disc id of tag
198  * @return TechList
199  */
GetTechList(int tagRfDiscId)200 std::vector<int> TagSession::GetTechList(int tagRfDiscId)
201 {
202     std::vector<int> techList;
203     // Check if NFC is enabled
204     if (nfcService_.expired() || nciTagProxy_.expired() || !nfcService_.lock()->IsNfcEnabled()) {
205         ErrorLog("GetTechList, IsTagFieldOn error");
206         return techList;
207     }
208 
209     return nciTagProxy_.lock()->GetTechList(tagRfDiscId);
210 }
211 /**
212  * @brief Checking the tagRfDiscId is present.
213  * @param tagRfDiscId the rf disc id of tag
214  * @return true - Presnet; the other - No Presnet
215  */
IsTagFieldOn(int tagRfDiscId)216 bool TagSession::IsTagFieldOn(int tagRfDiscId)
217 {
218     // Check if NFC is enabled
219     if (nfcService_.expired() || nciTagProxy_.expired() || !nfcService_.lock()->IsNfcEnabled()) {
220         ErrorLog("IsTagFieldOn, IsTagFieldOn error");
221         return false;
222     }
223 
224     return nciTagProxy_.lock()->IsTagFieldOn(tagRfDiscId);
225 }
226 /**
227  * @brief Checking the tagRfDiscId is a Ndef Tag.
228  * @param tagRfDiscId the rf disc id of tag
229  * @return true - Ndef Tag; the other - No Ndef Tag
230  */
IsNdef(int tagRfDiscId)231 bool TagSession::IsNdef(int tagRfDiscId)
232 {
233     // Check if NFC is enabled
234     if (nfcService_.expired() || nciTagProxy_.expired() || !nfcService_.lock()->IsNfcEnabled()) {
235         ErrorLog("IsNdef, IsTagFieldOn error");
236         return false;
237     }
238 
239     std::vector<int> ndefInfo;
240     return nciTagProxy_.lock()->DetectNdefInfo(tagRfDiscId, ndefInfo);
241 }
242 
SendRawFrame(const int tagRfDiscId,std::string hexCmdData,bool raw,std::string & hexRespData)243 int TagSession::SendRawFrame(const int tagRfDiscId, std::string hexCmdData, bool raw, std::string &hexRespData)
244 {
245     DebugLog("Send Raw(%{public}d) Frame", raw);
246     // Check if NFC is enabled
247     if (nfcService_.expired() || nciTagProxy_.expired()) {
248         ErrorLog("SendRawFrame, expired");
249         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
250     }
251     if (!nfcService_.lock()->IsNfcEnabled()) {
252         ErrorLog("SendRawFrame, IsNfcEnabled error");
253         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
254     }
255 
256     // Check if length is within limits
257     int maxSize = 0;
258     GetMaxTransceiveLength(nciTagProxy_.lock()->GetConnectedTech(tagRfDiscId), maxSize);
259     if (KITS::NfcSdkCommon::GetHexStrBytesLen(hexCmdData) > static_cast<uint32_t>(maxSize)) {
260         return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
261     }
262 
263     int result = nciTagProxy_.lock()->Transceive(tagRfDiscId, hexCmdData, hexRespData);
264     DebugLog("TagSession::SendRawFrame, result = 0x%{public}X", result);
265     if ((result == 0) && (!hexRespData.empty())) {
266         return NFC::KITS::ErrorCode::ERR_NONE;
267     } else if (result == 1) {  // result == 1 means that Tag lost
268         ErrorLog("TagSession::SendRawFrame: tag lost.");
269         return NFC::KITS::ErrorCode::ERR_TAG_STATE_LOST;
270     }
271     ErrorLog("TagSession::SendRawFrame: result failed.");
272     return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
273 }
274 /**
275  * @brief Reading from the host tag
276  * @param tagRfDiscId the rf disc id of tag
277  * @return the read data
278  */
NdefRead(int tagRfDiscId,std::string & ndefMessage)279 int TagSession::NdefRead(int tagRfDiscId, std::string &ndefMessage)
280 {
281     // Check if NFC is enabled
282     if (nfcService_.expired() || nciTagProxy_.expired() || !nfcService_.lock()->IsNfcEnabled()) {
283         ErrorLog("NdefRead, IsTagFieldOn error");
284         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
285     }
286 
287     ndefMessage = nciTagProxy_.lock()->ReadNdef(tagRfDiscId);
288     return NFC::KITS::ErrorCode::ERR_NONE;
289 }
290 /**
291  * @brief Writing the data into the host tag.
292  * @param tagRfDiscId the rf disc id of tag
293  * @param msg the wrote data
294  * @return the Writing Result
295  */
NdefWrite(int tagRfDiscId,std::string msg)296 int TagSession::NdefWrite(int tagRfDiscId, std::string msg)
297 {
298     // Check if NFC is enabled
299     if (nfcService_.expired() || nciTagProxy_.expired()) {
300         ErrorLog("NdefWrite, expired");
301         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
302     }
303     if (!nfcService_.lock()->IsNfcEnabled()) {
304         ErrorLog("NdefWrite, IsNfcEnabled error");
305         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
306     }
307 
308     if (msg.empty()) {
309         ErrorLog("NdefWrite, msg.empty error");
310         return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
311     }
312 
313     if (nciTagProxy_.lock()->WriteNdef(tagRfDiscId, msg)) {
314         return NFC::KITS::ErrorCode::ERR_NONE;
315     }
316     return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
317 }
318 /**
319  * @brief Making the host tag to read only.
320  * @param tagRfDiscId the rf disc id of tag
321  * @return the making result
322  */
NdefMakeReadOnly(int tagRfDiscId)323 int TagSession::NdefMakeReadOnly(int tagRfDiscId)
324 {
325     // Check if NFC is enabled
326     if (nfcService_.expired() || nciTagProxy_.expired()) {
327         ErrorLog("NdefMakeReadOnly, expired");
328         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
329     }
330     if (!nfcService_.lock()->IsNfcEnabled()) {
331         ErrorLog("NdefMakeReadOnly, IsNfcEnabled error");
332         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
333     }
334 
335     if (nciTagProxy_.lock()->SetNdefReadOnly(tagRfDiscId)) {
336         return NFC::KITS::ErrorCode::ERR_NONE;
337     }
338     return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
339 }
340 /**
341  * @brief format the tag by Ndef
342  * @param tagRfDiscId the rf disc id of tag
343  * @param key the format key
344  * @return the format result
345  */
FormatNdef(int tagRfDiscId,const std::string & key)346 int TagSession::FormatNdef(int tagRfDiscId, const std::string& key)
347 {
348     // Check if NFC is enabled
349     if (nfcService_.expired() || nciTagProxy_.expired()) {
350         ErrorLog("FormatNdef, expired");
351         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
352     }
353     if (!nfcService_.lock()->IsNfcEnabled()) {
354         ErrorLog("FormatNdef, IsNfcEnabled error");
355         return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
356     }
357 
358     if (nciTagProxy_.lock()->FormatNdef(tagRfDiscId, key)) {
359         return NFC::KITS::ErrorCode::ERR_NONE;
360     }
361     return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
362 }
363 
CanMakeReadOnly(int ndefType,bool & canSetReadOnly)364 int TagSession::CanMakeReadOnly(int ndefType, bool &canSetReadOnly)
365 {
366     if (nfcService_.expired() || nciTagProxy_.expired()) {
367         ErrorLog("CanMakeReadOnly, expired");
368         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
369     }
370     canSetReadOnly = nciTagProxy_.lock()->CanMakeReadOnly(ndefType);
371     return NFC::KITS::ErrorCode::ERR_NONE;
372 }
373 /**
374  * @brief Get Max Transceive Length
375  * @param technology the tag technology
376  * @return Max Transceive Length
377  */
GetMaxTransceiveLength(int technology,int & maxSize)378 int TagSession::GetMaxTransceiveLength(int technology, int &maxSize)
379 {
380     if (technology < 0 || technology >= MAX_TECH) {
381         ErrorLog("GetMaxTransceiveLength, technology not support");
382         return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
383     }
384     maxSize = g_maxTransLength[technology];
385     return NFC::KITS::ErrorCode::ERR_NONE;
386 }
387 
IsSupportedApdusExtended(bool & isSupported)388 int TagSession::IsSupportedApdusExtended(bool &isSupported)
389 {
390     if (nfcService_.expired() || nciTagProxy_.expired()) {
391         ErrorLog("IsSupportedApdusExtended, expired");
392         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
393     }
394     isSupported = nciTagProxy_.lock()->IsExtendedLengthApduSupported();
395     return NFC::KITS::ErrorCode::ERR_NONE;
396 }
397 
GetFgDataVecSize()398 uint16_t TagSession::GetFgDataVecSize()
399 {
400     std::unique_lock<std::shared_mutex> guard(fgMutex_);
401     return fgDataVec_.size();
402 }
403 
GetReaderDataVecSize()404 uint16_t TagSession::GetReaderDataVecSize()
405 {
406     std::unique_lock<std::shared_mutex> guard(fgMutex_);
407     return readerDataVec_.size();
408 }
409 
CheckFgAppStateChanged(const std::string & bundleName,const std::string & abilityName,int abilityState)410 void TagSession::CheckFgAppStateChanged(const std::string &bundleName, const std::string &abilityName,
411     int abilityState)
412 {
413     std::unique_lock<std::shared_mutex> guard(fgMutex_);
414     for (auto fgData = fgDataVec_.begin(); fgData != fgDataVec_.end(); fgData++) {
415         ElementName element = fgData->element_;
416         if (element.GetBundleName() == bundleName && element.GetAbilityName() == abilityName) {
417             // app changes to foreground, RegForegroundDispatch.
418             if (abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND) &&
419                 !fgData->isEnableForeground_) {
420                 InfoLog("app changes to foreground, RegForegroundDispatchInner");
421                 RegForegroundDispatchInner(element, fgData->techs_, fgData->cb_);
422                 return;
423             }
424             // app changes to background, UnregForegroundDispatchInner.
425             if (abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND) &&
426                 fgData->isEnableForeground_) {
427                 InfoLog("app changes to background, UnregForegroundDispatchInner");
428                 UnregForegroundDispatchInner(element, false);
429                 return;
430             }
431             // app death, UnregForegroundDispatchInner and erase from fgDtataVec_.
432             if (abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED)) {
433                 InfoLog("app died, unregForegroundDispatchInner and erase fgData");
434                 UnregForegroundDispatchInner(element, false);
435                 fgDataVec_.erase(fgData);
436                 return;
437             }
438         }
439     }
440 }
441 
CheckReaderAppStateChanged(const std::string & bundleName,const std::string & abilityName,int abilityState)442 void TagSession::CheckReaderAppStateChanged(const std::string &bundleName, const std::string &abilityName,
443     int abilityState)
444 {
445     std::unique_lock<std::shared_mutex> guard(fgMutex_);
446     for (auto readerData = readerDataVec_.begin(); readerData != readerDataVec_.end(); readerData++) {
447         ElementName element = readerData->element_;
448         if (element.GetBundleName() == bundleName && element.GetAbilityName() == abilityName) {
449             // app changes to foreground, RegReaderModeInner.
450             if (abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND) &&
451                 !readerData->isEnabled_) {
452                 InfoLog("app changes to foreground, RegReaderModeInner");
453                 RegReaderModeInner(element, readerData->techs_, readerData->cb_);
454                 return;
455             }
456             // app changes to background, UnregReaderModeInner.
457             if (abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND) &&
458                 readerData->isEnabled_) {
459                 InfoLog("app changes to background, UnregReaderModeInner");
460                 UnregReaderModeInner(element, false);
461                 return;
462             }
463             // app death, UnregReaderModeInner and erase from readerDataVec_.
464             if (abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED)) {
465                 InfoLog("app died, UnregReaderModeInner and erase readerData");
466                 UnregReaderModeInner(element, false);
467                 readerDataVec_.erase(readerData);
468                 return;
469             }
470         }
471     }
472 }
473 
HandleAppStateChanged(const std::string & bundleName,const std::string & abilityName,int abilityState)474 void TagSession::HandleAppStateChanged(const std::string &bundleName, const std::string &abilityName,
475     int abilityState)
476 {
477     if (GetFgDataVecSize() == 0 && GetReaderDataVecSize() == 0) {
478         return;
479     }
480     InfoLog("HandleAppStateChanged: bundleName = %{public}s, abilityName = %{public}s, abilityState = %{public}d",
481         bundleName.c_str(), abilityName.c_str(), abilityState);
482     CheckFgAppStateChanged(bundleName, abilityName, abilityState);
483     CheckReaderAppStateChanged(bundleName, abilityName, abilityState);
484 }
485 
IsSameAppAbility(const ElementName & element,const ElementName & fgElement)486 bool TagSession::IsSameAppAbility(const ElementName &element, const ElementName &fgElement)
487 {
488     if (element.GetBundleName() == fgElement.GetBundleName() &&
489         element.GetAbilityName() == fgElement.GetAbilityName()) {
490         return true;
491     }
492     return false;
493 }
494 
495 #ifdef VENDOR_APPLICATIONS_ENABLED
IsVendorProcess()496 bool TagSession::IsVendorProcess()
497 {
498     auto tag = nciTagProxy_.lock();
499     if (tag) {
500         return tag->IsVendorProcess();
501     }
502     ErrorLog("IsVendorProcess: tag proxy null");
503     return false;
504 }
505 #endif
506 
RegForegroundDispatch(ElementName & element,std::vector<uint32_t> & discTech,const sptr<KITS::IForegroundCallback> & callback)507 int TagSession::RegForegroundDispatch(ElementName &element, std::vector<uint32_t> &discTech,
508     const sptr<KITS::IForegroundCallback> &callback)
509 {
510     if (!g_appStateObserver->IsForegroundApp(element.GetBundleName())) {
511 #ifdef VENDOR_APPLICATIONS_ENABLED
512         if (!IsVendorProcess()) {
513             ErrorLog("not foreground app.");
514             return KITS::ERR_NONE;
515         }
516 #else
517         ErrorLog("not foreground app.");
518         return KITS::ERR_NONE;
519 #endif
520     }
521     std::unique_lock<std::shared_mutex> guard(fgMutex_);
522     return RegForegroundDispatchInner(element, discTech, callback);
523 }
524 
RegForegroundDispatchInner(ElementName & element,const std::vector<uint32_t> & discTech,const sptr<KITS::IForegroundCallback> & callback)525 int TagSession::RegForegroundDispatchInner(ElementName &element, const std::vector<uint32_t> &discTech,
526     const sptr<KITS::IForegroundCallback> &callback)
527 {
528     if (IsFgRegistered(element, discTech, callback)) {
529         WarnLog("%{public}s already RegForegroundDispatch", element.GetBundleName().c_str());
530         return KITS::ERR_NONE;
531     }
532     InfoLog("RegForegroundDispatch: bundleName = %{public}s, abilityName = %{public}s",
533         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
534     if (nfcPollingManager_.expired()) {
535         ErrorLog("RegForegroundDispatch, expired");
536         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
537     }
538     if (nfcPollingManager_.lock()->EnableForegroundDispatch(element, discTech, callback)) {
539         NfcFailedParams nfcFailedParams;
540         ExternalDepsProxy::GetInstance().BuildFailedParams(nfcFailedParams,
541             MainErrorCode::APP_BEHAVIOR, SubErrorCode::REG_FOREGROUND_DISPATCH);
542         nfcFailedParams.appPackageName = element.GetBundleName();
543         ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(&nfcFailedParams);
544         return KITS::ERR_NONE;
545     }
546     return KITS::ERR_NFC_PARAMETERS;
547 }
548 
IsFgRegistered(const ElementName & element,const std::vector<uint32_t> & discTech,const sptr<KITS::IForegroundCallback> & callback)549 bool TagSession::IsFgRegistered(const ElementName &element, const std::vector<uint32_t> &discTech,
550     const sptr<KITS::IForegroundCallback> &callback)
551 {
552     for (FgData &fgData : fgDataVec_) {
553         ElementName fgElement = fgData.element_;
554         if (IsSameAppAbility(element, fgElement)) {
555             if (fgData.isEnableForeground_) {
556                 return true;
557             }
558             InfoLog("Enable FgData: bundleName = %{public}s, abilityName = %{public}s",
559                 fgElement.GetBundleName().c_str(), fgElement.GetAbilityName().c_str());
560             fgData.isEnableForeground_ = true;
561             return false;
562         }
563     }
564     FgData fgData(true, element, discTech, callback);
565     fgDataVec_.push_back(fgData);
566     InfoLog("Add new FgData to vector: %{public}s, %{public}s", element.GetBundleName().c_str(),
567         element.GetAbilityName().c_str());
568     return false;
569 }
570 
UnregForegroundDispatch(ElementName & element)571 int TagSession::UnregForegroundDispatch(ElementName &element)
572 {
573     std::unique_lock<std::shared_mutex> guard(fgMutex_);
574     return UnregForegroundDispatchInner(element, true);
575 }
576 
UnregForegroundDispatchInner(const ElementName & element,bool isAppUnregister)577 int TagSession::UnregForegroundDispatchInner(const ElementName &element, bool isAppUnregister)
578 {
579     if (IsFgUnregistered(element, isAppUnregister)) {
580         WarnLog("%{public}s already UnregForegroundDispatch", element.GetBundleName().c_str());
581         return KITS::ERR_NONE;
582     }
583     InfoLog("UnregForegroundDispatchInner: bundleName = %{public}s, abilityName = %{public}s",
584         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
585     if (nfcPollingManager_.expired()) {
586         ErrorLog("UnregForegroundDispatch, expired");
587         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
588     }
589     if (nfcPollingManager_.lock()->DisableForegroundDispatch(element)) {
590         return KITS::ERR_NONE;
591     }
592     return KITS::ERR_NFC_PARAMETERS;
593 }
594 
IsFgUnregistered(const ElementName & element,bool isAppUnregister)595 bool TagSession::IsFgUnregistered(const ElementName &element, bool isAppUnregister)
596 {
597     if (fgDataVec_.size() == 0) {
598         return true;
599     }
600     bool isUnregistered = false;
601     for (auto fgData = fgDataVec_.begin(); fgData != fgDataVec_.end(); fgData++) {
602         if (IsSameAppAbility(element, fgData->element_)) {
603             // isEnableForeground_ is false => is already unregistered.
604             if (!fgData->isEnableForeground_) {
605                 isUnregistered = true;
606             }
607             fgData->isEnableForeground_ = false;
608             // app unregister, delete record
609             // background unregister, retain record, re-register when switching to foreground
610             if (isAppUnregister) {
611                 InfoLog("isAppUnregister: erase fgData");
612                 fgDataVec_.erase(fgData);
613             }
614             return isUnregistered;
615         }
616     }
617     // No record, indicating has not registered(or IsFgUnregistered).
618     return true;
619 }
620 
IsReaderRegistered(const ElementName & element,const std::vector<uint32_t> & discTech,const sptr<KITS::IReaderModeCallback> & callback)621 bool TagSession::IsReaderRegistered(const ElementName &element, const std::vector<uint32_t> &discTech,
622     const sptr<KITS::IReaderModeCallback> &callback)
623 {
624     for (ReaderData &readerData : readerDataVec_) {
625         ElementName readerElement = readerData.element_;
626         if (IsSameAppAbility(element, readerElement)) {
627             if (readerData.isEnabled_) {
628                 return true;
629             }
630             InfoLog("Enable ReaderData: bundleName = %{public}s, abilityName = %{public}s",
631                 readerElement.GetBundleName().c_str(), readerElement.GetAbilityName().c_str());
632             readerData.isEnabled_ = true;
633             return false;
634         }
635     }
636     ReaderData readerData(true, element, discTech, callback);
637     readerDataVec_.push_back(readerData);
638     InfoLog("Add new ReaderData to vector: %{public}s, %{public}s", element.GetBundleName().c_str(),
639         element.GetAbilityName().c_str());
640     return false;
641 }
642 
IsReaderUnregistered(const ElementName & element,bool isAppUnregistered)643 bool TagSession::IsReaderUnregistered(const ElementName &element, bool isAppUnregistered)
644 {
645     if (readerDataVec_.size() == 0) {
646         return true;
647     }
648     bool isUnregistered = false;
649     for (auto readerData = readerDataVec_.begin(); readerData != readerDataVec_.end(); readerData++) {
650         if (IsSameAppAbility(element, readerData->element_)) {
651             // isEnabled_ is false => is already unregistered.
652             if (!readerData->isEnabled_) {
653                 isUnregistered = true;
654             }
655             readerData->isEnabled_ = false;
656             // app unregister, delete record
657             // background unregister, retain record, re-register when switching to foreground
658             if (isAppUnregistered) {
659                 InfoLog("isAppUnregister: erase readerData");
660                 readerDataVec_.erase(readerData);
661             }
662             return isUnregistered;
663         }
664     }
665     // No record, indicating has not registered(or IsReaderUnregistered).
666     return true;
667 }
668 
RegReaderModeInner(ElementName & element,std::vector<uint32_t> & discTech,const sptr<KITS::IReaderModeCallback> & callback)669 int TagSession::RegReaderModeInner(ElementName &element, std::vector<uint32_t> &discTech,
670     const sptr<KITS::IReaderModeCallback> &callback)
671 {
672     if (IsReaderRegistered(element, discTech, callback)) {
673         WarnLog("%{public}s already RegReaderMode", element.GetBundleName().c_str());
674         return KITS::ERR_NONE;
675     }
676     InfoLog("RegReaderModeInner: bundleName = %{public}s, abilityName = %{public}s",
677         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
678     if (nfcPollingManager_.expired()) {
679         ErrorLog("RegReaderModeInner, expired");
680         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
681     }
682     if (nfcPollingManager_.lock()->EnableReaderMode(element, discTech, callback)) {
683         NfcFailedParams nfcFailedParams;
684         ExternalDepsProxy::GetInstance().BuildFailedParams(nfcFailedParams,
685             MainErrorCode::APP_BEHAVIOR, SubErrorCode::REG_READERMODE);
686         nfcFailedParams.appPackageName = element.GetBundleName();
687         ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(&nfcFailedParams);
688         return KITS::ERR_NONE;
689     }
690     return KITS::ERR_NFC_PARAMETERS;
691 }
692 
UnregReaderModeInner(ElementName & element,bool isAppUnregister)693 int TagSession::UnregReaderModeInner(ElementName &element, bool isAppUnregister)
694 {
695     if (IsReaderUnregistered(element, isAppUnregister)) {
696         WarnLog("%{public}s already UnregReaderMode", element.GetBundleName().c_str());
697         return KITS::ERR_NONE;
698     }
699     InfoLog("UnregReaderModeInner: bundleName = %{public}s, abilityName = %{public}s",
700         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
701     if (nfcPollingManager_.expired()) {
702         ErrorLog("UnregReaderMode, expired");
703         return NFC::KITS::ErrorCode::ERR_NFC_STATE_UNBIND;
704     }
705     if (nfcPollingManager_.lock()->DisableReaderMode(element)) {
706         return KITS::ERR_NONE;
707     }
708     return KITS::ERR_NFC_PARAMETERS;
709 }
710 
RegReaderMode(ElementName & element,std::vector<uint32_t> & discTech,const sptr<KITS::IReaderModeCallback> & callback)711 int TagSession::RegReaderMode(ElementName &element, std::vector<uint32_t> &discTech,
712     const sptr<KITS::IReaderModeCallback> &callback)
713 {
714     if (!g_appStateObserver->IsForegroundApp(element.GetBundleName())) {
715 #ifdef VENDOR_APPLICATIONS_ENABLED
716         if (!IsVendorProcess()) {
717             return KITS::ERR_TAG_APP_NOT_FOREGROUND;
718         }
719 #else
720         return KITS::ERR_TAG_APP_NOT_FOREGROUND;
721 #endif
722     }
723     std::unique_lock<std::shared_mutex> guard(fgMutex_);
724     return RegReaderModeInner(element, discTech, callback);
725 }
726 
UnregReaderMode(ElementName & element)727 int TagSession::UnregReaderMode(ElementName &element)
728 {
729     std::unique_lock<std::shared_mutex> guard(fgMutex_);
730     return UnregReaderModeInner(element, true);
731 }
732 
Dump(int32_t fd,const std::vector<std::u16string> & args)733 int32_t TagSession::Dump(int32_t fd, const std::vector<std::u16string>& args)
734 {
735     std::string info = GetDumpInfo();
736     int ret = dprintf(fd, "%s\n", info.c_str());
737     if (ret < 0) {
738         ErrorLog("TagSession Dump ret = %{public}d", ret);
739         return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
740     }
741     return NFC::KITS::ErrorCode::ERR_NONE;
742 }
743 
GetDumpInfo()744 std::string TagSession::GetDumpInfo()
745 {
746     std::string info;
747     if (nfcService_.expired()) {
748         return info;
749     }
750 
751     return info.append(DUMP_LINE)
752         .append(" TAG DUMP ")
753         .append(DUMP_LINE)
754         .append(DUMP_END)
755         .append("NFC_STATE          : ")
756         .append(std::to_string(nfcService_.lock()->GetNfcState()))
757         .append(DUMP_END)
758         .append("SCREEN_STATE       : ")
759         .append(std::to_string(nfcService_.lock()->GetScreenState()))
760         .append(DUMP_END)
761         .append("NCI_VERSION        : ")
762         .append(std::to_string(nfcService_.lock()->GetNciVersion()))
763         .append(DUMP_END);
764 }
765 }  // namespace TAG
766 }  // namespace NFC
767 }  // namespace OHOS
768