Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 1 | /* |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 2 | * Copyright (C) 2022 The Android Open Source Project |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 3 | * |
| 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 Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 18 | #include <regex> |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 19 | #include "Fingerprint.h" |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 20 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 22 | #include <android-base/parseint.h> |
Joshua McCloskey | c8c0bad | 2022-05-10 05:17:44 +0000 | [diff] [blame] | 23 | |
| 24 | #include <fingerprint.sysprop.h> |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 25 | |
Joshua McCloskey | c8c0bad | 2022-05-10 05:17:44 +0000 | [diff] [blame] | 26 | #include "util/CancellationSignal.h" |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 27 | #include "util/Util.h" |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 28 | |
| 29 | using namespace ::android::fingerprint::virt; |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 30 | using ::android::base::ParseInt; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 31 | |
| 32 | namespace aidl::android::hardware::biometrics::fingerprint { |
| 33 | |
| 34 | void 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 | |
| 42 | void FakeFingerprintEngine::revokeChallengeImpl(ISessionCallback* cb, int64_t challenge) { |
| 43 | BEGIN_OP(0); |
| 44 | FingerprintHalProperties::challenge({}); |
| 45 | cb->onChallengeRevoked(challenge); |
| 46 | } |
| 47 | |
| 48 | void FakeFingerprintEngine::enrollImpl(ISessionCallback* cb, |
| 49 | const keymaster::HardwareAuthToken& hat, |
| 50 | const std::future<void>& cancel) { |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 51 | BEGIN_OP(getLatency(FingerprintHalProperties::operation_enroll_latency())); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 52 | |
| 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 Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 60 | // 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 69 | // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result> |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 70 | auto nextEnroll = FingerprintHalProperties::next_enrollment().value_or(""); |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 71 | auto parts = Util::split(nextEnroll, ":"); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 72 | if (parts.size() != 3) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 73 | LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 74 | cb->onError(Error::VENDOR, 0 /* vendorError */); |
| 75 | return; |
| 76 | } |
| 77 | auto enrollmentId = std::stoi(parts[0]); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 78 | 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 84 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 85 | 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 97 | 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 Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 102 | LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 103 | if (left == 0) { |
| 104 | auto enrollments = FingerprintHalProperties::enrollments(); |
| 105 | enrollments.emplace_back(enrollmentId); |
| 106 | FingerprintHalProperties::enrollments(enrollments); |
| 107 | FingerprintHalProperties::next_enrollment({}); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 108 | // 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 112 | LOG(INFO) << "Enrolled: " << enrollmentId; |
| 113 | } |
| 114 | cb->onEnrollmentProgress(enrollmentId, left); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void FakeFingerprintEngine::authenticateImpl(ISessionCallback* cb, int64_t /* operationId */, |
| 120 | const std::future<void>& cancel) { |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 121 | BEGIN_OP(getLatency(FingerprintHalProperties::operation_authenticate_latency())); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 122 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 123 | 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 Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 135 | // 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 Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 147 | int i = 0; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 148 | do { |
| 149 | if (FingerprintHalProperties::operation_authenticate_fails().value_or(false)) { |
| 150 | LOG(ERROR) << "Fail: operation_authenticate_fails"; |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 151 | mLockoutTracker.addFailedAttempt(); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 152 | 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 161 | 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 Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 177 | if (i < N) { |
| 178 | auto ac = convertAcquiredInfo(acquiredInfos[i]); |
| 179 | cb->onAcquired(ac.first, ac.second); |
| 180 | i++; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 183 | SLEEP_MS(duration / N); |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 184 | } while (!Util::hasElapsed(now, duration)); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 185 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 186 | 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 Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 191 | mLockoutTracker.reset(); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 192 | return; |
| 193 | } else { |
| 194 | LOG(ERROR) << "Fail: fingerprint not enrolled"; |
| 195 | cb->onAuthenticationFailed(); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 196 | mLockoutTracker.addFailedAttempt(); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 197 | } |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void FakeFingerprintEngine::detectInteractionImpl(ISessionCallback* cb, |
| 201 | const std::future<void>& cancel) { |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 202 | BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency())); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 203 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 204 | int64_t duration = |
| 205 | FingerprintHalProperties::operation_detect_interaction_duration().value_or(10); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 206 | |
| 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 Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 215 | 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 226 | 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 249 | |
| 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 | |
| 262 | void FakeFingerprintEngine::enumerateEnrollmentsImpl(ISessionCallback* cb) { |
| 263 | BEGIN_OP(0); |
| 264 | |
| 265 | std::vector<int32_t> ids; |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 266 | // 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 271 | 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 | |
| 281 | void 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 | |
| 300 | void FakeFingerprintEngine::getAuthenticatorIdImpl(ISessionCallback* cb) { |
| 301 | BEGIN_OP(0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 302 | 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 Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 308 | } |
| 309 | cb->onAuthenticatorIdRetrieved(authenticatorId); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void FakeFingerprintEngine::invalidateAuthenticatorIdImpl(ISessionCallback* cb) { |
| 313 | BEGIN_OP(0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 314 | 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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 321 | FingerprintHalProperties::authenticator_id(newId); |
| 322 | cb->onAuthenticatorIdInvalidated(newId); |
| 323 | } |
| 324 | |
| 325 | void FakeFingerprintEngine::resetLockoutImpl(ISessionCallback* cb, |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 326 | const keymaster::HardwareAuthToken& hat) { |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 327 | BEGIN_OP(0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 328 | if (hat.mac.empty()) { |
| 329 | LOG(ERROR) << "Fail: hat in resetLockout()"; |
| 330 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
| 331 | return; |
| 332 | } |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 333 | FingerprintHalProperties::lockout(false); |
| 334 | cb->onLockoutCleared(); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 335 | mLockoutTracker.reset(); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 338 | ndk::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 | |
| 345 | ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) { |
| 346 | BEGIN_OP(0); |
| 347 | return ndk::ScopedAStatus::ok(); |
| 348 | } |
| 349 | |
| 350 | ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() { |
| 351 | BEGIN_OP(0); |
| 352 | return ndk::ScopedAStatus::ok(); |
| 353 | } |
| 354 | |
| 355 | bool 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 | } |
| 377 | SensorLocation FakeFingerprintEngine::getSensorLocation() { |
| 378 | SensorLocation location; |
| 379 | |
| 380 | if (getSensorLocationConfig(location)) { |
| 381 | return location; |
| 382 | } else { |
| 383 | return defaultSensorLocation(); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | SensorLocation FakeFingerprintEngine::defaultSensorLocation() { |
| 388 | return {0 /* displayId (not used) */, 0 /* sensorLocationX */, 0 /* sensorLocationY */, |
| 389 | 0 /* sensorRadius */, "" /* display */}; |
| 390 | } |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 391 | |
| 392 | std::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 Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 411 | bool FakeFingerprintEngine::parseEnrollmentCaptureSingle(const std::string& str, |
| 412 | std::vector<std::vector<int32_t>>& res) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 413 | std::vector<int32_t> defaultAcquiredInfo = {(int32_t)AcquiredInfo::GOOD}; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 414 | bool aborted = true; |
| 415 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 416 | 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 Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 428 | } else |
| 429 | res.push_back(defaultAcquiredInfo); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 430 | aborted = false; |
| 431 | } while (0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 432 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 433 | return !aborted; |
| 434 | } |
| 435 | |
| 436 | std::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 Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 453 | } |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 454 | if (aborted || s.length() != 0) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 455 | res.clear(); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 456 | LOG(ERROR) << "Failed to parse enrollment captures:" + str; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | return res; |
| 460 | } |
| 461 | |
| 462 | std::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 | |
| 474 | std::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 Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 486 | int32_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 | |
| 511 | int32_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 Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 516 | } // namespace aidl::android::hardware::biometrics::fingerprint |