1 /* 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 #pragma once 18 19 #define LOG_TAG "FingerprintVirtualHal" 20 21 #include <aidl/android/hardware/biometrics/common/SensorStrength.h> 22 #include <aidl/android/hardware/biometrics/fingerprint/ISessionCallback.h> 23 #include <android/binder_to_string.h> 24 #include <string> 25 26 #include <random> 27 28 #include <aidl/android/hardware/biometrics/fingerprint/SensorLocation.h> 29 #include <future> 30 #include <vector> 31 32 #include "FakeLockoutTracker.h" 33 34 using namespace ::aidl::android::hardware::biometrics::common; 35 36 namespace aidl::android::hardware::biometrics::fingerprint { 37 38 // A fake engine that is backed by system properties instead of hardware. 39 class FakeFingerprintEngine { 40 public: 41 FakeFingerprintEngine(); ~FakeFingerprintEngine()42 virtual ~FakeFingerprintEngine() {} 43 44 void generateChallengeImpl(ISessionCallback* cb); 45 void revokeChallengeImpl(ISessionCallback* cb, int64_t challenge); 46 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); 51 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*/); 56 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(); 68 69 virtual void fingerDownAction(); 70 71 int32_t getLatency(const std::vector<std::optional<std::int32_t>>& latencyVec); 72 73 std::mt19937 mRandom; 74 75 enum class WorkMode : int8_t { kIdle = 0, kAuthenticate, kEnroll, kDetectInteract }; 76 getWorkMode()77 WorkMode getWorkMode() { return mWorkMode; } notifyFingerdown()78 void notifyFingerdown() { mFingerIsDown = true; } 79 toString()80 virtual std::string toString() const { 81 std::ostringstream os; 82 os << "----- FakeFingerprintEngine:: -----" << std::endl; 83 os << "mWorkMode:" << (int)mWorkMode; 84 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 90 protected: 91 virtual void updateContext(WorkMode mode, ISessionCallback* cb, std::future<void>& cancel, 92 int64_t operationId, const keymaster::HardwareAuthToken& hat); 93 94 bool onEnrollFingerDown(ISessionCallback* cb, const keymaster::HardwareAuthToken& hat, 95 const std::future<void>& cancel); 96 bool onAuthenticateFingerDown(ISessionCallback* cb, int64_t, const std::future<void>& cancel); 97 bool onDetectInteractFingerDown(ISessionCallback* cb, const std::future<void>& cancel); 98 99 WorkMode mWorkMode; 100 ISessionCallback* mCb; 101 keymaster::HardwareAuthToken mHat; 102 std::future<void> mCancel; 103 int64_t mOperationId; 104 bool mFingerIsDown; 105 106 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); 111 int32_t getRandomInRange(int32_t bound1, int32_t bound2); 112 bool checkSensorLockout(ISessionCallback*); 113 void clearLockout(ISessionCallback* cb); 114 void waitForFingerDown(ISessionCallback* cb, const std::future<void>& cancel); 115 116 FakeLockoutTracker mLockoutTracker; 117 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); getLockoutTimerStarted()127 bool getLockoutTimerStarted() { return isLockoutTimerStarted; } 128 }; 129 130 } // namespace aidl::android::hardware::biometrics::fingerprint 131