Implement the state machine

Bug: 166800618
Bug: 175070939
Test: atest VtsHalBiometricsFingerprintTargetTest
Change-Id: I3a908b0f910323d643b220e560e9c2d8e4c5675a
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