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 "external_deps_proxy.h"
18 #include "foreground_death_recipient.h"
19 #include "ipc_skeleton.h"
20 #include "loghelper.h"
21 #include "nfc_sdk_common.h"
22 #include "nfc_service_ipc_interface_code.h"
23 #include "nfc_permission_checker.h"
24 #include "reader_mode_death_recipient.h"
25 
26 namespace OHOS {
27 namespace NFC {
28 namespace TAG {
29 using OHOS::AppExecFwk::ElementName;
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int TagSessionStub::OnRemoteRequest(uint32_t code,         /* [in] */
31                                     MessageParcel& data,   /* [in] */
32                                     MessageParcel& reply,  /* [out] */
33                                     MessageOption& option) /* [in] */
34 {
35     DebugLog("TagSessionStub OnRemoteRequest occur, code is %d", code);
36     if (data.ReadInterfaceToken() != GetDescriptor()) {
37         ErrorLog("TagSessionStub OnRemoteRequest GetDescriptor failed");
38         return KITS::ErrorCode::ERR_TAG_PARAMETERS;
39     }
40 
41     switch (code) {
42         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CONNECT):
43             return HandleConnect(data, reply);
44         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_CONNECTED):
45             return HandleIsConnected(data, reply);
46         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_RECONNECT):
47             return HandleReconnect(data, reply);
48         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_DISCONNECT):
49             return HandleDisconnect(data, reply);
50         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_SET_TIMEOUT):
51             return HandleSetTimeout(data, reply);
52         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_TIMEOUT):
53             return HandleGetTimeout(data, reply);
54         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_RESET_TIMEOUT):
55             return HandleResetTimeout(data, reply);
56         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_TECHLIST):
57             return HandleGetTechList(data, reply);
58         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_PRESENT):
59             return HandleIsTagFieldOn(data, reply);
60         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_NDEF):
61             return HandleIsNdef(data, reply);
62         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_SEND_RAW_FRAME):
63             return HandleSendRawFrame(data, reply);
64         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_NDEF_READ):
65             return HandleNdefRead(data, reply);
66         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_NDEF_WRITE):
67             return HandleNdefWrite(data, reply);
68         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_NDEF_MAKE_READ_ONLY):
69             return HandleNdefMakeReadOnly(data, reply);
70         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_FORMAT_NDEF):
71             return HandleFormatNdef(data, reply);
72         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CAN_MAKE_READ_ONLY):
73             return HandleCanMakeReadOnly(data, reply);
74         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_MAX_TRANSCEIVE_LENGTH):
75             return HandleGetMaxTransceiveLength(data, reply);
76         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_SUPPORTED_APDUS_EXTENDED):
77             return HandleIsSupportedApdusExtended(data, reply);
78         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REG_FOREGROUND):
79             return HandleRegForegroundDispatch(data, reply);
80         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_UNREG_FOREGROUND):
81             return HandleUnregForegroundDispatch(data, reply);
82         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REG_READER_MODE):
83             return HandleRegReaderMode(data, reply);
84         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_UNREG_READER_MODE):
85             return HandleUnregReaderMode(data, reply);
86         default:
87             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
88     }
89 }
90 
HandleConnect(MessageParcel & data,MessageParcel & reply)91 int TagSessionStub::HandleConnect(MessageParcel& data, MessageParcel& reply)
92 {
93     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
94         ErrorLog("HandleConnect, ERR_NO_PERMISSION");
95         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
96         return KITS::ErrorCode::ERR_NO_PERMISSION;
97     }
98 
99     int tagRfDiscId = data.ReadInt32();
100     int tech = data.ReadInt32();
101     int statusCode = Connect(tagRfDiscId, tech);
102     reply.WriteInt32(statusCode);
103     return ERR_NONE;
104 }
105 
HandleIsConnected(MessageParcel & data,MessageParcel & reply)106 int TagSessionStub::HandleIsConnected(MessageParcel& data, MessageParcel& reply)
107 {
108     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
109         ErrorLog("HandleConnect, ERR_NO_PERMISSION");
110         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
111         return KITS::ErrorCode::ERR_NO_PERMISSION;
112     }
113     bool isConnected = false;
114     int tagRfDiscId = data.ReadInt32();
115     int statusCode = IsConnected(tagRfDiscId, isConnected);
116     reply.WriteBool(isConnected);
117     return statusCode;
118 }
119 
HandleReconnect(MessageParcel & data,MessageParcel & reply)120 int TagSessionStub::HandleReconnect(MessageParcel& data, MessageParcel& reply)
121 {
122     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
123         ErrorLog("HandleReconnect, ERR_NO_PERMISSION");
124         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
125         return KITS::ErrorCode::ERR_NO_PERMISSION;
126     }
127 
128     int tagRfDiscId = data.ReadInt32();
129     int statusCode = Reconnect(tagRfDiscId);
130     reply.WriteInt32(statusCode);
131     return ERR_NONE;
132 }
133 
HandleDisconnect(MessageParcel & data,MessageParcel & reply)134 int TagSessionStub::HandleDisconnect(MessageParcel& data, MessageParcel& reply)
135 {
136     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
137         ErrorLog("HandleDisconnect, ERR_NO_PERMISSION");
138         return KITS::ErrorCode::ERR_NO_PERMISSION;
139     }
140 
141     int tagRfDiscId = data.ReadInt32();
142     Disconnect(tagRfDiscId);
143     return ERR_NONE;
144 }
145 
HandleSetTimeout(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)146 int TagSessionStub::HandleSetTimeout(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
147 {
148     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
149         ErrorLog("HandleSetTimeout, ERR_NO_PERMISSION");
150         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
151         return KITS::ErrorCode::ERR_NO_PERMISSION;
152     }
153     int tagRfDiscId = data.ReadInt32();
154     int tech = data.ReadInt32();
155     int timeout = data.ReadInt32();
156     int statusCode = SetTimeout(tagRfDiscId, timeout, tech);
157     reply.WriteInt32(statusCode);
158     return ERR_NONE;
159 }
160 
HandleGetTimeout(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)161 int TagSessionStub::HandleGetTimeout(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
162 {
163     int timeout = 0;
164     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
165         ErrorLog("HandleGetTimeout, ERR_NO_PERMISSION");
166         reply.WriteInt32(timeout);
167         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
168         return KITS::ErrorCode::ERR_NO_PERMISSION;
169     }
170     int tagRfDiscId = data.ReadInt32();
171     int tech = data.ReadInt32();
172     int statusCode = GetTimeout(tagRfDiscId, tech, timeout);
173     reply.WriteInt32(timeout);
174     reply.WriteInt32(statusCode);
175     return ERR_NONE;
176 }
177 
HandleResetTimeout(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)178 int TagSessionStub::HandleResetTimeout(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
179 {
180     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
181         ErrorLog("HandleResetTimeout, ERR_NO_PERMISSION");
182         return KITS::ErrorCode::ERR_NO_PERMISSION;
183     }
184     int tagRfDiscId = data.ReadInt32();
185     ResetTimeout(tagRfDiscId);
186     return ERR_NONE;
187 }
188 
HandleGetTechList(MessageParcel & data,MessageParcel & reply)189 int TagSessionStub::HandleGetTechList(MessageParcel& data, MessageParcel& reply)
190 {
191     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
192         ErrorLog("HandleGetTechList, ERR_NO_PERMISSION");
193         return KITS::ErrorCode::ERR_NO_PERMISSION;
194     }
195 
196     int tagRfDiscId = data.ReadInt32();
197     std::vector<int> techList = GetTechList(tagRfDiscId);
198     reply.WriteInt32Vector(techList);
199     return ERR_NONE;
200 }
201 
HandleIsTagFieldOn(MessageParcel & data,MessageParcel & reply)202 int TagSessionStub::HandleIsTagFieldOn(MessageParcel& data, MessageParcel& reply)
203 {
204     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
205         ErrorLog("HandleIsTagFieldOn, ERR_NO_PERMISSION");
206         return KITS::ErrorCode::ERR_NO_PERMISSION;
207     }
208     int tagRfDiscId = data.ReadInt32();
209     reply.WriteBool(IsTagFieldOn(tagRfDiscId));
210     return ERR_NONE;
211 }
212 
HandleIsNdef(MessageParcel & data,MessageParcel & reply)213 int TagSessionStub::HandleIsNdef(MessageParcel& data, MessageParcel& reply)
214 {
215     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
216         ErrorLog("HandleIsNdef, ERR_NO_PERMISSION");
217         return KITS::ErrorCode::ERR_NO_PERMISSION;
218     }
219     int tagRfDiscId = data.ReadInt32();
220     reply.WriteBool(IsNdef(tagRfDiscId));
221     return ERR_NONE;
222 }
223 
HandleSendRawFrame(MessageParcel & data,MessageParcel & reply)224 int TagSessionStub::HandleSendRawFrame(MessageParcel& data, MessageParcel& reply)
225 {
226     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
227         ErrorLog("HandleSendRawFrame, ERR_NO_PERMISSION");
228         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
229         return KITS::ErrorCode::ERR_NO_PERMISSION;
230     }
231 
232     int tagRfDiscId = data.ReadInt32();
233     std::string hexCmdData = data.ReadString();
234     bool raw = data.ReadBool();
235     std::string hexRespData;
236     int statusCode = SendRawFrame(tagRfDiscId, hexCmdData, raw, hexRespData);
237     reply.WriteString(hexRespData);
238     DebugLog("TagSessionStub::HandleSendRawFrame:statusCode=0x%{public}X", statusCode);
239     reply.WriteInt32(statusCode);
240     return ERR_NONE;
241 }
242 
HandleNdefRead(MessageParcel & data,MessageParcel & reply)243 int TagSessionStub::HandleNdefRead(MessageParcel& data, MessageParcel& reply)
244 {
245     std::string readData {};
246     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
247         ErrorLog("HandleNdefRead, ERR_NO_PERMISSION");
248         reply.WriteString(readData);
249         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
250         return KITS::ErrorCode::ERR_NO_PERMISSION;
251     }
252 
253     int tagRfDiscId = data.ReadInt32();
254     int statusCode = NdefRead(tagRfDiscId, readData);
255     reply.WriteString(readData);
256     reply.WriteInt32(statusCode);
257     return ERR_NONE;
258 }
259 
HandleNdefWrite(MessageParcel & data,MessageParcel & reply)260 int TagSessionStub::HandleNdefWrite(MessageParcel& data, MessageParcel& reply)
261 {
262     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
263         ErrorLog("HandleNdefWrite, ERR_NO_PERMISSION");
264         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
265         return KITS::ErrorCode::ERR_NO_PERMISSION;
266     }
267 
268     int tagRfDiscId = data.ReadInt32();
269     std::string msg = data.ReadString();
270     int statusCode = NdefWrite(tagRfDiscId, msg);
271     reply.WriteInt32(statusCode);
272     return ERR_NONE;
273 }
274 
HandleNdefMakeReadOnly(MessageParcel & data,MessageParcel & reply)275 int TagSessionStub::HandleNdefMakeReadOnly(MessageParcel& data, MessageParcel& reply)
276 {
277     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
278         ErrorLog("HandleNdefMakeReadOnly, ERR_NO_PERMISSION");
279         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
280         return KITS::ErrorCode::ERR_NO_PERMISSION;
281     }
282 
283     int tagRfDiscId = data.ReadInt32();
284     reply.WriteInt32(NdefMakeReadOnly(tagRfDiscId));
285     return ERR_NONE;
286 }
287 
HandleFormatNdef(MessageParcel & data,MessageParcel & reply)288 int TagSessionStub::HandleFormatNdef(MessageParcel& data, MessageParcel& reply)
289 {
290     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
291         ErrorLog("HandleFormatNdef, ERR_NO_PERMISSION");
292         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
293         return KITS::ErrorCode::ERR_NO_PERMISSION;
294     }
295 
296     int tagRfDiscId = data.ReadInt32();
297     std::string key = data.ReadString();
298     reply.WriteInt32(FormatNdef(tagRfDiscId, key));
299     return ERR_NONE;
300 }
301 
HandleCanMakeReadOnly(MessageParcel & data,MessageParcel & reply)302 int TagSessionStub::HandleCanMakeReadOnly(MessageParcel& data, MessageParcel& reply)
303 {
304     bool canSetReadOnly = false;
305     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
306         ErrorLog("HandleCanMakeReadOnly, ERR_NO_PERMISSION");
307         reply.WriteBool(canSetReadOnly);
308         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
309         return KITS::ErrorCode::ERR_NO_PERMISSION;
310     }
311     int ndefType = data.ReadInt32();
312     int statusCode = CanMakeReadOnly(ndefType, canSetReadOnly);
313     reply.WriteBool(canSetReadOnly);
314     reply.WriteInt32(statusCode);
315     return ERR_NONE;
316 }
317 
HandleGetMaxTransceiveLength(MessageParcel & data,MessageParcel & reply)318 int TagSessionStub::HandleGetMaxTransceiveLength(MessageParcel& data, MessageParcel& reply)
319 {
320     int maxSize = 0;
321     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
322         ErrorLog("HandleGetMaxTransceiveLength, ERR_NO_PERMISSION");
323         reply.WriteInt32(maxSize);
324         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
325         return KITS::ErrorCode::ERR_NO_PERMISSION;
326     }
327     int tech = data.ReadInt32();
328     int statusCode = GetMaxTransceiveLength(tech, maxSize);
329     reply.WriteInt32(maxSize);
330     reply.WriteInt32(statusCode);
331     return ERR_NONE;
332 }
333 
HandleIsSupportedApdusExtended(MessageParcel & data,MessageParcel & reply)334 int TagSessionStub::HandleIsSupportedApdusExtended(MessageParcel& data, MessageParcel& reply)
335 {
336     bool isSupported = false;
337     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
338         ErrorLog("HandleIsSupportedApdusExtended, ERR_NO_PERMISSION");
339         reply.WriteBool(isSupported);
340         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
341         return KITS::ErrorCode::ERR_NO_PERMISSION;
342     }
343     int statusCode = IsSupportedApdusExtended(isSupported);
344     reply.WriteBool(isSupported);
345     reply.WriteInt32(statusCode);
346     return ERR_NONE;
347 }
348 
RemoveForegroundDeathRcpt(const wptr<IRemoteObject> & remote)349 void TagSessionStub::RemoveForegroundDeathRcpt(const wptr<IRemoteObject> &remote)
350 {
351     std::lock_guard<std::mutex> guard(mutex_);
352     if (foregroundCallback_ == nullptr) {
353         ErrorLog("OnRemoteDied callback_ is nullptr");
354         return;
355     }
356     auto serviceRemote = foregroundCallback_->AsObject();
357     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
358         serviceRemote->RemoveDeathRecipient(foregroundDeathRecipient_);
359         foregroundCallback_ = nullptr;
360         ErrorLog("on remote died");
361     }
362 }
363 
HandleRegForegroundDispatch(MessageParcel & data,MessageParcel & reply)364 int TagSessionStub::HandleRegForegroundDispatch(MessageParcel &data, MessageParcel &reply)
365 {
366     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
367         ErrorLog("HandleRegForegroundDispatch, ERR_NO_PERMISSION");
368         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
369         return KITS::ErrorCode::ERR_NO_PERMISSION;
370     }
371     ElementName* element = ElementName::Unmarshalling(data);
372     if (element == nullptr) {
373         ErrorLog("HandleRegForegroundDispatch, unmarshalled element is null");
374         return KITS::ERR_NFC_PARAMETERS;
375     }
376     std::vector<uint32_t> discTech;
377     data.ReadUInt32Vector(&discTech);
378     int ret = KITS::ERR_NFC_PARAMETERS;
379     do {
380         sptr<IRemoteObject> remote = data.ReadRemoteObject();
381         if (remote == nullptr) {
382             DebugLog("Failed to readRemoteObject!");
383             break;
384         }
385         std::unique_ptr<ForegroundDeathRecipient> recipient
386             = std::make_unique<ForegroundDeathRecipient>(this, IPCSkeleton::GetCallingTokenID());
387         sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
388         if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(dr))) {
389             ErrorLog("Failed to add death recipient");
390             break;
391         }
392         {
393             std::lock_guard<std::mutex> guard(mutex_);
394             foregroundDeathRecipient_ = dr;
395             foregroundCallback_ = iface_cast<KITS::IForegroundCallback>(remote);
396             if (foregroundCallback_ == nullptr) {
397                 foregroundCallback_ = new (std::nothrow) ForegroundCallbackProxy(remote);
398                 DebugLog("create new `ForegroundCallbackProxy`!");
399             }
400             ret = RegForegroundDispatch(*(element), discTech, foregroundCallback_);
401         }
402     } while (0);
403     reply.WriteInt32(ret);
404 
405     // element is newed by Unmarshalling, should be deleted
406     delete element;
407     element = nullptr;
408     return ERR_NONE;
409 }
410 
HandleUnregForegroundDispatch(MessageParcel & data,MessageParcel & reply)411 int TagSessionStub::HandleUnregForegroundDispatch(MessageParcel &data, MessageParcel &reply)
412 {
413     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
414         ErrorLog("HandleUnregForegroundDispatch, ERR_NO_PERMISSION");
415         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
416         return KITS::ErrorCode::ERR_NO_PERMISSION;
417     }
418     InfoLog("HandleUnregForegroundDispatch");
419     ElementName* element = ElementName::Unmarshalling(data);
420     if (element == nullptr) {
421         ErrorLog("HandleUnregForegroundDispatch, unmarshalled element is null");
422         return KITS::ERR_NFC_PARAMETERS;
423     }
424     int exception = data.ReadInt32();
425     if (exception) {
426         // element is newed by Unmarshalling, should be deleted
427         delete element;
428         element = nullptr;
429         return KITS::ERR_NFC_PARAMETERS;
430     }
431     int ret = UnregForegroundDispatch(*(element));
432     DebugLog("HandleUnregForegroundDispatch end##ret=%{public}d\n", ret);
433     reply.WriteInt32(ret);
434 
435     // element is newed by Unmarshalling, should be deleted
436     delete element;
437     element = nullptr;
438     return ERR_NONE;
439 }
440 
RemoveReaderModeDeathRcpt(const wptr<IRemoteObject> & remote)441 void TagSessionStub::RemoveReaderModeDeathRcpt(const wptr<IRemoteObject> &remote)
442 {
443     std::lock_guard<std::mutex> guard(mutex_);
444     if (readerModeCallback_ == nullptr) {
445         ErrorLog("OnRemoteDied callback_ is nullptr");
446         return;
447     }
448     auto serviceRemote = readerModeCallback_->AsObject();
449     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
450         serviceRemote->RemoveDeathRecipient(readerModeDeathRecipient_);
451         readerModeCallback_ = nullptr;
452         ErrorLog("on remote died");
453     }
454 }
455 
HandleRegReaderMode(MessageParcel & data,MessageParcel & reply)456 int TagSessionStub::HandleRegReaderMode(MessageParcel &data, MessageParcel &reply)
457 {
458     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
459         ErrorLog("HandleRegReaderMode, ERR_NO_PERMISSION");
460         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
461         return KITS::ErrorCode::ERR_NO_PERMISSION;
462     }
463     ElementName* element = ElementName::Unmarshalling(data);
464     if (element == nullptr) {
465         ErrorLog("HandleRegReaderMode, unmarshalled element is null");
466         return KITS::ERR_NFC_PARAMETERS;
467     }
468     std::vector<uint32_t> discTech;
469     data.ReadUInt32Vector(&discTech);
470     int ret = KITS::ERR_NFC_PARAMETERS;
471     do {
472         sptr<IRemoteObject> remote = data.ReadRemoteObject();
473         if (remote == nullptr) {
474             DebugLog("Failed to readRemoteObject!");
475             break;
476         }
477         std::unique_ptr<ReaderModeDeathRecipient> recipient
478             = std::make_unique<ReaderModeDeathRecipient>(this, IPCSkeleton::GetCallingTokenID());
479         sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
480         if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(dr))) {
481             ErrorLog("Failed to add death recipient");
482             break;
483         }
484         {
485             std::lock_guard<std::mutex> guard(mutex_);
486             readerModeDeathRecipient_ = dr;
487             readerModeCallback_ = iface_cast<KITS::IReaderModeCallback>(remote);
488             if (readerModeCallback_ == nullptr) {
489                 readerModeCallback_ = new (std::nothrow) ReaderModeCallbackProxy(remote);
490                 DebugLog("create new `ReaderModeCallbackProxy`!");
491             }
492             ret = RegReaderMode(*(element), discTech, readerModeCallback_);
493         }
494     } while (0);
495     reply.WriteInt32(ret);
496 
497     // element is newed by Unmarshalling, should be deleted
498     delete element;
499     element = nullptr;
500     return ERR_NONE;
501 }
502 
HandleUnregReaderMode(MessageParcel & data,MessageParcel & reply)503 int TagSessionStub::HandleUnregReaderMode(MessageParcel &data, MessageParcel &reply)
504 {
505     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
506         ErrorLog("HandleUnregReaderMode, ERR_NO_PERMISSION");
507         reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
508         return KITS::ErrorCode::ERR_NO_PERMISSION;
509     }
510     InfoLog("HandleUnregReaderMode");
511     ElementName* element = ElementName::Unmarshalling(data);
512     if (element == nullptr) {
513         ErrorLog("HandleUnregReaderMode, unmarshalled element is null");
514         return KITS::ERR_NFC_PARAMETERS;
515     }
516     int exception = data.ReadInt32();
517     if (exception) {
518         // element is newed by Unmarshalling, should be deleted
519         delete element;
520         element = nullptr;
521         return KITS::ERR_NFC_PARAMETERS;
522     }
523     int ret = UnregReaderMode(*(element));
524     DebugLog("HandleUnregReaderMode end##ret=%{public}d\n", ret);
525     reply.WriteInt32(ret);
526 
527     // element is newed by Unmarshalling, should be deleted
528     delete element;
529     element = nullptr;
530     return ERR_NONE;
531 }
532 }  // namespace TAG
533 }  // namespace NFC
534 }  // namespace OHOS
535