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:
FakeLockoutTracker()29     FakeLockoutTracker()
30         : mFailedCount(0),
31           mTimedFailedCount(0),
32           mLastFailedTime(0),
33           mCurrentMode(LockoutMode::kNone),
34           mIsLockoutTimerStarted(false),
35           mIsLockoutTimerAborted(false) {}
~FakeLockoutTracker()36     ~FakeLockoutTracker() {}
37 
38     enum class LockoutMode : int8_t { kNone = 0, kTimed, kPermanent };
39 
40     bool checkIfLockout(ISessionCallback*);
41     void addFailedAttempt(ISessionCallback*);
42     int64_t getLockoutTimeLeft();
43     LockoutMode getMode();
44     void reset(bool dueToTimerExpire = false);
toString()45     inline std::string toString() const {
46         std::ostringstream os;
47         os << "----- FakeLockoutTracker:: -----" << std::endl;
48         os << "mFailedCount:" << mFailedCount;
49         os << ", mCurrentMode:" << (int)mCurrentMode;
50         os << ", mLastFailedTime:" << (int)(mLastFailedTime / 1000000LL);
51         os << ",  mIsLockoutTimerStarted:" << mIsLockoutTimerStarted;
52         os << ", mIsLockoutTimerAborted:" << mIsLockoutTimerAborted;
53         os << std::endl;
54         return os.str();
55     }
56 
57   private:
58     void startLockoutTimer(int64_t timeout, ISessionCallback* cb);
59     void lockoutTimerExpired(ISessionCallback* cb);
60     int32_t getTimedLockoutDuration();
61     void abortTimer();
62 
63   private:
64     int32_t mFailedCount;
65     int32_t mTimedFailedCount;
66     int64_t mLastFailedTime;
67     LockoutMode mCurrentMode;
68     bool mIsLockoutTimerStarted;
69     bool mIsLockoutTimerAborted;
70 };
71 
72 }  // namespace aidl::android::hardware::biometrics::face
73