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,const std::string & type)39 KITS::ErrorCode HceCmdCallbackStub::RegHceCmdCallback(const sptr<IHceCmdCallback> &callback, const std::string &type)
40 {
41     DebugLog("HceCmdCallbackStub RegisterCallBack");
42     if (callback_ != nullptr) {
43         ErrorLog("HceCmdCallbackStub:Callback_ has registered!");
44         return KITS::ERR_NFC_PARAMETERS;
45     }
46     std::unique_lock<std::shared_mutex> guard(callbackMutex);
47     if (callback == nullptr) {
48         ErrorLog("HceCmdCallbackStub:callBack is nullptr!");
49         callback_ = callback;
50         return KITS::ERR_NFC_PARAMETERS;
51     }
52     callback_ = callback;
53     return KITS::ERR_NONE;
54 }
55 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)56 int HceCmdCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
57 {
58     DebugLog("HceCmdCallbackStub::OnRemoteRequest,code = %{public}d", code);
59     if (mRemoteDied) {
60         return KITS::ERR_NFC_STATE_UNBIND;
61     }
62     if (data.ReadInterfaceToken() != GetDescriptor()) {
63         ErrorLog("nfc callback stub token verification error");
64         return KITS::ERR_NFC_PARAMETERS;
65     }
66     int exception = data.ReadInt32();
67     if (exception) {
68         ErrorLog("HceCmdCallbackStub::OnRemoteRequest, got exception: (%{public}d))", exception);
69         return exception;
70     }
71     int ret = KITS::ERR_NFC_STATE_UNBIND;
72     switch (code) {
73         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_CE_APDU_DATA): {
74             ret = RemoteOnCeApduData(data, reply);
75             break;
76         }
77         default: {
78             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79             break;
80         }
81     }
82     return ret;
83 }
RemoteOnCeApduData(MessageParcel & data,MessageParcel & reply)84 int HceCmdCallbackStub::RemoteOnCeApduData(MessageParcel &data, MessageParcel &reply)
85 {
86     InfoLog("run %{public}zu datasize ", data.GetRawDataSize());
87     std::vector<uint8_t> apduData;
88     data.ReadUInt8Vector(&apduData);
89     std::unique_lock<std::shared_mutex> guard(callbackMutex);
90     OnCeApduData(apduData);
91     reply.WriteInt32(KITS::ERR_NONE); /* Reply 0 to indicate that no exception occurs. */
92     return KITS::ERR_NONE;
93 }
OnCeApduData(const std::vector<uint8_t> & data)94 void HceCmdCallbackStub::OnCeApduData(const std::vector<uint8_t> &data)
95 {
96     if (callback_) {
97         DebugLog("HceCmdCallbackStub callback_");
98         callback_->OnCeApduData(data);
99     }
100 }
101 }
102 }
103 }
104