1#define LOG_TAG "android.hardware.nfc@1.0-impl"
2
3#include <log/log.h>
4
5#include <hardware/hardware.h>
6#include <hardware/nfc.h>
7#include "Nfc.h"
8
9namespace android {
10namespace hardware {
11namespace nfc {
12namespace V1_0 {
13namespace implementation {
14
15sp<INfcClientCallback> Nfc::mCallback = nullptr;
16
17Nfc::Nfc(nfc_nci_device_t* device) : mDevice(device) {}
18
19// Methods from ::android::hardware::nfc::V1_0::INfc follow.
20::android::hardware::Return<NfcStatus> Nfc::open(const sp<INfcClientCallback>& clientCallback)  {
21    mCallback = clientCallback;
22
23    if (mDevice == nullptr || mCallback == nullptr) {
24        return NfcStatus::FAILED;
25    }
26    mCallback->linkToDeath(this, 0 /*cookie*/);
27    int ret = mDevice->open(mDevice, eventCallback, dataCallback);
28    return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
29}
30
31::android::hardware::Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data)  {
32    if (mDevice == nullptr) {
33        return -1;
34    }
35    return mDevice->write(mDevice, data.size(), &data[0]);
36}
37
38::android::hardware::Return<NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data)  {
39    hidl_vec<uint8_t> copy = data;
40
41    if (mDevice == nullptr) {
42        return NfcStatus::FAILED;
43    }
44    int ret = mDevice->core_initialized(mDevice, &copy[0]);
45    return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
46}
47
48::android::hardware::Return<NfcStatus> Nfc::prediscover()  {
49    if (mDevice == nullptr) {
50        return NfcStatus::FAILED;
51    }
52    return mDevice->pre_discover(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
53}
54
55::android::hardware::Return<NfcStatus> Nfc::close()  {
56    if (mDevice == nullptr || mCallback == nullptr) {
57        return NfcStatus::FAILED;
58    }
59    mCallback->unlinkToDeath(this);
60    return mDevice->close(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
61}
62
63::android::hardware::Return<NfcStatus> Nfc::controlGranted()  {
64    if (mDevice == nullptr) {
65        return NfcStatus::FAILED;
66    }
67    return mDevice->control_granted(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
68}
69
70::android::hardware::Return<NfcStatus> Nfc::powerCycle()  {
71    if (mDevice == nullptr) {
72        return NfcStatus::FAILED;
73    }
74    return mDevice->power_cycle(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
75}
76
77
78INfc* HIDL_FETCH_INfc(const char * /*name*/) {
79    nfc_nci_device_t* nfc_device;
80    int ret = 0;
81    const hw_module_t* hw_module = nullptr;
82
83    ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
84    if (ret == 0) {
85        ret = nfc_nci_open (hw_module, &nfc_device);
86        if (ret != 0) {
87            ALOGE ("nfc_nci_open failed: %d", ret);
88        }
89    }
90    else
91        ALOGE ("hw_get_module %s failed: %d", NFC_NCI_HARDWARE_MODULE_ID, ret);
92
93    if (ret == 0) {
94        return new Nfc(nfc_device);
95    } else {
96        ALOGE("Passthrough failed to load legacy HAL.");
97        return nullptr;
98    }
99}
100
101} // namespace implementation
102}  // namespace V1_0
103}  // namespace nfc
104}  // namespace hardware
105}  // namespace android
106