blob: 13166862d60444407f323568a9937d876e81cd7c [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 */
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080016#define LOG_TAG "android.hardware.biometrics.fingerprint@2.1-impl"
Sasha Levitskiy965bd322016-10-21 10:55:25 -070017
18#include <hardware/hardware.h>
19#include <hardware/fingerprint.h>
20#include "BiometricsFingerprint.h"
21
22namespace android {
23namespace hardware {
24namespace biometrics {
25namespace fingerprint {
26namespace V2_1 {
27namespace implementation {
28
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080029using RequestStatus =
30 android::hardware::biometrics::fingerprint::V2_1::RequestStatus;
31
Sasha Levitskiy965bd322016-10-21 10:55:25 -070032sp<IBiometricsFingerprintClientCallback>
33 BiometricsFingerprint::mClientCallback = nullptr;
34
35BiometricsFingerprint::BiometricsFingerprint(fingerprint_device_t *device)
36 : mDevice(device) {}
37
38BiometricsFingerprint::~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 Levitskiy52640ee2016-11-10 14:15:33 -080053Return<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
73Return<RequestStatus> BiometricsFingerprint::setNotify(
74 const sp<IBiometricsFingerprintClientCallback>& clientCallback) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -070075 mClientCallback = clientCallback;
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080076 return RequestStatus::SYS_OK;
Sasha Levitskiy965bd322016-10-21 10:55:25 -070077}
78
79Return<uint64_t> BiometricsFingerprint::preEnroll() {
80 return mDevice->pre_enroll(mDevice);
81}
82
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080083Return<RequestStatus> BiometricsFingerprint::enroll(const hidl_array<uint8_t, 69>& hat,
84 uint32_t gid, uint32_t timeoutSec) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -070085 const hw_auth_token_t* authToken =
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080086 reinterpret_cast<const hw_auth_token_t*>(hat.data());
87 return ErrorFilter(mDevice->enroll(mDevice, authToken, gid, timeoutSec));
Sasha Levitskiy965bd322016-10-21 10:55:25 -070088}
89
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080090Return<RequestStatus> BiometricsFingerprint::postEnroll() {
91 return ErrorFilter(mDevice->post_enroll(mDevice));
Sasha Levitskiy965bd322016-10-21 10:55:25 -070092}
93
94Return<uint64_t> BiometricsFingerprint::getAuthenticatorId() {
95 return mDevice->get_authenticator_id(mDevice);
96}
97
Sasha Levitskiy52640ee2016-11-10 14:15:33 -080098Return<RequestStatus> BiometricsFingerprint::cancel() {
99 return ErrorFilter(mDevice->cancel(mDevice));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700100}
101
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800102Return<RequestStatus> BiometricsFingerprint::enumerate() {
103 return ErrorFilter(mDevice->enumerate(mDevice));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700104}
105
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800106Return<RequestStatus> BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) {
107 return ErrorFilter(mDevice->remove(mDevice, gid, fid));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700108}
109
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800110Return<RequestStatus> BiometricsFingerprint::setActiveGroup(uint32_t gid,
111 const hidl_string& storePath) {
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700112 if (storePath.size() >= PATH_MAX || storePath.size() <= 0) {
113 ALOGE("Bad path length: %zd", storePath.size());
114 }
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800115 return ErrorFilter(mDevice->set_active_group(mDevice, gid,
116 storePath.c_str()));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700117}
118
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800119Return<RequestStatus> BiometricsFingerprint::authenticate(uint64_t operationId,
120 uint32_t gid) {
121 return ErrorFilter(mDevice->authenticate(mDevice, operationId, gid));
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700122}
123
124IBiometricsFingerprint* HIDL_FETCH_IBiometricsFingerprint(const char*) {
125 int err;
126 const hw_module_t *hw_mdl = NULL;
Sasha Levitskiy52640ee2016-11-10 14:15:33 -0800127 ALOGE("Opening fingerprint hal library...");
Sasha Levitskiy965bd322016-10-21 10:55:25 -0700128 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 Levitskiy52640ee2016-11-10 14:15:33 -0800151 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 Levitskiy965bd322016-10-21 10:55:25 -0700161}
162
163} // namespace implementation
164} // namespace V2_1
165} // namespace fingerprint
166} // namespace biometrics
167} // namespace hardware
168} // namespace android