• 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 
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 
30 std::mutex g_hceSessionProxyLock;
HceService()31 HceService::HceService()
32 {
33     DebugLog("[HceService] new HceService");
34 }
35 
~HceService()36 HceService::~HceService()
37 {
38     DebugLog("destruct HceService");
39 }
40 
GetInstance()41 HceService &HceService::GetInstance()
42 {
43     static HceService instance;
44     return instance;
45 }
46 
RegHceCmdCallback(const sptr<IHceCmdCallback> & callback,const std::string & type)47 ErrorCode HceService::RegHceCmdCallback(const sptr<IHceCmdCallback> &callback, const std::string &type)
48 {
49     DebugLog("HceService::RegHceCmdCallback");
50     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
51     if (hceSession == nullptr) {
52         ErrorLog("HceService::RegHceCmdCallback, ERR_HCE_STATE_UNBIND");
53         return ErrorCode::ERR_HCE_STATE_UNBIND;
54     }
55     return hceSession->RegHceCmdCallback(callback, type);
56 }
57 
UnRegHceCmdCallback(const sptr<IHceCmdCallback> & callback,const std::string & type)58 ErrorCode HceService::UnRegHceCmdCallback(const sptr<IHceCmdCallback> &callback, const std::string &type)
59 {
60     DebugLog("HceService::UnRegHceCmdCallback");
61     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
62     if (hceSession == nullptr) {
63         ErrorLog("HceService::UnRegHceCmdCallback, ERR_HCE_STATE_UNBIND");
64         return ErrorCode::ERR_HCE_STATE_UNBIND;
65     }
66     return hceSession->UnregHceCmdCallback(callback, type);
67 }
68 
StopHce(ElementName & element)69 ErrorCode HceService::StopHce(ElementName &element)
70 {
71     DebugLog("HceService::StopHce");
72     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
73     if (hceSession == nullptr) {
74         ErrorLog("HceService::StopHce, ERR_HCE_STATE_UNBIND");
75         return ErrorCode::ERR_HCE_STATE_UNBIND;
76     }
77     return hceSession->StopHce(element);
78 }
IsDefaultService(ElementName & element,const std::string & type,bool & isDefaultService)79 ErrorCode HceService::IsDefaultService(ElementName &element, const std::string &type, bool &isDefaultService)
80 {
81     DebugLog("HceService::IsDefaultService");
82     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
83     if (hceSession == nullptr) {
84         ErrorLog("HceService::IsDefaultService, ERR_HCE_STATE_UNBIND");
85         return ErrorCode::ERR_HCE_STATE_UNBIND;
86     }
87     return hceSession->IsDefaultService(element, type, isDefaultService);
88 }
SendRawFrame(std::string hexCmdData,bool raw,std::string & hexRespData)89 int HceService::SendRawFrame(std::string hexCmdData, bool raw, std::string &hexRespData)
90 {
91     DebugLog("HceService::SendRawFrame");
92     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
93     if (hceSession == nullptr) {
94         ErrorLog("HceService::SendRawFrame, ERR_HCE_STATE_UNBIND");
95         return ErrorCode::ERR_HCE_STATE_UNBIND;
96     }
97     return hceSession->SendRawFrame(hexCmdData, raw, hexRespData);
98 }
GetPaymentServices(std::vector<AbilityInfo> & abilityInfos)99 int HceService::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
100 {
101     DebugLog("HceService::GetPaymentServices");
102     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
103     if (hceSession == nullptr) {
104         ErrorLog("HceService::GetPaymentServices, ERR_HCE_STATE_UNBIND");
105         return ErrorCode::ERR_HCE_STATE_UNBIND;
106     }
107     return hceSession->GetPaymentServices(abilityInfos);
108 }
109 
StartHce(const ElementName & element,const std::vector<std::string> & aids)110 KITS::ErrorCode HceService::StartHce(const ElementName &element, const std::vector<std::string> &aids)
111 {
112     DebugLog("HceService::StartHce");
113     OHOS::sptr<HCE::IHceSession> hceSession = GetHceSessionProxy();
114     if (hceSession == nullptr) {
115         ErrorLog("HceService::StartHce, ERR_HCE_STATE_UNBIND");
116         return ErrorCode::ERR_HCE_STATE_UNBIND;
117     }
118     return hceSession->StartHce(element, aids);
119 }
GetHceSessionProxy()120 OHOS::sptr<HCE::IHceSession> HceService::GetHceSessionProxy()
121 {
122     std::lock_guard<std::mutex> lock(g_hceSessionProxyLock);
123     if (hceSessionProxy_ == nullptr) {
124         OHOS::sptr<IRemoteObject> iface = NfcController::GetInstance().GetHceServiceIface();
125         if (iface != nullptr) {
126             hceSessionProxy_ = new HCE::HceSessionProxy(iface);
127         }
128     }
129     return hceSessionProxy_;
130 }
ClearHceSessionProxy()131 void HceService::ClearHceSessionProxy()
132 {
133     WarnLog("ClearHceSessionProxy");
134     std::lock_guard<std::mutex> lock(g_hceSessionProxyLock);
135     hceSessionProxy_ = nullptr;
136 }
137 } // namespace KITS
138 } // namespace NFC
139 } // namespace OHOS