blob: 90ec8f26eeee3f448ec30ef1b6d223f823889076 [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
34void FakeFingerprintEngine::generateChallengeImpl(ISessionCallback* cb) {
35 BEGIN_OP(0);
36 std::uniform_int_distribution<int64_t> dist;
37 auto challenge = dist(mRandom);
38 FingerprintHalProperties::challenge(challenge);
39 cb->onChallengeGenerated(challenge);
40}
41
42void FakeFingerprintEngine::revokeChallengeImpl(ISessionCallback* cb, int64_t challenge) {
43 BEGIN_OP(0);
44 FingerprintHalProperties::challenge({});
45 cb->onChallengeRevoked(challenge);
46}
47
48void FakeFingerprintEngine::enrollImpl(ISessionCallback* cb,
49 const keymaster::HardwareAuthToken& hat,
50 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -040051 BEGIN_OP(getLatency(FingerprintHalProperties::operation_enroll_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -080052
53 // Do proper HAT verification in the real implementation.
54 if (hat.mac.empty()) {
55 LOG(ERROR) << "Fail: hat";
56 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
57 return;
58 }
59
Jeff Pu343ca942022-09-14 15:56:30 -040060 // Force error-out
61 auto err = FingerprintHalProperties::operation_enroll_error().value_or(0);
62 if (err != 0) {
63 LOG(ERROR) << "Fail: operation_enroll_error";
64 auto ec = convertError(err);
65 cb->onError(ec.first, ec.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -080066 return;
67 }
68
Jeff Pu343ca942022-09-14 15:56:30 -040069 // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result>
Joe Bolingerde94aa02021-12-09 17:00:32 -080070 auto nextEnroll = FingerprintHalProperties::next_enrollment().value_or("");
Joshua McCloskeydb009a52022-05-10 05:18:20 +000071 auto parts = Util::split(nextEnroll, ":");
Joe Bolingerde94aa02021-12-09 17:00:32 -080072 if (parts.size() != 3) {
Jeff Pu343ca942022-09-14 15:56:30 -040073 LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll;
Joe Bolingerde94aa02021-12-09 17:00:32 -080074 cb->onError(Error::VENDOR, 0 /* vendorError */);
75 return;
76 }
77 auto enrollmentId = std::stoi(parts[0]);
Jeff Pu343ca942022-09-14 15:56:30 -040078 auto progress = parseEnrollmentCapture(parts[1]);
79 for (size_t i = 0; i < progress.size(); i += 2) {
80 auto left = (progress.size() - i) / 2 - 1;
81 auto duration = progress[i][0];
82 auto acquired = progress[i + 1];
83 auto N = acquired.size();
Joe Bolingerde94aa02021-12-09 17:00:32 -080084
Jeff Pu343ca942022-09-14 15:56:30 -040085 for (int j = 0; j < N; j++) {
86 SLEEP_MS(duration / N);
87
88 if (shouldCancel(cancel)) {
89 LOG(ERROR) << "Fail: cancel";
90 cb->onError(Error::CANCELED, 0 /* vendorCode */);
91 return;
92 }
93 auto ac = convertAcquiredInfo(acquired[j]);
94 cb->onAcquired(ac.first, ac.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -080095 }
96
Joe Bolingerde94aa02021-12-09 17:00:32 -080097 if (left == 0 && !IS_TRUE(parts[2])) { // end and failed
98 LOG(ERROR) << "Fail: requested by caller: " << nextEnroll;
99 FingerprintHalProperties::next_enrollment({});
100 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */);
101 } else { // progress and update props if last time
Jeff Pu343ca942022-09-14 15:56:30 -0400102 LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800103 if (left == 0) {
104 auto enrollments = FingerprintHalProperties::enrollments();
105 enrollments.emplace_back(enrollmentId);
106 FingerprintHalProperties::enrollments(enrollments);
107 FingerprintHalProperties::next_enrollment({});
Jeff Pu343ca942022-09-14 15:56:30 -0400108 // change authenticatorId after new enrollment
109 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
110 auto newId = id + 1;
111 FingerprintHalProperties::authenticator_id(newId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800112 LOG(INFO) << "Enrolled: " << enrollmentId;
113 }
114 cb->onEnrollmentProgress(enrollmentId, left);
115 }
116 }
117}
118
119void FakeFingerprintEngine::authenticateImpl(ISessionCallback* cb, int64_t /* operationId */,
120 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400121 BEGIN_OP(getLatency(FingerprintHalProperties::operation_authenticate_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800122
Jeff Pu343ca942022-09-14 15:56:30 -0400123 int64_t now = Util::getSystemNanoTime();
124 int64_t duration = FingerprintHalProperties::operation_authenticate_duration().value_or(10);
125 auto acquired = FingerprintHalProperties::operation_authenticate_acquired().value_or("1");
126 auto acquiredInfos = parseIntSequence(acquired);
127 int N = acquiredInfos.size();
128
129 if (N == 0) {
130 LOG(ERROR) << "Fail to parse authentiate acquired info: " + acquired;
131 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
132 return;
133 }
134
Jeff Pu52653182022-10-12 16:27:23 -0400135 // got lockout?
136 FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode();
137 if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) {
138 LOG(ERROR) << "Fail: lockout permanent";
139 cb->onLockoutPermanent();
140 return;
141 } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) {
142 int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft();
143 LOG(ERROR) << "Fail: lockout timed " << timeLeft;
144 cb->onLockoutTimed(timeLeft);
145 }
146
Jeff Pu343ca942022-09-14 15:56:30 -0400147 int i = 0;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800148 do {
149 if (FingerprintHalProperties::operation_authenticate_fails().value_or(false)) {
150 LOG(ERROR) << "Fail: operation_authenticate_fails";
Jeff Pu52653182022-10-12 16:27:23 -0400151 mLockoutTracker.addFailedAttempt();
Jeff Pu343ca942022-09-14 15:56:30 -0400152 cb->onAuthenticationFailed();
153 return;
154 }
155
156 auto err = FingerprintHalProperties::operation_authenticate_error().value_or(0);
157 if (err != 0) {
158 LOG(ERROR) << "Fail: operation_authenticate_error";
159 auto ec = convertError(err);
160 cb->onError(ec.first, ec.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800161 return;
162 }
163
164 if (FingerprintHalProperties::lockout().value_or(false)) {
165 LOG(ERROR) << "Fail: lockout";
166 cb->onLockoutPermanent();
167 cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */);
168 return;
169 }
170
171 if (shouldCancel(cancel)) {
172 LOG(ERROR) << "Fail: cancel";
173 cb->onError(Error::CANCELED, 0 /* vendorCode */);
174 return;
175 }
176
Jeff Pu343ca942022-09-14 15:56:30 -0400177 if (i < N) {
178 auto ac = convertAcquiredInfo(acquiredInfos[i]);
179 cb->onAcquired(ac.first, ac.second);
180 i++;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800181 }
182
Jeff Pu343ca942022-09-14 15:56:30 -0400183 SLEEP_MS(duration / N);
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000184 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800185
Jeff Pu343ca942022-09-14 15:56:30 -0400186 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
187 auto enrolls = FingerprintHalProperties::enrollments();
188 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
189 if (id > 0 && isEnrolled) {
190 cb->onAuthenticationSucceeded(id, {} /* hat */);
Jeff Pu52653182022-10-12 16:27:23 -0400191 mLockoutTracker.reset();
Jeff Pu343ca942022-09-14 15:56:30 -0400192 return;
193 } else {
194 LOG(ERROR) << "Fail: fingerprint not enrolled";
195 cb->onAuthenticationFailed();
Jeff Pu52653182022-10-12 16:27:23 -0400196 mLockoutTracker.addFailedAttempt();
Jeff Pu343ca942022-09-14 15:56:30 -0400197 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800198}
199
200void FakeFingerprintEngine::detectInteractionImpl(ISessionCallback* cb,
201 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400202 BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800203
Jeff Pu343ca942022-09-14 15:56:30 -0400204 int64_t duration =
205 FingerprintHalProperties::operation_detect_interaction_duration().value_or(10);
Jeff Pu52653182022-10-12 16:27:23 -0400206
207 auto detectInteractionSupported =
208 FingerprintHalProperties::detect_interaction().value_or(false);
209 if (!detectInteractionSupported) {
210 LOG(ERROR) << "Detect interaction is not supported";
211 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
212 return;
213 }
214
Jeff Pu343ca942022-09-14 15:56:30 -0400215 auto acquired = FingerprintHalProperties::operation_detect_interaction_acquired().value_or("1");
216 auto acquiredInfos = parseIntSequence(acquired);
217 int N = acquiredInfos.size();
218 int64_t now = Util::getSystemNanoTime();
219
220 if (N == 0) {
221 LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired;
222 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800223 return;
224 }
225
Jeff Pu343ca942022-09-14 15:56:30 -0400226 int i = 0;
227 do {
228 auto err = FingerprintHalProperties::operation_detect_interaction_error().value_or(0);
229 if (err != 0) {
230 LOG(ERROR) << "Fail: operation_detect_interaction_error";
231 auto ec = convertError(err);
232 cb->onError(ec.first, ec.second);
233 return;
234 }
235
236 if (shouldCancel(cancel)) {
237 LOG(ERROR) << "Fail: cancel";
238 cb->onError(Error::CANCELED, 0 /* vendorCode */);
239 return;
240 }
241
242 if (i < N) {
243 auto ac = convertAcquiredInfo(acquiredInfos[i]);
244 cb->onAcquired(ac.first, ac.second);
245 i++;
246 }
247 SLEEP_MS(duration / N);
248 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800249
250 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 LOG(ERROR) << "Fail: not enrolled";
255 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
256 return;
257 }
258
259 cb->onInteractionDetected();
260}
261
262void FakeFingerprintEngine::enumerateEnrollmentsImpl(ISessionCallback* cb) {
263 BEGIN_OP(0);
264
265 std::vector<int32_t> ids;
Jeff Pu63f33c72022-07-28 16:06:23 -0400266 // There are some enrollment sync issue with framework, which results in
267 // a single template removal during the very firt sync command after reboot.
268 // This is a workaround for now. TODO(b/243129174)
269 ids.push_back(-1);
270
Joe Bolingerde94aa02021-12-09 17:00:32 -0800271 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
272 auto id = enrollment.value_or(0);
273 if (id > 0) {
274 ids.push_back(id);
275 }
276 }
277
278 cb->onEnrollmentsEnumerated(ids);
279}
280
281void FakeFingerprintEngine::removeEnrollmentsImpl(ISessionCallback* cb,
282 const std::vector<int32_t>& enrollmentIds) {
283 BEGIN_OP(0);
284
285 std::vector<std::optional<int32_t>> newEnrollments;
286 std::vector<int32_t> removed;
287 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
288 auto id = enrollment.value_or(0);
289 if (std::find(enrollmentIds.begin(), enrollmentIds.end(), id) != enrollmentIds.end()) {
290 removed.push_back(id);
291 } else if (id > 0) {
292 newEnrollments.emplace_back(id);
293 }
294 }
295 FingerprintHalProperties::enrollments(newEnrollments);
296
297 cb->onEnrollmentsRemoved(enrollmentIds);
298}
299
300void FakeFingerprintEngine::getAuthenticatorIdImpl(ISessionCallback* cb) {
301 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400302 int64_t authenticatorId;
303 if (FingerprintHalProperties::enrollments().size() == 0) {
304 authenticatorId = 0;
305 } else {
306 authenticatorId = FingerprintHalProperties::authenticator_id().value_or(0);
307 if (authenticatorId == 0) authenticatorId = 1;
Jeff Pu63f33c72022-07-28 16:06:23 -0400308 }
309 cb->onAuthenticatorIdRetrieved(authenticatorId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800310}
311
312void FakeFingerprintEngine::invalidateAuthenticatorIdImpl(ISessionCallback* cb) {
313 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400314 int64_t newId;
315 if (FingerprintHalProperties::enrollments().size() == 0) {
316 newId = 0;
317 } else {
318 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
319 newId = id + 1;
320 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800321 FingerprintHalProperties::authenticator_id(newId);
322 cb->onAuthenticatorIdInvalidated(newId);
323}
324
325void FakeFingerprintEngine::resetLockoutImpl(ISessionCallback* cb,
Jeff Pu343ca942022-09-14 15:56:30 -0400326 const keymaster::HardwareAuthToken& hat) {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800327 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400328 if (hat.mac.empty()) {
329 LOG(ERROR) << "Fail: hat in resetLockout()";
330 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
331 return;
332 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800333 FingerprintHalProperties::lockout(false);
334 cb->onLockoutCleared();
Jeff Pu52653182022-10-12 16:27:23 -0400335 mLockoutTracker.reset();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800336}
337
Jeff Pu63f33c72022-07-28 16:06:23 -0400338ndk::ScopedAStatus FakeFingerprintEngine::onPointerDownImpl(int32_t /*pointerId*/, int32_t /*x*/,
339 int32_t /*y*/, float /*minor*/,
340 float /*major*/) {
341 BEGIN_OP(0);
342 return ndk::ScopedAStatus::ok();
343}
344
345ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) {
346 BEGIN_OP(0);
347 return ndk::ScopedAStatus::ok();
348}
349
350ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() {
351 BEGIN_OP(0);
352 return ndk::ScopedAStatus::ok();
353}
354
355bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) {
356 auto loc = FingerprintHalProperties::sensor_location().value_or("");
357 auto isValidStr = false;
358 auto dim = Util::split(loc, ":");
359
360 if (dim.size() < 3 or dim.size() > 4) {
361 if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc;
362 return false;
363 } else {
364 int32_t x, y, r;
365 std::string d = "";
366 if (dim.size() >= 3) {
367 isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r);
368 }
369 if (dim.size() >= 4) {
370 d = dim[3];
371 }
372 if (isValidStr) out = {0, x, y, r, d};
373
374 return isValidStr;
375 }
376}
377SensorLocation FakeFingerprintEngine::getSensorLocation() {
378 SensorLocation location;
379
380 if (getSensorLocationConfig(location)) {
381 return location;
382 } else {
383 return defaultSensorLocation();
384 }
385}
386
387SensorLocation FakeFingerprintEngine::defaultSensorLocation() {
388 return {0 /* displayId (not used) */, 0 /* sensorLocationX */, 0 /* sensorLocationY */,
389 0 /* sensorRadius */, "" /* display */};
390}
Jeff Pu343ca942022-09-14 15:56:30 -0400391
392std::vector<int32_t> FakeFingerprintEngine::parseIntSequence(const std::string& str,
393 const std::string& sep) {
394 std::vector<std::string> seqs = Util::split(str, sep);
395 std::vector<int32_t> res;
396
397 for (const auto& seq : seqs) {
398 int32_t val;
399 if (ParseInt(seq, &val)) {
400 res.push_back(val);
401 } else {
402 LOG(WARNING) << "Invalid int sequence:" + str;
403 res.clear();
404 break;
405 }
406 }
407
408 return res;
409}
410
Jeff Pu52653182022-10-12 16:27:23 -0400411bool FakeFingerprintEngine::parseEnrollmentCaptureSingle(const std::string& str,
412 std::vector<std::vector<int32_t>>& res) {
Jeff Pu343ca942022-09-14 15:56:30 -0400413 std::vector<int32_t> defaultAcquiredInfo = {(int32_t)AcquiredInfo::GOOD};
Jeff Pu343ca942022-09-14 15:56:30 -0400414 bool aborted = true;
415
Jeff Pu52653182022-10-12 16:27:23 -0400416 do {
417 std::smatch sms;
418 // Parses strings like "1000-[5,1]" or "500"
419 std::regex ex("((\\d+)(-\\[([\\d|,]+)\\])?)");
420 if (!regex_match(str.cbegin(), str.cend(), sms, ex)) break;
421 int32_t duration;
422 if (!ParseInt(sms.str(2), &duration)) break;
423 res.push_back({duration});
424 if (!sms.str(4).empty()) {
425 auto acqv = parseIntSequence(sms.str(4));
426 if (acqv.empty()) break;
427 res.push_back(acqv);
Jeff Pu343ca942022-09-14 15:56:30 -0400428 } else
429 res.push_back(defaultAcquiredInfo);
Jeff Pu52653182022-10-12 16:27:23 -0400430 aborted = false;
431 } while (0);
Jeff Pu343ca942022-09-14 15:56:30 -0400432
Jeff Pu52653182022-10-12 16:27:23 -0400433 return !aborted;
434}
435
436std::vector<std::vector<int32_t>> FakeFingerprintEngine::parseEnrollmentCapture(
437 const std::string& str) {
438 std::vector<std::vector<int32_t>> res;
439
440 std::string s(str);
441 s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());
442 bool aborted = false;
443 std::smatch sms;
444 // Parses strings like "1000-[5,1],500,800-[6,5,1]"
445 // ---------- --- -----------
446 // into parts: A B C
447 while (regex_search(s, sms, std::regex("^(,)?(\\d+(-\\[[\\d|,]+\\])?)"))) {
448 if (!parseEnrollmentCaptureSingle(sms.str(2), res)) {
449 aborted = true;
450 break;
451 }
452 s = sms.suffix();
Jeff Pu343ca942022-09-14 15:56:30 -0400453 }
Jeff Pu52653182022-10-12 16:27:23 -0400454 if (aborted || s.length() != 0) {
Jeff Pu343ca942022-09-14 15:56:30 -0400455 res.clear();
Jeff Pu52653182022-10-12 16:27:23 -0400456 LOG(ERROR) << "Failed to parse enrollment captures:" + str;
Jeff Pu343ca942022-09-14 15:56:30 -0400457 }
458
459 return res;
460}
461
462std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) {
463 std::pair<AcquiredInfo, int32_t> res;
464 if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) {
465 res.first = AcquiredInfo::VENDOR;
466 res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE;
467 } else {
468 res.first = (AcquiredInfo)code;
469 res.second = 0;
470 }
471 return res;
472}
473
474std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) {
475 std::pair<Error, int32_t> res;
476 if (code > FINGERPRINT_ERROR_VENDOR_BASE) {
477 res.first = Error::VENDOR;
478 res.second = code - FINGERPRINT_ERROR_VENDOR_BASE;
479 } else {
480 res.first = (Error)code;
481 res.second = 0;
482 }
483 return res;
484}
485
Jeff Pu52653182022-10-12 16:27:23 -0400486int32_t FakeFingerprintEngine::getLatency(
487 const std::vector<std::optional<std::int32_t>>& latencyIn) {
488 int32_t res = DEFAULT_LATENCY;
489
490 std::vector<int32_t> latency;
491 for (auto x : latencyIn)
492 if (x.has_value()) latency.push_back(*x);
493
494 switch (latency.size()) {
495 case 0:
496 break;
497 case 1:
498 res = latency[0];
499 break;
500 case 2:
501 res = getRandomInRange(latency[0], latency[1]);
502 break;
503 default:
504 LOG(ERROR) << "ERROR: unexpected input of size " << latency.size();
505 break;
506 }
507
508 return res;
509}
510
511int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) {
512 std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2));
513 return dist(mRandom);
514}
515
Joe Bolingerde94aa02021-12-09 17:00:32 -0800516} // namespace aidl::android::hardware::biometrics::fingerprint