1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "FakeLockoutTracker.h"
18 #include <fingerprint.sysprop.h>
19 #include "Fingerprint.h"
20 #include "util/Util.h"
21 
22 using namespace ::android::fingerprint::virt;
23 
24 namespace aidl::android::hardware::biometrics::fingerprint {
25 
reset()26 void FakeLockoutTracker::reset() {
27     mFailedCount = 0;
28     mLockoutTimedStart = 0;
29     mCurrentMode = LockoutMode::kNone;
30 }
31 
addFailedAttempt()32 void FakeLockoutTracker::addFailedAttempt() {
33     bool enabled = Fingerprint::cfg().get<bool>("lockout_enable");
34     if (enabled) {
35         mFailedCount++;
36         int32_t lockoutTimedThreshold =
37                 Fingerprint::cfg().get<std::int32_t>("lockout_timed_threshold");
38         int32_t lockoutPermanetThreshold =
39                 Fingerprint::cfg().get<std::int32_t>("lockout_permanent_threshold");
40         if (mFailedCount >= lockoutPermanetThreshold) {
41             mCurrentMode = LockoutMode::kPermanent;
42             Fingerprint::cfg().set<bool>("lockout", true);
43         } else if (mFailedCount >= lockoutTimedThreshold) {
44             if (mCurrentMode == LockoutMode::kNone) {
45                 mCurrentMode = LockoutMode::kTimed;
46                 mLockoutTimedStart = Util::getSystemNanoTime();
47             }
48         }
49     } else {
50         reset();
51     }
52 }
53 
getMode()54 FakeLockoutTracker::LockoutMode FakeLockoutTracker::getMode() {
55     if (mCurrentMode == LockoutMode::kTimed) {
56         int32_t lockoutTimedDuration =
57                 Fingerprint::cfg().get<std::int32_t>("lockout_timed_duration");
58         if (Util::hasElapsed(mLockoutTimedStart, lockoutTimedDuration)) {
59             mCurrentMode = LockoutMode::kNone;
60             mLockoutTimedStart = 0;
61         }
62     }
63 
64     return mCurrentMode;
65 }
66 
getLockoutTimeLeft()67 int64_t FakeLockoutTracker::getLockoutTimeLeft() {
68     int64_t res = 0;
69 
70     if (mLockoutTimedStart > 0) {
71         int32_t lockoutTimedDuration =
72                 Fingerprint::cfg().get<std::int32_t>("lockout_timed_duration");
73         auto now = Util::getSystemNanoTime();
74         auto elapsed = (now - mLockoutTimedStart) / 1000000LL;
75         res = lockoutTimedDuration - elapsed;
76         LOG(INFO) << "elapsed=" << elapsed << " now = " << now
77                   << " mLockoutTimedStart=" << mLockoutTimedStart << " res=" << res;
78     }
79 
80     return res;
81 }
82 }  // namespace aidl::android::hardware::biometrics::fingerprint
83