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