blob: ec28846a6aafe619471f7dbc957ac6a23e39a863 [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);
Jeff Pu5055e3c2023-07-27 11:26:50 -040055
56 // Do proper HAT verification in the real implementation.
57 if (hat.mac.empty()) {
58 LOG(ERROR) << "Fail: hat";
59 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
60 return;
61 }
62
Jeff Pudef5b042023-05-25 14:28:16 -040063 updateContext(WorkMode::kEnroll, cb, const_cast<std::future<void>&>(cancel), 0, hat);
64}
65
66void FakeFingerprintEngine::authenticateImpl(ISessionCallback* cb, int64_t operationId,
67 const std::future<void>& cancel) {
68 BEGIN_OP(0);
69 updateContext(WorkMode::kAuthenticate, cb, const_cast<std::future<void>&>(cancel), operationId,
70 keymaster::HardwareAuthToken());
71}
72
73void FakeFingerprintEngine::detectInteractionImpl(ISessionCallback* cb,
74 const std::future<void>& cancel) {
75 BEGIN_OP(0);
76
77 auto detectInteractionSupported =
78 FingerprintHalProperties::detect_interaction().value_or(false);
79 if (!detectInteractionSupported) {
80 LOG(ERROR) << "Detect interaction is not supported";
81 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
82 return;
83 }
84
85 updateContext(WorkMode::kDetectInteract, cb, const_cast<std::future<void>&>(cancel), 0,
86 keymaster::HardwareAuthToken());
87}
88
89void FakeFingerprintEngine::updateContext(WorkMode mode, ISessionCallback* cb,
90 std::future<void>& cancel, int64_t operationId,
91 const keymaster::HardwareAuthToken& hat) {
92 mCancel = std::move(cancel);
93 mWorkMode = mode;
94 mCb = cb;
95 mOperationId = operationId;
96 mHat = hat;
97}
98
99void FakeFingerprintEngine::fingerDownAction() {
Jeff Pu073af182023-07-12 18:53:52 +0000100 bool isTerminal = false;
Jeff Pudef5b042023-05-25 14:28:16 -0400101 LOG(INFO) << __func__;
102 switch (mWorkMode) {
103 case WorkMode::kAuthenticate:
Jeff Pu073af182023-07-12 18:53:52 +0000104 isTerminal = onAuthenticateFingerDown(mCb, mOperationId, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400105 break;
106 case WorkMode::kEnroll:
Jeff Pu073af182023-07-12 18:53:52 +0000107 isTerminal = onEnrollFingerDown(mCb, mHat, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400108 break;
109 case WorkMode::kDetectInteract:
Jeff Pu073af182023-07-12 18:53:52 +0000110 isTerminal = onDetectInteractFingerDown(mCb, mCancel);
Jeff Pudef5b042023-05-25 14:28:16 -0400111 break;
112 default:
113 LOG(WARNING) << "unexpected mode: on fingerDownAction(), " << (int)mWorkMode;
114 break;
115 }
Jeff Pu073af182023-07-12 18:53:52 +0000116
117 if (isTerminal) {
118 mWorkMode = WorkMode::kIdle;
119 }
Jeff Pudef5b042023-05-25 14:28:16 -0400120}
121
Jeff Pu073af182023-07-12 18:53:52 +0000122bool FakeFingerprintEngine::onEnrollFingerDown(ISessionCallback* cb,
Jeff Pu5055e3c2023-07-27 11:26:50 -0400123 const keymaster::HardwareAuthToken&,
Jeff Pudef5b042023-05-25 14:28:16 -0400124 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400125 BEGIN_OP(getLatency(FingerprintHalProperties::operation_enroll_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800126
Jeff Pu343ca942022-09-14 15:56:30 -0400127 // Force error-out
128 auto err = FingerprintHalProperties::operation_enroll_error().value_or(0);
129 if (err != 0) {
130 LOG(ERROR) << "Fail: operation_enroll_error";
131 auto ec = convertError(err);
132 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000133 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800134 }
135
Jeff Pu343ca942022-09-14 15:56:30 -0400136 // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result>
Joe Bolingerde94aa02021-12-09 17:00:32 -0800137 auto nextEnroll = FingerprintHalProperties::next_enrollment().value_or("");
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000138 auto parts = Util::split(nextEnroll, ":");
Joe Bolingerde94aa02021-12-09 17:00:32 -0800139 if (parts.size() != 3) {
Jeff Pu343ca942022-09-14 15:56:30 -0400140 LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800141 cb->onError(Error::VENDOR, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000142 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800143 }
144 auto enrollmentId = std::stoi(parts[0]);
Jeff Pu343ca942022-09-14 15:56:30 -0400145 auto progress = parseEnrollmentCapture(parts[1]);
146 for (size_t i = 0; i < progress.size(); i += 2) {
147 auto left = (progress.size() - i) / 2 - 1;
148 auto duration = progress[i][0];
149 auto acquired = progress[i + 1];
150 auto N = acquired.size();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800151
Jeff Pu343ca942022-09-14 15:56:30 -0400152 for (int j = 0; j < N; j++) {
153 SLEEP_MS(duration / N);
154
155 if (shouldCancel(cancel)) {
156 LOG(ERROR) << "Fail: cancel";
157 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000158 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400159 }
160 auto ac = convertAcquiredInfo(acquired[j]);
161 cb->onAcquired(ac.first, ac.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800162 }
163
Joe Bolingerde94aa02021-12-09 17:00:32 -0800164 if (left == 0 && !IS_TRUE(parts[2])) { // end and failed
165 LOG(ERROR) << "Fail: requested by caller: " << nextEnroll;
166 FingerprintHalProperties::next_enrollment({});
167 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */);
168 } else { // progress and update props if last time
Jeff Pu343ca942022-09-14 15:56:30 -0400169 LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800170 if (left == 0) {
171 auto enrollments = FingerprintHalProperties::enrollments();
172 enrollments.emplace_back(enrollmentId);
173 FingerprintHalProperties::enrollments(enrollments);
174 FingerprintHalProperties::next_enrollment({});
Jeff Pu343ca942022-09-14 15:56:30 -0400175 // change authenticatorId after new enrollment
176 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
177 auto newId = id + 1;
178 FingerprintHalProperties::authenticator_id(newId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800179 LOG(INFO) << "Enrolled: " << enrollmentId;
180 }
181 cb->onEnrollmentProgress(enrollmentId, left);
182 }
183 }
Jeff Pu073af182023-07-12 18:53:52 +0000184
185 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800186}
187
Jeff Pu073af182023-07-12 18:53:52 +0000188bool FakeFingerprintEngine::onAuthenticateFingerDown(ISessionCallback* cb,
Jeff Pudef5b042023-05-25 14:28:16 -0400189 int64_t /* operationId */,
190 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400191 BEGIN_OP(getLatency(FingerprintHalProperties::operation_authenticate_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800192
Jeff Pu343ca942022-09-14 15:56:30 -0400193 int64_t now = Util::getSystemNanoTime();
194 int64_t duration = FingerprintHalProperties::operation_authenticate_duration().value_or(10);
195 auto acquired = FingerprintHalProperties::operation_authenticate_acquired().value_or("1");
196 auto acquiredInfos = parseIntSequence(acquired);
197 int N = acquiredInfos.size();
198
199 if (N == 0) {
200 LOG(ERROR) << "Fail to parse authentiate acquired info: " + acquired;
201 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000202 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400203 }
204
Jeff Pu52653182022-10-12 16:27:23 -0400205 // got lockout?
Jeff Pu073af182023-07-12 18:53:52 +0000206 if (checkSensorLockout(cb)) {
207 return FakeLockoutTracker::LockoutMode::kPermanent == mLockoutTracker.getMode();
208 }
Jeff Pu52653182022-10-12 16:27:23 -0400209
Jeff Pu343ca942022-09-14 15:56:30 -0400210 int i = 0;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800211 do {
212 if (FingerprintHalProperties::operation_authenticate_fails().value_or(false)) {
213 LOG(ERROR) << "Fail: operation_authenticate_fails";
Jeff Pu52653182022-10-12 16:27:23 -0400214 mLockoutTracker.addFailedAttempt();
Jeff Pu343ca942022-09-14 15:56:30 -0400215 cb->onAuthenticationFailed();
Jeff Pu073af182023-07-12 18:53:52 +0000216 return false;
Jeff Pu343ca942022-09-14 15:56:30 -0400217 }
218
219 auto err = FingerprintHalProperties::operation_authenticate_error().value_or(0);
220 if (err != 0) {
221 LOG(ERROR) << "Fail: operation_authenticate_error";
222 auto ec = convertError(err);
223 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000224 return true; /* simply terminating current operation for any user inserted error,
225 revisit if tests need*/
Joe Bolingerde94aa02021-12-09 17:00:32 -0800226 }
227
228 if (FingerprintHalProperties::lockout().value_or(false)) {
229 LOG(ERROR) << "Fail: lockout";
230 cb->onLockoutPermanent();
231 cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000232 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800233 }
234
235 if (shouldCancel(cancel)) {
236 LOG(ERROR) << "Fail: cancel";
237 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000238 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800239 }
240
Jeff Pu343ca942022-09-14 15:56:30 -0400241 if (i < N) {
242 auto ac = convertAcquiredInfo(acquiredInfos[i]);
243 cb->onAcquired(ac.first, ac.second);
244 i++;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800245 }
246
Jeff Pu343ca942022-09-14 15:56:30 -0400247 SLEEP_MS(duration / N);
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000248 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800249
Jeff Pu343ca942022-09-14 15:56:30 -0400250 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
251 auto enrolls = FingerprintHalProperties::enrollments();
252 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
253 if (id > 0 && isEnrolled) {
254 cb->onAuthenticationSucceeded(id, {} /* hat */);
Jeff Pu52653182022-10-12 16:27:23 -0400255 mLockoutTracker.reset();
Jeff Pu073af182023-07-12 18:53:52 +0000256 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400257 } else {
258 LOG(ERROR) << "Fail: fingerprint not enrolled";
259 cb->onAuthenticationFailed();
Jeff Pu52653182022-10-12 16:27:23 -0400260 mLockoutTracker.addFailedAttempt();
Jeff Pu437516e2023-06-28 15:21:21 +0000261 checkSensorLockout(cb);
Jeff Pu8fec5562023-07-20 13:07:04 +0000262 return false;
Jeff Pu343ca942022-09-14 15:56:30 -0400263 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800264}
265
Jeff Pu073af182023-07-12 18:53:52 +0000266bool FakeFingerprintEngine::onDetectInteractFingerDown(ISessionCallback* cb,
Jeff Pudef5b042023-05-25 14:28:16 -0400267 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400268 BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800269
Jeff Pu343ca942022-09-14 15:56:30 -0400270 int64_t duration =
271 FingerprintHalProperties::operation_detect_interaction_duration().value_or(10);
Jeff Pu52653182022-10-12 16:27:23 -0400272
Jeff Pu343ca942022-09-14 15:56:30 -0400273 auto acquired = FingerprintHalProperties::operation_detect_interaction_acquired().value_or("1");
274 auto acquiredInfos = parseIntSequence(acquired);
275 int N = acquiredInfos.size();
276 int64_t now = Util::getSystemNanoTime();
277
278 if (N == 0) {
279 LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired;
280 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000281 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800282 }
283
Jeff Pu343ca942022-09-14 15:56:30 -0400284 int i = 0;
285 do {
286 auto err = FingerprintHalProperties::operation_detect_interaction_error().value_or(0);
287 if (err != 0) {
288 LOG(ERROR) << "Fail: operation_detect_interaction_error";
289 auto ec = convertError(err);
290 cb->onError(ec.first, ec.second);
Jeff Pu073af182023-07-12 18:53:52 +0000291 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400292 }
293
294 if (shouldCancel(cancel)) {
295 LOG(ERROR) << "Fail: cancel";
296 cb->onError(Error::CANCELED, 0 /* vendorCode */);
Jeff Pu073af182023-07-12 18:53:52 +0000297 return true;
Jeff Pu343ca942022-09-14 15:56:30 -0400298 }
299
300 if (i < N) {
301 auto ac = convertAcquiredInfo(acquiredInfos[i]);
302 cb->onAcquired(ac.first, ac.second);
303 i++;
304 }
305 SLEEP_MS(duration / N);
306 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800307
308 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
309 auto enrolls = FingerprintHalProperties::enrollments();
310 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
311 if (id <= 0 || !isEnrolled) {
312 LOG(ERROR) << "Fail: not enrolled";
313 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Jeff Pu073af182023-07-12 18:53:52 +0000314 return true;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800315 }
316
317 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 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800388 FingerprintHalProperties::lockout(false);
389 cb->onLockoutCleared();
Jeff Pu52653182022-10-12 16:27:23 -0400390 mLockoutTracker.reset();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800391}
392
Jeff Pu63f33c72022-07-28 16:06:23 -0400393ndk::ScopedAStatus FakeFingerprintEngine::onPointerDownImpl(int32_t /*pointerId*/, int32_t /*x*/,
394 int32_t /*y*/, float /*minor*/,
395 float /*major*/) {
396 BEGIN_OP(0);
Jeff Pudef5b042023-05-25 14:28:16 -0400397 fingerDownAction();
Jeff Pu63f33c72022-07-28 16:06:23 -0400398 return ndk::ScopedAStatus::ok();
399}
400
401ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) {
402 BEGIN_OP(0);
403 return ndk::ScopedAStatus::ok();
404}
405
406ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() {
407 BEGIN_OP(0);
408 return ndk::ScopedAStatus::ok();
409}
410
411bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) {
412 auto loc = FingerprintHalProperties::sensor_location().value_or("");
413 auto isValidStr = false;
414 auto dim = Util::split(loc, ":");
415
416 if (dim.size() < 3 or dim.size() > 4) {
417 if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc;
418 return false;
419 } else {
420 int32_t x, y, r;
421 std::string d = "";
422 if (dim.size() >= 3) {
423 isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r);
424 }
425 if (dim.size() >= 4) {
426 d = dim[3];
427 }
Jeff Pudef5b042023-05-25 14:28:16 -0400428 if (isValidStr)
429 out = {.sensorLocationX = x, .sensorLocationY = y, .sensorRadius = r, .display = d};
Jeff Pu63f33c72022-07-28 16:06:23 -0400430
431 return isValidStr;
432 }
433}
434SensorLocation FakeFingerprintEngine::getSensorLocation() {
435 SensorLocation location;
436
437 if (getSensorLocationConfig(location)) {
438 return location;
439 } else {
440 return defaultSensorLocation();
441 }
442}
443
444SensorLocation FakeFingerprintEngine::defaultSensorLocation() {
Jeff Pudef5b042023-05-25 14:28:16 -0400445 return SensorLocation();
Jeff Pu63f33c72022-07-28 16:06:23 -0400446}
Jeff Pu343ca942022-09-14 15:56:30 -0400447
448std::vector<int32_t> FakeFingerprintEngine::parseIntSequence(const std::string& str,
449 const std::string& sep) {
450 std::vector<std::string> seqs = Util::split(str, sep);
451 std::vector<int32_t> res;
452
453 for (const auto& seq : seqs) {
454 int32_t val;
455 if (ParseInt(seq, &val)) {
456 res.push_back(val);
457 } else {
458 LOG(WARNING) << "Invalid int sequence:" + str;
459 res.clear();
460 break;
461 }
462 }
463
464 return res;
465}
466
Jeff Pu52653182022-10-12 16:27:23 -0400467bool FakeFingerprintEngine::parseEnrollmentCaptureSingle(const std::string& str,
468 std::vector<std::vector<int32_t>>& res) {
Jeff Pu343ca942022-09-14 15:56:30 -0400469 std::vector<int32_t> defaultAcquiredInfo = {(int32_t)AcquiredInfo::GOOD};
Jeff Pu343ca942022-09-14 15:56:30 -0400470 bool aborted = true;
471
Jeff Pu52653182022-10-12 16:27:23 -0400472 do {
473 std::smatch sms;
474 // Parses strings like "1000-[5,1]" or "500"
475 std::regex ex("((\\d+)(-\\[([\\d|,]+)\\])?)");
476 if (!regex_match(str.cbegin(), str.cend(), sms, ex)) break;
477 int32_t duration;
478 if (!ParseInt(sms.str(2), &duration)) break;
479 res.push_back({duration});
480 if (!sms.str(4).empty()) {
481 auto acqv = parseIntSequence(sms.str(4));
482 if (acqv.empty()) break;
483 res.push_back(acqv);
Jeff Pu343ca942022-09-14 15:56:30 -0400484 } else
485 res.push_back(defaultAcquiredInfo);
Jeff Pu52653182022-10-12 16:27:23 -0400486 aborted = false;
487 } while (0);
Jeff Pu343ca942022-09-14 15:56:30 -0400488
Jeff Pu52653182022-10-12 16:27:23 -0400489 return !aborted;
490}
491
492std::vector<std::vector<int32_t>> FakeFingerprintEngine::parseEnrollmentCapture(
493 const std::string& str) {
494 std::vector<std::vector<int32_t>> res;
495
496 std::string s(str);
497 s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());
498 bool aborted = false;
499 std::smatch sms;
500 // Parses strings like "1000-[5,1],500,800-[6,5,1]"
501 // ---------- --- -----------
502 // into parts: A B C
503 while (regex_search(s, sms, std::regex("^(,)?(\\d+(-\\[[\\d|,]+\\])?)"))) {
504 if (!parseEnrollmentCaptureSingle(sms.str(2), res)) {
505 aborted = true;
506 break;
507 }
508 s = sms.suffix();
Jeff Pu343ca942022-09-14 15:56:30 -0400509 }
Jeff Pu52653182022-10-12 16:27:23 -0400510 if (aborted || s.length() != 0) {
Jeff Pu343ca942022-09-14 15:56:30 -0400511 res.clear();
Jeff Pu52653182022-10-12 16:27:23 -0400512 LOG(ERROR) << "Failed to parse enrollment captures:" + str;
Jeff Pu343ca942022-09-14 15:56:30 -0400513 }
514
515 return res;
516}
517
518std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) {
519 std::pair<AcquiredInfo, int32_t> res;
520 if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) {
521 res.first = AcquiredInfo::VENDOR;
522 res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE;
523 } else {
524 res.first = (AcquiredInfo)code;
525 res.second = 0;
526 }
527 return res;
528}
529
530std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) {
531 std::pair<Error, int32_t> res;
532 if (code > FINGERPRINT_ERROR_VENDOR_BASE) {
533 res.first = Error::VENDOR;
534 res.second = code - FINGERPRINT_ERROR_VENDOR_BASE;
535 } else {
536 res.first = (Error)code;
537 res.second = 0;
538 }
539 return res;
540}
541
Jeff Pu52653182022-10-12 16:27:23 -0400542int32_t FakeFingerprintEngine::getLatency(
543 const std::vector<std::optional<std::int32_t>>& latencyIn) {
544 int32_t res = DEFAULT_LATENCY;
545
546 std::vector<int32_t> latency;
547 for (auto x : latencyIn)
548 if (x.has_value()) latency.push_back(*x);
549
550 switch (latency.size()) {
551 case 0:
552 break;
553 case 1:
554 res = latency[0];
555 break;
556 case 2:
557 res = getRandomInRange(latency[0], latency[1]);
558 break;
559 default:
560 LOG(ERROR) << "ERROR: unexpected input of size " << latency.size();
561 break;
562 }
563
564 return res;
565}
566
567int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) {
568 std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2));
569 return dist(mRandom);
570}
571
Jeff Pu437516e2023-06-28 15:21:21 +0000572bool FakeFingerprintEngine::checkSensorLockout(ISessionCallback* cb) {
573 FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode();
574 if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) {
575 LOG(ERROR) << "Fail: lockout permanent";
576 cb->onLockoutPermanent();
577 return true;
578 } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) {
579 int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft();
580 LOG(ERROR) << "Fail: lockout timed " << timeLeft;
581 cb->onLockoutTimed(timeLeft);
582 return true;
583 }
584 return false;
585}
Joe Bolingerde94aa02021-12-09 17:00:32 -0800586} // namespace aidl::android::hardware::biometrics::fingerprint