blob: 9e464229e1ec7f803e3b6b58e2b3ae0217160974 [file] [log] [blame]
Ilya Matyukhina9a3c852020-08-18 03:09:41 -07001/*
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 Matyukhin48ff8962021-02-22 13:13:13 -080022#include "FakeFingerprintEngine.h"
23#include "WorkerThread.h"
24
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070025namespace aidl::android::hardware::biometrics::fingerprint {
26
Kevin Chyne33abd62020-09-18 10:31:50 -070027namespace common = aidl::android::hardware::biometrics::common;
28namespace keymaster = aidl::android::hardware::keymaster;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070029
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070030enum 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 Matyukhina9a3c852020-08-18 03:09:41 -070045class Session : public BnSession {
46 public:
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080047 Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb,
48 FakeFingerprintEngine* engine, WorkerThread* worker);
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070049
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070050 ndk::ScopedAStatus generateChallenge() override;
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070051
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070052 ndk::ScopedAStatus revokeChallenge(int64_t challenge) override;
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070053
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070054 ndk::ScopedAStatus enroll(const keymaster::HardwareAuthToken& hat,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080055 std::shared_ptr<common::ICancellationSignal>* out) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070056
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070057 ndk::ScopedAStatus authenticate(int64_t operationId,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080058 std::shared_ptr<common::ICancellationSignal>* out) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070059
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070060 ndk::ScopedAStatus detectInteraction(
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070061 std::shared_ptr<common::ICancellationSignal>* out) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070062
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070063 ndk::ScopedAStatus enumerateEnrollments() override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070064
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070065 ndk::ScopedAStatus removeEnrollments(const std::vector<int32_t>& enrollmentIds) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070066
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070067 ndk::ScopedAStatus getAuthenticatorId() override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070068
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070069 ndk::ScopedAStatus invalidateAuthenticatorId() override;
Kevin Chyn6e862c32020-09-16 18:27:37 -070070
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070071 ndk::ScopedAStatus resetLockout(const keymaster::HardwareAuthToken& hat) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070072
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070073 ndk::ScopedAStatus close() override;
Ilya Matyukhin71005c52021-02-17 12:44:14 -080074
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070075 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
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080082 bool isClosed();
83
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070084 private:
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080085 // Crashes the HAL if it's not currently idling because that would be an invalid state machine
86 // transition. Otherwise, sets the scheduled state to the given state.
87 void scheduleStateOrCrash(SessionState state);
88
89 // Crashes the HAL if the provided state doesn't match the previously scheduled state.
90 // Otherwise, transitions into the provided state, clears the scheduled state, and notifies
91 // the client about the transition by calling ISessionCallback#onStateChanged.
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070092 void enterStateOrCrash(SessionState state);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080093
94 // Sets the current state to SessionState::IDLING and notifies the client about the transition
95 // by calling ISessionCallback#onStateChanged.
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070096 void enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080097
f48dc9fc2021-03-10 04:40:58 +000098 // The sensor and user IDs for which this session was created.
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080099 int32_t mSensorId;
100 int32_t mUserId;
f48dc9fc2021-03-10 04:40:58 +0000101
102 // Callback for talking to the framework. This callback must only be called from non-binder
103 // threads to prevent nested binder calls and consequently a binder thread exhaustion.
104 // Practically, it means that this callback should always be called from the worker thread.
Ilya Matyukhin124e70a2021-02-12 13:00:15 -0800105 std::shared_ptr<ISessionCallback> mCb;
f48dc9fc2021-03-10 04:40:58 +0000106
107 // Module that communicates to the actual fingerprint hardware, keystore, TEE, etc. In real
108 // life such modules typically consume a lot of memory and are slow to initialize. This is here
109 // to showcase how such a module can be used within a Session without incurring the high
110 // initialization costs every time a Session is constructed.
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800111 FakeFingerprintEngine* mEngine;
f48dc9fc2021-03-10 04:40:58 +0000112
113 // Worker thread that allows to schedule tasks for asynchronous execution.
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800114 WorkerThread* mWorker;
f48dc9fc2021-03-10 04:40:58 +0000115
116 // Simple representation of the session's state machine. These are atomic because they can be
117 // modified from both the main and the worker threads.
118 std::atomic<SessionState> mScheduledState;
119 std::atomic<SessionState> mCurrentState;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700120};
121
122} // namespace aidl::android::hardware::biometrics::fingerprint