Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 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 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 17 | #include "Session.h" |
| 18 | |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 20 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 21 | #include "CancellationSignal.h" |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 22 | |
| 23 | namespace aidl::android::hardware::biometrics::fingerprint { |
| 24 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 25 | Session::Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb, |
| 26 | FakeFingerprintEngine* engine, WorkerThread* worker) |
| 27 | : mSensorId(sensorId), |
| 28 | mUserId(userId), |
| 29 | mCb(std::move(cb)), |
| 30 | mEngine(engine), |
| 31 | mWorker(worker), |
| 32 | mScheduledState(SessionState::IDLING), |
| 33 | mCurrentState(SessionState::IDLING) { |
| 34 | CHECK_GE(mSensorId, 0); |
| 35 | CHECK_GE(mUserId, 0); |
| 36 | CHECK(mEngine); |
| 37 | CHECK(mWorker); |
| 38 | CHECK(mCb); |
| 39 | } |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 40 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 41 | void Session::scheduleStateOrCrash(SessionState state) { |
| 42 | CHECK(mScheduledState == SessionState::IDLING); |
| 43 | CHECK(mCurrentState == SessionState::IDLING); |
| 44 | mScheduledState = state; |
| 45 | } |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 46 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 47 | void Session::enterStateOrCrash(int cookie, SessionState state) { |
| 48 | CHECK(mScheduledState == state); |
| 49 | mCurrentState = mScheduledState; |
| 50 | mScheduledState = SessionState::IDLING; |
| 51 | mCb->onStateChanged(cookie, mCurrentState); |
| 52 | } |
| 53 | |
| 54 | void Session::enterIdling(int cookie) { |
| 55 | mCurrentState = SessionState::IDLING; |
| 56 | mCb->onStateChanged(cookie, mCurrentState); |
| 57 | } |
| 58 | |
| 59 | bool Session::isClosed() { |
| 60 | return mCurrentState == SessionState::CLOSED; |
| 61 | } |
| 62 | |
| 63 | ndk::ScopedAStatus Session::generateChallenge(int32_t cookie, int32_t timeoutSec) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 64 | LOG(INFO) << "generateChallenge"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 65 | scheduleStateOrCrash(SessionState::GENERATING_CHALLENGE); |
| 66 | |
| 67 | mWorker->schedule(Callable::from([this, cookie, timeoutSec] { |
| 68 | enterStateOrCrash(cookie, SessionState::GENERATING_CHALLENGE); |
| 69 | mEngine->generateChallengeImpl(mCb.get(), timeoutSec); |
| 70 | enterIdling(cookie); |
| 71 | })); |
| 72 | |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 73 | return ndk::ScopedAStatus::ok(); |
| 74 | } |
| 75 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 76 | ndk::ScopedAStatus Session::revokeChallenge(int32_t cookie, int64_t challenge) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 77 | LOG(INFO) << "revokeChallenge"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 78 | scheduleStateOrCrash(SessionState::REVOKING_CHALLENGE); |
| 79 | |
| 80 | mWorker->schedule(Callable::from([this, cookie, challenge] { |
| 81 | enterStateOrCrash(cookie, SessionState::REVOKING_CHALLENGE); |
| 82 | mEngine->revokeChallengeImpl(mCb.get(), challenge); |
| 83 | enterIdling(cookie); |
| 84 | })); |
| 85 | |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 86 | return ndk::ScopedAStatus::ok(); |
| 87 | } |
| 88 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 89 | ndk::ScopedAStatus Session::enroll(int32_t cookie, const keymaster::HardwareAuthToken& hat, |
| 90 | std::shared_ptr<common::ICancellationSignal>* out) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 91 | LOG(INFO) << "enroll"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 92 | scheduleStateOrCrash(SessionState::ENROLLING); |
| 93 | |
| 94 | std::promise<void> cancellationPromise; |
| 95 | auto cancFuture = cancellationPromise.get_future(); |
| 96 | |
| 97 | mWorker->schedule(Callable::from([this, cookie, hat, cancFuture = std::move(cancFuture)] { |
| 98 | enterStateOrCrash(cookie, SessionState::ENROLLING); |
| 99 | if (shouldCancel(cancFuture)) { |
| 100 | mCb->onError(Error::CANCELED, 0 /* vendorCode */); |
| 101 | } else { |
| 102 | mEngine->enrollImpl(mCb.get(), hat); |
| 103 | } |
| 104 | enterIdling(cookie); |
| 105 | })); |
| 106 | |
| 107 | *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise)); |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 108 | return ndk::ScopedAStatus::ok(); |
| 109 | } |
| 110 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 111 | ndk::ScopedAStatus Session::authenticate(int32_t cookie, int64_t operationId, |
Ilya Matyukhin | 124e70a | 2021-02-12 13:00:15 -0800 | [diff] [blame] | 112 | std::shared_ptr<common::ICancellationSignal>* out) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 113 | LOG(INFO) << "authenticate"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 114 | scheduleStateOrCrash(SessionState::AUTHENTICATING); |
| 115 | |
| 116 | std::promise<void> cancPromise; |
| 117 | auto cancFuture = cancPromise.get_future(); |
| 118 | |
| 119 | mWorker->schedule( |
| 120 | Callable::from([this, cookie, operationId, cancFuture = std::move(cancFuture)] { |
| 121 | enterStateOrCrash(cookie, SessionState::AUTHENTICATING); |
| 122 | if (shouldCancel(cancFuture)) { |
| 123 | mCb->onError(Error::CANCELED, 0 /* vendorCode */); |
| 124 | } else { |
| 125 | mEngine->authenticateImpl(mCb.get(), operationId); |
| 126 | } |
| 127 | enterIdling(cookie); |
| 128 | })); |
| 129 | |
| 130 | *out = SharedRefBase::make<CancellationSignal>(std::move(cancPromise)); |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 131 | return ndk::ScopedAStatus::ok(); |
| 132 | } |
| 133 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 134 | ndk::ScopedAStatus Session::detectInteraction(int32_t cookie, |
| 135 | std::shared_ptr<common::ICancellationSignal>* out) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 136 | LOG(INFO) << "detectInteraction"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 137 | scheduleStateOrCrash(SessionState::DETECTING_INTERACTION); |
| 138 | |
| 139 | std::promise<void> cancellationPromise; |
| 140 | auto cancFuture = cancellationPromise.get_future(); |
| 141 | |
| 142 | mWorker->schedule(Callable::from([this, cookie, cancFuture = std::move(cancFuture)] { |
| 143 | enterStateOrCrash(cookie, SessionState::DETECTING_INTERACTION); |
| 144 | if (shouldCancel(cancFuture)) { |
| 145 | mCb->onError(Error::CANCELED, 0 /* vendorCode */); |
| 146 | } else { |
| 147 | mEngine->detectInteractionImpl(mCb.get()); |
| 148 | } |
| 149 | enterIdling(cookie); |
| 150 | })); |
| 151 | |
| 152 | *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise)); |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 153 | return ndk::ScopedAStatus::ok(); |
| 154 | } |
| 155 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 156 | ndk::ScopedAStatus Session::enumerateEnrollments(int32_t cookie) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 157 | LOG(INFO) << "enumerateEnrollments"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 158 | scheduleStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS); |
| 159 | |
| 160 | mWorker->schedule(Callable::from([this, cookie] { |
| 161 | enterStateOrCrash(cookie, SessionState::ENUMERATING_ENROLLMENTS); |
| 162 | mEngine->enumerateEnrollmentsImpl(mCb.get()); |
| 163 | enterIdling(cookie); |
| 164 | })); |
| 165 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 166 | return ndk::ScopedAStatus::ok(); |
| 167 | } |
| 168 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 169 | ndk::ScopedAStatus Session::removeEnrollments(int32_t cookie, |
| 170 | const std::vector<int32_t>& enrollmentIds) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 171 | LOG(INFO) << "removeEnrollments"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 172 | scheduleStateOrCrash(SessionState::REMOVING_ENROLLMENTS); |
| 173 | |
| 174 | mWorker->schedule(Callable::from([this, cookie, enrollmentIds] { |
| 175 | enterStateOrCrash(cookie, SessionState::REMOVING_ENROLLMENTS); |
| 176 | mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds); |
| 177 | enterIdling(cookie); |
| 178 | })); |
| 179 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 180 | return ndk::ScopedAStatus::ok(); |
| 181 | } |
| 182 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 183 | ndk::ScopedAStatus Session::getAuthenticatorId(int32_t cookie) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 184 | LOG(INFO) << "getAuthenticatorId"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 185 | scheduleStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID); |
| 186 | |
| 187 | mWorker->schedule(Callable::from([this, cookie] { |
| 188 | enterStateOrCrash(cookie, SessionState::GETTING_AUTHENTICATOR_ID); |
| 189 | mEngine->getAuthenticatorIdImpl(mCb.get()); |
| 190 | enterIdling(cookie); |
| 191 | })); |
| 192 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 193 | return ndk::ScopedAStatus::ok(); |
| 194 | } |
| 195 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 196 | ndk::ScopedAStatus Session::invalidateAuthenticatorId(int32_t cookie) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 197 | LOG(INFO) << "invalidateAuthenticatorId"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 198 | scheduleStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID); |
| 199 | |
| 200 | mWorker->schedule(Callable::from([this, cookie] { |
| 201 | enterStateOrCrash(cookie, SessionState::INVALIDATING_AUTHENTICATOR_ID); |
| 202 | mEngine->invalidateAuthenticatorIdImpl(mCb.get()); |
| 203 | enterIdling(cookie); |
| 204 | })); |
| 205 | |
Kevin Chyn | 6e862c3 | 2020-09-16 18:27:37 -0700 | [diff] [blame] | 206 | return ndk::ScopedAStatus::ok(); |
| 207 | } |
| 208 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 209 | ndk::ScopedAStatus Session::resetLockout(int32_t cookie, const keymaster::HardwareAuthToken& hat) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 210 | LOG(INFO) << "resetLockout"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 211 | scheduleStateOrCrash(SessionState::RESETTING_LOCKOUT); |
| 212 | |
| 213 | mWorker->schedule(Callable::from([this, cookie, hat] { |
| 214 | enterStateOrCrash(cookie, SessionState::RESETTING_LOCKOUT); |
| 215 | mEngine->resetLockoutImpl(mCb.get(), hat); |
| 216 | enterIdling(cookie); |
| 217 | })); |
| 218 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 219 | return ndk::ScopedAStatus::ok(); |
| 220 | } |
| 221 | |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 222 | ndk::ScopedAStatus Session::close(int32_t cookie) { |
Ilya Matyukhin | 71005c5 | 2021-02-17 12:44:14 -0800 | [diff] [blame] | 223 | LOG(INFO) << "close"; |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 224 | CHECK(mCurrentState == SessionState::IDLING) << "Can't close a non-idling session. Crashing."; |
| 225 | mCurrentState = SessionState::CLOSED; |
| 226 | mCb->onStateChanged(cookie, mCurrentState); |
Ilya Matyukhin | 71005c5 | 2021-02-17 12:44:14 -0800 | [diff] [blame] | 227 | return ndk::ScopedAStatus::ok(); |
| 228 | } |
| 229 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 230 | ndk::ScopedAStatus Session::onPointerDown(int32_t /*pointerId*/, int32_t /*x*/, int32_t /*y*/, |
| 231 | float /*minor*/, float /*major*/) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 232 | LOG(INFO) << "onPointerDown"; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 233 | return ndk::ScopedAStatus::ok(); |
| 234 | } |
| 235 | |
| 236 | ndk::ScopedAStatus Session::onPointerUp(int32_t /*pointerId*/) { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 237 | LOG(INFO) << "onPointerUp"; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 238 | return ndk::ScopedAStatus::ok(); |
| 239 | } |
| 240 | |
| 241 | ndk::ScopedAStatus Session::onUiReady() { |
Kevin Chyn | 146f6c8 | 2021-02-10 11:32:35 -0800 | [diff] [blame] | 242 | LOG(INFO) << "onUiReady"; |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 243 | return ndk::ScopedAStatus::ok(); |
| 244 | } |
Ilya Matyukhin | 48ff896 | 2021-02-22 13:13:13 -0800 | [diff] [blame] | 245 | |
Ilya Matyukhin | a9a3c85 | 2020-08-18 03:09:41 -0700 | [diff] [blame] | 246 | } // namespace aidl::android::hardware::biometrics::fingerprint |