blob: 97d5645c9297cbfd6c6eb3374acdd96a228025e1 [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
30class Session : public BnSession {
31 public:
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080032 Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb,
33 FakeFingerprintEngine* engine, WorkerThread* worker);
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070034
f8c8e4c62021-03-05 05:12:58 +000035 ndk::ScopedAStatus generateChallenge(int32_t cookie) override;
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070036
37 ndk::ScopedAStatus revokeChallenge(int32_t cookie, int64_t challenge) override;
38
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070039 ndk::ScopedAStatus enroll(int32_t cookie, const keymaster::HardwareAuthToken& hat,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080040 std::shared_ptr<common::ICancellationSignal>* out) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070041
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080042 ndk::ScopedAStatus authenticate(int32_t cookie, int64_t operationId,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080043 std::shared_ptr<common::ICancellationSignal>* out) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070044
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070045 ndk::ScopedAStatus detectInteraction(
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080046 int32_t cookie, std::shared_ptr<common::ICancellationSignal>* out) override;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070047
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 Chyn4d0df262020-11-16 12:39:44 -080055 ndk::ScopedAStatus invalidateAuthenticatorId(int32_t cookie) override;
Kevin Chyn6e862c32020-09-16 18:27:37 -070056
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070057 ndk::ScopedAStatus resetLockout(int32_t cookie,
58 const keymaster::HardwareAuthToken& hat) override;
59
Ilya Matyukhin71005c52021-02-17 12:44:14 -080060 ndk::ScopedAStatus close(int32_t cookie) override;
61
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070062 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 Matyukhin48ff8962021-02-22 13:13:13 -080069 bool isClosed();
70
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070071 private:
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080072 // 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
f48dc9fc2021-03-10 04:40:58 +000085 // The sensor and user IDs for which this session was created.
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080086 int32_t mSensorId;
87 int32_t mUserId;
f48dc9fc2021-03-10 04:40:58 +000088
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 Matyukhin124e70a2021-02-12 13:00:15 -080092 std::shared_ptr<ISessionCallback> mCb;
f48dc9fc2021-03-10 04:40:58 +000093
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 Matyukhin48ff8962021-02-22 13:13:13 -080098 FakeFingerprintEngine* mEngine;
f48dc9fc2021-03-10 04:40:58 +000099
100 // Worker thread that allows to schedule tasks for asynchronous execution.
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800101 WorkerThread* mWorker;
f48dc9fc2021-03-10 04:40:58 +0000102
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 Matyukhina9a3c852020-08-18 03:09:41 -0700107};
108
109} // namespace aidl::android::hardware::biometrics::fingerprint