blob: a056db50d0680c0e43765b468ea9dff92236da4f [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>
Jeff Pu6ccd9562024-02-21 10:46:35 -050019#include "Fingerprint.h"
Jeff Pu52653182022-10-12 16:27:23 -040020#include "util/Util.h"
21
22using namespace ::android::fingerprint::virt;
23
24namespace aidl::android::hardware::biometrics::fingerprint {
25
26void FakeLockoutTracker::reset() {
27 mFailedCount = 0;
28 mLockoutTimedStart = 0;
29 mCurrentMode = LockoutMode::kNone;
30}
31
32void FakeLockoutTracker::addFailedAttempt() {
Jeff Pu6ccd9562024-02-21 10:46:35 -050033 bool enabled = Fingerprint::cfg().get<bool>("lockout_enable");
Jeff Pu52653182022-10-12 16:27:23 -040034 if (enabled) {
35 mFailedCount++;
36 int32_t lockoutTimedThreshold =
Jeff Pu6ccd9562024-02-21 10:46:35 -050037 Fingerprint::cfg().get<std::int32_t>("lockout_timed_threshold");
Jeff Pu52653182022-10-12 16:27:23 -040038 int32_t lockoutPermanetThreshold =
Jeff Pu6ccd9562024-02-21 10:46:35 -050039 Fingerprint::cfg().get<std::int32_t>("lockout_permanent_threshold");
Jeff Pu52653182022-10-12 16:27:23 -040040 if (mFailedCount >= lockoutPermanetThreshold) {
41 mCurrentMode = LockoutMode::kPermanent;
Jeff Pu6ccd9562024-02-21 10:46:35 -050042 Fingerprint::cfg().set<bool>("lockout", true);
Jeff Pu52653182022-10-12 16:27:23 -040043 } 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
54FakeLockoutTracker::LockoutMode FakeLockoutTracker::getMode() {
55 if (mCurrentMode == LockoutMode::kTimed) {
56 int32_t lockoutTimedDuration =
Jeff Pu6ccd9562024-02-21 10:46:35 -050057 Fingerprint::cfg().get<std::int32_t>("lockout_timed_duration");
Jeff Pu52653182022-10-12 16:27:23 -040058 if (Util::hasElapsed(mLockoutTimedStart, lockoutTimedDuration)) {
59 mCurrentMode = LockoutMode::kNone;
60 mLockoutTimedStart = 0;
61 }
62 }
63
64 return mCurrentMode;
65}
66
67int64_t FakeLockoutTracker::getLockoutTimeLeft() {
68 int64_t res = 0;
69
70 if (mLockoutTimedStart > 0) {
Jeff Pu437516e2023-06-28 15:21:21 +000071 int32_t lockoutTimedDuration =
Jeff Pu6ccd9562024-02-21 10:46:35 -050072 Fingerprint::cfg().get<std::int32_t>("lockout_timed_duration");
Jeff Pu52653182022-10-12 16:27:23 -040073 auto now = Util::getSystemNanoTime();
Jeff Pu437516e2023-06-28 15:21:21 +000074 auto elapsed = (now - mLockoutTimedStart) / 1000000LL;
75 res = lockoutTimedDuration - elapsed;
Jeff Pu6ccd9562024-02-21 10:46:35 -050076 LOG(INFO) << "elapsed=" << elapsed << " now = " << now
Jeff Pu437516e2023-06-28 15:21:21 +000077 << " mLockoutTimedStart=" << mLockoutTimedStart << " res=" << res;
Jeff Pu52653182022-10-12 16:27:23 -040078 }
79
80 return res;
81}
82} // namespace aidl::android::hardware::biometrics::fingerprint