summaryrefslogtreecommitdiffstats
path: root/fingerprint/bauth/fingerprint.c
blob: 6e5ed90e71d7721c187ffd6ee435dc152ec911f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) 2017 Jesse Chan, The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "SS Fingerprint HAL"

#include <dlfcn.h>
#include <stdlib.h>
#include <errno.h>

#include <cutils/log.h>

#include <hardware/hardware.h>
#include <hardware/fingerprint.h>
#include <utils/threads.h>

typedef struct {
    void *libhandle;
    int (*ss_fingerprint_close)(void);
    uint64_t (*ss_fingerprint_pre_enroll)(void);
    int (*ss_fingerprint_enroll)(const hw_auth_token_t *hat, uint32_t gid, uint32_t timeout_sec);
    int (*ss_fingerprint_post_enroll)(void);
    uint64_t (*ss_fingerprint_get_auth_id)(void);
    int (*ss_fingerprint_remove)(uint32_t gid, uint32_t fid);
    int (*ss_fingerprint_set_active_group)(uint32_t gid, const char *store_path);
    int (*ss_fingerprint_authenticate)(uint64_t operation_id, uint32_t gid);
    int (*ss_set_notify_callback)(fingerprint_notify_t notify);
    int (*ss_fingerprint_cancel)(void);
    int (*ss_fingerprint_open)(const char *id);
} bauth_server_handle_t;

bauth_server_handle_t* bauth_handle;

static fingerprint_notify_t original_notify;

static int load_bauth_server(void)
{
    bauth_handle = (bauth_server_handle_t *)malloc(sizeof(bauth_server_handle_t));
    if (bauth_handle == NULL)
        goto no_memory;

    void *libhandle = bauth_handle->libhandle;

    libhandle = dlopen("libbauthserver.so", RTLD_NOW);
    bauth_handle->ss_fingerprint_close = dlsym(libhandle, "ss_fingerprint_close");
    bauth_handle->ss_fingerprint_pre_enroll = dlsym(libhandle, "ss_fingerprint_pre_enroll");
    bauth_handle->ss_fingerprint_enroll = dlsym(libhandle, "ss_fingerprint_enroll");
    bauth_handle->ss_fingerprint_post_enroll = dlsym(libhandle, "ss_fingerprint_post_enroll");
    bauth_handle->ss_fingerprint_get_auth_id = dlsym(libhandle, "ss_fingerprint_get_auth_id");
    bauth_handle->ss_fingerprint_remove = dlsym(libhandle, "ss_fingerprint_remove");
    bauth_handle->ss_fingerprint_set_active_group = dlsym(libhandle, "ss_fingerprint_set_active_group");
    bauth_handle->ss_fingerprint_authenticate = dlsym(libhandle, "ss_fingerprint_authenticate");
    bauth_handle->ss_set_notify_callback = dlsym(libhandle, "ss_set_notify_callback");
    bauth_handle->ss_fingerprint_cancel = dlsym(libhandle, "ss_fingerprint_cancel");
    bauth_handle->ss_fingerprint_open = dlsym(libhandle, "ss_fingerprint_open");

    return 0;

no_memory:
    ALOGE("%s: not enough memory to load the libhandle", __func__);
    return -ENOMEM;
}

static void hal_notify_convert(const fingerprint_msg_t *msg)
{
    fingerprint_msg_t *new_msg = (fingerprint_msg_t *)msg;

    switch (msg->type) {
        case FINGERPRINT_TEMPLATE_ENROLLING:
            new_msg->data.enroll.samples_remaining = 100 - msg->data.enroll.samples_remaining;
            break;

        default:
            break;
    }

    return original_notify(new_msg);
}

static int fingerprint_close(hw_device_t *dev)
{
    bauth_handle->ss_fingerprint_close();

    if (dev)
        free(dev);

    return 0;
}

static uint64_t fingerprint_pre_enroll(struct fingerprint_device __unused *dev)
{
    return bauth_handle->ss_fingerprint_pre_enroll();
}

static int fingerprint_enroll(struct fingerprint_device __unused *dev, const hw_auth_token_t *hat,
        uint32_t gid, uint32_t timeout_sec)
{
    return bauth_handle->ss_fingerprint_enroll(hat, gid, timeout_sec);
}

static int fingerprint_post_enroll(struct fingerprint_device __unused *dev)
{
    return bauth_handle->ss_fingerprint_post_enroll();
}

static uint64_t fingerprint_get_auth_id(struct fingerprint_device __unused *dev)
{
    return bauth_handle->ss_fingerprint_get_auth_id();
}

static int fingerprint_cancel(struct fingerprint_device __unused *dev)
{
    return bauth_handle->ss_fingerprint_cancel();
}

static int fingerprint_remove(struct fingerprint_device __unused *dev, uint32_t gid, uint32_t fid)
{
    return bauth_handle->ss_fingerprint_remove(gid, fid);
}

static int fingerprint_set_active_group(struct fingerprint_device __unused *dev, uint32_t gid,
        const char *store_path)
{
    return bauth_handle->ss_fingerprint_set_active_group(gid, store_path);
}

static int fingerprint_authenticate(struct fingerprint_device __unused *dev, uint64_t operation_id,
        uint32_t gid)
{
    return bauth_handle->ss_fingerprint_authenticate(operation_id, gid);
}

static int set_notify_callback(struct fingerprint_device *dev, fingerprint_notify_t notify)
{
    /* Decorate with locks */
    dev->notify = notify;
    original_notify = notify;
    return bauth_handle->ss_set_notify_callback(hal_notify_convert);
}

static int fingerprint_open(const hw_module_t* module, const char *id, hw_device_t** device)
{
    int ret;
    fingerprint_device_t *dev = NULL;

    if (device == NULL) {
        ALOGE("NULL device on open");
        ret = -ENODEV;
        goto fail;
    }

    ret = load_bauth_server();
    if (ret < 0)
        goto fail;

    dev = (fingerprint_device_t*)calloc(1, sizeof(fingerprint_device_t));
    if (dev == NULL) {
        ret = -ENOMEM;
        goto fail;
    }

    dev->common.tag = HARDWARE_DEVICE_TAG;
    dev->common.version = FINGERPRINT_MODULE_API_VERSION_2_0;
    dev->common.module = (struct hw_module_t*) module;
    dev->common.close = fingerprint_close;

    dev->pre_enroll = fingerprint_pre_enroll;
    dev->enroll = fingerprint_enroll;
    dev->post_enroll = fingerprint_post_enroll;
    dev->get_authenticator_id = fingerprint_get_auth_id;
    dev->cancel = fingerprint_cancel;
    dev->remove = fingerprint_remove;
    dev->set_active_group = fingerprint_set_active_group;
    dev->authenticate = fingerprint_authenticate;
    dev->set_notify = set_notify_callback;
    dev->notify = NULL;

    *device = (hw_device_t*) dev;
    return (*bauth_handle->ss_fingerprint_open)(id);

fail:
    ALOGE("%s: failed to open FP device (ret=%d)", __func__, ret);
    return ret;
}

static struct hw_module_methods_t fingerprint_module_methods = {
    .open = fingerprint_open,
};

fingerprint_module_t HAL_MODULE_INFO_SYM = {
    .common = {
        .tag                = HARDWARE_MODULE_TAG,
        .module_api_version = FINGERPRINT_MODULE_API_VERSION_2_0,
        .hal_api_version    = HARDWARE_HAL_API_VERSION,
        .id                 = FINGERPRINT_HARDWARE_MODULE_ID,
        .name               = "Samsung TZ Fingerprint HAL",
        .author             = "The LineageOS Project",
        .methods            = &fingerprint_module_methods,
    },
};