Jeff Pu | df81c96 | 2024-03-06 10:58:17 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2024 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 | #include <unordered_map> |
| 18 | |
| 19 | #include "VirtualHal.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | #include "util/CancellationSignal.h" |
| 24 | |
| 25 | #undef LOG_TAG |
| 26 | #define LOG_TAG "FingerprintVirtualHalAidl" |
| 27 | |
| 28 | namespace aidl::android::hardware::biometrics::fingerprint { |
| 29 | |
| 30 | ::ndk::ScopedAStatus VirtualHal::setEnrollments(const std::vector<int32_t>& enrollments) { |
| 31 | Fingerprint::cfg().sourcedFromAidl(); |
| 32 | Fingerprint::cfg().setopt<OptIntVec>("enrollments", intVec2OptIntVec(enrollments)); |
| 33 | return ndk::ScopedAStatus::ok(); |
| 34 | } |
| 35 | |
| 36 | ::ndk::ScopedAStatus VirtualHal::setEnrollmentHit(int32_t enrollment_hit) { |
| 37 | Fingerprint::cfg().sourcedFromAidl(); |
| 38 | Fingerprint::cfg().set<std::int32_t>("enrollment_hit", enrollment_hit); |
| 39 | return ndk::ScopedAStatus::ok(); |
| 40 | } |
| 41 | |
| 42 | ::ndk::ScopedAStatus VirtualHal::setAuthenticatorId(int64_t in_id) { |
| 43 | Fingerprint::cfg().sourcedFromAidl(); |
| 44 | Fingerprint::cfg().set<int64_t>("authenticator_id", in_id); |
| 45 | return ndk::ScopedAStatus::ok(); |
| 46 | } |
| 47 | |
| 48 | ::ndk::ScopedAStatus VirtualHal::setChallenge(int64_t in_challenge) { |
| 49 | Fingerprint::cfg().sourcedFromAidl(); |
| 50 | Fingerprint::cfg().set<int64_t>("challenge", in_challenge); |
| 51 | return ndk::ScopedAStatus::ok(); |
| 52 | } |
| 53 | |
| 54 | ::ndk::ScopedAStatus VirtualHal::setOperationAuthenticateFails(bool in_fail) { |
| 55 | Fingerprint::cfg().sourcedFromAidl(); |
| 56 | Fingerprint::cfg().set<bool>("operation_authenticate_fails", in_fail); |
| 57 | return ndk::ScopedAStatus::ok(); |
| 58 | } |
| 59 | |
| 60 | ::ndk::ScopedAStatus VirtualHal::setOperationAuthenticateLatency( |
| 61 | const std::vector<int32_t>& in_latency) { |
| 62 | ndk::ScopedAStatus status = sanityCheckLatency(in_latency); |
| 63 | if (!status.isOk()) { |
| 64 | return status; |
| 65 | } |
| 66 | |
| 67 | Fingerprint::cfg().sourcedFromAidl(); |
| 68 | Fingerprint::cfg().setopt<OptIntVec>("operation_authenticate_latency", |
| 69 | intVec2OptIntVec(in_latency)); |
| 70 | return ndk::ScopedAStatus::ok(); |
| 71 | } |
| 72 | |
| 73 | ::ndk::ScopedAStatus VirtualHal::setOperationAuthenticateDuration(int32_t in_duration) { |
| 74 | if (in_duration < 0) { |
| 75 | return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 76 | IVirtualHal::STATUS_INVALID_PARAMETER, "Error: duration can not be negative")); |
| 77 | } |
| 78 | Fingerprint::cfg().sourcedFromAidl(); |
| 79 | Fingerprint::cfg().set<int32_t>("operation_authenticate_duration", in_duration); |
| 80 | return ndk::ScopedAStatus::ok(); |
| 81 | } |
| 82 | |
| 83 | ::ndk::ScopedAStatus VirtualHal::setOperationAuthenticateError(int32_t in_error) { |
| 84 | Fingerprint::cfg().sourcedFromAidl(); |
| 85 | Fingerprint::cfg().set<int32_t>("operation_authenticate_error", in_error); |
| 86 | return ndk::ScopedAStatus::ok(); |
| 87 | } |
| 88 | |
| 89 | ::ndk::ScopedAStatus VirtualHal::setOperationAuthenticateAcquired( |
| 90 | const std::vector<int32_t>& in_acquired) { |
| 91 | Fingerprint::cfg().sourcedFromAidl(); |
| 92 | Fingerprint::cfg().setopt<OptIntVec>("operation_authenticate_acquired", |
| 93 | intVec2OptIntVec(in_acquired)); |
| 94 | return ndk::ScopedAStatus::ok(); |
| 95 | } |
| 96 | |
| 97 | ::ndk::ScopedAStatus VirtualHal::setOperationEnrollError(int32_t in_error) { |
| 98 | Fingerprint::cfg().sourcedFromAidl(); |
| 99 | Fingerprint::cfg().set<int32_t>("operation_enroll_error", in_error); |
| 100 | return ndk::ScopedAStatus::ok(); |
| 101 | } |
| 102 | |
| 103 | ::ndk::ScopedAStatus VirtualHal::setOperationEnrollLatency(const std::vector<int32_t>& in_latency) { |
| 104 | ndk::ScopedAStatus status = sanityCheckLatency(in_latency); |
| 105 | if (!status.isOk()) { |
| 106 | return status; |
| 107 | } |
| 108 | Fingerprint::cfg().sourcedFromAidl(); |
| 109 | Fingerprint::cfg().setopt<OptIntVec>("operation_enroll_latency", intVec2OptIntVec(in_latency)); |
| 110 | return ndk::ScopedAStatus::ok(); |
| 111 | } |
| 112 | |
| 113 | ::ndk::ScopedAStatus VirtualHal::setOperationDetectInteractionLatency( |
| 114 | const std::vector<int32_t>& in_latency) { |
| 115 | ndk::ScopedAStatus status = sanityCheckLatency(in_latency); |
| 116 | if (!status.isOk()) { |
| 117 | return status; |
| 118 | } |
| 119 | Fingerprint::cfg().sourcedFromAidl(); |
| 120 | Fingerprint::cfg().setopt<OptIntVec>("operation_detect_interact_latency", |
| 121 | intVec2OptIntVec(in_latency)); |
| 122 | return ndk::ScopedAStatus::ok(); |
| 123 | } |
| 124 | |
| 125 | ::ndk::ScopedAStatus VirtualHal::setOperationDetectInteractionError(int32_t in_error) { |
| 126 | Fingerprint::cfg().sourcedFromAidl(); |
| 127 | Fingerprint::cfg().set<int32_t>("operation_detect_interaction_error", in_error); |
| 128 | return ndk::ScopedAStatus::ok(); |
| 129 | } |
| 130 | |
| 131 | ::ndk::ScopedAStatus VirtualHal::setOperationDetectInteractionDuration(int32_t in_duration) { |
| 132 | if (in_duration < 0) { |
| 133 | return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 134 | IVirtualHal::STATUS_INVALID_PARAMETER, "Error: duration can not be negative")); |
| 135 | } |
| 136 | Fingerprint::cfg().sourcedFromAidl(); |
| 137 | Fingerprint::cfg().set<int32_t>("operation_detect_interaction_duration", in_duration); |
| 138 | return ndk::ScopedAStatus::ok(); |
| 139 | } |
| 140 | |
| 141 | ::ndk::ScopedAStatus VirtualHal::setOperationDetectInteractionAcquired( |
| 142 | const std::vector<int32_t>& in_acquired) { |
| 143 | Fingerprint::cfg().sourcedFromAidl(); |
| 144 | Fingerprint::cfg().setopt<OptIntVec>("operation_detect_interaction_acquired", |
| 145 | intVec2OptIntVec(in_acquired)); |
| 146 | return ndk::ScopedAStatus::ok(); |
| 147 | } |
| 148 | |
| 149 | ::ndk::ScopedAStatus VirtualHal::setLockout(bool in_lockout) { |
| 150 | Fingerprint::cfg().sourcedFromAidl(); |
| 151 | Fingerprint::cfg().set<bool>("lockout", in_lockout); |
| 152 | return ndk::ScopedAStatus::ok(); |
| 153 | } |
| 154 | |
| 155 | ::ndk::ScopedAStatus VirtualHal::setLockoutEnable(bool in_enable) { |
| 156 | Fingerprint::cfg().sourcedFromAidl(); |
| 157 | Fingerprint::cfg().set<bool>("lockout_enable", in_enable); |
| 158 | return ndk::ScopedAStatus::ok(); |
| 159 | } |
| 160 | |
| 161 | ::ndk::ScopedAStatus VirtualHal::setLockoutTimedThreshold(int32_t in_threshold) { |
| 162 | if (in_threshold < 0) { |
| 163 | return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 164 | IVirtualHal::STATUS_INVALID_PARAMETER, "Error: threshold can not be negative")); |
| 165 | } |
| 166 | Fingerprint::cfg().sourcedFromAidl(); |
| 167 | Fingerprint::cfg().set<int32_t>("lockout_timed_threshold", in_threshold); |
| 168 | return ndk::ScopedAStatus::ok(); |
| 169 | } |
| 170 | |
| 171 | ::ndk::ScopedAStatus VirtualHal::setLockoutTimedDuration(int32_t in_duration) { |
| 172 | if (in_duration < 0) { |
| 173 | return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 174 | IVirtualHal::STATUS_INVALID_PARAMETER, "Error: duration can not be negative")); |
| 175 | } |
| 176 | Fingerprint::cfg().sourcedFromAidl(); |
| 177 | Fingerprint::cfg().set<int32_t>("lockout_timed_duration", in_duration); |
| 178 | return ndk::ScopedAStatus::ok(); |
| 179 | } |
| 180 | |
| 181 | ::ndk::ScopedAStatus VirtualHal::setLockoutPermanentThreshold(int32_t in_threshold) { |
| 182 | if (in_threshold < 0) { |
| 183 | return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 184 | IVirtualHal::STATUS_INVALID_PARAMETER, "Error: threshold can not be negative")); |
| 185 | } |
| 186 | Fingerprint::cfg().sourcedFromAidl(); |
| 187 | Fingerprint::cfg().set<int32_t>("lockout_permanent_threshold", in_threshold); |
| 188 | return ndk::ScopedAStatus::ok(); |
| 189 | } |
| 190 | |
| 191 | ::ndk::ScopedAStatus VirtualHal::setType( |
| 192 | ::aidl::android::hardware::biometrics::fingerprint::FingerprintSensorType in_type) { |
| 193 | Fingerprint::cfg().sourcedFromAidl(); |
| 194 | Fingerprint::cfg().set<std::string>("type", Fingerprint::type2String(in_type)); |
| 195 | return ndk::ScopedAStatus::ok(); |
| 196 | } |
| 197 | |
| 198 | ::ndk::ScopedAStatus VirtualHal::setSensorId(int32_t in_id) { |
| 199 | Fingerprint::cfg().sourcedFromAidl(); |
| 200 | Fingerprint::cfg().set<int32_t>("sensor_id", in_id); |
| 201 | return ndk::ScopedAStatus::ok(); |
| 202 | } |
| 203 | |
| 204 | ::ndk::ScopedAStatus VirtualHal::setSensorStrength(SensorStrength in_strength) { |
| 205 | Fingerprint::cfg().sourcedFromAidl(); |
| 206 | Fingerprint::cfg().set<int32_t>("sensor_strength", (int32_t)in_strength); |
| 207 | return ndk::ScopedAStatus::ok(); |
| 208 | } |
| 209 | |
| 210 | ::ndk::ScopedAStatus VirtualHal::setMaxEnrollmentPerUser(int32_t in_max) { |
| 211 | Fingerprint::cfg().sourcedFromAidl(); |
| 212 | Fingerprint::cfg().set<int32_t>("max_enrollments", in_max); |
| 213 | return ndk::ScopedAStatus::ok(); |
| 214 | } |
| 215 | |
| 216 | ::ndk::ScopedAStatus VirtualHal::setSensorLocation(const SensorLocation& in_loc) { |
| 217 | std::string str = std::to_string(in_loc.sensorLocationX) + ":" + |
| 218 | std::to_string(in_loc.sensorLocationY) + ":" + |
| 219 | std::to_string(in_loc.sensorRadius); |
| 220 | ; |
| 221 | Fingerprint::cfg().sourcedFromAidl(); |
| 222 | Fingerprint::cfg().set<std::string>("sensor_location", str); |
| 223 | return ndk::ScopedAStatus::ok(); |
| 224 | } |
| 225 | |
| 226 | ::ndk::ScopedAStatus VirtualHal::setNavigationGuesture(bool in_v) { |
| 227 | Fingerprint::cfg().sourcedFromAidl(); |
| 228 | Fingerprint::cfg().set<bool>("navigation_guesture", in_v); |
| 229 | return ndk::ScopedAStatus::ok(); |
| 230 | } |
| 231 | |
| 232 | ::ndk::ScopedAStatus VirtualHal::setDetectInteraction(bool in_v) { |
| 233 | Fingerprint::cfg().sourcedFromAidl(); |
| 234 | Fingerprint::cfg().set<bool>("detect_interaction", in_v); |
| 235 | return ndk::ScopedAStatus::ok(); |
| 236 | } |
| 237 | |
| 238 | ::ndk::ScopedAStatus VirtualHal::setDisplayTouch(bool in_v) { |
| 239 | Fingerprint::cfg().sourcedFromAidl(); |
| 240 | Fingerprint::cfg().set<bool>("display_touch", in_v); |
| 241 | return ndk::ScopedAStatus::ok(); |
| 242 | } |
| 243 | |
| 244 | ::ndk::ScopedAStatus VirtualHal::setControlIllumination(bool in_v) { |
| 245 | Fingerprint::cfg().sourcedFromAidl(); |
| 246 | Fingerprint::cfg().set<bool>("control_illumination", in_v); |
| 247 | return ndk::ScopedAStatus::ok(); |
| 248 | } |
| 249 | |
| 250 | OptIntVec VirtualHal::intVec2OptIntVec(const std::vector<int32_t>& in_vec) { |
| 251 | OptIntVec optIntVec; |
| 252 | std::transform(in_vec.begin(), in_vec.end(), std::back_inserter(optIntVec), |
| 253 | [](int value) { return std::optional<int>(value); }); |
| 254 | return optIntVec; |
| 255 | } |
| 256 | |
| 257 | ::ndk::ScopedAStatus VirtualHal::sanityCheckLatency(const std::vector<int32_t>& in_latency) { |
| 258 | if (in_latency.size() == 0 || in_latency.size() > 2) { |
| 259 | return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 260 | IVirtualHal::STATUS_INVALID_PARAMETER, |
| 261 | "Error: input input array must contain 1 or 2 elements")); |
| 262 | } |
| 263 | |
| 264 | for (auto x : in_latency) { |
| 265 | if (x < 0) { |
| 266 | return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 267 | IVirtualHal::STATUS_INVALID_PARAMETER, |
| 268 | "Error: input data must not be negative")); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return ndk::ScopedAStatus::ok(); |
| 273 | } |
| 274 | |
| 275 | } // namespace aidl::android::hardware::biometrics::fingerprint |