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 int nfcState = nfcService_.lock()->GetNfcState();
46 if (nfcState == KITS::STATE_OFF || nfcState == KITS::STATE_TURNING_OFF) {
47 DebugLog("HandleCommitRouting: NOT Handle CommitRouting in state off or turning off.");
48 return;
49 }
50 std::shared_ptr<NfcPollingParams> currPollingParams =
51 nfcService_.lock()->GetNfcPollingManager().lock()->GetCurrentParameters();
52 if (currPollingParams == nullptr) {
53 ErrorLog("HandleCommitRouting: currPollingParams is nullptr.");
54 return;
55 }
56 NfcWatchDog CommitRoutingDog("CommitRouting", WAIT_ROUTING_INIT, nciNfccProxy_);
57 CommitRoutingDog.Run();
58 if (currPollingParams->ShouldEnablePolling()) {
59 bool result = nciCeProxy_.lock()->CommitRouting();
60 DebugLog("HandleCommitRouting: result = %{public}d", result);
61 } else {
62 ErrorLog("HandleCommitRouting: NOT Handle CommitRouting when polling not enabled.");
63 }
64 CommitRoutingDog.Cancel();
65 }
66
ComputeRoutingParams(KITS::DefaultPaymentType defaultPaymentType)67 void NfcRoutingManager::ComputeRoutingParams(KITS::DefaultPaymentType defaultPaymentType)
68 {
69 eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_COMPUTE_ROUTING_PARAMS),
70 static_cast<int>(defaultPaymentType), ROUTING_DELAY_TIME);
71 }
72
HandleComputeRoutingParams(int defaultPaymentType)73 void NfcRoutingManager::HandleComputeRoutingParams(int defaultPaymentType)
74 {
75 if (!nfcService_.lock()->IsNfcEnabled()) {
76 ErrorLog("HandleComputeRoutingParams: NFC not enabled, do not Compute Routing Params");
77 return;
78 }
79 std::lock_guard<std::mutex> lock(mutex_);
80 NfcWatchDog ComputeRoutingParamDog("ComputeRoutingParam", WAIT_ROUTING_INIT, nciNfccProxy_);
81 ComputeRoutingParamDog.Run();
82 bool result = nciCeProxy_.lock()->ComputeRoutingParams(defaultPaymentType);
83 DebugLog("HandleComputeRoutingParams result = %{public}d", result);
84 ComputeRoutingParamDog.Cancel();
85 }
86 } // namespace NFC
87 } // namespace OHOS