blob: a7acf3d5205e5fe1402ed735fce51e79fc49f59c [file] [log] [blame]
Joe Bolingerde94aa02021-12-09 17:00:32 -08001/*
Jeff Pu52653182022-10-12 16:27:23 -04002 * Copyright (C) 2022 The Android Open Source Project
Joe Bolingerde94aa02021-12-09 17:00:32 -08003 *
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
17#include "FakeFingerprintEngine.h"
Jeff Pu52653182022-10-12 16:27:23 -040018#include <regex>
Jeff Pu63f33c72022-07-28 16:06:23 -040019#include "Fingerprint.h"
Joe Bolingerde94aa02021-12-09 17:00:32 -080020
Joe Bolingerde94aa02021-12-09 17:00:32 -080021#include <android-base/logging.h>
Jeff Pu63f33c72022-07-28 16:06:23 -040022#include <android-base/parseint.h>
Joshua McCloskeyc8c0bad2022-05-10 05:17:44 +000023
24#include <fingerprint.sysprop.h>
Joe Bolingerde94aa02021-12-09 17:00:32 -080025
Joshua McCloskeyc8c0bad2022-05-10 05:17:44 +000026#include "util/CancellationSignal.h"
Joshua McCloskeydb009a52022-05-10 05:18:20 +000027#include "util/Util.h"
Joe Bolingerde94aa02021-12-09 17:00:32 -080028
29using namespace ::android::fingerprint::virt;
Jeff Pu63f33c72022-07-28 16:06:23 -040030using ::android::base::ParseInt;
Joe Bolingerde94aa02021-12-09 17:00:32 -080031
32namespace aidl::android::hardware::biometrics::fingerprint {
33
Jeff Pudef5b042023-05-25 14:28:16 -040034FakeFingerprintEngine::FakeFingerprintEngine()
Jeff Pu29df1e92023-10-06 14:12:44 +000035 : mRandom(std::mt19937::default_seed),
36 mWorkMode(WorkMode::kIdle),
37 isLockoutTimerSupported(true) {}
Jeff Pudef5b042023-05-25 14:28:16 -040038
Joe Bolingerde94aa02021-12-09 17:00:32 -080039void FakeFingerprintEngine::generateChallengeImpl(ISessionCallback* cb) {
40 BEGIN_OP(0);
41 std::uniform_int_distribution<int64_t> dist;
42 auto challenge = dist(mRandom);
43 FingerprintHalProperties::challenge(challenge);
44 cb->onChallengeGenerated(challenge);
45}
46
47void FakeFingerprintEngine::revokeChallengeImpl(ISessionCallback* cb, int64_t challenge) {
48 BEGIN_OP(0);
49 FingerprintHalProperties::challenge({});
50 cb->onChallengeRevoked(challenge);
51}
52
53void FakeFingerprintEngine::enrollImpl(ISessionCallback* cb,
54 const keymaster::HardwareAuthToken& hat,
55 const std::future<void>& cancel) {
Jeff Pudef5b042023-05-25 14:28:16 -040056 BEGIN_OP(0);
Jeff Pu5055e3c2023-07-27 11:26:50 -040057
58 // Do proper HAT verification in the real implementation.
59 if (hat.mac.empty()) {
60 LOG(ERROR) << "Fail: hat";
61 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
62 return;
63 }
64
Jeff Pu74e25d22024-01-08 22:21:20 +000065 waitForFingerDown(cb, cancel);
66
Jeff Pudef5b042023-05-25 14:28:16 -040067 updateContext(WorkMode::kEnroll, cb, const_cast<std::future<void>&>(cancel), 0, hat);
68}
69
70void FakeFingerprintEngine::authenticateImpl(ISessionCallback* cb, int64_t operationId,
71 const std::future<void>& cancel) {
72 BEGIN_OP(0);
Jeff Pu74e25d22024-01-08 22:21:20 +000073
74 waitForFingerDown(cb, cancel);
75
Jeff Pudef5b042023-05-25 14:28:16 -040076 updateContext(WorkMode::kAuthenticate, cb, const_cast<std::future<void>&>(cancel), operationId,
77 keymaster::HardwareAuthToken());
78}
79
80void FakeFingerprintEngine::detectInteractionImpl(ISessionCallback* cb,
81 const std::future<void>& cancel) {
82 BEGIN_OP(0);
83
84 auto detectInteractionSupported =
85 FingerprintHalProperties::detect_interaction().value_or(false);
86 if (!detectInteractionSupported) {
87 LOG(ERROR) << "Detect interaction is not supported";
88 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
89 return;
90 }
91
Jeff Pu74e25d22024-01-08 22:21:20 +000092 waitForFingerDown(cb, cancel);
93
Jeff Pudef5b042023-05-25 14:28:16 -040094 updateContext(WorkMode::kDetectInteract, cb, const_cast<std::future<void>&>(cancel), 0,
95 keymaster::HardwareAuthToken());
96}
97
98void FakeFingerprintEngine::updateContext(WorkMode mode, ISessionCallback* cb,
99 std::future<void>& cancel, int64_t operationId,
100 const keymaster::HardwareAuthToken& hat) {
101 mCancel = std::move(cancel);
102 mWorkMode = mode;
103 mCb = cb;
104 mOperationId = operationId;
105 mHat = hat;
106}
107
108void FakeFingerprintEngine::fingerDownAction() {
Jeff Pu073af182023-07-12 18:53:52 +0000109 bool isTerminal = false;
Jeff Pudef5b042023-05-25 14:28:16 -0400110 LOG(INFO) << __func__;
111 switch (mWorkMode) {
112 case WorkMode::kAuthenticate:
Jeff Pu073af182023-07-12 18:53:52 +0000113 isTerminal = onAuthenticateFingerDown(mCb, mOperationId, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400114 break;
115 case WorkMode::kEnroll:
Jeff Pu073af182023-07-12 18:53:52 +0000116 isTerminal = onEnrollFingerDown(mCb, mHat, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400117 break;
118 case WorkMode::kDetectInteract:
Jeff Pu073af182023-07-12 18:53:52 +0000119 isTerminal = onDetectInteractFingerDown(mCb, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400120 break;
121 default:
122 LOG(WARNING) << "unexpected mode: on fingerDownAction(), " << (int)mWorkMode;
123 break;
124 }
Jeff Pu073af182023-07-12 18:53:52 +0000125
126 if (isTerminal) {
127 mWorkMode = WorkMode::kIdle;
128 }
Jeff Pudef5b042023-05-25 14:28:16 -0400129}
130
Jeff Pu073af182023-07-12 18:53:52 +0000131bool FakeFingerprintEngine::onEnrollFingerDown(ISessionCallback* cb,
Jeff Pu5055e3c2023-07-27 11:26:50 -0400132 const keymaster::HardwareAuthToken&,
Jeff Pudef5b042023-05-25 14:28:16 -0400133 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400134 BEGIN_OP(getLatency(FingerprintHalProperties::operation_enroll_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800135
Jeff Pu343ca942022-09-14 15:56:30 -0400136 // Force error-out
137 auto err = FingerprintHalProperties::operation_enroll_error().value_or(0);
138 if (err != 0) {
139 LOG(ERROR) << "Fail: operation_enroll_error";
140 auto ec = convertError(err);
141 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000142 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800143 }
144
Jeff Pu343ca942022-09-14 15:56:30 -0400145 // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result>
Joe Bolingerde94aa02021-12-09 17:00:32 -0800146 auto nextEnroll = FingerprintHalProperties::next_enrollment().value_or("");
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000147 auto parts = Util::split(nextEnroll, ":");
Joe Bolingerde94aa02021-12-09 17:00:32 -0800148 if (parts.size() != 3) {
Jeff Pu343ca942022-09-14 15:56:30 -0400149 LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800150 cb->onError(Error::VENDOR, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000151 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800152 }
153 auto enrollmentId = std::stoi(parts[0]);
Jeff Pu484d2e72023-09-25 15:11:19 +0000154 auto progress = Util::parseEnrollmentCapture(parts[1]);
Jeff Pu343ca942022-09-14 15:56:30 -0400155 for (size_t i = 0; i < progress.size(); i += 2) {
156 auto left = (progress.size() - i) / 2 - 1;
157 auto duration = progress[i][0];
158 auto acquired = progress[i + 1];
159 auto N = acquired.size();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800160
Jeff Pu343ca942022-09-14 15:56:30 -0400161 for (int j = 0; j < N; j++) {
162 SLEEP_MS(duration / N);
163
164 if (shouldCancel(cancel)) {
165 LOG(ERROR) << "Fail: cancel";
166 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000167 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400168 }
169 auto ac = convertAcquiredInfo(acquired[j]);
170 cb->onAcquired(ac.first, ac.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800171 }
172
Joe Bolingerde94aa02021-12-09 17:00:32 -0800173 if (left == 0 && !IS_TRUE(parts[2])) { // end and failed
174 LOG(ERROR) << "Fail: requested by caller: " << nextEnroll;
175 FingerprintHalProperties::next_enrollment({});
176 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */);
177 } else { // progress and update props if last time
Jeff Pu343ca942022-09-14 15:56:30 -0400178 LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800179 if (left == 0) {
180 auto enrollments = FingerprintHalProperties::enrollments();
181 enrollments.emplace_back(enrollmentId);
182 FingerprintHalProperties::enrollments(enrollments);
183 FingerprintHalProperties::next_enrollment({});
Jeff Pu343ca942022-09-14 15:56:30 -0400184 // change authenticatorId after new enrollment
185 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
186 auto newId = id + 1;
187 FingerprintHalProperties::authenticator_id(newId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800188 LOG(INFO) << "Enrolled: " << enrollmentId;
189 }
190 cb->onEnrollmentProgress(enrollmentId, left);
191 }
192 }
Jeff Pu073af182023-07-12 18:53:52 +0000193
194 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800195}
196
Jeff Pu073af182023-07-12 18:53:52 +0000197bool FakeFingerprintEngine::onAuthenticateFingerDown(ISessionCallback* cb,
Jeff Pudef5b042023-05-25 14:28:16 -0400198 int64_t /* operationId */,
199 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400200 BEGIN_OP(getLatency(FingerprintHalProperties::operation_authenticate_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800201
Jeff Pu343ca942022-09-14 15:56:30 -0400202 int64_t now = Util::getSystemNanoTime();
203 int64_t duration = FingerprintHalProperties::operation_authenticate_duration().value_or(10);
204 auto acquired = FingerprintHalProperties::operation_authenticate_acquired().value_or("1");
Jeff Pu484d2e72023-09-25 15:11:19 +0000205 auto acquiredInfos = Util::parseIntSequence(acquired);
Jeff Pu343ca942022-09-14 15:56:30 -0400206 int N = acquiredInfos.size();
207
208 if (N == 0) {
209 LOG(ERROR) << "Fail to parse authentiate acquired info: " + acquired;
210 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000211 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400212 }
213
Jeff Pu52653182022-10-12 16:27:23 -0400214 // got lockout?
Jeff Pu073af182023-07-12 18:53:52 +0000215 if (checkSensorLockout(cb)) {
216 return FakeLockoutTracker::LockoutMode::kPermanent == mLockoutTracker.getMode();
217 }
Jeff Pu52653182022-10-12 16:27:23 -0400218
Jeff Pu343ca942022-09-14 15:56:30 -0400219 int i = 0;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800220 do {
221 if (FingerprintHalProperties::operation_authenticate_fails().value_or(false)) {
222 LOG(ERROR) << "Fail: operation_authenticate_fails";
Jeff Pu52653182022-10-12 16:27:23 -0400223 mLockoutTracker.addFailedAttempt();
Jeff Pu343ca942022-09-14 15:56:30 -0400224 cb->onAuthenticationFailed();
Jeff Pu073af182023-07-12 18:53:52 +0000225 return false;
Jeff Pu343ca942022-09-14 15:56:30 -0400226 }
227
228 auto err = FingerprintHalProperties::operation_authenticate_error().value_or(0);
229 if (err != 0) {
230 LOG(ERROR) << "Fail: operation_authenticate_error";
231 auto ec = convertError(err);
232 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000233 return true; /* simply terminating current operation for any user inserted error,
234 revisit if tests need*/
Joe Bolingerde94aa02021-12-09 17:00:32 -0800235 }
236
237 if (FingerprintHalProperties::lockout().value_or(false)) {
238 LOG(ERROR) << "Fail: lockout";
239 cb->onLockoutPermanent();
240 cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000241 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800242 }
243
244 if (shouldCancel(cancel)) {
245 LOG(ERROR) << "Fail: cancel";
246 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000247 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800248 }
249
Jeff Pu343ca942022-09-14 15:56:30 -0400250 if (i < N) {
251 auto ac = convertAcquiredInfo(acquiredInfos[i]);
252 cb->onAcquired(ac.first, ac.second);
253 i++;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800254 }
255
Jeff Pu343ca942022-09-14 15:56:30 -0400256 SLEEP_MS(duration / N);
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000257 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800258
Jeff Pu343ca942022-09-14 15:56:30 -0400259 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
260 auto enrolls = FingerprintHalProperties::enrollments();
261 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
262 if (id > 0 && isEnrolled) {
263 cb->onAuthenticationSucceeded(id, {} /* hat */);
Jeff Pu52653182022-10-12 16:27:23 -0400264 mLockoutTracker.reset();
Jeff Pu073af182023-07-12 18:53:52 +0000265 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400266 } else {
267 LOG(ERROR) << "Fail: fingerprint not enrolled";
268 cb->onAuthenticationFailed();
Jeff Pu52653182022-10-12 16:27:23 -0400269 mLockoutTracker.addFailedAttempt();
Jeff Pu437516e2023-06-28 15:21:21 +0000270 checkSensorLockout(cb);
Jeff Pu8fec5562023-07-20 13:07:04 +0000271 return false;
Jeff Pu343ca942022-09-14 15:56:30 -0400272 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800273}
274
Jeff Pu073af182023-07-12 18:53:52 +0000275bool FakeFingerprintEngine::onDetectInteractFingerDown(ISessionCallback* cb,
Jeff Pudef5b042023-05-25 14:28:16 -0400276 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400277 BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800278
Jeff Pu343ca942022-09-14 15:56:30 -0400279 int64_t duration =
280 FingerprintHalProperties::operation_detect_interaction_duration().value_or(10);
Jeff Pu52653182022-10-12 16:27:23 -0400281
Jeff Pu343ca942022-09-14 15:56:30 -0400282 auto acquired = FingerprintHalProperties::operation_detect_interaction_acquired().value_or("1");
Jeff Pu484d2e72023-09-25 15:11:19 +0000283 auto acquiredInfos = Util::parseIntSequence(acquired);
Jeff Pu343ca942022-09-14 15:56:30 -0400284 int N = acquiredInfos.size();
285 int64_t now = Util::getSystemNanoTime();
286
287 if (N == 0) {
288 LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired;
289 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000290 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800291 }
292
Jeff Pu343ca942022-09-14 15:56:30 -0400293 int i = 0;
294 do {
295 auto err = FingerprintHalProperties::operation_detect_interaction_error().value_or(0);
296 if (err != 0) {
297 LOG(ERROR) << "Fail: operation_detect_interaction_error";
298 auto ec = convertError(err);
299 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000300 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400301 }
302
303 if (shouldCancel(cancel)) {
304 LOG(ERROR) << "Fail: cancel";
305 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000306 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400307 }
308
309 if (i < N) {
310 auto ac = convertAcquiredInfo(acquiredInfos[i]);
311 cb->onAcquired(ac.first, ac.second);
312 i++;
313 }
314 SLEEP_MS(duration / N);
315 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800316
Joe Bolingerde94aa02021-12-09 17:00:32 -0800317 cb->onInteractionDetected();
Jeff Pu073af182023-07-12 18:53:52 +0000318
319 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800320}
321
322void FakeFingerprintEngine::enumerateEnrollmentsImpl(ISessionCallback* cb) {
323 BEGIN_OP(0);
324
325 std::vector<int32_t> ids;
326 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
327 auto id = enrollment.value_or(0);
328 if (id > 0) {
329 ids.push_back(id);
330 }
331 }
332
333 cb->onEnrollmentsEnumerated(ids);
334}
335
336void FakeFingerprintEngine::removeEnrollmentsImpl(ISessionCallback* cb,
337 const std::vector<int32_t>& enrollmentIds) {
338 BEGIN_OP(0);
339
340 std::vector<std::optional<int32_t>> newEnrollments;
341 std::vector<int32_t> removed;
342 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
343 auto id = enrollment.value_or(0);
344 if (std::find(enrollmentIds.begin(), enrollmentIds.end(), id) != enrollmentIds.end()) {
345 removed.push_back(id);
346 } else if (id > 0) {
347 newEnrollments.emplace_back(id);
348 }
349 }
350 FingerprintHalProperties::enrollments(newEnrollments);
351
352 cb->onEnrollmentsRemoved(enrollmentIds);
353}
354
355void FakeFingerprintEngine::getAuthenticatorIdImpl(ISessionCallback* cb) {
356 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400357 int64_t authenticatorId;
358 if (FingerprintHalProperties::enrollments().size() == 0) {
359 authenticatorId = 0;
360 } else {
361 authenticatorId = FingerprintHalProperties::authenticator_id().value_or(0);
362 if (authenticatorId == 0) authenticatorId = 1;
Jeff Pu63f33c72022-07-28 16:06:23 -0400363 }
364 cb->onAuthenticatorIdRetrieved(authenticatorId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800365}
366
367void FakeFingerprintEngine::invalidateAuthenticatorIdImpl(ISessionCallback* cb) {
368 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400369 int64_t newId;
370 if (FingerprintHalProperties::enrollments().size() == 0) {
371 newId = 0;
372 } else {
373 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
374 newId = id + 1;
375 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800376 FingerprintHalProperties::authenticator_id(newId);
377 cb->onAuthenticatorIdInvalidated(newId);
378}
379
380void FakeFingerprintEngine::resetLockoutImpl(ISessionCallback* cb,
Jeff Pu343ca942022-09-14 15:56:30 -0400381 const keymaster::HardwareAuthToken& hat) {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800382 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400383 if (hat.mac.empty()) {
384 LOG(ERROR) << "Fail: hat in resetLockout()";
385 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
386 return;
387 }
Jeff Puc6f21462023-08-04 13:41:37 +0000388 clearLockout(cb);
Jeff Pu29df1e92023-10-06 14:12:44 +0000389 if (isLockoutTimerStarted) isLockoutTimerAborted = true;
Jeff Puc6f21462023-08-04 13:41:37 +0000390}
391
392void FakeFingerprintEngine::clearLockout(ISessionCallback* cb) {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800393 FingerprintHalProperties::lockout(false);
394 cb->onLockoutCleared();
Jeff Pu52653182022-10-12 16:27:23 -0400395 mLockoutTracker.reset();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800396}
397
Jeff Pu63f33c72022-07-28 16:06:23 -0400398ndk::ScopedAStatus FakeFingerprintEngine::onPointerDownImpl(int32_t /*pointerId*/, int32_t /*x*/,
399 int32_t /*y*/, float /*minor*/,
400 float /*major*/) {
401 BEGIN_OP(0);
Jeff Pudef5b042023-05-25 14:28:16 -0400402 fingerDownAction();
Jeff Pu63f33c72022-07-28 16:06:23 -0400403 return ndk::ScopedAStatus::ok();
404}
405
406ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) {
407 BEGIN_OP(0);
Jeff Pu74e25d22024-01-08 22:21:20 +0000408 mFingerIsDown = false;
Jeff Pu63f33c72022-07-28 16:06:23 -0400409 return ndk::ScopedAStatus::ok();
410}
411
412ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() {
413 BEGIN_OP(0);
414 return ndk::ScopedAStatus::ok();
415}
416
417bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) {
418 auto loc = FingerprintHalProperties::sensor_location().value_or("");
419 auto isValidStr = false;
420 auto dim = Util::split(loc, ":");
421
422 if (dim.size() < 3 or dim.size() > 4) {
423 if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc;
424 return false;
425 } else {
426 int32_t x, y, r;
427 std::string d = "";
428 if (dim.size() >= 3) {
429 isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r);
430 }
431 if (dim.size() >= 4) {
432 d = dim[3];
433 }
Jeff Pudef5b042023-05-25 14:28:16 -0400434 if (isValidStr)
435 out = {.sensorLocationX = x, .sensorLocationY = y, .sensorRadius = r, .display = d};
Jeff Pu63f33c72022-07-28 16:06:23 -0400436
437 return isValidStr;
438 }
439}
440SensorLocation FakeFingerprintEngine::getSensorLocation() {
441 SensorLocation location;
442
443 if (getSensorLocationConfig(location)) {
444 return location;
445 } else {
446 return defaultSensorLocation();
447 }
448}
449
450SensorLocation FakeFingerprintEngine::defaultSensorLocation() {
Jeff Pudef5b042023-05-25 14:28:16 -0400451 return SensorLocation();
Jeff Pu63f33c72022-07-28 16:06:23 -0400452}
Jeff Pu343ca942022-09-14 15:56:30 -0400453
Jeff Pu343ca942022-09-14 15:56:30 -0400454std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) {
455 std::pair<AcquiredInfo, int32_t> res;
456 if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) {
457 res.first = AcquiredInfo::VENDOR;
458 res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE;
459 } else {
460 res.first = (AcquiredInfo)code;
461 res.second = 0;
462 }
463 return res;
464}
465
466std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) {
467 std::pair<Error, int32_t> res;
468 if (code > FINGERPRINT_ERROR_VENDOR_BASE) {
469 res.first = Error::VENDOR;
470 res.second = code - FINGERPRINT_ERROR_VENDOR_BASE;
471 } else {
472 res.first = (Error)code;
473 res.second = 0;
474 }
475 return res;
476}
477
Jeff Pu52653182022-10-12 16:27:23 -0400478int32_t FakeFingerprintEngine::getLatency(
479 const std::vector<std::optional<std::int32_t>>& latencyIn) {
480 int32_t res = DEFAULT_LATENCY;
481
482 std::vector<int32_t> latency;
483 for (auto x : latencyIn)
484 if (x.has_value()) latency.push_back(*x);
485
486 switch (latency.size()) {
487 case 0:
488 break;
489 case 1:
490 res = latency[0];
491 break;
492 case 2:
493 res = getRandomInRange(latency[0], latency[1]);
494 break;
495 default:
496 LOG(ERROR) << "ERROR: unexpected input of size " << latency.size();
497 break;
498 }
499
500 return res;
501}
502
503int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) {
504 std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2));
505 return dist(mRandom);
506}
507
Jeff Pu437516e2023-06-28 15:21:21 +0000508bool FakeFingerprintEngine::checkSensorLockout(ISessionCallback* cb) {
509 FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode();
510 if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) {
511 LOG(ERROR) << "Fail: lockout permanent";
512 cb->onLockoutPermanent();
Jeff Puc6f21462023-08-04 13:41:37 +0000513 isLockoutTimerAborted = true;
Jeff Pu437516e2023-06-28 15:21:21 +0000514 return true;
515 } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) {
516 int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft();
517 LOG(ERROR) << "Fail: lockout timed " << timeLeft;
518 cb->onLockoutTimed(timeLeft);
Jeff Puc6f21462023-08-04 13:41:37 +0000519 if (isLockoutTimerSupported && !isLockoutTimerStarted) startLockoutTimer(timeLeft, cb);
Jeff Pu437516e2023-06-28 15:21:21 +0000520 return true;
521 }
522 return false;
523}
Jeff Puc6f21462023-08-04 13:41:37 +0000524
525void FakeFingerprintEngine::startLockoutTimer(int64_t timeout, ISessionCallback* cb) {
526 BEGIN_OP(0);
527 std::function<void(ISessionCallback*)> action =
528 std::bind(&FakeFingerprintEngine::lockoutTimerExpired, this, std::placeholders::_1);
529 std::thread([timeout, action, cb]() {
530 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
531 action(cb);
532 }).detach();
533
534 isLockoutTimerStarted = true;
535}
536void FakeFingerprintEngine::lockoutTimerExpired(ISessionCallback* cb) {
Jeff Pu29df1e92023-10-06 14:12:44 +0000537 BEGIN_OP(0);
Jeff Puc6f21462023-08-04 13:41:37 +0000538 if (!isLockoutTimerAborted) {
539 clearLockout(cb);
540 }
541 isLockoutTimerStarted = false;
542 isLockoutTimerAborted = false;
543}
Jeff Pu74e25d22024-01-08 22:21:20 +0000544
545void FakeFingerprintEngine::waitForFingerDown(ISessionCallback* cb,
546 const std::future<void>& cancel) {
547 while (!mFingerIsDown) {
548 if (shouldCancel(cancel)) {
549 LOG(ERROR) << "waitForFingerDown, Fail: cancel";
550 cb->onError(Error::CANCELED, 0 /* vendorCode */);
551 return;
552 }
553 SLEEP_MS(10);
554 }
555}
556
Joe Bolingerde94aa02021-12-09 17:00:32 -0800557} // namespace aidl::android::hardware::biometrics::fingerprint