blob: b496a6abbe2646b2c133069b22d2051e8275f24a [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() {
92 LOG(INFO) << __func__;
93 switch (mWorkMode) {
94 case WorkMode::kAuthenticate:
95 onAuthenticateFingerDown(mCb, mOperationId, mCancel);
96 break;
97 case WorkMode::kEnroll:
98 onEnrollFingerDown(mCb, mHat, mCancel);
99 break;
100 case WorkMode::kDetectInteract:
101 onDetectInteractFingerDown(mCb, mCancel);
102 break;
103 default:
104 LOG(WARNING) << "unexpected mode: on fingerDownAction(), " << (int)mWorkMode;
105 break;
106 }
107}
108
109void FakeFingerprintEngine::onEnrollFingerDown(ISessionCallback* cb,
110 const keymaster::HardwareAuthToken& hat,
111 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400112 BEGIN_OP(getLatency(FingerprintHalProperties::operation_enroll_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800113
114 // Do proper HAT verification in the real implementation.
115 if (hat.mac.empty()) {
116 LOG(ERROR) << "Fail: hat";
117 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
118 return;
119 }
120
Jeff Pu343ca942022-09-14 15:56:30 -0400121 // Force error-out
122 auto err = FingerprintHalProperties::operation_enroll_error().value_or(0);
123 if (err != 0) {
124 LOG(ERROR) << "Fail: operation_enroll_error";
125 auto ec = convertError(err);
126 cb->onError(ec.first, ec.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800127 return;
128 }
129
Jeff Pu343ca942022-09-14 15:56:30 -0400130 // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result>
Joe Bolingerde94aa02021-12-09 17:00:32 -0800131 auto nextEnroll = FingerprintHalProperties::next_enrollment().value_or("");
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000132 auto parts = Util::split(nextEnroll, ":");
Joe Bolingerde94aa02021-12-09 17:00:32 -0800133 if (parts.size() != 3) {
Jeff Pu343ca942022-09-14 15:56:30 -0400134 LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800135 cb->onError(Error::VENDOR, 0 /* vendorError */);
136 return;
137 }
138 auto enrollmentId = std::stoi(parts[0]);
Jeff Pu343ca942022-09-14 15:56:30 -0400139 auto progress = parseEnrollmentCapture(parts[1]);
140 for (size_t i = 0; i < progress.size(); i += 2) {
141 auto left = (progress.size() - i) / 2 - 1;
142 auto duration = progress[i][0];
143 auto acquired = progress[i + 1];
144 auto N = acquired.size();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800145
Jeff Pu343ca942022-09-14 15:56:30 -0400146 for (int j = 0; j < N; j++) {
147 SLEEP_MS(duration / N);
148
149 if (shouldCancel(cancel)) {
150 LOG(ERROR) << "Fail: cancel";
151 cb->onError(Error::CANCELED, 0 /* vendorCode */);
152 return;
153 }
154 auto ac = convertAcquiredInfo(acquired[j]);
155 cb->onAcquired(ac.first, ac.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800156 }
157
Joe Bolingerde94aa02021-12-09 17:00:32 -0800158 if (left == 0 && !IS_TRUE(parts[2])) { // end and failed
159 LOG(ERROR) << "Fail: requested by caller: " << nextEnroll;
160 FingerprintHalProperties::next_enrollment({});
161 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */);
162 } else { // progress and update props if last time
Jeff Pu343ca942022-09-14 15:56:30 -0400163 LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800164 if (left == 0) {
165 auto enrollments = FingerprintHalProperties::enrollments();
166 enrollments.emplace_back(enrollmentId);
167 FingerprintHalProperties::enrollments(enrollments);
168 FingerprintHalProperties::next_enrollment({});
Jeff Pu343ca942022-09-14 15:56:30 -0400169 // change authenticatorId after new enrollment
170 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
171 auto newId = id + 1;
172 FingerprintHalProperties::authenticator_id(newId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800173 LOG(INFO) << "Enrolled: " << enrollmentId;
174 }
175 cb->onEnrollmentProgress(enrollmentId, left);
176 }
177 }
178}
179
Jeff Pudef5b042023-05-25 14:28:16 -0400180void FakeFingerprintEngine::onAuthenticateFingerDown(ISessionCallback* cb,
181 int64_t /* operationId */,
182 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400183 BEGIN_OP(getLatency(FingerprintHalProperties::operation_authenticate_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800184
Jeff Pu343ca942022-09-14 15:56:30 -0400185 int64_t now = Util::getSystemNanoTime();
186 int64_t duration = FingerprintHalProperties::operation_authenticate_duration().value_or(10);
187 auto acquired = FingerprintHalProperties::operation_authenticate_acquired().value_or("1");
188 auto acquiredInfos = parseIntSequence(acquired);
189 int N = acquiredInfos.size();
190
191 if (N == 0) {
192 LOG(ERROR) << "Fail to parse authentiate acquired info: " + acquired;
193 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
194 return;
195 }
196
Jeff Pu52653182022-10-12 16:27:23 -0400197 // got lockout?
198 FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode();
199 if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) {
200 LOG(ERROR) << "Fail: lockout permanent";
201 cb->onLockoutPermanent();
202 return;
203 } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) {
204 int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft();
205 LOG(ERROR) << "Fail: lockout timed " << timeLeft;
206 cb->onLockoutTimed(timeLeft);
207 }
208
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();
215 return;
216 }
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);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800223 return;
224 }
225
226 if (FingerprintHalProperties::lockout().value_or(false)) {
227 LOG(ERROR) << "Fail: lockout";
228 cb->onLockoutPermanent();
229 cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */);
230 return;
231 }
232
233 if (shouldCancel(cancel)) {
234 LOG(ERROR) << "Fail: cancel";
235 cb->onError(Error::CANCELED, 0 /* vendorCode */);
236 return;
237 }
238
Jeff Pu343ca942022-09-14 15:56:30 -0400239 if (i < N) {
240 auto ac = convertAcquiredInfo(acquiredInfos[i]);
241 cb->onAcquired(ac.first, ac.second);
242 i++;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800243 }
244
Jeff Pu343ca942022-09-14 15:56:30 -0400245 SLEEP_MS(duration / N);
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000246 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800247
Jeff Pu343ca942022-09-14 15:56:30 -0400248 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
249 auto enrolls = FingerprintHalProperties::enrollments();
250 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
251 if (id > 0 && isEnrolled) {
252 cb->onAuthenticationSucceeded(id, {} /* hat */);
Jeff Pu52653182022-10-12 16:27:23 -0400253 mLockoutTracker.reset();
Jeff Pu343ca942022-09-14 15:56:30 -0400254 return;
255 } else {
256 LOG(ERROR) << "Fail: fingerprint not enrolled";
257 cb->onAuthenticationFailed();
Jeff Pu52653182022-10-12 16:27:23 -0400258 mLockoutTracker.addFailedAttempt();
Jeff Pu343ca942022-09-14 15:56:30 -0400259 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800260}
261
Jeff Pudef5b042023-05-25 14:28:16 -0400262void FakeFingerprintEngine::onDetectInteractFingerDown(ISessionCallback* cb,
263 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400264 BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800265
Jeff Pu343ca942022-09-14 15:56:30 -0400266 int64_t duration =
267 FingerprintHalProperties::operation_detect_interaction_duration().value_or(10);
Jeff Pu52653182022-10-12 16:27:23 -0400268
Jeff Pu343ca942022-09-14 15:56:30 -0400269 auto acquired = FingerprintHalProperties::operation_detect_interaction_acquired().value_or("1");
270 auto acquiredInfos = parseIntSequence(acquired);
271 int N = acquiredInfos.size();
272 int64_t now = Util::getSystemNanoTime();
273
274 if (N == 0) {
275 LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired;
276 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800277 return;
278 }
279
Jeff Pu343ca942022-09-14 15:56:30 -0400280 int i = 0;
281 do {
282 auto err = FingerprintHalProperties::operation_detect_interaction_error().value_or(0);
283 if (err != 0) {
284 LOG(ERROR) << "Fail: operation_detect_interaction_error";
285 auto ec = convertError(err);
286 cb->onError(ec.first, ec.second);
287 return;
288 }
289
290 if (shouldCancel(cancel)) {
291 LOG(ERROR) << "Fail: cancel";
292 cb->onError(Error::CANCELED, 0 /* vendorCode */);
293 return;
294 }
295
296 if (i < N) {
297 auto ac = convertAcquiredInfo(acquiredInfos[i]);
298 cb->onAcquired(ac.first, ac.second);
299 i++;
300 }
301 SLEEP_MS(duration / N);
302 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800303
304 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
305 auto enrolls = FingerprintHalProperties::enrollments();
306 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
307 if (id <= 0 || !isEnrolled) {
308 LOG(ERROR) << "Fail: not enrolled";
309 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
310 return;
311 }
312
313 cb->onInteractionDetected();
314}
315
316void FakeFingerprintEngine::enumerateEnrollmentsImpl(ISessionCallback* cb) {
317 BEGIN_OP(0);
318
319 std::vector<int32_t> ids;
320 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
321 auto id = enrollment.value_or(0);
322 if (id > 0) {
323 ids.push_back(id);
324 }
325 }
326
327 cb->onEnrollmentsEnumerated(ids);
328}
329
330void FakeFingerprintEngine::removeEnrollmentsImpl(ISessionCallback* cb,
331 const std::vector<int32_t>& enrollmentIds) {
332 BEGIN_OP(0);
333
334 std::vector<std::optional<int32_t>> newEnrollments;
335 std::vector<int32_t> removed;
336 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
337 auto id = enrollment.value_or(0);
338 if (std::find(enrollmentIds.begin(), enrollmentIds.end(), id) != enrollmentIds.end()) {
339 removed.push_back(id);
340 } else if (id > 0) {
341 newEnrollments.emplace_back(id);
342 }
343 }
344 FingerprintHalProperties::enrollments(newEnrollments);
345
346 cb->onEnrollmentsRemoved(enrollmentIds);
347}
348
349void FakeFingerprintEngine::getAuthenticatorIdImpl(ISessionCallback* cb) {
350 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400351 int64_t authenticatorId;
352 if (FingerprintHalProperties::enrollments().size() == 0) {
353 authenticatorId = 0;
354 } else {
355 authenticatorId = FingerprintHalProperties::authenticator_id().value_or(0);
356 if (authenticatorId == 0) authenticatorId = 1;
Jeff Pu63f33c72022-07-28 16:06:23 -0400357 }
358 cb->onAuthenticatorIdRetrieved(authenticatorId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800359}
360
361void FakeFingerprintEngine::invalidateAuthenticatorIdImpl(ISessionCallback* cb) {
362 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400363 int64_t newId;
364 if (FingerprintHalProperties::enrollments().size() == 0) {
365 newId = 0;
366 } else {
367 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
368 newId = id + 1;
369 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800370 FingerprintHalProperties::authenticator_id(newId);
371 cb->onAuthenticatorIdInvalidated(newId);
372}
373
374void FakeFingerprintEngine::resetLockoutImpl(ISessionCallback* cb,
Jeff Pu343ca942022-09-14 15:56:30 -0400375 const keymaster::HardwareAuthToken& hat) {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800376 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400377 if (hat.mac.empty()) {
378 LOG(ERROR) << "Fail: hat in resetLockout()";
379 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
380 return;
381 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800382 FingerprintHalProperties::lockout(false);
383 cb->onLockoutCleared();
Jeff Pu52653182022-10-12 16:27:23 -0400384 mLockoutTracker.reset();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800385}
386
Jeff Pu63f33c72022-07-28 16:06:23 -0400387ndk::ScopedAStatus FakeFingerprintEngine::onPointerDownImpl(int32_t /*pointerId*/, int32_t /*x*/,
388 int32_t /*y*/, float /*minor*/,
389 float /*major*/) {
390 BEGIN_OP(0);
Jeff Pudef5b042023-05-25 14:28:16 -0400391 fingerDownAction();
Jeff Pu63f33c72022-07-28 16:06:23 -0400392 return ndk::ScopedAStatus::ok();
393}
394
395ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) {
396 BEGIN_OP(0);
397 return ndk::ScopedAStatus::ok();
398}
399
400ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() {
401 BEGIN_OP(0);
402 return ndk::ScopedAStatus::ok();
403}
404
405bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) {
406 auto loc = FingerprintHalProperties::sensor_location().value_or("");
407 auto isValidStr = false;
408 auto dim = Util::split(loc, ":");
409
410 if (dim.size() < 3 or dim.size() > 4) {
411 if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc;
412 return false;
413 } else {
414 int32_t x, y, r;
415 std::string d = "";
416 if (dim.size() >= 3) {
417 isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r);
418 }
419 if (dim.size() >= 4) {
420 d = dim[3];
421 }
Jeff Pudef5b042023-05-25 14:28:16 -0400422 if (isValidStr)
423 out = {.sensorLocationX = x, .sensorLocationY = y, .sensorRadius = r, .display = d};
Jeff Pu63f33c72022-07-28 16:06:23 -0400424
425 return isValidStr;
426 }
427}
428SensorLocation FakeFingerprintEngine::getSensorLocation() {
429 SensorLocation location;
430
431 if (getSensorLocationConfig(location)) {
432 return location;
433 } else {
434 return defaultSensorLocation();
435 }
436}
437
438SensorLocation FakeFingerprintEngine::defaultSensorLocation() {
Jeff Pudef5b042023-05-25 14:28:16 -0400439 return SensorLocation();
Jeff Pu63f33c72022-07-28 16:06:23 -0400440}
Jeff Pu343ca942022-09-14 15:56:30 -0400441
442std::vector<int32_t> FakeFingerprintEngine::parseIntSequence(const std::string& str,
443 const std::string& sep) {
444 std::vector<std::string> seqs = Util::split(str, sep);
445 std::vector<int32_t> res;
446
447 for (const auto& seq : seqs) {
448 int32_t val;
449 if (ParseInt(seq, &val)) {
450 res.push_back(val);
451 } else {
452 LOG(WARNING) << "Invalid int sequence:" + str;
453 res.clear();
454 break;
455 }
456 }
457
458 return res;
459}
460
Jeff Pu52653182022-10-12 16:27:23 -0400461bool FakeFingerprintEngine::parseEnrollmentCaptureSingle(const std::string& str,
462 std::vector<std::vector<int32_t>>& res) {
Jeff Pu343ca942022-09-14 15:56:30 -0400463 std::vector<int32_t> defaultAcquiredInfo = {(int32_t)AcquiredInfo::GOOD};
Jeff Pu343ca942022-09-14 15:56:30 -0400464 bool aborted = true;
465
Jeff Pu52653182022-10-12 16:27:23 -0400466 do {
467 std::smatch sms;
468 // Parses strings like "1000-[5,1]" or "500"
469 std::regex ex("((\\d+)(-\\[([\\d|,]+)\\])?)");
470 if (!regex_match(str.cbegin(), str.cend(), sms, ex)) break;
471 int32_t duration;
472 if (!ParseInt(sms.str(2), &duration)) break;
473 res.push_back({duration});
474 if (!sms.str(4).empty()) {
475 auto acqv = parseIntSequence(sms.str(4));
476 if (acqv.empty()) break;
477 res.push_back(acqv);
Jeff Pu343ca942022-09-14 15:56:30 -0400478 } else
479 res.push_back(defaultAcquiredInfo);
Jeff Pu52653182022-10-12 16:27:23 -0400480 aborted = false;
481 } while (0);
Jeff Pu343ca942022-09-14 15:56:30 -0400482
Jeff Pu52653182022-10-12 16:27:23 -0400483 return !aborted;
484}
485
486std::vector<std::vector<int32_t>> FakeFingerprintEngine::parseEnrollmentCapture(
487 const std::string& str) {
488 std::vector<std::vector<int32_t>> res;
489
490 std::string s(str);
491 s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());
492 bool aborted = false;
493 std::smatch sms;
494 // Parses strings like "1000-[5,1],500,800-[6,5,1]"
495 // ---------- --- -----------
496 // into parts: A B C
497 while (regex_search(s, sms, std::regex("^(,)?(\\d+(-\\[[\\d|,]+\\])?)"))) {
498 if (!parseEnrollmentCaptureSingle(sms.str(2), res)) {
499 aborted = true;
500 break;
501 }
502 s = sms.suffix();
Jeff Pu343ca942022-09-14 15:56:30 -0400503 }
Jeff Pu52653182022-10-12 16:27:23 -0400504 if (aborted || s.length() != 0) {
Jeff Pu343ca942022-09-14 15:56:30 -0400505 res.clear();
Jeff Pu52653182022-10-12 16:27:23 -0400506 LOG(ERROR) << "Failed to parse enrollment captures:" + str;
Jeff Pu343ca942022-09-14 15:56:30 -0400507 }
508
509 return res;
510}
511
512std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) {
513 std::pair<AcquiredInfo, int32_t> res;
514 if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) {
515 res.first = AcquiredInfo::VENDOR;
516 res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE;
517 } else {
518 res.first = (AcquiredInfo)code;
519 res.second = 0;
520 }
521 return res;
522}
523
524std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) {
525 std::pair<Error, int32_t> res;
526 if (code > FINGERPRINT_ERROR_VENDOR_BASE) {
527 res.first = Error::VENDOR;
528 res.second = code - FINGERPRINT_ERROR_VENDOR_BASE;
529 } else {
530 res.first = (Error)code;
531 res.second = 0;
532 }
533 return res;
534}
535
Jeff Pu52653182022-10-12 16:27:23 -0400536int32_t FakeFingerprintEngine::getLatency(
537 const std::vector<std::optional<std::int32_t>>& latencyIn) {
538 int32_t res = DEFAULT_LATENCY;
539
540 std::vector<int32_t> latency;
541 for (auto x : latencyIn)
542 if (x.has_value()) latency.push_back(*x);
543
544 switch (latency.size()) {
545 case 0:
546 break;
547 case 1:
548 res = latency[0];
549 break;
550 case 2:
551 res = getRandomInRange(latency[0], latency[1]);
552 break;
553 default:
554 LOG(ERROR) << "ERROR: unexpected input of size " << latency.size();
555 break;
556 }
557
558 return res;
559}
560
561int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) {
562 std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2));
563 return dist(mRandom);
564}
565
Joe Bolingerde94aa02021-12-09 17:00:32 -0800566} // namespace aidl::android::hardware::biometrics::fingerprint