blob: c06c9317e85b7a381139310b24938904cfcedb2f [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
Jeff Pu87e9f2b2023-05-03 17:59:21 +000028void onClientDeath(void* cookie) {
29 LOG(INFO) << "FingerprintService has died";
30 Session* session = static_cast<Session*>(cookie);
31 if (session && !session->isClosed()) {
32 session->close();
33 }
34}
35
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080036Session::Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb,
37 FakeFingerprintEngine* engine, WorkerThread* worker)
38 : mSensorId(sensorId),
39 mUserId(userId),
40 mCb(std::move(cb)),
41 mEngine(engine),
42 mWorker(worker),
43 mScheduledState(SessionState::IDLING),
44 mCurrentState(SessionState::IDLING) {
45 CHECK_GE(mSensorId, 0);
46 CHECK_GE(mUserId, 0);
47 CHECK(mEngine);
48 CHECK(mWorker);
49 CHECK(mCb);
Jeff Pu87e9f2b2023-05-03 17:59:21 +000050
51 mDeathRecipient = AIBinder_DeathRecipient_new(onClientDeath);
52}
53
54binder_status_t Session::linkToDeath(AIBinder* binder) {
55 return AIBinder_linkToDeath(binder, mDeathRecipient, this);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080056}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070057
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080058void Session::scheduleStateOrCrash(SessionState state) {
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070059 // TODO(b/166800618): call enterIdling from the terminal callbacks and restore these checks.
60 // CHECK(mScheduledState == SessionState::IDLING);
61 // CHECK(mCurrentState == SessionState::IDLING);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080062 mScheduledState = state;
63}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070064
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070065void Session::enterStateOrCrash(SessionState state) {
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080066 CHECK(mScheduledState == state);
f48dc9fc2021-03-10 04:40:58 +000067 mCurrentState = state;
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080068 mScheduledState = SessionState::IDLING;
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080069}
70
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070071void Session::enterIdling() {
72 // TODO(b/166800618): call enterIdling from the terminal callbacks and rethink this conditional.
73 if (mCurrentState != SessionState::CLOSED) {
74 mCurrentState = SessionState::IDLING;
75 }
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080076}
77
78bool Session::isClosed() {
79 return mCurrentState == SessionState::CLOSED;
80}
81
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070082ndk::ScopedAStatus Session::generateChallenge() {
Kevin Chyn146f6c82021-02-10 11:32:35 -080083 LOG(INFO) << "generateChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080084 scheduleStateOrCrash(SessionState::GENERATING_CHALLENGE);
85
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070086 mWorker->schedule(Callable::from([this] {
87 enterStateOrCrash(SessionState::GENERATING_CHALLENGE);
f8c8e4c62021-03-05 05:12:58 +000088 mEngine->generateChallengeImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070089 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080090 }));
91
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070092 return ndk::ScopedAStatus::ok();
93}
94
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070095ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080096 LOG(INFO) << "revokeChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080097 scheduleStateOrCrash(SessionState::REVOKING_CHALLENGE);
98
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070099 mWorker->schedule(Callable::from([this, challenge] {
100 enterStateOrCrash(SessionState::REVOKING_CHALLENGE);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800101 mEngine->revokeChallengeImpl(mCb.get(), challenge);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700102 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800103 }));
104
Ilya Matyukhin3d54f452020-10-15 17:32:09 -0700105 return ndk::ScopedAStatus::ok();
106}
107
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700108ndk::ScopedAStatus Session::enroll(const keymaster::HardwareAuthToken& hat,
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800109 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800110 LOG(INFO) << "enroll";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800111 scheduleStateOrCrash(SessionState::ENROLLING);
112
113 std::promise<void> cancellationPromise;
114 auto cancFuture = cancellationPromise.get_future();
115
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700116 mWorker->schedule(Callable::from([this, hat, cancFuture = std::move(cancFuture)] {
117 enterStateOrCrash(SessionState::ENROLLING);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800118 if (shouldCancel(cancFuture)) {
119 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
120 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800121 mEngine->enrollImpl(mCb.get(), hat, cancFuture);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800122 }
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700123 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800124 }));
125
126 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700127 return ndk::ScopedAStatus::ok();
128}
129
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700130ndk::ScopedAStatus Session::authenticate(int64_t operationId,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -0800131 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800132 LOG(INFO) << "authenticate";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800133 scheduleStateOrCrash(SessionState::AUTHENTICATING);
134
135 std::promise<void> cancPromise;
136 auto cancFuture = cancPromise.get_future();
137
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700138 mWorker->schedule(Callable::from([this, operationId, cancFuture = std::move(cancFuture)] {
139 enterStateOrCrash(SessionState::AUTHENTICATING);
140 if (shouldCancel(cancFuture)) {
141 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
142 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800143 mEngine->authenticateImpl(mCb.get(), operationId, cancFuture);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700144 }
145 enterIdling();
146 }));
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800147
148 *out = SharedRefBase::make<CancellationSignal>(std::move(cancPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700149 return ndk::ScopedAStatus::ok();
150}
151
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700152ndk::ScopedAStatus Session::detectInteraction(std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800153 LOG(INFO) << "detectInteraction";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800154 scheduleStateOrCrash(SessionState::DETECTING_INTERACTION);
155
156 std::promise<void> cancellationPromise;
157 auto cancFuture = cancellationPromise.get_future();
158
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700159 mWorker->schedule(Callable::from([this, cancFuture = std::move(cancFuture)] {
160 enterStateOrCrash(SessionState::DETECTING_INTERACTION);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800161 if (shouldCancel(cancFuture)) {
162 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
163 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800164 mEngine->detectInteractionImpl(mCb.get(), cancFuture);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800165 }
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700166 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800167 }));
168
169 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700170 return ndk::ScopedAStatus::ok();
171}
172
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700173ndk::ScopedAStatus Session::enumerateEnrollments() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800174 LOG(INFO) << "enumerateEnrollments";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800175 scheduleStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS);
176
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700177 mWorker->schedule(Callable::from([this] {
178 enterStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800179 mEngine->enumerateEnrollmentsImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700180 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800181 }));
182
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700183 return ndk::ScopedAStatus::ok();
184}
185
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700186ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& enrollmentIds) {
Jeff Pu63f33c72022-07-28 16:06:23 -0400187 LOG(INFO) << "removeEnrollments, size:" << enrollmentIds.size();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800188 scheduleStateOrCrash(SessionState::REMOVING_ENROLLMENTS);
189
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700190 mWorker->schedule(Callable::from([this, enrollmentIds] {
191 enterStateOrCrash(SessionState::REMOVING_ENROLLMENTS);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800192 mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700193 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800194 }));
195
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700196 return ndk::ScopedAStatus::ok();
197}
198
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700199ndk::ScopedAStatus Session::getAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800200 LOG(INFO) << "getAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800201 scheduleStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID);
202
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700203 mWorker->schedule(Callable::from([this] {
204 enterStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800205 mEngine->getAuthenticatorIdImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700206 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800207 }));
208
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700209 return ndk::ScopedAStatus::ok();
210}
211
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700212ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800213 LOG(INFO) << "invalidateAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800214 scheduleStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID);
215
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700216 mWorker->schedule(Callable::from([this] {
217 enterStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800218 mEngine->invalidateAuthenticatorIdImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700219 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800220 }));
221
Kevin Chyn6e862c32020-09-16 18:27:37 -0700222 return ndk::ScopedAStatus::ok();
223}
224
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700225ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& hat) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800226 LOG(INFO) << "resetLockout";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800227 scheduleStateOrCrash(SessionState::RESETTING_LOCKOUT);
228
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700229 mWorker->schedule(Callable::from([this, hat] {
230 enterStateOrCrash(SessionState::RESETTING_LOCKOUT);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800231 mEngine->resetLockoutImpl(mCb.get(), hat);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700232 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800233 }));
234
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700235 return ndk::ScopedAStatus::ok();
236}
237
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700238ndk::ScopedAStatus Session::close() {
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800239 LOG(INFO) << "close";
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700240 // TODO(b/166800618): call enterIdling from the terminal callbacks and restore this check.
241 // CHECK(mCurrentState == SessionState::IDLING) << "Can't close a non-idling session.
242 // Crashing.";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800243 mCurrentState = SessionState::CLOSED;
Ilya Matyukhincbbfa932021-03-22 13:25:15 -0700244 mCb->onSessionClosed();
Jeff Pu87e9f2b2023-05-03 17:59:21 +0000245 AIBinder_DeathRecipient_delete(mDeathRecipient);
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800246 return ndk::ScopedAStatus::ok();
247}
248
Jeff Pu63f33c72022-07-28 16:06:23 -0400249ndk::ScopedAStatus Session::onPointerDown(int32_t pointerId, int32_t x, int32_t y, float minor,
250 float major) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800251 LOG(INFO) << "onPointerDown";
Jeff Pu63f33c72022-07-28 16:06:23 -0400252 mWorker->schedule(Callable::from([this, pointerId, x, y, minor, major] {
253 mEngine->onPointerDownImpl(pointerId, x, y, minor, major);
254 enterIdling();
255 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700256 return ndk::ScopedAStatus::ok();
257}
258
Jeff Pu63f33c72022-07-28 16:06:23 -0400259ndk::ScopedAStatus Session::onPointerUp(int32_t pointerId) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800260 LOG(INFO) << "onPointerUp";
Jeff Pu63f33c72022-07-28 16:06:23 -0400261 mWorker->schedule(Callable::from([this, pointerId] {
262 mEngine->onPointerUpImpl(pointerId);
263 enterIdling();
264 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700265 return ndk::ScopedAStatus::ok();
266}
267
268ndk::ScopedAStatus Session::onUiReady() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800269 LOG(INFO) << "onUiReady";
Jeff Pu63f33c72022-07-28 16:06:23 -0400270 mWorker->schedule(Callable::from([this] {
271 mEngine->onUiReadyImpl();
272 enterIdling();
273 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700274 return ndk::ScopedAStatus::ok();
275}
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800276
Joe Bolinger13cb0fb2021-12-03 12:45:48 -0800277ndk::ScopedAStatus Session::authenticateWithContext(
278 int64_t operationId, const common::OperationContext& /*context*/,
279 std::shared_ptr<common::ICancellationSignal>* out) {
280 return authenticate(operationId, out);
281}
282
283ndk::ScopedAStatus Session::enrollWithContext(const keymaster::HardwareAuthToken& hat,
284 const common::OperationContext& /*context*/,
285 std::shared_ptr<common::ICancellationSignal>* out) {
286 return enroll(hat, out);
287}
288
289ndk::ScopedAStatus Session::detectInteractionWithContext(
290 const common::OperationContext& /*context*/,
291 std::shared_ptr<common::ICancellationSignal>* out) {
292 return detectInteraction(out);
293}
294
295ndk::ScopedAStatus Session::onPointerDownWithContext(const PointerContext& context) {
296 return onPointerDown(context.pointerId, context.x, context.y, context.minor, context.major);
297}
298
299ndk::ScopedAStatus Session::onPointerUpWithContext(const PointerContext& context) {
300 return onPointerUp(context.pointerId);
301}
302
Joe Bolinger25e98232022-01-24 18:56:23 +0000303ndk::ScopedAStatus Session::onContextChanged(const common::OperationContext& /*context*/) {
304 return ndk::ScopedAStatus::ok();
305}
306
Austin Delgado88ded642023-02-16 11:34:50 -0800307ndk::ScopedAStatus Session::onPointerCancelWithContext(const PointerContext& /*context*/) {
308 return ndk::ScopedAStatus::ok();
309}
310
311ndk::ScopedAStatus Session::setIgnoreDisplayTouches(bool /*shouldIgnore*/) {
312 return ndk::ScopedAStatus::ok();
313}
314
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700315} // namespace aidl::android::hardware::biometrics::fingerprint