blob: 673d879d79680210b4cad8467317aca85ed06b64 [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
Jeff Pu1e93ca62024-01-24 10:51:31 -050017#undef LOG_TAG
18#define LOG_TAG "FaceVirtualHalSession"
19
Kevin Chyn146f6c82021-02-10 11:32:35 -080020#include <android-base/logging.h>
Ilya Matyukhin09166982020-10-12 13:41:03 -070021
22#include "Session.h"
23
24namespace 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
Jeff Pu1e93ca62024-01-24 10:51:31 -050028void onClientDeath(void* cookie) {
29 LOG(INFO) << "FaceService has died";
30 Session* session = static_cast<Session*>(cookie);
31 if (session && !session->isClosed()) {
32 session->close();
33 }
34}
35
Joshua McCloskeydb009a52022-05-10 05:18:20 +000036Session::Session(std::unique_ptr<FakeFaceEngine> engine, std::shared_ptr<ISessionCallback> cb)
Jeff Pu1e93ca62024-01-24 10:51:31 -050037 : mEngine(std::move(engine)),
38 mCb(std::move(cb)),
39 mRandom(std::mt19937::default_seed),
40 mStateClosed(false) {
41 CHECK(mEngine);
42 CHECK(mCb);
Joshua McCloskeydb009a52022-05-10 05:18:20 +000043 mThread = std::make_unique<WorkerThread>(MAX_WORKER_QUEUE_SIZE);
Jeff Pu1e93ca62024-01-24 10:51:31 -050044 mDeathRecipient = AIBinder_DeathRecipient_new(onClientDeath);
45}
46
47binder_status_t Session::linkToDeath(AIBinder* binder) {
48 return AIBinder_linkToDeath(binder, mDeathRecipient, this);
Joshua McCloskeydb009a52022-05-10 05:18:20 +000049}
Ilya Matyukhin09166982020-10-12 13:41:03 -070050
Ilya Matyukhin66c64642021-03-23 22:33:31 -070051ndk::ScopedAStatus Session::generateChallenge() {
Kevin Chyn146f6c82021-02-10 11:32:35 -080052 LOG(INFO) << "generateChallenge";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000053 mThread->schedule(Callable::from([this] { mEngine->generateChallengeImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -070054 return ndk::ScopedAStatus::ok();
55}
56
Ilya Matyukhin66c64642021-03-23 22:33:31 -070057ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080058 LOG(INFO) << "revokeChallenge";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000059 mThread->schedule(Callable::from(
60 [this, challenge] { mEngine->revokeChallengeImpl(mCb.get(), challenge); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -070061 return ndk::ScopedAStatus::ok();
62}
63
Joshua McCloskeydb009a52022-05-10 05:18:20 +000064ndk::ScopedAStatus Session::getEnrollmentConfig(
65 EnrollmentType /*enrollmentType*/, std::vector<EnrollmentStageConfig>* cancellationSignal) {
66 *cancellationSignal = {};
Ilya Matyukhin929146e2021-04-22 00:29:01 -070067 return ndk::ScopedAStatus::ok();
68}
69
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -080070ndk::ScopedAStatus Session::enroll(
Joshua McCloskeydb009a52022-05-10 05:18:20 +000071 const keymaster::HardwareAuthToken& hat, EnrollmentType enrollmentType,
72 const std::vector<Feature>& features, const std::optional<NativeHandle>& /*previewSurface*/,
73 std::shared_ptr<biometrics::common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080074 LOG(INFO) << "enroll";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000075 std::promise<void> cancellationPromise;
76 auto cancFuture = cancellationPromise.get_future();
77
78 mThread->schedule(Callable::from(
79 [this, hat, enrollmentType, features, cancFuture = std::move(cancFuture)] {
80 mEngine->enrollImpl(mCb.get(), hat, enrollmentType, features, cancFuture);
81 }));
82
83 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -070084 return ndk::ScopedAStatus::ok();
85}
86
Joshua McCloskeydb009a52022-05-10 05:18:20 +000087ndk::ScopedAStatus Session::authenticate(
88 int64_t keystoreOperationId,
89 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080090 LOG(INFO) << "authenticate";
Joshua McCloskeydb009a52022-05-10 05:18:20 +000091 std::promise<void> cancellationPromise;
92 auto cancFuture = cancellationPromise.get_future();
93
94 mThread->schedule(
95 Callable::from([this, keystoreOperationId, cancFuture = std::move(cancFuture)] {
96 mEngine->authenticateImpl(mCb.get(), keystoreOperationId, cancFuture);
97 }));
98
99 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700100 return ndk::ScopedAStatus::ok();
101}
102
103ndk::ScopedAStatus Session::detectInteraction(
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000104 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800105 LOG(INFO) << "detectInteraction";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000106 std::promise<void> cancellationPromise;
107 auto cancFuture = cancellationPromise.get_future();
108
109 mThread->schedule(Callable::from([this, cancFuture = std::move(cancFuture)] {
110 mEngine->detectInteractionImpl(mCb.get(), cancFuture);
111 }));
112
113 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700114 return ndk::ScopedAStatus::ok();
115}
116
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700117ndk::ScopedAStatus Session::enumerateEnrollments() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800118 LOG(INFO) << "enumerateEnrollments";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000119 mThread->schedule(Callable::from([this] { mEngine->enumerateEnrollmentsImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700120 return ndk::ScopedAStatus::ok();
121}
122
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000123ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& enrollmentIds) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800124 LOG(INFO) << "removeEnrollments";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000125 mThread->schedule(Callable::from(
126 [this, enrollmentIds] { mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700127 return ndk::ScopedAStatus::ok();
128}
129
Ilya Matyukhin9fcf6b12021-04-14 13:43:06 -0700130ndk::ScopedAStatus Session::getFeatures() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800131 LOG(INFO) << "getFeatures";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000132 mThread->schedule(Callable::from([this] { mEngine->getFeaturesImpl(mCb.get()); }));
Ilya Matyukhin26b20b92021-01-22 11:39:49 -0800133 return ndk::ScopedAStatus::ok();
134}
135
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000136ndk::ScopedAStatus Session::setFeature(const keymaster::HardwareAuthToken& hat, Feature feature,
137 bool enabled) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800138 LOG(INFO) << "setFeature";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000139 mThread->schedule(Callable::from([this, hat, feature, enabled] {
140 mEngine->setFeatureImpl(mCb.get(), hat, feature, enabled);
141 }));
Ilya Matyukhin26b20b92021-01-22 11:39:49 -0800142 return ndk::ScopedAStatus::ok();
143}
144
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700145ndk::ScopedAStatus Session::getAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800146 LOG(INFO) << "getAuthenticatorId";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000147 mThread->schedule(Callable::from([this] { mEngine->getAuthenticatorIdImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700148 return ndk::ScopedAStatus::ok();
149}
150
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700151ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800152 LOG(INFO) << "invalidateAuthenticatorId";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000153 mThread->schedule(
154 Callable::from([this] { mEngine->invalidateAuthenticatorIdImpl(mCb.get()); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700155 return ndk::ScopedAStatus::ok();
156}
157
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000158ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& hat) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800159 LOG(INFO) << "resetLockout";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000160 mThread->schedule(Callable::from([this, hat] { mEngine->resetLockoutImpl(mCb.get(), hat); }));
Ilya Matyukhin09166982020-10-12 13:41:03 -0700161 return ndk::ScopedAStatus::ok();
162}
Ilya Matyukhinaf30cde2021-01-08 09:42:50 -0800163
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700164ndk::ScopedAStatus Session::close() {
Jeff Pu1e93ca62024-01-24 10:51:31 -0500165 LOG(INFO) << "close";
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000166 if (mCb) {
167 mCb->onSessionClosed();
Ilya Matyukhin66c64642021-03-23 22:33:31 -0700168 }
Jeff Pu1e93ca62024-01-24 10:51:31 -0500169 AIBinder_DeathRecipient_delete(mDeathRecipient);
170 mStateClosed = true;
Ilya Matyukhine52cae02021-02-17 16:38:56 -0800171 return ndk::ScopedAStatus::ok();
172}
173
Joe Bolinger7bd42e12022-01-12 16:27:03 -0800174ndk::ScopedAStatus Session::authenticateWithContext(
175 int64_t operationId, const common::OperationContext& /*context*/,
176 std::shared_ptr<common::ICancellationSignal>* out) {
177 return authenticate(operationId, out);
178}
179
180ndk::ScopedAStatus Session::enrollWithContext(const keymaster::HardwareAuthToken& hat,
181 EnrollmentType enrollmentType,
182 const std::vector<Feature>& features,
183 const std::optional<NativeHandle>& previewSurface,
184 const common::OperationContext& /*context*/,
185 std::shared_ptr<common::ICancellationSignal>* out) {
186 return enroll(hat, enrollmentType, features, previewSurface, out);
187}
188
189ndk::ScopedAStatus Session::detectInteractionWithContext(
190 const common::OperationContext& /*context*/,
191 std::shared_ptr<common::ICancellationSignal>* out) {
192 return detectInteraction(out);
193}
194
Joe Bolinger25e98232022-01-24 18:56:23 +0000195ndk::ScopedAStatus Session::onContextChanged(const common::OperationContext& /*context*/) {
196 return ndk::ScopedAStatus::ok();
197}
198
Joshua McCloskey67310c32023-11-01 15:57:55 +0000199ndk::ScopedAStatus Session::enrollWithOptions(const FaceEnrollOptions& options,
200 std::shared_ptr<common::ICancellationSignal>* out) {
201 return enroll(options.hardwareAuthToken, options.enrollmentType, options.features,
202 options.nativeHandlePreview, out);
203}
204
Ilya Matyukhin09166982020-10-12 13:41:03 -0700205} // namespace aidl::android::hardware::biometrics::face