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_routing_manager.h"
16
17 #include "loghelper.h"
18 #include "nfc_service.h"
19 #include "nfc_watch_dog.h"
20
21 namespace OHOS {
22 namespace NFC {
23 // ms wait for setting the routing table.
24 const int ROUTING_DELAY_TIME = 0; // ms
NfcRoutingManager(std::shared_ptr<NfcEventHandler> eventHandler,std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy,std::weak_ptr<NCI::INciCeInterface> nciCeProxy,std::weak_ptr<NfcService> nfcService)25 NfcRoutingManager::NfcRoutingManager(std::shared_ptr<NfcEventHandler> eventHandler,
26 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy,
27 std::weak_ptr<NCI::INciCeInterface> nciCeProxy,
28 std::weak_ptr<NfcService> nfcService)
29 : eventHandler_(eventHandler), nciNfccProxy_(nciNfccProxy), nciCeProxy_(nciCeProxy), nfcService_(nfcService)
30 {}
31
~NfcRoutingManager()32 NfcRoutingManager::~NfcRoutingManager()
33 {
34 eventHandler_ = nullptr;
35 }
36
CommitRouting()37 void NfcRoutingManager::CommitRouting()
38 {
39 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_COMMIT_ROUTING), ROUTING_DELAY_TIME);
40 }
41
HandleCommitRouting()42 void NfcRoutingManager::HandleCommitRouting()
43 {
44 std::lock_guard<std::mutex> lock(mutex_);
45 if (nfcService_.expired() || nciCeProxy_.expired()) {
46 ErrorLog("HandleCommitRouting nfcService_ or nciCeProxy_ is nullptr.");
47 return;
48 }
49 int nfcState = nfcService_.lock()->GetNfcState();
50 if (nfcState == KITS::STATE_OFF || nfcState == KITS::STATE_TURNING_OFF) {
51 WarnLog("HandleCommitRouting: NOT Handle CommitRouting in state off or turning off.");
52 return;
53 }
54 std::shared_ptr<NfcPollingParams> currPollingParams =
55 nfcService_.lock()->GetNfcPollingManager().lock()->GetCurrentParameters();
56 if (currPollingParams == nullptr) {
57 ErrorLog("HandleCommitRouting: currPollingParams is nullptr.");
58 return;
59 }
60 NfcWatchDog CommitRoutingDog("CommitRouting", WAIT_ROUTING_INIT, nciNfccProxy_);
61 CommitRoutingDog.Run();
62 if (currPollingParams->ShouldEnablePolling()) {
63 bool result = nciCeProxy_.lock()->CommitRouting();
64 DebugLog("HandleCommitRouting: result = %{public}d", result);
65 } else {
66 ErrorLog("HandleCommitRouting: NOT Handle CommitRouting when polling not enabled.");
67 }
68 CommitRoutingDog.Cancel();
69 }
70
ComputeRoutingParams(KITS::DefaultPaymentType defaultPaymentType)71 void NfcRoutingManager::ComputeRoutingParams(KITS::DefaultPaymentType defaultPaymentType)
72 {
73 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_COMPUTE_ROUTING_PARAMS),
74 static_cast<int>(defaultPaymentType), ROUTING_DELAY_TIME);
75 }
76
HandleComputeRoutingParams(int defaultPaymentType)77 void NfcRoutingManager::HandleComputeRoutingParams(int defaultPaymentType)
78 {
79 if (nfcService_.expired() || nciCeProxy_.expired()) {
80 ErrorLog("HandleComputeRoutingParams nfcService_ or nciCeProxy_ is nullptr.");
81 return;
82 }
83 if (!nfcService_.lock()->IsNfcEnabled()) {
84 ErrorLog("HandleComputeRoutingParams: NFC not enabled, do not Compute Routing Params");
85 return;
86 }
87 std::lock_guard<std::mutex> lock(mutex_);
88 NfcWatchDog ComputeRoutingParamDog("ComputeRoutingParam", WAIT_ROUTING_INIT, nciNfccProxy_);
89 ComputeRoutingParamDog.Run();
90 bool result = nciCeProxy_.lock()->ComputeRoutingParams(defaultPaymentType);
91 DebugLog("HandleComputeRoutingParams result = %{public}d", result);
92 ComputeRoutingParamDog.Cancel();
93 }
94 } // namespace NFC
95 } // namespace OHOS