blob: 362d0df3c8e99966bae48992adf19cd3729d3af9 [file] [log] [blame]
Ilya Matyukhin48ff8962021-02-22 13:13:13 -08001/*
Jeff Pu52653182022-10-12 16:27:23 -04002 * Copyright (C) 2022 The Android Open Source Project
Ilya Matyukhin48ff8962021-02-22 13:13:13 -08003 *
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
Jeff Pu52653182022-10-12 16:27:23 -040018
19#define LOG_TAG "FingerprintVirtualHal"
20
Joshua McCloskeyc8c0bad2022-05-10 05:17:44 +000021#include <aidl/android/hardware/biometrics/common/SensorStrength.h>
Joe Bolingerde94aa02021-12-09 17:00:32 -080022#include <aidl/android/hardware/biometrics/fingerprint/ISessionCallback.h>
Jeff Pu52653182022-10-12 16:27:23 -040023#include <android/binder_to_string.h>
24#include <string>
Joe Bolingerde94aa02021-12-09 17:00:32 -080025
Ilya Matyukhin1d524382021-07-02 20:33:51 +000026#include <random>
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080027
Jeff Pu63f33c72022-07-28 16:06:23 -040028#include <aidl/android/hardware/biometrics/fingerprint/SensorLocation.h>
Joshua McCloskeyc8c0bad2022-05-10 05:17:44 +000029#include <future>
30#include <vector>
Joe Bolingerde94aa02021-12-09 17:00:32 -080031
Jeff Pu52653182022-10-12 16:27:23 -040032#include "FakeLockoutTracker.h"
33
Joe Bolingerde94aa02021-12-09 17:00:32 -080034using namespace ::aidl::android::hardware::biometrics::common;
35
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080036namespace aidl::android::hardware::biometrics::fingerprint {
37
Joe Bolingerde94aa02021-12-09 17:00:32 -080038// A fake engine that is backed by system properties instead of hardware.
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080039class FakeFingerprintEngine {
40 public:
Jeff Pudef5b042023-05-25 14:28:16 -040041 FakeFingerprintEngine();
Jeff Pu63f33c72022-07-28 16:06:23 -040042 virtual ~FakeFingerprintEngine() {}
Ilya Matyukhin1d524382021-07-02 20:33:51 +000043
Joe Bolingerde94aa02021-12-09 17:00:32 -080044 void generateChallengeImpl(ISessionCallback* cb);
45 void revokeChallengeImpl(ISessionCallback* cb, int64_t challenge);
Jeff Pu52653182022-10-12 16:27:23 -040046 virtual void enrollImpl(ISessionCallback* cb, const keymaster::HardwareAuthToken& hat,
47 const std::future<void>& cancel);
48 virtual void authenticateImpl(ISessionCallback* cb, int64_t operationId,
49 const std::future<void>& cancel);
50 virtual void detectInteractionImpl(ISessionCallback* cb, const std::future<void>& cancel);
Joe Bolingerde94aa02021-12-09 17:00:32 -080051 void enumerateEnrollmentsImpl(ISessionCallback* cb);
52 void removeEnrollmentsImpl(ISessionCallback* cb, const std::vector<int32_t>& enrollmentIds);
53 void getAuthenticatorIdImpl(ISessionCallback* cb);
54 void invalidateAuthenticatorIdImpl(ISessionCallback* cb);
55 void resetLockoutImpl(ISessionCallback* cb, const keymaster::HardwareAuthToken& /*hat*/);
Jeff Pu63f33c72022-07-28 16:06:23 -040056 bool getSensorLocationConfig(SensorLocation& out);
57
58 virtual ndk::ScopedAStatus onPointerDownImpl(int32_t pointerId, int32_t x, int32_t y,
59 float minor, float major);
60
61 virtual ndk::ScopedAStatus onPointerUpImpl(int32_t pointerId);
62
63 virtual ndk::ScopedAStatus onUiReadyImpl();
64
65 virtual SensorLocation getSensorLocation();
66
67 virtual SensorLocation defaultSensorLocation();
Ilya Matyukhin1d524382021-07-02 20:33:51 +000068
Jeff Pudef5b042023-05-25 14:28:16 -040069 virtual void fingerDownAction();
70
Jeff Pu52653182022-10-12 16:27:23 -040071 int32_t getLatency(const std::vector<std::optional<std::int32_t>>& latencyVec);
72
Ilya Matyukhin1d524382021-07-02 20:33:51 +000073 std::mt19937 mRandom;
Jeff Pu343ca942022-09-14 15:56:30 -040074
Jeff Pudef5b042023-05-25 14:28:16 -040075 enum class WorkMode : int8_t { kIdle = 0, kAuthenticate, kEnroll, kDetectInteract };
76
77 WorkMode getWorkMode() { return mWorkMode; }
Jeff Pu74e25d22024-01-08 22:21:20 +000078 void notifyFingerdown() { mFingerIsDown = true; }
Jeff Pudef5b042023-05-25 14:28:16 -040079
Jeff Pu52653182022-10-12 16:27:23 -040080 virtual std::string toString() const {
81 std::ostringstream os;
82 os << "----- FakeFingerprintEngine:: -----" << std::endl;
Jeff Pudef5b042023-05-25 14:28:16 -040083 os << "mWorkMode:" << (int)mWorkMode;
Jeff Pu52653182022-10-12 16:27:23 -040084 os << "acquiredVendorInfoBase:" << FINGERPRINT_ACQUIRED_VENDOR_BASE;
85 os << ", errorVendorBase:" << FINGERPRINT_ERROR_VENDOR_BASE << std::endl;
86 os << mLockoutTracker.toString();
87 return os.str();
88 }
89
Jeff Pudef5b042023-05-25 14:28:16 -040090 protected:
91 virtual void updateContext(WorkMode mode, ISessionCallback* cb, std::future<void>& cancel,
92 int64_t operationId, const keymaster::HardwareAuthToken& hat);
93
Jeff Pu073af182023-07-12 18:53:52 +000094 bool onEnrollFingerDown(ISessionCallback* cb, const keymaster::HardwareAuthToken& hat,
Jeff Pudef5b042023-05-25 14:28:16 -040095 const std::future<void>& cancel);
Jeff Pu073af182023-07-12 18:53:52 +000096 bool onAuthenticateFingerDown(ISessionCallback* cb, int64_t, const std::future<void>& cancel);
97 bool onDetectInteractFingerDown(ISessionCallback* cb, const std::future<void>& cancel);
Jeff Pudef5b042023-05-25 14:28:16 -040098
99 WorkMode mWorkMode;
100 ISessionCallback* mCb;
101 keymaster::HardwareAuthToken mHat;
102 std::future<void> mCancel;
103 int64_t mOperationId;
Jeff Pu74e25d22024-01-08 22:21:20 +0000104 bool mFingerIsDown;
Jeff Pudef5b042023-05-25 14:28:16 -0400105
Jeff Pu343ca942022-09-14 15:56:30 -0400106 private:
107 static constexpr int32_t FINGERPRINT_ACQUIRED_VENDOR_BASE = 1000;
108 static constexpr int32_t FINGERPRINT_ERROR_VENDOR_BASE = 1000;
109 std::pair<AcquiredInfo, int32_t> convertAcquiredInfo(int32_t code);
110 std::pair<Error, int32_t> convertError(int32_t code);
Jeff Pu52653182022-10-12 16:27:23 -0400111 int32_t getRandomInRange(int32_t bound1, int32_t bound2);
Jeff Pu437516e2023-06-28 15:21:21 +0000112 bool checkSensorLockout(ISessionCallback*);
Jeff Pub00dade2024-08-06 10:59:21 -0400113 void clearLockout(ISessionCallback* cb, bool dueToTimeout = false);
Jeff Pu74e25d22024-01-08 22:21:20 +0000114 void waitForFingerDown(ISessionCallback* cb, const std::future<void>& cancel);
Jeff Pu52653182022-10-12 16:27:23 -0400115
116 FakeLockoutTracker mLockoutTracker;
Jeff Puc6f21462023-08-04 13:41:37 +0000117
118 protected:
119 // lockout timer
120 void lockoutTimerExpired(ISessionCallback* cb);
121 bool isLockoutTimerSupported;
122 bool isLockoutTimerStarted;
123 bool isLockoutTimerAborted;
124
125 public:
126 void startLockoutTimer(int64_t timeout, ISessionCallback* cb);
127 bool getLockoutTimerStarted() { return isLockoutTimerStarted; }
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800128};
129
f8c8e4c62021-03-05 05:12:58 +0000130} // namespace aidl::android::hardware::biometrics::fingerprint