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