• 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 "basic_tag_session.h"
16 
17 #include "loghelper.h"
18 #include "nfc_sdk_common.h"
19 
20 namespace OHOS {
21 namespace NFC {
22 namespace KITS {
BasicTagSession(std::weak_ptr<TagInfo> tagInfo,KITS::TagTechnology technology)23 BasicTagSession::BasicTagSession(std::weak_ptr<TagInfo> tagInfo, KITS::TagTechnology technology)
24     : tagInfo_(tagInfo), tagTechnology_(technology), isConnected_(false)
25 {
26 }
27 
GetTagSessionProxy() const28 OHOS::sptr<TAG::ITagSession> BasicTagSession::GetTagSessionProxy() const
29 {
30     if (tagInfo_.expired()) {
31         ErrorLog("[BasicTagSession::GetTagSessionProxy] tag is null.");
32         return nullptr;
33     }
34     return tagInfo_.lock()->GetTagSessionProxy();
35 }
36 
Connect()37 int BasicTagSession::Connect()
38 {
39     OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
40     if (tagSession == nullptr) {
41         ErrorLog("Connect, ERR_TAG_STATE_UNBIND");
42         return ErrorCode::ERR_TAG_STATE_UNBIND;
43     }
44     int tagRfDiscId = GetTagRfDiscId();
45     int ret = tagSession->Connect(tagRfDiscId, static_cast<int>(tagTechnology_));
46     if (ret == ErrorCode::ERR_NONE) {
47         isConnected_ = true;
48         SetConnectedTagTech(tagTechnology_);
49     }
50     return ret;
51 }
52 
IsConnected() const53 bool BasicTagSession::IsConnected() const
54 {
55     if (!isConnected_) {
56         return false;
57     }
58     KITS::TagTechnology connectedTagTech = GetConnectedTagTech();
59     if ((connectedTagTech != tagTechnology_) || (connectedTagTech == KITS::TagTechnology::NFC_INVALID_TECH)) {
60         return false;
61     }
62     return true;
63 }
64 
Close()65 int BasicTagSession::Close()
66 {
67     OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
68     if (tagSession == nullptr) {
69         ErrorLog("Close, ERR_TAG_STATE_UNBIND");
70         return ErrorCode::ERR_TAG_STATE_UNBIND;
71     }
72 
73     // do reconnect to reset the tag's state.
74     isConnected_ = false;
75     tagInfo_.lock()->SetConnectedTagTech(KITS::TagTechnology::NFC_INVALID_TECH);
76 
77     int statusCode = tagSession->Reconnect(GetTagRfDiscId());
78     if (statusCode == ErrorCode::ERR_NONE) {
79         isConnected_ = true;
80         SetConnectedTagTech(tagTechnology_);
81     }
82     return statusCode;
83 }
84 
SetTimeout(int timeout)85 int BasicTagSession::SetTimeout(int timeout)
86 {
87     OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
88     if (tagSession == nullptr) {
89         ErrorLog("SetTimeout, ERR_TAG_STATE_UNBIND");
90         return ErrorCode::ERR_TAG_STATE_UNBIND;
91     }
92     return tagSession->SetTimeout(timeout, static_cast<int>(tagTechnology_));
93 }
94 
GetTimeout(int & timeout)95 int BasicTagSession::GetTimeout(int &timeout)
96 {
97     OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
98     if (tagSession == nullptr) {
99         ErrorLog("GetTimeout, ERR_TAG_STATE_UNBIND");
100         return ErrorCode::ERR_TAG_STATE_UNBIND;
101     }
102     return tagSession->GetTimeout(static_cast<int>(tagTechnology_), timeout);
103 }
104 
GetTagUid()105 std::string BasicTagSession::GetTagUid()
106 {
107     if (tagInfo_.expired()) {
108         return "";
109     }
110     return tagInfo_.lock()->GetTagUid();
111 }
112 
SendCommand(std::string & hexCmdData,bool raw,std::string & hexRespData)113 int BasicTagSession::SendCommand(std::string& hexCmdData, bool raw, std::string &hexRespData)
114 {
115     OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
116     if (tagSession == nullptr) {
117         ErrorLog("BasicTagSession::SendCommand tagSession invalid");
118         return ErrorCode::ERR_TAG_STATE_UNBIND;
119     }
120     return tagSession->SendRawFrame(GetTagRfDiscId(), hexCmdData, raw, hexRespData);
121 }
122 
GetMaxSendCommandLength(int & maxSize) const123 int BasicTagSession::GetMaxSendCommandLength(int &maxSize) const
124 {
125     if (tagInfo_.expired() || (tagTechnology_ == KITS::TagTechnology::NFC_INVALID_TECH)) {
126         ErrorLog("GetMaxSendCommandLength ERR_TAG_PARAMETERS");
127         return ErrorCode::ERR_TAG_PARAMETERS;
128     }
129     OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
130     if (tagSession == nullptr) {
131         ErrorLog("GetMaxSendCommandLength ERR_TAG_STATE_UNBIND");
132         return ErrorCode::ERR_TAG_STATE_UNBIND;
133     }
134     return tagSession->GetMaxTransceiveLength(static_cast<int>(tagTechnology_), maxSize);
135 }
136 
GetTagRfDiscId() const137 int BasicTagSession::GetTagRfDiscId() const
138 {
139     if (tagInfo_.expired()) {
140         ErrorLog("[BasicTagSession::GetTagRfDiscId] tag is null.");
141         return ErrorCode::ERR_TAG_PARAMETERS;
142     }
143     return tagInfo_.lock()->GetTagRfDiscId();
144 }
145 
SetConnectedTagTech(KITS::TagTechnology tech) const146 void BasicTagSession::SetConnectedTagTech(KITS::TagTechnology tech) const
147 {
148     if (tagInfo_.expired()) {
149         ErrorLog("[BasicTagSession::SetConnectedTagTech] tag is null.");
150         return;
151     }
152     tagInfo_.lock()->SetConnectedTagTech(tech);
153 }
154 
GetConnectedTagTech() const155 KITS::TagTechnology BasicTagSession::GetConnectedTagTech() const
156 {
157     if (tagInfo_.expired()) {
158         ErrorLog("[BasicTagSession::GetConnectedTagTech] tag is null.");
159         return KITS::TagTechnology::NFC_INVALID_TECH;
160     }
161 
162     return tagInfo_.lock()->GetConnectedTagTech();
163 }
164 
GetTagInfo() const165 std::weak_ptr<TagInfo> BasicTagSession::GetTagInfo() const
166 {
167     return tagInfo_;
168 }
169 }  // namespace KITS
170 }  // namespace NFC
171 }  // namespace OHOS
172