Jeff Pu | 3e7448d | 2023-12-07 17:25:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #pragma once |
| 18 | |
| 19 | #include <aidl/android/hardware/biometrics/face/ISessionCallback.h> |
| 20 | #include <android/binder_to_string.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace aidl::android::hardware::biometrics::face { |
| 25 | |
| 26 | // Lockout implementation for Face Virtual HAL |
| 27 | class FakeLockoutTracker { |
| 28 | public: |
| 29 | FakeLockoutTracker() |
| 30 | : mFailedCount(0), |
| 31 | mLastFailedTime(0), |
| 32 | mIsLockoutTimerStarted(false), |
| 33 | mIsLockoutTimerAborted(false) {} |
| 34 | ~FakeLockoutTracker() {} |
| 35 | |
| 36 | enum class LockoutMode : int8_t { kNone = 0, kTimed, kPermanent }; |
| 37 | |
| 38 | bool checkIfLockout(ISessionCallback*); |
| 39 | void addFailedAttempt(ISessionCallback*); |
| 40 | int64_t getLockoutTimeLeft(); |
| 41 | LockoutMode getMode(); |
| 42 | void reset(bool dueToTimerExpire = false); |
| 43 | inline std::string toString() const { |
| 44 | std::ostringstream os; |
| 45 | os << "----- FakeLockoutTracker:: -----" << std::endl; |
| 46 | os << "mFailedCount:" << mFailedCount; |
| 47 | os << ", mCurrentMode:" << (int)mCurrentMode; |
| 48 | os << ", mLastFailedTime:" << (int)(mLastFailedTime / 1000000LL); |
| 49 | os << ", mIsLockoutTimerStarted:" << mIsLockoutTimerStarted; |
| 50 | os << ", mIsLockoutTimerAborted:" << mIsLockoutTimerAborted; |
| 51 | os << std::endl; |
| 52 | return os.str(); |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | void startLockoutTimer(int64_t timeout, ISessionCallback* cb); |
| 57 | void lockoutTimerExpired(ISessionCallback* cb); |
| 58 | int32_t getTimedLockoutDuration(); |
| 59 | void abortTimer(); |
| 60 | |
| 61 | private: |
| 62 | int32_t mFailedCount; |
| 63 | int32_t mTimedFailedCount; |
| 64 | int64_t mLastFailedTime; |
| 65 | LockoutMode mCurrentMode; |
| 66 | bool mIsLockoutTimerStarted; |
| 67 | bool mIsLockoutTimerAborted; |
| 68 | }; |
| 69 | |
| 70 | } // namespace aidl::android::hardware::biometrics::face |