1bc2ed2b3Sopenharmony_ci/*
2bc2ed2b3Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3bc2ed2b3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc2ed2b3Sopenharmony_ci * you may not use this file except in compliance with the License.
5bc2ed2b3Sopenharmony_ci * You may obtain a copy of the License at
6bc2ed2b3Sopenharmony_ci *
7bc2ed2b3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc2ed2b3Sopenharmony_ci *
9bc2ed2b3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc2ed2b3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc2ed2b3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc2ed2b3Sopenharmony_ci * See the License for the specific language governing permissions and
13bc2ed2b3Sopenharmony_ci * limitations under the License.
14bc2ed2b3Sopenharmony_ci */
15bc2ed2b3Sopenharmony_ci
16bc2ed2b3Sopenharmony_ci#include "loghelper.h"
17bc2ed2b3Sopenharmony_ci#include "nfc_sdk_common.h"
18bc2ed2b3Sopenharmony_ci#include "hce_service.h"
19bc2ed2b3Sopenharmony_ci#include "ihce_session.h"
20bc2ed2b3Sopenharmony_ci#include "hce_cmd_callback_stub.h"
21bc2ed2b3Sopenharmony_ci#include "nfc_controller.h"
22bc2ed2b3Sopenharmony_ci#include "iservice_registry.h"
23bc2ed2b3Sopenharmony_ci#include "system_ability_definition.h"
24bc2ed2b3Sopenharmony_ci#include "hce_session_proxy.h"
25bc2ed2b3Sopenharmony_ci
26bc2ed2b3Sopenharmony_cinamespace OHOS {
27bc2ed2b3Sopenharmony_cinamespace NFC {
28bc2ed2b3Sopenharmony_cinamespace KITS {
29bc2ed2b3Sopenharmony_ci
30bc2ed2b3Sopenharmony_ciHceService::HceService()
31bc2ed2b3Sopenharmony_ci{
32bc2ed2b3Sopenharmony_ci    DebugLog("[HceService] new HceService");
33bc2ed2b3Sopenharmony_ci}
34bc2ed2b3Sopenharmony_ci
35bc2ed2b3Sopenharmony_ciHceService::~HceService()
36bc2ed2b3Sopenharmony_ci{
37bc2ed2b3Sopenharmony_ci    DebugLog("destruct HceService");
38bc2ed2b3Sopenharmony_ci}
39bc2ed2b3Sopenharmony_ci
40bc2ed2b3Sopenharmony_ciHceService &HceService::GetInstance()
41bc2ed2b3Sopenharmony_ci{
42bc2ed2b3Sopenharmony_ci    static HceService instance;
43bc2ed2b3Sopenharmony_ci    return instance;
44bc2ed2b3Sopenharmony_ci}
45bc2ed2b3Sopenharmony_ci
46bc2ed2b3Sopenharmony_ciErrorCode HceService::RegHceCmdCallback(const sptr<IHceCmdCallback> &callback, const std::string &type)
47bc2ed2b3Sopenharmony_ci{
48bc2ed2b3Sopenharmony_ci    DebugLog("HceService::RegHceCmdCallback");
49bc2ed2b3Sopenharmony_ci    OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
50bc2ed2b3Sopenharmony_ci    if (hceSession == nullptr) {
51bc2ed2b3Sopenharmony_ci        ErrorLog("HceService::RegHceCmdCallback, ERR_HCE_STATE_UNBIND");
52bc2ed2b3Sopenharmony_ci        return ErrorCode::ERR_HCE_STATE_UNBIND;
53bc2ed2b3Sopenharmony_ci    }
54bc2ed2b3Sopenharmony_ci    return hceSession->RegHceCmdCallback(callback, type);
55bc2ed2b3Sopenharmony_ci}
56bc2ed2b3Sopenharmony_ciErrorCode HceService::StopHce(ElementName &element)
57bc2ed2b3Sopenharmony_ci{
58bc2ed2b3Sopenharmony_ci    DebugLog("HceService::StopHce");
59bc2ed2b3Sopenharmony_ci    OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
60bc2ed2b3Sopenharmony_ci    if (hceSession == nullptr) {
61bc2ed2b3Sopenharmony_ci        ErrorLog("HceService::StopHce, ERR_HCE_STATE_UNBIND");
62bc2ed2b3Sopenharmony_ci        return ErrorCode::ERR_HCE_STATE_UNBIND;
63bc2ed2b3Sopenharmony_ci    }
64bc2ed2b3Sopenharmony_ci    return hceSession->StopHce(element);
65bc2ed2b3Sopenharmony_ci}
66bc2ed2b3Sopenharmony_ciErrorCode HceService::IsDefaultService(ElementName &element, const std::string &type, bool &isDefaultService)
67bc2ed2b3Sopenharmony_ci{
68bc2ed2b3Sopenharmony_ci    DebugLog("HceService::IsDefaultService");
69bc2ed2b3Sopenharmony_ci    OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
70bc2ed2b3Sopenharmony_ci    if (hceSession == nullptr) {
71bc2ed2b3Sopenharmony_ci        ErrorLog("HceService::IsDefaultService, ERR_HCE_STATE_UNBIND");
72bc2ed2b3Sopenharmony_ci        return ErrorCode::ERR_HCE_STATE_UNBIND;
73bc2ed2b3Sopenharmony_ci    }
74bc2ed2b3Sopenharmony_ci    return hceSession->IsDefaultService(element, type, isDefaultService);
75bc2ed2b3Sopenharmony_ci}
76bc2ed2b3Sopenharmony_ciint HceService::SendRawFrame(std::string hexCmdData, bool raw, std::string &hexRespData)
77bc2ed2b3Sopenharmony_ci{
78bc2ed2b3Sopenharmony_ci    DebugLog("HceService::SendRawFrame");
79bc2ed2b3Sopenharmony_ci    OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
80bc2ed2b3Sopenharmony_ci    if (hceSession == nullptr) {
81bc2ed2b3Sopenharmony_ci        ErrorLog("HceService::SendRawFrame, ERR_HCE_STATE_UNBIND");
82bc2ed2b3Sopenharmony_ci        return ErrorCode::ERR_HCE_STATE_UNBIND;
83bc2ed2b3Sopenharmony_ci    }
84bc2ed2b3Sopenharmony_ci    return hceSession->SendRawFrame(hexCmdData, raw, hexRespData);
85bc2ed2b3Sopenharmony_ci}
86bc2ed2b3Sopenharmony_ciint HceService::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
87bc2ed2b3Sopenharmony_ci{
88bc2ed2b3Sopenharmony_ci    DebugLog("HceService::GetPaymentServices");
89bc2ed2b3Sopenharmony_ci    OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
90bc2ed2b3Sopenharmony_ci    if (hceSession == nullptr) {
91bc2ed2b3Sopenharmony_ci        ErrorLog("HceService::GetPaymentServices, ERR_HCE_STATE_UNBIND");
92bc2ed2b3Sopenharmony_ci        return ErrorCode::ERR_HCE_STATE_UNBIND;
93bc2ed2b3Sopenharmony_ci    }
94bc2ed2b3Sopenharmony_ci    return hceSession->GetPaymentServices(abilityInfos);
95bc2ed2b3Sopenharmony_ci}
96bc2ed2b3Sopenharmony_ci
97bc2ed2b3Sopenharmony_ciKITS::ErrorCode HceService::StartHce(const ElementName &element, const std::vector<std::string> &aids)
98bc2ed2b3Sopenharmony_ci{
99bc2ed2b3Sopenharmony_ci    DebugLog("HceService::StartHce");
100bc2ed2b3Sopenharmony_ci    OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
101bc2ed2b3Sopenharmony_ci    if (hceSession == nullptr) {
102bc2ed2b3Sopenharmony_ci        ErrorLog("HceService::StartHce, ERR_HCE_STATE_UNBIND");
103bc2ed2b3Sopenharmony_ci        return ErrorCode::ERR_HCE_STATE_UNBIND;
104bc2ed2b3Sopenharmony_ci    }
105bc2ed2b3Sopenharmony_ci    return hceSession->StartHce(element, aids);
106bc2ed2b3Sopenharmony_ci}
107bc2ed2b3Sopenharmony_ciOHOS::sptr<HCE::IHceSession> HceService::GetHceSessionProxy()
108bc2ed2b3Sopenharmony_ci{
109bc2ed2b3Sopenharmony_ci    if (hceSessionProxy_ == nullptr) {
110bc2ed2b3Sopenharmony_ci        OHOS::sptr<IRemoteObject> iface = NfcController::GetInstance().GetHceServiceIface();
111bc2ed2b3Sopenharmony_ci        if (iface != nullptr) {
112bc2ed2b3Sopenharmony_ci            hceSessionProxy_ = new HCE::HceSessionProxy(iface);
113bc2ed2b3Sopenharmony_ci        }
114bc2ed2b3Sopenharmony_ci    }
115bc2ed2b3Sopenharmony_ci    return hceSessionProxy_;
116bc2ed2b3Sopenharmony_ci}
117bc2ed2b3Sopenharmony_ci} // namespace KITS
118bc2ed2b3Sopenharmony_ci} // namespace NFC
119bc2ed2b3Sopenharmony_ci} // namespace OHOS