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 | |
| 30 | class Session : public BnSession { |
| 31 | public: |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 32 | Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb, |
| 33 | FakeFingerprintEngine* engine, WorkerThread* worker); |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 34 | |
f | 8c8e4c6 | 2021-03-05 05:12:58 +0000 | [diff] [blame] | 35 | ndk::ScopedAStatus generateChallenge(int32_t cookie) override; |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 36 | |
| 37 | ndk::ScopedAStatus revokeChallenge(int32_t cookie, int64_t challenge) override; |
| 38 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 39 | ndk::ScopedAStatus enroll(int32_t cookie, const keymaster::HardwareAuthToken& hat, |
Ilya Matyukhin | 124e70a | 2021-02-12 13:00:15 -0800 | [diff] [blame] | 40 | std::shared_ptr<common::ICancellationSignal>* out) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 41 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 42 | ndk::ScopedAStatus authenticate(int32_t cookie, int64_t operationId, |
Ilya Matyukhin | 124e70a | 2021-02-12 13:00:15 -0800 | [diff] [blame] | 43 | std::shared_ptr<common::ICancellationSignal>* out) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 44 | |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 45 | ndk::ScopedAStatus detectInteraction( |
Ilya Matyukhin | 124e70a | 2021-02-12 13:00:15 -0800 | [diff] [blame] | 46 | int32_t cookie, std::shared_ptr<common::ICancellationSignal>* out) override; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 47 | |
| 48 | ndk::ScopedAStatus enumerateEnrollments(int32_t cookie) override; |
| 49 | |
| 50 | ndk::ScopedAStatus removeEnrollments(int32_t cookie, |
| 51 | const std::vector<int32_t>& enrollmentIds) override; |
| 52 | |
| 53 | ndk::ScopedAStatus getAuthenticatorId(int32_t cookie) override; |
| 54 | |
Kevin Chyn | 4d0df26 | 2020-11-16 12:39:44 -0800 | [diff] [blame] | 55 | ndk::ScopedAStatus invalidateAuthenticatorId(int32_t cookie) override; |
Kevin Chyn | 6e862c3 | 2020-09-16 18:27:37 -0700 | [diff] [blame] | 56 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 57 | ndk::ScopedAStatus resetLockout(int32_t cookie, |
| 58 | const keymaster::HardwareAuthToken& hat) override; |
| 59 | |
Ilya Matyukhin | 71005c5 | 2021-02-17 12:44:14 -0800 | [diff] [blame] | 60 | ndk::ScopedAStatus close(int32_t cookie) override; |
| 61 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 62 | ndk::ScopedAStatus onPointerDown(int32_t pointerId, int32_t x, int32_t y, float minor, |
| 63 | float major) override; |
| 64 | |
| 65 | ndk::ScopedAStatus onPointerUp(int32_t pointerId) override; |
| 66 | |
| 67 | ndk::ScopedAStatus onUiReady() override; |
| 68 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 69 | bool isClosed(); |
| 70 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 71 | private: |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 72 | // Crashes the HAL if it's not currently idling because that would be an invalid state machine |
| 73 | // transition. Otherwise, sets the scheduled state to the given state. |
| 74 | void scheduleStateOrCrash(SessionState state); |
| 75 | |
| 76 | // Crashes the HAL if the provided state doesn't match the previously scheduled state. |
| 77 | // Otherwise, transitions into the provided state, clears the scheduled state, and notifies |
| 78 | // the client about the transition by calling ISessionCallback#onStateChanged. |
| 79 | void enterStateOrCrash(int cookie, SessionState state); |
| 80 | |
| 81 | // Sets the current state to SessionState::IDLING and notifies the client about the transition |
| 82 | // by calling ISessionCallback#onStateChanged. |
| 83 | void enterIdling(int cookie); |
| 84 | |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 85 | // The sensor and user IDs for which this session was created. |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 86 | int32_t mSensorId; |
| 87 | int32_t mUserId; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 88 | |
| 89 | // Callback for talking to the framework. This callback must only be called from non-binder |
| 90 | // threads to prevent nested binder calls and consequently a binder thread exhaustion. |
| 91 | // 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] | 92 | std::shared_ptr<ISessionCallback> mCb; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 93 | |
| 94 | // Module that communicates to the actual fingerprint hardware, keystore, TEE, etc. In real |
| 95 | // life such modules typically consume a lot of memory and are slow to initialize. This is here |
| 96 | // to showcase how such a module can be used within a Session without incurring the high |
| 97 | // initialization costs every time a Session is constructed. |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 98 | FakeFingerprintEngine* mEngine; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 99 | |
| 100 | // Worker thread that allows to schedule tasks for asynchronous execution. |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 101 | WorkerThread* mWorker; |
f | 48dc9fc | 2021-03-10 04:40:58 +0000 | [diff] [blame] | 102 | |
| 103 | // Simple representation of the session's state machine. These are atomic because they can be |
| 104 | // modified from both the main and the worker threads. |
| 105 | std::atomic<SessionState> mScheduledState; |
| 106 | std::atomic<SessionState> mCurrentState; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace aidl::android::hardware::biometrics::fingerprint |