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 "hce_session.h"
18 #include "external_deps_proxy.h"
19 
20 namespace OHOS {
21 namespace NFC {
22 namespace HCE {
23 using OHOS::AppExecFwk::ElementName;
24 const std::string DUMP_LINE = "---------------------------";
25 const std::string DUMP_END = "\n";
26 
HceSession(std::shared_ptr<INfcService> service)27 HceSession::HceSession(std::shared_ptr<INfcService> service) : nfcService_(service)
28 {
29     if (service == nullptr) {
30         ErrorLog("HceSession create fail, service is nullptr");
31         return;
32     }
33     ceService_ = service->GetCeService();
34 }
35 
~HceSession()36 HceSession::~HceSession()
37 {
38 }
39 
RegHceCmdCallbackByToken(const sptr<KITS::IHceCmdCallback> & callback,const std::string & type,Security::AccessToken::AccessTokenID callerToken)40 KITS::ErrorCode HceSession::RegHceCmdCallbackByToken(const sptr<KITS::IHceCmdCallback> &callback,
41                                                      const std::string &type,
42                                                      Security::AccessToken::AccessTokenID callerToken)
43 {
44     if (ceService_.expired()) {
45         ErrorLog("RegHceCmdCallback:ceService_ is nullptr");
46         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
47     }
48     if (ceService_.lock()->RegHceCmdCallback(callback, type, callerToken)) {
49         return KITS::ERR_NONE;
50     }
51     return KITS::ERR_NFC_PARAMETERS;
52 }
53 
UnRegHceCmdCallback(const std::string & type,Security::AccessToken::AccessTokenID callerToken)54 KITS::ErrorCode HceSession::UnRegHceCmdCallback(const std::string &type,
55                                                 Security::AccessToken::AccessTokenID callerToken)
56 {
57     if (ceService_.expired()) {
58         ErrorLog("UnRegHceCmdCallback ceService_ is nullptr");
59         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
60     }
61     if (ceService_.lock()->UnRegHceCmdCallback(type, callerToken)) {
62         return KITS::ERR_NONE;
63     }
64     return KITS::ERR_HCE_PARAMETERS;
65 }
66 
UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken)67 KITS::ErrorCode HceSession::UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken)
68 {
69     if (ceService_.expired()) {
70         ErrorLog("UnRegAllCallback ceService_ is nullptr");
71         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
72     }
73     if (ceService_.lock()->UnRegAllCallback(callerToken)) {
74         return KITS::ERR_NONE;
75     }
76     return KITS::ERR_HCE_PARAMETERS;
77 }
78 
HandleWhenRemoteDie(Security::AccessToken::AccessTokenID callerToken)79 KITS::ErrorCode HceSession::HandleWhenRemoteDie(Security::AccessToken::AccessTokenID callerToken)
80 {
81     if (ceService_.expired()) {
82         ErrorLog("HandleWhenRemoteDie ceService_ is nullptr");
83         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
84     }
85     if (ceService_.lock()->HandleWhenRemoteDie(callerToken)) {
86         return KITS::ERR_NONE;
87     }
88     return KITS::ERR_HCE_PARAMETERS;
89 }
90 
SendRawFrameByToken(std::string hexCmdData,bool raw,std::string & hexRespData,Security::AccessToken::AccessTokenID callerToken)91 int HceSession::SendRawFrameByToken(std::string hexCmdData, bool raw, std::string &hexRespData,
92                                     Security::AccessToken::AccessTokenID callerToken)
93 {
94     if (ceService_.expired()) {
95         ErrorLog("SendRawFrame ceService_ is nullptr");
96         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
97     }
98     bool success = ceService_.lock()->SendHostApduData(hexCmdData, raw, hexRespData, callerToken);
99     if (success) {
100         return NFC::KITS::ErrorCode::ERR_NONE;
101     } else {
102         return NFC::KITS::ErrorCode::ERR_HCE_STATE_IO_FAILED;
103     }
104 }
105 
IsDefaultService(ElementName & element,const std::string & type,bool & isDefaultService)106 KITS::ErrorCode HceSession::IsDefaultService(ElementName &element, const std::string &type, bool &isDefaultService)
107 {
108     if (ceService_.expired()) {
109         ErrorLog("IsDefaultService ceService_ is nullptr");
110         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
111     }
112     isDefaultService = ceService_.lock()->IsDefaultService(element, type);
113     return KITS::ERR_NONE;
114 }
115 
StartHce(const ElementName & element,const std::vector<std::string> & aids)116 KITS::ErrorCode HceSession::StartHce(const ElementName &element, const std::vector<std::string> &aids)
117 {
118     if (ceService_.expired()) {
119         ErrorLog("StartHce ceService_ is nullptr");
120         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
121     }
122     if (ceService_.lock()->StartHce(element, aids)) {
123         return KITS::ERR_NONE;
124     }
125     return KITS::ERR_HCE_PARAMETERS;
126 }
127 
StopHce(const ElementName & element,Security::AccessToken::AccessTokenID callerToken)128 KITS::ErrorCode HceSession::StopHce(const ElementName &element, Security::AccessToken::AccessTokenID callerToken)
129 {
130     if (ceService_.expired()) {
131         ErrorLog("StopHce ceService_ is nullptr");
132         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
133     }
134     if (ceService_.lock()->StopHce(element, callerToken)) {
135         return KITS::ERR_NONE;
136     }
137     return KITS::ERR_HCE_PARAMETERS;
138 }
139 
Dump(int32_t fd,const std::vector<std::u16string> & args)140 int32_t HceSession::Dump(int32_t fd, const std::vector<std::u16string> &args)
141 {
142     std::string info = GetDumpInfo();
143     int ret = dprintf(fd, "%s\n", info.c_str());
144     if (ret < 0) {
145         ErrorLog("hceSession Dump ret = %{public}d", ret);
146         return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
147     }
148     return NFC::KITS::ErrorCode::ERR_NONE;
149 }
150 
GetDumpInfo()151 std::string HceSession::GetDumpInfo()
152 {
153     std::string info;
154     return info.append(DUMP_LINE)
155         .append("Hce DUMP ")
156         .append(DUMP_LINE)
157         .append(DUMP_END)
158         .append("NFC_STATE          : ")
159         .append(std::to_string(nfcService_.lock()->GetNfcState()))
160         .append(DUMP_END)
161         .append("SCREEN_STATE       : ")
162         .append(std::to_string(nfcService_.lock()->GetScreenState()))
163         .append(DUMP_END)
164         .append("NCI_VERSION        : ")
165         .append(std::to_string(nfcService_.lock()->GetNciVersion()))
166         .append(DUMP_END);
167 }
GetPaymentServices(std::vector<AbilityInfo> & abilityInfos)168 int HceSession::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
169 {
170     ExternalDepsProxy::GetInstance().GetPaymentAbilityInfos(abilityInfos);
171 #ifdef NFC_SIM_FEATURE
172     AppendSimBundle(abilityInfos);
173 #endif
174     return NFC::KITS::ErrorCode::ERR_NONE;
175 }
176 #ifdef NFC_SIM_FEATURE
AppendSimBundle(std::vector<AbilityInfo> & paymentAbilityInfos)177 void HceSession::AppendSimBundle(std::vector<AbilityInfo> &paymentAbilityInfos)
178 {
179     std::string simBundleName = nfcService_.lock()->GetSimVendorBundleName();
180     AppExecFwk::BundleInfo bundleInfo;
181     bool result = ExternalDepsProxy::GetInstance().GetBundleInfo(bundleInfo, simBundleName);
182     if (!result) {
183         ErrorLog("get sim bundle info failed.");
184         return;
185     }
186     AbilityInfo simAbility;
187     simAbility.bundleName = simBundleName;
188     simAbility.labelId = bundleInfo.applicationInfo.labelId;
189     simAbility.iconId = bundleInfo.applicationInfo.iconId;
190     paymentAbilityInfos.push_back(simAbility);
191 }
192 #endif
193 } // namespace HCE
194 } // namespace NFC
195 } // namespace OHOS
196