blob: 722460922d9fab1b7b2bbfb9880e6d5fa1e5ce4f [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>
22
23#include <cutils/log.h>
24
25#include <hardware/hardware.h>
26#include <hardware/fingerprint.h>
27#include <utils/threads.h>
28
29typedef struct {
30 void *libhandle;
31 int (*ss_fingerprint_close)(void);
32 uint64_t (*ss_fingerprint_pre_enroll)(void);
33 int (*ss_fingerprint_enroll)(const hw_auth_token_t *hat, uint32_t gid, uint32_t timeout_sec);
34 int (*ss_fingerprint_post_enroll)(void);
35 uint64_t (*ss_fingerprint_get_auth_id)(void);
36 int (*ss_fingerprint_remove)(uint32_t gid, uint32_t fid);
37 int (*ss_fingerprint_set_active_group)(uint32_t gid, const char *store_path);
38 int (*ss_fingerprint_authenticate)(uint64_t operation_id, uint32_t gid);
39 int (*ss_set_notify_callback)(fingerprint_notify_t notify);
40 int (*ss_fingerprint_cancel)(void);
41 int (*ss_fingerprint_open)(const char *id);
42} bauth_server_handle_t;
43
44bauth_server_handle_t* bauth_handle;
45
Jesse Chancd60dd82017-02-05 18:53:42 +080046static fingerprint_notify_t original_notify;
47
Jesse Chanb23aaf72017-02-05 21:18:09 +080048static int load_bauth_server(void)
49{
50 bauth_handle = (bauth_server_handle_t *)malloc(sizeof(bauth_server_handle_t));
51 if (bauth_handle == NULL)
52 goto no_memory;
53
54 void *libhandle = bauth_handle->libhandle;
55
56 libhandle = dlopen("libbauthserver.so", RTLD_NOW);
57 bauth_handle->ss_fingerprint_close = dlsym(libhandle, "ss_fingerprint_close");
58 bauth_handle->ss_fingerprint_pre_enroll = dlsym(libhandle, "ss_fingerprint_pre_enroll");
59 bauth_handle->ss_fingerprint_enroll = dlsym(libhandle, "ss_fingerprint_enroll");
60 bauth_handle->ss_fingerprint_post_enroll = dlsym(libhandle, "ss_fingerprint_post_enroll");
61 bauth_handle->ss_fingerprint_get_auth_id = dlsym(libhandle, "ss_fingerprint_get_auth_id");
62 bauth_handle->ss_fingerprint_remove = dlsym(libhandle, "ss_fingerprint_remove");
63 bauth_handle->ss_fingerprint_set_active_group = dlsym(libhandle, "ss_fingerprint_set_active_group");
64 bauth_handle->ss_fingerprint_authenticate = dlsym(libhandle, "ss_fingerprint_authenticate");
65 bauth_handle->ss_set_notify_callback = dlsym(libhandle, "ss_set_notify_callback");
66 bauth_handle->ss_fingerprint_cancel = dlsym(libhandle, "ss_fingerprint_cancel");
67 bauth_handle->ss_fingerprint_open = dlsym(libhandle, "ss_fingerprint_open");
68
69 return 0;
70
71no_memory:
72 ALOGE("%s: not enough memory to load the libhandle", __func__);
73 return -ENOMEM;
74}
75
Jesse Chancd60dd82017-02-05 18:53:42 +080076static void hal_notify_convert(const fingerprint_msg_t *msg)
77{
78 fingerprint_msg_t *new_msg = (fingerprint_msg_t *)msg;
79
80 switch (msg->type) {
81 case FINGERPRINT_TEMPLATE_ENROLLING:
82 new_msg->data.enroll.samples_remaining = 100 - msg->data.enroll.samples_remaining;
83 break;
84
85 default:
86 break;
87 }
88
89 return original_notify(new_msg);
90}
91
Jesse Chanb23aaf72017-02-05 21:18:09 +080092static int fingerprint_close(hw_device_t *dev)
93{
94 bauth_handle->ss_fingerprint_close();
95
96 if (dev)
97 free(dev);
98
99 return 0;
100}
101
102static uint64_t fingerprint_pre_enroll(struct fingerprint_device __unused *dev)
103{
104 return bauth_handle->ss_fingerprint_pre_enroll();
105}
106
107static int fingerprint_enroll(struct fingerprint_device __unused *dev, const hw_auth_token_t *hat,
108 uint32_t gid, uint32_t timeout_sec)
109{
110 return bauth_handle->ss_fingerprint_enroll(hat, gid, timeout_sec);
111}
112
113static int fingerprint_post_enroll(struct fingerprint_device __unused *dev)
114{
115 return bauth_handle->ss_fingerprint_post_enroll();
116}
117
118static uint64_t fingerprint_get_auth_id(struct fingerprint_device __unused *dev)
119{
120 return bauth_handle->ss_fingerprint_get_auth_id();
121}
122
123static int fingerprint_cancel(struct fingerprint_device __unused *dev)
124{
125 return bauth_handle->ss_fingerprint_cancel();
126}
127
128static int fingerprint_remove(struct fingerprint_device __unused *dev, uint32_t gid, uint32_t fid)
129{
130 return bauth_handle->ss_fingerprint_remove(gid, fid);
131}
132
133static int fingerprint_set_active_group(struct fingerprint_device __unused *dev, uint32_t gid,
134 const char *store_path)
135{
136 return bauth_handle->ss_fingerprint_set_active_group(gid, store_path);
137}
138
139static int fingerprint_authenticate(struct fingerprint_device __unused *dev, uint64_t operation_id,
140 uint32_t gid)
141{
142 return bauth_handle->ss_fingerprint_authenticate(operation_id, gid);
143}
144
145static int set_notify_callback(struct fingerprint_device *dev, fingerprint_notify_t notify)
146{
147 /* Decorate with locks */
148 dev->notify = notify;
Jesse Chancd60dd82017-02-05 18:53:42 +0800149 original_notify = notify;
150 return bauth_handle->ss_set_notify_callback(hal_notify_convert);
Jesse Chanb23aaf72017-02-05 21:18:09 +0800151}
152
153static int fingerprint_open(const hw_module_t* module, const char *id, hw_device_t** device)
154{
155 int ret;
156 fingerprint_device_t *dev = NULL;
157
158 if (device == NULL) {
159 ALOGE("NULL device on open");
160 ret = -ENODEV;
161 goto fail;
162 }
163
164 ret = load_bauth_server();
165 if (ret < 0)
166 goto fail;
167
168 dev = (fingerprint_device_t*)calloc(1, sizeof(fingerprint_device_t));
169 if (dev == NULL) {
170 ret = -ENOMEM;
171 goto fail;
172 }
173
174 dev->common.tag = HARDWARE_DEVICE_TAG;
LuK1337bdb254d2017-08-28 22:22:42 +0200175 dev->common.version = FINGERPRINT_MODULE_API_VERSION_2_1;
Jesse Chanb23aaf72017-02-05 21:18:09 +0800176 dev->common.module = (struct hw_module_t*) module;
177 dev->common.close = fingerprint_close;
178
179 dev->pre_enroll = fingerprint_pre_enroll;
180 dev->enroll = fingerprint_enroll;
181 dev->post_enroll = fingerprint_post_enroll;
182 dev->get_authenticator_id = fingerprint_get_auth_id;
183 dev->cancel = fingerprint_cancel;
184 dev->remove = fingerprint_remove;
185 dev->set_active_group = fingerprint_set_active_group;
186 dev->authenticate = fingerprint_authenticate;
187 dev->set_notify = set_notify_callback;
188 dev->notify = NULL;
189
190 *device = (hw_device_t*) dev;
191 return (*bauth_handle->ss_fingerprint_open)(id);
192
193fail:
194 ALOGE("%s: failed to open FP device (ret=%d)", __func__, ret);
195 return ret;
196}
197
198static struct hw_module_methods_t fingerprint_module_methods = {
199 .open = fingerprint_open,
200};
201
202fingerprint_module_t HAL_MODULE_INFO_SYM = {
203 .common = {
204 .tag = HARDWARE_MODULE_TAG,
LuK1337bdb254d2017-08-28 22:22:42 +0200205 .module_api_version = FINGERPRINT_MODULE_API_VERSION_2_1,
Jesse Chanb23aaf72017-02-05 21:18:09 +0800206 .hal_api_version = HARDWARE_HAL_API_VERSION,
207 .id = FINGERPRINT_HARDWARE_MODULE_ID,
208 .name = "Samsung TZ Fingerprint HAL",
209 .author = "The LineageOS Project",
210 .methods = &fingerprint_module_methods,
211 },
212};