• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 if (foregroundData_->isEnabled_) {
73         params->SetTechMask(foregroundData_->techMask_);
74         params->SetEnableReaderMode(true);
75     } else {
76         params->SetTechMask(NfcPollingParams::NFC_POLL_DEFAULT);
77         params->SetEnableReaderMode(false);
78     }
79     return params;
80 }
81 
StartPollingLoop(bool force)82 void NfcPollingManager::StartPollingLoop(bool force)
83 {
84     InfoLog("StartPollingLoop force = %{public}d", force);
85     if (nfcService_.expired()) {
86         ErrorLog("StartPollingLoop: nfcService_ is nullptr.");
87         return;
88     }
89     if (!nfcService_.lock()->IsNfcEnabled()) {
90         ErrorLog("StartPollingLoop: NFC not enabled, do not Compute Routing Params.");
91         return;
92     }
93     std::lock_guard<std::mutex> lock(mutex_);
94 
95     NfcWatchDog pollingWatchDog("StartPollingLoop", WAIT_MS_SET_ROUTE, nciNfccProxy_);
96     pollingWatchDog.Run();
97     // Compute new polling parameters
98     std::shared_ptr<NfcPollingParams> newParams = GetPollingParameters(screenState_);
99     InfoLog("newParams: %{public}s", newParams->ToString().c_str());
100     InfoLog("currParams: %{public}s", currPollingParams_->ToString().c_str());
101     if (force || !(newParams == currPollingParams_)) {
102         if (newParams->ShouldEnablePolling()) {
103             bool shouldRestart = currPollingParams_->ShouldEnablePolling();
104             InfoLog("StartPollingLoop shouldRestart = %{public}d", shouldRestart);
105 
106             nciNfccProxy_.lock()->EnableDiscovery(newParams->GetTechMask(),
107                                                   newParams->ShouldEnableReaderMode(),
108                                                   newParams->ShouldEnableHostRouting(),
109                                                   shouldRestart || force);
110         } else {
111             nciNfccProxy_.lock()->DisableDiscovery();
112         }
113         currPollingParams_ = newParams;
114     } else {
115         InfoLog("StartPollingLoop: polling params equal, not updating");
116     }
117     pollingWatchDog.Cancel();
118 }
119 
HandleScreenChanged(int screenState)120 void NfcPollingManager::HandleScreenChanged(int screenState)
121 {
122     std::lock_guard<std::mutex> lock(mutex_);
123     screenState_ = screenState;
124     DebugLog("Screen changed screenState %{public}d", screenState_);
125     nciNfccProxy_.lock()->SetScreenStatus(screenState_);
126 }
127 
HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data)128 bool NfcPollingManager::HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data)
129 {
130     std::lock_guard<std::mutex> lock(mutex_);
131     std::string action = data->GetWant().GetAction();
132     if (action.empty()) {
133         ErrorLog("action is empty");
134         return false;
135     }
136     if ((action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) ||
137         (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED)) {
138         return ExternalDepsProxy::GetInstance().HandleAppAddOrChangedEvent(data);
139     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
140         return ExternalDepsProxy::GetInstance().HandleAppRemovedEvent(data);
141     } else {
142         DebugLog("not need event.");
143         return false;
144     }
145 }
146 
EnableForegroundDispatch(AppExecFwk::ElementName & element,const std::vector<uint32_t> & discTech,const sptr<KITS::IForegroundCallback> & callback)147 bool NfcPollingManager::EnableForegroundDispatch(AppExecFwk::ElementName &element,
148     const std::vector<uint32_t> &discTech, const sptr<KITS::IForegroundCallback> &callback)
149 {
150     if (nfcService_.expired() || nciTagProxy_.expired()) {
151         ErrorLog("EnableForegroundDispatch: nfcService_ is nullptr.");
152         return false;
153     }
154     if (!nfcService_.lock()->IsNfcEnabled()) {
155         ErrorLog("EnableForegroundDispatch: NFC not enabled, do not set foreground");
156         return false;
157     }
158     if (callback == nullptr) {
159         ErrorLog("EnableForegroundDispatch: ForegroundCallback invalid");
160         return false;
161     }
162     bool isDisablePolling = (discTech.size() == 0);
163     DebugLog("EnableForegroundDispatch: element: %{public}s/%{public}s",
164         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
165     if (!isDisablePolling) {
166         foregroundData_->isEnabled_ = true;
167         foregroundData_->techMask_ = nciTagProxy_.lock()->GetTechMaskFromTechList(discTech);
168         foregroundData_->element_ = element;
169         foregroundData_->callback_ = callback;
170     }
171     StartPollingLoop(true);
172     return true;
173 }
174 
DisableForegroundDispatch(const AppExecFwk::ElementName & element)175 bool NfcPollingManager::DisableForegroundDispatch(const AppExecFwk::ElementName &element)
176 {
177     DebugLog("DisableForegroundDispatch: element: %{public}s/%{public}s",
178         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
179     foregroundData_->isEnabled_ = false;
180     foregroundData_->techMask_ = 0xFFFF;
181     foregroundData_->callerToken_ = 0;
182     foregroundData_->callback_ = nullptr;
183 
184     StartPollingLoop(true);
185     return true;
186 }
187 
DisableForegroundByDeathRcpt()188 bool NfcPollingManager::DisableForegroundByDeathRcpt()
189 {
190     return DisableForegroundDispatch(foregroundData_->element_);
191 }
192 
IsForegroundEnabled()193 bool NfcPollingManager::IsForegroundEnabled()
194 {
195     return foregroundData_->isEnabled_;
196 }
197 
SendTagToForeground(KITS::TagInfoParcelable * tagInfo)198 void NfcPollingManager::SendTagToForeground(KITS::TagInfoParcelable* tagInfo)
199 {
200     if (!IsForegroundEnabled() || foregroundData_->callback_ == nullptr) {
201         ErrorLog("SendTagToForeground: invalid foreground state");
202         return;
203     }
204     DebugLog("SendTagToForeground: OnTagDiscovered, tagInfo = %{public}s", tagInfo->ToString().c_str());
205     foregroundData_->callback_->OnTagDiscovered(tagInfo);
206 }
207 
EnableReaderMode(AppExecFwk::ElementName & element,std::vector<uint32_t> & discTech,const sptr<KITS::IReaderModeCallback> & callback)208 bool NfcPollingManager::EnableReaderMode(AppExecFwk::ElementName &element, std::vector<uint32_t> &discTech,
209     const sptr<KITS::IReaderModeCallback> &callback)
210 {
211     if (nfcService_.expired() || nciTagProxy_.expired()) {
212         ErrorLog("EnableReaderMode: nfcService_ is nullptr.");
213         return false;
214     }
215     if (!nfcService_.lock()->IsNfcEnabled()) {
216         ErrorLog("EnableReaderMode: NFC not enabled, do not set reader mode");
217         return false;
218     }
219     if (callback == nullptr) {
220         ErrorLog("EnableReaderMode: ReaderModeCallback invalid");
221         return false;
222     }
223     bool isDisablePolling = (discTech.size() == 0);
224     DebugLog("EnableReaderMode: element: %{public}s/%{public}s",
225         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
226     if (!isDisablePolling) {
227         readerModeData_->isEnabled_ = true;
228         readerModeData_->techMask_ = nciTagProxy_.lock()->GetTechMaskFromTechList(discTech);
229         readerModeData_->element_ = element;
230         readerModeData_->callback_ = callback;
231     }
232     StartPollingLoop(true);
233     return true;
234 }
235 
DisableReaderMode(AppExecFwk::ElementName & element)236 bool NfcPollingManager::DisableReaderMode(AppExecFwk::ElementName &element)
237 {
238     DebugLog("DisableReaderMode: element: %{public}s/%{public}s",
239         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
240     readerModeData_->isEnabled_ = false;
241     readerModeData_->techMask_ = 0xFFFF;
242     readerModeData_->callerToken_ = 0;
243     readerModeData_->callback_ = nullptr;
244 
245     StartPollingLoop(true);
246     return true;
247 }
248 
DisableReaderModeByDeathRcpt()249 bool NfcPollingManager::DisableReaderModeByDeathRcpt()
250 {
251     return DisableReaderMode(readerModeData_->element_);
252 }
253 
IsReaderModeEnabled()254 bool NfcPollingManager::IsReaderModeEnabled()
255 {
256     return readerModeData_->isEnabled_;
257 }
258 
SendTagToReaderApp(KITS::TagInfoParcelable * tagInfo)259 void NfcPollingManager::SendTagToReaderApp(KITS::TagInfoParcelable* tagInfo)
260 {
261     if (!IsReaderModeEnabled() || readerModeData_->callback_ == nullptr) {
262         ErrorLog("SendTagToReaderApp: invalid readermode state");
263         return;
264     }
265     DebugLog("SendTagToReaderApp: OnTagDiscovered, tagInfo = %{public}s", tagInfo->ToString().c_str());
266     readerModeData_->callback_->OnTagDiscovered(tagInfo);
267 }
268 } // namespace NFC
269 } // namespace OHOS