blob: 7ab5af3c4d80356333276637df3a6c8484805415 [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
Joshua McCloskeyc8c0bad2022-05-10 05:17:44 +000021#include "util/CancellationSignal.h"
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070022
Jeff Pu52653182022-10-12 16:27:23 -040023#undef LOG_TAG
24#define LOG_TAG "FingerprintVirtualHalSession"
25
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070026namespace aidl::android::hardware::biometrics::fingerprint {
27
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080028Session::Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb,
29 FakeFingerprintEngine* engine, WorkerThread* worker)
30 : mSensorId(sensorId),
31 mUserId(userId),
32 mCb(std::move(cb)),
33 mEngine(engine),
34 mWorker(worker),
35 mScheduledState(SessionState::IDLING),
36 mCurrentState(SessionState::IDLING) {
37 CHECK_GE(mSensorId, 0);
38 CHECK_GE(mUserId, 0);
39 CHECK(mEngine);
40 CHECK(mWorker);
41 CHECK(mCb);
42}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070043
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080044void Session::scheduleStateOrCrash(SessionState state) {
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070045 // TODO(b/166800618): call enterIdling from the terminal callbacks and restore these checks.
46 // CHECK(mScheduledState == SessionState::IDLING);
47 // CHECK(mCurrentState == SessionState::IDLING);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080048 mScheduledState = state;
49}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070050
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070051void Session::enterStateOrCrash(SessionState state) {
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080052 CHECK(mScheduledState == state);
f48dc9fc2021-03-10 04:40:58 +000053 mCurrentState = state;
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080054 mScheduledState = SessionState::IDLING;
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080055}
56
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070057void Session::enterIdling() {
58 // TODO(b/166800618): call enterIdling from the terminal callbacks and rethink this conditional.
59 if (mCurrentState != SessionState::CLOSED) {
60 mCurrentState = SessionState::IDLING;
61 }
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080062}
63
64bool Session::isClosed() {
65 return mCurrentState == SessionState::CLOSED;
66}
67
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070068ndk::ScopedAStatus Session::generateChallenge() {
Kevin Chyn146f6c82021-02-10 11:32:35 -080069 LOG(INFO) << "generateChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080070 scheduleStateOrCrash(SessionState::GENERATING_CHALLENGE);
71
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070072 mWorker->schedule(Callable::from([this] {
73 enterStateOrCrash(SessionState::GENERATING_CHALLENGE);
f8c8e4c62021-03-05 05:12:58 +000074 mEngine->generateChallengeImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070075 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080076 }));
77
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070078 return ndk::ScopedAStatus::ok();
79}
80
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070081ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080082 LOG(INFO) << "revokeChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080083 scheduleStateOrCrash(SessionState::REVOKING_CHALLENGE);
84
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070085 mWorker->schedule(Callable::from([this, challenge] {
86 enterStateOrCrash(SessionState::REVOKING_CHALLENGE);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080087 mEngine->revokeChallengeImpl(mCb.get(), challenge);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070088 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080089 }));
90
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070091 return ndk::ScopedAStatus::ok();
92}
93
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070094ndk::ScopedAStatus Session::enroll(const keymaster::HardwareAuthToken& hat,
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080095 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080096 LOG(INFO) << "enroll";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080097 scheduleStateOrCrash(SessionState::ENROLLING);
98
99 std::promise<void> cancellationPromise;
100 auto cancFuture = cancellationPromise.get_future();
101
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700102 mWorker->schedule(Callable::from([this, hat, cancFuture = std::move(cancFuture)] {
103 enterStateOrCrash(SessionState::ENROLLING);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800104 if (shouldCancel(cancFuture)) {
105 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
106 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800107 mEngine->enrollImpl(mCb.get(), hat, cancFuture);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800108 }
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700109 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800110 }));
111
112 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700113 return ndk::ScopedAStatus::ok();
114}
115
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700116ndk::ScopedAStatus Session::authenticate(int64_t operationId,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -0800117 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800118 LOG(INFO) << "authenticate";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800119 scheduleStateOrCrash(SessionState::AUTHENTICATING);
120
121 std::promise<void> cancPromise;
122 auto cancFuture = cancPromise.get_future();
123
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700124 mWorker->schedule(Callable::from([this, operationId, cancFuture = std::move(cancFuture)] {
125 enterStateOrCrash(SessionState::AUTHENTICATING);
126 if (shouldCancel(cancFuture)) {
127 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
128 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800129 mEngine->authenticateImpl(mCb.get(), operationId, cancFuture);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700130 }
131 enterIdling();
132 }));
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800133
134 *out = SharedRefBase::make<CancellationSignal>(std::move(cancPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700135 return ndk::ScopedAStatus::ok();
136}
137
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700138ndk::ScopedAStatus Session::detectInteraction(std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800139 LOG(INFO) << "detectInteraction";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800140 scheduleStateOrCrash(SessionState::DETECTING_INTERACTION);
141
142 std::promise<void> cancellationPromise;
143 auto cancFuture = cancellationPromise.get_future();
144
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700145 mWorker->schedule(Callable::from([this, cancFuture = std::move(cancFuture)] {
146 enterStateOrCrash(SessionState::DETECTING_INTERACTION);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800147 if (shouldCancel(cancFuture)) {
148 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
149 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800150 mEngine->detectInteractionImpl(mCb.get(), cancFuture);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800151 }
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700152 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800153 }));
154
155 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700156 return ndk::ScopedAStatus::ok();
157}
158
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700159ndk::ScopedAStatus Session::enumerateEnrollments() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800160 LOG(INFO) << "enumerateEnrollments";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800161 scheduleStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS);
162
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700163 mWorker->schedule(Callable::from([this] {
164 enterStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800165 mEngine->enumerateEnrollmentsImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700166 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800167 }));
168
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700169 return ndk::ScopedAStatus::ok();
170}
171
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700172ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& enrollmentIds) {
Jeff Pu63f33c72022-07-28 16:06:23 -0400173 LOG(INFO) << "removeEnrollments, size:" << enrollmentIds.size();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800174 scheduleStateOrCrash(SessionState::REMOVING_ENROLLMENTS);
175
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700176 mWorker->schedule(Callable::from([this, enrollmentIds] {
177 enterStateOrCrash(SessionState::REMOVING_ENROLLMENTS);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800178 mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700179 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800180 }));
181
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700182 return ndk::ScopedAStatus::ok();
183}
184
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700185ndk::ScopedAStatus Session::getAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800186 LOG(INFO) << "getAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800187 scheduleStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID);
188
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700189 mWorker->schedule(Callable::from([this] {
190 enterStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800191 mEngine->getAuthenticatorIdImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700192 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800193 }));
194
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700195 return ndk::ScopedAStatus::ok();
196}
197
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700198ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800199 LOG(INFO) << "invalidateAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800200 scheduleStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID);
201
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700202 mWorker->schedule(Callable::from([this] {
203 enterStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800204 mEngine->invalidateAuthenticatorIdImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700205 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800206 }));
207
Kevin Chyn6e862c32020-09-16 18:27:37 -0700208 return ndk::ScopedAStatus::ok();
209}
210
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700211ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& hat) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800212 LOG(INFO) << "resetLockout";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800213 scheduleStateOrCrash(SessionState::RESETTING_LOCKOUT);
214
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700215 mWorker->schedule(Callable::from([this, hat] {
216 enterStateOrCrash(SessionState::RESETTING_LOCKOUT);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800217 mEngine->resetLockoutImpl(mCb.get(), hat);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700218 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800219 }));
220
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700221 return ndk::ScopedAStatus::ok();
222}
223
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700224ndk::ScopedAStatus Session::close() {
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800225 LOG(INFO) << "close";
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700226 // TODO(b/166800618): call enterIdling from the terminal callbacks and restore this check.
227 // CHECK(mCurrentState == SessionState::IDLING) << "Can't close a non-idling session.
228 // Crashing.";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800229 mCurrentState = SessionState::CLOSED;
Ilya Matyukhincbbfa932021-03-22 13:25:15 -0700230 mCb->onSessionClosed();
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800231 return ndk::ScopedAStatus::ok();
232}
233
Jeff Pu63f33c72022-07-28 16:06:23 -0400234ndk::ScopedAStatus Session::onPointerDown(int32_t pointerId, int32_t x, int32_t y, float minor,
235 float major) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800236 LOG(INFO) << "onPointerDown";
Jeff Pu63f33c72022-07-28 16:06:23 -0400237 mWorker->schedule(Callable::from([this, pointerId, x, y, minor, major] {
238 mEngine->onPointerDownImpl(pointerId, x, y, minor, major);
239 enterIdling();
240 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700241 return ndk::ScopedAStatus::ok();
242}
243
Jeff Pu63f33c72022-07-28 16:06:23 -0400244ndk::ScopedAStatus Session::onPointerUp(int32_t pointerId) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800245 LOG(INFO) << "onPointerUp";
Jeff Pu63f33c72022-07-28 16:06:23 -0400246 mWorker->schedule(Callable::from([this, pointerId] {
247 mEngine->onPointerUpImpl(pointerId);
248 enterIdling();
249 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700250 return ndk::ScopedAStatus::ok();
251}
252
253ndk::ScopedAStatus Session::onUiReady() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800254 LOG(INFO) << "onUiReady";
Jeff Pu63f33c72022-07-28 16:06:23 -0400255 mWorker->schedule(Callable::from([this] {
256 mEngine->onUiReadyImpl();
257 enterIdling();
258 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700259 return ndk::ScopedAStatus::ok();
260}
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800261
Joe Bolinger13cb0fb2021-12-03 12:45:48 -0800262ndk::ScopedAStatus Session::authenticateWithContext(
263 int64_t operationId, const common::OperationContext& /*context*/,
264 std::shared_ptr<common::ICancellationSignal>* out) {
265 return authenticate(operationId, out);
266}
267
268ndk::ScopedAStatus Session::enrollWithContext(const keymaster::HardwareAuthToken& hat,
269 const common::OperationContext& /*context*/,
270 std::shared_ptr<common::ICancellationSignal>* out) {
271 return enroll(hat, out);
272}
273
274ndk::ScopedAStatus Session::detectInteractionWithContext(
275 const common::OperationContext& /*context*/,
276 std::shared_ptr<common::ICancellationSignal>* out) {
277 return detectInteraction(out);
278}
279
280ndk::ScopedAStatus Session::onPointerDownWithContext(const PointerContext& context) {
281 return onPointerDown(context.pointerId, context.x, context.y, context.minor, context.major);
282}
283
284ndk::ScopedAStatus Session::onPointerUpWithContext(const PointerContext& context) {
285 return onPointerUp(context.pointerId);
286}
287
Joe Bolinger25e98232022-01-24 18:56:23 +0000288ndk::ScopedAStatus Session::onContextChanged(const common::OperationContext& /*context*/) {
289 return ndk::ScopedAStatus::ok();
290}
291
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700292} // namespace aidl::android::hardware::biometrics::fingerprint