blob: 6235b83195795c49faa9b56a1c4ab3d6637800f9 [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
Jeff Pu4b5e5ce2023-09-06 14:47:49 +000021#undef LOG_TAG
22#define LOG_TAG "FaceVirtualHalSession"
23
Ilya Matyukhin09166982020-10-12 13:41:03 -070024namespace aidl::android::hardware::biometrics::face {
25
Joshua McCloskeydb009a52022-05-10 05:18:20 +000026constexpr size_t MAX_WORKER_QUEUE_SIZE = 5;
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -080027
Joshua McCloskeydb009a52022-05-10 05:18:20 +000028Session::Session(std::unique_ptr<FakeFaceEngine> engine, std::shared_ptr<ISessionCallback> cb)
29 : mEngine(std::move(engine)), mCb(std::move(cb)), mRandom(std::mt19937::default_seed) {
30 mThread = std::make_unique<WorkerThread>(MAX_WORKER_QUEUE_SIZE);
31}
Ilya Matyukhin09166982020-10-12 13:41:03 -070032
Ilya Matyukhin66c64642021-03-23 22:33:31 -070033ndk::ScopedAStatus Session::generateChallenge() {
Kevin Chyn146f6c82021-02-10 11:32:35 -080034 LOG(INFO) << "generateChallenge";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000035 mThread->schedule(Callable::from([this] { mEngine->generateChallengeImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -070036 return ndk::ScopedAStatus::ok();
37}
38
Ilya Matyukhin66c64642021-03-23 22:33:31 -070039ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080040 LOG(INFO) << "revokeChallenge";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000041 mThread->schedule(Callable::from(
42 [this, challenge] { mEngine->revokeChallengeImpl(mCb.get(), challenge); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -070043 return ndk::ScopedAStatus::ok();
44}
45
Joshua McCloskeydb009a52022-05-10 05:18:20 +000046ndk::ScopedAStatus Session::getEnrollmentConfig(
47 EnrollmentType /*enrollmentType*/, std::vector<EnrollmentStageConfig>* cancellationSignal) {
48 *cancellationSignal = {};
Ilya Matyukhin929146e2021-04-22 00:29:01 -070049 return ndk::ScopedAStatus::ok();
50}
51
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -080052ndk::ScopedAStatus Session::enroll(
Joshua McCloskeydb009a52022-05-10 05:18:20 +000053 const keymaster::HardwareAuthToken& hat, EnrollmentType enrollmentType,
54 const std::vector<Feature>& features, const std::optional<NativeHandle>& /*previewSurface*/,
55 std::shared_ptr<biometrics::common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080056 LOG(INFO) << "enroll";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000057 std::promise<void> cancellationPromise;
58 auto cancFuture = cancellationPromise.get_future();
59
60 mThread->schedule(Callable::from(
61 [this, hat, enrollmentType, features, cancFuture = std::move(cancFuture)] {
62 mEngine->enrollImpl(mCb.get(), hat, enrollmentType, features, cancFuture);
63 }));
64
65 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -070066 return ndk::ScopedAStatus::ok();
67}
68
Joshua McCloskeydb009a52022-05-10 05:18:20 +000069ndk::ScopedAStatus Session::authenticate(
70 int64_t keystoreOperationId,
71 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080072 LOG(INFO) << "authenticate";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000073 std::promise<void> cancellationPromise;
74 auto cancFuture = cancellationPromise.get_future();
75
76 mThread->schedule(
77 Callable::from([this, keystoreOperationId, cancFuture = std::move(cancFuture)] {
78 mEngine->authenticateImpl(mCb.get(), keystoreOperationId, cancFuture);
79 }));
80
81 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -070082 return ndk::ScopedAStatus::ok();
83}
84
85ndk::ScopedAStatus Session::detectInteraction(
Joshua McCloskeydb009a52022-05-10 05:18:20 +000086 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080087 LOG(INFO) << "detectInteraction";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000088 std::promise<void> cancellationPromise;
89 auto cancFuture = cancellationPromise.get_future();
90
91 mThread->schedule(Callable::from([this, cancFuture = std::move(cancFuture)] {
92 mEngine->detectInteractionImpl(mCb.get(), cancFuture);
93 }));
94
95 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -070096 return ndk::ScopedAStatus::ok();
97}
98
Ilya Matyukhin66c64642021-03-23 22:33:31 -070099ndk::ScopedAStatus Session::enumerateEnrollments() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800100 LOG(INFO) << "enumerateEnrollments";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000101 mThread->schedule(Callable::from([this] { mEngine->enumerateEnrollmentsImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700102 return ndk::ScopedAStatus::ok();
103}
104
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000105ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& enrollmentIds) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800106 LOG(INFO) << "removeEnrollments";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000107 mThread->schedule(Callable::from(
108 [this, enrollmentIds] { mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700109 return ndk::ScopedAStatus::ok();
110}
111
Ilya Matyukhin9fcf6b12021-04-14 13:43:06 -0700112ndk::ScopedAStatus Session::getFeatures() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800113 LOG(INFO) << "getFeatures";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000114 mThread->schedule(Callable::from([this] { mEngine->getFeaturesImpl(mCb.get()); }));
Ilya Matyukhin26b20b92021-01-22 11:39:49 -0800115 return ndk::ScopedAStatus::ok();
116}
117
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000118ndk::ScopedAStatus Session::setFeature(const keymaster::HardwareAuthToken& hat, Feature feature,
119 bool enabled) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800120 LOG(INFO) << "setFeature";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000121 mThread->schedule(Callable::from([this, hat, feature, enabled] {
122 mEngine->setFeatureImpl(mCb.get(), hat, feature, enabled);
123 }));
Ilya Matyukhin26b20b92021-01-22 11:39:49 -0800124 return ndk::ScopedAStatus::ok();
125}
126
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700127ndk::ScopedAStatus Session::getAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800128 LOG(INFO) << "getAuthenticatorId";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000129 mThread->schedule(Callable::from([this] { mEngine->getAuthenticatorIdImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700130 return ndk::ScopedAStatus::ok();
131}
132
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700133ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800134 LOG(INFO) << "invalidateAuthenticatorId";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000135 mThread->schedule(
136 Callable::from([this] { mEngine->invalidateAuthenticatorIdImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700137 return ndk::ScopedAStatus::ok();
138}
139
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000140ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& hat) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800141 LOG(INFO) << "resetLockout";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000142 mThread->schedule(Callable::from([this, hat] { mEngine->resetLockoutImpl(mCb.get(), hat); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700143 return ndk::ScopedAStatus::ok();
144}
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -0800145
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700146ndk::ScopedAStatus Session::close() {
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000147 if (mCb) {
148 mCb->onSessionClosed();
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700149 }
Ilya Matyukhine52cae02021-02-17 16:38:56 -0800150 return ndk::ScopedAStatus::ok();
151}
152
Joe Bolinger7bd42e12022-01-12 16:27:03 -0800153ndk::ScopedAStatus Session::authenticateWithContext(
154 int64_t operationId, const common::OperationContext& /*context*/,
155 std::shared_ptr<common::ICancellationSignal>* out) {
156 return authenticate(operationId, out);
157}
158
159ndk::ScopedAStatus Session::enrollWithContext(const keymaster::HardwareAuthToken& hat,
160 EnrollmentType enrollmentType,
161 const std::vector<Feature>& features,
162 const std::optional<NativeHandle>& previewSurface,
163 const common::OperationContext& /*context*/,
164 std::shared_ptr<common::ICancellationSignal>* out) {
165 return enroll(hat, enrollmentType, features, previewSurface, out);
166}
167
168ndk::ScopedAStatus Session::detectInteractionWithContext(
169 const common::OperationContext& /*context*/,
170 std::shared_ptr<common::ICancellationSignal>* out) {
171 return detectInteraction(out);
172}
173
Joe Bolinger25e98232022-01-24 18:56:23 +0000174ndk::ScopedAStatus Session::onContextChanged(const common::OperationContext& /*context*/) {
175 return ndk::ScopedAStatus::ok();
176}
177
Ilya Matyukhin09166982020-10-12 13:41:03 -0700178} // namespace aidl::android::hardware::biometrics::face