Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 16 | #define LOG_TAG "android.hardware.biometrics.fingerprint@2.1-service" |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 17 | |
| 18 | #include <hardware/hardware.h> |
| 19 | #include <hardware/fingerprint.h> |
| 20 | #include "BiometricsFingerprint.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace hardware { |
| 24 | namespace biometrics { |
| 25 | namespace fingerprint { |
| 26 | namespace V2_1 { |
| 27 | namespace implementation { |
| 28 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 29 | // Supported fingerprint HAL version |
| 30 | static const uint16_t kVersion = HARDWARE_MODULE_API_VERSION(2, 1); |
| 31 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 32 | using RequestStatus = |
| 33 | android::hardware::biometrics::fingerprint::V2_1::RequestStatus; |
| 34 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 35 | sp<IBiometricsFingerprintClientCallback> |
| 36 | BiometricsFingerprint::mClientCallback = nullptr; |
| 37 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 38 | // TODO: This is here because HAL 2.1 doesn't have a way to propagate a |
| 39 | // unique token for its driver. Subsequent versions should send a unique |
| 40 | // token for each call to notify(). This is fine as long as there's only |
| 41 | // one fingerprint device on the platform. |
| 42 | fingerprint_device_t *BiometricsFingerprint::sDevice = nullptr; |
| 43 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 44 | BiometricsFingerprint::BiometricsFingerprint(fingerprint_device_t *device) |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 45 | : mDevice(device) { |
| 46 | sDevice = mDevice; // keep track of the most recent instance |
| 47 | } |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 48 | |
| 49 | BiometricsFingerprint::~BiometricsFingerprint() { |
| 50 | ALOG(LOG_VERBOSE, LOG_TAG, "nativeCloseHal()\n"); |
| 51 | if (mDevice == NULL) { |
| 52 | ALOGE("No valid device"); |
| 53 | return; |
| 54 | } |
| 55 | int err; |
| 56 | if (0 != (err = mDevice->common.close( |
| 57 | reinterpret_cast<hw_device_t*>(mDevice)))) { |
| 58 | ALOGE("Can't close fingerprint module, error: %d", err); |
| 59 | return; |
| 60 | } |
| 61 | mDevice = NULL; |
| 62 | } |
| 63 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 64 | Return<RequestStatus> BiometricsFingerprint::ErrorFilter(int32_t error) { |
| 65 | switch(error) { |
| 66 | case 0: return RequestStatus::SYS_OK; |
| 67 | case -2: return RequestStatus::SYS_ENOENT; |
| 68 | case -4: return RequestStatus::SYS_EINTR; |
| 69 | case -5: return RequestStatus::SYS_EIO; |
| 70 | case -11: return RequestStatus::SYS_EAGAIN; |
| 71 | case -12: return RequestStatus::SYS_ENOMEM; |
| 72 | case -13: return RequestStatus::SYS_EACCES; |
| 73 | case -14: return RequestStatus::SYS_EFAULT; |
| 74 | case -16: return RequestStatus::SYS_EBUSY; |
| 75 | case -22: return RequestStatus::SYS_EINVAL; |
| 76 | case -28: return RequestStatus::SYS_ENOSPC; |
| 77 | case -110: return RequestStatus::SYS_ETIMEDOUT; |
| 78 | default: |
| 79 | ALOGE("An unknown error returned from fingerprint vendor library"); |
| 80 | return RequestStatus::SYS_UNKNOWN; |
| 81 | } |
| 82 | } |
| 83 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 84 | // Translate from errors returned by traditional HAL (see fingerprint.h) to |
| 85 | // HIDL-compliant FingerprintError. |
| 86 | FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, |
| 87 | int32_t* vendorCode) { |
| 88 | *vendorCode = 0; |
| 89 | switch(error) { |
| 90 | case FINGERPRINT_ERROR_HW_UNAVAILABLE: |
| 91 | return FingerprintError::ERROR_HW_UNAVAILABLE; |
| 92 | case FINGERPRINT_ERROR_UNABLE_TO_PROCESS: |
| 93 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 94 | case FINGERPRINT_ERROR_TIMEOUT: |
| 95 | return FingerprintError::ERROR_TIMEOUT; |
| 96 | case FINGERPRINT_ERROR_NO_SPACE: |
| 97 | return FingerprintError::ERROR_NO_SPACE; |
| 98 | case FINGERPRINT_ERROR_CANCELED: |
| 99 | return FingerprintError::ERROR_CANCELED; |
| 100 | case FINGERPRINT_ERROR_UNABLE_TO_REMOVE: |
| 101 | return FingerprintError::ERROR_UNABLE_TO_REMOVE; |
| 102 | default: |
| 103 | if (error >= FINGERPRINT_ERROR_VENDOR_BASE) { |
| 104 | // vendor specific code. |
| 105 | *vendorCode = error - FINGERPRINT_ERROR_VENDOR_BASE; |
| 106 | return FingerprintError::ERROR_VENDOR; |
| 107 | } |
| 108 | } |
| 109 | ALOGE("Unknown error from fingerprint vendor library"); |
| 110 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 111 | } |
| 112 | |
| 113 | // Translate acquired messages returned by traditional HAL (see fingerprint.h) |
| 114 | // to HIDL-compliant FingerprintAcquiredInfo. |
| 115 | FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter( |
| 116 | int32_t info, int32_t* vendorCode) { |
| 117 | *vendorCode = 0; |
| 118 | switch(info) { |
| 119 | case FINGERPRINT_ACQUIRED_GOOD: |
| 120 | return FingerprintAcquiredInfo::ACQUIRED_GOOD; |
| 121 | case FINGERPRINT_ACQUIRED_PARTIAL: |
| 122 | return FingerprintAcquiredInfo::ACQUIRED_PARTIAL; |
| 123 | case FINGERPRINT_ACQUIRED_INSUFFICIENT: |
| 124 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 125 | case FINGERPRINT_ACQUIRED_IMAGER_DIRTY: |
| 126 | return FingerprintAcquiredInfo::ACQUIRED_IMAGER_DIRTY; |
| 127 | case FINGERPRINT_ACQUIRED_TOO_SLOW: |
| 128 | return FingerprintAcquiredInfo::ACQUIRED_TOO_SLOW; |
| 129 | case FINGERPRINT_ACQUIRED_TOO_FAST: |
| 130 | return FingerprintAcquiredInfo::ACQUIRED_TOO_FAST; |
| 131 | default: |
| 132 | if (info >= FINGERPRINT_ACQUIRED_VENDOR_BASE) { |
| 133 | // vendor specific code. |
| 134 | *vendorCode = info - FINGERPRINT_ACQUIRED_VENDOR_BASE; |
| 135 | return FingerprintAcquiredInfo::ACQUIRED_VENDOR; |
| 136 | } |
| 137 | } |
| 138 | ALOGE("Unknown acquiredmsg from fingerprint vendor library"); |
| 139 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 140 | } |
| 141 | |
| 142 | Return<uint64_t> BiometricsFingerprint::setNotify( |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 143 | const sp<IBiometricsFingerprintClientCallback>& clientCallback) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 144 | mClientCallback = clientCallback; |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 145 | return reinterpret_cast<uint64_t>(mDevice); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | Return<uint64_t> BiometricsFingerprint::preEnroll() { |
| 149 | return mDevice->pre_enroll(mDevice); |
| 150 | } |
| 151 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 152 | Return<RequestStatus> BiometricsFingerprint::enroll(const hidl_array<uint8_t, 69>& hat, |
| 153 | uint32_t gid, uint32_t timeoutSec) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 154 | const hw_auth_token_t* authToken = |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 155 | reinterpret_cast<const hw_auth_token_t*>(hat.data()); |
| 156 | return ErrorFilter(mDevice->enroll(mDevice, authToken, gid, timeoutSec)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 159 | Return<RequestStatus> BiometricsFingerprint::postEnroll() { |
| 160 | return ErrorFilter(mDevice->post_enroll(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | Return<uint64_t> BiometricsFingerprint::getAuthenticatorId() { |
| 164 | return mDevice->get_authenticator_id(mDevice); |
| 165 | } |
| 166 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 167 | Return<RequestStatus> BiometricsFingerprint::cancel() { |
| 168 | return ErrorFilter(mDevice->cancel(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 171 | Return<RequestStatus> BiometricsFingerprint::enumerate() { |
| 172 | return ErrorFilter(mDevice->enumerate(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 175 | Return<RequestStatus> BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) { |
| 176 | return ErrorFilter(mDevice->remove(mDevice, gid, fid)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 179 | Return<RequestStatus> BiometricsFingerprint::setActiveGroup(uint32_t gid, |
| 180 | const hidl_string& storePath) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 181 | if (storePath.size() >= PATH_MAX || storePath.size() <= 0) { |
| 182 | ALOGE("Bad path length: %zd", storePath.size()); |
| 183 | } |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 184 | return ErrorFilter(mDevice->set_active_group(mDevice, gid, |
| 185 | storePath.c_str())); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 188 | Return<RequestStatus> BiometricsFingerprint::authenticate(uint64_t operationId, |
| 189 | uint32_t gid) { |
| 190 | return ErrorFilter(mDevice->authenticate(mDevice, operationId, gid)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 193 | IBiometricsFingerprint* BiometricsFingerprint::getInstance() { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 194 | int err; |
| 195 | const hw_module_t *hw_mdl = NULL; |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 196 | ALOGE("Opening fingerprint hal library..."); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 197 | if (0 != (err = hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_mdl))) { |
| 198 | ALOGE("Can't open fingerprint HW Module, error: %d", err); |
| 199 | return nullptr; |
| 200 | } |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 201 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 202 | if (hw_mdl == NULL) { |
| 203 | ALOGE("No valid fingerprint module"); |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | fingerprint_module_t const *module = |
| 208 | reinterpret_cast<const fingerprint_module_t*>(hw_mdl); |
| 209 | if (module->common.methods->open == NULL) { |
| 210 | ALOGE("No valid open method"); |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | hw_device_t *device = NULL; |
| 215 | |
| 216 | if (0 != (err = module->common.methods->open(hw_mdl, NULL, &device))) { |
| 217 | ALOGE("Can't open fingerprint methods, error: %d", err); |
| 218 | return nullptr; |
| 219 | } |
| 220 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 221 | if (kVersion != device->version) { |
| 222 | ALOGE("Wrong fp version. Expected %d, got %d", kVersion, device->version); |
| 223 | return 0; // enforce this on new devices because of HIDL translation layer |
| 224 | } |
| 225 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 226 | fingerprint_device_t* fp_device = |
| 227 | reinterpret_cast<fingerprint_device_t*>(device); |
| 228 | |
| 229 | if (0 != (err = |
| 230 | fp_device->set_notify(fp_device, BiometricsFingerprint::notify))) { |
| 231 | ALOGE("Can't register fingerprint module callback, error: %d", err); |
| 232 | return nullptr; |
| 233 | } |
| 234 | |
| 235 | return new BiometricsFingerprint(fp_device); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | } // namespace implementation |
| 239 | } // namespace V2_1 |
| 240 | } // namespace fingerprint |
| 241 | } // namespace biometrics |
| 242 | } // namespace hardware |
| 243 | } // namespace android |