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_service.h"
17
18 #include "ce_payment_services_parcelable.h"
19 #include "loghelper.h"
20 #include "nfc_sdk_common.h"
21 #include "ihce_session.h"
22 #include "hce_cmd_callback_stub.h"
23 #include "nfc_controller.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 #include "hce_session_proxy.h"
27
28 namespace OHOS {
29 namespace NFC {
30 namespace KITS {
31 std::mutex g_hceSessionProxyLock;
32 static sptr<HCE::HceCmdCallbackStub> g_hceCmdCallbackStub =
33 sptr<HCE::HceCmdCallbackStub>(new HCE::HceCmdCallbackStub);
34
HceService()35 HceService::HceService()
36 {
37 DebugLog("[HceService] new HceService");
38 }
39
~HceService()40 HceService::~HceService()
41 {
42 DebugLog("destruct HceService");
43 }
44
GetInstance()45 HceService &HceService::GetInstance()
46 {
47 static HceService instance;
48 return instance;
49 }
50
RegHceCmdCallback(const sptr<IHceCmdCallback> & callback,const std::string & type)51 ErrorCode HceService::RegHceCmdCallback(const sptr<IHceCmdCallback> &callback, const std::string &type)
52 {
53 InfoLog("HceService::RegHceCmdCallback");
54 int32_t res = ErrorCode::ERR_NONE;
55 OHOS::sptr<IHceSession> hceSession = GetHceSessionProxy(res);
56 if (res == ErrorCode::ERR_NO_PERMISSION) {
57 ErrorLog("HceService::RegHceCmdCallback, ERR_NO_PERMISSION");
58 return ErrorCode::ERR_NO_PERMISSION;
59 }
60 if (hceSession == nullptr || hceSession->AsObject() == nullptr) {
61 ErrorLog("HceService::RegHceCmdCallback, ERR_HCE_STATE_UNBIND");
62 return ErrorCode::ERR_HCE_STATE_UNBIND;
63 }
64 if (g_hceCmdCallbackStub == nullptr) {
65 ErrorLog("g_hceCmdCallbackStub is nullptr");
66 return KITS::ERR_HCE_PARAMETERS;
67 }
68 g_hceCmdCallbackStub->RegHceCmdCallback(callback);
69 hceSession->RegHceCmdCallback(g_hceCmdCallbackStub, type);
70 return ErrorCode::ERR_NONE;
71 }
72
UnRegHceCmdCallback(const sptr<IHceCmdCallback> & callback,const std::string & type)73 ErrorCode HceService::UnRegHceCmdCallback(const sptr<IHceCmdCallback> &callback, const std::string &type)
74 {
75 InfoLog("HceService::UnRegHceCmdCallback");
76 int32_t res = ErrorCode::ERR_NONE;
77 OHOS::sptr<IHceSession> hceSession = GetHceSessionProxy(res);
78 if (res == ErrorCode::ERR_NO_PERMISSION) {
79 ErrorLog("HceService::UnRegHceCmdCallback, ERR_NO_PERMISSION");
80 return ErrorCode::ERR_NO_PERMISSION;
81 }
82 if (hceSession == nullptr || hceSession->AsObject() == nullptr) {
83 ErrorLog("HceService::UnRegHceCmdCallback, ERR_HCE_STATE_UNBIND");
84 return ErrorCode::ERR_HCE_STATE_UNBIND;
85 }
86 if (g_hceCmdCallbackStub == nullptr) {
87 ErrorLog("g_hceCmdCallbackStub is nullptr");
88 return KITS::ERR_HCE_PARAMETERS;
89 }
90 g_hceCmdCallbackStub->UnRegHceCmdCallback(callback);
91 hceSession->UnregHceCmdCallback(g_hceCmdCallbackStub, type);
92 return ErrorCode::ERR_NONE;
93 }
94
StopHce(ElementName & element)95 ErrorCode HceService::StopHce(ElementName &element)
96 {
97 InfoLog("HceService::StopHce");
98 int32_t res = ErrorCode::ERR_NONE;
99 OHOS::sptr<IHceSession> hceSession = GetHceSessionProxy(res);
100 if (res == ErrorCode::ERR_NO_PERMISSION) {
101 ErrorLog("HceService::StopHce, ERR_NO_PERMISSION");
102 return ErrorCode::ERR_NO_PERMISSION;
103 }
104 if (hceSession == nullptr || hceSession->AsObject() == nullptr) {
105 ErrorLog("HceService::StopHce, ERR_HCE_STATE_UNBIND");
106 return ErrorCode::ERR_HCE_STATE_UNBIND;
107 }
108 ErrorCode ret = static_cast<ErrorCode>(hceSession->StopHce(element));
109 if (ret != ErrorCode::ERR_NO_PERMISSION) {
110 ret = ErrorCode::ERR_NONE;
111 }
112 return ret;
113 }
114
IsDefaultService(ElementName & element,const std::string & type,bool & isDefaultService)115 ErrorCode HceService::IsDefaultService(ElementName &element, const std::string &type, bool &isDefaultService)
116 {
117 InfoLog("HceService::IsDefaultService");
118 int32_t res = ErrorCode::ERR_NONE;
119 OHOS::sptr<IHceSession> hceSession = GetHceSessionProxy(res);
120 if (res == ErrorCode::ERR_NO_PERMISSION) {
121 ErrorLog("HceService::IsDefaultService, ERR_NO_PERMISSION");
122 return ErrorCode::ERR_NO_PERMISSION;
123 }
124 if (hceSession == nullptr || hceSession->AsObject() == nullptr) {
125 ErrorLog("HceService::IsDefaultService, ERR_HCE_STATE_UNBIND");
126 return ErrorCode::ERR_HCE_STATE_UNBIND;
127 }
128 return static_cast<ErrorCode>(hceSession->IsDefaultService(element, type, isDefaultService));
129 }
130
SendRawFrame(std::string hexCmdData,bool raw,std::string & hexRespData)131 int HceService::SendRawFrame(std::string hexCmdData, bool raw, std::string &hexRespData)
132 {
133 InfoLog("HceService::SendRawFrame");
134 int32_t res = ErrorCode::ERR_NONE;
135 OHOS::sptr<IHceSession> hceSession = GetHceSessionProxy(res);
136 if (res == ErrorCode::ERR_NO_PERMISSION) {
137 ErrorLog("HceService::SendRawFrame, ERR_NO_PERMISSION");
138 return ErrorCode::ERR_NO_PERMISSION;
139 }
140 if (hceSession == nullptr || hceSession->AsObject() == nullptr) {
141 ErrorLog("HceService::SendRawFrame, ERR_HCE_STATE_UNBIND");
142 return ErrorCode::ERR_HCE_STATE_UNBIND;
143 }
144 res = static_cast<int>(hceSession->SendRawFrame(hexCmdData, raw, hexRespData));
145 if (res != ErrorCode::ERR_NO_PERMISSION) {
146 res = ErrorCode::ERR_NONE;
147 }
148 return res;
149 }
150
GetPaymentServices(std::vector<AbilityInfo> & abilityInfos)151 int HceService::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
152 {
153 InfoLog("HceService::GetPaymentServices");
154 int32_t res = ErrorCode::ERR_NONE;
155 OHOS::sptr<IHceSession> hceSession = GetHceSessionProxy(res);
156 if (res == ErrorCode::ERR_NO_PERMISSION) {
157 ErrorLog("HceService::GetPaymentServices, ERR_NO_PERMISSION");
158 return ErrorCode::ERR_NO_PERMISSION;
159 }
160 if (hceSession == nullptr || hceSession->AsObject() == nullptr) {
161 ErrorLog("HceService::GetPaymentServices, ERR_HCE_STATE_UNBIND");
162 return ErrorCode::ERR_HCE_STATE_UNBIND;
163 }
164
165 KITS::CePaymentServicesParcelable paymentServices;
166 ErrCode errCode = hceSession->GetPaymentServices(paymentServices);
167
168 std::vector<AbilityInfo> paymentAbilityInfos = paymentServices.paymentAbilityInfos;
169 InfoLog("size %{public}zu", paymentAbilityInfos.size());
170 abilityInfos = std::move(paymentAbilityInfos);
171 return static_cast<int>(errCode);
172 }
173
StartHce(const ElementName & element,const std::vector<std::string> & aids)174 KITS::ErrorCode HceService::StartHce(const ElementName &element, const std::vector<std::string> &aids)
175 {
176 InfoLog("HceService::StartHce");
177 int32_t res = ErrorCode::ERR_NONE;
178 OHOS::sptr<IHceSession> hceSession = GetHceSessionProxy(res);
179 if (res == ErrorCode::ERR_NO_PERMISSION) {
180 ErrorLog("HceService::StartHce, ERR_NO_PERMISSION");
181 return ErrorCode::ERR_NO_PERMISSION;
182 }
183 if (hceSession == nullptr || hceSession->AsObject() == nullptr) {
184 ErrorLog("HceService::StartHce, ERR_HCE_STATE_UNBIND");
185 return ErrorCode::ERR_HCE_STATE_UNBIND;
186 }
187 ErrorCode ret = static_cast<ErrorCode>(hceSession->StartHce(element, aids));
188 if (ret != ErrorCode::ERR_NO_PERMISSION) {
189 ret = ErrorCode::ERR_NONE;
190 }
191 return ret;
192 }
193
GetHceSessionProxy(int32_t & res)194 OHOS::sptr<IHceSession> HceService::GetHceSessionProxy(int32_t &res)
195 {
196 std::lock_guard<std::mutex> lock(g_hceSessionProxyLock);
197 if (hceSessionProxy_ == nullptr) {
198 hceSessionProxy_ = iface_cast<IHceSession>(NfcController::GetInstance().GetHceServiceIface(res));
199 }
200 return hceSessionProxy_;
201 }
202
ClearHceSessionProxy()203 void HceService::ClearHceSessionProxy()
204 {
205 WarnLog("ClearHceSessionProxy");
206 std::lock_guard<std::mutex> lock(g_hceSessionProxyLock);
207 hceSessionProxy_ = nullptr;
208 }
209 } // namespace KITS
210 } // namespace NFC
211 } // namespace OHOS