1bc2ed2b3Sopenharmony_ci/* 2bc2ed2b3Sopenharmony_ci * Copyright (c) 2022 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#include "nfc_sa_manager.h" 16bc2ed2b3Sopenharmony_ci#include "loghelper.h" 17bc2ed2b3Sopenharmony_ci#include "system_ability_definition.h" 18bc2ed2b3Sopenharmony_ci#include "external_deps_proxy.h" 19bc2ed2b3Sopenharmony_ci 20bc2ed2b3Sopenharmony_cinamespace OHOS { 21bc2ed2b3Sopenharmony_cinamespace NFC { 22bc2ed2b3Sopenharmony_ciconst bool REGISTER_RESULT = 23bc2ed2b3Sopenharmony_ci SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NfcSaManager>::GetInstance().get()); 24bc2ed2b3Sopenharmony_ci 25bc2ed2b3Sopenharmony_ciNfcSaManager::NfcSaManager() : SystemAbility(NFC_MANAGER_SYS_ABILITY_ID, false) {} 26bc2ed2b3Sopenharmony_ci 27bc2ed2b3Sopenharmony_ciNfcSaManager::~NfcSaManager() 28bc2ed2b3Sopenharmony_ci{ 29bc2ed2b3Sopenharmony_ci if (nfcService_) { 30bc2ed2b3Sopenharmony_ci nfcService_ = nullptr; 31bc2ed2b3Sopenharmony_ci } 32bc2ed2b3Sopenharmony_ci} 33bc2ed2b3Sopenharmony_ci 34bc2ed2b3Sopenharmony_civoid NfcSaManager::OnStart() 35bc2ed2b3Sopenharmony_ci{ 36bc2ed2b3Sopenharmony_ci if (state_ == ServiceRunningState::STATE_RUNNING) { 37bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager has already started."); 38bc2ed2b3Sopenharmony_ci return; 39bc2ed2b3Sopenharmony_ci } 40bc2ed2b3Sopenharmony_ci 41bc2ed2b3Sopenharmony_ci if (!Init()) { 42bc2ed2b3Sopenharmony_ci InfoLog("failed to init NfcSaManager"); 43bc2ed2b3Sopenharmony_ci // record init sa failed event. 44bc2ed2b3Sopenharmony_ci NfcFailedParams err; 45bc2ed2b3Sopenharmony_ci ExternalDepsProxy::GetInstance().BuildFailedParams(err, MainErrorCode::INIT_SA_FAILED, 46bc2ed2b3Sopenharmony_ci SubErrorCode::DEFAULT_ERR_DEF); 47bc2ed2b3Sopenharmony_ci ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(&err); 48bc2ed2b3Sopenharmony_ci return; 49bc2ed2b3Sopenharmony_ci } 50bc2ed2b3Sopenharmony_ci state_ = ServiceRunningState::STATE_RUNNING; 51bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager::OnStart start service success."); 52bc2ed2b3Sopenharmony_ci} 53bc2ed2b3Sopenharmony_ci 54bc2ed2b3Sopenharmony_cibool NfcSaManager::Init() 55bc2ed2b3Sopenharmony_ci{ 56bc2ed2b3Sopenharmony_ci std::lock_guard<std::mutex> guard(initMutex_); 57bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager::Init ready to init."); 58bc2ed2b3Sopenharmony_ci if (!registerToService_) { 59bc2ed2b3Sopenharmony_ci nfcService_ = std::make_shared<NfcService>(); 60bc2ed2b3Sopenharmony_ci nfcService_->Initialize(); 61bc2ed2b3Sopenharmony_ci bool ret = Publish(nfcService_->nfcControllerImpl_); 62bc2ed2b3Sopenharmony_ci if (ret) { 63bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager::Init Add System Ability SUCCESS!"); 64bc2ed2b3Sopenharmony_ci } else { 65bc2ed2b3Sopenharmony_ci ErrorLog("NfcSaManager::Init Add System Ability FAILED!"); 66bc2ed2b3Sopenharmony_ci return false; 67bc2ed2b3Sopenharmony_ci } 68bc2ed2b3Sopenharmony_ci AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID); 69bc2ed2b3Sopenharmony_ci registerToService_ = true; 70bc2ed2b3Sopenharmony_ci } 71bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager::Init init success."); 72bc2ed2b3Sopenharmony_ci return true; 73bc2ed2b3Sopenharmony_ci} 74bc2ed2b3Sopenharmony_ci 75bc2ed2b3Sopenharmony_civoid NfcSaManager::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) 76bc2ed2b3Sopenharmony_ci{ 77bc2ed2b3Sopenharmony_ci InfoLog("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId); 78bc2ed2b3Sopenharmony_ci if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { 79bc2ed2b3Sopenharmony_ci InfoLog("OnAddSystemAbility systemAbilityId is not COMMON_EVENT_SERVICE_ID"); 80bc2ed2b3Sopenharmony_ci return; 81bc2ed2b3Sopenharmony_ci } 82bc2ed2b3Sopenharmony_ci InfoLog("Start to resubscribe common event."); 83bc2ed2b3Sopenharmony_ci if (nfcService_ == nullptr) { 84bc2ed2b3Sopenharmony_ci ErrorLog("nfcService_ is nullptr"); 85bc2ed2b3Sopenharmony_ci return; 86bc2ed2b3Sopenharmony_ci } 87bc2ed2b3Sopenharmony_ci if (nfcService_->eventHandler_ == nullptr) { 88bc2ed2b3Sopenharmony_ci ErrorLog("eventHandler_ is nullptr"); 89bc2ed2b3Sopenharmony_ci return; 90bc2ed2b3Sopenharmony_ci } 91bc2ed2b3Sopenharmony_ci nfcService_->eventHandler_->SubscribePackageChangedEvent(); 92bc2ed2b3Sopenharmony_ci nfcService_->eventHandler_->SubscribeScreenChangedEvent(); 93bc2ed2b3Sopenharmony_ci nfcService_->eventHandler_->SubscribeShutdownEvent(); 94bc2ed2b3Sopenharmony_ci} 95bc2ed2b3Sopenharmony_ci 96bc2ed2b3Sopenharmony_civoid NfcSaManager::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) 97bc2ed2b3Sopenharmony_ci{ 98bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager OnRemoveSystemAbility finish"); 99bc2ed2b3Sopenharmony_ci} 100bc2ed2b3Sopenharmony_ci 101bc2ed2b3Sopenharmony_civoid NfcSaManager::OnStop() 102bc2ed2b3Sopenharmony_ci{ 103bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager::OnStop ready to stop service."); 104bc2ed2b3Sopenharmony_ci state_ = ServiceRunningState::STATE_NOT_START; 105bc2ed2b3Sopenharmony_ci registerToService_ = false; 106bc2ed2b3Sopenharmony_ci InfoLog("NfcSaManager::OnStop stop service success."); 107bc2ed2b3Sopenharmony_ci} 108bc2ed2b3Sopenharmony_ci} // namespace NFC 109bc2ed2b3Sopenharmony_ci} // namespace OHOS 110