Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | */ |
Han Sol Jin | 3f11734 | 2024-08-31 14:09:43 -0700 | [diff] [blame] | 16 | #define LOG_TAG "android.hardware.biometrics.fingerprint@2.3-service.otter" |
| 17 | #define LOG_VERBOSE "android.hardware.biometrics.fingerprint@2.3-service.otter" |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 18 | |
| 19 | #include <hardware/hw_auth_token.h> |
| 20 | |
Alexander Martinz | 58df398 | 2024-09-02 11:55:18 +0200 | [diff] [blame] | 21 | #include <android-base/file.h> |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 22 | #include <hardware/hardware.h> |
Michael Bestas | 845b8f5 | 2024-09-18 05:08:38 +0300 | [diff] [blame] | 23 | #include "fingerprint.h" |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 24 | #include "BiometricsFingerprint.h" |
| 25 | |
| 26 | #include <inttypes.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace biometrics { |
| 32 | namespace fingerprint { |
Han Sol Jin | 3f11734 | 2024-08-31 14:09:43 -0700 | [diff] [blame] | 33 | namespace V2_3 { |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 34 | namespace implementation { |
| 35 | |
Alexander Martinz | 58df398 | 2024-09-02 11:55:18 +0200 | [diff] [blame] | 36 | #define UDFPS_HBM_PATH "/sys/class/display/panel/bl_fps_func" |
| 37 | #define UDFPS_HBM_ON "1" |
| 38 | #define UDFPS_HBM_OFF "0" |
| 39 | |
| 40 | #define CMD_FINGERPRINT_EVENT 0xA |
| 41 | #define CMD_FINGERPRINT_EVENT_DOWN 1 |
| 42 | #define CMD_FINGERPRINT_EVENT_UP 0 |
| 43 | |
| 44 | static void setUdfpsHbm(bool status) { |
| 45 | android::base::WriteStringToFile(status ? UDFPS_HBM_ON : UDFPS_HBM_OFF, UDFPS_HBM_PATH); |
| 46 | } |
| 47 | |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 48 | // Supported fingerprint HAL version |
| 49 | static const uint16_t kVersion = HARDWARE_MODULE_API_VERSION(2, 1); |
| 50 | |
| 51 | using RequestStatus = |
| 52 | android::hardware::biometrics::fingerprint::V2_1::RequestStatus; |
| 53 | |
| 54 | BiometricsFingerprint *BiometricsFingerprint::sInstance = nullptr; |
| 55 | |
| 56 | BiometricsFingerprint::BiometricsFingerprint() : mClientCallback(nullptr), mDevice(nullptr) { |
| 57 | sInstance = this; // keep track of the most recent instance |
| 58 | mDevice = openHal(); |
| 59 | if (!mDevice) { |
| 60 | ALOGE("Can't open HAL module"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | BiometricsFingerprint::~BiometricsFingerprint() { |
| 65 | ALOGV("~BiometricsFingerprint()"); |
| 66 | if (mDevice == nullptr) { |
| 67 | ALOGE("No valid device"); |
| 68 | return; |
| 69 | } |
| 70 | int err; |
| 71 | if (0 != (err = mDevice->common.close( |
| 72 | reinterpret_cast<hw_device_t*>(mDevice)))) { |
| 73 | ALOGE("Can't close fingerprint module, error: %d", err); |
| 74 | return; |
| 75 | } |
| 76 | mDevice = nullptr; |
| 77 | } |
| 78 | |
| 79 | Return<RequestStatus> BiometricsFingerprint::ErrorFilter(int32_t error) { |
| 80 | switch(error) { |
| 81 | case 0: return RequestStatus::SYS_OK; |
| 82 | case -2: return RequestStatus::SYS_ENOENT; |
| 83 | case -4: return RequestStatus::SYS_EINTR; |
| 84 | case -5: return RequestStatus::SYS_EIO; |
| 85 | case -11: return RequestStatus::SYS_EAGAIN; |
| 86 | case -12: return RequestStatus::SYS_ENOMEM; |
| 87 | case -13: return RequestStatus::SYS_EACCES; |
| 88 | case -14: return RequestStatus::SYS_EFAULT; |
| 89 | case -16: return RequestStatus::SYS_EBUSY; |
| 90 | case -22: return RequestStatus::SYS_EINVAL; |
| 91 | case -28: return RequestStatus::SYS_ENOSPC; |
| 92 | case -110: return RequestStatus::SYS_ETIMEDOUT; |
| 93 | default: |
| 94 | ALOGE("An unknown error returned from fingerprint vendor library: %d", error); |
| 95 | return RequestStatus::SYS_UNKNOWN; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Translate from errors returned by traditional HAL (see fingerprint.h) to |
| 100 | // HIDL-compliant FingerprintError. |
| 101 | FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, |
| 102 | int32_t* vendorCode) { |
| 103 | *vendorCode = 0; |
| 104 | switch(error) { |
| 105 | case FINGERPRINT_ERROR_HW_UNAVAILABLE: |
| 106 | return FingerprintError::ERROR_HW_UNAVAILABLE; |
| 107 | case FINGERPRINT_ERROR_UNABLE_TO_PROCESS: |
| 108 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 109 | case FINGERPRINT_ERROR_TIMEOUT: |
| 110 | return FingerprintError::ERROR_TIMEOUT; |
| 111 | case FINGERPRINT_ERROR_NO_SPACE: |
| 112 | return FingerprintError::ERROR_NO_SPACE; |
| 113 | case FINGERPRINT_ERROR_CANCELED: |
| 114 | return FingerprintError::ERROR_CANCELED; |
| 115 | case FINGERPRINT_ERROR_UNABLE_TO_REMOVE: |
| 116 | return FingerprintError::ERROR_UNABLE_TO_REMOVE; |
| 117 | case FINGERPRINT_ERROR_LOCKOUT: |
| 118 | return FingerprintError::ERROR_LOCKOUT; |
| 119 | default: |
| 120 | if (error >= FINGERPRINT_ERROR_VENDOR_BASE) { |
| 121 | // vendor specific code. |
| 122 | *vendorCode = error - FINGERPRINT_ERROR_VENDOR_BASE; |
| 123 | return FingerprintError::ERROR_VENDOR; |
| 124 | } |
| 125 | } |
| 126 | ALOGE("Unknown error from fingerprint vendor library: %d", error); |
| 127 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 128 | } |
| 129 | |
| 130 | // Translate acquired messages returned by traditional HAL (see fingerprint.h) |
| 131 | // to HIDL-compliant FingerprintAcquiredInfo. |
| 132 | FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter( |
| 133 | int32_t info, int32_t* vendorCode) { |
| 134 | *vendorCode = 0; |
| 135 | switch(info) { |
| 136 | case FINGERPRINT_ACQUIRED_GOOD: |
| 137 | return FingerprintAcquiredInfo::ACQUIRED_GOOD; |
| 138 | case FINGERPRINT_ACQUIRED_PARTIAL: |
| 139 | return FingerprintAcquiredInfo::ACQUIRED_PARTIAL; |
| 140 | case FINGERPRINT_ACQUIRED_INSUFFICIENT: |
| 141 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 142 | case FINGERPRINT_ACQUIRED_IMAGER_DIRTY: |
| 143 | return FingerprintAcquiredInfo::ACQUIRED_IMAGER_DIRTY; |
| 144 | case FINGERPRINT_ACQUIRED_TOO_SLOW: |
| 145 | return FingerprintAcquiredInfo::ACQUIRED_TOO_SLOW; |
| 146 | case FINGERPRINT_ACQUIRED_TOO_FAST: |
| 147 | return FingerprintAcquiredInfo::ACQUIRED_TOO_FAST; |
| 148 | default: |
| 149 | if (info >= FINGERPRINT_ACQUIRED_VENDOR_BASE) { |
| 150 | // vendor specific code. |
| 151 | *vendorCode = info - FINGERPRINT_ACQUIRED_VENDOR_BASE; |
| 152 | return FingerprintAcquiredInfo::ACQUIRED_VENDOR; |
| 153 | } |
| 154 | } |
| 155 | ALOGE("Unknown acquiredmsg from fingerprint vendor library: %d", info); |
| 156 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 157 | } |
| 158 | |
| 159 | Return<uint64_t> BiometricsFingerprint::setNotify( |
| 160 | const sp<IBiometricsFingerprintClientCallback>& clientCallback) { |
| 161 | std::lock_guard<std::mutex> lock(mClientCallbackMutex); |
| 162 | mClientCallback = clientCallback; |
| 163 | // This is here because HAL 2.1 doesn't have a way to propagate a |
| 164 | // unique token for its driver. Subsequent versions should send a unique |
| 165 | // token for each call to setNotify(). This is fine as long as there's only |
| 166 | // one fingerprint device on the platform. |
| 167 | return reinterpret_cast<uint64_t>(mDevice); |
| 168 | } |
| 169 | |
| 170 | Return<uint64_t> BiometricsFingerprint::preEnroll() { |
| 171 | return mDevice->pre_enroll(mDevice); |
| 172 | } |
| 173 | |
| 174 | Return<RequestStatus> BiometricsFingerprint::enroll(const hidl_array<uint8_t, 69>& hat, |
| 175 | uint32_t gid, uint32_t timeoutSec) { |
| 176 | const hw_auth_token_t* authToken = |
| 177 | reinterpret_cast<const hw_auth_token_t*>(hat.data()); |
| 178 | return ErrorFilter(mDevice->enroll(mDevice, authToken, gid, timeoutSec)); |
| 179 | } |
| 180 | |
| 181 | Return<RequestStatus> BiometricsFingerprint::postEnroll() { |
| 182 | return ErrorFilter(mDevice->post_enroll(mDevice)); |
| 183 | } |
| 184 | |
| 185 | Return<uint64_t> BiometricsFingerprint::getAuthenticatorId() { |
| 186 | return mDevice->get_authenticator_id(mDevice); |
| 187 | } |
| 188 | |
| 189 | Return<RequestStatus> BiometricsFingerprint::cancel() { |
Michael Bestas | cc96108 | 2024-11-11 01:50:06 +0200 | [diff] [blame] | 190 | onFingerUp(); |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 191 | return ErrorFilter(mDevice->cancel(mDevice)); |
| 192 | } |
| 193 | |
| 194 | Return<RequestStatus> BiometricsFingerprint::enumerate() { |
| 195 | return ErrorFilter(mDevice->enumerate(mDevice)); |
| 196 | } |
| 197 | |
| 198 | Return<RequestStatus> BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) { |
| 199 | return ErrorFilter(mDevice->remove(mDevice, gid, fid)); |
| 200 | } |
| 201 | |
| 202 | Return<RequestStatus> BiometricsFingerprint::setActiveGroup(uint32_t gid, |
| 203 | const hidl_string& storePath) { |
| 204 | if (storePath.size() >= PATH_MAX || storePath.size() <= 0) { |
| 205 | ALOGE("Bad path length: %zd", storePath.size()); |
| 206 | return RequestStatus::SYS_EINVAL; |
| 207 | } |
| 208 | if (access(storePath.c_str(), W_OK)) { |
| 209 | return RequestStatus::SYS_EINVAL; |
| 210 | } |
| 211 | |
| 212 | return ErrorFilter(mDevice->set_active_group(mDevice, gid, |
| 213 | storePath.c_str())); |
| 214 | } |
| 215 | |
| 216 | Return<RequestStatus> BiometricsFingerprint::authenticate(uint64_t operationId, |
| 217 | uint32_t gid) { |
| 218 | return ErrorFilter(mDevice->authenticate(mDevice, operationId, gid)); |
| 219 | } |
| 220 | |
| 221 | IBiometricsFingerprint* BiometricsFingerprint::getInstance() { |
| 222 | if (!sInstance) { |
| 223 | sInstance = new BiometricsFingerprint(); |
| 224 | } |
| 225 | return sInstance; |
| 226 | } |
| 227 | |
| 228 | fingerprint_device_t* BiometricsFingerprint::openHal() { |
| 229 | int err; |
| 230 | const hw_module_t *hw_mdl = nullptr; |
| 231 | ALOGD("Opening fingerprint hal library..."); |
| 232 | if (0 != (err = hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_mdl))) { |
| 233 | ALOGE("Can't open fingerprint HW Module, error: %d", err); |
| 234 | return nullptr; |
| 235 | } |
| 236 | |
| 237 | if (hw_mdl == nullptr) { |
| 238 | ALOGE("No valid fingerprint module"); |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | fingerprint_module_t const *module = |
| 243 | reinterpret_cast<const fingerprint_module_t*>(hw_mdl); |
| 244 | if (module->common.methods->open == nullptr) { |
| 245 | ALOGE("No valid open method"); |
| 246 | return nullptr; |
| 247 | } |
| 248 | |
| 249 | hw_device_t *device = nullptr; |
| 250 | |
| 251 | if (0 != (err = module->common.methods->open(hw_mdl, nullptr, &device))) { |
| 252 | ALOGE("Can't open fingerprint methods, error: %d", err); |
| 253 | return nullptr; |
| 254 | } |
| 255 | |
| 256 | if (kVersion != device->version) { |
| 257 | // enforce version on new devices because of HIDL@2.1 translation layer |
| 258 | ALOGE("Wrong fp version. Expected %d, got %d", kVersion, device->version); |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
| 262 | fingerprint_device_t* fp_device = |
| 263 | reinterpret_cast<fingerprint_device_t*>(device); |
| 264 | |
| 265 | if (0 != (err = |
| 266 | fp_device->set_notify(fp_device, BiometricsFingerprint::notify))) { |
| 267 | ALOGE("Can't register fingerprint module callback, error: %d", err); |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
| 271 | return fp_device; |
| 272 | } |
| 273 | |
| 274 | void BiometricsFingerprint::notify(const fingerprint_msg_t *msg) { |
| 275 | BiometricsFingerprint* thisPtr = static_cast<BiometricsFingerprint*>( |
| 276 | BiometricsFingerprint::getInstance()); |
| 277 | std::lock_guard<std::mutex> lock(thisPtr->mClientCallbackMutex); |
| 278 | if (thisPtr == nullptr || thisPtr->mClientCallback == nullptr) { |
| 279 | ALOGE("Receiving callbacks before the client callback is registered."); |
| 280 | return; |
| 281 | } |
| 282 | const uint64_t devId = reinterpret_cast<uint64_t>(thisPtr->mDevice); |
| 283 | switch (msg->type) { |
| 284 | case FINGERPRINT_ERROR: { |
| 285 | int32_t vendorCode = 0; |
| 286 | FingerprintError result = VendorErrorFilter(msg->data.error, &vendorCode); |
| 287 | ALOGD("onError(%d)", result); |
| 288 | if (!thisPtr->mClientCallback->onError(devId, result, vendorCode).isOk()) { |
| 289 | ALOGE("failed to invoke fingerprint onError callback"); |
| 290 | } |
| 291 | } |
| 292 | break; |
| 293 | case FINGERPRINT_ACQUIRED: { |
| 294 | int32_t vendorCode = 0; |
| 295 | FingerprintAcquiredInfo result = |
| 296 | VendorAcquiredFilter(msg->data.acquired.acquired_info, &vendorCode); |
| 297 | ALOGD("onAcquired(%d)", result); |
| 298 | if (!thisPtr->mClientCallback->onAcquired(devId, result, vendorCode).isOk()) { |
| 299 | ALOGE("failed to invoke fingerprint onAcquired callback"); |
| 300 | } |
| 301 | } |
| 302 | break; |
| 303 | case FINGERPRINT_TEMPLATE_ENROLLING: |
| 304 | ALOGD("onEnrollResult(fid=%d, gid=%d, rem=%d)", |
| 305 | msg->data.enroll.finger.fid, |
| 306 | msg->data.enroll.finger.gid, |
| 307 | msg->data.enroll.samples_remaining); |
| 308 | if (!thisPtr->mClientCallback->onEnrollResult(devId, |
| 309 | msg->data.enroll.finger.fid, |
| 310 | msg->data.enroll.finger.gid, |
| 311 | msg->data.enroll.samples_remaining).isOk()) { |
| 312 | ALOGE("failed to invoke fingerprint onEnrollResult callback"); |
| 313 | } |
Michael Bestas | cc96108 | 2024-11-11 01:50:06 +0200 | [diff] [blame] | 314 | if (msg->data.enroll.samples_remaining == 0) { |
| 315 | thisPtr->onFingerUp(); |
| 316 | } |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 317 | break; |
| 318 | case FINGERPRINT_TEMPLATE_REMOVED: |
| 319 | ALOGD("onRemove(fid=%d, gid=%d, rem=%d)", |
| 320 | msg->data.removed.finger.fid, |
| 321 | msg->data.removed.finger.gid, |
| 322 | msg->data.removed.remaining_templates); |
| 323 | if (!thisPtr->mClientCallback->onRemoved(devId, |
| 324 | msg->data.removed.finger.fid, |
| 325 | msg->data.removed.finger.gid, |
| 326 | msg->data.removed.remaining_templates).isOk()) { |
| 327 | ALOGE("failed to invoke fingerprint onRemoved callback"); |
| 328 | } |
| 329 | break; |
| 330 | case FINGERPRINT_AUTHENTICATED: |
| 331 | if (msg->data.authenticated.finger.fid != 0) { |
| 332 | ALOGD("onAuthenticated(fid=%d, gid=%d)", |
| 333 | msg->data.authenticated.finger.fid, |
| 334 | msg->data.authenticated.finger.gid); |
| 335 | const uint8_t* hat = |
| 336 | reinterpret_cast<const uint8_t *>(&msg->data.authenticated.hat); |
| 337 | const hidl_vec<uint8_t> token( |
| 338 | std::vector<uint8_t>(hat, hat + sizeof(msg->data.authenticated.hat))); |
| 339 | if (!thisPtr->mClientCallback->onAuthenticated(devId, |
| 340 | msg->data.authenticated.finger.fid, |
| 341 | msg->data.authenticated.finger.gid, |
| 342 | token).isOk()) { |
| 343 | ALOGE("failed to invoke fingerprint onAuthenticated callback"); |
| 344 | } |
| 345 | } else { |
| 346 | // Not a recognized fingerprint |
| 347 | if (!thisPtr->mClientCallback->onAuthenticated(devId, |
| 348 | msg->data.authenticated.finger.fid, |
| 349 | msg->data.authenticated.finger.gid, |
| 350 | hidl_vec<uint8_t>()).isOk()) { |
| 351 | ALOGE("failed to invoke fingerprint onAuthenticated callback"); |
| 352 | } |
| 353 | } |
Michael Bestas | cc96108 | 2024-11-11 01:50:06 +0200 | [diff] [blame] | 354 | thisPtr->onFingerUp(); |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 355 | break; |
| 356 | case FINGERPRINT_TEMPLATE_ENUMERATING: |
| 357 | ALOGD("onEnumerate(fid=%d, gid=%d, rem=%d)", |
| 358 | msg->data.enumerated.finger.fid, |
| 359 | msg->data.enumerated.finger.gid, |
| 360 | msg->data.enumerated.remaining_templates); |
| 361 | if (!thisPtr->mClientCallback->onEnumerate(devId, |
| 362 | msg->data.enumerated.finger.fid, |
| 363 | msg->data.enumerated.finger.gid, |
| 364 | msg->data.enumerated.remaining_templates).isOk()) { |
| 365 | ALOGE("failed to invoke fingerprint onEnumerate callback"); |
| 366 | } |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
Han Sol Jin | 3f11734 | 2024-08-31 14:09:43 -0700 | [diff] [blame] | 371 | // Methods from ::android::hardware::biometrics::fingerprint::V2_3::IBiometricsFingerprint follow. |
| 372 | |
| 373 | Return<bool> BiometricsFingerprint::isUdfps(uint32_t) { |
Alexander Martinz | 58df398 | 2024-09-02 11:55:18 +0200 | [diff] [blame] | 374 | return true; |
Han Sol Jin | 3f11734 | 2024-08-31 14:09:43 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | Return<void> BiometricsFingerprint::onFingerDown(uint32_t, uint32_t, float, float) { |
Michael Bestas | cc96108 | 2024-11-11 01:50:06 +0200 | [diff] [blame] | 378 | ALOGD("onFingerDown()"); |
Alexander Martinz | 58df398 | 2024-09-02 11:55:18 +0200 | [diff] [blame] | 379 | setUdfpsHbm(true); |
| 380 | mDevice->send_customized_command(mDevice, CMD_FINGERPRINT_EVENT, CMD_FINGERPRINT_EVENT_DOWN); |
Han Sol Jin | 3f11734 | 2024-08-31 14:09:43 -0700 | [diff] [blame] | 381 | return Void(); |
| 382 | } |
| 383 | |
| 384 | Return<void> BiometricsFingerprint::onFingerUp() { |
Michael Bestas | cc96108 | 2024-11-11 01:50:06 +0200 | [diff] [blame] | 385 | ALOGD("onFingerUp()"); |
Alexander Martinz | 58df398 | 2024-09-02 11:55:18 +0200 | [diff] [blame] | 386 | mDevice->send_customized_command(mDevice, CMD_FINGERPRINT_EVENT, CMD_FINGERPRINT_EVENT_UP); |
Michael Bestas | cc96108 | 2024-11-11 01:50:06 +0200 | [diff] [blame] | 387 | setUdfpsHbm(false); |
Han Sol Jin | 3f11734 | 2024-08-31 14:09:43 -0700 | [diff] [blame] | 388 | return Void(); |
| 389 | } |
| 390 | |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 391 | } // namespace implementation |
Han Sol Jin | 3f11734 | 2024-08-31 14:09:43 -0700 | [diff] [blame] | 392 | } // namespace V2_3 |
Alexander Martinz | 6b59526 | 2024-09-02 11:18:25 +0200 | [diff] [blame] | 393 | } // namespace fingerprint |
| 394 | } // namespace biometrics |
| 395 | } // namespace hardware |
| 396 | } // namespace android |