1 /*
2 * Copyright (C) 2023 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
16 #include "ndef_msg_callback_stub.h"
17
18 #include "nfc_service_ipc_interface_code.h"
19 #include "loghelper.h"
20
21 namespace OHOS {
22 namespace NFC {
NdefMsgCallbackStub()23 NdefMsgCallbackStub::NdefMsgCallbackStub() : callback_(nullptr), isRemoteDied_(false)
24 {}
25
~NdefMsgCallbackStub()26 NdefMsgCallbackStub::~NdefMsgCallbackStub()
27 {}
28
GetInstance()29 NdefMsgCallbackStub& NdefMsgCallbackStub::GetInstance()
30 {
31 static NdefMsgCallbackStub instance;
32 return instance;
33 }
34
OnNdefMsgDiscovered(const std::string & tagUid,const std::string & ndef,int ndefMsgType)35 bool NdefMsgCallbackStub::OnNdefMsgDiscovered(const std::string &tagUid, const std::string &ndef, int ndefMsgType)
36 {
37 if (callback_) {
38 DebugLog("NdefMsgCallbackStub callback_");
39 return callback_->OnNdefMsgDiscovered(tagUid, ndef, ndefMsgType);
40 }
41 return false;
42 }
43
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int NdefMsgCallbackStub::OnRemoteRequest(
45 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
46 {
47 DebugLog("NdefMsgCallbackStub::OnRemoteRequest,code = %{public}d", code);
48 if (isRemoteDied_) {
49 return KITS::ERR_NFC_STATE_UNBIND;
50 }
51 if (data.ReadInterfaceToken() != GetDescriptor()) {
52 ErrorLog("NdefMsgCallbackStub: token verification error");
53 return KITS::ERR_NFC_PARAMETERS;
54 }
55 int exception = data.ReadInt32();
56 if (exception) {
57 ErrorLog("NdefMsgCallbackStub::OnRemoteRequest, got exception: (%{public}d))", exception);
58 return exception;
59 }
60 int ret = KITS::ERR_NFC_STATE_UNBIND;
61 switch (code) {
62 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_NDEF_MSG_NOTIFY): {
63 ret = RemoteNdefMsgDiscovered(data, reply);
64 break;
65 }
66 default: {
67 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68 break;
69 }
70 }
71 return ret;
72 }
73
RegisterCallback(const sptr<INdefMsgCallback> & callback)74 KITS::ErrorCode NdefMsgCallbackStub::RegisterCallback(const sptr<INdefMsgCallback> &callback)
75 {
76 DebugLog("NdefMsgCallbackStub RegisterCallBack");
77 std::unique_lock<std::shared_mutex> guard(mutex_);
78 if (callback == nullptr) {
79 ErrorLog("RegisterUserCallBack:callBack is nullptr!");
80 callback_ = callback;
81 return KITS::ERR_NFC_PARAMETERS;
82 }
83 callback_ = callback;
84 return KITS::ERR_NONE;
85 }
86
RemoteNdefMsgDiscovered(MessageParcel & data,MessageParcel & reply)87 int NdefMsgCallbackStub::RemoteNdefMsgDiscovered(MessageParcel &data, MessageParcel &reply)
88 {
89 InfoLog("NdefMsgCallbackStub::RemoteNdefMsgDiscovered");
90 std::string tagUid = data.ReadString();
91 std::string ndef = data.ReadString();
92 int type = data.ReadInt32();
93 std::unique_lock<std::shared_mutex> guard(mutex_);
94 bool res = OnNdefMsgDiscovered(tagUid, ndef, type);
95 reply.WriteBool(res); // Reply for ndef parse result
96 return KITS::ERR_NONE;
97 }
98 } // namespace NFC
99 } // namespace OHOS