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