Hardware: Fingerprint: Introducing Fingerprint HAL

Change-Id: I4e55c42841c3b6a1327a16bdf6d1d4bb3847c7f3
Signed-off-by: Sasha Levitskiy <sanek@google.com>
diff --git a/include/hardware/fingerprint.h b/include/hardware/fingerprint.h
new file mode 100644
index 0000000..be2f9e2
--- /dev/null
+++ b/include/hardware/fingerprint.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2014 The Android Open Source 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.
+ */
+
+#ifndef ANDROID_INCLUDE_HARDWARE_FINGERPRINT_H
+#define ANDROID_INCLUDE_HARDWARE_FINGERPRINT_H
+
+#define FINGERPRINT_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(1, 0)
+#define FINGERPRINT_HARDWARE_MODULE_ID "fingerprint"
+
+typedef enum fingerprint_msg {
+    FINGERPRINT_ERROR = -1,
+    FINGERPRINT_NO_MATCH = 0,
+    FINGERPRINT_MATCH = 1,
+    FINGERPRINT_TEMPLATE_COLLECTING = 2,
+    FINGERPRINT_TEMPLATE_REGISTERED = 3,
+    FINGERPRINT_TEMPLATE_DELETED = 4
+} fingerprint_msg_t;
+
+typedef enum fingerprint_error {
+    FINGERPRINT_ERROR_HW_UNAVAILABLE = 1,
+    FINGERPRINT_ERROR_BAD_CAPTURE = 2
+} fingerprint_error_t;
+
+/* Synchronous operation */
+typedef struct fingerprint_device {
+    struct hw_device_t common;
+
+    /*
+     * Figerprint enroll request: records and stores a fingerprint template.
+     * Timeout after temeout_sec seconds.
+     *
+     * Function return:
+     *   - Machine state: error, collected or registered.
+     *   - Data is interpreted as error code, collection percentage
+     *     or fingerprint id.
+     */
+    fingerprint_msg_t (*enroll)(unsigned timeout_sec, unsigned *data);
+
+    /*
+     * Figerprint remove request: deletes a fingerprint template.
+     *
+     * Function return:
+     *   - Delete result: error or success.
+     */
+    fingerprint_msg_t (*remove)(unsigned fingerprint_id);
+
+    /*
+     * Figerprint match request: Collect a fingerprint and
+     * match against stored template with fingerprint_id.
+     * Timeout after temeout_sec seconds.
+     *
+     * Function return:
+     *   - Match, no match or error.
+     */
+    fingerprint_msg_t (*match)(unsigned fingerprint_id, unsigned timeout_sec);
+
+    /* Reserved for future use. Must be NULL. */
+    void* reserved[8 - 3];
+} fingerprint_device_t;
+
+typedef struct fingerprint_module {
+    struct hw_module_t common;
+} fingerprint_module_t;
+
+/* For asyncronous mode - as a possible API model
+typedef void (*fingerprint_callback)(int request_id, fingerprint_msg msg, data);
+*/
+
+#endif  /* ANDROID_INCLUDE_HARDWARE_FINGERPRINT_H */
diff --git a/modules/Android.mk b/modules/Android.mk
index 5f1de32..c903eee 100644
--- a/modules/Android.mk
+++ b/modules/Android.mk
@@ -1,4 +1,4 @@
 hardware_modules := gralloc hwcomposer audio nfc nfc-nci local_time \
 	power usbaudio audio_remote_submix camera consumerir sensors vibrator \
-	mcu tv_input
+	mcu tv_input fingerprint
 include $(call all-named-subdir-makefiles,$(hardware_modules))
diff --git a/modules/fingerprint/Android.mk b/modules/fingerprint/Android.mk
new file mode 100644
index 0000000..58c0a83
--- /dev/null
+++ b/modules/fingerprint/Android.mk
@@ -0,0 +1,25 @@
+# Copyright (C) 2013 The Android Open Source 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.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := fingerprint.default
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_SRC_FILES := fingerprint.c
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/fingerprint/fingerprint.c b/modules/fingerprint/fingerprint.c
new file mode 100644
index 0000000..3bdcdab
--- /dev/null
+++ b/modules/fingerprint/fingerprint.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2014 The Android Open Source 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 "FingerprintHal"
+
+#include <errno.h>
+#include <string.h>
+#include <cutils/log.h>
+#include <hardware/hardware.h>
+#include <hardware/fingerprint.h>
+
+static int fingerprint_close(hw_device_t *dev)
+{
+    if (dev) {
+        free(dev);
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
+static fingerprint_msg_t fingerprint_enroll(unsigned timeout_sec,
+                                            unsigned *data) {
+    (void)timeout_sec;
+    (void)data;
+    return FINGERPRINT_ERROR;
+}
+
+static fingerprint_msg_t fingerprint_remove(unsigned fingerprint_id) {
+    (void)fingerprint_id;
+    return FINGERPRINT_ERROR;
+}
+
+static fingerprint_msg_t fingerprint_match(unsigned fingerprint_id,
+                                            unsigned timeout_sec) {
+    (void)fingerprint_id;
+    (void)timeout_sec;
+    return FINGERPRINT_ERROR;
+}
+
+static int fingerprint_open(const hw_module_t* module, const char* id,
+                            hw_device_t** device)
+{
+    (void)id;
+
+    if (device == NULL) {
+        ALOGE("NULL device on open");
+        return -EINVAL;
+    }
+
+    fingerprint_device_t *dev = malloc(sizeof(fingerprint_device_t));
+    memset(dev, 0, sizeof(fingerprint_device_t));
+
+    dev->common.tag = HARDWARE_DEVICE_TAG;
+    dev->common.version = 0;
+    dev->common.module = (struct hw_module_t*) module;
+    dev->common.close = fingerprint_close;
+
+    dev->enroll = fingerprint_enroll;
+    dev->remove = fingerprint_remove;
+    dev->match = fingerprint_match;
+
+    *device = (hw_device_t*) dev;
+    return 0;
+}
+
+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_1_0,
+        .hal_api_version    = HARDWARE_HAL_API_VERSION,
+        .id                 = FINGERPRINT_HARDWARE_MODULE_ID,
+        .name               = "Demo Fingerprint HAL",
+        .author             = "The Android Open Source Project",
+        .methods            = &fingerprint_module_methods,
+    },
+};
diff --git a/tests/fingerprint/Android.mk b/tests/fingerprint/Android.mk
new file mode 100644
index 0000000..4f03c39
--- /dev/null
+++ b/tests/fingerprint/Android.mk
@@ -0,0 +1,19 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+    fingerprint_tests.cpp \
+
+LOCAL_SHARED_LIBRARIES := \
+    liblog \
+    libhardware \
+
+#LOCAL_C_INCLUDES += \
+#    system/media/camera/include \
+
+LOCAL_CFLAGS += -Wall -Wextra
+
+LOCAL_MODULE:= fingerprint_tests
+LOCAL_MODULE_TAGS := tests
+
+include $(BUILD_NATIVE_TEST)
diff --git a/tests/fingerprint/fingerprint_test_fixtures.h b/tests/fingerprint/fingerprint_test_fixtures.h
new file mode 100644
index 0000000..a526203
--- /dev/null
+++ b/tests/fingerprint/fingerprint_test_fixtures.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2014 The Android Open Source 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.
+ */
+
+#ifndef __ANDROID_HAL_FINGERPRINT_TEST_COMMON__
+#define __ANDROID_HAL_FINGERPRINT_TEST_COMMON__
+
+#include <gtest/gtest.h>
+#include <hardware/hardware.h>
+#include <hardware/fingerprint.h>
+
+namespace tests {
+
+static const uint16_t kVersion = HARDWARE_MODULE_API_VERSION(1, 0);
+
+class FingerprintModule : public testing::Test {
+ public:
+    FingerprintModule() :
+        fp_module_(NULL) {}
+    ~FingerprintModule() {}
+ protected:
+    virtual void SetUp() {
+        const hw_module_t *hw_module = NULL;
+        ASSERT_EQ(0, hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_module))
+                    << "Can't get fingerprint module";
+        ASSERT_TRUE(NULL != hw_module)
+                    << "hw_get_module didn't return a valid fingerprint module";
+
+        fp_module_ = reinterpret_cast<const fingerprint_module_t*>(hw_module);
+    }
+    const fingerprint_module_t* fp_module() { return fp_module_; }
+ private:
+    const fingerprint_module_t *fp_module_;
+};
+
+class FingerprintDevice : public FingerprintModule {
+ public:
+    FingerprintDevice() :
+        fp_device_(NULL) {}
+    ~FingerprintDevice() {}
+ protected:
+    virtual void SetUp() {
+        FingerprintModule::SetUp();
+        hw_device_t *device = NULL;
+        ASSERT_TRUE(NULL != fp_module()->common.methods->open)
+                    << "Fingerprint open() is unimplemented";
+        ASSERT_EQ(0, fp_module()->common.methods->open(
+            (const hw_module_t*)fp_module(), NULL, &device))
+                << "Can't open fingerprint device";
+        ASSERT_TRUE(NULL != device)
+                    << "Fingerprint open() returned a NULL device";
+        ASSERT_EQ(kVersion, device->version)
+                    << "Unsupported version";
+        fp_device_ = reinterpret_cast<fingerprint_device_t*>(device);
+    }
+    fingerprint_device_t* fp_device() { return fp_device_; }
+ private:
+    fingerprint_device_t *fp_device_;
+};
+
+}  // namespace tests
+
+#endif  // __ANDROID_HAL_FINGERPRINT_TEST_COMMON__
diff --git a/tests/fingerprint/fingerprint_tests.cpp b/tests/fingerprint/fingerprint_tests.cpp
new file mode 100644
index 0000000..ad6e1dd
--- /dev/null
+++ b/tests/fingerprint/fingerprint_tests.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2014 The Android Open Source 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.
+ */
+
+#include <gtest/gtest.h>
+#include "fingerprint_test_fixtures.h"
+
+namespace tests {
+
+TEST_F(FingerprintDevice, isThereEnroll) {
+    ASSERT_TRUE(NULL != fp_device()->enroll)
+        << "Enroll function is not implemented";
+}
+
+TEST_F(FingerprintDevice, isThereRemove) {
+    ASSERT_TRUE(NULL != fp_device()->remove)
+        << "Remove function is not implemented";
+}
+
+TEST_F(FingerprintDevice, isThereMatch) {
+    ASSERT_TRUE(NULL != fp_device()->match)
+        << "Match function is not implemented";
+}
+
+}  // namespace tests