• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tag_native_impl.h"
16 #include "nfa_api.h"
17 #include "nfc_config.h"
18 #include "nfc_sdk_common.h"
19 
20 namespace OHOS {
21 namespace NFC {
22 namespace NCI {
23 /* The maximum length of a default IsoDep consists of:
24  * CLA, INS, P1, P2, LC, LE + 255 payload bytes = 261 bytes
25  */
26 constexpr uint32_t ISO_DEP_FRAME_MAX_LEN = 261;
27 
28 constexpr uint32_t ISO_DEP_MAX_TRANSEIVE_LENGTH = 0xFEFF;
29 
GetInstance()30 TagNativeImpl& TagNativeImpl::GetInstance()
31 {
32     static TagNativeImpl tagNativeImpl;
33     return tagNativeImpl;
34 }
35 
36 /**
37  * @brief Set tag listener to receive tag status.
38  * @param listener The listener to receive tag status.
39  */
SetTagListener(std::weak_ptr<INciTagInterface::ITagListener> listener)40 void TagNativeImpl::SetTagListener(std::weak_ptr<INciTagInterface::ITagListener> listener)
41 {
42     tagListener_ = listener;
43 }
44 
45 /**
46  * @brief Get the TagHost instance by the tag discovered id.
47  * @param tagDiscId The tag discovered id given from nci stack.
48  */
GetTag(uint32_t tagDiscId)49 std::weak_ptr<TagHost> TagNativeImpl::GetTag(uint32_t tagDiscId)
50 {
51     std::map<uint32_t, std::shared_ptr<TagHost>>::iterator iter = tagHostMap_.find(tagDiscId);
52     if (iter == tagHostMap_.end()) {
53         return std::shared_ptr<TagHost>();
54     }
55     return iter->second;
56 }
57 
58 /**
59  * @brief Tag discovered, need to callback to nfc service.
60  * @param tagDiscId The tag discovered id given from nci stack.
61  * @param tagHost The TagHost instance created in TagNciAdapter when tag discorvered.
62  */
OnTagDiscovered(uint32_t tagDiscId,std::shared_ptr<TagHost> tagHost)63 void TagNativeImpl::OnTagDiscovered(uint32_t tagDiscId, std::shared_ptr<TagHost> tagHost)
64 {
65     if (tagListener_.expired()) {
66         return;
67     }
68     tagHostMap_.insert(make_pair(tagDiscId, tagHost));
69     tagListener_.lock()->OnTagDiscovered(tagDiscId);
70 }
71 
72 /**
73  * @brief Tag lost, need to callback to nfc service.
74  * @param tagDiscId The tag discovered id given from nci stack.
75  */
OnTagLost(uint32_t tagDiscId)76 void TagNativeImpl::OnTagLost(uint32_t tagDiscId)
77 {
78     if (tagListener_.expired()) {
79         return;
80     }
81     tagHostMap_.erase(tagDiscId);
82     tagListener_.lock()->OnTagLost(tagDiscId);
83 }
84 
85 /**
86  * @brief Check can make the ndef to be read only.
87  * @param ndefType The ndef type to check.
88  * @return True if can make read only, otherwise false.
89  */
CanMakeReadOnly(uint32_t ndefType)90 bool TagNativeImpl::CanMakeReadOnly(uint32_t ndefType)
91 {
92     return ndefType == KITS::EmNfcForumType::NFC_FORUM_TYPE_1 ||
93         ndefType == KITS::EmNfcForumType::NFC_FORUM_TYPE_2;
94 }
95 
96 /**
97  * @brief Build the tech mask by all given technologies.
98  * @param discTech The given technology list.
99  * @return The technology mask.
100  */
GetTechMaskFromTechList(const std::vector<uint32_t> & discTech)101 uint16_t TagNativeImpl::GetTechMaskFromTechList(const std::vector<uint32_t> &discTech)
102 {
103     uint16_t techMask = 0;
104     for (uint16_t i = 0; i < discTech.size(); i++) {
105         switch (discTech[i]) {
106             case static_cast<int32_t>(KITS::TagTechnology::NFC_A_TECH):
107                 techMask |= NFA_TECHNOLOGY_MASK_A;
108                 break;
109             case static_cast<int32_t>(KITS::TagTechnology::NFC_B_TECH):
110                 techMask |= NFA_TECHNOLOGY_MASK_B;
111                 break;
112             case static_cast<int32_t>(KITS::TagTechnology::NFC_F_TECH):
113                 techMask |= NFA_TECHNOLOGY_MASK_F;
114                 break;
115             case static_cast<int32_t>(KITS::TagTechnology::NFC_V_TECH):
116                 techMask |= NFA_TECHNOLOGY_MASK_V;
117                 break;
118             default:
119                 break;
120         }
121     }
122     return techMask;
123 }
124 
125 /**
126  * @brief Get the max transceive length of ISO-DEP technology.
127  * @return The max transceive length of ISO-DEP technology.
128  */
GetIsoDepMaxTransceiveLength()129 uint32_t TagNativeImpl::GetIsoDepMaxTransceiveLength()
130 {
131     if (NfcConfig::hasKey(NAME_ISO_DEP_MAX_TRANSCEIVE)) {
132         return NfcConfig::getUnsigned(NAME_ISO_DEP_MAX_TRANSCEIVE);
133     } else {
134         return ISO_DEP_MAX_TRANSEIVE_LENGTH;
135     }
136 }
137 
138 /**
139  * @brief Check if the nfc controller support extended APDU or not.
140  * @param length The max isodep length to check.
141  * @return True if the nfc controller support extended APDU, otherwise false.
142  */
IsExtendedLengthApduSupported(uint32_t length)143 bool TagNativeImpl::IsExtendedLengthApduSupported(uint32_t length)
144 {
145     return length > ISO_DEP_FRAME_MAX_LEN;
146 }
147 }  // namespace NCI
148 }  // namespace NFC
149 }  // namespace OHOS
150