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 
17 #pragma once
18 
19 #include <random>
20 
21 #include <aidl/android/hardware/biometrics/face/BnSession.h>
22 #include <aidl/android/hardware/biometrics/face/FaceEnrollOptions.h>
23 #include <aidl/android/hardware/biometrics/face/ISessionCallback.h>
24 
25 #include "FakeFaceEngine.h"
26 #include "thread/WorkerThread.h"
27 #include "util/CancellationSignal.h"
28 
29 namespace aidl::android::hardware::biometrics::face {
30 
31 namespace common = aidl::android::hardware::biometrics::common;
32 namespace keymaster = aidl::android::hardware::keymaster;
33 
34 using aidl::android::hardware::common::NativeHandle;
35 
36 enum class SessionState {
37     IDLING,
38     CLOSED,
39 };
40 
41 class Session : public BnSession {
42   public:
43     explicit Session(std::unique_ptr<FakeFaceEngine> engine, std::shared_ptr<ISessionCallback> cb);
44 
45     ndk::ScopedAStatus generateChallenge() override;
46 
47     ndk::ScopedAStatus revokeChallenge(int64_t challenge) override;
48 
49     ndk::ScopedAStatus getEnrollmentConfig(EnrollmentType enrollmentType,
50                                            std::vector<EnrollmentStageConfig>* return_val) override;
51 
52     ndk::ScopedAStatus enroll(const keymaster::HardwareAuthToken& hat,
53                               EnrollmentType enrollmentType, const std::vector<Feature>& features,
54                               const std::optional<NativeHandle>& previewSurface,
55                               std::shared_ptr<common::ICancellationSignal>* return_val) override;
56 
57     ndk::ScopedAStatus authenticate(
58             int64_t keystoreOperationId,
59             std::shared_ptr<common::ICancellationSignal>* returnVal) override;
60 
61     ndk::ScopedAStatus detectInteraction(
62             std::shared_ptr<common::ICancellationSignal>* returnVal) override;
63 
64     ndk::ScopedAStatus enumerateEnrollments() override;
65 
66     ndk::ScopedAStatus removeEnrollments(const std::vector<int32_t>& enrollmentIds) override;
67 
68     ndk::ScopedAStatus getFeatures() override;
69 
70     ndk::ScopedAStatus setFeature(const keymaster::HardwareAuthToken& hat, Feature feature,
71                                   bool enabled) override;
72 
73     ndk::ScopedAStatus getAuthenticatorId() override;
74 
75     ndk::ScopedAStatus invalidateAuthenticatorId() override;
76 
77     ndk::ScopedAStatus resetLockout(const keymaster::HardwareAuthToken& hat) override;
78 
79     ndk::ScopedAStatus close() override;
80 
81     ndk::ScopedAStatus authenticateWithContext(
82             int64_t operationId, const common::OperationContext& context,
83             std::shared_ptr<common::ICancellationSignal>* out) override;
84 
85     ndk::ScopedAStatus enrollWithContext(
86             const keymaster::HardwareAuthToken& hat, EnrollmentType enrollmentType,
87             const std::vector<Feature>& features, const std::optional<NativeHandle>& previewSurface,
88             const common::OperationContext& context,
89             std::shared_ptr<common::ICancellationSignal>* out) override;
90 
91     ndk::ScopedAStatus detectInteractionWithContext(
92             const common::OperationContext& context,
93             std::shared_ptr<common::ICancellationSignal>* out) override;
94 
95     ndk::ScopedAStatus onContextChanged(const common::OperationContext& context) override;
96 
97     ndk::ScopedAStatus enrollWithOptions(
98             const FaceEnrollOptions& options,
99             std::shared_ptr<common::ICancellationSignal>* out) override;
100 
101     binder_status_t linkToDeath(AIBinder* binder);
102 
toString()103     virtual std::string toString() const {
104         std::ostringstream os;
105         os << std::endl << "----- Face::Session:: -----" << std::endl;
106         os << "mStateClosed:" << mStateClosed << std::endl;
107         os << mEngine->toString();
108 
109         return os.str();
110     }
111 
isClosed()112     bool isClosed() { return mStateClosed; }
113 
114   private:
115     std::unique_ptr<FakeFaceEngine> mEngine;
116     std::shared_ptr<ISessionCallback> mCb;
117     std::mt19937 mRandom;
118     std::unique_ptr<WorkerThread> mThread;
119 
120     // Binder death handler.
121     AIBinder_DeathRecipient* mDeathRecipient;
122     bool mStateClosed;
123 };
124 
125 }  // namespace aidl::android::hardware::biometrics::face
126