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 "nfc_sa_client.h"
17
18 #include "iremote_broker.h"
19 #include "iremote_object.h"
20 #include "iservice_registry.h"
21 #include "infc_controller_callback.h"
22 #include "loghelper.h"
23
24 namespace OHOS {
25 namespace NFC {
26 static constexpr int32_t NFC_LOADSA_TIMEOUT_MS = 1000; // ms
27
GetInstance()28 NfcSaClient &NfcSaClient::GetInstance()
29 {
30 DebugLog("NfcSaClient::%{public}s enter", __func__);
31 static NfcSaClient nfcSaClient;
32 return nfcSaClient;
33 }
34
LoadNfcSa(int32_t systemAbilityId)35 sptr<IRemoteObject> NfcSaClient::LoadNfcSa(int32_t systemAbilityId)
36 {
37 DebugLog("NfcSaClient::%{public}s enter, systemAbilityId [%{public}d] loading", __func__, systemAbilityId);
38 InitLoadState();
39 sptr<ISystemAbilityManager> samgr =
40 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41 if (samgr == nullptr) {
42 ErrorLog("NfcSaClient::%{public}s get system ability manager failed!", __func__);
43 return nullptr;
44 }
45 auto object = samgr->CheckSystemAbility(systemAbilityId);
46 if (object != nullptr) {
47 InfoLog("NfcSaClient::%{public}s CheckSystemAbility systemAbilityId [%{public}d] SUCCESS",
48 __func__, systemAbilityId);
49 remoteObject_ = object;
50 return remoteObject_;
51 }
52
53 auto nfcSaLoadCallback = sptr<NfcSaLoadCallback>(new NfcSaLoadCallback());
54 int32_t ret = samgr->LoadSystemAbility(systemAbilityId, nfcSaLoadCallback);
55 if (ret != ERR_NONE) {
56 ErrorLog("NfcSaClient::%{public}s LoadSystemAbility systemAbilityId [%{public}d] FAILED, ret %{public}d",
57 __func__, systemAbilityId, ret);
58 return nullptr;
59 }
60 if (WaitLoadStateChange(systemAbilityId)) {
61 InfoLog("NfcSaClient::%{public}s LoadSystemAbility systemAbilityId [%{public}d] SUCCESS",
62 __func__, systemAbilityId);
63 return remoteObject_;
64 }
65 ErrorLog("NfcSaClient::%{public}s LoadSystemAbility systemAbilityId [%{public}d] FAILED",
66 __func__, systemAbilityId);
67 return nullptr;
68 }
69
InitLoadState()70 void NfcSaClient::InitLoadState()
71 {
72 std::unique_lock<std::mutex> lock(locatorMutex_);
73 loadState_ = false;
74 }
75
WaitLoadStateChange(int32_t systemAbilityId)76 bool NfcSaClient::WaitLoadStateChange(int32_t systemAbilityId)
77 {
78 std::unique_lock<std::mutex> lock(locatorMutex_);
79 auto wait = locatorCond_.wait_for(lock, std::chrono::milliseconds(NFC_LOADSA_TIMEOUT_MS), [this] {
80 return loadState_ == true;
81 });
82 if (!wait) {
83 ErrorLog("NfcSaClient::%{public}s locator sa [%{public}d] time out.", __func__, systemAbilityId);
84 return false;
85 }
86 return true;
87 }
88
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)89 void NfcSaClient::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
90 {
91 std::unique_lock<std::mutex> lock(locatorMutex_);
92 loadState_ = true;
93 remoteObject_ = remoteObject;
94 locatorCond_.notify_one();
95 }
96
LoadSystemAbilityFail()97 void NfcSaClient::LoadSystemAbilityFail()
98 {
99 std::unique_lock<std::mutex> lock(locatorMutex_);
100 loadState_ = false;
101 locatorCond_.notify_one();
102 }
103
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)104 void NfcSaLoadCallback::OnLoadSystemAbilitySuccess(
105 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
106 {
107 DebugLog("NfcSaClient Load SA success, systemAbilityId = [%{public}d]", systemAbilityId);
108 NfcSaClient::GetInstance().LoadSystemAbilitySuccess(remoteObject);
109 }
110
OnLoadSystemAbilityFail(int32_t systemAbilityId)111 void NfcSaLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
112 {
113 DebugLog("NfcSaClient Load SA failed, systemAbilityId = [%{public}d]", systemAbilityId);
114 NfcSaClient::GetInstance().LoadSystemAbilityFail();
115 }
116 } // namespace NFC
117 } // namespace OHOS