blob: 2450115aaf4d7b38cf1dc03dff20b23fb87ef51c [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 Pu343ca942022-09-14 15:56:30 -040071 std::vector<int32_t> parseIntSequence(const std::string& str, const std::string& sep = ",");
72
73 std::vector<std::vector<int32_t>> parseEnrollmentCapture(const std::string& str);
74
Jeff Pu52653182022-10-12 16:27:23 -040075 int32_t getLatency(const std::vector<std::optional<std::int32_t>>& latencyVec);
76
Ilya Matyukhin1d524382021-07-02 20:33:51 +000077 std::mt19937 mRandom;
Jeff Pu343ca942022-09-14 15:56:30 -040078
Jeff Pudef5b042023-05-25 14:28:16 -040079 enum class WorkMode : int8_t { kIdle = 0, kAuthenticate, kEnroll, kDetectInteract };
80
81 WorkMode getWorkMode() { return mWorkMode; }
82
Jeff Pu52653182022-10-12 16:27:23 -040083 virtual std::string toString() const {
84 std::ostringstream os;
85 os << "----- FakeFingerprintEngine:: -----" << std::endl;
Jeff Pudef5b042023-05-25 14:28:16 -040086 os << "mWorkMode:" << (int)mWorkMode;
Jeff Pu52653182022-10-12 16:27:23 -040087 os << "acquiredVendorInfoBase:" << FINGERPRINT_ACQUIRED_VENDOR_BASE;
88 os << ", errorVendorBase:" << FINGERPRINT_ERROR_VENDOR_BASE << std::endl;
89 os << mLockoutTracker.toString();
90 return os.str();
91 }
92
Jeff Pudef5b042023-05-25 14:28:16 -040093 protected:
94 virtual void updateContext(WorkMode mode, ISessionCallback* cb, std::future<void>& cancel,
95 int64_t operationId, const keymaster::HardwareAuthToken& hat);
96
Jeff Pu073af182023-07-12 18:53:52 +000097 bool onEnrollFingerDown(ISessionCallback* cb, const keymaster::HardwareAuthToken& hat,
Jeff Pudef5b042023-05-25 14:28:16 -040098 const std::future<void>& cancel);
Jeff Pu073af182023-07-12 18:53:52 +000099 bool onAuthenticateFingerDown(ISessionCallback* cb, int64_t, const std::future<void>& cancel);
100 bool onDetectInteractFingerDown(ISessionCallback* cb, const std::future<void>& cancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400101
102 WorkMode mWorkMode;
103 ISessionCallback* mCb;
104 keymaster::HardwareAuthToken mHat;
105 std::future<void> mCancel;
106 int64_t mOperationId;
107
Jeff Pu343ca942022-09-14 15:56:30 -0400108 private:
109 static constexpr int32_t FINGERPRINT_ACQUIRED_VENDOR_BASE = 1000;
110 static constexpr int32_t FINGERPRINT_ERROR_VENDOR_BASE = 1000;
111 std::pair<AcquiredInfo, int32_t> convertAcquiredInfo(int32_t code);
112 std::pair<Error, int32_t> convertError(int32_t code);
Jeff Pu52653182022-10-12 16:27:23 -0400113 bool parseEnrollmentCaptureSingle(const std::string& str,
114 std::vector<std::vector<int32_t>>& res);
115 int32_t getRandomInRange(int32_t bound1, int32_t bound2);
Jeff Pu437516e2023-06-28 15:21:21 +0000116 bool checkSensorLockout(ISessionCallback*);
Jeff Puc6f21462023-08-04 13:41:37 +0000117 void clearLockout(ISessionCallback* cb);
Jeff Pu52653182022-10-12 16:27:23 -0400118
119 FakeLockoutTracker mLockoutTracker;
Jeff Puc6f21462023-08-04 13:41:37 +0000120
121 protected:
122 // lockout timer
123 void lockoutTimerExpired(ISessionCallback* cb);
124 bool isLockoutTimerSupported;
125 bool isLockoutTimerStarted;
126 bool isLockoutTimerAborted;
127
128 public:
129 void startLockoutTimer(int64_t timeout, ISessionCallback* cb);
130 bool getLockoutTimerStarted() { return isLockoutTimerStarted; }
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800131};
132
f8c8e4c62021-03-05 05:12:58 +0000133} // namespace aidl::android::hardware::biometrics::fingerprint