blob: 5145de63938b0bd6fca498516a0083a772b30551 [file] [log] [blame]
Jesse Chanb23aaf72017-02-05 21:18:09 +08001/*
2 * Copyright (C) 2017 Jesse Chan, 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
17#define LOG_TAG "SS Fingerprint HAL"
18
19#include <dlfcn.h>
20#include <stdlib.h>
21#include <errno.h>
Lukas0610b0b78342018-03-07 13:04:55 +010022#include <string.h>
Jesse Chanb23aaf72017-02-05 21:18:09 +080023
24#include <cutils/log.h>
25
26#include <hardware/hardware.h>
27#include <hardware/fingerprint.h>
28#include <utils/threads.h>
29
30typedef struct {
31 void *libhandle;
32 int (*ss_fingerprint_close)(void);
33 uint64_t (*ss_fingerprint_pre_enroll)(void);
34 int (*ss_fingerprint_enroll)(const hw_auth_token_t *hat, uint32_t gid, uint32_t timeout_sec);
35 int (*ss_fingerprint_post_enroll)(void);
36 uint64_t (*ss_fingerprint_get_auth_id)(void);
37 int (*ss_fingerprint_remove)(uint32_t gid, uint32_t fid);
38 int (*ss_fingerprint_set_active_group)(uint32_t gid, const char *store_path);
39 int (*ss_fingerprint_authenticate)(uint64_t operation_id, uint32_t gid);
40 int (*ss_set_notify_callback)(fingerprint_notify_t notify);
41 int (*ss_fingerprint_cancel)(void);
42 int (*ss_fingerprint_open)(const char *id);
43} bauth_server_handle_t;
44
45bauth_server_handle_t* bauth_handle;
46
Jesse Chancd60dd82017-02-05 18:53:42 +080047static fingerprint_notify_t original_notify;
48
Jesse Chanb23aaf72017-02-05 21:18:09 +080049static int load_bauth_server(void)
50{
51 bauth_handle = (bauth_server_handle_t *)malloc(sizeof(bauth_server_handle_t));
52 if (bauth_handle == NULL)
53 goto no_memory;
54
55 void *libhandle = bauth_handle->libhandle;
56
57 libhandle = dlopen("libbauthserver.so", RTLD_NOW);
58 bauth_handle->ss_fingerprint_close = dlsym(libhandle, "ss_fingerprint_close");
59 bauth_handle->ss_fingerprint_pre_enroll = dlsym(libhandle, "ss_fingerprint_pre_enroll");
60 bauth_handle->ss_fingerprint_enroll = dlsym(libhandle, "ss_fingerprint_enroll");
61 bauth_handle->ss_fingerprint_post_enroll = dlsym(libhandle, "ss_fingerprint_post_enroll");
62 bauth_handle->ss_fingerprint_get_auth_id = dlsym(libhandle, "ss_fingerprint_get_auth_id");
63 bauth_handle->ss_fingerprint_remove = dlsym(libhandle, "ss_fingerprint_remove");
64 bauth_handle->ss_fingerprint_set_active_group = dlsym(libhandle, "ss_fingerprint_set_active_group");
65 bauth_handle->ss_fingerprint_authenticate = dlsym(libhandle, "ss_fingerprint_authenticate");
66 bauth_handle->ss_set_notify_callback = dlsym(libhandle, "ss_set_notify_callback");
67 bauth_handle->ss_fingerprint_cancel = dlsym(libhandle, "ss_fingerprint_cancel");
68 bauth_handle->ss_fingerprint_open = dlsym(libhandle, "ss_fingerprint_open");
69
70 return 0;
71
72no_memory:
73 ALOGE("%s: not enough memory to load the libhandle", __func__);
74 return -ENOMEM;
75}
76
Jesse Chancd60dd82017-02-05 18:53:42 +080077static void hal_notify_convert(const fingerprint_msg_t *msg)
78{
79 fingerprint_msg_t *new_msg = (fingerprint_msg_t *)msg;
80
81 switch (msg->type) {
82 case FINGERPRINT_TEMPLATE_ENROLLING:
83 new_msg->data.enroll.samples_remaining = 100 - msg->data.enroll.samples_remaining;
84 break;
85
86 default:
87 break;
88 }
89
90 return original_notify(new_msg);
91}
92
Jesse Chanb23aaf72017-02-05 21:18:09 +080093static int fingerprint_close(hw_device_t *dev)
94{
95 bauth_handle->ss_fingerprint_close();
96
97 if (dev)
98 free(dev);
99
100 return 0;
101}
102
103static uint64_t fingerprint_pre_enroll(struct fingerprint_device __unused *dev)
104{
105 return bauth_handle->ss_fingerprint_pre_enroll();
106}
107
108static int fingerprint_enroll(struct fingerprint_device __unused *dev, const hw_auth_token_t *hat,
109 uint32_t gid, uint32_t timeout_sec)
110{
111 return bauth_handle->ss_fingerprint_enroll(hat, gid, timeout_sec);
112}
113
114static int fingerprint_post_enroll(struct fingerprint_device __unused *dev)
115{
116 return bauth_handle->ss_fingerprint_post_enroll();
117}
118
119static uint64_t fingerprint_get_auth_id(struct fingerprint_device __unused *dev)
120{
121 return bauth_handle->ss_fingerprint_get_auth_id();
122}
123
124static int fingerprint_cancel(struct fingerprint_device __unused *dev)
125{
Lukas0610b0b78342018-03-07 13:04:55 +0100126 fingerprint_msg_t *cancel_msg;
127 int ret = 0;
128
129 ret = bauth_handle->ss_fingerprint_cancel();
130
131 cancel_msg = (fingerprint_msg_t *)malloc(sizeof(fingerprint_msg_t));
132 memset(cancel_msg, 0, sizeof(fingerprint_msg_t));
133
134 cancel_msg->type = FINGERPRINT_ERROR;
135 cancel_msg->data.error = FINGERPRINT_ERROR_CANCELED;
136
137 original_notify(cancel_msg);
138 return ret;
Jesse Chanb23aaf72017-02-05 21:18:09 +0800139}
140
141static int fingerprint_remove(struct fingerprint_device __unused *dev, uint32_t gid, uint32_t fid)
142{
143 return bauth_handle->ss_fingerprint_remove(gid, fid);
144}
145
146static int fingerprint_set_active_group(struct fingerprint_device __unused *dev, uint32_t gid,
147 const char *store_path)
148{
149 return bauth_handle->ss_fingerprint_set_active_group(gid, store_path);
150}
151
152static int fingerprint_authenticate(struct fingerprint_device __unused *dev, uint64_t operation_id,
153 uint32_t gid)
154{
155 return bauth_handle->ss_fingerprint_authenticate(operation_id, gid);
156}
157
158static int set_notify_callback(struct fingerprint_device *dev, fingerprint_notify_t notify)
159{
160 /* Decorate with locks */
161 dev->notify = notify;
Jesse Chancd60dd82017-02-05 18:53:42 +0800162 original_notify = notify;
163 return bauth_handle->ss_set_notify_callback(hal_notify_convert);
Jesse Chanb23aaf72017-02-05 21:18:09 +0800164}
165
166static int fingerprint_open(const hw_module_t* module, const char *id, hw_device_t** device)
167{
168 int ret;
169 fingerprint_device_t *dev = NULL;
170
171 if (device == NULL) {
172 ALOGE("NULL device on open");
173 ret = -ENODEV;
174 goto fail;
175 }
176
177 ret = load_bauth_server();
178 if (ret < 0)
179 goto fail;
180
181 dev = (fingerprint_device_t*)calloc(1, sizeof(fingerprint_device_t));
182 if (dev == NULL) {
183 ret = -ENOMEM;
184 goto fail;
185 }
186
187 dev->common.tag = HARDWARE_DEVICE_TAG;
LuK1337bdb254d2017-08-28 22:22:42 +0200188 dev->common.version = FINGERPRINT_MODULE_API_VERSION_2_1;
Jesse Chanb23aaf72017-02-05 21:18:09 +0800189 dev->common.module = (struct hw_module_t*) module;
190 dev->common.close = fingerprint_close;
191
192 dev->pre_enroll = fingerprint_pre_enroll;
193 dev->enroll = fingerprint_enroll;
194 dev->post_enroll = fingerprint_post_enroll;
195 dev->get_authenticator_id = fingerprint_get_auth_id;
196 dev->cancel = fingerprint_cancel;
197 dev->remove = fingerprint_remove;
198 dev->set_active_group = fingerprint_set_active_group;
199 dev->authenticate = fingerprint_authenticate;
200 dev->set_notify = set_notify_callback;
201 dev->notify = NULL;
202
203 *device = (hw_device_t*) dev;
204 return (*bauth_handle->ss_fingerprint_open)(id);
205
206fail:
207 ALOGE("%s: failed to open FP device (ret=%d)", __func__, ret);
208 return ret;
209}
210
211static struct hw_module_methods_t fingerprint_module_methods = {
212 .open = fingerprint_open,
213};
214
215fingerprint_module_t HAL_MODULE_INFO_SYM = {
216 .common = {
217 .tag = HARDWARE_MODULE_TAG,
LuK1337bdb254d2017-08-28 22:22:42 +0200218 .module_api_version = FINGERPRINT_MODULE_API_VERSION_2_1,
Jesse Chanb23aaf72017-02-05 21:18:09 +0800219 .hal_api_version = HARDWARE_HAL_API_VERSION,
220 .id = FINGERPRINT_HARDWARE_MODULE_ID,
221 .name = "Samsung TZ Fingerprint HAL",
222 .author = "The LineageOS Project",
223 .methods = &fingerprint_module_methods,
224 },
225};