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 #include "nfc_polling_manager.h"
16 #include "common_event_support.h"
17 #include "loghelper.h"
18 #include "nfc_service.h"
19 #include "nfc_watch_dog.h"
20 #include "external_deps_proxy.h"
21
22 namespace OHOS {
23 namespace NFC {
NfcPollingManager(std::weak_ptr<NfcService> nfcService,std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy,std::weak_ptr<NCI::INciTagInterface> nciTagProxy)24 NfcPollingManager::NfcPollingManager(std::weak_ptr<NfcService> nfcService,
25 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy,
26 std::weak_ptr<NCI::INciTagInterface> nciTagProxy)
27 : nfcService_(nfcService), nciNfccProxy_(nciNfccProxy), nciTagProxy_(nciTagProxy)
28 {
29 foregroundData_ = std::make_shared<NfcPollingManager::ForegroundRegistryData>();
30 readerModeData_ = std::make_shared<NfcPollingManager::ReaderModeRegistryData>();
31 currPollingParams_ = NfcPollingParams::GetNfcOffParameters();
32 }
33
~NfcPollingManager()34 NfcPollingManager::~NfcPollingManager()
35 {
36 foregroundData_ = nullptr;
37 readerModeData_ = nullptr;
38 currPollingParams_ = nullptr;
39 }
40
ResetCurrPollingParams()41 void NfcPollingManager::ResetCurrPollingParams()
42 {
43 currPollingParams_ = std::make_shared<NfcPollingParams>();
44 }
45
GetForegroundData()46 std::shared_ptr<NfcPollingManager::ForegroundRegistryData> NfcPollingManager::GetForegroundData()
47 {
48 std::lock_guard<std::mutex> lock(mutex_);
49 return foregroundData_;
50 }
51
GetReaderModeData()52 std::shared_ptr<NfcPollingManager::ReaderModeRegistryData> NfcPollingManager::GetReaderModeData()
53 {
54 std::lock_guard<std::mutex> lock(mutex_);
55 return readerModeData_;
56 }
57
GetCurrentParameters()58 std::shared_ptr<NfcPollingParams> NfcPollingManager::GetCurrentParameters()
59 {
60 std::lock_guard<std::mutex> lock(mutex_);
61 return currPollingParams_;
62 }
63
GetPollingParameters(int screenState)64 std::shared_ptr<NfcPollingParams> NfcPollingManager::GetPollingParameters(int screenState)
65 {
66 // Recompute polling parameters based on screen state
67 std::shared_ptr<NfcPollingParams> params = std::make_shared<NfcPollingParams>();
68
69 if (readerModeData_->isEnabled_) {
70 params->SetTechMask(readerModeData_->techMask_);
71 params->SetEnableReaderMode(true);
72 } else {
73 params->SetTechMask(NfcPollingParams::NFC_POLL_DEFAULT);
74 params->SetEnableReaderMode(false);
75 }
76 return params;
77 }
78
StartPollingLoop(bool force)79 void NfcPollingManager::StartPollingLoop(bool force)
80 {
81 InfoLog("StartPollingLoop force = %{public}d", force);
82 if (nfcService_.expired()) {
83 ErrorLog("StartPollingLoop: nfcService_ is nullptr.");
84 return;
85 }
86 if (!nfcService_.lock()->IsNfcEnabled()) {
87 ErrorLog("StartPollingLoop: NFC not enabled, do not Compute Routing Params.");
88 return;
89 }
90 std::lock_guard<std::mutex> lock(mutex_);
91
92 NfcWatchDog pollingWatchDog("StartPollingLoop", WAIT_MS_SET_ROUTE, nciNfccProxy_);
93 pollingWatchDog.Run();
94 // Compute new polling parameters
95 std::shared_ptr<NfcPollingParams> newParams = GetPollingParameters(screenState_);
96 InfoLog("newParams: %{public}s", newParams->ToString().c_str());
97 InfoLog("currParams: %{public}s", currPollingParams_->ToString().c_str());
98 if ((force || !(newParams == currPollingParams_)) && !nciNfccProxy_.expired()) {
99 if (newParams->ShouldEnablePolling()) {
100 bool shouldRestart = currPollingParams_->ShouldEnablePolling();
101 InfoLog("StartPollingLoop shouldRestart = %{public}d", shouldRestart);
102
103 nciNfccProxy_.lock()->EnableDiscovery(newParams->GetTechMask(),
104 newParams->ShouldEnableReaderMode(),
105 newParams->ShouldEnableHostRouting(),
106 shouldRestart || force);
107 } else {
108 nciNfccProxy_.lock()->DisableDiscovery();
109 }
110 currPollingParams_ = newParams;
111 } else {
112 InfoLog("StartPollingLoop: polling params equal, not updating");
113 }
114 pollingWatchDog.Cancel();
115 }
116
HandleScreenChanged(int screenState)117 void NfcPollingManager::HandleScreenChanged(int screenState)
118 {
119 std::lock_guard<std::mutex> lock(mutex_);
120 screenState_ = screenState;
121 InfoLog("Screen changed screenState %{public}d", screenState_);
122 if (nciTagProxy_.expired() || nciNfccProxy_.expired()) {
123 ErrorLog("nci proxy nullptr");
124 return;
125 }
126 nciTagProxy_.lock()->StopFieldChecking();
127 nciNfccProxy_.lock()->SetScreenStatus(screenState_);
128 }
129
HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data)130 bool NfcPollingManager::HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data)
131 {
132 std::lock_guard<std::mutex> lock(mutex_);
133 if (data == nullptr) {
134 ErrorLog("data is null");
135 return false;
136 }
137 std::string action = data->GetWant().GetAction();
138 if (action.empty()) {
139 ErrorLog("action is empty");
140 return false;
141 }
142 if ((action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) ||
143 (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED)) {
144 return ExternalDepsProxy::GetInstance().HandleAppAddOrChangedEvent(data);
145 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
146 return ExternalDepsProxy::GetInstance().HandleAppRemovedEvent(data);
147 } else {
148 DebugLog("not need event.");
149 return false;
150 }
151 }
152
EnableForegroundDispatch(AppExecFwk::ElementName & element,const std::vector<uint32_t> & discTech,const sptr<KITS::IForegroundCallback> & callback)153 bool NfcPollingManager::EnableForegroundDispatch(AppExecFwk::ElementName &element,
154 const std::vector<uint32_t> &discTech, const sptr<KITS::IForegroundCallback> &callback)
155 {
156 if (nfcService_.expired() || nciTagProxy_.expired()) {
157 ErrorLog("EnableForegroundDispatch: nfcService_ is nullptr.");
158 return false;
159 }
160 if (!nfcService_.lock()->IsNfcEnabled()) {
161 ErrorLog("EnableForegroundDispatch: NFC not enabled, do not set foreground");
162 return false;
163 }
164 if (callback == nullptr) {
165 ErrorLog("EnableForegroundDispatch: ForegroundCallback invalid");
166 return false;
167 }
168 bool isDisablePolling = (discTech.size() == 0);
169 DebugLog("EnableForegroundDispatch: element: %{public}s/%{public}s",
170 element.GetBundleName().c_str(), element.GetAbilityName().c_str());
171 if (!isDisablePolling) {
172 foregroundData_->isEnabled_ = true;
173 foregroundData_->techMask_ = nciTagProxy_.lock()->GetTechMaskFromTechList(discTech);
174 foregroundData_->element_ = element;
175 foregroundData_->callback_ = callback;
176 nciNfccProxy_.lock()->NotifyMessageToVendor(KITS::FOREGROUND_APP_KEY, element.GetBundleName());
177 }
178 return true;
179 }
180
DisableForegroundDispatch(const AppExecFwk::ElementName & element)181 bool NfcPollingManager::DisableForegroundDispatch(const AppExecFwk::ElementName &element)
182 {
183 DebugLog("DisableForegroundDispatch: element: %{public}s/%{public}s",
184 element.GetBundleName().c_str(), element.GetAbilityName().c_str());
185 foregroundData_->isEnabled_ = false;
186 foregroundData_->techMask_ = 0xFFFF;
187 foregroundData_->callerToken_ = 0;
188 foregroundData_->callback_ = nullptr;
189 if (nciNfccProxy_.expired()) {
190 ErrorLog("DisableForegroundDispatch: nciNfccProxy_ is nullptr.");
191 return false;
192 }
193 nciNfccProxy_.lock()->NotifyMessageToVendor(KITS::FOREGROUND_APP_KEY, "");
194 return true;
195 }
196
DisableForegroundByDeathRcpt()197 bool NfcPollingManager::DisableForegroundByDeathRcpt()
198 {
199 return DisableForegroundDispatch(foregroundData_->element_);
200 }
201
IsForegroundEnabled()202 bool NfcPollingManager::IsForegroundEnabled()
203 {
204 return foregroundData_->isEnabled_;
205 }
206
SendTagToForeground(KITS::TagInfoParcelable * tagInfo)207 void NfcPollingManager::SendTagToForeground(KITS::TagInfoParcelable* tagInfo)
208 {
209 if (!IsForegroundEnabled() || foregroundData_->callback_ == nullptr) {
210 ErrorLog("SendTagToForeground: invalid foreground state");
211 return;
212 }
213 DebugLog("SendTagToForeground: OnTagDiscovered, tagInfo = %{public}s", tagInfo->ToString().c_str());
214 foregroundData_->callback_->OnTagDiscovered(tagInfo);
215 }
216
EnableReaderMode(AppExecFwk::ElementName & element,std::vector<uint32_t> & discTech,const sptr<KITS::IReaderModeCallback> & callback)217 bool NfcPollingManager::EnableReaderMode(AppExecFwk::ElementName &element, std::vector<uint32_t> &discTech,
218 const sptr<KITS::IReaderModeCallback> &callback)
219 {
220 if (nfcService_.expired() || nciTagProxy_.expired()) {
221 ErrorLog("EnableReaderMode: nfcService_ or nciTagProxy_ is nullptr.");
222 return false;
223 }
224 if (!nfcService_.lock()->IsNfcEnabled()) {
225 ErrorLog("EnableReaderMode: NFC not enabled, do not set reader mode");
226 return false;
227 }
228 if (callback == nullptr) {
229 ErrorLog("EnableReaderMode: ReaderModeCallback invalid");
230 return false;
231 }
232 bool isDisablePolling = (discTech.size() == 0);
233 DebugLog("EnableReaderMode: element: %{public}s/%{public}s",
234 element.GetBundleName().c_str(), element.GetAbilityName().c_str());
235 if (!isDisablePolling) {
236 readerModeData_->isEnabled_ = true;
237 readerModeData_->techMask_ = nciTagProxy_.lock()->GetTechMaskFromTechList(discTech);
238 readerModeData_->element_ = element;
239 readerModeData_->callback_ = callback;
240 nciNfccProxy_.lock()->NotifyMessageToVendor(KITS::READERMODE_APP_KEY, element.GetBundleName());
241 }
242 nciTagProxy_.lock()->StopFieldChecking();
243 StartPollingLoop(true);
244 return true;
245 }
246
DisableReaderMode(AppExecFwk::ElementName & element)247 bool NfcPollingManager::DisableReaderMode(AppExecFwk::ElementName &element)
248 {
249 DebugLog("DisableReaderMode: element: %{public}s/%{public}s",
250 element.GetBundleName().c_str(), element.GetAbilityName().c_str());
251 readerModeData_->isEnabled_ = false;
252 readerModeData_->techMask_ = 0xFFFF;
253 readerModeData_->callerToken_ = 0;
254 readerModeData_->callback_ = nullptr;
255 if (!nciTagProxy_.expired()) {
256 nciTagProxy_.lock()->StopFieldChecking();
257 }
258 StartPollingLoop(true);
259 if (!nciNfccProxy_.expired()) {
260 nciNfccProxy_.lock()->NotifyMessageToVendor(KITS::READERMODE_APP_KEY, "");
261 }
262 return true;
263 }
264
DisableReaderModeByDeathRcpt()265 bool NfcPollingManager::DisableReaderModeByDeathRcpt()
266 {
267 return DisableReaderMode(readerModeData_->element_);
268 }
269
IsReaderModeEnabled()270 bool NfcPollingManager::IsReaderModeEnabled()
271 {
272 return readerModeData_->isEnabled_;
273 }
274
SendTagToReaderApp(KITS::TagInfoParcelable * tagInfo)275 void NfcPollingManager::SendTagToReaderApp(KITS::TagInfoParcelable* tagInfo)
276 {
277 if (!IsReaderModeEnabled() || readerModeData_->callback_ == nullptr) {
278 ErrorLog("SendTagToReaderApp: invalid readermode state");
279 return;
280 }
281 DebugLog("SendTagToReaderApp: OnTagDiscovered, tagInfo = %{public}s", tagInfo->ToString().c_str());
282 readerModeData_->callback_->OnTagDiscovered(tagInfo);
283 }
284 } // namespace NFC
285 } // namespace OHOS