blob: 5996406fd0b116c5008801b43adb3b2571127bf3 [file] [log] [blame]
Jeff Pu52653182022-10-12 16:27:23 -04001/*
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 "util/Util.h"
20
21using namespace ::android::fingerprint::virt;
22
23namespace aidl::android::hardware::biometrics::fingerprint {
24
25void FakeLockoutTracker::reset() {
26 mFailedCount = 0;
27 mLockoutTimedStart = 0;
28 mCurrentMode = LockoutMode::kNone;
29}
30
31void FakeLockoutTracker::addFailedAttempt() {
32 bool enabled = FingerprintHalProperties::lockout_enable().value_or(false);
33 if (enabled) {
34 mFailedCount++;
35 int32_t lockoutTimedThreshold =
36 FingerprintHalProperties::lockout_timed_threshold().value_or(5);
37 int32_t lockoutPermanetThreshold =
38 FingerprintHalProperties::lockout_permanent_threshold().value_or(20);
39 if (mFailedCount >= lockoutPermanetThreshold) {
40 mCurrentMode = LockoutMode::kPermanent;
41 FingerprintHalProperties::lockout(true);
42 } else if (mFailedCount >= lockoutTimedThreshold) {
43 if (mCurrentMode == LockoutMode::kNone) {
44 mCurrentMode = LockoutMode::kTimed;
45 mLockoutTimedStart = Util::getSystemNanoTime();
46 }
47 }
48 } else {
49 reset();
50 }
51}
52
53FakeLockoutTracker::LockoutMode FakeLockoutTracker::getMode() {
54 if (mCurrentMode == LockoutMode::kTimed) {
55 int32_t lockoutTimedDuration =
56 FingerprintHalProperties::lockout_timed_duration().value_or(10 * 100);
57 if (Util::hasElapsed(mLockoutTimedStart, lockoutTimedDuration)) {
58 mCurrentMode = LockoutMode::kNone;
59 mLockoutTimedStart = 0;
60 }
61 }
62
63 return mCurrentMode;
64}
65
66int64_t FakeLockoutTracker::getLockoutTimeLeft() {
67 int64_t res = 0;
68
69 if (mLockoutTimedStart > 0) {
70 auto now = Util::getSystemNanoTime();
71 auto left = now - mLockoutTimedStart;
72 res = (left > 0) ? (left / 1000000LL) : 0;
73 }
74
75 return res;
76}
77} // namespace aidl::android::hardware::biometrics::fingerprint