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 "taginfo.h"
16 #include "loghelper.h"
17 #include "nfc_controller.h"
18 #include "nfc_sdk_common.h"
19 #include "parcel.h"
20 #include "refbase.h"
21 #include "tag_session_proxy.h"
22
23 namespace OHOS {
24 namespace NFC {
25 namespace KITS {
TagInfo(std::vector<int> tagTechList,std::vector<AppExecFwk::PacMap> tagTechExtrasData,std::string & tagUid,int tagRfDiscId,OHOS::sptr<IRemoteObject> tagServiceIface)26 TagInfo::TagInfo(std::vector<int> tagTechList,
27 std::vector<AppExecFwk::PacMap> tagTechExtrasData,
28 std::string& tagUid,
29 int tagRfDiscId,
30 OHOS::sptr<IRemoteObject> tagServiceIface)
31 {
32 tagRfDiscId_ = tagRfDiscId;
33 tagUid_ = tagUid;
34 tagTechList_ = std::move(tagTechList);
35 tagServiceIface_ = tagServiceIface;
36 tagTechExtrasData_ = std::move(tagTechExtrasData);
37 connectedTagTech_ = KITS::TagTechnology::NFC_INVALID_TECH;
38 if (tagServiceIface != nullptr) {
39 tagSessionProxy_ = new TAG::TagSessionProxy(tagServiceIface);
40 }
41 }
42
~TagInfo()43 TagInfo::~TagInfo()
44 {
45 tagUid_.clear();
46 tagTechList_.clear();
47 connectedTagTech_ = KITS::TagTechnology::NFC_INVALID_TECH;
48 tagRfDiscId_ = 0;
49 }
50
IsTechSupported(KITS::TagTechnology tech)51 bool TagInfo::IsTechSupported(KITS::TagTechnology tech)
52 {
53 for (auto n : tagTechList_) {
54 if (n == static_cast<int>(tech)) {
55 return true;
56 }
57 }
58 return false;
59 }
60
GetTagSessionProxy()61 OHOS::sptr<TAG::ITagSession> TagInfo::GetTagSessionProxy()
62 {
63 if (tagSessionProxy_ == nullptr) {
64 OHOS::sptr<IRemoteObject> iface = NfcController::GetInstance().GetTagServiceIface();
65 if (iface != nullptr) {
66 tagSessionProxy_ = new TAG::TagSessionProxy(iface);
67 }
68 }
69 return tagSessionProxy_;
70 }
71
GetTagTechList() const72 std::vector<int> TagInfo::GetTagTechList() const
73 {
74 return std::move(tagTechList_);
75 }
76
GetStringTach(int tech)77 std::string TagInfo::GetStringTach(int tech)
78 {
79 switch (tech) {
80 case static_cast<int>(TagTechnology::NFC_A_TECH):
81 return "NfcA";
82 case static_cast<int>(TagTechnology::NFC_B_TECH):
83 return "NfcB";
84 case static_cast<int>(TagTechnology::NFC_F_TECH):
85 return "NfcF";
86 case static_cast<int>(TagTechnology::NFC_V_TECH):
87 return "NfcV";
88 case static_cast<int>(TagTechnology::NFC_ISODEP_TECH):
89 return "IsoDep";
90 case static_cast<int>(TagTechnology::NFC_MIFARE_CLASSIC_TECH):
91 return "MifareClassic";
92 case static_cast<int>(TagTechnology::NFC_MIFARE_ULTRALIGHT_TECH):
93 return "MifareUL";
94 case static_cast<int>(TagTechnology::NFC_NDEF_TECH):
95 return "Ndef";
96 case static_cast<int>(TagTechnology::NFC_NDEF_FORMATABLE_TECH):
97 return "NdefFormatable";
98 default:
99 break;
100 }
101 return "";
102 }
103
GetTechExtrasByIndex(size_t techIndex)104 AppExecFwk::PacMap TagInfo::GetTechExtrasByIndex(size_t techIndex)
105 {
106 AppExecFwk::PacMap pacmap;
107 if (tagTechList_.size() == 0 || tagTechList_.size() != tagTechExtrasData_.size()) {
108 ErrorLog("Taginfo:: tagTechList_lenth != tagTechExtrasData_length.");
109 return pacmap;
110 }
111 if (techIndex < 0 || techIndex >= tagTechExtrasData_.size()) {
112 return pacmap;
113 }
114 return tagTechExtrasData_[techIndex];
115 }
116
GetTechExtrasByTech(KITS::TagTechnology tech)117 AppExecFwk::PacMap TagInfo::GetTechExtrasByTech(KITS::TagTechnology tech)
118 {
119 AppExecFwk::PacMap pacmap;
120 if (tagTechList_.size() == 0 || tagTechList_.size() != tagTechExtrasData_.size()) {
121 return pacmap;
122 }
123
124 for (size_t i = 0; i < tagTechList_.size(); i++) {
125 if (static_cast<int>(tech) == tagTechList_[i]) {
126 pacmap = tagTechExtrasData_[i];
127 break;
128 }
129 }
130 return pacmap;
131 }
132
GetStringExtrasData(AppExecFwk::PacMap & extrasData,const std::string & extrasName)133 std::string TagInfo::GetStringExtrasData(AppExecFwk::PacMap& extrasData, const std::string& extrasName)
134 {
135 if (extrasData.IsEmpty() || extrasName.empty()) {
136 return "";
137 }
138 return extrasData.GetStringValue(extrasName, "");
139 }
140
GetIntExtrasData(AppExecFwk::PacMap & extrasData,const std::string & extrasName)141 int TagInfo::GetIntExtrasData(AppExecFwk::PacMap& extrasData, const std::string& extrasName)
142 {
143 if (extrasData.IsEmpty() || extrasName.empty()) {
144 return ErrorCode::ERR_TAG_PARAMETERS;
145 }
146 return extrasData.GetIntValue(extrasName, 0);
147 }
148
GetBoolExtrasData(AppExecFwk::PacMap & extrasData,const std::string & extrasName)149 bool TagInfo::GetBoolExtrasData(AppExecFwk::PacMap& extrasData, const std::string& extrasName)
150 {
151 if (extrasData.IsEmpty() || extrasName.empty()) {
152 return false;
153 }
154 return extrasData.GetBooleanValue(extrasName, false);
155 }
156
SetConnectedTagTech(KITS::TagTechnology connectedTagTech)157 void TagInfo::SetConnectedTagTech(KITS::TagTechnology connectedTagTech)
158 {
159 connectedTagTech_ = connectedTagTech;
160 }
161
GetConnectedTagTech() const162 KITS::TagTechnology TagInfo::GetConnectedTagTech() const
163 {
164 return connectedTagTech_;
165 }
166
GetTagUid() const167 std::string TagInfo::GetTagUid() const
168 {
169 return tagUid_;
170 }
171
GetTagRfDiscId() const172 int TagInfo::GetTagRfDiscId() const
173 {
174 return tagRfDiscId_;
175 }
176
177 } // namespace KITS
178 } // namespace NFC
179 } // namespace OHOS