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_stub.h"
16
17 #include "foreground_death_recipient.h"
18 #include "ipc_skeleton.h"
19 #include "loghelper.h"
20 #include "nfc_sdk_common.h"
21 #include "nfc_service_ipc_interface_code.h"
22 #include "permission_tools.h"
23
24 namespace OHOS {
25 namespace NFC {
26 namespace TAG {
27 using OHOS::AppExecFwk::ElementName;
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int TagSessionStub::OnRemoteRequest(uint32_t code, /* [in] */
29 MessageParcel& data, /* [in] */
30 MessageParcel& reply, /* [out] */
31 MessageOption& option) /* [in] */
32 {
33 DebugLog("TagSessionStub OnRemoteRequest occur, code is %d", code);
34 if (data.ReadInterfaceToken() != GetDescriptor()) {
35 ErrorLog("TagSessionStub OnRemoteRequest GetDescriptor failed");
36 return KITS::ErrorCode::ERR_TAG_PARAMETERS;
37 }
38
39 switch (code) {
40 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CONNECT):
41 return HandleConnect(data, reply);
42 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_RECONNECT):
43 return HandleReconnect(data, reply);
44 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_DISCONNECT):
45 return HandleDisconnect(data, reply);
46 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_SET_TIMEOUT):
47 return HandleSetTimeout(data, reply);
48 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_TIMEOUT):
49 return HandleGetTimeout(data, reply);
50 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_TECHLIST):
51 return HandleGetTechList(data, reply);
52 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_PRESENT):
53 return HandleIsTagFieldOn(data, reply);
54 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_NDEF):
55 return HandleIsNdef(data, reply);
56 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_SEND_RAW_FRAME):
57 return HandleSendRawFrame(data, reply);
58 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_NDEF_READ):
59 return HandleNdefRead(data, reply);
60 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_NDEF_WRITE):
61 return HandleNdefWrite(data, reply);
62 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_NDEF_MAKE_READ_ONLY):
63 return HandleNdefMakeReadOnly(data, reply);
64 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_FORMAT_NDEF):
65 return HandleFormatNdef(data, reply);
66 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CAN_MAKE_READ_ONLY):
67 return HandleCanMakeReadOnly(data, reply);
68 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_MAX_TRANSCEIVE_LENGTH):
69 return HandleGetMaxTransceiveLength(data, reply);
70 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_SUPPORTED_APDUS_EXTENDED):
71 return HandleIsSupportedApdusExtended(data, reply);
72 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REG_FOREGROUND):
73 return HandleRegForegroundDispatch(data, reply);
74 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_UNREG_FOREGROUND):
75 return HandleUnregForegroundDispatch(data, reply);
76 default:
77 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78 }
79 }
80
HandleConnect(MessageParcel & data,MessageParcel & reply)81 int TagSessionStub::HandleConnect(MessageParcel& data, MessageParcel& reply)
82 {
83 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
84 ErrorLog("HandleConnect, ERR_NO_PERMISSION");
85 return KITS::ErrorCode::ERR_NO_PERMISSION;
86 }
87
88 int tagRfDiscId = data.ReadInt32();
89 int tech = data.ReadInt32();
90 int statusCode = Connect(tagRfDiscId, tech);
91 reply.WriteInt32(statusCode);
92 return statusCode;
93 }
94
HandleReconnect(MessageParcel & data,MessageParcel & reply)95 int TagSessionStub::HandleReconnect(MessageParcel& data, MessageParcel& reply)
96 {
97 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
98 ErrorLog("HandleReconnect, ERR_NO_PERMISSION");
99 return KITS::ErrorCode::ERR_NO_PERMISSION;
100 }
101
102 int tagRfDiscId = data.ReadInt32();
103 int statusCode = Reconnect(tagRfDiscId);
104 reply.WriteInt32(statusCode);
105 return statusCode;
106 }
107
HandleDisconnect(MessageParcel & data,MessageParcel & reply)108 int TagSessionStub::HandleDisconnect(MessageParcel& data, MessageParcel& reply)
109 {
110 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
111 ErrorLog("HandleDisconnect, ERR_NO_PERMISSION");
112 return KITS::ErrorCode::ERR_NO_PERMISSION;
113 }
114
115 int tagRfDiscId = data.ReadInt32();
116 Disconnect(tagRfDiscId);
117 return ERR_NONE;
118 }
119
HandleSetTimeout(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)120 int TagSessionStub::HandleSetTimeout(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
121 {
122 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
123 ErrorLog("HandleSetTimeout, ERR_NO_PERMISSION");
124 return KITS::ErrorCode::ERR_NO_PERMISSION;
125 }
126 int tech = data.ReadInt32();
127 int timeout = data.ReadInt32();
128 int statusCode = SetTimeout(timeout, tech);
129 reply.WriteInt32(statusCode);
130 return statusCode;
131 }
132
HandleGetTimeout(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)133 int TagSessionStub::HandleGetTimeout(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
134 {
135 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
136 ErrorLog("HandleGetTimeout, ERR_NO_PERMISSION");
137 return KITS::ErrorCode::ERR_NO_PERMISSION;
138 }
139 int timeout = 0;
140 int tech = data.ReadInt32();
141 int statusCode = GetTimeout(tech, timeout);
142 reply.WriteInt32(timeout);
143 return statusCode;
144 }
145
HandleGetTechList(MessageParcel & data,MessageParcel & reply)146 int TagSessionStub::HandleGetTechList(MessageParcel& data, MessageParcel& reply)
147 {
148 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
149 ErrorLog("HandleGetTechList, ERR_NO_PERMISSION");
150 return KITS::ErrorCode::ERR_NO_PERMISSION;
151 }
152
153 int tagRfDiscId = data.ReadInt32();
154 std::vector<int32_t> techList = GetTechList(tagRfDiscId);
155 reply.WriteInt32Vector(techList);
156 return ERR_NONE;
157 }
158
HandleIsTagFieldOn(MessageParcel & data,MessageParcel & reply)159 int TagSessionStub::HandleIsTagFieldOn(MessageParcel& data, MessageParcel& reply)
160 {
161 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
162 ErrorLog("HandleIsTagFieldOn, ERR_NO_PERMISSION");
163 return KITS::ErrorCode::ERR_NO_PERMISSION;
164 }
165 int tagRfDiscId = data.ReadInt32();
166 reply.WriteBool(IsNdef(tagRfDiscId));
167 return ERR_NONE;
168 }
169
HandleIsNdef(MessageParcel & data,MessageParcel & reply)170 int TagSessionStub::HandleIsNdef(MessageParcel& data, MessageParcel& reply)
171 {
172 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
173 ErrorLog("HandleIsNdef, ERR_NO_PERMISSION");
174 return KITS::ErrorCode::ERR_NO_PERMISSION;
175 }
176 int tagRfDiscId = data.ReadInt32();
177 reply.WriteBool(IsNdef(tagRfDiscId));
178 return ERR_NONE;
179 }
180
HandleSendRawFrame(MessageParcel & data,MessageParcel & reply)181 int TagSessionStub::HandleSendRawFrame(MessageParcel& data, MessageParcel& reply)
182 {
183 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
184 ErrorLog("HandleSendRawFrame, ERR_NO_PERMISSION");
185 return KITS::ErrorCode::ERR_NO_PERMISSION;
186 }
187
188 int tagRfDiscId = data.ReadInt32();
189 std::string hexCmdData = data.ReadString();
190 bool raw = data.ReadBool();
191 std::string hexRespData;
192 int statusCode = SendRawFrame(tagRfDiscId, hexCmdData, raw, hexRespData);
193 reply.WriteString(hexRespData);
194 return statusCode;
195 }
196
HandleNdefRead(MessageParcel & data,MessageParcel & reply)197 int TagSessionStub::HandleNdefRead(MessageParcel& data, MessageParcel& reply)
198 {
199 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
200 ErrorLog("HandleNdefRead, ERR_NO_PERMISSION");
201 return KITS::ErrorCode::ERR_NO_PERMISSION;
202 }
203
204 int tagRfDiscId = data.ReadInt32();
205 std::string readData = NdefRead(tagRfDiscId);
206 reply.WriteString(readData);
207 return ERR_NONE;
208 }
209
HandleNdefWrite(MessageParcel & data,MessageParcel & reply)210 int TagSessionStub::HandleNdefWrite(MessageParcel& data, MessageParcel& reply)
211 {
212 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
213 ErrorLog("HandleNdefWrite, ERR_NO_PERMISSION");
214 return KITS::ErrorCode::ERR_NO_PERMISSION;
215 }
216
217 int tagRfDiscId = data.ReadInt32();
218 std::string msg = data.ReadString();
219 int status = NdefWrite(tagRfDiscId, msg);
220 reply.WriteInt32(status);
221 return ERR_NONE;
222 }
223
HandleNdefMakeReadOnly(MessageParcel & data,MessageParcel & reply)224 int TagSessionStub::HandleNdefMakeReadOnly(MessageParcel& data, MessageParcel& reply)
225 {
226 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
227 ErrorLog("HandleNdefMakeReadOnly, ERR_NO_PERMISSION");
228 return KITS::ErrorCode::ERR_NO_PERMISSION;
229 }
230
231 int tagRfDiscId = data.ReadInt32();
232 reply.WriteInt32(NdefMakeReadOnly(tagRfDiscId));
233 return ERR_NONE;
234 }
235
HandleFormatNdef(MessageParcel & data,MessageParcel & reply)236 int TagSessionStub::HandleFormatNdef(MessageParcel& data, MessageParcel& reply)
237 {
238 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
239 ErrorLog("HandleFormatNdef, ERR_NO_PERMISSION");
240 return KITS::ErrorCode::ERR_NO_PERMISSION;
241 }
242
243 int tagRfDiscId = data.ReadInt32();
244 std::string key = data.ReadString();
245 reply.WriteInt32(FormatNdef(tagRfDiscId, key));
246 return ERR_NONE;
247 }
248
HandleCanMakeReadOnly(MessageParcel & data,MessageParcel & reply)249 int TagSessionStub::HandleCanMakeReadOnly(MessageParcel& data, MessageParcel& reply)
250 {
251 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
252 ErrorLog("HandleCanMakeReadOnly, ERR_NO_PERMISSION");
253 return KITS::ErrorCode::ERR_NO_PERMISSION;
254 }
255 int ndefType = data.ReadInt32();
256 bool canSetReadOnly = false;
257 int statusCode = CanMakeReadOnly(ndefType, canSetReadOnly);
258 reply.WriteBool(canSetReadOnly);
259 return statusCode;
260 }
261
HandleGetMaxTransceiveLength(MessageParcel & data,MessageParcel & reply)262 int TagSessionStub::HandleGetMaxTransceiveLength(MessageParcel& data, MessageParcel& reply)
263 {
264 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
265 ErrorLog("HandleGetMaxTransceiveLength, ERR_NO_PERMISSION");
266 return KITS::ErrorCode::ERR_NO_PERMISSION;
267 }
268 int maxSize = 0;
269 int tech = data.ReadInt32();
270 int statusCode = GetMaxTransceiveLength(tech, maxSize);
271 reply.WriteInt32(maxSize);
272 return statusCode;
273 }
274
HandleIsSupportedApdusExtended(MessageParcel & data,MessageParcel & reply)275 int TagSessionStub::HandleIsSupportedApdusExtended(MessageParcel& data, MessageParcel& reply)
276 {
277 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
278 ErrorLog("HandleIsSupportedApdusExtended, ERR_NO_PERMISSION");
279 return KITS::ErrorCode::ERR_NO_PERMISSION;
280 }
281 bool isSupported = false;
282 int statusCode = IsSupportedApdusExtended(isSupported);
283 reply.WriteBool(isSupported);
284 return statusCode;
285 }
286
RemoveForegroundDeathRcpt(const wptr<IRemoteObject> & remote)287 void TagSessionStub::RemoveForegroundDeathRcpt(const wptr<IRemoteObject> &remote)
288 {
289 std::lock_guard<std::mutex> guard(mutex_);
290 if (foregroundCallback_ == nullptr) {
291 ErrorLog("OnRemoteDied callback_ is nullptr");
292 return;
293 }
294 auto serviceRemote = foregroundCallback_->AsObject();
295 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
296 serviceRemote->RemoveDeathRecipient(deathRecipient_);
297 foregroundCallback_ = nullptr;
298 ErrorLog("on remote died");
299 }
300 }
301
HandleRegForegroundDispatch(MessageParcel & data,MessageParcel & reply)302 int TagSessionStub::HandleRegForegroundDispatch(MessageParcel &data, MessageParcel &reply)
303 {
304 if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
305 ErrorLog("HandleRegForegroundDispatch, ERR_NO_PERMISSION");
306 return KITS::ErrorCode::ERR_NO_PERMISSION;
307 }
308 ElementName element = (*ElementName::Unmarshalling(data));
309 std::vector<uint32_t> discTech;
310 data.ReadUInt32Vector(&discTech);
311 KITS::ErrorCode ret = KITS::ERR_NFC_PARAMETERS;
312 do {
313 sptr<IRemoteObject> remote = data.ReadRemoteObject();
314 if (remote == nullptr) {
315 DebugLog("Failed to readRemoteObject!");
316 break;
317 }
318 std::unique_ptr<ForegroundDeathRecipient> recipient
319 = std::make_unique<ForegroundDeathRecipient>(this, IPCSkeleton::GetCallingTokenID());
320 sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
321 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(dr))) {
322 ErrorLog("Failed to add death recipient");
323 return ERR_NONE;
324 }
325 {
326 std::lock_guard<std::mutex> guard(mutex_);
327 deathRecipient_ = dr;
328 foregroundCallback_ = iface_cast<KITS::IForegroundCallback>(remote);
329 if (foregroundCallback_ == nullptr) {
330 foregroundCallback_ = new (std::nothrow) ForegroundCallbackProxy(remote);
331 DebugLog("create new `ForegroundCallbackProxy`!");
332 }
333 ret = RegForegroundDispatch(element, discTech, foregroundCallback_);
334 }
335 } while (0);
336 reply.WriteInt32(ret);
337 return ERR_NONE;
338 }
339
HandleUnregForegroundDispatch(MessageParcel & data,MessageParcel & reply)340 int TagSessionStub::HandleUnregForegroundDispatch(MessageParcel &data, MessageParcel &reply)
341 {
342 InfoLog("HandleUnregForegroundDispatch");
343 ElementName element = (*ElementName::Unmarshalling(data));
344 int exception = data.ReadInt32();
345 if (exception) {
346 return KITS::ERR_NFC_PARAMETERS;
347 }
348 KITS::ErrorCode ret = UnregForegroundDispatch(element);
349 DebugLog("HandleUnregForegroundDispatch end##ret=%{public}d\n", ret);
350 reply.WriteInt32(ret);
351 return ERR_NONE;
352 }
353 } // namespace TAG
354 } // namespace NFC
355 } // namespace OHOS
356