Implement the state machine

Bug: 166800618
Bug: 175070939
Test: atest VtsHalBiometricsFingerprintTargetTest
Change-Id: I3a908b0f910323d643b220e560e9c2d8e4c5675a
diff --git a/biometrics/fingerprint/aidl/default/include/CancellationSignal.h b/biometrics/fingerprint/aidl/default/include/CancellationSignal.h
new file mode 100644
index 0000000..99f2fba
--- /dev/null
+++ b/biometrics/fingerprint/aidl/default/include/CancellationSignal.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <aidl/android/hardware/biometrics/common/BnCancellationSignal.h>
+#include <aidl/android/hardware/biometrics/fingerprint/ISessionCallback.h>
+#include <functional>
+#include <future>
+
+#include "WorkerThread.h"
+
+namespace aidl::android::hardware::biometrics::fingerprint {
+
+class CancellationSignal : public common::BnCancellationSignal {
+  public:
+    explicit CancellationSignal(std::promise<void>&& cancellationPromise);
+
+    ndk::ScopedAStatus cancel() override;
+
+  private:
+    std::promise<void> mCancellationPromise;
+};
+
+// Returns whether the given cancellation future is ready, i.e. whether the operation corresponding
+// to this future should be cancelled.
+bool shouldCancel(const std::future<void>& cancellationFuture);
+
+}  // namespace aidl::android::hardware::biometrics::fingerprint
diff --git a/biometrics/fingerprint/aidl/default/include/FakeFingerprintEngine.h b/biometrics/fingerprint/aidl/default/include/FakeFingerprintEngine.h
new file mode 100644
index 0000000..9343316
--- /dev/null
+++ b/biometrics/fingerprint/aidl/default/include/FakeFingerprintEngine.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <android-base/logging.h>
+
+namespace aidl::android::hardware::biometrics::fingerprint {
+
+class FakeFingerprintEngine {
+  public:
+    void generateChallengeImpl(ISessionCallback* cb, int32_t /*timeoutSec*/) {
+        LOG(INFO) << "generateChallengeImpl";
+        cb->onChallengeGenerated(0 /* challenge */);
+    }
+
+    void revokeChallengeImpl(ISessionCallback* cb, int64_t challenge) {
+        LOG(INFO) << "revokeChallengeImpl";
+        cb->onChallengeRevoked(challenge);
+    }
+
+    void enrollImpl(ISessionCallback* cb, const keymaster::HardwareAuthToken& /*hat*/) {
+        LOG(INFO) << "enrollImpl";
+        cb->onEnrollmentProgress(0 /* enrollmentId */, 0 /* remaining */);
+    }
+
+    void authenticateImpl(ISessionCallback* cb, int64_t /*operationId*/) {
+        LOG(INFO) << "authenticateImpl";
+        cb->onAuthenticationSucceeded(0 /* enrollmentId */, {} /* hat */);
+    }
+
+    void detectInteractionImpl(ISessionCallback* cb) {
+        LOG(INFO) << "detectInteractionImpl";
+        cb->onInteractionDetected();
+    }
+
+    void enumerateEnrollmentsImpl(ISessionCallback* cb) {
+        LOG(INFO) << "enumerateEnrollmentsImpl";
+        cb->onEnrollmentsEnumerated({} /* enrollmentIds */);
+    }
+
+    void removeEnrollmentsImpl(ISessionCallback* cb, const std::vector<int32_t>& enrollmentIds) {
+        LOG(INFO) << "removeEnrollmentsImpl";
+        cb->onEnrollmentsRemoved(enrollmentIds);
+    }
+
+    void getAuthenticatorIdImpl(ISessionCallback* cb) {
+        LOG(INFO) << "getAuthenticatorIdImpl";
+        cb->onAuthenticatorIdRetrieved(0 /* authenticatorId */);
+    }
+
+    void invalidateAuthenticatorIdImpl(ISessionCallback* cb) {
+        LOG(INFO) << "invalidateAuthenticatorIdImpl";
+        cb->onAuthenticatorIdInvalidated(0 /* newAuthenticatorId */);
+    }
+
+    void resetLockoutImpl(ISessionCallback* cb, const keymaster::HardwareAuthToken& /*hat*/) {
+        LOG(INFO) << "resetLockoutImpl";
+        cb->onLockoutCleared();
+    }
+};
+
+}  // namespace aidl::android::hardware::biometrics::fingerprint
\ No newline at end of file
diff --git a/biometrics/fingerprint/aidl/default/include/Fingerprint.h b/biometrics/fingerprint/aidl/default/include/Fingerprint.h
index ce1366c..9b8eef8 100644
--- a/biometrics/fingerprint/aidl/default/include/Fingerprint.h
+++ b/biometrics/fingerprint/aidl/default/include/Fingerprint.h
@@ -18,9 +18,13 @@
 
 #include <aidl/android/hardware/biometrics/fingerprint/BnFingerprint.h>
 
+#include "FakeFingerprintEngine.h"
+#include "Session.h"
+#include "WorkerThread.h"
+
 namespace aidl::android::hardware::biometrics::fingerprint {
 
-class Fingerprint final : public BnFingerprint {
+class Fingerprint : public BnFingerprint {
   public:
     Fingerprint();
 
@@ -31,6 +35,11 @@
                                      std::shared_ptr<ISession>* out) override;
 
     ndk::ScopedAStatus reset() override;
+
+  private:
+    std::unique_ptr<FakeFingerprintEngine> mEngine;
+    WorkerThread mWorker;
+    std::weak_ptr<Session> mSession;
 };
 
 }  // namespace aidl::android::hardware::biometrics::fingerprint
diff --git a/biometrics/fingerprint/aidl/default/include/Session.h b/biometrics/fingerprint/aidl/default/include/Session.h
index 99d806b..adda831 100644
--- a/biometrics/fingerprint/aidl/default/include/Session.h
+++ b/biometrics/fingerprint/aidl/default/include/Session.h
@@ -19,6 +19,9 @@
 #include <aidl/android/hardware/biometrics/fingerprint/BnSession.h>
 #include <aidl/android/hardware/biometrics/fingerprint/ISessionCallback.h>
 
+#include "FakeFingerprintEngine.h"
+#include "WorkerThread.h"
+
 namespace aidl::android::hardware::biometrics::fingerprint {
 
 namespace common = aidl::android::hardware::biometrics::common;
@@ -26,7 +29,8 @@
 
 class Session : public BnSession {
   public:
-    explicit Session(std::shared_ptr<ISessionCallback> cb);
+    Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb,
+            FakeFingerprintEngine* engine, WorkerThread* worker);
 
     ndk::ScopedAStatus generateChallenge(int32_t cookie, int32_t timeoutSec) override;
 
@@ -35,7 +39,7 @@
     ndk::ScopedAStatus enroll(int32_t cookie, const keymaster::HardwareAuthToken& hat,
                               std::shared_ptr<common::ICancellationSignal>* out) override;
 
-    ndk::ScopedAStatus authenticate(int32_t cookie, int64_t keystoreOperationId,
+    ndk::ScopedAStatus authenticate(int32_t cookie, int64_t operationId,
                                     std::shared_ptr<common::ICancellationSignal>* out) override;
 
     ndk::ScopedAStatus detectInteraction(
@@ -62,8 +66,29 @@
 
     ndk::ScopedAStatus onUiReady() override;
 
+    bool isClosed();
+
   private:
+    // Crashes the HAL if it's not currently idling because that would be an invalid state machine
+    // transition. Otherwise, sets the scheduled state to the given state.
+    void scheduleStateOrCrash(SessionState state);
+
+    // Crashes the HAL if the provided state doesn't match the previously scheduled state.
+    // Otherwise, transitions into the provided state, clears the scheduled state, and notifies
+    // the client about the transition by calling ISessionCallback#onStateChanged.
+    void enterStateOrCrash(int cookie, SessionState state);
+
+    // Sets the current state to SessionState::IDLING and notifies the client about the transition
+    // by calling ISessionCallback#onStateChanged.
+    void enterIdling(int cookie);
+
+    int32_t mSensorId;
+    int32_t mUserId;
     std::shared_ptr<ISessionCallback> mCb;
+    FakeFingerprintEngine* mEngine;
+    WorkerThread* mWorker;
+    SessionState mScheduledState;
+    SessionState mCurrentState;
 };
 
 }  // namespace aidl::android::hardware::biometrics::fingerprint