blob: 154b7a66b4aae1a30799f57ee234ad6d500eace6 [file] [log] [blame]
Sasha Levitskiy965bd322016-10-21 10:55:25 -07001/*
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 Miller902de512016-12-15 19:42:19 -080016#define LOG_TAG "android.hardware.biometrics.fingerprint@2.1-service"
Jim Miller5f26c1a2017-01-19 18:39:06 -080017#define LOG_VERBOSE "android.hardware.biometrics.fingerprint@2.1-service"
Sasha Levitskiy965bd322016-10-21 10:55:25 -070018
Jim Miller7f7eb472017-01-18 21:03:45 -080019// For communication with Keystore binder interface
Jim Miller7f7eb472017-01-18 21:03:45 -080020#include <keystore/keystore.h> // for error codes
21#include <hardware/hw_auth_token.h>
22
Sasha Levitskiy965bd322016-10-21 10:55:25 -070023#include <hardware/hardware.h>
24#include <hardware/fingerprint.h>
25#include "BiometricsFingerprint.h"
26
Janis Danisevskisa47afa92017-01-20 13:50:10 +000027#include <inttypes.h>
28
Sasha Levitskiy965bd322016-10-21 10:55:25 -070029namespace android {
30namespace hardware {
31namespace biometrics {
32namespace fingerprint {
33namespace V2_1 {
34namespace implementation {
35
Jim Miller902de512016-12-15 19:42:19 -080036// Supported fingerprint HAL version
37static const uint16_t kVersion = HARDWARE_MODULE_API_VERSION(2, 1);
38
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080039using RequestStatus =
40 android::hardware::biometrics::fingerprint::V2_1::RequestStatus;
41
Jim Miller5f26c1a2017-01-19 18:39:06 -080042BiometricsFingerprint *BiometricsFingerprint::sInstance = nullptr;
Sasha Levitskiy965bd322016-10-21 10:55:25 -070043
Jim Miller5f26c1a2017-01-19 18:39:06 -080044BiometricsFingerprint::BiometricsFingerprint() : mClientCallback(nullptr), mDevice(nullptr) {
45 sInstance = this; // keep track of the most recent instance
46 mDevice = openHal();
47 if (!mDevice) {
48 ALOGE("Can't open HAL module");
49 }
Jim Miller902de512016-12-15 19:42:19 -080050}
Sasha Levitskiy965bd322016-10-21 10:55:25 -070051
52BiometricsFingerprint::~BiometricsFingerprint() {
Jim Miller5f26c1a2017-01-19 18:39:06 -080053 ALOGV(LOG_VERBOSE, LOG_TAG, "~BiometricsFingerprint()\n");
54 if (mDevice == nullptr) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -070055 ALOGE("No valid device");
56 return;
57 }
58 int err;
59 if (0 != (err = mDevice->common.close(
60 reinterpret_cast<hw_device_t*>(mDevice)))) {
61 ALOGE("Can't close fingerprint module, error: %d", err);
62 return;
63 }
Jim Miller5f26c1a2017-01-19 18:39:06 -080064 mDevice = nullptr;
Sasha Levitskiy965bd322016-10-21 10:55:25 -070065}
66
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080067Return<RequestStatus> BiometricsFingerprint::ErrorFilter(int32_t error) {
68 switch(error) {
69 case 0: return RequestStatus::SYS_OK;
70 case -2: return RequestStatus::SYS_ENOENT;
71 case -4: return RequestStatus::SYS_EINTR;
72 case -5: return RequestStatus::SYS_EIO;
73 case -11: return RequestStatus::SYS_EAGAIN;
74 case -12: return RequestStatus::SYS_ENOMEM;
75 case -13: return RequestStatus::SYS_EACCES;
76 case -14: return RequestStatus::SYS_EFAULT;
77 case -16: return RequestStatus::SYS_EBUSY;
78 case -22: return RequestStatus::SYS_EINVAL;
79 case -28: return RequestStatus::SYS_ENOSPC;
80 case -110: return RequestStatus::SYS_ETIMEDOUT;
81 default:
82 ALOGE("An unknown error returned from fingerprint vendor library");
83 return RequestStatus::SYS_UNKNOWN;
84 }
85}
86
Jim Miller902de512016-12-15 19:42:19 -080087// Translate from errors returned by traditional HAL (see fingerprint.h) to
88// HIDL-compliant FingerprintError.
89FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error,
90 int32_t* vendorCode) {
91 *vendorCode = 0;
92 switch(error) {
93 case FINGERPRINT_ERROR_HW_UNAVAILABLE:
94 return FingerprintError::ERROR_HW_UNAVAILABLE;
95 case FINGERPRINT_ERROR_UNABLE_TO_PROCESS:
96 return FingerprintError::ERROR_UNABLE_TO_PROCESS;
97 case FINGERPRINT_ERROR_TIMEOUT:
98 return FingerprintError::ERROR_TIMEOUT;
99 case FINGERPRINT_ERROR_NO_SPACE:
100 return FingerprintError::ERROR_NO_SPACE;
101 case FINGERPRINT_ERROR_CANCELED:
102 return FingerprintError::ERROR_CANCELED;
103 case FINGERPRINT_ERROR_UNABLE_TO_REMOVE:
104 return FingerprintError::ERROR_UNABLE_TO_REMOVE;
Jim Miller5f26c1a2017-01-19 18:39:06 -0800105 case FINGERPRINT_ERROR_LOCKOUT:
106 return FingerprintError::ERROR_LOCKOUT;
Jim Miller902de512016-12-15 19:42:19 -0800107 default:
108 if (error >= FINGERPRINT_ERROR_VENDOR_BASE) {
109 // vendor specific code.
110 *vendorCode = error - FINGERPRINT_ERROR_VENDOR_BASE;
111 return FingerprintError::ERROR_VENDOR;
112 }
113 }
Jim Miller5f26c1a2017-01-19 18:39:06 -0800114 ALOGE("Unknown error from fingerprint vendor library: %d", error);
Jim Miller902de512016-12-15 19:42:19 -0800115 return FingerprintError::ERROR_UNABLE_TO_PROCESS;
116}
117
118// Translate acquired messages returned by traditional HAL (see fingerprint.h)
119// to HIDL-compliant FingerprintAcquiredInfo.
120FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter(
121 int32_t info, int32_t* vendorCode) {
122 *vendorCode = 0;
123 switch(info) {
124 case FINGERPRINT_ACQUIRED_GOOD:
125 return FingerprintAcquiredInfo::ACQUIRED_GOOD;
126 case FINGERPRINT_ACQUIRED_PARTIAL:
127 return FingerprintAcquiredInfo::ACQUIRED_PARTIAL;
128 case FINGERPRINT_ACQUIRED_INSUFFICIENT:
129 return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT;
130 case FINGERPRINT_ACQUIRED_IMAGER_DIRTY:
131 return FingerprintAcquiredInfo::ACQUIRED_IMAGER_DIRTY;
132 case FINGERPRINT_ACQUIRED_TOO_SLOW:
133 return FingerprintAcquiredInfo::ACQUIRED_TOO_SLOW;
134 case FINGERPRINT_ACQUIRED_TOO_FAST:
135 return FingerprintAcquiredInfo::ACQUIRED_TOO_FAST;
136 default:
137 if (info >= FINGERPRINT_ACQUIRED_VENDOR_BASE) {
138 // vendor specific code.
139 *vendorCode = info - FINGERPRINT_ACQUIRED_VENDOR_BASE;
140 return FingerprintAcquiredInfo::ACQUIRED_VENDOR;
141 }
142 }
Jim Miller5f26c1a2017-01-19 18:39:06 -0800143 ALOGE("Unknown acquiredmsg from fingerprint vendor library: %d", info);
Jim Miller902de512016-12-15 19:42:19 -0800144 return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT;
145}
146
147Return<uint64_t> BiometricsFingerprint::setNotify(
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800148 const sp<IBiometricsFingerprintClientCallback>& clientCallback) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700149 mClientCallback = clientCallback;
Jim Miller5f26c1a2017-01-19 18:39:06 -0800150 // This is here because HAL 2.1 doesn't have a way to propagate a
151 // unique token for its driver. Subsequent versions should send a unique
152 // token for each call to setNotify(). This is fine as long as there's only
153 // one fingerprint device on the platform.
Jim Miller902de512016-12-15 19:42:19 -0800154 return reinterpret_cast<uint64_t>(mDevice);
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700155}
156
157Return<uint64_t> BiometricsFingerprint::preEnroll() {
158 return mDevice->pre_enroll(mDevice);
159}
160
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800161Return<RequestStatus> BiometricsFingerprint::enroll(const hidl_array<uint8_t, 69>& hat,
162 uint32_t gid, uint32_t timeoutSec) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700163 const hw_auth_token_t* authToken =
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800164 reinterpret_cast<const hw_auth_token_t*>(hat.data());
165 return ErrorFilter(mDevice->enroll(mDevice, authToken, gid, timeoutSec));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700166}
167
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800168Return<RequestStatus> BiometricsFingerprint::postEnroll() {
169 return ErrorFilter(mDevice->post_enroll(mDevice));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700170}
171
172Return<uint64_t> BiometricsFingerprint::getAuthenticatorId() {
173 return mDevice->get_authenticator_id(mDevice);
174}
175
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800176Return<RequestStatus> BiometricsFingerprint::cancel() {
177 return ErrorFilter(mDevice->cancel(mDevice));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700178}
179
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800180Return<RequestStatus> BiometricsFingerprint::enumerate() {
181 return ErrorFilter(mDevice->enumerate(mDevice));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700182}
183
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800184Return<RequestStatus> BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) {
185 return ErrorFilter(mDevice->remove(mDevice, gid, fid));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700186}
187
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800188Return<RequestStatus> BiometricsFingerprint::setActiveGroup(uint32_t gid,
189 const hidl_string& storePath) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700190 if (storePath.size() >= PATH_MAX || storePath.size() <= 0) {
191 ALOGE("Bad path length: %zd", storePath.size());
192 }
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800193 return ErrorFilter(mDevice->set_active_group(mDevice, gid,
194 storePath.c_str()));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700195}
196
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800197Return<RequestStatus> BiometricsFingerprint::authenticate(uint64_t operationId,
198 uint32_t gid) {
199 return ErrorFilter(mDevice->authenticate(mDevice, operationId, gid));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700200}
201
Jim Miller902de512016-12-15 19:42:19 -0800202IBiometricsFingerprint* BiometricsFingerprint::getInstance() {
Jim Miller5f26c1a2017-01-19 18:39:06 -0800203 if (!sInstance) {
204 sInstance = new BiometricsFingerprint();
205 }
206 return sInstance;
207}
208
209fingerprint_device_t* BiometricsFingerprint::openHal() {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700210 int err;
Jim Miller5f26c1a2017-01-19 18:39:06 -0800211 const hw_module_t *hw_mdl = nullptr;
212 ALOGD("Opening fingerprint hal library...");
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700213 if (0 != (err = hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_mdl))) {
214 ALOGE("Can't open fingerprint HW Module, error: %d", err);
215 return nullptr;
216 }
Jim Miller902de512016-12-15 19:42:19 -0800217
Jim Miller5f26c1a2017-01-19 18:39:06 -0800218 if (hw_mdl == nullptr) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700219 ALOGE("No valid fingerprint module");
220 return nullptr;
221 }
222
223 fingerprint_module_t const *module =
224 reinterpret_cast<const fingerprint_module_t*>(hw_mdl);
Jim Miller5f26c1a2017-01-19 18:39:06 -0800225 if (module->common.methods->open == nullptr) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700226 ALOGE("No valid open method");
227 return nullptr;
228 }
229
Jim Miller5f26c1a2017-01-19 18:39:06 -0800230 hw_device_t *device = nullptr;
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700231
Jim Miller5f26c1a2017-01-19 18:39:06 -0800232 if (0 != (err = module->common.methods->open(hw_mdl, nullptr, &device))) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700233 ALOGE("Can't open fingerprint methods, error: %d", err);
234 return nullptr;
235 }
236
Jim Miller902de512016-12-15 19:42:19 -0800237 if (kVersion != device->version) {
Jim Miller5f26c1a2017-01-19 18:39:06 -0800238 // enforce version on new devices because of HIDL@2.1 translation layer
Jim Miller902de512016-12-15 19:42:19 -0800239 ALOGE("Wrong fp version. Expected %d, got %d", kVersion, device->version);
Jim Miller5f26c1a2017-01-19 18:39:06 -0800240 return nullptr;
Jim Miller902de512016-12-15 19:42:19 -0800241 }
242
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800243 fingerprint_device_t* fp_device =
244 reinterpret_cast<fingerprint_device_t*>(device);
245
246 if (0 != (err =
247 fp_device->set_notify(fp_device, BiometricsFingerprint::notify))) {
248 ALOGE("Can't register fingerprint module callback, error: %d", err);
249 return nullptr;
250 }
251
Jim Miller5f26c1a2017-01-19 18:39:06 -0800252 return fp_device;
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700253}
254
Jim Miller5f26c1a2017-01-19 18:39:06 -0800255void BiometricsFingerprint::notify(const fingerprint_msg_t *msg) {
256 BiometricsFingerprint* thisPtr = static_cast<BiometricsFingerprint*>(
257 BiometricsFingerprint::getInstance());
258 if (thisPtr == nullptr || thisPtr->mClientCallback == nullptr) {
259 ALOGE("Receiving callbacks before the client callback is registered.");
260 return;
261 }
262 const uint64_t devId = reinterpret_cast<uint64_t>(thisPtr->mDevice);
263 switch (msg->type) {
264 case FINGERPRINT_ERROR: {
265 int32_t vendorCode = 0;
266 FingerprintError result = VendorErrorFilter(msg->data.error, &vendorCode);
267 thisPtr->mClientCallback->onError(devId, result, vendorCode);
Jim Miller7f7eb472017-01-18 21:03:45 -0800268 }
Jim Miller5f26c1a2017-01-19 18:39:06 -0800269 break;
270 case FINGERPRINT_ACQUIRED: {
271 int32_t vendorCode = 0;
272 FingerprintAcquiredInfo result =
273 VendorAcquiredFilter(msg->data.acquired.acquired_info, &vendorCode);
274 thisPtr->mClientCallback->onAcquired(devId, result, vendorCode);
275 }
276 break;
277 case FINGERPRINT_TEMPLATE_ENROLLING:
278 thisPtr->mClientCallback->onEnrollResult(devId,
279 msg->data.enroll.finger.fid,
280 msg->data.enroll.finger.gid,
281 msg->data.enroll.samples_remaining);
282 break;
283 case FINGERPRINT_TEMPLATE_REMOVED:
284 thisPtr->mClientCallback->onRemoved(devId,
285 msg->data.removed.finger.fid,
286 msg->data.removed.finger.gid,
287 msg->data.removed.remaining_templates);
288 break;
289 case FINGERPRINT_AUTHENTICATED:
290 if (msg->data.authenticated.finger.fid != 0) {
291 const uint8_t* hat =
292 reinterpret_cast<const uint8_t *>(&msg->data.authenticated.hat);
293 const hidl_vec<uint8_t> token(
294 std::vector<uint8_t>(hat, hat + sizeof(msg->data.authenticated.hat)));
295 thisPtr->mClientCallback->onAuthenticated(devId,
296 msg->data.authenticated.finger.fid,
297 msg->data.authenticated.finger.gid,
298 token);
299 } else {
300 // Not a recognized fingerprint
301 thisPtr->mClientCallback->onAuthenticated(devId,
302 msg->data.authenticated.finger.fid,
303 msg->data.authenticated.finger.gid,
304 hidl_vec<uint8_t>());
305 }
306 break;
307 case FINGERPRINT_TEMPLATE_ENUMERATING:
308 thisPtr->mClientCallback->onEnumerate(devId,
309 msg->data.enumerated.finger.fid,
310 msg->data.enumerated.finger.gid,
311 msg->data.enumerated.remaining_templates);
312 break;
Jim Miller7f7eb472017-01-18 21:03:45 -0800313 }
314}
315
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700316} // namespace implementation
317} // namespace V2_1
318} // namespace fingerprint
319} // namespace biometrics
320} // namespace hardware
321} // namespace android