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 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 75 int32_t getLatency(const std::vector<std::optional<std::int32_t>>& latencyVec); 76 77 std::mt19937 mRandom; 78 79 enum class WorkMode : int8_t { kIdle = 0, kAuthenticate, kEnroll, kDetectInteract }; 80 getWorkMode()81 WorkMode getWorkMode() { return mWorkMode; } 82 toString()83 virtual std::string toString() const { 84 std::ostringstream os; 85 os << "----- FakeFingerprintEngine:: -----" << std::endl; 86 os << "mWorkMode:" << (int)mWorkMode; 87 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 93 protected: 94 virtual void updateContext(WorkMode mode, ISessionCallback* cb, std::future<void>& cancel, 95 int64_t operationId, const keymaster::HardwareAuthToken& hat); 96 97 bool onEnrollFingerDown(ISessionCallback* cb, const keymaster::HardwareAuthToken& hat, 98 const std::future<void>& cancel); 99 bool onAuthenticateFingerDown(ISessionCallback* cb, int64_t, const std::future<void>& cancel); 100 bool onDetectInteractFingerDown(ISessionCallback* cb, const std::future<void>& cancel); 101 102 WorkMode mWorkMode; 103 ISessionCallback* mCb; 104 keymaster::HardwareAuthToken mHat; 105 std::future<void> mCancel; 106 int64_t mOperationId; 107 108 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); 113 bool parseEnrollmentCaptureSingle(const std::string& str, 114 std::vector<std::vector<int32_t>>& res); 115 int32_t getRandomInRange(int32_t bound1, int32_t bound2); 116 bool checkSensorLockout(ISessionCallback*); 117 void clearLockout(ISessionCallback* cb); 118 119 FakeLockoutTracker mLockoutTracker; 120 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); getLockoutTimerStarted()130 bool getLockoutTimerStarted() { return isLockoutTimerStarted; } 131 }; 132 133 } // namespace aidl::android::hardware::biometrics::fingerprint 134