blob: 975728764b46303de31b1c4a24354424ee0f5028 [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()
35 : mRandom(std::mt19937::default_seed), mWorkMode(WorkMode::kIdle) {}
36
Joe Bolingerde94aa02021-12-09 17:00:32 -080037void FakeFingerprintEngine::generateChallengeImpl(ISessionCallback* cb) {
38 BEGIN_OP(0);
39 std::uniform_int_distribution<int64_t> dist;
40 auto challenge = dist(mRandom);
41 FingerprintHalProperties::challenge(challenge);
42 cb->onChallengeGenerated(challenge);
43}
44
45void FakeFingerprintEngine::revokeChallengeImpl(ISessionCallback* cb, int64_t challenge) {
46 BEGIN_OP(0);
47 FingerprintHalProperties::challenge({});
48 cb->onChallengeRevoked(challenge);
49}
50
51void FakeFingerprintEngine::enrollImpl(ISessionCallback* cb,
52 const keymaster::HardwareAuthToken& hat,
53 const std::future<void>& cancel) {
Jeff Pudef5b042023-05-25 14:28:16 -040054 BEGIN_OP(0);
55 updateContext(WorkMode::kEnroll, cb, const_cast<std::future<void>&>(cancel), 0, hat);
56}
57
58void FakeFingerprintEngine::authenticateImpl(ISessionCallback* cb, int64_t operationId,
59 const std::future<void>& cancel) {
60 BEGIN_OP(0);
61 updateContext(WorkMode::kAuthenticate, cb, const_cast<std::future<void>&>(cancel), operationId,
62 keymaster::HardwareAuthToken());
63}
64
65void FakeFingerprintEngine::detectInteractionImpl(ISessionCallback* cb,
66 const std::future<void>& cancel) {
67 BEGIN_OP(0);
68
69 auto detectInteractionSupported =
70 FingerprintHalProperties::detect_interaction().value_or(false);
71 if (!detectInteractionSupported) {
72 LOG(ERROR) << "Detect interaction is not supported";
73 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
74 return;
75 }
76
77 updateContext(WorkMode::kDetectInteract, cb, const_cast<std::future<void>&>(cancel), 0,
78 keymaster::HardwareAuthToken());
79}
80
81void FakeFingerprintEngine::updateContext(WorkMode mode, ISessionCallback* cb,
82 std::future<void>& cancel, int64_t operationId,
83 const keymaster::HardwareAuthToken& hat) {
84 mCancel = std::move(cancel);
85 mWorkMode = mode;
86 mCb = cb;
87 mOperationId = operationId;
88 mHat = hat;
89}
90
91void FakeFingerprintEngine::fingerDownAction() {
Jeff Pu073af182023-07-12 18:53:52 +000092 bool isTerminal = false;
Jeff Pudef5b042023-05-25 14:28:16 -040093 LOG(INFO) << __func__;
94 switch (mWorkMode) {
95 case WorkMode::kAuthenticate:
Jeff Pu073af182023-07-12 18:53:52 +000096 isTerminal = onAuthenticateFingerDown(mCb, mOperationId, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -040097 break;
98 case WorkMode::kEnroll:
Jeff Pu073af182023-07-12 18:53:52 +000099 isTerminal = onEnrollFingerDown(mCb, mHat, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400100 break;
101 case WorkMode::kDetectInteract:
Jeff Pu073af182023-07-12 18:53:52 +0000102 isTerminal = onDetectInteractFingerDown(mCb, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400103 break;
104 default:
105 LOG(WARNING) << "unexpected mode: on fingerDownAction(), " << (int)mWorkMode;
106 break;
107 }
Jeff Pu073af182023-07-12 18:53:52 +0000108
109 if (isTerminal) {
110 mWorkMode = WorkMode::kIdle;
111 }
Jeff Pudef5b042023-05-25 14:28:16 -0400112}
113
Jeff Pu073af182023-07-12 18:53:52 +0000114bool FakeFingerprintEngine::onEnrollFingerDown(ISessionCallback* cb,
Jeff Pudef5b042023-05-25 14:28:16 -0400115 const keymaster::HardwareAuthToken& hat,
116 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400117 BEGIN_OP(getLatency(FingerprintHalProperties::operation_enroll_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800118
119 // Do proper HAT verification in the real implementation.
120 if (hat.mac.empty()) {
121 LOG(ERROR) << "Fail: hat";
122 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000123 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800124 }
125
Jeff Pu343ca942022-09-14 15:56:30 -0400126 // Force error-out
127 auto err = FingerprintHalProperties::operation_enroll_error().value_or(0);
128 if (err != 0) {
129 LOG(ERROR) << "Fail: operation_enroll_error";
130 auto ec = convertError(err);
131 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000132 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800133 }
134
Jeff Pu343ca942022-09-14 15:56:30 -0400135 // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result>
Joe Bolingerde94aa02021-12-09 17:00:32 -0800136 auto nextEnroll = FingerprintHalProperties::next_enrollment().value_or("");
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000137 auto parts = Util::split(nextEnroll, ":");
Joe Bolingerde94aa02021-12-09 17:00:32 -0800138 if (parts.size() != 3) {
Jeff Pu343ca942022-09-14 15:56:30 -0400139 LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800140 cb->onError(Error::VENDOR, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000141 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800142 }
143 auto enrollmentId = std::stoi(parts[0]);
Jeff Pu343ca942022-09-14 15:56:30 -0400144 auto progress = parseEnrollmentCapture(parts[1]);
145 for (size_t i = 0; i < progress.size(); i += 2) {
146 auto left = (progress.size() - i) / 2 - 1;
147 auto duration = progress[i][0];
148 auto acquired = progress[i + 1];
149 auto N = acquired.size();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800150
Jeff Pu343ca942022-09-14 15:56:30 -0400151 for (int j = 0; j < N; j++) {
152 SLEEP_MS(duration / N);
153
154 if (shouldCancel(cancel)) {
155 LOG(ERROR) << "Fail: cancel";
156 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000157 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400158 }
159 auto ac = convertAcquiredInfo(acquired[j]);
160 cb->onAcquired(ac.first, ac.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800161 }
162
Joe Bolingerde94aa02021-12-09 17:00:32 -0800163 if (left == 0 && !IS_TRUE(parts[2])) { // end and failed
164 LOG(ERROR) << "Fail: requested by caller: " << nextEnroll;
165 FingerprintHalProperties::next_enrollment({});
166 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */);
167 } else { // progress and update props if last time
Jeff Pu343ca942022-09-14 15:56:30 -0400168 LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800169 if (left == 0) {
170 auto enrollments = FingerprintHalProperties::enrollments();
171 enrollments.emplace_back(enrollmentId);
172 FingerprintHalProperties::enrollments(enrollments);
173 FingerprintHalProperties::next_enrollment({});
Jeff Pu343ca942022-09-14 15:56:30 -0400174 // change authenticatorId after new enrollment
175 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
176 auto newId = id + 1;
177 FingerprintHalProperties::authenticator_id(newId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800178 LOG(INFO) << "Enrolled: " << enrollmentId;
179 }
180 cb->onEnrollmentProgress(enrollmentId, left);
181 }
182 }
Jeff Pu073af182023-07-12 18:53:52 +0000183
184 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800185}
186
Jeff Pu073af182023-07-12 18:53:52 +0000187bool FakeFingerprintEngine::onAuthenticateFingerDown(ISessionCallback* cb,
Jeff Pudef5b042023-05-25 14:28:16 -0400188 int64_t /* operationId */,
189 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400190 BEGIN_OP(getLatency(FingerprintHalProperties::operation_authenticate_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800191
Jeff Pu343ca942022-09-14 15:56:30 -0400192 int64_t now = Util::getSystemNanoTime();
193 int64_t duration = FingerprintHalProperties::operation_authenticate_duration().value_or(10);
194 auto acquired = FingerprintHalProperties::operation_authenticate_acquired().value_or("1");
195 auto acquiredInfos = parseIntSequence(acquired);
196 int N = acquiredInfos.size();
197
198 if (N == 0) {
199 LOG(ERROR) << "Fail to parse authentiate acquired info: " + acquired;
200 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000201 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400202 }
203
Jeff Pu52653182022-10-12 16:27:23 -0400204 // got lockout?
Jeff Pu073af182023-07-12 18:53:52 +0000205 if (checkSensorLockout(cb)) {
206 return FakeLockoutTracker::LockoutMode::kPermanent == mLockoutTracker.getMode();
207 }
Jeff Pu52653182022-10-12 16:27:23 -0400208
Jeff Pu343ca942022-09-14 15:56:30 -0400209 int i = 0;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800210 do {
211 if (FingerprintHalProperties::operation_authenticate_fails().value_or(false)) {
212 LOG(ERROR) << "Fail: operation_authenticate_fails";
Jeff Pu52653182022-10-12 16:27:23 -0400213 mLockoutTracker.addFailedAttempt();
Jeff Pu343ca942022-09-14 15:56:30 -0400214 cb->onAuthenticationFailed();
Jeff Pu073af182023-07-12 18:53:52 +0000215 return false;
Jeff Pu343ca942022-09-14 15:56:30 -0400216 }
217
218 auto err = FingerprintHalProperties::operation_authenticate_error().value_or(0);
219 if (err != 0) {
220 LOG(ERROR) << "Fail: operation_authenticate_error";
221 auto ec = convertError(err);
222 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000223 return true; /* simply terminating current operation for any user inserted error,
224 revisit if tests need*/
Joe Bolingerde94aa02021-12-09 17:00:32 -0800225 }
226
227 if (FingerprintHalProperties::lockout().value_or(false)) {
228 LOG(ERROR) << "Fail: lockout";
229 cb->onLockoutPermanent();
230 cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000231 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800232 }
233
234 if (shouldCancel(cancel)) {
235 LOG(ERROR) << "Fail: cancel";
236 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000237 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800238 }
239
Jeff Pu343ca942022-09-14 15:56:30 -0400240 if (i < N) {
241 auto ac = convertAcquiredInfo(acquiredInfos[i]);
242 cb->onAcquired(ac.first, ac.second);
243 i++;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800244 }
245
Jeff Pu343ca942022-09-14 15:56:30 -0400246 SLEEP_MS(duration / N);
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000247 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800248
Jeff Pu343ca942022-09-14 15:56:30 -0400249 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
250 auto enrolls = FingerprintHalProperties::enrollments();
251 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
252 if (id > 0 && isEnrolled) {
253 cb->onAuthenticationSucceeded(id, {} /* hat */);
Jeff Pu52653182022-10-12 16:27:23 -0400254 mLockoutTracker.reset();
Jeff Pu073af182023-07-12 18:53:52 +0000255 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400256 } else {
257 LOG(ERROR) << "Fail: fingerprint not enrolled";
258 cb->onAuthenticationFailed();
Jeff Pu52653182022-10-12 16:27:23 -0400259 mLockoutTracker.addFailedAttempt();
Jeff Pu437516e2023-06-28 15:21:21 +0000260 checkSensorLockout(cb);
Jeff Pu073af182023-07-12 18:53:52 +0000261 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400262 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800263}
264
Jeff Pu073af182023-07-12 18:53:52 +0000265bool FakeFingerprintEngine::onDetectInteractFingerDown(ISessionCallback* cb,
Jeff Pudef5b042023-05-25 14:28:16 -0400266 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400267 BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800268
Jeff Pu343ca942022-09-14 15:56:30 -0400269 int64_t duration =
270 FingerprintHalProperties::operation_detect_interaction_duration().value_or(10);
Jeff Pu52653182022-10-12 16:27:23 -0400271
Jeff Pu343ca942022-09-14 15:56:30 -0400272 auto acquired = FingerprintHalProperties::operation_detect_interaction_acquired().value_or("1");
273 auto acquiredInfos = parseIntSequence(acquired);
274 int N = acquiredInfos.size();
275 int64_t now = Util::getSystemNanoTime();
276
277 if (N == 0) {
278 LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired;
279 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000280 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800281 }
282
Jeff Pu343ca942022-09-14 15:56:30 -0400283 int i = 0;
284 do {
285 auto err = FingerprintHalProperties::operation_detect_interaction_error().value_or(0);
286 if (err != 0) {
287 LOG(ERROR) << "Fail: operation_detect_interaction_error";
288 auto ec = convertError(err);
289 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000290 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400291 }
292
293 if (shouldCancel(cancel)) {
294 LOG(ERROR) << "Fail: cancel";
295 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000296 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400297 }
298
299 if (i < N) {
300 auto ac = convertAcquiredInfo(acquiredInfos[i]);
301 cb->onAcquired(ac.first, ac.second);
302 i++;
303 }
304 SLEEP_MS(duration / N);
305 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800306
307 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
308 auto enrolls = FingerprintHalProperties::enrollments();
309 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
310 if (id <= 0 || !isEnrolled) {
311 LOG(ERROR) << "Fail: not enrolled";
312 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000313 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800314 }
315
316 cb->onInteractionDetected();
Jeff Pu073af182023-07-12 18:53:52 +0000317
318 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800319}
320
321void FakeFingerprintEngine::enumerateEnrollmentsImpl(ISessionCallback* cb) {
322 BEGIN_OP(0);
323
324 std::vector<int32_t> ids;
325 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
326 auto id = enrollment.value_or(0);
327 if (id > 0) {
328 ids.push_back(id);
329 }
330 }
331
332 cb->onEnrollmentsEnumerated(ids);
333}
334
335void FakeFingerprintEngine::removeEnrollmentsImpl(ISessionCallback* cb,
336 const std::vector<int32_t>& enrollmentIds) {
337 BEGIN_OP(0);
338
339 std::vector<std::optional<int32_t>> newEnrollments;
340 std::vector<int32_t> removed;
341 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
342 auto id = enrollment.value_or(0);
343 if (std::find(enrollmentIds.begin(), enrollmentIds.end(), id) != enrollmentIds.end()) {
344 removed.push_back(id);
345 } else if (id > 0) {
346 newEnrollments.emplace_back(id);
347 }
348 }
349 FingerprintHalProperties::enrollments(newEnrollments);
350
351 cb->onEnrollmentsRemoved(enrollmentIds);
352}
353
354void FakeFingerprintEngine::getAuthenticatorIdImpl(ISessionCallback* cb) {
355 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400356 int64_t authenticatorId;
357 if (FingerprintHalProperties::enrollments().size() == 0) {
358 authenticatorId = 0;
359 } else {
360 authenticatorId = FingerprintHalProperties::authenticator_id().value_or(0);
361 if (authenticatorId == 0) authenticatorId = 1;
Jeff Pu63f33c72022-07-28 16:06:23 -0400362 }
363 cb->onAuthenticatorIdRetrieved(authenticatorId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800364}
365
366void FakeFingerprintEngine::invalidateAuthenticatorIdImpl(ISessionCallback* cb) {
367 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400368 int64_t newId;
369 if (FingerprintHalProperties::enrollments().size() == 0) {
370 newId = 0;
371 } else {
372 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
373 newId = id + 1;
374 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800375 FingerprintHalProperties::authenticator_id(newId);
376 cb->onAuthenticatorIdInvalidated(newId);
377}
378
379void FakeFingerprintEngine::resetLockoutImpl(ISessionCallback* cb,
Jeff Pu343ca942022-09-14 15:56:30 -0400380 const keymaster::HardwareAuthToken& hat) {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800381 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400382 if (hat.mac.empty()) {
383 LOG(ERROR) << "Fail: hat in resetLockout()";
384 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
385 return;
386 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800387 FingerprintHalProperties::lockout(false);
388 cb->onLockoutCleared();
Jeff Pu52653182022-10-12 16:27:23 -0400389 mLockoutTracker.reset();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800390}
391
Jeff Pu63f33c72022-07-28 16:06:23 -0400392ndk::ScopedAStatus FakeFingerprintEngine::onPointerDownImpl(int32_t /*pointerId*/, int32_t /*x*/,
393 int32_t /*y*/, float /*minor*/,
394 float /*major*/) {
395 BEGIN_OP(0);
Jeff Pudef5b042023-05-25 14:28:16 -0400396 fingerDownAction();
Jeff Pu63f33c72022-07-28 16:06:23 -0400397 return ndk::ScopedAStatus::ok();
398}
399
400ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) {
401 BEGIN_OP(0);
402 return ndk::ScopedAStatus::ok();
403}
404
405ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() {
406 BEGIN_OP(0);
407 return ndk::ScopedAStatus::ok();
408}
409
410bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) {
411 auto loc = FingerprintHalProperties::sensor_location().value_or("");
412 auto isValidStr = false;
413 auto dim = Util::split(loc, ":");
414
415 if (dim.size() < 3 or dim.size() > 4) {
416 if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc;
417 return false;
418 } else {
419 int32_t x, y, r;
420 std::string d = "";
421 if (dim.size() >= 3) {
422 isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r);
423 }
424 if (dim.size() >= 4) {
425 d = dim[3];
426 }
Jeff Pudef5b042023-05-25 14:28:16 -0400427 if (isValidStr)
428 out = {.sensorLocationX = x, .sensorLocationY = y, .sensorRadius = r, .display = d};
Jeff Pu63f33c72022-07-28 16:06:23 -0400429
430 return isValidStr;
431 }
432}
433SensorLocation FakeFingerprintEngine::getSensorLocation() {
434 SensorLocation location;
435
436 if (getSensorLocationConfig(location)) {
437 return location;
438 } else {
439 return defaultSensorLocation();
440 }
441}
442
443SensorLocation FakeFingerprintEngine::defaultSensorLocation() {
Jeff Pudef5b042023-05-25 14:28:16 -0400444 return SensorLocation();
Jeff Pu63f33c72022-07-28 16:06:23 -0400445}
Jeff Pu343ca942022-09-14 15:56:30 -0400446
447std::vector<int32_t> FakeFingerprintEngine::parseIntSequence(const std::string& str,
448 const std::string& sep) {
449 std::vector<std::string> seqs = Util::split(str, sep);
450 std::vector<int32_t> res;
451
452 for (const auto& seq : seqs) {
453 int32_t val;
454 if (ParseInt(seq, &val)) {
455 res.push_back(val);
456 } else {
457 LOG(WARNING) << "Invalid int sequence:" + str;
458 res.clear();
459 break;
460 }
461 }
462
463 return res;
464}
465
Jeff Pu52653182022-10-12 16:27:23 -0400466bool FakeFingerprintEngine::parseEnrollmentCaptureSingle(const std::string& str,
467 std::vector<std::vector<int32_t>>& res) {
Jeff Pu343ca942022-09-14 15:56:30 -0400468 std::vector<int32_t> defaultAcquiredInfo = {(int32_t)AcquiredInfo::GOOD};
Jeff Pu343ca942022-09-14 15:56:30 -0400469 bool aborted = true;
470
Jeff Pu52653182022-10-12 16:27:23 -0400471 do {
472 std::smatch sms;
473 // Parses strings like "1000-[5,1]" or "500"
474 std::regex ex("((\\d+)(-\\[([\\d|,]+)\\])?)");
475 if (!regex_match(str.cbegin(), str.cend(), sms, ex)) break;
476 int32_t duration;
477 if (!ParseInt(sms.str(2), &duration)) break;
478 res.push_back({duration});
479 if (!sms.str(4).empty()) {
480 auto acqv = parseIntSequence(sms.str(4));
481 if (acqv.empty()) break;
482 res.push_back(acqv);
Jeff Pu343ca942022-09-14 15:56:30 -0400483 } else
484 res.push_back(defaultAcquiredInfo);
Jeff Pu52653182022-10-12 16:27:23 -0400485 aborted = false;
486 } while (0);
Jeff Pu343ca942022-09-14 15:56:30 -0400487
Jeff Pu52653182022-10-12 16:27:23 -0400488 return !aborted;
489}
490
491std::vector<std::vector<int32_t>> FakeFingerprintEngine::parseEnrollmentCapture(
492 const std::string& str) {
493 std::vector<std::vector<int32_t>> res;
494
495 std::string s(str);
496 s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());
497 bool aborted = false;
498 std::smatch sms;
499 // Parses strings like "1000-[5,1],500,800-[6,5,1]"
500 // ---------- --- -----------
501 // into parts: A B C
502 while (regex_search(s, sms, std::regex("^(,)?(\\d+(-\\[[\\d|,]+\\])?)"))) {
503 if (!parseEnrollmentCaptureSingle(sms.str(2), res)) {
504 aborted = true;
505 break;
506 }
507 s = sms.suffix();
Jeff Pu343ca942022-09-14 15:56:30 -0400508 }
Jeff Pu52653182022-10-12 16:27:23 -0400509 if (aborted || s.length() != 0) {
Jeff Pu343ca942022-09-14 15:56:30 -0400510 res.clear();
Jeff Pu52653182022-10-12 16:27:23 -0400511 LOG(ERROR) << "Failed to parse enrollment captures:" + str;
Jeff Pu343ca942022-09-14 15:56:30 -0400512 }
513
514 return res;
515}
516
517std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) {
518 std::pair<AcquiredInfo, int32_t> res;
519 if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) {
520 res.first = AcquiredInfo::VENDOR;
521 res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE;
522 } else {
523 res.first = (AcquiredInfo)code;
524 res.second = 0;
525 }
526 return res;
527}
528
529std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) {
530 std::pair<Error, int32_t> res;
531 if (code > FINGERPRINT_ERROR_VENDOR_BASE) {
532 res.first = Error::VENDOR;
533 res.second = code - FINGERPRINT_ERROR_VENDOR_BASE;
534 } else {
535 res.first = (Error)code;
536 res.second = 0;
537 }
538 return res;
539}
540
Jeff Pu52653182022-10-12 16:27:23 -0400541int32_t FakeFingerprintEngine::getLatency(
542 const std::vector<std::optional<std::int32_t>>& latencyIn) {
543 int32_t res = DEFAULT_LATENCY;
544
545 std::vector<int32_t> latency;
546 for (auto x : latencyIn)
547 if (x.has_value()) latency.push_back(*x);
548
549 switch (latency.size()) {
550 case 0:
551 break;
552 case 1:
553 res = latency[0];
554 break;
555 case 2:
556 res = getRandomInRange(latency[0], latency[1]);
557 break;
558 default:
559 LOG(ERROR) << "ERROR: unexpected input of size " << latency.size();
560 break;
561 }
562
563 return res;
564}
565
566int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) {
567 std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2));
568 return dist(mRandom);
569}
570
Jeff Pu437516e2023-06-28 15:21:21 +0000571bool FakeFingerprintEngine::checkSensorLockout(ISessionCallback* cb) {
572 FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode();
573 if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) {
574 LOG(ERROR) << "Fail: lockout permanent";
575 cb->onLockoutPermanent();
576 return true;
577 } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) {
578 int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft();
579 LOG(ERROR) << "Fail: lockout timed " << timeLeft;
580 cb->onLockoutTimed(timeLeft);
581 return true;
582 }
583 return false;
584}
Joe Bolingerde94aa02021-12-09 17:00:32 -0800585} // namespace aidl::android::hardware::biometrics::fingerprint