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