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 bool result = nciCeProxy_.lock()->CommitRouting();
63 DebugLog("HandleCommitRouting: result = %{public}d", result);
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_.expired() || nciCeProxy_.expired()) {
76 ErrorLog("HandleComputeRoutingParams nfcService_ or nciCeProxy_ is nullptr.");
77 return;
78 }
79 if (!nfcService_.lock()->IsNfcEnabled()) {
80 ErrorLog("HandleComputeRoutingParams: NFC not enabled, do not Compute Routing Params");
81 return;
82 }
83 std::lock_guard<std::mutex> lock(mutex_);
84 NfcWatchDog ComputeRoutingParamDog("ComputeRoutingParam", WAIT_ROUTING_INIT, nciNfccProxy_);
85 ComputeRoutingParamDog.Run();
86 bool result = nciCeProxy_.lock()->ComputeRoutingParams(defaultPaymentType);
87 DebugLog("HandleComputeRoutingParams result = %{public}d", result);
88 ComputeRoutingParamDog.Cancel();
89 }
90 } // namespace NFC
91 } // namespace OHOS