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 "itag_host.h"
17 #include "loghelper.h"
18
19 namespace OHOS {
20 namespace NFC {
21 namespace TAG {
22 using OHOS::AppExecFwk::ElementName;
23 const std::string DUMP_LINE = "---------------------------";
24 const std::string DUMP_END = "\n";
25
26 // NFC_A = 1 ~ NDEF_FORMATABLE = 10
27 const int MAX_TECH = 12;
28 int g_techTimeout[MAX_TECH] = {0};
29 int g_maxTransLength[MAX_TECH] = {0, 253, 253, 261, 255, 253, 0, 0, 253, 253, 0, 0};
30
TagSession(std::shared_ptr<INfcService> service)31 TagSession::TagSession(std::shared_ptr<INfcService> service)
32 : nfcService_(service)
33 {
34 if (service == nullptr) {
35 ErrorLog("TagSession create fail, service is nullptr");
36 return;
37 }
38 nfccHost_ = service->GetNfccHost();
39 tagDispatcher_ = service->GetTagDispatcher();
40 }
41
~TagSession()42 TagSession::~TagSession()
43 {
44 }
45
46 /**
47 * @brief To connect the tagRfDiscId by technology.
48 * @param tagRfDiscId the rf disc id of tag
49 * @param technology the tag technology
50 * @return the result to connect the tag
51 */
Connect(int tagRfDiscId,int technology)52 int TagSession::Connect(int tagRfDiscId, int technology)
53 {
54 if (!nfcService_.lock()->IsNfcEnabled()) {
55 ErrorLog("Connect, IsNfcEnabled error");
56 return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
57 }
58
59 /* find the tag in the hmap */
60 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
61 if (tag.expired()) {
62 ErrorLog("Connect, tagRfDiscId not found");
63 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
64 }
65
66 if (!tag.lock()->IsTagFieldOn()) {
67 ErrorLog("Connect, IsTagFieldOn error");
68 return NFC::KITS::ErrorCode::ERR_TAG_STATE_LOST;
69 }
70
71 if (tag.lock()->Connect(technology)) {
72 return NFC::KITS::ErrorCode::ERR_NONE;
73 } else {
74 ErrorLog("Connect, unallowd call error");
75 return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
76 }
77 }
78 /**
79 * @brief To reconnect the tagRfDiscId.
80 * @param tagRfDiscId the rf disc id of tag
81 * @return the result to reconnect the tag
82 */
Reconnect(int tagRfDiscId)83 int TagSession::Reconnect(int tagRfDiscId)
84 {
85 // Check if NFC is enabled
86 if (!nfcService_.lock()->IsNfcEnabled()) {
87 ErrorLog("Reconnect, IsTagFieldOn error");
88 return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
89 }
90
91 /* find the tag in the hmap */
92 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
93 if (tag.expired()) {
94 ErrorLog("Reconnect, tagRfDiscId not found");
95 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
96 }
97
98 if (tag.lock()->Reconnect()) {
99 return NFC::KITS::ErrorCode::ERR_NONE;
100 } else {
101 ErrorLog("Reconnect, unallowd call error");
102 return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
103 }
104 }
105 /**
106 * @brief To disconnect the tagRfDiscId.
107 * @param tagRfDiscId the rf disc id of tag
108 */
Disconnect(int tagRfDiscId)109 void TagSession::Disconnect(int tagRfDiscId)
110 {
111 // Check if NFC is enabled
112 if (!nfcService_.lock()->IsNfcEnabled()) {
113 ErrorLog("Disconnect, IsTagFieldOn error");
114 return;
115 }
116
117 /* find the tag in the hmap */
118 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
119 if (tag.expired()) {
120 ErrorLog("Disconnect, tagRfDiscId not found");
121 return;
122 }
123 tag.lock()->Disconnect();
124 }
SetTimeout(int timeout,int technology)125 int TagSession::SetTimeout(int timeout, int technology)
126 {
127 if (technology < 0 || technology >= MAX_TECH) {
128 ErrorLog("SetTimeout, invalid technology %{public}d", technology);
129 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
130 }
131 g_techTimeout[technology] = timeout;
132 return NFC::KITS::ErrorCode::ERR_NONE;
133 }
134
GetTimeout(int technology,int & timeout)135 int TagSession::GetTimeout(int technology, int &timeout)
136 {
137 if (technology < 0 || technology >= MAX_TECH) {
138 ErrorLog("GetTimeout, invalid technology %{public}d", technology);
139 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
140 }
141 timeout = g_techTimeout[technology];
142 return NFC::KITS::ErrorCode::ERR_NONE;
143 }
144 /**
145 * @brief Get the TechList of the tagRfDiscId.
146 * @param tagRfDiscId the rf disc id of tag
147 * @return TechList
148 */
GetTechList(int tagRfDiscId)149 std::vector<int> TagSession::GetTechList(int tagRfDiscId)
150 {
151 std::vector<int> techList;
152 // Check if NFC is enabled
153 if (!nfcService_.lock()->IsNfcEnabled()) {
154 ErrorLog("GetTechList, IsTagFieldOn error");
155 return techList;
156 }
157
158 /* find the tag in the hmap */
159 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
160 if (tag.expired()) {
161 ErrorLog("GetTechList, tagRfDiscId not found");
162 return techList;
163 }
164 return tag.lock()->GetTechList();
165 }
166 /**
167 * @brief Checking the tagRfDiscId is present.
168 * @param tagRfDiscId the rf disc id of tag
169 * @return true - Presnet; the other - No Presnet
170 */
IsTagFieldOn(int tagRfDiscId)171 bool TagSession::IsTagFieldOn(int tagRfDiscId)
172 {
173 // Check if NFC is enabled
174 if (!nfcService_.lock()->IsNfcEnabled()) {
175 ErrorLog("IsTagFieldOn, IsTagFieldOn error");
176 return false;
177 }
178
179 /* find the tag in the hmap */
180 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
181 if (tag.expired()) {
182 ErrorLog("IsTagFieldOn, tagRfDiscId not found");
183 return false;
184 }
185 return tag.lock()->IsTagFieldOn();
186 }
187 /**
188 * @brief Checking the tagRfDiscId is a Ndef Tag.
189 * @param tagRfDiscId the rf disc id of tag
190 * @return true - Ndef Tag; the other - No Ndef Tag
191 */
IsNdef(int tagRfDiscId)192 bool TagSession::IsNdef(int tagRfDiscId)
193 {
194 // Check if NFC is enabled
195 if (!nfcService_.lock()->IsNfcEnabled()) {
196 ErrorLog("IsNdef, IsTagFieldOn error");
197 return false;
198 }
199
200 /* find the tag in the hmap */
201 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
202 if (tag.expired()) {
203 ErrorLog("IsNdef, tagRfDiscId not found");
204 return false;
205 }
206 std::vector<int> ndefInfo;
207 return tag.lock()->IsNdefMsgContained(ndefInfo);
208 }
209
SendRawFrame(int tagRfDiscId,std::string hexCmdData,bool raw,std::string & hexRespData)210 int TagSession::SendRawFrame(int tagRfDiscId, std::string hexCmdData, bool raw, std::string &hexRespData)
211 {
212 DebugLog("Send Raw(%{public}d) Frame", raw);
213 // Check if NFC is enabled
214 if (!nfcService_.lock()->IsNfcEnabled()) {
215 ErrorLog("SendRawFrame, IsTagFieldOn error");
216 return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
217 }
218
219 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
220 if (tag.expired()) {
221 ErrorLog("SendRawFrame, tagRfDiscId not found");
222 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
223 }
224
225 // Check if length is within limits
226 int maxSize = 0;
227 GetMaxTransceiveLength(tag.lock()->GetConnectedTech(), maxSize);
228 if (KITS::NfcSdkCommon::GetHexStrBytesLen(hexCmdData) > static_cast<uint32_t>(maxSize)) {
229 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
230 }
231
232 int result = tag.lock()->Transceive(hexCmdData, hexRespData);
233 if (!hexRespData.empty()) {
234 return NFC::KITS::ErrorCode::ERR_NONE;
235 } else if (result == 1) { // result == 1 means that Tag lost
236 return NFC::KITS::ErrorCode::ERR_TAG_STATE_LOST;
237 }
238 return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
239 }
240 /**
241 * @brief Reading from the host tag
242 * @param tagRfDiscId the rf disc id of tag
243 * @return the read data
244 */
NdefRead(int tagRfDiscId)245 std::string TagSession::NdefRead(int tagRfDiscId)
246 {
247 // Check if NFC is enabled
248 if (!nfcService_.lock()->IsNfcEnabled()) {
249 ErrorLog("NdefRead, IsTagFieldOn error");
250 return "";
251 }
252 /* find the tag in the hmap */
253 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
254 if (tag.expired()) {
255 ErrorLog("NdefRead, tagRfDiscId not found");
256 return "";
257 }
258 return tag.lock()->ReadNdef();
259 }
260 /**
261 * @brief Writing the data into the host tag.
262 * @param tagRfDiscId the rf disc id of tag
263 * @param msg the wrote data
264 * @return the Writing Result
265 */
NdefWrite(int tagRfDiscId,std::string msg)266 int TagSession::NdefWrite(int tagRfDiscId, std::string msg)
267 {
268 // Check if NFC is enabled
269 if (!nfcService_.lock()->IsNfcEnabled()) {
270 ErrorLog("NdefWrite, IsTagFieldOn error");
271 return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
272 }
273
274 /* find the tag in the hmap */
275 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
276 if (tag.expired()) {
277 ErrorLog("NdefWrite, tagRfDiscId not found");
278 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
279 }
280
281 if (msg.empty()) {
282 ErrorLog("NdefWrite, msg.empty error");
283 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
284 }
285
286 if (tag.lock()->WriteNdef(msg)) {
287 return NFC::KITS::ErrorCode::ERR_NONE;
288 }
289 return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
290 }
291 /**
292 * @brief Making the host tag to read only.
293 * @param tagRfDiscId the rf disc id of tag
294 * @return the making result
295 */
NdefMakeReadOnly(int tagRfDiscId)296 int TagSession::NdefMakeReadOnly(int tagRfDiscId)
297 {
298 // Check if NFC is enabled
299 if (!nfcService_.lock()->IsNfcEnabled()) {
300 ErrorLog("NdefMakeReadOnly, IsTagFieldOn error");
301 return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
302 }
303
304 /* find the tag in the hmap */
305 std::weak_ptr<NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
306 if (tag.expired()) {
307 ErrorLog("NdefMakeReadOnly, tagRfDiscId not found");
308 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
309 }
310
311 if (tag.lock()->SetNdefReadOnly()) {
312 return NFC::KITS::ErrorCode::ERR_NONE;
313 }
314 return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
315 }
316 /**
317 * @brief format the tag by Ndef
318 * @param tagRfDiscId the rf disc id of tag
319 * @param key the format key
320 * @return the format result
321 */
FormatNdef(int tagRfDiscId,const std::string & key)322 int TagSession::FormatNdef(int tagRfDiscId, const std::string& key)
323 {
324 // Check if NFC is enabled
325 if (!nfcService_.lock()->IsNfcEnabled()) {
326 ErrorLog("FormatNdef, IsTagFieldOn error");
327 return NFC::KITS::ErrorCode::ERR_TAG_STATE_NFC_CLOSED;
328 }
329
330 /* find the tag in the hmap */
331 std::weak_ptr<NFC::NCI::ITagHost> tag = tagDispatcher_.lock()->FindTagHost(tagRfDiscId);
332 if (tag.expired()) {
333 ErrorLog("FormatNdef, tagRfDiscId not found");
334 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
335 }
336
337 if (tag.lock()->FormatNdef(key)) {
338 return NFC::KITS::ErrorCode::ERR_NONE;
339 }
340 return NFC::KITS::ErrorCode::ERR_TAG_STATE_IO_FAILED;
341 }
342
CanMakeReadOnly(int ndefType,bool & canSetReadOnly)343 int TagSession::CanMakeReadOnly(int ndefType, bool &canSetReadOnly)
344 {
345 if (nfccHost_.lock() == nullptr) {
346 ErrorLog("CanMakeReadOnly, nfccHost_ is nullptr");
347 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
348 }
349 canSetReadOnly = nfccHost_.lock()->CanMakeReadOnly(ndefType);
350 return NFC::KITS::ErrorCode::ERR_NONE;
351 }
352 /**
353 * @brief Get Max Transceive Length
354 * @param technology the tag technology
355 * @return Max Transceive Length
356 */
GetMaxTransceiveLength(int technology,int & maxSize)357 int TagSession::GetMaxTransceiveLength(int technology, int &maxSize)
358 {
359 if (technology >= MAX_TECH || technology < 0) {
360 ErrorLog("GetMaxTransceiveLength, technology not support");
361 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
362 }
363 maxSize = g_maxTransLength[technology];
364 return NFC::KITS::ErrorCode::ERR_NONE;
365 }
366
IsSupportedApdusExtended(bool & isSupported)367 int TagSession::IsSupportedApdusExtended(bool &isSupported)
368 {
369 if (nfccHost_.lock() == nullptr) {
370 ErrorLog("IsSupportedApdusExtended, nfccHost_ is nullptr");
371 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
372 }
373 isSupported = nfccHost_.lock()->GetExtendedLengthApdusSupported();
374 return NFC::KITS::ErrorCode::ERR_NONE;
375 }
376
RegForegroundDispatch(ElementName element,std::vector<uint32_t> & discTech,const sptr<KITS::IForegroundCallback> & callback)377 KITS::ErrorCode TagSession::RegForegroundDispatch(ElementName element, std::vector<uint32_t> &discTech,
378 const sptr<KITS::IForegroundCallback> &callback)
379 {
380 if (nfcService_.lock()->EnableForegroundDispatch(element, discTech, callback)) {
381 return KITS::ERR_NONE;
382 }
383 return KITS::ERR_NFC_PARAMETERS;
384 }
385
UnregForegroundDispatch(ElementName element)386 KITS::ErrorCode TagSession::UnregForegroundDispatch(ElementName element)
387 {
388 if (nfcService_.lock()->DisableForegroundDispatch(element)) {
389 return KITS::ERR_NONE;
390 }
391 return KITS::ERR_NFC_PARAMETERS;
392 }
393
Dump(int32_t fd,const std::vector<std::u16string> & args)394 int32_t TagSession::Dump(int32_t fd, const std::vector<std::u16string>& args)
395 {
396 std::string info = GetDumpInfo();
397 int ret = dprintf(fd, "%s\n", info.c_str());
398 if (ret < 0) {
399 ErrorLog("TagSession Dump ret = %{public}d", ret);
400 return NFC::KITS::ErrorCode::ERR_TAG_PARAMETERS;
401 }
402 return NFC::KITS::ErrorCode::ERR_NONE;
403 }
404
GetDumpInfo()405 std::string TagSession::GetDumpInfo()
406 {
407 std::string info;
408 return info.append(DUMP_LINE)
409 .append(" TAG DUMP ")
410 .append(DUMP_LINE)
411 .append(DUMP_END)
412 .append("NFC_STATE : ")
413 .append(std::to_string(nfcService_.lock()->GetNfcState()))
414 .append(DUMP_END)
415 .append("SCREEN_STATE : ")
416 .append(std::to_string(nfcService_.lock()->GetScreenState()))
417 .append(DUMP_END)
418 .append("NCI_VERSION : ")
419 .append(std::to_string(nfcService_.lock()->GetNciVersion()))
420 .append(DUMP_END);
421 }
422 } // namespace TAG
423 } // namespace NFC
424 } // namespace OHOS
425