2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This is a very basic implementation of fingerprint to allow testing on the emulator. It
19 * is *not* meant to be the final implementation on real devices. For example, it does *not*
20 * implement all of the required features, such as secure template storage and recognition
21 * inside a Trusted Execution Environment (TEE). However, this file is a reasonable starting
22 * point as developers add fingerprint support to their platform. See inline comments and
23 * recommendations for details.
25 * Please see the Android Compatibility Definition Document (CDD) for a full list of requirements
28 #define LOG_TAG "FingerprintHal"
35 #include <cutils/log.h>
36 #include <hardware/hardware.h>
37 #include <hardware/fingerprint.h>
38 #include <hardware/qemud.h>
42 #define FINGERPRINT_LISTEN_SERVICE_NAME "fingerprintlisten"
43 #define FINGERPRINT_FILENAME "emufp.bin"
44 #define AUTHENTICATOR_ID_FILENAME "emuauthid.bin"
45 #define MAX_COMM_CHARS 128
46 #define MAX_COMM_ERRORS 8
47 // Typical devices will allow up to 5 fingerprints per user to maintain performance of
48 // t < 500ms for recognition. This is the total number of fingerprints we'll store.
49 #define MAX_NUM_FINGERS 20
50 #define MAX_FID_VALUE 0x7FFFFFFF // Arbitrary limit
53 * Most devices will have an internal state machine resembling this. There are 3 basic states, as
54 * shown below. When device is not authenticating or enrolling, it is expected to be in
57 * Note that this is completely independent of device wake state. If the hardware device was in
58 * the "scan" state when the device drops into power collapse, it should resume scanning when power
59 * is restored. This is to facilitate rapid touch-to-unlock from keyguard.
61 typedef enum worker_state_t {
68 typedef struct worker_thread_t {
71 uint64_t secureid[MAX_NUM_FINGERS];
72 uint64_t fingerid[MAX_NUM_FINGERS];
73 char fp_filename[PATH_MAX];
74 char authid_filename[PATH_MAX];
77 typedef struct qemu_fingerprint_device_t {
78 fingerprint_device_t device; // "inheritance"
79 worker_thread_t listener;
84 uint64_t secure_user_id;
85 uint64_t authenticator_id;
88 } qemu_fingerprint_device_t;
90 /******************************************************************************/
92 static FILE* openForWrite(const char* filename);
94 static void saveFingerprint(worker_thread_t* listener, int idx) {
95 ALOGD("----------------> %s -----------------> idx %d", __FUNCTION__, idx);
97 // Save fingerprints to file
98 FILE* fp = openForWrite(listener->fp_filename);
100 ALOGE("Could not open fingerprints storage at %s; "
101 "fingerprints won't be saved",
102 listener->fp_filename);
103 perror("Failed to open file");
107 ALOGD("Write fingerprint[%d] (0x%" PRIx64 ",0x%" PRIx64 ")", idx,
108 listener->secureid[idx], listener->fingerid[idx]);
110 if (fseek(fp, (idx) * sizeof(uint64_t), SEEK_SET) < 0) {
111 ALOGE("Failed while seeking for fingerprint[%d] in emulator storage",
116 int ns = fwrite(&listener->secureid[idx], sizeof(uint64_t), 1, fp);
118 if (fseek(fp, (MAX_NUM_FINGERS + idx) * sizeof(uint64_t), SEEK_SET) < 0) {
119 ALOGE("Failed while seeking for fingerprint[%d] in emulator storage",
124 int nf = fwrite(&listener->fingerid[idx], sizeof(uint64_t), 1, fp);
125 if (ns != 1 || ns !=1)
126 ALOGW("Corrupt emulator fingerprints storage; could not save "
134 static FILE* openForWrite(const char* filename) {
136 if (!filename) return NULL;
138 FILE* fp = fopen(filename, "r+"); // write but don't truncate
140 fp = fopen(filename, "w");
144 for (i = 0; i < 2*MAX_NUM_FINGERS; ++i) {
145 fwrite(&zero, sizeof(uint64_t), 1, fp);
148 //the last one is for authenticator id
149 fwrite(&zero, sizeof(uint64_t), 1, fp);
155 static void saveAuthenticatorId(const char* filename, uint64_t authenid) {
156 ALOGD("----------------> %s ----------------->", __FUNCTION__);
157 FILE* fp = openForWrite(filename);
159 ALOGE("Failed to open emulator storage file to save authenticator id");
165 int na = fwrite(&authenid, sizeof(authenid), 1, fp);
167 ALOGE("Failed while writing authenticator id in emulator storage");
170 ALOGD("Save authenticator id (0x%" PRIx64 ")", authenid);
175 static void loadAuthenticatorId(const char* authid_filename, uint64_t* pauthenid) {
176 ALOGD("----------------> %s ----------------->", __FUNCTION__);
177 FILE* fp = fopen(authid_filename, "r");
179 ALOGE("Could not load authenticator id from storage at %s; "
180 "it has not yet been created.",
182 perror("Failed to open/create file");
188 int na = fread(pauthenid, sizeof(*pauthenid), 1, fp);
190 ALOGW("Corrupt emulator authenticator id storage (read %d)", na);
192 ALOGD("Read authenticator id (0x%" PRIx64 ")", *pauthenid);
199 static void loadFingerprints(worker_thread_t* listener) {
200 ALOGD("----------------> %s ----------------->", __FUNCTION__);
201 FILE* fp = fopen(listener->fp_filename, "r");
203 ALOGE("Could not load fingerprints from storage at %s; "
204 "it has not yet been created.",
205 listener->fp_filename);
206 perror("Failed to open/create file");
210 int ns = fread(listener->secureid, MAX_NUM_FINGERS * sizeof(uint64_t), 1,
212 int nf = fread(listener->fingerid, MAX_NUM_FINGERS * sizeof(uint64_t), 1,
214 if (ns != 1 || nf != 1)
215 ALOGW("Corrupt emulator fingerprints storage (read %d+%db)", ns, nf);
218 for (i = 0; i < MAX_NUM_FINGERS; i++)
219 ALOGD("Read fingerprint %d (0x%" PRIx64 ",0x%" PRIx64 ")", i,
220 listener->secureid[i], listener->fingerid[i]);
227 /******************************************************************************/
229 static uint64_t get_64bit_rand() {
230 // This should use a cryptographically-secure random number generator like arc4random().
231 // It should be generated inside of the TEE where possible. Here we just use something
233 ALOGD("----------------> %s ----------------->", __FUNCTION__);
234 uint64_t r = (((uint64_t)rand()) << 32) | ((uint64_t)rand());
235 return r != 0 ? r : 1;
238 static uint64_t fingerprint_get_auth_id(struct fingerprint_device* device) {
239 // This should return the authentication_id generated when the fingerprint template database
240 // was created. Though this isn't expected to be secret, it is reasonable to expect it to be
241 // cryptographically generated to avoid replay attacks.
242 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
243 ALOGD("----------------> %s ----------------->", __FUNCTION__);
244 uint64_t authenticator_id = 0;
245 pthread_mutex_lock(&qdev->lock);
246 authenticator_id = qdev->authenticator_id;
247 pthread_mutex_unlock(&qdev->lock);
249 ALOGD("----------------> %s auth id %" PRIx64 "----------------->", __FUNCTION__, authenticator_id);
250 return authenticator_id;
253 static int fingerprint_set_active_group(struct fingerprint_device *device, uint32_t gid,
255 ALOGD("----------------> %s -----------------> path %s", __FUNCTION__, path);
256 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
257 pthread_mutex_lock(&qdev->lock);
258 qdev->group_id = gid;
259 snprintf(qdev->listener.fp_filename, sizeof(qdev->listener.fp_filename),
260 "%s/%s", path, FINGERPRINT_FILENAME);
261 snprintf(qdev->listener.authid_filename, sizeof(qdev->listener.authid_filename),
262 "%s/%s", path, AUTHENTICATOR_ID_FILENAME);
263 uint64_t authenticator_id = 0;
264 loadFingerprints(&qdev->listener);
265 loadAuthenticatorId(qdev->listener.authid_filename, &authenticator_id);
266 if (authenticator_id == 0) {
267 // firs time, create an authenticator id
268 authenticator_id = get_64bit_rand();
270 saveAuthenticatorId(qdev->listener.authid_filename, authenticator_id);
273 qdev->authenticator_id = authenticator_id;
274 pthread_mutex_unlock(&qdev->lock);
280 * If fingerprints are enrolled, then this function is expected to put the sensor into a
281 * "scanning" state where it's actively scanning and recognizing fingerprint features.
282 * Actual authentication must happen in TEE and should be monitored in a separate thread
283 * since this function is expected to return immediately.
285 static int fingerprint_authenticate(struct fingerprint_device *device,
286 uint64_t operation_id, __unused uint32_t gid)
288 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
290 pthread_mutex_lock(&qdev->lock);
291 qdev->op_id = operation_id;
292 qdev->listener.state = STATE_SCAN;
293 pthread_mutex_unlock(&qdev->lock);
299 * This is expected to put the sensor into an "enroll" state where it's actively scanning and
300 * working towards a finished fingerprint database entry. Authentication must happen in
301 * a separate thread since this function is expected to return immediately.
303 * Note: This method should always generate a new random authenticator_id.
305 * Note: As with fingerprint_authenticate(), this would run in TEE on a real device.
307 static int fingerprint_enroll(struct fingerprint_device *device,
308 const hw_auth_token_t *hat,
309 uint32_t __unused gid,
310 uint32_t __unused timeout_sec) {
311 ALOGD("fingerprint_enroll");
312 qemu_fingerprint_device_t* dev = (qemu_fingerprint_device_t*)device;
314 ALOGW("%s: null auth token", __func__);
315 return -EPROTONOSUPPORT;
317 if (hat->challenge == dev->challenge) {
318 // The secure_user_id retrieved from the auth token should be stored
319 // with the enrolled fingerprint template and returned in the auth result
320 // for a successful authentication with that finger.
321 dev->secure_user_id = hat->user_id;
323 ALOGW("%s: invalid auth token", __func__);
326 if (hat->version != HW_AUTH_TOKEN_VERSION) {
327 return -EPROTONOSUPPORT;
329 if (hat->challenge != dev->challenge && !(hat->authenticator_type & HW_AUTH_FINGERPRINT)) {
333 dev->user_id = hat->user_id;
335 pthread_mutex_lock(&dev->lock);
336 dev->listener.state = STATE_ENROLL;
337 pthread_mutex_unlock(&dev->lock);
339 // fingerprint id, authenticator id, and secure_user_id
340 // will be stored by worked thread
347 * The pre-enrollment step is simply to get an authentication token that can be wrapped and
348 * verified at a later step. The primary purpose is to return a token that protects against
349 * spoofing and replay attacks. It is passed to password authentication where it is wrapped and
350 * propagated to the enroll step.
352 static uint64_t fingerprint_pre_enroll(struct fingerprint_device *device) {
353 ALOGD("----------------> %s ----------------->", __FUNCTION__);
354 uint64_t challenge = 0;
355 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
357 // The challenge will typically be a cryptographically-secure key
358 // coming from the TEE so it can be verified at a later step. For now we just generate a
360 challenge = get_64bit_rand();
362 pthread_mutex_lock(&qdev->lock);
363 qdev->challenge = challenge;
364 pthread_mutex_unlock(&qdev->lock);
369 static int fingerprint_post_enroll(struct fingerprint_device* device) {
370 ALOGD("----------------> %s ----------------->", __FUNCTION__);
371 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
373 pthread_mutex_lock(&qdev->lock);
375 pthread_mutex_unlock(&qdev->lock);
381 * Cancel is called by the framework to cancel an outstanding event. This should *not* be called
382 * by the driver since it will cause the framework to stop listening for fingerprints.
384 static int fingerprint_cancel(struct fingerprint_device *device) {
385 ALOGD("----------------> %s ----------------->", __FUNCTION__);
386 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
388 pthread_mutex_lock(&qdev->lock);
389 qdev->listener.state = STATE_IDLE;
390 pthread_mutex_unlock(&qdev->lock);
392 fingerprint_msg_t msg = {0, {0}};
393 msg.type = FINGERPRINT_ERROR;
394 msg.data.error = FINGERPRINT_ERROR_CANCELED;
395 qdev->device.notify(&msg);
400 static int fingerprint_enumerate(struct fingerprint_device *device) {
401 ALOGD("----------------> %s ----------------->", __FUNCTION__);
402 if (device == NULL) {
403 ALOGE("Cannot enumerate saved fingerprints with uninitialized params");
407 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
408 int template_count = 0;
409 for (int i = 0; i < MAX_NUM_FINGERS; i++) {
410 if (qdev->listener.secureid[i] != 0 ||
411 qdev->listener.fingerid[i] != 0) {
412 ALOGD("ENUM: Fingerprint [%d] = 0x%" PRIx64 ",%" PRIx64, i,
413 qdev->listener.secureid[i], qdev->listener.fingerid[i]);
417 fingerprint_msg_t message = {0, {0}};
418 message.type = FINGERPRINT_TEMPLATE_ENUMERATING;
419 message.data.enumerated.finger.gid = qdev->group_id;
420 for (int i = 0; i < MAX_NUM_FINGERS; i++) {
421 if (qdev->listener.secureid[i] != 0 ||
422 qdev->listener.fingerid[i] != 0) {
424 message.data.enumerated.remaining_templates = template_count;
425 message.data.enumerated.finger.fid = qdev->listener.fingerid[i];
426 qdev->device.notify(&message);
433 static int fingerprint_remove(struct fingerprint_device *device,
434 uint32_t __unused gid, uint32_t fid) {
436 fingerprint_msg_t msg = {0, {0}};
437 ALOGD("----------------> %s -----------------> fid %d", __FUNCTION__, fid);
438 if (device == NULL) {
439 ALOGE("Can't remove fingerprint (gid=%d, fid=%d); "
440 "device not initialized properly",
445 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
448 // Delete all fingerprints
449 // I'll do this one at a time, so I am not
450 // holding the mutext during the notification
453 pthread_mutex_lock(&qdev->lock);
454 listIsEmpty = true; // Haven't seen a valid entry yet
455 for (idx = 0; idx < MAX_NUM_FINGERS; idx++) {
456 uint32_t theFid = qdev->listener.fingerid[idx];
459 qdev->listener.secureid[idx] = 0;
460 qdev->listener.fingerid[idx] = 0;
461 saveFingerprint(&qdev->listener, idx);
463 // Send a notification that we deleted this one
464 pthread_mutex_unlock(&qdev->lock);
465 msg.type = FINGERPRINT_TEMPLATE_REMOVED;
466 msg.data.removed.finger.fid = theFid;
467 device->notify(&msg);
469 // Because we released the mutex, the list
470 // may have changed. Restart the 'for' loop
471 // after reacquiring the mutex.
475 } // end for (idx < MAX_NUM_FINGERS)
476 } while (!listIsEmpty);
477 msg.type = FINGERPRINT_TEMPLATE_REMOVED;
478 msg.data.removed.finger.fid = 0;
479 device->notify(&msg);
480 qdev->listener.state = STATE_IDLE;
481 pthread_mutex_unlock(&qdev->lock);
483 // Delete one fingerprint
484 // Look for this finger ID in our table.
485 pthread_mutex_lock(&qdev->lock);
486 for (idx = 0; idx < MAX_NUM_FINGERS; idx++) {
487 if (qdev->listener.fingerid[idx] == fid &&
488 qdev->listener.secureid[idx] != 0) {
493 if (idx >= MAX_NUM_FINGERS) {
494 qdev->listener.state = STATE_IDLE;
495 pthread_mutex_unlock(&qdev->lock);
496 ALOGE("Fingerprint ID %d not found", fid);
497 return FINGERPRINT_ERROR;
500 qdev->listener.secureid[idx] = 0;
501 qdev->listener.fingerid[idx] = 0;
502 saveFingerprint(&qdev->listener, idx);
504 qdev->listener.state = STATE_IDLE;
505 pthread_mutex_unlock(&qdev->lock);
507 msg.type = FINGERPRINT_TEMPLATE_REMOVED;
508 msg.data.removed.finger.fid = fid;
509 device->notify(&msg);
515 static int set_notify_callback(struct fingerprint_device *device,
516 fingerprint_notify_t notify) {
517 ALOGD("----------------> %s ----------------->", __FUNCTION__);
518 if (device == NULL || notify == NULL) {
519 ALOGE("Failed to set notify callback @ %p for fingerprint device %p",
524 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
525 pthread_mutex_lock(&qdev->lock);
526 qdev->listener.state = STATE_IDLE;
527 device->notify = notify;
528 pthread_mutex_unlock(&qdev->lock);
529 ALOGD("fingerprint callback notification set");
534 static bool is_valid_fid(qemu_fingerprint_device_t* qdev, uint64_t fid) {
536 if (0 == fid) { return false; }
537 for (idx = 0; idx < MAX_NUM_FINGERS; idx++) {
538 if (qdev->listener.fingerid[idx] == fid) {
545 static void send_scan_notice(qemu_fingerprint_device_t* qdev, int fid) {
546 ALOGD("----------------> %s ----------------->", __FUNCTION__);
549 fingerprint_msg_t acqu_msg = {0, {0}};
550 acqu_msg.type = FINGERPRINT_ACQUIRED;
551 acqu_msg.data.acquired.acquired_info = FINGERPRINT_ACQUIRED_GOOD;
553 // authenticated message
554 fingerprint_msg_t auth_msg = {0, {0}};
555 auth_msg.type = FINGERPRINT_AUTHENTICATED;
556 auth_msg.data.authenticated.finger.fid = is_valid_fid(qdev, fid) ? fid : 0;
557 auth_msg.data.authenticated.finger.gid = 0; // unused
558 auth_msg.data.authenticated.hat.version = HW_AUTH_TOKEN_VERSION;
559 auth_msg.data.authenticated.hat.authenticator_type =
560 htobe32(HW_AUTH_FINGERPRINT);
561 auth_msg.data.authenticated.hat.challenge = qdev->op_id;
562 auth_msg.data.authenticated.hat.authenticator_id = qdev->authenticator_id;
563 auth_msg.data.authenticated.hat.user_id = qdev->secure_user_id;
565 clock_gettime(CLOCK_MONOTONIC, &ts);
566 auth_msg.data.authenticated.hat.timestamp =
567 htobe64((uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
569 // pthread_mutex_lock(&qdev->lock);
570 qdev->device.notify(&acqu_msg);
571 qdev->device.notify(&auth_msg);
572 // pthread_mutex_unlock(&qdev->lock);
577 static void send_enroll_notice(qemu_fingerprint_device_t* qdev, int fid) {
578 ALOGD("----------------> %s -----------------> fid %d", __FUNCTION__, fid);
581 ALOGD("Fingerprint ID is zero (invalid)");
584 if (qdev->secure_user_id == 0) {
585 ALOGD("Secure user ID is zero (invalid)");
589 // Find an available entry in the table
590 pthread_mutex_lock(&qdev->lock);
592 for (idx = 0; idx < MAX_NUM_FINGERS; idx++) {
593 if (qdev->listener.secureid[idx] == 0 ||
594 qdev->listener.fingerid[idx] == 0) {
595 // This entry is available
599 if (idx >= MAX_NUM_FINGERS) {
600 qdev->listener.state = STATE_SCAN;
601 pthread_mutex_unlock(&qdev->lock);
602 ALOGD("Fingerprint ID table is full");
606 qdev->listener.secureid[idx] = qdev->secure_user_id;
607 qdev->listener.fingerid[idx] = fid;
608 saveFingerprint(&qdev->listener, idx);
610 qdev->listener.state = STATE_SCAN;
611 pthread_mutex_unlock(&qdev->lock);
613 // LOCKED notification?
614 fingerprint_msg_t msg = {0, {0}};
615 msg.type = FINGERPRINT_TEMPLATE_ENROLLING;
616 msg.data.enroll.finger.fid = fid;
617 msg.data.enroll.samples_remaining = 0;
618 qdev->device.notify(&msg);
623 static worker_state_t getListenerState(qemu_fingerprint_device_t* dev) {
624 ALOGV("----------------> %s ----------------->", __FUNCTION__);
625 worker_state_t state = STATE_IDLE;
627 pthread_mutex_lock(&dev->lock);
628 state = dev->listener.state;
629 pthread_mutex_unlock(&dev->lock);
635 * This a very simple event loop for the fingerprint sensor. For a given state (enroll, scan),
636 * this would receive events from the sensor and forward them to fingerprintd using the
639 * In this simple example, we open a qemu channel (a pipe) where the developer can inject events to
640 * exercise the API and test application code.
642 * The scanner should remain in the scanning state until either an error occurs or the operation
645 * Recoverable errors such as EINTR should be handled locally; they should not
646 * be propagated unless there's something the user can do about it (e.g. "clean sensor"). Such
647 * messages should go through the onAcquired() interface.
649 * If an unrecoverable error occurs, an acquired message (e.g. ACQUIRED_PARTIAL) should be sent,
650 * followed by an error message (e.g. FINGERPRINT_ERROR_UNABLE_TO_PROCESS).
652 * Note that this event loop would typically run in TEE since it must interact with the sensor
653 * hardware and handle raw fingerprint data and encrypted templates. It is expected that
654 * this code monitors the TEE for resulting events, such as enrollment and authentication status.
655 * Here we just have a very simple event loop that monitors a qemu channel for pseudo events.
657 static void* listenerFunction(void* data) {
658 ALOGD("----------------> %s ----------------->", __FUNCTION__);
659 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)data;
661 pthread_mutex_lock(&qdev->lock);
662 qdev->qchanfd = qemud_channel_open(FINGERPRINT_LISTEN_SERVICE_NAME);
663 if (qdev->qchanfd < 0) {
664 ALOGE("listener cannot open fingerprint listener service exit");
665 pthread_mutex_unlock(&qdev->lock);
668 qdev->listener.state = STATE_IDLE;
669 pthread_mutex_unlock(&qdev->lock);
671 const char* cmd = "listen";
672 if (qemud_channel_send(qdev->qchanfd, cmd, strlen(cmd)) < 0) {
673 ALOGE("cannot write fingerprint 'listen' to host");
678 struct pollfd pfd = {
685 char buffer[MAX_COMM_CHARS] = {0};
686 bool disconnected = false;
688 if (getListenerState(qdev) == STATE_EXIT) {
689 ALOGD("Received request to exit listener thread");
693 // Reset revents before poll() (just to be safe)
696 // Poll qemud channel for 5 seconds
697 // TODO: Eliminate the timeout so that polling can be interrupted
698 // instantly. One possible solution is to follow the example of
699 // android::Looper ($AOSP/system/core/include/utils/Looper.h and
700 // $AOSP/system/core/libutils/Looper.cpp), which makes use of an
701 // additional file descriptor ("wake event fd").
702 int nfds = poll(&pfd, 1, 5000);
704 ALOGE("Could not poll qemud channel: %s", strerror(errno));
709 // poll() timed out - try again
714 if (pfd.revents & POLLIN) {
715 // Input data being available doesn't rule out a disconnection
716 disconnected = pfd.revents & (POLLERR | POLLHUP);
717 break; // Exit inner while loop
719 // Some event(s) other than "input data available" occurred,
720 // i.e. POLLERR or POLLHUP, indicating a disconnection
721 ALOGW("Lost connection to qemud channel");
726 // Shouldn't block since we were just notified of a POLLIN event
727 if ((size = qemud_channel_recv(qdev->qchanfd, buffer,
728 sizeof(buffer) - 1)) > 0) {
730 if (sscanf(buffer, "on:%d", &fid) == 1) {
731 if (fid > 0 && fid <= MAX_FID_VALUE) {
732 switch (qdev->listener.state) {
734 send_enroll_notice(qdev, fid);
737 send_scan_notice(qdev, fid);
740 ALOGE("fingerprint event listener at unexpected "
742 qdev->listener.state);
745 ALOGE("fingerprintid %d not in valid range [%d, %d] and "
748 fid, 1, MAX_FID_VALUE);
751 } else if (strncmp("off", buffer, 3) == 0) {
752 // TODO: Nothing to do here ? Looks valid
753 ALOGD("fingerprint ID %d off", fid);
755 ALOGE("Invalid command '%s' to fingerprint listener", buffer);
759 ALOGW("Connection to qemud channel has been lost");
763 ALOGE("fingerprint listener receive failure");
764 if (comm_errors > MAX_COMM_ERRORS)
770 ALOGD("Listener exit with %d receive errors", comm_errors);
772 close(qdev->qchanfd);
776 static int fingerprint_close(hw_device_t* device) {
777 ALOGD("----------------> %s ----------------->", __FUNCTION__);
778 if (device == NULL) {
779 ALOGE("fingerprint hw device is NULL");
783 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)device;
784 pthread_mutex_lock(&qdev->lock);
785 // Ask listener thread to exit
786 qdev->listener.state = STATE_EXIT;
787 pthread_mutex_unlock(&qdev->lock);
789 pthread_join(qdev->listener.thread, NULL);
790 pthread_mutex_destroy(&qdev->lock);
796 static int fingerprint_open(const hw_module_t* module, const char __unused *id,
797 hw_device_t** device)
800 ALOGD("----------------> %s ----------------->", __FUNCTION__);
801 if (device == NULL) {
802 ALOGE("NULL device on open");
806 qemu_fingerprint_device_t* qdev = (qemu_fingerprint_device_t*)calloc(
807 1, sizeof(qemu_fingerprint_device_t));
809 ALOGE("Insufficient memory for virtual fingerprint device");
814 qdev->device.common.tag = HARDWARE_DEVICE_TAG;
815 qdev->device.common.version = HARDWARE_MODULE_API_VERSION(2, 1);
816 qdev->device.common.module = (struct hw_module_t*)module;
817 qdev->device.common.close = fingerprint_close;
819 qdev->device.pre_enroll = fingerprint_pre_enroll;
820 qdev->device.enroll = fingerprint_enroll;
821 qdev->device.post_enroll = fingerprint_post_enroll;
822 qdev->device.get_authenticator_id = fingerprint_get_auth_id;
823 qdev->device.set_active_group = fingerprint_set_active_group;
824 qdev->device.authenticate = fingerprint_authenticate;
825 qdev->device.cancel = fingerprint_cancel;
826 qdev->device.enumerate = fingerprint_enumerate;
827 qdev->device.remove = fingerprint_remove;
828 qdev->device.set_notify = set_notify_callback;
829 qdev->device.notify = NULL;
831 // init and create listener thread
832 pthread_mutex_init(&qdev->lock, NULL);
833 if (pthread_create(&qdev->listener.thread, NULL, listenerFunction, qdev) !=
837 // "Inheritance" / casting
838 *device = &qdev->device.common;
843 static struct hw_module_methods_t fingerprint_module_methods = {
844 .open = fingerprint_open,
847 fingerprint_module_t HAL_MODULE_INFO_SYM = {
849 .tag = HARDWARE_MODULE_TAG,
850 .module_api_version = FINGERPRINT_MODULE_API_VERSION_2_1,
851 .hal_api_version = HARDWARE_HAL_API_VERSION,
852 .id = FINGERPRINT_HARDWARE_MODULE_ID,
853 .name = "Emulator Fingerprint HAL",
854 .author = "The Android Open Source Project",
855 .methods = &fingerprint_module_methods,