blob: 5f5455af6139a56ae4d9386f01c1db7fdb7de621 [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?
Jeff Pu437516e2023-06-28 15:21:21 +0000198 if (checkSensorLockout(cb)) return;
Jeff Pu52653182022-10-12 16:27:23 -0400199
Jeff Pu343ca942022-09-14 15:56:30 -0400200 int i = 0;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800201 do {
202 if (FingerprintHalProperties::operation_authenticate_fails().value_or(false)) {
203 LOG(ERROR) << "Fail: operation_authenticate_fails";
Jeff Pu52653182022-10-12 16:27:23 -0400204 mLockoutTracker.addFailedAttempt();
Jeff Pu343ca942022-09-14 15:56:30 -0400205 cb->onAuthenticationFailed();
206 return;
207 }
208
209 auto err = FingerprintHalProperties::operation_authenticate_error().value_or(0);
210 if (err != 0) {
211 LOG(ERROR) << "Fail: operation_authenticate_error";
212 auto ec = convertError(err);
213 cb->onError(ec.first, ec.second);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800214 return;
215 }
216
217 if (FingerprintHalProperties::lockout().value_or(false)) {
218 LOG(ERROR) << "Fail: lockout";
219 cb->onLockoutPermanent();
220 cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */);
221 return;
222 }
223
224 if (shouldCancel(cancel)) {
225 LOG(ERROR) << "Fail: cancel";
226 cb->onError(Error::CANCELED, 0 /* vendorCode */);
227 return;
228 }
229
Jeff Pu343ca942022-09-14 15:56:30 -0400230 if (i < N) {
231 auto ac = convertAcquiredInfo(acquiredInfos[i]);
232 cb->onAcquired(ac.first, ac.second);
233 i++;
Joe Bolingerde94aa02021-12-09 17:00:32 -0800234 }
235
Jeff Pu343ca942022-09-14 15:56:30 -0400236 SLEEP_MS(duration / N);
Joshua McCloskeydb009a52022-05-10 05:18:20 +0000237 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800238
Jeff Pu343ca942022-09-14 15:56:30 -0400239 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
240 auto enrolls = FingerprintHalProperties::enrollments();
241 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
242 if (id > 0 && isEnrolled) {
243 cb->onAuthenticationSucceeded(id, {} /* hat */);
Jeff Pu52653182022-10-12 16:27:23 -0400244 mLockoutTracker.reset();
Jeff Pu343ca942022-09-14 15:56:30 -0400245 return;
246 } else {
247 LOG(ERROR) << "Fail: fingerprint not enrolled";
248 cb->onAuthenticationFailed();
Jeff Pu52653182022-10-12 16:27:23 -0400249 mLockoutTracker.addFailedAttempt();
Jeff Pu437516e2023-06-28 15:21:21 +0000250 checkSensorLockout(cb);
Jeff Pu343ca942022-09-14 15:56:30 -0400251 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800252}
253
Jeff Pudef5b042023-05-25 14:28:16 -0400254void FakeFingerprintEngine::onDetectInteractFingerDown(ISessionCallback* cb,
255 const std::future<void>& cancel) {
Jeff Pu52653182022-10-12 16:27:23 -0400256 BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency()));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800257
Jeff Pu343ca942022-09-14 15:56:30 -0400258 int64_t duration =
259 FingerprintHalProperties::operation_detect_interaction_duration().value_or(10);
Jeff Pu52653182022-10-12 16:27:23 -0400260
Jeff Pu343ca942022-09-14 15:56:30 -0400261 auto acquired = FingerprintHalProperties::operation_detect_interaction_acquired().value_or("1");
262 auto acquiredInfos = parseIntSequence(acquired);
263 int N = acquiredInfos.size();
264 int64_t now = Util::getSystemNanoTime();
265
266 if (N == 0) {
267 LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired;
268 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800269 return;
270 }
271
Jeff Pu343ca942022-09-14 15:56:30 -0400272 int i = 0;
273 do {
274 auto err = FingerprintHalProperties::operation_detect_interaction_error().value_or(0);
275 if (err != 0) {
276 LOG(ERROR) << "Fail: operation_detect_interaction_error";
277 auto ec = convertError(err);
278 cb->onError(ec.first, ec.second);
279 return;
280 }
281
282 if (shouldCancel(cancel)) {
283 LOG(ERROR) << "Fail: cancel";
284 cb->onError(Error::CANCELED, 0 /* vendorCode */);
285 return;
286 }
287
288 if (i < N) {
289 auto ac = convertAcquiredInfo(acquiredInfos[i]);
290 cb->onAcquired(ac.first, ac.second);
291 i++;
292 }
293 SLEEP_MS(duration / N);
294 } while (!Util::hasElapsed(now, duration));
Joe Bolingerde94aa02021-12-09 17:00:32 -0800295
296 auto id = FingerprintHalProperties::enrollment_hit().value_or(0);
297 auto enrolls = FingerprintHalProperties::enrollments();
298 auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end();
299 if (id <= 0 || !isEnrolled) {
300 LOG(ERROR) << "Fail: not enrolled";
301 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
302 return;
303 }
304
305 cb->onInteractionDetected();
306}
307
308void FakeFingerprintEngine::enumerateEnrollmentsImpl(ISessionCallback* cb) {
309 BEGIN_OP(0);
310
311 std::vector<int32_t> ids;
312 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
313 auto id = enrollment.value_or(0);
314 if (id > 0) {
315 ids.push_back(id);
316 }
317 }
318
319 cb->onEnrollmentsEnumerated(ids);
320}
321
322void FakeFingerprintEngine::removeEnrollmentsImpl(ISessionCallback* cb,
323 const std::vector<int32_t>& enrollmentIds) {
324 BEGIN_OP(0);
325
326 std::vector<std::optional<int32_t>> newEnrollments;
327 std::vector<int32_t> removed;
328 for (auto& enrollment : FingerprintHalProperties::enrollments()) {
329 auto id = enrollment.value_or(0);
330 if (std::find(enrollmentIds.begin(), enrollmentIds.end(), id) != enrollmentIds.end()) {
331 removed.push_back(id);
332 } else if (id > 0) {
333 newEnrollments.emplace_back(id);
334 }
335 }
336 FingerprintHalProperties::enrollments(newEnrollments);
337
338 cb->onEnrollmentsRemoved(enrollmentIds);
339}
340
341void FakeFingerprintEngine::getAuthenticatorIdImpl(ISessionCallback* cb) {
342 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400343 int64_t authenticatorId;
344 if (FingerprintHalProperties::enrollments().size() == 0) {
345 authenticatorId = 0;
346 } else {
347 authenticatorId = FingerprintHalProperties::authenticator_id().value_or(0);
348 if (authenticatorId == 0) authenticatorId = 1;
Jeff Pu63f33c72022-07-28 16:06:23 -0400349 }
350 cb->onAuthenticatorIdRetrieved(authenticatorId);
Joe Bolingerde94aa02021-12-09 17:00:32 -0800351}
352
353void FakeFingerprintEngine::invalidateAuthenticatorIdImpl(ISessionCallback* cb) {
354 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400355 int64_t newId;
356 if (FingerprintHalProperties::enrollments().size() == 0) {
357 newId = 0;
358 } else {
359 auto id = FingerprintHalProperties::authenticator_id().value_or(0);
360 newId = id + 1;
361 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800362 FingerprintHalProperties::authenticator_id(newId);
363 cb->onAuthenticatorIdInvalidated(newId);
364}
365
366void FakeFingerprintEngine::resetLockoutImpl(ISessionCallback* cb,
Jeff Pu343ca942022-09-14 15:56:30 -0400367 const keymaster::HardwareAuthToken& hat) {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800368 BEGIN_OP(0);
Jeff Pu343ca942022-09-14 15:56:30 -0400369 if (hat.mac.empty()) {
370 LOG(ERROR) << "Fail: hat in resetLockout()";
371 cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
372 return;
373 }
Joe Bolingerde94aa02021-12-09 17:00:32 -0800374 FingerprintHalProperties::lockout(false);
375 cb->onLockoutCleared();
Jeff Pu52653182022-10-12 16:27:23 -0400376 mLockoutTracker.reset();
Joe Bolingerde94aa02021-12-09 17:00:32 -0800377}
378
Jeff Pu63f33c72022-07-28 16:06:23 -0400379ndk::ScopedAStatus FakeFingerprintEngine::onPointerDownImpl(int32_t /*pointerId*/, int32_t /*x*/,
380 int32_t /*y*/, float /*minor*/,
381 float /*major*/) {
382 BEGIN_OP(0);
Jeff Pudef5b042023-05-25 14:28:16 -0400383 fingerDownAction();
Jeff Pu63f33c72022-07-28 16:06:23 -0400384 return ndk::ScopedAStatus::ok();
385}
386
387ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) {
388 BEGIN_OP(0);
389 return ndk::ScopedAStatus::ok();
390}
391
392ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() {
393 BEGIN_OP(0);
394 return ndk::ScopedAStatus::ok();
395}
396
397bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) {
398 auto loc = FingerprintHalProperties::sensor_location().value_or("");
399 auto isValidStr = false;
400 auto dim = Util::split(loc, ":");
401
402 if (dim.size() < 3 or dim.size() > 4) {
403 if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc;
404 return false;
405 } else {
406 int32_t x, y, r;
407 std::string d = "";
408 if (dim.size() >= 3) {
409 isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r);
410 }
411 if (dim.size() >= 4) {
412 d = dim[3];
413 }
Jeff Pudef5b042023-05-25 14:28:16 -0400414 if (isValidStr)
415 out = {.sensorLocationX = x, .sensorLocationY = y, .sensorRadius = r, .display = d};
Jeff Pu63f33c72022-07-28 16:06:23 -0400416
417 return isValidStr;
418 }
419}
420SensorLocation FakeFingerprintEngine::getSensorLocation() {
421 SensorLocation location;
422
423 if (getSensorLocationConfig(location)) {
424 return location;
425 } else {
426 return defaultSensorLocation();
427 }
428}
429
430SensorLocation FakeFingerprintEngine::defaultSensorLocation() {
Jeff Pudef5b042023-05-25 14:28:16 -0400431 return SensorLocation();
Jeff Pu63f33c72022-07-28 16:06:23 -0400432}
Jeff Pu343ca942022-09-14 15:56:30 -0400433
434std::vector<int32_t> FakeFingerprintEngine::parseIntSequence(const std::string& str,
435 const std::string& sep) {
436 std::vector<std::string> seqs = Util::split(str, sep);
437 std::vector<int32_t> res;
438
439 for (const auto& seq : seqs) {
440 int32_t val;
441 if (ParseInt(seq, &val)) {
442 res.push_back(val);
443 } else {
444 LOG(WARNING) << "Invalid int sequence:" + str;
445 res.clear();
446 break;
447 }
448 }
449
450 return res;
451}
452
Jeff Pu52653182022-10-12 16:27:23 -0400453bool FakeFingerprintEngine::parseEnrollmentCaptureSingle(const std::string& str,
454 std::vector<std::vector<int32_t>>& res) {
Jeff Pu343ca942022-09-14 15:56:30 -0400455 std::vector<int32_t> defaultAcquiredInfo = {(int32_t)AcquiredInfo::GOOD};
Jeff Pu343ca942022-09-14 15:56:30 -0400456 bool aborted = true;
457
Jeff Pu52653182022-10-12 16:27:23 -0400458 do {
459 std::smatch sms;
460 // Parses strings like "1000-[5,1]" or "500"
461 std::regex ex("((\\d+)(-\\[([\\d|,]+)\\])?)");
462 if (!regex_match(str.cbegin(), str.cend(), sms, ex)) break;
463 int32_t duration;
464 if (!ParseInt(sms.str(2), &duration)) break;
465 res.push_back({duration});
466 if (!sms.str(4).empty()) {
467 auto acqv = parseIntSequence(sms.str(4));
468 if (acqv.empty()) break;
469 res.push_back(acqv);
Jeff Pu343ca942022-09-14 15:56:30 -0400470 } else
471 res.push_back(defaultAcquiredInfo);
Jeff Pu52653182022-10-12 16:27:23 -0400472 aborted = false;
473 } while (0);
Jeff Pu343ca942022-09-14 15:56:30 -0400474
Jeff Pu52653182022-10-12 16:27:23 -0400475 return !aborted;
476}
477
478std::vector<std::vector<int32_t>> FakeFingerprintEngine::parseEnrollmentCapture(
479 const std::string& str) {
480 std::vector<std::vector<int32_t>> res;
481
482 std::string s(str);
483 s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());
484 bool aborted = false;
485 std::smatch sms;
486 // Parses strings like "1000-[5,1],500,800-[6,5,1]"
487 // ---------- --- -----------
488 // into parts: A B C
489 while (regex_search(s, sms, std::regex("^(,)?(\\d+(-\\[[\\d|,]+\\])?)"))) {
490 if (!parseEnrollmentCaptureSingle(sms.str(2), res)) {
491 aborted = true;
492 break;
493 }
494 s = sms.suffix();
Jeff Pu343ca942022-09-14 15:56:30 -0400495 }
Jeff Pu52653182022-10-12 16:27:23 -0400496 if (aborted || s.length() != 0) {
Jeff Pu343ca942022-09-14 15:56:30 -0400497 res.clear();
Jeff Pu52653182022-10-12 16:27:23 -0400498 LOG(ERROR) << "Failed to parse enrollment captures:" + str;
Jeff Pu343ca942022-09-14 15:56:30 -0400499 }
500
501 return res;
502}
503
504std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) {
505 std::pair<AcquiredInfo, int32_t> res;
506 if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) {
507 res.first = AcquiredInfo::VENDOR;
508 res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE;
509 } else {
510 res.first = (AcquiredInfo)code;
511 res.second = 0;
512 }
513 return res;
514}
515
516std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) {
517 std::pair<Error, int32_t> res;
518 if (code > FINGERPRINT_ERROR_VENDOR_BASE) {
519 res.first = Error::VENDOR;
520 res.second = code - FINGERPRINT_ERROR_VENDOR_BASE;
521 } else {
522 res.first = (Error)code;
523 res.second = 0;
524 }
525 return res;
526}
527
Jeff Pu52653182022-10-12 16:27:23 -0400528int32_t FakeFingerprintEngine::getLatency(
529 const std::vector<std::optional<std::int32_t>>& latencyIn) {
530 int32_t res = DEFAULT_LATENCY;
531
532 std::vector<int32_t> latency;
533 for (auto x : latencyIn)
534 if (x.has_value()) latency.push_back(*x);
535
536 switch (latency.size()) {
537 case 0:
538 break;
539 case 1:
540 res = latency[0];
541 break;
542 case 2:
543 res = getRandomInRange(latency[0], latency[1]);
544 break;
545 default:
546 LOG(ERROR) << "ERROR: unexpected input of size " << latency.size();
547 break;
548 }
549
550 return res;
551}
552
553int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) {
554 std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2));
555 return dist(mRandom);
556}
557
Jeff Pu437516e2023-06-28 15:21:21 +0000558bool FakeFingerprintEngine::checkSensorLockout(ISessionCallback* cb) {
559 FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode();
560 if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) {
561 LOG(ERROR) << "Fail: lockout permanent";
562 cb->onLockoutPermanent();
563 return true;
564 } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) {
565 int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft();
566 LOG(ERROR) << "Fail: lockout timed " << timeLeft;
567 cb->onLockoutTimed(timeLeft);
568 return true;
569 }
570 return false;
571}
Joe Bolingerde94aa02021-12-09 17:00:32 -0800572} // namespace aidl::android::hardware::biometrics::fingerprint