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 "loghelper.h"
17 #include "nfc_sdk_common.h"
18 #include "hce_service.h"
19 #include "ihce_session.h"
20 #include "hce_cmd_callback_stub.h"
21 #include "nfc_controller.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "hce_session_proxy.h"
25 
26 namespace OHOS {
27 namespace NFC {
28 namespace KITS {
29 
HceService()30 HceService::HceService()
31 {
32     DebugLog("[HceService] new HceService");
33 }
34 
~HceService()35 HceService::~HceService()
36 {
37     DebugLog("destruct HceService");
38 }
39 
GetInstance()40 HceService &HceService::GetInstance()
41 {
42     static HceService instance;
43     return instance;
44 }
45 
RegHceCmdCallback(const sptr<IHceCmdCallback> & callback,const std::string & type)46 ErrorCode HceService::RegHceCmdCallback(const sptr<IHceCmdCallback> &callback, const std::string &type)
47 {
48     DebugLog("HceService::RegHceCmdCallback");
49     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
50     if (hceSession == nullptr) {
51         ErrorLog("HceService::RegHceCmdCallback, ERR_HCE_STATE_UNBIND");
52         return ErrorCode::ERR_HCE_STATE_UNBIND;
53     }
54     return hceSession->RegHceCmdCallback(callback, type);
55 }
StopHce(ElementName & element)56 ErrorCode HceService::StopHce(ElementName &element)
57 {
58     DebugLog("HceService::StopHce");
59     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
60     if (hceSession == nullptr) {
61         ErrorLog("HceService::StopHce, ERR_HCE_STATE_UNBIND");
62         return ErrorCode::ERR_HCE_STATE_UNBIND;
63     }
64     return hceSession->StopHce(element);
65 }
IsDefaultService(ElementName & element,const std::string & type,bool & isDefaultService)66 ErrorCode HceService::IsDefaultService(ElementName &element, const std::string &type, bool &isDefaultService)
67 {
68     DebugLog("HceService::IsDefaultService");
69     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
70     if (hceSession == nullptr) {
71         ErrorLog("HceService::IsDefaultService, ERR_HCE_STATE_UNBIND");
72         return ErrorCode::ERR_HCE_STATE_UNBIND;
73     }
74     return hceSession->IsDefaultService(element, type, isDefaultService);
75 }
SendRawFrame(std::string hexCmdData,bool raw,std::string & hexRespData)76 int HceService::SendRawFrame(std::string hexCmdData, bool raw, std::string &hexRespData)
77 {
78     DebugLog("HceService::SendRawFrame");
79     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
80     if (hceSession == nullptr) {
81         ErrorLog("HceService::SendRawFrame, ERR_HCE_STATE_UNBIND");
82         return ErrorCode::ERR_HCE_STATE_UNBIND;
83     }
84     return hceSession->SendRawFrame(hexCmdData, raw, hexRespData);
85 }
GetPaymentServices(std::vector<AbilityInfo> & abilityInfos)86 int HceService::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
87 {
88     DebugLog("HceService::GetPaymentServices");
89     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
90     if (hceSession == nullptr) {
91         ErrorLog("HceService::GetPaymentServices, ERR_HCE_STATE_UNBIND");
92         return ErrorCode::ERR_HCE_STATE_UNBIND;
93     }
94     return hceSession->GetPaymentServices(abilityInfos);
95 }
96 
StartHce(const ElementName & element,const std::vector<std::string> & aids)97 KITS::ErrorCode HceService::StartHce(const ElementName &element, const std::vector<std::string> &aids)
98 {
99     DebugLog("HceService::StartHce");
100     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
101     if (hceSession == nullptr) {
102         ErrorLog("HceService::StartHce, ERR_HCE_STATE_UNBIND");
103         return ErrorCode::ERR_HCE_STATE_UNBIND;
104     }
105     return hceSession->StartHce(element, aids);
106 }
GetHceSessionProxy()107 OHOS::sptr<HCE::IHceSession> HceService::GetHceSessionProxy()
108 {
109     if (hceSessionProxy_ == nullptr) {
110         OHOS::sptr<IRemoteObject> iface = NfcController::GetInstance().GetHceServiceIface();
111         if (iface != nullptr) {
112             hceSessionProxy_ = new HCE::HceSessionProxy(iface);
113         }
114     }
115     return hceSessionProxy_;
116 }
117 } // namespace KITS
118 } // namespace NFC
119 } // namespace OHOS