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