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 "ndef_tag.h"
16 #include "loghelper.h"
17
18 namespace OHOS {
19 namespace NFC {
20 namespace KITS {
NdefTag(std::weak_ptr<TagInfo> tag)21 NdefTag::NdefTag(std::weak_ptr<TagInfo> tag) : BasicTagSession(tag, KITS::TagTechnology::NFC_NDEF_TECH)
22 {
23 AppExecFwk::PacMap extraData = tag.lock()->GetTechExtrasByTech(KITS::TagTechnology::NFC_NDEF_TECH);
24 if (extraData.IsEmpty()) {
25 ErrorLog("NdefTag::NdefTag extra data invalid");
26 return;
27 }
28
29 nfcForumType_ = (EmNfcForumType)tag.lock()->GetIntExtrasData(extraData, TagInfo::NDEF_FORUM_TYPE);
30 ndefTagMode_ = (EmNdefTagMode)tag.lock()->GetIntExtrasData(extraData, TagInfo::NDEF_TAG_MODE);
31 ndefMsg_ = tag.lock()->GetStringExtrasData(extraData, TagInfo::NDEF_MSG);
32 maxTagSize_ = static_cast<uint32_t>(tag.lock()->GetIntExtrasData(extraData, TagInfo::NDEF_TAG_LENGTH));
33
34 InfoLog("NdefTag::NdefTag nfcForumType_(%{public}d) ndefTagMode_(%{public}d) maxTagSize_(%{public}d)",
35 nfcForumType_, ndefTagMode_, maxTagSize_);
36 }
37
GetTag(std::weak_ptr<TagInfo> tag)38 std::shared_ptr<NdefTag> NdefTag::GetTag(std::weak_ptr<TagInfo> tag)
39 {
40 if (tag.expired() || !tag.lock()->IsTechSupported(KITS::TagTechnology::NFC_NDEF_TECH)) {
41 ErrorLog("NdefTag::GetTag error, no mathced technology.");
42 return nullptr;
43 }
44
45 return std::make_shared<NdefTag>(tag);
46 }
47
GetNdefTagType() const48 NdefTag::EmNfcForumType NdefTag::GetNdefTagType() const
49 {
50 return nfcForumType_;
51 }
52
GetNdefTagMode() const53 NdefTag::EmNdefTagMode NdefTag::GetNdefTagMode() const
54 {
55 return ndefTagMode_;
56 }
57
GetMaxTagSize() const58 uint32_t NdefTag::GetMaxTagSize() const
59 {
60 return maxTagSize_;
61 }
62
GetCachedNdefMsg() const63 std::shared_ptr<NdefMessage> NdefTag::GetCachedNdefMsg() const
64 {
65 return NdefMessage::GetNdefMessage(ndefMsg_);
66 }
67
GetNdefTagTypeString(EmNfcForumType emNfcForumType)68 std::string NdefTag::GetNdefTagTypeString(EmNfcForumType emNfcForumType)
69 {
70 std::string typeString;
71 switch (emNfcForumType) {
72 case NFC_FORUM_TYPE_1:
73 typeString = STRING_NFC_FORUM_TYPE_1;
74 break;
75 case NFC_FORUM_TYPE_2:
76 typeString = STRING_NFC_FORUM_TYPE_2;
77 break;
78 case NFC_FORUM_TYPE_3:
79 typeString = STRING_NFC_FORUM_TYPE_3;
80 break;
81 case NFC_FORUM_TYPE_4:
82 typeString = STRING_NFC_FORUM_TYPE_4;
83 break;
84 case MIFARE_CLASSIC:
85 typeString = STRING_MIFARE_CLASSIC;
86 break;
87 case ICODE_SLI:
88 typeString = STRING_ICODE_SLI;
89 break;
90 default:
91 break;
92 }
93 return typeString;
94 }
95
IsNdefWritable() const96 bool NdefTag::IsNdefWritable() const
97 {
98 return (ndefTagMode_ == EmNdefTagMode::MODE_READ_WRITE);
99 }
100
ReadNdef(std::shared_ptr<NdefMessage> & ndefMessage)101 int NdefTag::ReadNdef(std::shared_ptr<NdefMessage> &ndefMessage)
102 {
103 OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
104 if (!tagSession) {
105 ErrorLog("[NdefTag::ReadNdef] tagSession is null.");
106 return ErrorCode::ERR_TAG_STATE_UNBIND;
107 }
108
109 if (tagSession->IsNdef(GetTagRfDiscId())) {
110 std::string messageData = tagSession->NdefRead(GetTagRfDiscId());
111 if (messageData.empty() && !tagSession->IsTagFieldOn(GetTagRfDiscId())) {
112 ErrorLog("[NdefTag::ReadNdef] read ndef message is null and tag is not field on");
113 return ErrorCode::ERR_TAG_STATE_LOST;
114 }
115
116 ndefMessage = NdefMessage::GetNdefMessage(messageData);
117 return ErrorCode::ERR_NONE;
118 } else {
119 if (!tagSession->IsTagFieldOn(GetTagRfDiscId())) {
120 WarnLog("[NdefTag::ReadNdef] tag is not field on.");
121 return ErrorCode::ERR_TAG_STATE_LOST;
122 }
123 }
124 return ErrorCode::ERR_TAG_PARAMETERS;
125 }
126
WriteNdef(std::shared_ptr<NdefMessage> msg)127 int NdefTag::WriteNdef(std::shared_ptr<NdefMessage> msg)
128 {
129 if (!IsConnected()) {
130 ErrorLog("[NdefTag::WriteNdef] connect tag first!");
131 return ErrorCode::ERR_TAG_STATE_DISCONNECTED;
132 }
133
134 OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
135 if (!tagSession) {
136 ErrorLog("[NdefTag::WriteNdef] tagSession is null.");
137 return ErrorCode::ERR_TAG_STATE_UNBIND;
138 }
139 if (!tagSession->IsNdef(GetTagRfDiscId())) {
140 ErrorLog("[NdefTag::WriteNdef] not ndef tag.");
141 return ErrorCode::ERR_TAG_PARAMETERS;
142 }
143 if (!IsNdefWritable()) {
144 ErrorLog("[NdefTag::WriteNdef] not writable.");
145 return ErrorCode::ERR_TAG_STATE_IO_FAILED;
146 }
147 std::string ndefMessage = NdefMessage::MessageToString(msg);
148 return tagSession->NdefWrite(GetTagRfDiscId(), ndefMessage);
149 }
150
IsEnableReadOnly(bool & canSetReadOnly)151 int NdefTag::IsEnableReadOnly(bool &canSetReadOnly)
152 {
153 OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
154 if (!tagSession) {
155 DebugLog("[NdefTag::IsEnableReadOnly] tagSession is null.");
156 return ErrorCode::ERR_TAG_STATE_UNBIND;
157 }
158 return tagSession->CanMakeReadOnly(nfcForumType_, canSetReadOnly);
159 }
160
EnableReadOnly()161 int NdefTag::EnableReadOnly()
162 {
163 if (!IsConnected()) {
164 ErrorLog("[NdefTag::EnableReadOnly] connect tag first!");
165 return ErrorCode::ERR_TAG_STATE_DISCONNECTED;
166 }
167 OHOS::sptr<TAG::ITagSession> tagSession = GetTagSessionProxy();
168 if (!tagSession) {
169 ErrorLog("[NdefTag::EnableReadOnly] tagSession is null.");
170 return ErrorCode::ERR_TAG_STATE_UNBIND;
171 }
172 return tagSession->NdefMakeReadOnly(GetTagRfDiscId());
173 }
174 } // namespace KITS
175 } // namespace NFC
176 } // namespace OHOS