• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
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 errCode = ERR_NONE;
60     int error = SendRequestExpectReplyInt(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_ON),
61                                           data, option, errCode);
62     if (error != ERR_NONE) {
63         ErrorLog("RegHceCmdCallback send request failed, ipc error code is %{public}d", error);
64         return KITS::ERR_HCE_PARAMETERS;
65     }
66     if (errCode != ERR_NONE) {
67         ErrorLog("RegHceCmdCallback failed, errCode is %{public}d", errCode);
68         return KITS::ERR_HCE_PARAMETERS;
69     }
70     return KITS::ERR_NONE;
71 }
72 
UnregHceCmdCallback(const sptr<KITS::IHceCmdCallback> & callback,const std::string & type)73 KITS::ErrorCode HceSessionProxy::UnregHceCmdCallback(
74     const sptr<KITS::IHceCmdCallback> &callback, const std::string &type)
75 {
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option(MessageOption::TF_SYNC);
79     if (g_hceCmdCallbackStub == nullptr) {
80         ErrorLog("%{public}s:g_hceCmdCallbackStub is nullptr", __func__);
81         return KITS::ERR_HCE_PARAMETERS;
82     }
83     g_hceCmdCallbackStub->UnRegHceCmdCallback(callback);
84     if (!data.WriteInterfaceToken(GetDescriptor())) {
85         ErrorLog("Write interface token error");
86         return KITS::ERR_HCE_PARAMETERS;
87     }
88     if (!data.WriteString(type)) {
89         ErrorLog("Write type error");
90         return KITS::ERR_HCE_PARAMETERS;
91     }
92     data.WriteInt32(0);
93     if (!data.WriteRemoteObject(g_hceCmdCallbackStub->AsObject())) {
94         ErrorLog("UnregHceCmdCallback WriteRemoteObject failed!");
95         return KITS::ERR_HCE_PARAMETERS;
96     }
97 
98     int errCode = ERR_NONE;
99     int error =
100         SendRequestExpectReplyInt(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_OFF), data, option,
101                                   errCode);
102     if (error != ERR_NONE) {
103         ErrorLog("UnregHceCmdCallback send request failed, ipc error code is %{public}d", error);
104         return KITS::ERR_HCE_PARAMETERS;
105     }
106     if (errCode != ERR_NONE) {
107         ErrorLog("UnregHceCmdCallback failed, errCode is %{public}d", errCode);
108         return KITS::ERR_HCE_PARAMETERS;
109     }
110     return KITS::ERR_NONE;
111 }
112 
SendRawFrame(std::string hexCmdData,bool raw,std::string & hexRespData)113 int HceSessionProxy::SendRawFrame(std::string hexCmdData, bool raw, std::string &hexRespData)
114 {
115     MessageParcel data;
116     MessageParcel reply;
117     MessageOption option(MessageOption::TF_SYNC);
118     if (!data.WriteInterfaceToken(GetDescriptor())) {
119         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
120     }
121 
122     if (hexCmdData.size() > KITS::MAX_APDU_DATA_HEX_STR) {
123         ErrorLog("raw frame too long");
124         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
125     }
126     data.WriteString(hexCmdData);
127     data.WriteBool(raw);
128     int statusCode = Remote()->SendRequest(
129         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_TRANSMIT), data, reply, option);
130     if (statusCode == ERR_NONE) {
131         hexRespData = reply.ReadString();
132     }
133     return statusCode;
134 }
GetPaymentServices(std::vector<AbilityInfo> & abilityInfos)135 int HceSessionProxy::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
136 {
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option(MessageOption::TF_SYNC);
140     if (!data.WriteInterfaceToken(GetDescriptor())) {
141         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
142     }
143     data.WriteInt32(0);
144 
145     int statusCode = Remote()->SendRequest(
146         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_GET_PAYMENT_SERVICES), data, reply,
147         option);
148     if (statusCode != ERR_NONE) {
149         ErrorLog("GetPaymentServices failed, error code is %{public}d", statusCode);
150         return statusCode;
151     }
152 
153     std::shared_ptr<KITS::CePaymentServicesParcelable> paymentServices(
154         reply.ReadParcelable<KITS::CePaymentServicesParcelable>());
155     if (paymentServices == nullptr) {
156         ErrorLog("paymentServices read failed.");
157         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
158     }
159     std::vector<AbilityInfo> paymentAbilityInfos = paymentServices->paymentAbilityInfos;
160     DebugLog("GetPaymentServices size %{public}zu", paymentAbilityInfos.size());
161     abilityInfos = std::move(paymentAbilityInfos);
162     return statusCode;
163 }
StopHce(ElementName & element)164 KITS::ErrorCode HceSessionProxy::StopHce(ElementName &element)
165 {
166     MessageParcel data;
167     MessageParcel reply;
168     MessageOption option(MessageOption::TF_SYNC);
169     if (g_hceCmdCallbackStub == nullptr) {
170         ErrorLog("%{public}s:g_hceCmdCallbackStub is nullptr", __func__);
171         return KITS::ERR_HCE_PARAMETERS;
172     }
173     g_hceCmdCallbackStub->UnRegHceCmdCallback(nullptr);
174     if (!data.WriteInterfaceToken(GetDescriptor())) {
175         ErrorLog("Write interface token error");
176         return KITS::ERR_HCE_PARAMETERS;
177     }
178     if (!element.Marshalling(data)) {
179         ErrorLog("Write element error");
180         return KITS::ERR_HCE_PARAMETERS;
181     }
182 
183     data.WriteInt32(0);
184     int error = SendRequestExpectReplyNone(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_STOP),
185                                            data, option);
186     if (error != ERR_NONE) {
187         ErrorLog("StopHce failed, error code is %{public}d", error);
188         return KITS::ERR_HCE_PARAMETERS;
189     }
190     return KITS::ERR_NONE;
191 }
IsDefaultService(ElementName & element,const std::string & type,bool & isDefaultService)192 KITS::ErrorCode HceSessionProxy::IsDefaultService(ElementName &element, const std::string &type,
193                                                   bool &isDefaultService)
194 {
195     MessageParcel data;
196     MessageParcel reply;
197     MessageOption option(MessageOption::TF_SYNC);
198     if (!data.WriteInterfaceToken(GetDescriptor())) {
199         ErrorLog("Write interface token error");
200         return KITS::ERR_HCE_PARAMETERS;
201     }
202     if (!element.Marshalling(data)) {
203         ErrorLog("Write element error");
204         return KITS::ERR_HCE_PARAMETERS;
205     }
206 
207     if (!data.WriteString(type)) {
208         ErrorLog("Write type error");
209         return KITS::ERR_HCE_PARAMETERS;
210     }
211     data.WriteInt32(0);
212     int error = SendRequestExpectReplyBool(
213         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_IS_DEFAULT_SERVICE), data, option,
214         isDefaultService);
215     if (error != ERR_NONE) {
216         ErrorLog("IsDefaultService failed, error code is %{public}d", error);
217         return KITS::ERR_HCE_PARAMETERS;
218     }
219     return KITS::ERR_NONE;
220 }
221 
StartHce(const ElementName & element,const std::vector<std::string> & aids)222 KITS::ErrorCode HceSessionProxy::StartHce(const ElementName &element, const std::vector<std::string> &aids)
223 {
224     MessageParcel data;
225     MessageParcel reply;
226     MessageOption option(MessageOption::TF_SYNC);
227     if (!data.WriteInterfaceToken(GetDescriptor())) {
228         ErrorLog("Write interface token error");
229         return KITS::ERR_HCE_PARAMETERS;
230     }
231 
232     KITS::StartHceInfoParcelable startHceInfo;
233     startHceInfo.SetAids(aids);
234     startHceInfo.SetElement(element);
235     if (!startHceInfo.Marshalling(data)) {
236         ErrorLog("Write start info error");
237         return KITS::ERR_HCE_PARAMETERS;
238     }
239 
240     data.WriteInt32(0);
241     int error = SendRequestExpectReplyNone(
242         static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_START), data, option);
243     if (error != ERR_NONE) {
244         ErrorLog("StartHce failed, error code is %{public}d", error);
245         return KITS::ERR_HCE_PARAMETERS;
246     }
247     return KITS::ERR_NONE;
248 }
249 } // namespace HCE
250 } // namespace NFC
251 } // namespace OHOS
252