xref: /aosp_15_r20/hardware/interfaces/biometrics/fingerprint/aidl/default/FakeLockoutTracker.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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(bool dueToTimeout)26 void FakeLockoutTracker::reset(bool dueToTimeout) {
27     if (!dueToTimeout) {
28         mFailedCount = 0;
29     }
30     mFailedCountTimed = 0;
31     mLockoutTimedStart = 0;
32     mCurrentMode = LockoutMode::kNone;
33 }
34 
addFailedAttempt()35 void FakeLockoutTracker::addFailedAttempt() {
36     bool enabled = Fingerprint::cfg().get<bool>("lockout_enable");
37     if (enabled) {
38         mFailedCount++;
39         mFailedCountTimed++;
40         int32_t lockoutTimedThreshold =
41                 Fingerprint::cfg().get<std::int32_t>("lockout_timed_threshold");
42         int32_t lockoutPermanetThreshold =
43                 Fingerprint::cfg().get<std::int32_t>("lockout_permanent_threshold");
44         if (mFailedCount >= lockoutPermanetThreshold) {
45             mCurrentMode = LockoutMode::kPermanent;
46             Fingerprint::cfg().set<bool>("lockout", true);
47         } else if (mFailedCountTimed >= lockoutTimedThreshold) {
48             if (mCurrentMode == LockoutMode::kNone) {
49                 mCurrentMode = LockoutMode::kTimed;
50                 mLockoutTimedStart = Util::getSystemNanoTime();
51             }
52         }
53     } else {
54         reset();
55     }
56 }
57 
getMode()58 FakeLockoutTracker::LockoutMode FakeLockoutTracker::getMode() {
59     if (mCurrentMode == LockoutMode::kTimed) {
60         int32_t lockoutTimedDuration =
61                 Fingerprint::cfg().get<std::int32_t>("lockout_timed_duration");
62         if (Util::hasElapsed(mLockoutTimedStart, lockoutTimedDuration)) {
63             mCurrentMode = LockoutMode::kNone;
64             mLockoutTimedStart = 0;
65         }
66     }
67 
68     return mCurrentMode;
69 }
70 
getLockoutTimeLeft()71 int64_t FakeLockoutTracker::getLockoutTimeLeft() {
72     int64_t res = 0;
73 
74     if (mLockoutTimedStart > 0) {
75         int32_t lockoutTimedDuration =
76                 Fingerprint::cfg().get<std::int32_t>("lockout_timed_duration");
77         auto now = Util::getSystemNanoTime();
78         auto elapsed = (now - mLockoutTimedStart) / 1000000LL;
79         res = lockoutTimedDuration - elapsed;
80         LOG(INFO) << "elapsed=" << elapsed << " now = " << now
81                   << " mLockoutTimedStart=" << mLockoutTimedStart << " res=" << res;
82     }
83 
84     return res;
85 }
86 }  // namespace aidl::android::hardware::biometrics::fingerprint
87