blob: f030f138f5cfd7bfd50186897b297c84159e5eba [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
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080017#include "Session.h"
18
Kevin Chyn146f6c82021-02-10 11:32:35 -080019#include <android-base/logging.h>
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070020
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080021#include "CancellationSignal.h"
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070022
23namespace aidl::android::hardware::biometrics::fingerprint {
24
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080025Session::Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb,
26 FakeFingerprintEngine* engine, WorkerThread* worker)
27 : mSensorId(sensorId),
28 mUserId(userId),
29 mCb(std::move(cb)),
30 mEngine(engine),
31 mWorker(worker),
32 mScheduledState(SessionState::IDLING),
33 mCurrentState(SessionState::IDLING) {
34 CHECK_GE(mSensorId, 0);
35 CHECK_GE(mUserId, 0);
36 CHECK(mEngine);
37 CHECK(mWorker);
38 CHECK(mCb);
39}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070040
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080041void Session::scheduleStateOrCrash(SessionState state) {
42 CHECK(mScheduledState == SessionState::IDLING);
43 CHECK(mCurrentState == SessionState::IDLING);
44 mScheduledState = state;
45}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070046
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080047void Session::enterStateOrCrash(int cookie, SessionState state) {
48 CHECK(mScheduledState == state);
f48dc9fc2021-03-10 04:40:58 +000049 mCurrentState = state;
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080050 mScheduledState = SessionState::IDLING;
51 mCb->onStateChanged(cookie, mCurrentState);
52}
53
54void Session::enterIdling(int cookie) {
55 mCurrentState = SessionState::IDLING;
56 mCb->onStateChanged(cookie, mCurrentState);
57}
58
59bool Session::isClosed() {
60 return mCurrentState == SessionState::CLOSED;
61}
62
f8c8e4c62021-03-05 05:12:58 +000063ndk::ScopedAStatus Session::generateChallenge(int32_t cookie) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080064 LOG(INFO) << "generateChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080065 scheduleStateOrCrash(SessionState::GENERATING_CHALLENGE);
66
f8c8e4c62021-03-05 05:12:58 +000067 mWorker->schedule(Callable::from([this, cookie] {
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080068 enterStateOrCrash(cookie, SessionState::GENERATING_CHALLENGE);
f8c8e4c62021-03-05 05:12:58 +000069 mEngine->generateChallengeImpl(mCb.get());
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080070 enterIdling(cookie);
71 }));
72
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070073 return ndk::ScopedAStatus::ok();
74}
75
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080076ndk::ScopedAStatus Session::revokeChallenge(int32_t cookie, int64_t challenge) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080077 LOG(INFO) << "revokeChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080078 scheduleStateOrCrash(SessionState::REVOKING_CHALLENGE);
79
80 mWorker->schedule(Callable::from([this, cookie, challenge] {
81 enterStateOrCrash(cookie, SessionState::REVOKING_CHALLENGE);
82 mEngine->revokeChallengeImpl(mCb.get(), challenge);
83 enterIdling(cookie);
84 }));
85
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070086 return ndk::ScopedAStatus::ok();
87}
88
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080089ndk::ScopedAStatus Session::enroll(int32_t cookie, const keymaster::HardwareAuthToken& hat,
90 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080091 LOG(INFO) << "enroll";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080092 scheduleStateOrCrash(SessionState::ENROLLING);
93
94 std::promise<void> cancellationPromise;
95 auto cancFuture = cancellationPromise.get_future();
96
97 mWorker->schedule(Callable::from([this, cookie, hat, cancFuture = std::move(cancFuture)] {
98 enterStateOrCrash(cookie, SessionState::ENROLLING);
99 if (shouldCancel(cancFuture)) {
100 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
101 } else {
102 mEngine->enrollImpl(mCb.get(), hat);
103 }
104 enterIdling(cookie);
105 }));
106
107 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700108 return ndk::ScopedAStatus::ok();
109}
110
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800111ndk::ScopedAStatus Session::authenticate(int32_t cookie, int64_t operationId,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -0800112 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800113 LOG(INFO) << "authenticate";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800114 scheduleStateOrCrash(SessionState::AUTHENTICATING);
115
116 std::promise<void> cancPromise;
117 auto cancFuture = cancPromise.get_future();
118
119 mWorker->schedule(
120 Callable::from([this, cookie, operationId, cancFuture = std::move(cancFuture)] {
121 enterStateOrCrash(cookie, SessionState::AUTHENTICATING);
122 if (shouldCancel(cancFuture)) {
123 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
124 } else {
125 mEngine->authenticateImpl(mCb.get(), operationId);
126 }
127 enterIdling(cookie);
128 }));
129
130 *out = SharedRefBase::make<CancellationSignal>(std::move(cancPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700131 return ndk::ScopedAStatus::ok();
132}
133
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800134ndk::ScopedAStatus Session::detectInteraction(int32_t cookie,
135 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800136 LOG(INFO) << "detectInteraction";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800137 scheduleStateOrCrash(SessionState::DETECTING_INTERACTION);
138
139 std::promise<void> cancellationPromise;
140 auto cancFuture = cancellationPromise.get_future();
141
142 mWorker->schedule(Callable::from([this, cookie, cancFuture = std::move(cancFuture)] {
143 enterStateOrCrash(cookie, SessionState::DETECTING_INTERACTION);
144 if (shouldCancel(cancFuture)) {
145 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
146 } else {
147 mEngine->detectInteractionImpl(mCb.get());
148 }
149 enterIdling(cookie);
150 }));
151
152 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700153 return ndk::ScopedAStatus::ok();
154}
155
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800156ndk::ScopedAStatus Session::enumerateEnrollments(int32_t cookie) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800157 LOG(INFO) << "enumerateEnrollments";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800158 scheduleStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS);
159
160 mWorker->schedule(Callable::from([this, cookie] {
161 enterStateOrCrash(cookie, SessionState::ENUMERATING_ENROLLMENTS);
162 mEngine->enumerateEnrollmentsImpl(mCb.get());
163 enterIdling(cookie);
164 }));
165
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700166 return ndk::ScopedAStatus::ok();
167}
168
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800169ndk::ScopedAStatus Session::removeEnrollments(int32_t cookie,
170 const std::vector<int32_t>& enrollmentIds) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800171 LOG(INFO) << "removeEnrollments";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800172 scheduleStateOrCrash(SessionState::REMOVING_ENROLLMENTS);
173
174 mWorker->schedule(Callable::from([this, cookie, enrollmentIds] {
175 enterStateOrCrash(cookie, SessionState::REMOVING_ENROLLMENTS);
176 mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds);
177 enterIdling(cookie);
178 }));
179
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700180 return ndk::ScopedAStatus::ok();
181}
182
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800183ndk::ScopedAStatus Session::getAuthenticatorId(int32_t cookie) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800184 LOG(INFO) << "getAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800185 scheduleStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID);
186
187 mWorker->schedule(Callable::from([this, cookie] {
188 enterStateOrCrash(cookie, SessionState::GETTING_AUTHENTICATOR_ID);
189 mEngine->getAuthenticatorIdImpl(mCb.get());
190 enterIdling(cookie);
191 }));
192
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700193 return ndk::ScopedAStatus::ok();
194}
195
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800196ndk::ScopedAStatus Session::invalidateAuthenticatorId(int32_t cookie) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800197 LOG(INFO) << "invalidateAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800198 scheduleStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID);
199
200 mWorker->schedule(Callable::from([this, cookie] {
201 enterStateOrCrash(cookie, SessionState::INVALIDATING_AUTHENTICATOR_ID);
202 mEngine->invalidateAuthenticatorIdImpl(mCb.get());
203 enterIdling(cookie);
204 }));
205
Kevin Chyn6e862c32020-09-16 18:27:37 -0700206 return ndk::ScopedAStatus::ok();
207}
208
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800209ndk::ScopedAStatus Session::resetLockout(int32_t cookie, const keymaster::HardwareAuthToken& hat) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800210 LOG(INFO) << "resetLockout";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800211 scheduleStateOrCrash(SessionState::RESETTING_LOCKOUT);
212
213 mWorker->schedule(Callable::from([this, cookie, hat] {
214 enterStateOrCrash(cookie, SessionState::RESETTING_LOCKOUT);
215 mEngine->resetLockoutImpl(mCb.get(), hat);
216 enterIdling(cookie);
217 }));
218
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700219 return ndk::ScopedAStatus::ok();
220}
221
Ilya Matyukhincbbfa932021-03-22 13:25:15 -0700222ndk::ScopedAStatus Session::close(int32_t /*cookie*/) {
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800223 LOG(INFO) << "close";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800224 CHECK(mCurrentState == SessionState::IDLING) << "Can't close a non-idling session. Crashing.";
225 mCurrentState = SessionState::CLOSED;
Ilya Matyukhincbbfa932021-03-22 13:25:15 -0700226 mCb->onSessionClosed();
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800227 return ndk::ScopedAStatus::ok();
228}
229
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700230ndk::ScopedAStatus Session::onPointerDown(int32_t /*pointerId*/, int32_t /*x*/, int32_t /*y*/,
231 float /*minor*/, float /*major*/) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800232 LOG(INFO) << "onPointerDown";
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700233 return ndk::ScopedAStatus::ok();
234}
235
236ndk::ScopedAStatus Session::onPointerUp(int32_t /*pointerId*/) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800237 LOG(INFO) << "onPointerUp";
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700238 return ndk::ScopedAStatus::ok();
239}
240
241ndk::ScopedAStatus Session::onUiReady() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800242 LOG(INFO) << "onUiReady";
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700243 return ndk::ScopedAStatus::ok();
244}
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800245
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700246} // namespace aidl::android::hardware::biometrics::fingerprint