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_watch_dog.h"
16 #include <chrono>
17 #include "loghelper.h"
18 #include "external_deps_proxy.h"
19 
20 namespace OHOS {
21 namespace NFC {
NfcWatchDog(const std::string & threadName,int timeout,std::weak_ptr<NCI::INciNfccInterface> nfccProxy)22 NfcWatchDog::NfcWatchDog(const std::string& threadName, int timeout, std::weak_ptr<NCI::INciNfccInterface> nfccProxy)
23     : threadName_(threadName), timeout_(timeout), canceled_(false), thread_(nullptr), nciNfccProxy_(nfccProxy)
24 {
25 }
26 
~NfcWatchDog()27 NfcWatchDog::~NfcWatchDog()
28 {
29     if (thread_ && thread_->joinable()) {
30         conditionVariable_.notify_one();
31         thread_->join();
32     }
33 }
34 
MainLoop()35 void NfcWatchDog::MainLoop()
36 {
37     std::unique_lock<std::mutex> lock(mutex_);
38     InfoLog("Watchdog [%{public}s] starts to run.", threadName_.c_str());
39     conditionVariable_.wait_for(lock, std::chrono::milliseconds(timeout_), [this] { return canceled_; });
40     if (canceled_) {
41         return;
42     }
43     // If Routing Wake Lock is held, Routing Wake Lock release. Watchdog triggered, release lock before aborting.
44     if (nciNfccProxy_.expired()) {
45         return;
46     }
47     InfoLog("Watchdog triggered, aborting.");
48     NfcFailedParams err;
49     if (threadName_.compare("DoTurnOn") == 0) {
50         ExternalDepsProxy::GetInstance().BuildFailedParams(err, MainErrorCode::NFC_OPEN_FAILED,
51             SubErrorCode::PROCESS_ABORT);
52     } else if (threadName_.compare("DoTurnOff") == 0) {
53         ExternalDepsProxy::GetInstance().BuildFailedParams(err, MainErrorCode::NFC_CLOSE_FAILED,
54             SubErrorCode::PROCESS_ABORT);
55     } else if (threadName_.compare("nfcProcessEvent") == 0) {
56         ExternalDepsProxy::GetInstance().BuildFailedParams(
57             err, MainErrorCode::NFC_EVENTHANDLER_TIMEOUT, SubErrorCode::PROCESS_ABORT);
58     } else {
59         ExternalDepsProxy::GetInstance().BuildFailedParams(err, MainErrorCode::NFC_GENERAL_ERR,
60             SubErrorCode::PROCESS_ABORT);
61     }
62     ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(&err);
63     nciNfccProxy_.lock()->Abort();
64 }
65 
Run()66 void NfcWatchDog::Run()
67 {
68     thread_ = std::make_unique<std::thread>([this]() { this->MainLoop(); });
69 }
70 
Cancel()71 void NfcWatchDog::Cancel()
72 {
73     std::unique_lock<std::mutex> lock(mutex_);
74     canceled_ = true;
75     conditionVariable_.notify_one();
76 }
77 }  // namespace NFC
78 }  // namespace OHOS
79