Iliyan Malchev | 3db0f60 | 2016-09-26 09:57:20 -0700 | [diff] [blame] | 1 | #define LOG_TAG "android.hardware.nfc@1.0-impl" |
Mark Salyzyn | a4842ac | 2017-01-10 10:16:48 -0800 | [diff] [blame] | 2 | |
| 3 | #include <log/log.h> |
Iliyan Malchev | 3db0f60 | 2016-09-26 09:57:20 -0700 | [diff] [blame] | 4 | |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 5 | #include <hardware/hardware.h> |
| 6 | #include <hardware/nfc.h> |
| 7 | #include "Nfc.h" |
| 8 | |
| 9 | namespace android { |
| 10 | namespace hardware { |
| 11 | namespace nfc { |
| 12 | namespace V1_0 { |
| 13 | namespace implementation { |
| 14 | |
| 15 | sp<INfcClientCallback> Nfc::mCallback = NULL; |
| 16 | |
Martijn Coenen | 85ca130 | 2017-02-23 17:24:20 +0100 | [diff] [blame] | 17 | Nfc::Nfc(nfc_nci_device_t* device) : mDevice(device), |
| 18 | mDeathRecipient(new NfcDeathRecipient(this)) { |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | // Methods from ::android::hardware::nfc::V1_0::INfc follow. |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 22 | ::android::hardware::Return<NfcStatus> Nfc::open(const sp<INfcClientCallback>& clientCallback) { |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 23 | mCallback = clientCallback; |
Martijn Coenen | 85ca130 | 2017-02-23 17:24:20 +0100 | [diff] [blame] | 24 | mCallback->linkToDeath(mDeathRecipient, 0 /*cookie*/); |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 25 | int ret = mDevice->open(mDevice, eventCallback, dataCallback); |
| 26 | return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED; |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 29 | ::android::hardware::Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) { |
Steven Moreland | a74426d | 2016-09-26 12:41:23 -0700 | [diff] [blame] | 30 | return mDevice->write(mDevice, data.size(), &data[0]); |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 33 | ::android::hardware::Return<NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) { |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 34 | hidl_vec<uint8_t> copy = data; |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 35 | int ret = mDevice->core_initialized(mDevice, ©[0]); |
| 36 | return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED; |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 39 | ::android::hardware::Return<NfcStatus> Nfc::prediscover() { |
| 40 | return mDevice->pre_discover(mDevice) ? NfcStatus::FAILED : NfcStatus::OK; |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 43 | ::android::hardware::Return<NfcStatus> Nfc::close() { |
Martijn Coenen | 85ca130 | 2017-02-23 17:24:20 +0100 | [diff] [blame] | 44 | mCallback->unlinkToDeath(mDeathRecipient); |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 45 | return mDevice->close(mDevice) ? NfcStatus::FAILED : NfcStatus::OK; |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 48 | ::android::hardware::Return<NfcStatus> Nfc::controlGranted() { |
| 49 | return mDevice->control_granted(mDevice) ? NfcStatus::FAILED : NfcStatus::OK; |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ruchi Kandoi | 51068e0 | 2016-11-18 14:11:33 -0800 | [diff] [blame] | 52 | ::android::hardware::Return<NfcStatus> Nfc::powerCycle() { |
| 53 | return mDevice->power_cycle(mDevice) ? NfcStatus::FAILED : NfcStatus::OK; |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Martijn Coenen | ce2e656 | 2017-01-12 11:37:25 +0100 | [diff] [blame] | 57 | INfc* HIDL_FETCH_INfc(const char * /*name*/) { |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 58 | nfc_nci_device_t* nfc_device; |
| 59 | int ret = 0; |
| 60 | const hw_module_t* hw_module = NULL; |
| 61 | |
Martijn Coenen | ce2e656 | 2017-01-12 11:37:25 +0100 | [diff] [blame] | 62 | ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module); |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 63 | if (ret == 0) |
| 64 | { |
| 65 | ret = nfc_nci_open (hw_module, &nfc_device); |
| 66 | if (ret != 0) { |
Martijn Coenen | ce2e656 | 2017-01-12 11:37:25 +0100 | [diff] [blame] | 67 | ALOGE ("nfc_nci_open failed: %d", ret); |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | else |
Chris Phoenix | 3b06b22 | 2017-01-18 15:51:05 -0800 | [diff] [blame] | 71 | ALOGE ("hw_get_module %s failed: %d", NFC_NCI_HARDWARE_MODULE_ID, ret); |
Iliyan Malchev | 678fa1f | 2016-09-22 15:53:53 -0700 | [diff] [blame] | 72 | |
| 73 | if (ret == 0) { |
| 74 | return new Nfc(nfc_device); |
| 75 | } else { |
| 76 | ALOGE("Passthrough failed to load legacy HAL."); |
| 77 | return nullptr; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } // namespace implementation |
| 82 | } // namespace V1_0 |
| 83 | } // namespace nfc |
| 84 | } // namespace hardware |
| 85 | } // namespace android |