android.hardware.nfc@1.0: provide a default implementation

b/31524912
Test: pass

Change-Id: Id9d34f62f4a2b92bdc3beb3e62c89c82743c0ca0
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/nfc/1.0/default/Android.mk b/nfc/1.0/default/Android.mk
new file mode 100644
index 0000000..afd0cd6
--- /dev/null
+++ b/nfc/1.0/default/Android.mk
@@ -0,0 +1,8 @@
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.nfc@1.0-impl
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_SRC_FILES := Nfc.cpp
+LOCAL_SHARED_LIBRARIES := liblog libcutils libhardware libhwbinder libbase libcutils libutils libhidl android.hardware.nfc@1.0
+include $(BUILD_SHARED_LIBRARY)
diff --git a/nfc/1.0/default/Nfc.cpp b/nfc/1.0/default/Nfc.cpp
new file mode 100644
index 0000000..9023ecc
--- /dev/null
+++ b/nfc/1.0/default/Nfc.cpp
@@ -0,0 +1,76 @@
+#include <hardware/hardware.h>
+#include <hardware/nfc.h>
+#include "Nfc.h"
+
+namespace android {
+namespace hardware {
+namespace nfc {
+namespace V1_0 {
+namespace implementation {
+
+sp<INfcClientCallback> Nfc::mCallback = NULL;
+
+Nfc::Nfc(nfc_nci_device_t* device) : mDevice(device) {
+}
+
+// Methods from ::android::hardware::nfc::V1_0::INfc follow.
+::android::hardware::Return<int32_t> Nfc::open(const sp<INfcClientCallback>& clientCallback)  {
+    mCallback = clientCallback;
+    return mDevice->open(mDevice, event_callback, data_callback);
+}
+
+::android::hardware::Return<int32_t> Nfc::write(const nfc_data_t& data)  {
+    return mDevice->write(mDevice, data.data.size(), &data.data[0]);
+}
+
+::android::hardware::Return<int32_t> Nfc::core_initialized(const hidl_vec<uint8_t>& data)  {
+    hidl_vec<uint8_t> copy = data;
+    return mDevice->core_initialized(mDevice, &copy[0]);
+}
+
+::android::hardware::Return<int32_t> Nfc::pre_discover()  {
+    return mDevice->pre_discover(mDevice);
+}
+
+::android::hardware::Return<int32_t> Nfc::close()  {
+    return mDevice->close(mDevice);
+}
+
+::android::hardware::Return<int32_t> Nfc::control_granted()  {
+    return mDevice->control_granted(mDevice);
+}
+
+::android::hardware::Return<int32_t> Nfc::power_cycle()  {
+    return mDevice->power_cycle(mDevice);
+}
+
+
+INfc* HIDL_FETCH_INfc(const char *hal) {
+    nfc_nci_device_t* nfc_device;
+    int ret = 0;
+    const hw_module_t* hw_module = NULL;
+
+    ret = hw_get_module (hal, &hw_module);
+    if (ret == 0)
+    {
+        ret = nfc_nci_open (hw_module, &nfc_device);
+        if (ret != 0) {
+            ALOGE ("nfc_nci_open %s failed: %d", hal, ret);
+        }
+    }
+    else
+        ALOGE ("hw_get_module %s failed: %d", hal, ret);
+
+    if (ret == 0) {
+        return new Nfc(nfc_device);
+    } else {
+        ALOGE("Passthrough failed to load legacy HAL.");
+        return nullptr;
+    }
+}
+
+} // namespace implementation
+}  // namespace V1_0
+}  // namespace nfc
+}  // namespace hardware
+}  // namespace android
diff --git a/nfc/1.0/default/Nfc.h b/nfc/1.0/default/Nfc.h
new file mode 100644
index 0000000..98cd57e
--- /dev/null
+++ b/nfc/1.0/default/Nfc.h
@@ -0,0 +1,60 @@
+#ifndef HIDL_GENERATED_android_hardware_nfc_V1_0_Nfc_H_
+#define HIDL_GENERATED_android_hardware_nfc_V1_0_Nfc_H_
+
+#include <android/hardware/nfc/1.0/INfc.h>
+#include <hidl/Status.h>
+#include <hardware/hardware.h>
+#include <hardware/nfc.h>
+namespace android {
+namespace hardware {
+namespace nfc {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hardware::nfc::V1_0::INfc;
+using ::android::hardware::nfc::V1_0::INfcClientCallback;
+using ::android::hardware::nfc::V1_0::nfc_data_t;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::hidl_string;
+using ::android::sp;
+
+struct Nfc : public INfc {
+  Nfc(nfc_nci_device_t* device);
+  ::android::hardware::Return<int32_t> open(const sp<INfcClientCallback>& clientCallback)  override;
+  ::android::hardware::Return<int32_t> write(const nfc_data_t& data)  override;
+  ::android::hardware::Return<int32_t> core_initialized(const hidl_vec<uint8_t>& data)  override;
+  ::android::hardware::Return<int32_t> pre_discover()  override;
+  ::android::hardware::Return<int32_t> close()  override;
+  ::android::hardware::Return<int32_t> control_granted()  override;
+  ::android::hardware::Return<int32_t> power_cycle()  override;
+
+  static void event_callback(uint8_t event, uint8_t status) {
+      if (mCallback != nullptr) {
+          mCallback->sendEvent(
+                  (::android::hardware::nfc::V1_0::nfc_event_t) event,
+                  (::android::hardware::nfc::V1_0::nfc_status_t) status);
+      }
+  }
+  static void data_callback(uint16_t data_len, uint8_t* p_data) {
+      nfc_data_t data;
+      data.data.setToExternal(p_data, data_len);
+      if (mCallback != nullptr) {
+          mCallback->sendData(data);
+      }
+  }
+  private:
+    static sp<INfcClientCallback> mCallback;
+    const nfc_nci_device_t*       mDevice;
+};
+
+extern "C" INfc* HIDL_FETCH_INfc(const char* name);
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace nfc
+}  // namespace hardware
+}  // namespace android
+
+#endif  // HIDL_GENERATED_android_hardware_nfc_V1_0_Nfc_H_