blob: 1188459f3f3ff9f41403a853b9e1fc9bc46252f7 [file] [log] [blame]
Ilya Matyukhin09166982020-10-12 13:41:03 -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
Kevin Chyn146f6c82021-02-10 11:32:35 -080017#include <android-base/logging.h>
Ilya Matyukhin09166982020-10-12 13:41:03 -070018
19#include "Session.h"
20
21namespace aidl::android::hardware::biometrics::face {
22
Joshua McCloskeydb009a52022-05-10 05:18:20 +000023constexpr size_t MAX_WORKER_QUEUE_SIZE = 5;
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -080024
Joshua McCloskeydb009a52022-05-10 05:18:20 +000025Session::Session(std::unique_ptr<FakeFaceEngine> engine, std::shared_ptr<ISessionCallback> cb)
26 : mEngine(std::move(engine)), mCb(std::move(cb)), mRandom(std::mt19937::default_seed) {
27 mThread = std::make_unique<WorkerThread>(MAX_WORKER_QUEUE_SIZE);
28}
Ilya Matyukhin09166982020-10-12 13:41:03 -070029
Ilya Matyukhin66c64642021-03-23 22:33:31 -070030ndk::ScopedAStatus Session::generateChallenge() {
Kevin Chyn146f6c82021-02-10 11:32:35 -080031 LOG(INFO) << "generateChallenge";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000032 mThread->schedule(Callable::from([this] { mEngine->generateChallengeImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -070033 return ndk::ScopedAStatus::ok();
34}
35
Ilya Matyukhin66c64642021-03-23 22:33:31 -070036ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080037 LOG(INFO) << "revokeChallenge";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000038 mThread->schedule(Callable::from(
39 [this, challenge] { mEngine->revokeChallengeImpl(mCb.get(), challenge); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -070040 return ndk::ScopedAStatus::ok();
41}
42
Joshua McCloskeydb009a52022-05-10 05:18:20 +000043ndk::ScopedAStatus Session::getEnrollmentConfig(
44 EnrollmentType /*enrollmentType*/, std::vector<EnrollmentStageConfig>* cancellationSignal) {
45 *cancellationSignal = {};
Ilya Matyukhin929146e2021-04-22 00:29:01 -070046 return ndk::ScopedAStatus::ok();
47}
48
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -080049ndk::ScopedAStatus Session::enroll(
Joshua McCloskeydb009a52022-05-10 05:18:20 +000050 const keymaster::HardwareAuthToken& hat, EnrollmentType enrollmentType,
51 const std::vector<Feature>& features, const std::optional<NativeHandle>& /*previewSurface*/,
52 std::shared_ptr<biometrics::common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080053 LOG(INFO) << "enroll";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000054 std::promise<void> cancellationPromise;
55 auto cancFuture = cancellationPromise.get_future();
56
57 mThread->schedule(Callable::from(
58 [this, hat, enrollmentType, features, cancFuture = std::move(cancFuture)] {
59 mEngine->enrollImpl(mCb.get(), hat, enrollmentType, features, cancFuture);
60 }));
61
62 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -070063 return ndk::ScopedAStatus::ok();
64}
65
Joshua McCloskeydb009a52022-05-10 05:18:20 +000066ndk::ScopedAStatus Session::authenticate(
67 int64_t keystoreOperationId,
68 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080069 LOG(INFO) << "authenticate";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000070 std::promise<void> cancellationPromise;
71 auto cancFuture = cancellationPromise.get_future();
72
73 mThread->schedule(
74 Callable::from([this, keystoreOperationId, cancFuture = std::move(cancFuture)] {
75 mEngine->authenticateImpl(mCb.get(), keystoreOperationId, cancFuture);
76 }));
77
78 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -070079 return ndk::ScopedAStatus::ok();
80}
81
82ndk::ScopedAStatus Session::detectInteraction(
Joshua McCloskeydb009a52022-05-10 05:18:20 +000083 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080084 LOG(INFO) << "detectInteraction";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000085 std::promise<void> cancellationPromise;
86 auto cancFuture = cancellationPromise.get_future();
87
88 mThread->schedule(Callable::from([this, cancFuture = std::move(cancFuture)] {
89 mEngine->detectInteractionImpl(mCb.get(), cancFuture);
90 }));
91
92 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -070093 return ndk::ScopedAStatus::ok();
94}
95
Ilya Matyukhin66c64642021-03-23 22:33:31 -070096ndk::ScopedAStatus Session::enumerateEnrollments() {
Kevin Chyn146f6c82021-02-10 11:32:35 -080097 LOG(INFO) << "enumerateEnrollments";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000098 mThread->schedule(Callable::from([this] { mEngine->enumerateEnrollmentsImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -070099 return ndk::ScopedAStatus::ok();
100}
101
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000102ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& enrollmentIds) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800103 LOG(INFO) << "removeEnrollments";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000104 mThread->schedule(Callable::from(
105 [this, enrollmentIds] { mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700106 return ndk::ScopedAStatus::ok();
107}
108
Ilya Matyukhin9fcf6b12021-04-14 13:43:06 -0700109ndk::ScopedAStatus Session::getFeatures() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800110 LOG(INFO) << "getFeatures";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000111 mThread->schedule(Callable::from([this] { mEngine->getFeaturesImpl(mCb.get()); }));
Ilya Matyukhin26b20b92021-01-22 11:39:49 -0800112 return ndk::ScopedAStatus::ok();
113}
114
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000115ndk::ScopedAStatus Session::setFeature(const keymaster::HardwareAuthToken& hat, Feature feature,
116 bool enabled) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800117 LOG(INFO) << "setFeature";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000118 mThread->schedule(Callable::from([this, hat, feature, enabled] {
119 mEngine->setFeatureImpl(mCb.get(), hat, feature, enabled);
120 }));
Ilya Matyukhin26b20b92021-01-22 11:39:49 -0800121 return ndk::ScopedAStatus::ok();
122}
123
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700124ndk::ScopedAStatus Session::getAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800125 LOG(INFO) << "getAuthenticatorId";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000126 mThread->schedule(Callable::from([this] { mEngine->getAuthenticatorIdImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700127 return ndk::ScopedAStatus::ok();
128}
129
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700130ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800131 LOG(INFO) << "invalidateAuthenticatorId";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000132 mThread->schedule(
133 Callable::from([this] { mEngine->invalidateAuthenticatorIdImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700134 return ndk::ScopedAStatus::ok();
135}
136
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000137ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& hat) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800138 LOG(INFO) << "resetLockout";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000139 mThread->schedule(Callable::from([this, hat] { mEngine->resetLockoutImpl(mCb.get(), hat); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700140 return ndk::ScopedAStatus::ok();
141}
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -0800142
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700143ndk::ScopedAStatus Session::close() {
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000144 if (mCb) {
145 mCb->onSessionClosed();
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700146 }
Ilya Matyukhine52cae02021-02-17 16:38:56 -0800147 return ndk::ScopedAStatus::ok();
148}
149
Joe Bolinger7bd42e12022-01-12 16:27:03 -0800150ndk::ScopedAStatus Session::authenticateWithContext(
151 int64_t operationId, const common::OperationContext& /*context*/,
152 std::shared_ptr<common::ICancellationSignal>* out) {
153 return authenticate(operationId, out);
154}
155
156ndk::ScopedAStatus Session::enrollWithContext(const keymaster::HardwareAuthToken& hat,
157 EnrollmentType enrollmentType,
158 const std::vector<Feature>& features,
159 const std::optional<NativeHandle>& previewSurface,
160 const common::OperationContext& /*context*/,
161 std::shared_ptr<common::ICancellationSignal>* out) {
162 return enroll(hat, enrollmentType, features, previewSurface, out);
163}
164
165ndk::ScopedAStatus Session::detectInteractionWithContext(
166 const common::OperationContext& /*context*/,
167 std::shared_ptr<common::ICancellationSignal>* out) {
168 return detectInteraction(out);
169}
170
Joe Bolinger25e98232022-01-24 18:56:23 +0000171ndk::ScopedAStatus Session::onContextChanged(const common::OperationContext& /*context*/) {
172 return ndk::ScopedAStatus::ok();
173}
174
Ilya Matyukhin09166982020-10-12 13:41:03 -0700175} // namespace aidl::android::hardware::biometrics::face