Jan Altensen | 1813ff9 | 2019-08-20 23:20:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The LineageOS 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 | #define LOG_TAG "android.hardware.biometrics.fingerprint@2.1-service.samsung" |
| 17 | |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include <hardware/hw_auth_token.h> |
| 21 | |
| 22 | #include <hardware/fingerprint.h> |
| 23 | #include <hardware/hardware.h> |
| 24 | #include "BiometricsFingerprint.h" |
| 25 | |
| 26 | #include <dlfcn.h> |
| 27 | #include <inttypes.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace biometrics { |
| 33 | namespace fingerprint { |
| 34 | namespace V2_1 { |
| 35 | namespace implementation { |
| 36 | |
| 37 | using RequestStatus = android::hardware::biometrics::fingerprint::V2_1::RequestStatus; |
| 38 | |
| 39 | BiometricsFingerprint* BiometricsFingerprint::sInstance = nullptr; |
| 40 | |
| 41 | BiometricsFingerprint::BiometricsFingerprint() : mClientCallback(nullptr) { |
| 42 | sInstance = this; // keep track of the most recent instance |
| 43 | if (!openHal()) { |
| 44 | LOG(ERROR) << "Can't open HAL module"; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | BiometricsFingerprint::~BiometricsFingerprint() { |
| 49 | if (ss_fingerprint_close() != 0) { |
| 50 | LOG(ERROR) << "Can't close HAL module"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | Return<RequestStatus> BiometricsFingerprint::ErrorFilter(int32_t error) { |
| 55 | switch (error) { |
| 56 | case 0: |
| 57 | return RequestStatus::SYS_OK; |
| 58 | case -2: |
| 59 | return RequestStatus::SYS_ENOENT; |
| 60 | case -4: |
| 61 | return RequestStatus::SYS_EINTR; |
| 62 | case -5: |
| 63 | return RequestStatus::SYS_EIO; |
| 64 | case -11: |
| 65 | return RequestStatus::SYS_EAGAIN; |
| 66 | case -12: |
| 67 | return RequestStatus::SYS_ENOMEM; |
| 68 | case -13: |
| 69 | return RequestStatus::SYS_EACCES; |
| 70 | case -14: |
| 71 | return RequestStatus::SYS_EFAULT; |
| 72 | case -16: |
| 73 | return RequestStatus::SYS_EBUSY; |
| 74 | case -22: |
| 75 | return RequestStatus::SYS_EINVAL; |
| 76 | case -28: |
| 77 | return RequestStatus::SYS_ENOSPC; |
| 78 | case -110: |
| 79 | return RequestStatus::SYS_ETIMEDOUT; |
| 80 | default: |
| 81 | LOG(ERROR) << "An unknown error returned from fingerprint vendor library: " << error; |
| 82 | return RequestStatus::SYS_UNKNOWN; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Translate from errors returned by traditional HAL (see fingerprint.h) to |
| 87 | // HIDL-compliant FingerprintError. |
| 88 | FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, int32_t* vendorCode) { |
| 89 | *vendorCode = 0; |
| 90 | switch (error) { |
| 91 | case FINGERPRINT_ERROR_HW_UNAVAILABLE: |
| 92 | return FingerprintError::ERROR_HW_UNAVAILABLE; |
| 93 | case FINGERPRINT_ERROR_UNABLE_TO_PROCESS: |
| 94 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 95 | case FINGERPRINT_ERROR_TIMEOUT: |
| 96 | return FingerprintError::ERROR_TIMEOUT; |
| 97 | case FINGERPRINT_ERROR_NO_SPACE: |
| 98 | return FingerprintError::ERROR_NO_SPACE; |
| 99 | case FINGERPRINT_ERROR_CANCELED: |
| 100 | return FingerprintError::ERROR_CANCELED; |
| 101 | case FINGERPRINT_ERROR_UNABLE_TO_REMOVE: |
| 102 | return FingerprintError::ERROR_UNABLE_TO_REMOVE; |
| 103 | case FINGERPRINT_ERROR_LOCKOUT: |
| 104 | return FingerprintError::ERROR_LOCKOUT; |
| 105 | default: |
| 106 | if (error >= FINGERPRINT_ERROR_VENDOR_BASE) { |
| 107 | // vendor specific code. |
| 108 | *vendorCode = error - FINGERPRINT_ERROR_VENDOR_BASE; |
| 109 | return FingerprintError::ERROR_VENDOR; |
| 110 | } |
| 111 | } |
| 112 | LOG(ERROR) << "Unknown error from fingerprint vendor library: " << error; |
| 113 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 114 | } |
| 115 | |
| 116 | // Translate acquired messages returned by traditional HAL (see fingerprint.h) |
| 117 | // to HIDL-compliant FingerprintAcquiredInfo. |
| 118 | FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter(int32_t info, |
| 119 | int32_t* vendorCode) { |
| 120 | *vendorCode = 0; |
| 121 | switch (info) { |
| 122 | case FINGERPRINT_ACQUIRED_GOOD: |
| 123 | return FingerprintAcquiredInfo::ACQUIRED_GOOD; |
| 124 | case FINGERPRINT_ACQUIRED_PARTIAL: |
| 125 | return FingerprintAcquiredInfo::ACQUIRED_PARTIAL; |
| 126 | case FINGERPRINT_ACQUIRED_INSUFFICIENT: |
| 127 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 128 | case FINGERPRINT_ACQUIRED_IMAGER_DIRTY: |
| 129 | return FingerprintAcquiredInfo::ACQUIRED_IMAGER_DIRTY; |
| 130 | case FINGERPRINT_ACQUIRED_TOO_SLOW: |
| 131 | return FingerprintAcquiredInfo::ACQUIRED_TOO_SLOW; |
| 132 | case FINGERPRINT_ACQUIRED_TOO_FAST: |
| 133 | return FingerprintAcquiredInfo::ACQUIRED_TOO_FAST; |
| 134 | default: |
| 135 | if (info >= FINGERPRINT_ACQUIRED_VENDOR_BASE) { |
| 136 | // vendor specific code. |
| 137 | *vendorCode = info - FINGERPRINT_ACQUIRED_VENDOR_BASE; |
| 138 | return FingerprintAcquiredInfo::ACQUIRED_VENDOR; |
| 139 | } |
| 140 | } |
| 141 | LOG(ERROR) << "Unknown acquiredmsg from fingerprint vendor library: " << info; |
| 142 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 143 | } |
| 144 | |
| 145 | Return<uint64_t> BiometricsFingerprint::setNotify( |
| 146 | const sp<IBiometricsFingerprintClientCallback>& clientCallback) { |
| 147 | std::lock_guard<std::mutex> lock(mClientCallbackMutex); |
| 148 | mClientCallback = clientCallback; |
| 149 | // This is here because HAL 2.1 doesn't have a way to propagate a |
| 150 | // unique token for its driver. Subsequent versions should send a unique |
| 151 | // token for each call to setNotify(). This is fine as long as there's only |
| 152 | // one fingerprint device on the platform. |
| 153 | return reinterpret_cast<uint64_t>(this); |
| 154 | } |
| 155 | |
| 156 | Return<uint64_t> BiometricsFingerprint::preEnroll() { |
| 157 | return ss_fingerprint_pre_enroll(); |
| 158 | } |
| 159 | |
| 160 | Return<RequestStatus> BiometricsFingerprint::enroll(const hidl_array<uint8_t, 69>& hat, |
| 161 | uint32_t gid, uint32_t timeoutSec) { |
| 162 | const hw_auth_token_t* authToken = reinterpret_cast<const hw_auth_token_t*>(hat.data()); |
| 163 | |
| 164 | return ErrorFilter(ss_fingerprint_enroll(authToken, gid, timeoutSec)); |
| 165 | } |
| 166 | |
| 167 | Return<RequestStatus> BiometricsFingerprint::postEnroll() { |
| 168 | return ErrorFilter(ss_fingerprint_post_enroll()); |
| 169 | } |
| 170 | |
| 171 | Return<uint64_t> BiometricsFingerprint::getAuthenticatorId() { |
| 172 | return ss_fingerprint_get_auth_id(); |
| 173 | } |
| 174 | |
| 175 | Return<RequestStatus> BiometricsFingerprint::cancel() { |
| 176 | return ErrorFilter(ss_fingerprint_cancel()); |
| 177 | } |
| 178 | |
| 179 | Return<RequestStatus> BiometricsFingerprint::enumerate() { |
| 180 | if (ss_fingerprint_enumerate != nullptr) { |
| 181 | return ErrorFilter(ss_fingerprint_enumerate()); |
| 182 | } |
| 183 | |
| 184 | return RequestStatus::SYS_UNKNOWN; |
| 185 | } |
| 186 | |
| 187 | Return<RequestStatus> BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) { |
| 188 | return ErrorFilter(ss_fingerprint_remove(gid, fid)); |
| 189 | } |
| 190 | |
| 191 | Return<RequestStatus> BiometricsFingerprint::setActiveGroup(uint32_t gid, |
| 192 | const hidl_string& storePath) { |
| 193 | if (storePath.size() >= PATH_MAX || storePath.size() <= 0) { |
| 194 | LOG(ERROR) << "Bad path length: " << storePath.size(); |
| 195 | return RequestStatus::SYS_EINVAL; |
| 196 | } |
| 197 | |
| 198 | if (access(storePath.c_str(), W_OK)) { |
| 199 | return RequestStatus::SYS_EINVAL; |
| 200 | } |
| 201 | |
| 202 | return ErrorFilter(ss_fingerprint_set_active_group(gid, storePath.c_str())); |
| 203 | } |
| 204 | |
| 205 | Return<RequestStatus> BiometricsFingerprint::authenticate(uint64_t operationId, uint32_t gid) { |
| 206 | return ErrorFilter(ss_fingerprint_authenticate(operationId, gid)); |
| 207 | } |
| 208 | |
| 209 | IBiometricsFingerprint* BiometricsFingerprint::getInstance() { |
| 210 | if (!sInstance) { |
| 211 | sInstance = new BiometricsFingerprint(); |
| 212 | } |
| 213 | return sInstance; |
| 214 | } |
| 215 | |
| 216 | bool BiometricsFingerprint::openHal() { |
| 217 | void* handle = dlopen("libbauthserver.so", RTLD_NOW); |
| 218 | if (handle) { |
| 219 | int err; |
| 220 | |
| 221 | ss_fingerprint_close = |
| 222 | reinterpret_cast<typeof(ss_fingerprint_close)>(dlsym(handle, "ss_fingerprint_close")); |
| 223 | ss_fingerprint_open = |
| 224 | reinterpret_cast<typeof(ss_fingerprint_open)>(dlsym(handle, "ss_fingerprint_open")); |
| 225 | |
| 226 | ss_set_notify_callback = reinterpret_cast<typeof(ss_set_notify_callback)>( |
| 227 | dlsym(handle, "ss_set_notify_callback")); |
| 228 | ss_fingerprint_pre_enroll = reinterpret_cast<typeof(ss_fingerprint_pre_enroll)>( |
| 229 | dlsym(handle, "ss_fingerprint_pre_enroll")); |
| 230 | ss_fingerprint_enroll = |
| 231 | reinterpret_cast<typeof(ss_fingerprint_enroll)>(dlsym(handle, "ss_fingerprint_enroll")); |
| 232 | ss_fingerprint_post_enroll = reinterpret_cast<typeof(ss_fingerprint_post_enroll)>( |
| 233 | dlsym(handle, "ss_fingerprint_post_enroll")); |
| 234 | ss_fingerprint_get_auth_id = reinterpret_cast<typeof(ss_fingerprint_get_auth_id)>( |
| 235 | dlsym(handle, "ss_fingerprint_get_auth_id")); |
| 236 | ss_fingerprint_cancel = |
| 237 | reinterpret_cast<typeof(ss_fingerprint_cancel)>(dlsym(handle, "ss_fingerprint_cancel")); |
| 238 | ss_fingerprint_enumerate = reinterpret_cast<typeof(ss_fingerprint_enumerate)>( |
| 239 | dlsym(handle, "ss_fingerprint_enumerate")); |
| 240 | ss_fingerprint_remove = |
| 241 | reinterpret_cast<typeof(ss_fingerprint_remove)>(dlsym(handle, "ss_fingerprint_remove")); |
| 242 | ss_fingerprint_set_active_group = reinterpret_cast<typeof(ss_fingerprint_set_active_group)>( |
| 243 | dlsym(handle, "ss_fingerprint_set_active_group")); |
| 244 | ss_fingerprint_authenticate = reinterpret_cast<typeof(ss_fingerprint_authenticate)>( |
| 245 | dlsym(handle, "ss_fingerprint_authenticate")); |
| 246 | |
| 247 | if ((err = ss_fingerprint_open(nullptr)) != 0) { |
| 248 | LOG(ERROR) << "Can't open fingerprint, error: " << err; |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | if ((err = ss_set_notify_callback(BiometricsFingerprint::notify)) != 0) { |
| 253 | LOG(ERROR) << "Can't register fingerprint module callback, error: " << err; |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | void BiometricsFingerprint::notify(const fingerprint_msg_t* msg) { |
| 264 | BiometricsFingerprint* thisPtr = |
| 265 | static_cast<BiometricsFingerprint*>(BiometricsFingerprint::getInstance()); |
| 266 | std::lock_guard<std::mutex> lock(thisPtr->mClientCallbackMutex); |
| 267 | if (thisPtr == nullptr || thisPtr->mClientCallback == nullptr) { |
| 268 | LOG(ERROR) << "Receiving callbacks before the client callback is registered."; |
| 269 | return; |
| 270 | } |
| 271 | const uint64_t devId = 1; |
| 272 | switch (msg->type) { |
| 273 | case FINGERPRINT_ERROR: { |
| 274 | int32_t vendorCode = 0; |
| 275 | FingerprintError result = VendorErrorFilter(msg->data.error, &vendorCode); |
| 276 | LOG(DEBUG) << "onError(" << static_cast<int>(result) << ")"; |
| 277 | if (!thisPtr->mClientCallback->onError(devId, result, vendorCode).isOk()) { |
| 278 | LOG(ERROR) << "failed to invoke fingerprint onError callback"; |
| 279 | } |
| 280 | } break; |
| 281 | case FINGERPRINT_ACQUIRED: { |
| 282 | int32_t vendorCode = 0; |
| 283 | FingerprintAcquiredInfo result = |
| 284 | VendorAcquiredFilter(msg->data.acquired.acquired_info, &vendorCode); |
| 285 | LOG(DEBUG) << "onAcquired(" << static_cast<int>(result) << ")"; |
| 286 | if (!thisPtr->mClientCallback->onAcquired(devId, result, vendorCode).isOk()) { |
| 287 | LOG(ERROR) << "failed to invoke fingerprint onAcquired callback"; |
| 288 | } |
| 289 | } break; |
| 290 | case FINGERPRINT_TEMPLATE_ENROLLING: |
Danny Wood | 16d1a36 | 2019-12-23 11:37:10 +0000 | [diff] [blame^] | 291 | #ifdef USES_PERCENTAGE_SAMPLES |
| 292 | const_cast<fingerprint_msg_t*>(msg)->data.enroll.samples_remaining = |
| 293 | 100 - msg->data.enroll.samples_remaining; |
| 294 | #endif |
Jan Altensen | 1813ff9 | 2019-08-20 23:20:25 +0200 | [diff] [blame] | 295 | LOG(DEBUG) << "onEnrollResult(fid=" << msg->data.enroll.finger.fid |
| 296 | << ", gid=" << msg->data.enroll.finger.gid |
| 297 | << ", rem=" << msg->data.enroll.samples_remaining << ")"; |
Danny Wood | 16d1a36 | 2019-12-23 11:37:10 +0000 | [diff] [blame^] | 298 | if (!thisPtr->mClientCallback |
Jan Altensen | 1813ff9 | 2019-08-20 23:20:25 +0200 | [diff] [blame] | 299 | ->onEnrollResult(devId, msg->data.enroll.finger.fid, |
| 300 | msg->data.enroll.finger.gid, msg->data.enroll.samples_remaining) |
| 301 | .isOk()) { |
Jan Altensen | 1813ff9 | 2019-08-20 23:20:25 +0200 | [diff] [blame] | 302 | LOG(ERROR) << "failed to invoke fingerprint onEnrollResult callback"; |
| 303 | } |
| 304 | break; |
| 305 | case FINGERPRINT_TEMPLATE_REMOVED: |
| 306 | LOG(DEBUG) << "onRemove(fid=" << msg->data.removed.finger.fid |
| 307 | << ", gid=" << msg->data.removed.finger.gid |
| 308 | << ", rem=" << msg->data.removed.remaining_templates << ")"; |
| 309 | if (!thisPtr->mClientCallback |
| 310 | ->onRemoved(devId, msg->data.removed.finger.fid, msg->data.removed.finger.gid, |
| 311 | msg->data.removed.remaining_templates) |
| 312 | .isOk()) { |
| 313 | LOG(ERROR) << "failed to invoke fingerprint onRemoved callback"; |
| 314 | } |
| 315 | break; |
| 316 | case FINGERPRINT_AUTHENTICATED: |
| 317 | LOG(DEBUG) << "onAuthenticated(fid=" << msg->data.authenticated.finger.fid |
| 318 | << ", gid=" << msg->data.authenticated.finger.gid << ")"; |
| 319 | if (msg->data.authenticated.finger.fid != 0) { |
| 320 | const uint8_t* hat = reinterpret_cast<const uint8_t*>(&msg->data.authenticated.hat); |
| 321 | const hidl_vec<uint8_t> token( |
| 322 | std::vector<uint8_t>(hat, hat + sizeof(msg->data.authenticated.hat))); |
| 323 | if (!thisPtr->mClientCallback |
| 324 | ->onAuthenticated(devId, msg->data.authenticated.finger.fid, |
| 325 | msg->data.authenticated.finger.gid, token) |
| 326 | .isOk()) { |
| 327 | LOG(ERROR) << "failed to invoke fingerprint onAuthenticated callback"; |
| 328 | } |
| 329 | } else { |
| 330 | // Not a recognized fingerprint |
| 331 | if (!thisPtr->mClientCallback |
| 332 | ->onAuthenticated(devId, msg->data.authenticated.finger.fid, |
| 333 | msg->data.authenticated.finger.gid, hidl_vec<uint8_t>()) |
| 334 | .isOk()) { |
| 335 | LOG(ERROR) << "failed to invoke fingerprint onAuthenticated callback"; |
| 336 | } |
| 337 | } |
| 338 | break; |
| 339 | case FINGERPRINT_TEMPLATE_ENUMERATING: |
| 340 | LOG(DEBUG) << "onEnumerate(fid=" << msg->data.enumerated.finger.fid |
| 341 | << ", gid=" << msg->data.enumerated.finger.gid |
| 342 | << ", rem=" << msg->data.enumerated.remaining_templates << ")"; |
| 343 | if (!thisPtr->mClientCallback |
| 344 | ->onEnumerate(devId, msg->data.enumerated.finger.fid, |
| 345 | msg->data.enumerated.finger.gid, |
| 346 | msg->data.enumerated.remaining_templates) |
| 347 | .isOk()) { |
| 348 | LOG(ERROR) << "failed to invoke fingerprint onEnumerate callback"; |
| 349 | } |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | } // namespace implementation |
| 355 | } // namespace V2_1 |
| 356 | } // namespace fingerprint |
| 357 | } // namespace biometrics |
| 358 | } // namespace hardware |
| 359 | } // namespace android |