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 "hce_cmd_callback_stub.h"
17
18 #include "nfc_sdk_common.h"
19 #include "nfc_service_ipc_interface_code.h"
20 #include "loghelper.h"
21
22 namespace OHOS {
23 namespace NFC {
24 namespace HCE {
HceCmdCallbackStub()25 HceCmdCallbackStub::HceCmdCallbackStub() : callback_(nullptr), mRemoteDied(false)
26 {
27 }
28
~HceCmdCallbackStub()29 HceCmdCallbackStub::~HceCmdCallbackStub()
30 {
31 }
32
GetInstance()33 HceCmdCallbackStub &HceCmdCallbackStub::GetInstance()
34 {
35 static HceCmdCallbackStub hceCmdCallbackStub;
36 return hceCmdCallbackStub;
37 }
38
RegHceCmdCallback(const sptr<IHceCmdCallback> & callback)39 KITS::ErrorCode HceCmdCallbackStub::RegHceCmdCallback(const sptr<IHceCmdCallback> &callback)
40 {
41 DebugLog("HceCmdCallbackStub RegisterCallBack");
42 std::unique_lock<std::shared_mutex> guard(callbackMutex);
43 if (callback == nullptr) {
44 ErrorLog("HceCmdCallbackStub:callBack is nullptr!");
45 callback_ = callback;
46 return KITS::ERR_NFC_PARAMETERS;
47 }
48 callback_ = callback;
49 return KITS::ERR_NONE;
50 }
51
UnRegHceCmdCallback(const sptr<IHceCmdCallback> & callback)52 KITS::ErrorCode HceCmdCallbackStub::UnRegHceCmdCallback(const sptr<IHceCmdCallback> &callback)
53 {
54 DebugLog("HceCmdCallbackStub UnRegisterCallBack");
55 if (callback_ == nullptr) {
56 InfoLog("HceCmdCallbackStub:Callback_ has unregistered!");
57 return KITS::ERR_NFC_PARAMETERS;
58 }
59 std::unique_lock<std::shared_mutex> guard(callbackMutex);
60 callback_ = nullptr;
61 return KITS::ERR_NONE;
62 }
63
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)64 int HceCmdCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
65 {
66 DebugLog("HceCmdCallbackStub::OnRemoteRequest,code = %{public}d", code);
67 if (mRemoteDied) {
68 return KITS::ERR_NFC_STATE_UNBIND;
69 }
70 if (data.ReadInterfaceToken() != GetDescriptor()) {
71 ErrorLog("nfc callback stub token verification error");
72 return KITS::ERR_NFC_PARAMETERS;
73 }
74 int exception = data.ReadInt32();
75 if (exception) {
76 ErrorLog("HceCmdCallbackStub::OnRemoteRequest, got exception: (%{public}d))", exception);
77 return exception;
78 }
79 int ret = KITS::ERR_NFC_STATE_UNBIND;
80 switch (code) {
81 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_CE_APDU_DATA): {
82 ret = RemoteOnCeApduData(data, reply);
83 break;
84 }
85 default: {
86 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
87 break;
88 }
89 }
90 return ret;
91 }
RemoteOnCeApduData(MessageParcel & data,MessageParcel & reply)92 int HceCmdCallbackStub::RemoteOnCeApduData(MessageParcel &data, MessageParcel &reply)
93 {
94 InfoLog("run %{public}zu datasize ", data.GetRawDataSize());
95 std::vector<uint8_t> apduData;
96 data.ReadUInt8Vector(&apduData);
97 std::unique_lock<std::shared_mutex> guard(callbackMutex);
98 OnCeApduData(apduData);
99 reply.WriteInt32(KITS::ERR_NONE); /* Reply 0 to indicate that no exception occurs. */
100 return KITS::ERR_NONE;
101 }
OnCeApduData(const std::vector<uint8_t> & data)102 void HceCmdCallbackStub::OnCeApduData(const std::vector<uint8_t> &data)
103 {
104 if (callback_) {
105 DebugLog("HceCmdCallbackStub callback_");
106 callback_->OnCeApduData(data);
107 }
108 }
109 }
110 }
111 }
112