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 "on_card_emulation_notify_cb_stub.h"
17
18 #include "nfc_service_ipc_interface_code.h"
19 #include "loghelper.h"
20
21 namespace OHOS {
22 namespace NFC {
OnCardEmulationNotifyCbStub()23 OnCardEmulationNotifyCbStub::OnCardEmulationNotifyCbStub() : callback_(nullptr), isRemoteDied_(false)
24 {}
25
~OnCardEmulationNotifyCbStub()26 OnCardEmulationNotifyCbStub::~OnCardEmulationNotifyCbStub()
27 {}
28
GetInstance()29 OnCardEmulationNotifyCbStub& OnCardEmulationNotifyCbStub::GetInstance()
30 {
31 static OnCardEmulationNotifyCbStub instance;
32 return instance;
33 }
34
OnCardEmulationNotify(uint32_t eventType,std::string apduData)35 bool OnCardEmulationNotifyCbStub::OnCardEmulationNotify(uint32_t eventType, std::string apduData)
36 {
37 if (callback_) {
38 InfoLog("OnCardEmulationNotify:call callback_");
39 return callback_(eventType, apduData);
40 }
41 return false;
42 }
43
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int OnCardEmulationNotifyCbStub::OnRemoteRequest(
45 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
46 {
47 InfoLog("OnRemoteRequest: code = %{public}d", code);
48 if (isRemoteDied_) {
49 ErrorLog("remote service is died.");
50 return KITS::ERR_NFC_STATE_UNBIND;
51 }
52 if (data.ReadInterfaceToken() != GetDescriptor()) {
53 ErrorLog("OnRemoteRequest: token verification error.");
54 return KITS::ERR_NFC_PARAMETERS;
55 }
56 int exception = data.ReadInt32();
57 if (exception) {
58 ErrorLog("OnRemoteRequest:got exception: (%{public}d).", exception);
59 return exception;
60 }
61
62 int ret = KITS::ERR_NFC_STATE_UNBIND;
63 switch (code) {
64 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_CARD_EMULATION_NOTIFY): {
65 ret = RemoteCardEmulationNotify(data, reply);
66 break;
67 }
68
69 default: {
70 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71 break;
72 }
73 }
74 return ret;
75 }
76
RegisterCallback(const OnCardEmulationNotifyCb callback)77 KITS::ErrorCode OnCardEmulationNotifyCbStub::RegisterCallback(const OnCardEmulationNotifyCb callback)
78 {
79 if (callback_ != nullptr) {
80 InfoLog("RegisterCallback::callback_ has registered!");
81 return KITS::ERR_NFC_PARAMETERS;
82 }
83 std::unique_lock<std::shared_mutex> guard(mutex_);
84 if (callback == nullptr) {
85 InfoLog("RegisterCallback::callback is nullptr!");
86 callback_ = callback;
87 return KITS::ERR_NFC_PARAMETERS;
88 }
89 callback_ = callback;
90 return KITS::ERR_NONE;
91 }
92
RemoteCardEmulationNotify(MessageParcel & data,MessageParcel & reply)93 int OnCardEmulationNotifyCbStub::RemoteCardEmulationNotify(MessageParcel &data, MessageParcel &reply)
94 {
95 std::unique_lock<std::shared_mutex> guard(mutex_);
96 uint32_t eventType = static_cast<uint32_t>(data.ReadInt32());
97 std::string apduData = data.ReadString();
98 bool ret = OnCardEmulationNotify(eventType, apduData);
99 reply.WriteBool(ret);
100 return KITS::ERR_NONE;
101 }
102 } // namespace NFC
103 } // namespace OHOS