1/* 2 * Copyright (C) 2023-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#include "ce_service.h" 16#include "nfc_event_publisher.h" 17#include "nfc_event_handler.h" 18#include "external_deps_proxy.h" 19#include "loghelper.h" 20#include "nfc_sdk_common.h" 21#include "setting_data_share_impl.h" 22#include "accesstoken_kit.h" 23#include "hap_token_info.h" 24 25namespace OHOS { 26namespace NFC { 27const int FIELD_COMMON_EVENT_INTERVAL = 1000; 28const int DEACTIVATE_TIMEOUT = 6000; 29static const int DEFAULT_HOST_ROUTE_DEST = 0x00; 30static const int PWR_STA_SWTCH_ON_SCRN_UNLCK = 0x01; 31static const int DEFAULT_PWR_STA_HOST = PWR_STA_SWTCH_ON_SCRN_UNLCK; 32const std::string APP_REMOVED = "app_removed"; 33const std::string APP_ADDED = "app_added"; 34std::mutex g_defaultPaymentAppInitMutex = {}; 35 36CeService::CeService(std::weak_ptr<NfcService> nfcService, std::weak_ptr<NCI::INciCeInterface> nciCeProxy) 37 : nfcService_(nfcService), nciCeProxy_(nciCeProxy) 38 39{ 40 Uri nfcDefaultPaymentApp(KITS::NFC_DATA_URI_PAYMENT_DEFAULT_APP); 41 DelayedSingleton<SettingDataShareImpl>::GetInstance()->GetElementName( 42 nfcDefaultPaymentApp, KITS::DATA_SHARE_KEY_NFC_PAYMENT_DEFAULT_APP, defaultPaymentElement_); 43 44 appStateObserver_ = std::make_shared<AppStateObserver>(this); 45 DebugLog("CeService constructor end"); 46} 47 48CeService::~CeService() 49{ 50 hostCardEmulationManager_ = nullptr; 51 aidToAidEntry_.clear(); 52 foregroundElement_.SetBundleName(""); 53 foregroundElement_.SetAbilityName(""); 54 foregroundElement_.SetDeviceID(""); 55 foregroundElement_.SetModuleName(""); 56 defaultPaymentElement_.SetBundleName(""); 57 defaultPaymentElement_.SetAbilityName(""); 58 defaultPaymentElement_.SetDeviceID(""); 59 defaultPaymentElement_.SetModuleName(""); 60 initDefaultPaymentAppDone_ = false; 61 dynamicAids_.clear(); 62 DebugLog("CeService deconstructor end"); 63} 64 65void CeService::PublishFieldOnOrOffCommonEvent(bool isFieldOn) 66{ 67 ExternalDepsProxy::GetInstance().PublishNfcFieldStateChanged(isFieldOn); 68} 69 70bool CeService::RegHceCmdCallback(const sptr<KITS::IHceCmdCallback> &callback, const std::string &type, 71 Security::AccessToken::AccessTokenID callerToken) 72{ 73 if (hostCardEmulationManager_ == nullptr) { 74 ErrorLog("hce is null"); 75 return false; 76 } 77 return hostCardEmulationManager_->RegHceCmdCallback(callback, type, callerToken); 78} 79 80bool CeService::UnRegHceCmdCallback(const std::string &type, Security::AccessToken::AccessTokenID callerToken) 81{ 82 if (hostCardEmulationManager_ == nullptr) { 83 ErrorLog("hce is null"); 84 return false; 85 } 86 return hostCardEmulationManager_->UnRegHceCmdCallback(type, callerToken); 87} 88 89bool CeService::UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken) 90{ 91 if (hostCardEmulationManager_ == nullptr) { 92 ErrorLog("hce is null"); 93 return false; 94 } 95 return hostCardEmulationManager_->UnRegAllCallback(callerToken); 96} 97 98bool CeService::IsDefaultService(ElementName &element, const std::string &type) 99{ 100 return type == KITS::TYPE_PAYMENT && element.GetBundleName() == defaultPaymentElement_.GetBundleName() && 101 element.GetAbilityName() == defaultPaymentElement_.GetAbilityName(); 102} 103 104bool CeService::SendHostApduData(const std::string &hexCmdData, bool raw, std::string &hexRespData, 105 Security::AccessToken::AccessTokenID callerToken) 106{ 107 if (hostCardEmulationManager_ == nullptr) { 108 ErrorLog("hce is null"); 109 return false; 110 } 111 return hostCardEmulationManager_->SendHostApduData(hexCmdData, raw, hexRespData, callerToken); 112} 113 114bool CeService::InitConfigAidRouting(bool forceUpdate) 115{ 116 DebugLog("AddAidRoutingHceAids: start, forceUpdate is %{public}d", forceUpdate); 117 std::lock_guard<std::mutex> lock(configRoutingMutex_); 118 std::map<std::string, AidEntry> aidEntries; 119 BuildAidEntries(aidEntries); 120 InfoLog("AddAidRoutingHceAids, aid entries cache size %{public}zu,aid entries newly builded size %{public}zu", 121 aidToAidEntry_.size(), aidEntries.size()); 122 if (aidEntries == aidToAidEntry_ && !forceUpdate) { 123 InfoLog("aid entries do not change."); 124 return false; 125 } 126 127 if (nciCeProxy_.expired()) { 128 ErrorLog("InitConfigAidRouting: nciCeProxy_ is nullptr."); 129 return false; 130 } 131 nciCeProxy_.lock()->ClearAidTable(); 132 aidToAidEntry_.clear(); 133 bool addAllResult = true; 134 for (const auto &pair : aidEntries) { 135 AidEntry entry = pair.second; 136 std::string aid = entry.aid; 137 int aidInfo = entry.aidInfo; 138 int power = entry.power; 139 int route = entry.route; 140 InfoLog("AddAidRoutingHceAids: aid= %{public}s, aidInfo= " 141 "0x%{public}x, route=0x%{public}x, power=0x%{public}x", 142 aid.c_str(), aidInfo, route, power); 143 bool addResult = nciCeProxy_.lock()->AddAidRouting(aid, route, aidInfo, power); 144 if (!addResult) { 145 ErrorLog("AddAidRoutingHceAids: add aid failed aid= %{public}s", aid.c_str()); 146 addAllResult = false; 147 } 148 } 149 if (addAllResult) { 150 InfoLog("AddAidRoutingHceAids: add aids success, update the aid entries cache"); 151 aidToAidEntry_ = std::move(aidEntries); 152 } 153 DebugLog("AddAidRoutingHceAids: end"); 154 return true; 155} 156 157void CeService::HandleAppStateChanged(const std::string &bundleName, const std::string &abilityName, 158 int abilityState) 159{ 160 if (bundleName.empty()) { 161 ErrorLog("OnForegroundApplicationChanged bundle name is empty."); 162 return; 163 } 164 165 if (bundleName != foregroundElement_.GetBundleName()) { 166 DebugLog("OnForegroundApplicationChanged not equal to the foreground element, no need to handle."); 167 return; 168 } 169 if (abilityState == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND)) { 170 DebugLog("OnForegroundApplicationChanged foreground state, no need to handle."); 171 return; 172 } 173 174 ClearHceInfo(); 175 InfoLog("foreground app state change: refresh route table"); 176 ConfigRoutingAndCommit(); 177} 178 179void CeService::BuildAidEntries(std::map<std::string, AidEntry> &aidEntries) 180{ 181 std::vector<AppDataParser::HceAppAidInfo> hceApps; 182 ExternalDepsProxy::GetInstance().GetHceApps(hceApps); 183 InfoLog("AddAidRoutingHceOtherAids: hce apps size %{public}zu", hceApps.size()); 184 for (const AppDataParser::HceAppAidInfo &appAidInfo : hceApps) { 185 bool isForeground = appAidInfo.element.GetBundleName() == foregroundElement_.GetBundleName() && 186 appAidInfo.element.GetAbilityName() == foregroundElement_.GetAbilityName(); 187 bool isDefaultPayment = appAidInfo.element.GetBundleName() == defaultPaymentElement_.GetBundleName() && 188 appAidInfo.element.GetAbilityName() == defaultPaymentElement_.GetAbilityName(); 189 for (const AppDataParser::AidInfo &aidInfo : appAidInfo.customDataAid) { 190 // add payment aid of default payment app and foreground app 191 // add other aid of all apps 192 bool shouldAdd = KITS::KEY_OHTER_AID == aidInfo.name || isForeground || isDefaultPayment; 193 if (shouldAdd) { 194 AidEntry aidEntry; 195 aidEntry.aid = aidInfo.value; 196 aidEntry.aidInfo = 0; 197 aidEntry.power = DEFAULT_PWR_STA_HOST; 198 aidEntry.route = DEFAULT_HOST_ROUTE_DEST; 199 aidEntries[aidInfo.value] = aidEntry; 200 } 201 } 202 } 203 for (const std::string &aid : dynamicAids_) { 204 AidEntry aidEntry; 205 aidEntry.aid = aid; 206 aidEntry.aidInfo = 0; 207 aidEntry.power = DEFAULT_PWR_STA_HOST; 208 aidEntry.route = DEFAULT_HOST_ROUTE_DEST; 209 aidEntries[aid] = aidEntry; 210 } 211} 212 213void CeService::ClearAidEntriesCache() 214{ 215 std::lock_guard<std::mutex> lock(configRoutingMutex_); 216 aidToAidEntry_.clear(); 217 DebugLog("ClearAidEntriesCache end"); 218} 219 220bool CeService::IsDynamicAid(const std::string &targetAid) 221{ 222 for (const std::string &aid : dynamicAids_) { 223 if (aid == targetAid) { 224 return true; 225 } 226 } 227 return false; 228} 229 230void CeService::OnDefaultPaymentServiceChange() 231{ 232 ElementName newElement; 233 Uri nfcDefaultPaymentApp(KITS::NFC_DATA_URI_PAYMENT_DEFAULT_APP); 234 DelayedSingleton<SettingDataShareImpl>::GetInstance()->GetElementName( 235 nfcDefaultPaymentApp, KITS::DATA_SHARE_KEY_NFC_PAYMENT_DEFAULT_APP, newElement); 236 if (newElement.GetURI() == defaultPaymentElement_.GetURI()) { 237 InfoLog("OnDefaultPaymentServiceChange: payment service not change"); 238 return; 239 } 240 241 if (nfcService_.expired()) { 242 ErrorLog("nfcService_ is nullptr."); 243 return; 244 } 245 if (!nfcService_.lock()->IsNfcEnabled()) { 246 ErrorLog("NFC not enabled, should not happen.The default payment app is be set when nfc is enabled."); 247 return; 248 } 249 ExternalDepsProxy::GetInstance().WriteDefaultPaymentAppChangeHiSysEvent(defaultPaymentElement_.GetBundleName(), 250 newElement.GetBundleName()); 251 UpdateDefaultPaymentElement(newElement); 252 InfoLog("OnDefaultPaymentServiceChange: refresh route table"); 253 ConfigRoutingAndCommit(); 254} 255void CeService::OnAppAddOrChangeOrRemove(std::shared_ptr<EventFwk::CommonEventData> data) 256{ 257 DebugLog("OnAppAddOrChangeOrRemove start"); 258 259 if (!AppEventCheckValid(data)) { 260 return; 261 } 262 263 std::string action = data->GetWant().GetAction(); 264 ElementName element = data->GetWant().GetElement(); 265 std::string bundleName = element.GetBundleName(); 266 267 InfoLog("OnAppAddOrChangeOrRemove: change bundleName %{public}s, default payment bundle name %{public}s, " 268 "installed status %{public}d", 269 bundleName.c_str(), defaultPaymentElement_.GetBundleName().c_str(), 270 defaultPaymentBundleInstalled_); 271 272 if (bundleName == defaultPaymentElement_.GetBundleName() && 273 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { 274 UpdateDefaultPaymentBundleInstalledStatus(false); 275 ExternalDepsProxy::GetInstance().WriteDefaultPaymentAppChangeHiSysEvent( 276 defaultPaymentElement_.GetBundleName(), APP_REMOVED); 277 } 278 279 if (bundleName == defaultPaymentElement_.GetBundleName() && 280 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { 281 UpdateDefaultPaymentBundleInstalledStatus(true); 282 ExternalDepsProxy::GetInstance().WriteDefaultPaymentAppChangeHiSysEvent( 283 defaultPaymentElement_.GetBundleName(), APP_ADDED); 284 } 285 286 if (nfcService_.expired()) { 287 ErrorLog("nfcService_ is nullptr."); 288 return; 289 } 290 if (!nfcService_.lock()->IsNfcEnabled()) { 291 ErrorLog(" NFC not enabled, not need to update routing entry "); 292 return; 293 } 294 InfoLog("OnAppAddOrChangeOrRemove: refresh route table"); 295 ConfigRoutingAndCommit(); 296 DebugLog("OnAppAddOrChangeOrRemove end"); 297} 298 299bool CeService::AppEventCheckValid(std::shared_ptr<EventFwk::CommonEventData> data) 300{ 301 if (data == nullptr) { 302 ErrorLog("invalid event data"); 303 return false; 304 } 305 std::string action = data->GetWant().GetAction(); 306 if (action.empty()) { 307 ErrorLog("action is empty"); 308 return false; 309 } 310 if ((action != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) && 311 (action != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) && 312 (action != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED)) { 313 InfoLog("not the interested action"); 314 return false; 315 } 316 317 ElementName element = data->GetWant().GetElement(); 318 std::string bundleName = element.GetBundleName(); 319 if (bundleName.empty()) { 320 ErrorLog("invalid bundleName."); 321 return false; 322 } 323 return true; 324} 325void CeService::UpdateDefaultPaymentBundleInstalledStatus(bool installed) 326{ 327 InfoLog("UpdateDefaultPaymentBundleInstalledStatus: bundleName %{public}d", installed); 328 std::lock_guard<std::mutex> lock(configRoutingMutex_); 329 defaultPaymentBundleInstalled_ = installed; 330} 331 332void CeService::UpdateDefaultPaymentElement(const ElementName &element) 333{ 334 InfoLog("UpdateDefaultPaymentElement: bundleName %{public}s", element.GetURI().c_str()); 335 std::lock_guard<std::mutex> lock(configRoutingMutex_); 336 defaultPaymentElement_ = element; 337 defaultPaymentBundleInstalled_ = true; 338} 339KITS::DefaultPaymentType CeService::GetDefaultPaymentType() 340{ 341 InfoLog("GetDefaultPaymentType: default payment bundle name %{public}s, " 342 "installed status %{public}d", 343 defaultPaymentElement_.GetBundleName().c_str(), defaultPaymentBundleInstalled_); 344 345 if (defaultPaymentElement_.GetBundleName().empty()) { 346 return KITS::DefaultPaymentType::TYPE_EMPTY; 347 } 348 if (!defaultPaymentBundleInstalled_) { 349 return KITS::DefaultPaymentType::TYPE_UNINSTALLED; 350 } 351 if (defaultPaymentElement_.GetBundleName() == nciCeProxy_.lock()->GetSimVendorBundleName()) { 352 return KITS::DefaultPaymentType::TYPE_UICC; 353 } 354 if (ExternalDepsProxy::GetInstance().IsHceApp(defaultPaymentElement_)) { 355 return KITS::DefaultPaymentType::TYPE_HCE; 356 } 357 358 return KITS::DefaultPaymentType::TYPE_ESE; 359} 360 361void CeService::ConfigRoutingAndCommit() 362{ 363 if (nfcService_.expired()) { 364 ErrorLog("ConfigRoutingAndCommit: nfc service is null"); 365 return; 366 } 367 std::weak_ptr<NfcRoutingManager> routingManager = nfcService_.lock()->GetNfcRoutingManager(); 368 if (routingManager.expired()) { 369 ErrorLog("ConfigRoutingAndCommit: routing manager is null"); 370 return; 371 } 372 373 bool updateAids = false; 374 bool updatePaymentType = UpdateDefaultPaymentType(); 375 if (updatePaymentType) { 376 updateAids = InitConfigAidRouting(true); 377 } else { 378 updateAids = InitConfigAidRouting(false); 379 } 380 381 InfoLog( 382 "ConfigRoutingAndCommit: aids updated status %{public}d, default payment type updated status %{public}d.", 383 updateAids, updatePaymentType); 384 if (updateAids || updatePaymentType) { 385 routingManager.lock()->ComputeRoutingParams(defaultPaymentType_); 386 routingManager.lock()->CommitRouting(); 387 } 388} 389 390void CeService::SearchElementByAid(const std::string &aid, ElementName &aidElement) 391{ 392 if (aid.empty()) { 393 InfoLog("aid is empty"); 394 return; 395 } 396 // find dynamic aid 397 if (IsDynamicAid(aid) && !foregroundElement_.GetBundleName().empty()) { 398 InfoLog("is foreground element"); 399 aidElement.SetBundleName(foregroundElement_.GetBundleName()); 400 aidElement.SetAbilityName(foregroundElement_.GetAbilityName()); 401 return; 402 } 403 std::vector<AppDataParser::HceAppAidInfo> hceApps; 404 ExternalDepsProxy::GetInstance().GetHceAppsByAid(aid, hceApps); 405 if (hceApps.empty()) { 406 InfoLog("No applications found"); 407 return; 408 } 409 // only one element, resolved 410 if (hceApps.size() == 1) { 411 ElementName element = hceApps[0].element; 412 aidElement.SetBundleName(element.GetBundleName()); 413 aidElement.SetAbilityName(element.GetAbilityName()); 414 return; 415 } 416 InfoLog("Found too many applications"); 417 for (const AppDataParser::HceAppAidInfo &hceApp : hceApps) { 418 ElementName elementName = hceApp.element; 419 InfoLog("ElementName: %{public}s", elementName.GetBundleName().c_str()); 420 InfoLog("ElementValue: %{public}s", elementName.GetAbilityName().c_str()); 421 422 bool isForeground = elementName.GetBundleName() == foregroundElement_.GetBundleName() && 423 elementName.GetAbilityName() == foregroundElement_.GetAbilityName(); 424 bool isDefaultPayment = elementName.GetBundleName() == defaultPaymentElement_.GetBundleName() && 425 elementName.GetAbilityName() == defaultPaymentElement_.GetAbilityName(); 426 if (isForeground) { 427 // is foregroud, resolved 428 InfoLog("is foreground element"); 429 aidElement.SetBundleName(elementName.GetBundleName()); 430 aidElement.SetAbilityName(elementName.GetAbilityName()); 431 return; 432 } else if (isDefaultPayment && IsPaymentAid(aid, hceApp)) { 433 // is default payment, resolved 434 InfoLog("is default payment element"); 435 aidElement.SetBundleName(elementName.GetBundleName()); 436 aidElement.SetAbilityName(elementName.GetAbilityName()); 437 return; 438 } 439 } 440 441 HandleOtherAidConflicted(hceApps); 442 InfoLog("SearchElementByAid end."); 443} 444void CeService::HandleOtherAidConflicted(const std::vector<AppDataParser::HceAppAidInfo> &hceApps) 445{ 446 InfoLog("too many applications found, let user decide."); 447 TAG::NfcNotificationId notificationId = TAG::NFC_HCE_AID_CONFLICTED_ID; 448 ExternalDepsProxy::GetInstance().PublishNfcNotification(notificationId, "", 0); 449} 450 451bool CeService::UpdateDefaultPaymentType() 452{ 453 KITS::DefaultPaymentType defaultPaymentType = GetDefaultPaymentType(); 454 InfoLog("The last default payment type %{public}d, the new one %{public}d.", defaultPaymentType_, 455 defaultPaymentType); 456 if (defaultPaymentType == defaultPaymentType_) { 457 return false; 458 } 459 std::lock_guard<std::mutex> lock(configRoutingMutex_); 460 ExternalDepsProxy::GetInstance().WriteDefaultRouteChangeHiSysEvent( 461 static_cast<int>(defaultPaymentType_), static_cast<int>(defaultPaymentType)); 462 defaultPaymentType_ = defaultPaymentType; 463 NotifyDefaultPaymentType(static_cast<int>(defaultPaymentType_)); 464 return true; 465} 466 467bool CeService::IsPaymentAid(const std::string &aid, const AppDataParser::HceAppAidInfo &hceApp) 468{ 469 for (const AppDataParser::AidInfo &aidInfo : hceApp.customDataAid) { 470 if (KITS::KEY_PAYMENT_AID == aidInfo.name && aid == aidInfo.value) { 471 return true; 472 } 473 } 474 return false; 475} 476 477void CeService::HandleFieldActivated() 478{ 479 if (nfcService_.expired() || nfcService_.lock()->eventHandler_ == nullptr) { 480 return; 481 } 482 nfcService_.lock()->eventHandler_->RemoveEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF)); 483 nfcService_.lock()->eventHandler_->RemoveEvent( 484 static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF_TIMEOUT)); 485 nfcService_.lock()->eventHandler_->SendEvent( 486 static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF_TIMEOUT), DEACTIVATE_TIMEOUT); 487 488 uint64_t currentTime = KITS::NfcSdkCommon::GetRelativeTime(); 489 if (currentTime < lastFieldOnTime_) { 490 WarnLog("currentTime = %{public}lu, lastFieldOnTime_ = %{public}lu", currentTime, lastFieldOnTime_); 491 lastFieldOnTime_ = 0; 492 return; 493 } 494 if (currentTime - lastFieldOnTime_ > FIELD_COMMON_EVENT_INTERVAL) { 495 lastFieldOnTime_ = currentTime; 496 nfcService_.lock()->eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_ON)); 497 } 498} 499 500void CeService::HandleFieldDeactivated() 501{ 502 if (nfcService_.expired() || nfcService_.lock()->eventHandler_ == nullptr) { 503 return; 504 } 505 nfcService_.lock()->eventHandler_->RemoveEvent( 506 static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF_TIMEOUT)); 507 nfcService_.lock()->eventHandler_->RemoveEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF)); 508 509 uint64_t currentTime = KITS::NfcSdkCommon::GetRelativeTime(); 510 if (currentTime < lastFieldOffTime_) { 511 WarnLog("currentTime = %{public}lu, lastFieldOffTime_ = %{public}lu", currentTime, lastFieldOffTime_); 512 lastFieldOffTime_ = 0; 513 return; 514 } 515 if (currentTime - lastFieldOffTime_ > FIELD_COMMON_EVENT_INTERVAL) { 516 lastFieldOffTime_ = currentTime; 517 nfcService_.lock()->eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF), 518 FIELD_COMMON_EVENT_INTERVAL); 519 } 520} 521void CeService::OnCardEmulationData(const std::vector<uint8_t> &data) 522{ 523 if (hostCardEmulationManager_ == nullptr) { 524 ErrorLog("hce is null"); 525 return; 526 } 527 hostCardEmulationManager_->OnHostCardEmulationDataNfcA(data); 528} 529void CeService::OnCardEmulationActivated() 530{ 531 if (hostCardEmulationManager_ == nullptr) { 532 ErrorLog("hce is null"); 533 return; 534 } 535 hostCardEmulationManager_->OnCardEmulationActivated(); 536} 537void CeService::OnCardEmulationDeactivated() 538{ 539 if (hostCardEmulationManager_ == nullptr) { 540 ErrorLog("hce is null"); 541 return; 542 } 543 hostCardEmulationManager_->OnCardEmulationDeactivated(); 544} 545OHOS::sptr<OHOS::IRemoteObject> CeService::AsObject() 546{ 547 return nullptr; 548} 549void CeService::Initialize() 550{ 551 DebugLog("CeService Initialize start"); 552 dataRdbObserver_ = sptr<DefaultPaymentServiceChangeCallback>( 553 new (std::nothrow) DefaultPaymentServiceChangeCallback(shared_from_this())); 554 InitDefaultPaymentApp(); 555 defaultPaymentType_ = GetDefaultPaymentType(); 556 ExternalDepsProxy::GetInstance().WriteDefaultRouteChangeHiSysEvent( 557 static_cast<int>(KITS::DefaultPaymentType::TYPE_UNKNOWN), static_cast<int>(defaultPaymentType_)); 558 NotifyDefaultPaymentType(static_cast<int>(defaultPaymentType_)); 559 hostCardEmulationManager_ = 560 std::make_shared<HostCardEmulationManager>(nfcService_, nciCeProxy_, shared_from_this()); 561 DebugLog("CeService Initialize end"); 562} 563 564bool CeService::InitDefaultPaymentApp() 565{ 566 std::lock_guard<std::mutex> lock(g_defaultPaymentAppInitMutex); 567 if (initDefaultPaymentAppDone_) { 568 WarnLog("InitDefaultPaymentApp: already done"); 569 return false; 570 } 571 Uri nfcDefaultPaymentApp(KITS::NFC_DATA_URI_PAYMENT_DEFAULT_APP); 572 KITS::ErrorCode registerResult = DelayedSingleton<SettingDataShareImpl>::GetInstance()-> 573 RegisterDataObserver(nfcDefaultPaymentApp, dataRdbObserver_); 574 if (registerResult != KITS::ERR_NONE) { 575 initDefaultPaymentAppDone_ = false; 576 ErrorLog("register default payment app failed"); 577 return false; 578 } 579 DelayedSingleton<SettingDataShareImpl>::GetInstance()->GetElementName( 580 nfcDefaultPaymentApp, KITS::DATA_SHARE_KEY_NFC_PAYMENT_DEFAULT_APP, defaultPaymentElement_); 581 defaultPaymentBundleInstalled_ = 582 ExternalDepsProxy::GetInstance().IsBundleInstalled(defaultPaymentElement_.GetBundleName()); 583 584 std::string appStatus = defaultPaymentBundleInstalled_ ? APP_ADDED : APP_REMOVED; 585 ExternalDepsProxy::GetInstance().WriteDefaultPaymentAppChangeHiSysEvent(defaultPaymentElement_.GetBundleName(), 586 appStatus); 587 initDefaultPaymentAppDone_ = true; 588 return true; 589} 590 591void CeService::Deinitialize() 592{ 593 DebugLog("CeService Deinitialize start"); 594 ClearAidEntriesCache(); 595 foregroundElement_.SetBundleName(""); 596 foregroundElement_.SetAbilityName(""); 597 foregroundElement_.SetDeviceID(""); 598 foregroundElement_.SetModuleName(""); 599 defaultPaymentElement_.SetBundleName(""); 600 defaultPaymentElement_.SetAbilityName(""); 601 defaultPaymentElement_.SetDeviceID(""); 602 defaultPaymentElement_.SetModuleName(""); 603 initDefaultPaymentAppDone_ = false; 604 dynamicAids_.clear(); 605 Uri nfcDefaultPaymentApp(KITS::NFC_DATA_URI_PAYMENT_DEFAULT_APP); 606 DelayedSingleton<SettingDataShareImpl>::GetInstance()->ReleaseDataObserver(nfcDefaultPaymentApp, 607 dataRdbObserver_); 608 DebugLog("CeService Deinitialize end"); 609} 610 611bool CeService::StartHce(const ElementName &element, const std::vector<std::string> &aids) 612{ 613 if (nfcService_.expired()) { 614 ErrorLog("nfcService_ is nullptr."); 615 return false; 616 } 617 if (!nfcService_.lock()->IsNfcEnabled()) { 618 ErrorLog("NFC not enabled, should not happen."); 619 return false; 620 } 621 SetHceInfo(element, aids); 622 InfoLog("StartHce: refresh route table"); 623 ConfigRoutingAndCommit(); 624 return true; 625} 626 627void CeService::SetHceInfo(const ElementName &element, const std::vector<std::string> &aids) 628{ 629 InfoLog("SetHceInfo start."); 630 std::lock_guard<std::mutex> lock(configRoutingMutex_); 631 foregroundElement_ = element; 632 ExternalDepsProxy::GetInstance().WriteForegroundAppChangeHiSysEvent(foregroundElement_.GetBundleName()); 633 dynamicAids_.clear(); 634 dynamicAids_ = std::move(aids); 635} 636 637void CeService::ClearHceInfo() 638{ 639 InfoLog("ClearHceInfo start."); 640 std::lock_guard<std::mutex> lock(configRoutingMutex_); 641 foregroundElement_.SetBundleName(""); 642 foregroundElement_.SetAbilityName(""); 643 foregroundElement_.SetDeviceID(""); 644 foregroundElement_.SetModuleName(""); 645 ExternalDepsProxy::GetInstance().WriteForegroundAppChangeHiSysEvent(foregroundElement_.GetBundleName()); 646 dynamicAids_.clear(); 647} 648 649bool CeService::StopHce(const ElementName &element, Security::AccessToken::AccessTokenID callerToken) 650{ 651 bool isForegroud = element.GetBundleName() == foregroundElement_.GetBundleName() && 652 element.GetAbilityName() == foregroundElement_.GetAbilityName(); 653 if (isForegroud) { 654 ClearHceInfo(); 655 InfoLog("StopHce: refresh route table"); 656 ConfigRoutingAndCommit(); 657 } 658 return hostCardEmulationManager_->UnRegAllCallback(callerToken); 659} 660 661bool CeService::HandleWhenRemoteDie(Security::AccessToken::AccessTokenID callerToken) 662{ 663 Security::AccessToken::HapTokenInfo hapTokenInfo; 664 int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callerToken, hapTokenInfo); 665 666 InfoLog("get hap token info, result = %{public}d", result); 667 if (result) { 668 return false; 669 } 670 if (hapTokenInfo.bundleName.empty()) { 671 ErrorLog("HandleWhenRemoteDie: not got bundle name"); 672 return false; 673 } 674 675 bool isForegroud = hapTokenInfo.bundleName == foregroundElement_.GetBundleName(); 676 if (isForegroud) { 677 ClearHceInfo(); 678 InfoLog("remote die: refresh route table"); 679 ConfigRoutingAndCommit(); 680 } 681 return hostCardEmulationManager_->UnRegAllCallback(callerToken); 682} 683 684void CeService::NotifyDefaultPaymentType(int paymentType) 685{ 686 InfoLog("NotifyDefaultPaymentType: %{public}d", paymentType); 687 if (nciCeProxy_.expired()) { 688 ErrorLog("NotifyDefaultPaymentType: nciCeProxy_ is nullptr."); 689 return; 690 } 691 nciCeProxy_.lock()->NotifyDefaultPaymentType(paymentType); 692} 693 694void CeService::HandleDataShareReady() 695{ 696 // when bundle manager init done, when recv the event of dataShareReady; 697 // app list and default payment app need to get from bundle manager, need init if not been initialized. 698 InfoLog("HandleDataShareReady: Init app list and DefaultPayment App"); 699 ExternalDepsProxy::GetInstance().InitAppList(); 700 if (InitDefaultPaymentApp()) { 701 ConfigRoutingAndCommit(); 702 } 703} 704} // namespace NFC 705} // namespace OHOS