Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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/fingerprint/BnSession.h> |
| 20 | #include <aidl/android/hardware/biometrics/fingerprint/ISessionCallback.h> |
| 21 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 22 | #include "FakeFingerprintEngine.h" |
| 23 | #include "WorkerThread.h" |
| 24 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 25 | namespace aidl::android::hardware::biometrics::fingerprint { |
| 26 | |
Kevin Chyn | e33abd6 | 2020-09-18 10:31:50 -0700 | [diff] [blame] | 27 | namespace common = aidl::android::hardware::biometrics::common; |
| 28 | namespace keymaster = aidl::android::hardware::keymaster; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 29 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 30 | enum class SessionState { |
| 31 | IDLING, |
| 32 | CLOSED, |
| 33 | GENERATING_CHALLENGE, |
| 34 | REVOKING_CHALLENGE, |
| 35 | ENROLLING, |
| 36 | AUTHENTICATING, |
| 37 | DETECTING_INTERACTION, |
| 38 | ENUMERATING_ENROLLMENTS, |
| 39 | REMOVING_ENROLLMENTS, |
| 40 | GETTING_AUTHENTICATOR_ID, |
| 41 | INVALIDATING_AUTHENTICATOR_ID, |
| 42 | RESETTING_LOCKOUT, |
| 43 | }; |
| 44 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 45 | class Session : public BnSession { |
| 46 | public: |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 47 | Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb, |
| 48 | FakeFingerprintEngine* engine, WorkerThread* worker); |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 49 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 50 | ndk::ScopedAStatus generateChallenge() override; |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 51 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 52 | ndk::ScopedAStatus revokeChallenge(int64_t challenge) override; |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 53 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 54 | ndk::ScopedAStatus enroll(const keymaster::HardwareAuthToken& hat, |
Ilya Matyukhin | 124e70a | 2021-02-12 13:00:15 -0800 | [diff] [blame] | 55 | std::shared_ptr<common::ICancellationSignal>* out) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 56 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 57 | ndk::ScopedAStatus authenticate(int64_t operationId, |
Ilya Matyukhin | 124e70a | 2021-02-12 13:00:15 -0800 | [diff] [blame] | 58 | std::shared_ptr<common::ICancellationSignal>* out) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 59 | |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 60 | ndk::ScopedAStatus detectInteraction( |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 61 | std::shared_ptr<common::ICancellationSignal>* out) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 62 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 63 | ndk::ScopedAStatus enumerateEnrollments() override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 64 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 65 | ndk::ScopedAStatus removeEnrollments(const std::vector<int32_t>& enrollmentIds) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 66 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 67 | ndk::ScopedAStatus getAuthenticatorId() override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 68 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 69 | ndk::ScopedAStatus invalidateAuthenticatorId() override; |
Kevin Chyn | 6e862c3 | 2020-09-16 18:27:37 -0700 | [diff] [blame] | 70 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 71 | ndk::ScopedAStatus resetLockout(const keymaster::HardwareAuthToken& hat) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 72 | |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 73 | ndk::ScopedAStatus close() override; |
Ilya Matyukhin | 71005c5 | 2021-02-17 12:44:14 -0800 | [diff] [blame] | 74 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 75 | ndk::ScopedAStatus onPointerDown(int32_t pointerId, int32_t x, int32_t y, float minor, |
| 76 | float major) override; |
| 77 | |
| 78 | ndk::ScopedAStatus onPointerUp(int32_t pointerId) override; |
| 79 | |
| 80 | ndk::ScopedAStatus onUiReady() override; |
| 81 | |
Joe Bolinger | 13cb0fb | 2021-12-03 12:45:48 -0800 | [diff] [blame] | 82 | ndk::ScopedAStatus authenticateWithContext( |
| 83 | int64_t operationId, const common::OperationContext& context, |
| 84 | std::shared_ptr<common::ICancellationSignal>* out) override; |
| 85 | |
| 86 | ndk::ScopedAStatus enrollWithContext( |
| 87 | const keymaster::HardwareAuthToken& hat, const common::OperationContext& context, |
| 88 | std::shared_ptr<common::ICancellationSignal>* out) override; |
| 89 | |
| 90 | ndk::ScopedAStatus detectInteractionWithContext( |
| 91 | const common::OperationContext& context, |
| 92 | std::shared_ptr<common::ICancellationSignal>* out) override; |
| 93 | |
| 94 | ndk::ScopedAStatus onPointerDownWithContext(const PointerContext& context) override; |
| 95 | |
| 96 | ndk::ScopedAStatus onPointerUpWithContext(const PointerContext& context) override; |
| 97 | |
Joe Bolinger | 25e9823 | 2022-01-24 18:56:23 +0000 | [diff] [blame^] | 98 | ndk::ScopedAStatus onContextChanged(const common::OperationContext& context) override; |
| 99 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 100 | bool isClosed(); |
| 101 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 102 | private: |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 103 | // Crashes the HAL if it's not currently idling because that would be an invalid state machine |
| 104 | // transition. Otherwise, sets the scheduled state to the given state. |
| 105 | void scheduleStateOrCrash(SessionState state); |
| 106 | |
| 107 | // Crashes the HAL if the provided state doesn't match the previously scheduled state. |
| 108 | // Otherwise, transitions into the provided state, clears the scheduled state, and notifies |
| 109 | // the client about the transition by calling ISessionCallback#onStateChanged. |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 110 | void enterStateOrCrash(SessionState state); |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 111 | |
| 112 | // Sets the current state to SessionState::IDLING and notifies the client about the transition |
| 113 | // by calling ISessionCallback#onStateChanged. |
Ilya Matyukhin | aea213b | 2021-03-23 19:01:42 -0700 | [diff] [blame] | 114 | void enterIdling(); |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 115 | |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 116 | // The sensor and user IDs for which this session was created. |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 117 | int32_t mSensorId; |
| 118 | int32_t mUserId; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 119 | |
| 120 | // Callback for talking to the framework. This callback must only be called from non-binder |
| 121 | // threads to prevent nested binder calls and consequently a binder thread exhaustion. |
| 122 | // Practically, it means that this callback should always be called from the worker thread. |
Ilya Matyukhin | 124e70a | 2021-02-12 13:00:15 -0800 | [diff] [blame] | 123 | std::shared_ptr<ISessionCallback> mCb; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 124 | |
| 125 | // Module that communicates to the actual fingerprint hardware, keystore, TEE, etc. In real |
| 126 | // life such modules typically consume a lot of memory and are slow to initialize. This is here |
| 127 | // to showcase how such a module can be used within a Session without incurring the high |
| 128 | // initialization costs every time a Session is constructed. |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 129 | FakeFingerprintEngine* mEngine; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 130 | |
| 131 | // Worker thread that allows to schedule tasks for asynchronous execution. |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 132 | WorkerThread* mWorker; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 133 | |
| 134 | // Simple representation of the session's state machine. These are atomic because they can be |
| 135 | // modified from both the main and the worker threads. |
| 136 | std::atomic<SessionState> mScheduledState; |
| 137 | std::atomic<SessionState> mCurrentState; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | } // namespace aidl::android::hardware::biometrics::fingerprint |