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 #include "hce_session_proxy.h"
16 
17 #include "element_name.h"
18 #include "hce_cmd_callback_stub.h"
19 #include "loghelper.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22 #include "nfc_sdk_common.h"
23 #include "nfc_service_ipc_interface_code.h"
24 #include "ce_payment_services_parcelable.h"
25 #include "start_hce_info_parcelable.h"
26 
27 namespace OHOS {
28 namespace NFC {
29 namespace HCE {
30 using OHOS::AppExecFwk::ElementName;
31 static sptr<HceCmdCallbackStub> g_hceCmdCallbackStub =
32     sptr<HceCmdCallbackStub>(new (std::nothrow) HceCmdCallbackStub);
33 
RegHceCmdCallback(const sptr<KITS::IHceCmdCallback> & callback,const std::string & type)34 KITS::ErrorCode HceSessionProxy::RegHceCmdCallback(const sptr<KITS::IHceCmdCallback> &callback,
35                                                    const std::string &type)
36 {
37     MessageParcel data;
38     MessageParcel reply;
39     MessageOption option(MessageOption::TF_SYNC);
40     if (g_hceCmdCallbackStub == nullptr) {
41         ErrorLog("%{public}s:g_hceCmdCallbackStub is nullptr", __func__);
42         return KITS::ERR_HCE_PARAMETERS;
43     }
44     g_hceCmdCallbackStub->RegHceCmdCallback(callback, type);
45     if (!data.WriteInterfaceToken(GetDescriptor())) {
46         ErrorLog("Write interface token error");
47         return KITS::ERR_HCE_PARAMETERS;
48     }
49     if (!data.WriteString(type)) {
50         ErrorLog("Write type error");
51         return KITS::ERR_HCE_PARAMETERS;
52     }
53     data.WriteInt32(0);
54     if (!data.WriteRemoteObject(g_hceCmdCallbackStub->AsObject())) {
55         ErrorLog("RegHceCmdCallback WriteRemoteObject failed!");
56         return KITS::ERR_HCE_PARAMETERS;
57     }
58 
59     int error = SendRequestExpectReplyNone(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_ON),
60                                            data, option);
61     if (error != ERR_NONE) {
62         ErrorLog("RegHceCmdCallback failed, error code is %{public}d", error);
63         return KITS::ERR_HCE_PARAMETERS;
64     }
65     return KITS::ERR_NONE;
66 }
67 
SendRawFrame(std::string hexCmdData,bool raw,std::string & hexRespData)68 int HceSessionProxy::SendRawFrame(std::string hexCmdData, bool raw, std::string &hexRespData)
69 {
70     MessageParcel data;
71     MessageParcel reply;
72     MessageOption option(MessageOption::TF_SYNC);
73     if (!data.WriteInterfaceToken(GetDescriptor())) {
74         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
75     }
76 
77     if (hexCmdData.size() > KITS::MAX_APDU_DATA_HEX_STR) {
78         ErrorLog("raw frame too long");
79         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
80     }
81     data.WriteString(hexCmdData);
82     data.WriteBool(raw);
83     int statusCode = Remote()->SendRequest(
84         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_TRANSMIT), data, reply, option);
85     if (statusCode == ERR_NONE) {
86         hexRespData = reply.ReadString();
87     }
88     return statusCode;
89 }
GetPaymentServices(std::vector<AbilityInfo> & abilityInfos)90 int HceSessionProxy::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
91 {
92     MessageParcel data;
93     MessageParcel reply;
94     MessageOption option(MessageOption::TF_SYNC);
95     if (!data.WriteInterfaceToken(GetDescriptor())) {
96         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
97     }
98     data.WriteInt32(0);
99 
100     int statusCode = Remote()->SendRequest(
101         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_GET_PAYMENT_SERVICES), data, reply,
102         option);
103     if (statusCode != ERR_NONE) {
104         ErrorLog("GetPaymentServices failed, error code is %{public}d", statusCode);
105         return statusCode;
106     }
107 
108     std::shared_ptr<KITS::CePaymentServicesParcelable> paymentServices(
109         reply.ReadParcelable<KITS::CePaymentServicesParcelable>());
110     if (paymentServices == nullptr) {
111         ErrorLog("paymentServices read failed.");
112         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
113     }
114     std::vector<AbilityInfo> paymentAbilityInfos = paymentServices->paymentAbilityInfos;
115     DebugLog("GetPaymentServices size %{public}zu", paymentAbilityInfos.size());
116     abilityInfos = std::move(paymentAbilityInfos);
117     return statusCode;
118 }
StopHce(ElementName & element)119 KITS::ErrorCode HceSessionProxy::StopHce(ElementName &element)
120 {
121     MessageParcel data;
122     MessageParcel reply;
123     MessageOption option(MessageOption::TF_SYNC);
124     if (!data.WriteInterfaceToken(GetDescriptor())) {
125         ErrorLog("Write interface token error");
126         return KITS::ERR_HCE_PARAMETERS;
127     }
128     if (!element.Marshalling(data)) {
129         ErrorLog("Write element error");
130         return KITS::ERR_HCE_PARAMETERS;
131     }
132 
133     data.WriteInt32(0);
134     int error = SendRequestExpectReplyNone(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_STOP),
135                                            data, option);
136     if (error != ERR_NONE) {
137         ErrorLog("StopHce failed, error code is %{public}d", error);
138         return KITS::ERR_HCE_PARAMETERS;
139     }
140     return KITS::ERR_NONE;
141 }
IsDefaultService(ElementName & element,const std::string & type,bool & isDefaultService)142 KITS::ErrorCode HceSessionProxy::IsDefaultService(ElementName &element, const std::string &type,
143                                                   bool &isDefaultService)
144 {
145     MessageParcel data;
146     MessageParcel reply;
147     MessageOption option(MessageOption::TF_SYNC);
148     if (!data.WriteInterfaceToken(GetDescriptor())) {
149         ErrorLog("Write interface token error");
150         return KITS::ERR_HCE_PARAMETERS;
151     }
152     if (!element.Marshalling(data)) {
153         ErrorLog("Write element error");
154         return KITS::ERR_HCE_PARAMETERS;
155     }
156 
157     if (!data.WriteString(type)) {
158         ErrorLog("Write type error");
159         return KITS::ERR_HCE_PARAMETERS;
160     }
161     data.WriteInt32(0);
162     int error = SendRequestExpectReplyBool(
163         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_IS_DEFAULT_SERVICE), data, option,
164         isDefaultService);
165     if (error != ERR_NONE) {
166         ErrorLog("IsDefaultService failed, error code is %{public}d", error);
167         return KITS::ERR_HCE_PARAMETERS;
168     }
169     return KITS::ERR_NONE;
170 }
171 
StartHce(const ElementName & element,const std::vector<std::string> & aids)172 KITS::ErrorCode HceSessionProxy::StartHce(const ElementName &element, const std::vector<std::string> &aids)
173 {
174     MessageParcel data;
175     MessageParcel reply;
176     MessageOption option(MessageOption::TF_SYNC);
177     if (!data.WriteInterfaceToken(GetDescriptor())) {
178         ErrorLog("Write interface token error");
179         return KITS::ERR_HCE_PARAMETERS;
180     }
181 
182     KITS::StartHceInfoParcelable startHceInfo;
183     startHceInfo.SetAids(aids);
184     startHceInfo.SetElement(element);
185     if (!startHceInfo.Marshalling(data)) {
186         ErrorLog("Write start info error");
187         return KITS::ERR_HCE_PARAMETERS;
188     }
189 
190     data.WriteInt32(0);
191     int error = SendRequestExpectReplyNone(
192         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_START), data, option);
193     if (error != ERR_NONE) {
194         ErrorLog("StartHce failed, error code is %{public}d", error);
195         return KITS::ERR_HCE_PARAMETERS;
196     }
197     return KITS::ERR_NONE;
198 }
199 } // namespace HCE
200 } // namespace NFC
201 } // namespace OHOS
202