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 | */ |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 16 | #define LOG_TAG "android.hardware.biometrics.fingerprint@2.1-impl" |
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 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 29 | using RequestStatus = |
| 30 | android::hardware::biometrics::fingerprint::V2_1::RequestStatus; |
| 31 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 32 | sp<IBiometricsFingerprintClientCallback> |
| 33 | BiometricsFingerprint::mClientCallback = nullptr; |
| 34 | |
| 35 | BiometricsFingerprint::BiometricsFingerprint(fingerprint_device_t *device) |
| 36 | : mDevice(device) {} |
| 37 | |
| 38 | BiometricsFingerprint::~BiometricsFingerprint() { |
| 39 | ALOG(LOG_VERBOSE, LOG_TAG, "nativeCloseHal()\n"); |
| 40 | if (mDevice == NULL) { |
| 41 | ALOGE("No valid device"); |
| 42 | return; |
| 43 | } |
| 44 | int err; |
| 45 | if (0 != (err = mDevice->common.close( |
| 46 | reinterpret_cast<hw_device_t*>(mDevice)))) { |
| 47 | ALOGE("Can't close fingerprint module, error: %d", err); |
| 48 | return; |
| 49 | } |
| 50 | mDevice = NULL; |
| 51 | } |
| 52 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 53 | Return<RequestStatus> BiometricsFingerprint::ErrorFilter(int32_t error) { |
| 54 | switch(error) { |
| 55 | case 0: return RequestStatus::SYS_OK; |
| 56 | case -2: return RequestStatus::SYS_ENOENT; |
| 57 | case -4: return RequestStatus::SYS_EINTR; |
| 58 | case -5: return RequestStatus::SYS_EIO; |
| 59 | case -11: return RequestStatus::SYS_EAGAIN; |
| 60 | case -12: return RequestStatus::SYS_ENOMEM; |
| 61 | case -13: return RequestStatus::SYS_EACCES; |
| 62 | case -14: return RequestStatus::SYS_EFAULT; |
| 63 | case -16: return RequestStatus::SYS_EBUSY; |
| 64 | case -22: return RequestStatus::SYS_EINVAL; |
| 65 | case -28: return RequestStatus::SYS_ENOSPC; |
| 66 | case -110: return RequestStatus::SYS_ETIMEDOUT; |
| 67 | default: |
| 68 | ALOGE("An unknown error returned from fingerprint vendor library"); |
| 69 | return RequestStatus::SYS_UNKNOWN; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Return<RequestStatus> BiometricsFingerprint::setNotify( |
| 74 | const sp<IBiometricsFingerprintClientCallback>& clientCallback) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 75 | mClientCallback = clientCallback; |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 76 | return RequestStatus::SYS_OK; |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Return<uint64_t> BiometricsFingerprint::preEnroll() { |
| 80 | return mDevice->pre_enroll(mDevice); |
| 81 | } |
| 82 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 83 | Return<RequestStatus> BiometricsFingerprint::enroll(const hidl_array<uint8_t, 69>& hat, |
| 84 | uint32_t gid, uint32_t timeoutSec) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 85 | const hw_auth_token_t* authToken = |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 86 | reinterpret_cast<const hw_auth_token_t*>(hat.data()); |
| 87 | return ErrorFilter(mDevice->enroll(mDevice, authToken, gid, timeoutSec)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 90 | Return<RequestStatus> BiometricsFingerprint::postEnroll() { |
| 91 | return ErrorFilter(mDevice->post_enroll(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Return<uint64_t> BiometricsFingerprint::getAuthenticatorId() { |
| 95 | return mDevice->get_authenticator_id(mDevice); |
| 96 | } |
| 97 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 98 | Return<RequestStatus> BiometricsFingerprint::cancel() { |
| 99 | return ErrorFilter(mDevice->cancel(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 102 | Return<RequestStatus> BiometricsFingerprint::enumerate() { |
| 103 | return ErrorFilter(mDevice->enumerate(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 106 | Return<RequestStatus> BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) { |
| 107 | return ErrorFilter(mDevice->remove(mDevice, gid, fid)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 110 | Return<RequestStatus> BiometricsFingerprint::setActiveGroup(uint32_t gid, |
| 111 | const hidl_string& storePath) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 112 | if (storePath.size() >= PATH_MAX || storePath.size() <= 0) { |
| 113 | ALOGE("Bad path length: %zd", storePath.size()); |
| 114 | } |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 115 | return ErrorFilter(mDevice->set_active_group(mDevice, gid, |
| 116 | storePath.c_str())); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 119 | Return<RequestStatus> BiometricsFingerprint::authenticate(uint64_t operationId, |
| 120 | uint32_t gid) { |
| 121 | return ErrorFilter(mDevice->authenticate(mDevice, operationId, gid)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | IBiometricsFingerprint* HIDL_FETCH_IBiometricsFingerprint(const char*) { |
| 125 | int err; |
| 126 | const hw_module_t *hw_mdl = NULL; |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 127 | ALOGE("Opening fingerprint hal library..."); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 128 | if (0 != (err = hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_mdl))) { |
| 129 | ALOGE("Can't open fingerprint HW Module, error: %d", err); |
| 130 | return nullptr; |
| 131 | } |
| 132 | if (hw_mdl == NULL) { |
| 133 | ALOGE("No valid fingerprint module"); |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
| 137 | fingerprint_module_t const *module = |
| 138 | reinterpret_cast<const fingerprint_module_t*>(hw_mdl); |
| 139 | if (module->common.methods->open == NULL) { |
| 140 | ALOGE("No valid open method"); |
| 141 | return nullptr; |
| 142 | } |
| 143 | |
| 144 | hw_device_t *device = NULL; |
| 145 | |
| 146 | if (0 != (err = module->common.methods->open(hw_mdl, NULL, &device))) { |
| 147 | ALOGE("Can't open fingerprint methods, error: %d", err); |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 151 | fingerprint_device_t* fp_device = |
| 152 | reinterpret_cast<fingerprint_device_t*>(device); |
| 153 | |
| 154 | if (0 != (err = |
| 155 | fp_device->set_notify(fp_device, BiometricsFingerprint::notify))) { |
| 156 | ALOGE("Can't register fingerprint module callback, error: %d", err); |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
| 160 | return new BiometricsFingerprint(fp_device); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace implementation |
| 164 | } // namespace V2_1 |
| 165 | } // namespace fingerprint |
| 166 | } // namespace biometrics |
| 167 | } // namespace hardware |
| 168 | } // namespace android |