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_service.h"
16 #include <unistd.h>
17 #include "app_data_parser.h"
18 #include "infc_controller_callback.h"
19 #include "iservice_registry.h"
20 #include "loghelper.h"
21 #include "nfc_preferences.h"
22 #include "nfc_event_handler.h"
23 #include "nfc_event_publisher.h"
24 #include "nfc_hisysevent.h"
25 #include "nfc_polling_params.h"
26 #include "nfc_sdk_common.h"
27 #include "nfc_timer.h"
28 #include "nfc_watch_dog.h"
29 #include "tag_session.h"
30 #include "external_deps_proxy.h"
31 #include "want.h"
32 #include "nci_nfcc_proxy.h"
33 #include "nci_tag_proxy.h"
34 #include "nci_ce_proxy.h"
35 #include "hce_session.h"
36
37 namespace OHOS {
38 namespace NFC {
39 const std::u16string NFC_SERVICE_NAME = OHOS::to_utf16("ohos.nfc.service");
40 uint32_t NfcService::unloadStaSaTimerId{0};
41
NfcService()42 NfcService::NfcService()
43 : eventHandler_(nullptr),
44 tagDispatcher_(nullptr),
45 nfcControllerImpl_(nullptr),
46 nfcState_(KITS::STATE_OFF)
47 {
48 }
49
~NfcService()50 NfcService::~NfcService()
51 {
52 nfcControllerImpl_ = nullptr;
53 nfcPollingManager_ = nullptr;
54 nfcRoutingManager_ = nullptr;
55 }
56
GetInstance() const57 std::weak_ptr<NfcService> NfcService::GetInstance() const
58 {
59 return nfcService_;
60 }
61
GetNciNfccProxy(void)62 std::weak_ptr<NCI::INciNfccInterface> NfcService::GetNciNfccProxy(void)
63 {
64 return nciNfccProxy_;
65 }
66
GetNciTagProxy(void)67 std::weak_ptr<NCI::INciTagInterface> NfcService::GetNciTagProxy(void)
68 {
69 return nciTagProxy_;
70 }
71
GetNfcPollingManager()72 std::weak_ptr<NfcPollingManager> NfcService::GetNfcPollingManager()
73 {
74 return nfcPollingManager_;
75 }
76
GetNfcRoutingManager()77 std::weak_ptr<NfcRoutingManager> NfcService::GetNfcRoutingManager()
78 {
79 return nfcRoutingManager_;
80 }
81
GetCeService()82 std::weak_ptr<CeService> NfcService::GetCeService()
83 {
84 return ceService_;
85 }
86
GetSimVendorBundleName()87 std::string NfcService::GetSimVendorBundleName()
88 {
89 return nciCeProxy_->GetSimVendorBundleName();
90 }
91
Initialize()92 bool NfcService::Initialize()
93 {
94 nfcService_ = shared_from_this();
95 InfoLog("Nfc service initialize.");
96 nciNfccProxy_ = std::make_shared<NFC::NCI::NciNfccProxy>();
97 nciTagProxy_ = std::make_shared<NFC::NCI::NciTagProxy>();
98 nciCeProxy_ = std::make_shared<NFC::NCI::NciCeProxy>();
99 nciTagProxy_->SetTagListener(nfcService_);
100 nciCeProxy_->SetCeHostListener(nfcService_);
101
102 // inner message handler, used by other modules as initialization parameters
103 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("nfcservice::EventRunner");
104 eventHandler_ = std::make_shared<NfcEventHandler>(runner, shared_from_this());
105 nfcSwitchHandler_ = std::make_shared<NfcSwitchEventHandler>(
106 AppExecFwk::EventRunner::Create("NfcSwitchHandler", AppExecFwk::ThreadMode::FFRT), shared_from_this());
107 tagDispatcher_ = std::make_shared<TAG::TagDispatcher>(shared_from_this());
108 ceService_ = std::make_shared<CeService>(shared_from_this(), nciCeProxy_);
109
110 nfcPollingManager_ = std::make_shared<NfcPollingManager>(shared_from_this(), nciNfccProxy_, nciTagProxy_);
111 nfcRoutingManager_ = std::make_shared<NfcRoutingManager>(eventHandler_, nciNfccProxy_,
112 nciCeProxy_, shared_from_this());
113 tagSessionIface_ = new TAG::TagSession(shared_from_this());
114 hceSessionIface_ = new HCE::HceSession(shared_from_this());
115
116 // used by NfcSaManager::Init(), to public for the proxy.
117 nfcControllerImpl_ = new NfcControllerImpl(shared_from_this());
118 nfcPollingManager_->ResetCurrPollingParams();
119
120 runner->Run();
121 // NFC ROOT
122 ExecuteTask(KITS::TASK_INITIALIZE);
123 return true;
124 }
125
UnloadNfcSa()126 void NfcService::UnloadNfcSa()
127 {
128 #ifndef DTFUZZ_TEST // not for fuzz
129 InfoLog("%{public}s enter, systemAbilityId = [%{public}d] unloading", __func__, KITS::NFC_MANAGER_SYS_ABILITY_ID);
130 if (nfcState_ != KITS::STATE_OFF) {
131 InfoLog("%{public}s nfc state = [%{public}d] skip unload", __func__, nfcState_);
132 return;
133 }
134 sptr<ISystemAbilityManager> samgr =
135 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
136 if (samgr == nullptr) {
137 ErrorLog("%{public}s: get system ability manager failed!", __func__);
138 return;
139 }
140 int32_t ret = samgr->UnloadSystemAbility(KITS::NFC_MANAGER_SYS_ABILITY_ID);
141 if (ret != ERR_NONE) {
142 ErrorLog("%{public}s: Failed to unload system ability, SA Id = [%{public}d], ret = [%{public}d].",
143 __func__, KITS::NFC_MANAGER_SYS_ABILITY_ID, ret);
144 }
145 #endif
146 }
147
GetTagDispatcher()148 std::weak_ptr<TAG::TagDispatcher> NfcService::GetTagDispatcher()
149 {
150 return tagDispatcher_;
151 }
152
GetTagServiceIface()153 OHOS::sptr<IRemoteObject> NfcService::GetTagServiceIface()
154 {
155 return tagSessionIface_;
156 }
157
OnTagDiscovered(uint32_t tagDiscId)158 void NfcService::OnTagDiscovered(uint32_t tagDiscId)
159 {
160 InfoLog("NfcService::OnTagDiscovered tagDiscId %{public}d", tagDiscId);
161 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_TAG_FOUND), tagDiscId, 0);
162 InfoLog("NfcService::OnTagDiscovered end");
163 }
164
OnTagLost(uint32_t tagDiscId)165 void NfcService::OnTagLost(uint32_t tagDiscId)
166 {
167 InfoLog("NfcService::OnTagLost tagDiscId %{public}d", tagDiscId);
168 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_TAG_LOST), tagDiscId, 0);
169 }
170
FieldActivated()171 void NfcService::FieldActivated()
172 {
173 InfoLog("NfcService::FieldActivated");
174 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_FIELD_ACTIVATED));
175 }
176
FieldDeactivated()177 void NfcService::FieldDeactivated()
178 {
179 InfoLog("NfcService::FieldDeactivated");
180 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_FIELD_DEACTIVATED));
181 }
182
183 #ifdef VENDOR_APPLICATIONS_ENABLED
OnVendorEvent(int eventType,int arg1,std::string arg2)184 void NfcService::OnVendorEvent(int eventType, int arg1, std::string arg2)
185 {
186 InfoLog("NfcService::OnVendorEvent");
187 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_VENDOR_EVENT), eventType, 0);
188 }
189 #endif
190
OnCardEmulationData(const std::vector<uint8_t> & data)191 void NfcService::OnCardEmulationData(const std::vector<uint8_t> &data)
192 {
193 InfoLog("NfcService::OnCardEmulationData");
194 ceService_->OnCardEmulationData(data);
195 }
196
OnCardEmulationActivated()197 void NfcService::OnCardEmulationActivated()
198 {
199 InfoLog("NfcService::OnCardEmulationActivated");
200 ceService_->OnCardEmulationActivated();
201 }
202
GetHceServiceIface()203 OHOS::sptr<IRemoteObject> NfcService::GetHceServiceIface()
204 {
205 return hceSessionIface_;
206 }
207
OnCardEmulationDeactivated()208 void NfcService::OnCardEmulationDeactivated()
209 {
210 InfoLog("NfcService::OnCardEmulationDeactivated");
211 ceService_->OnCardEmulationDeactivated();
212 }
213
ExecuteTask(KITS::NfcTask param)214 int NfcService::ExecuteTask(KITS::NfcTask param)
215 {
216 if (nfcSwitchHandler_ == nullptr) {
217 ErrorLog("eventhandler nullptr.");
218 return KITS::ERR_NFC_STATE_INVALID;
219 }
220 InfoLog("executing task [%{public}d]", param);
221 nfcSwitchHandler_->RemoveAllEvents();
222 nfcSwitchHandler_->SendEvent(param);
223 return ERR_NONE;
224 }
225
NfcSwitchEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::weak_ptr<NfcService> service)226 NfcService::NfcSwitchEventHandler::NfcSwitchEventHandler(
227 const std::shared_ptr<AppExecFwk::EventRunner>& runner, std::weak_ptr<NfcService> service)
228 : EventHandler(runner), nfcService_(service)
229 {
230 }
231
~NfcSwitchEventHandler()232 NfcService::NfcSwitchEventHandler::~NfcSwitchEventHandler()
233 {
234 }
235
CheckNfcState(int param)236 bool NfcService::NfcSwitchEventHandler::CheckNfcState(int param)
237 {
238 if (nfcService_.expired()) {
239 ErrorLog("nfcService_ is nullptr.");
240 return false;
241 }
242 int nfcState = nfcService_.lock()->GetNfcState();
243 if (nfcState == KITS::STATE_TURNING_OFF || nfcState == KITS::STATE_TURNING_ON) {
244 WarnLog("Execute task %{public}d from bad state %{public}d", param, nfcState);
245 return false;
246 }
247 if (param == KITS::TASK_TURN_ON && nfcState == KITS::STATE_ON) {
248 WarnLog("NFC Turn On, already On");
249 ExternalDepsProxy::GetInstance().UpdateNfcState(KITS::STATE_ON);
250 return false;
251 }
252 if (param == KITS::TASK_TURN_OFF && nfcState == KITS::STATE_OFF) {
253 WarnLog("NFC Turn Off, already Off");
254 ExternalDepsProxy::GetInstance().UpdateNfcState(KITS::STATE_OFF);
255 return false;
256 }
257 return true;
258 }
259
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)260 void NfcService::NfcSwitchEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
261 {
262 if (event == nullptr) {
263 ErrorLog("event nullptr.");
264 return;
265 }
266 if (nfcService_.expired()) {
267 ErrorLog("nfc service expired.");
268 return;
269 }
270 int eventId = static_cast<int>(event->GetInnerEventId());
271 InfoLog("process eventid = [%{public}d]", eventId);
272 if (!CheckNfcState(eventId)) {
273 return;
274 }
275 switch (eventId) {
276 case KITS::TASK_INITIALIZE:
277 nfcService_.lock()->DoInitialize();
278 break;
279 case KITS::TASK_TURN_ON:
280 nfcService_.lock()->DoTurnOn();
281 break;
282 case KITS::TASK_TURN_OFF:
283 nfcService_.lock()->DoTurnOff();
284 break;
285 default:
286 WarnLog("ProcessEvent, unknown eventId %{public}d", eventId);
287 break;
288 }
289 InfoLog("process eventid finished.");
290 }
291
DoTurnOn()292 bool NfcService::DoTurnOn()
293 {
294 InfoLog("Nfc do turn on: current state %{public}d", nfcState_);
295
296 CancelUnloadNfcSaTimer();
297 UpdateNfcState(KITS::STATE_TURNING_ON);
298 NotifyMessageToVendor(KITS::NFC_SWITCH_KEY, std::to_string(KITS::STATE_TURNING_ON));
299 NfcWatchDog nfcWatchDog("DoTurnOn", WAIT_MS_INIT, nciNfccProxy_);
300 nfcWatchDog.Run();
301 // Routing WakeLock acquire
302 if (!nciNfccProxy_->Initialize()) {
303 ErrorLog("Nfc do turn on err");
304 UpdateNfcState(KITS::STATE_OFF);
305 // Routing Wake Lock release
306 nfcWatchDog.Cancel();
307 // Do turn on failed, openRequestCnt and openFailedCnt = 1, others = 0
308 ExternalDepsProxy::GetInstance().WriteOpenAndCloseHiSysEvent(DEFAULT_COUNT, DEFAULT_COUNT,
309 NOT_COUNT, NOT_COUNT);
310 // Record failed event
311 ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(MainErrorCode::NFC_OPEN_FAILED,
312 SubErrorCode::NCI_RESP_ERROR);
313 NotifyMessageToVendor(KITS::NFC_SWITCH_KEY, std::to_string(KITS::STATE_OFF));
314 return false;
315 }
316 // Routing Wake Lock release
317 nfcWatchDog.Cancel();
318
319 ceService_->Initialize();
320 nciVersion_ = nciNfccProxy_->GetNciVersion();
321 InfoLog("Get nci version: ver %{public}d", nciVersion_);
322
323 UpdateNfcState(KITS::STATE_ON);
324
325 NfcWatchDog nfcRoutingManagerDog("RoutingManager", WAIT_ROUTING_INIT, nciNfccProxy_);
326 nfcRoutingManagerDog.Run();
327 screenState_ = (int)eventHandler_->CheckScreenState();
328 nciNfccProxy_->SetScreenStatus(screenState_);
329
330 /* Start polling loop */
331 nfcPollingManager_->StartPollingLoop(true);
332 ceService_->InitConfigAidRouting(true);
333
334 nfcRoutingManager_->HandleComputeRoutingParams(static_cast<int>(ceService_->GetDefaultPaymentType()));
335 nfcRoutingManager_->HandleCommitRouting();
336 nfcRoutingManagerDog.Cancel();
337 // Do turn on success, openRequestCnt = 1, others = 0
338 ExternalDepsProxy::GetInstance().WriteOpenAndCloseHiSysEvent(DEFAULT_COUNT, NOT_COUNT, NOT_COUNT, NOT_COUNT);
339 // Record success event
340 ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(
341 MainErrorCode::NFC_OPEN_SUCCEED, SubErrorCode::DEFAULT_ERR_DEF);
342 NotifyMessageToVendor(KITS::NFC_SWITCH_KEY, std::to_string(KITS::STATE_ON));
343 InfoLog("Nfc do turn on successfully.");
344 return true;
345 }
346
DoTurnOff()347 bool NfcService::DoTurnOff()
348 {
349 InfoLog("Nfc do turn off: current state %{public}d", nfcState_);
350 UpdateNfcState(KITS::STATE_TURNING_OFF);
351 NotifyMessageToVendor(KITS::NFC_SWITCH_KEY, std::to_string(KITS::STATE_TURNING_OFF));
352
353 /* WatchDog to monitor for Deinitialize failed */
354 NfcWatchDog nfcWatchDog("DoTurnOff", WAIT_MS_SET_ROUTE, nciNfccProxy_);
355 nfcWatchDog.Run();
356
357 bool result = nciNfccProxy_->Deinitialize();
358 InfoLog("Nfcc deinitialize result %{public}d", result);
359
360 nfcWatchDog.Cancel();
361
362 nfcPollingManager_->ResetCurrPollingParams();
363 ceService_->Deinitialize();
364 UpdateNfcState(KITS::STATE_OFF);
365
366 // Do turn off success, closeRequestCnt = 1, others = 0
367 ExternalDepsProxy::GetInstance().WriteOpenAndCloseHiSysEvent(NOT_COUNT, NOT_COUNT, DEFAULT_COUNT, NOT_COUNT);
368 // Record success event
369 ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(
370 MainErrorCode::NFC_CLOSE_SUCCEED, SubErrorCode::DEFAULT_ERR_DEF);
371 NotifyMessageToVendor(KITS::NFC_SWITCH_KEY, std::to_string(KITS::STATE_OFF));
372 InfoLog("Nfc do turn off successfully.");
373 return result;
374 }
375
DoInitialize()376 void NfcService::DoInitialize()
377 {
378 eventHandler_->Intialize(tagDispatcher_, ceService_, nfcPollingManager_, nfcRoutingManager_, nciNfccProxy_);
379 ExternalDepsProxy::GetInstance().InitAppList();
380
381 int nfcStateFromParam = ExternalDepsProxy::GetInstance().GetNfcStateFromParam();
382 if (nfcStateFromParam == KITS::STATE_ON) {
383 InfoLog("should turn nfc on.");
384 ExecuteTask(KITS::TASK_TURN_ON);
385 } else {
386 // 5min later unload nfc_service, if nfc state is off
387 SetupUnloadNfcSaTimer(true);
388 }
389 }
390
SetRegisterCallBack(const sptr<INfcControllerCallback> & callback,const std::string & type,Security::AccessToken::AccessTokenID callerToken)391 int NfcService::SetRegisterCallBack(const sptr<INfcControllerCallback> &callback,
392 const std::string& type, Security::AccessToken::AccessTokenID callerToken)
393 {
394 InfoLog("NfcService SetRegisterCallBack");
395 if (callback == nullptr) {
396 ErrorLog("register callback is nullptr");
397 return KITS::ERR_NFC_PARAMETERS;
398 }
399 std::lock_guard<std::mutex> lock(mutex_);
400 bool isExist = false;
401 NfcStateRegistryRecord record;
402 InfoLog("RecordsSize=%{public}zu,isExist=%{public}d,type=%{public}s",
403 stateRecords_.size(), isExist, type.c_str());
404 for (size_t i = 0; i < stateRecords_.size(); i++) {
405 record = stateRecords_[i];
406 InfoLog("record.type_=%{public}s", record.type_.c_str());
407 if (record.type_.compare(type) == 0 && record.callerToken_ == callerToken) {
408 isExist = true;
409 break;
410 }
411 }
412 InfoLog("isExist=%{public}d", isExist);
413 if (!isExist) {
414 record.type_ = type;
415 record.callerToken_ = callerToken;
416 record.nfcStateChangeCallback_ = callback;
417 stateRecords_.push_back(record);
418 // if callback non-STATE_ON when register, the nfc icon of sceneboard will blink
419 if (nfcState_ == KITS::STATE_ON) {
420 callback->OnNfcStateChanged(nfcState_);
421 }
422 }
423 return KITS::ERR_NONE;
424 }
425
RemoveRegisterCallBack(const std::string & type,Security::AccessToken::AccessTokenID callerToken)426 int NfcService::RemoveRegisterCallBack(const std::string& type,
427 Security::AccessToken::AccessTokenID callerToken)
428 {
429 InfoLog("NfcService RemoveRegisterCallBack");
430 std::lock_guard<std::mutex> lock(mutex_);
431 int32_t result = KITS::ERR_NFC_PARAMETERS;
432 std::vector<NfcStateRegistryRecord>::iterator it;
433 for (it = stateRecords_.begin(); it != stateRecords_.end(); ++it) {
434 if (it->type_.compare(type) == 0 && it->callerToken_ == callerToken) {
435 InfoLog("NfcService RemoveRegisterCallBack success.");
436 stateRecords_.erase(it);
437 result = KITS::ERR_NONE;
438 break;
439 }
440 }
441 return result;
442 }
443
RemoveAllRegisterCallBack(Security::AccessToken::AccessTokenID callerToken)444 int NfcService::RemoveAllRegisterCallBack(Security::AccessToken::AccessTokenID callerToken)
445 {
446 InfoLog("NfcService RemoveAllRegisterCallBack");
447 std::lock_guard<std::mutex> lock(mutex_);
448 int32_t result = KITS::ERR_NFC_PARAMETERS;
449 std::vector<NfcStateRegistryRecord>::iterator it;
450 for (it = stateRecords_.begin(); it != stateRecords_.end(); ++it) {
451 if (it->callerToken_ == callerToken) {
452 InfoLog("NfcService RemoveAllRegisterCallBack success.");
453 stateRecords_.erase(it);
454 result = KITS::ERR_NONE;
455 break;
456 }
457 }
458 return result;
459 }
460
UpdateNfcState(int newState)461 void NfcService::UpdateNfcState(int newState)
462 {
463 InfoLog("Update nfc state: oldState %{public}d, newState %{public}d", nfcState_, newState);
464 std::lock_guard<std::mutex> lock(mutex_);
465 if (newState == nfcState_) {
466 return;
467 }
468 nfcState_ = newState;
469
470 ExternalDepsProxy::GetInstance().UpdateNfcState(newState);
471 ExternalDepsProxy::GetInstance().PublishNfcStateChanged(newState);
472 InfoLog("Update nfc state: nfcState_ %{public}d, newState %{public}d succ", nfcState_, newState);
473
474 // notify the nfc state changed by callback to JS APP
475 InfoLog("stateRecords_.size[%{public}zu]", stateRecords_.size());
476 for (size_t i = 0; i < stateRecords_.size(); i++) {
477 NfcStateRegistryRecord record = stateRecords_[i];
478 DebugLog("stateRecords_[%{public}d]:type_=%{public}s ",
479 (int)i, record.type_.c_str());
480 if (record.nfcStateChangeCallback_ != nullptr) {
481 record.nfcStateChangeCallback_->OnNfcStateChanged(newState);
482 }
483 }
484 if (nfcState_ == KITS::STATE_OFF) {
485 // 5min later unload nfc_service, if nfc state is off
486 SetupUnloadNfcSaTimer(true);
487 } else {
488 CancelUnloadNfcSaTimer();
489 }
490 }
491
GetNfcState()492 int NfcService::GetNfcState()
493 {
494 InfoLog("start to get nfc state.");
495 std::lock_guard<std::mutex> lock(mutex_);
496 // 5min later unload nfc_service, if nfc state is off
497 if (nfcState_ == KITS::STATE_OFF) {
498 SetupUnloadNfcSaTimer(false);
499 }
500 InfoLog("get nfc state[%{public}d]", nfcState_);
501 return nfcState_;
502 }
503
GetScreenState()504 int NfcService::GetScreenState()
505 {
506 std::lock_guard<std::mutex> lock(mutex_);
507 return screenState_;
508 }
509
GetNciVersion()510 int NfcService::GetNciVersion()
511 {
512 return nciVersion_;
513 }
514
IsNfcEnabled()515 bool NfcService::IsNfcEnabled()
516 {
517 InfoLog("IsNfcEnabled, nfcState_=%{public}d", nfcState_);
518 return (nfcState_ == KITS::STATE_ON);
519 }
520
HandleShutdown()521 void NfcService::HandleShutdown()
522 {
523 std::lock_guard<std::mutex> lock(mutex_);
524 ExternalDepsProxy::GetInstance().UpdateNfcState(nfcState_);
525 InfoLog("device is shutting down, nfcState_ = %{public}d", nfcState_);
526 nciNfccProxy_->Shutdown();
527 }
528
RegNdefMsgCb(const sptr<INdefMsgCallback> & callback)529 bool NfcService::RegNdefMsgCb(const sptr<INdefMsgCallback> &callback)
530 {
531 DebugLog("NfcService::RegNdefMsgCb");
532 tagDispatcher_->RegNdefMsgCb(callback);
533 return true;
534 }
535
SetupUnloadNfcSaTimer(bool shouldRestartTimer)536 void NfcService::SetupUnloadNfcSaTimer(bool shouldRestartTimer)
537 {
538 TimeOutCallback timeoutCallback = [this]() { UnloadNfcSa(); };
539 if (unloadStaSaTimerId != 0) {
540 if (!shouldRestartTimer) {
541 InfoLog("timer already started.");
542 return;
543 }
544 NfcTimer::GetInstance()->UnRegister(unloadStaSaTimerId);
545 unloadStaSaTimerId = 0;
546 }
547 NfcTimer::GetInstance()->Register(timeoutCallback, unloadStaSaTimerId, TIMEOUT_UNLOAD_NFC_SA);
548 }
549
CancelUnloadNfcSaTimer()550 void NfcService::CancelUnloadNfcSaTimer()
551 {
552 if (unloadStaSaTimerId != 0) {
553 NfcTimer::GetInstance()->UnRegister(unloadStaSaTimerId);
554 unloadStaSaTimerId = 0;
555 }
556 }
557
NotifyMessageToVendor(int key,const std::string & value)558 void NfcService::NotifyMessageToVendor(int key, const std::string &value)
559 {
560 if (nciNfccProxy_ == nullptr) {
561 ErrorLog("nciNfccProxy_ nullptr.");
562 return;
563 }
564 nciNfccProxy_->NotifyMessageToVendor(key, value);
565 }
566 } // namespace NFC
567 } // namespace OHOS
568