blob: fcdcbbc301dda3cf837010a494a01c20529bfee4 [file] [log] [blame]
Iliyan Malchev3db0f602016-09-26 09:57:20 -07001#define LOG_TAG "android.hardware.nfc@1.0-impl"
Mark Salyzyna4842ac2017-01-10 10:16:48 -08002
3#include <log/log.h>
Iliyan Malchev3db0f602016-09-26 09:57:20 -07004
Iliyan Malchev678fa1f2016-09-22 15:53:53 -07005#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
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070015sp<INfcClientCallback> Nfc::mCallback = nullptr;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070016
Ruchi Kandoi461dedd2017-11-10 08:22:48 -080017Nfc::Nfc(nfc_nci_device_t* device) : mDevice(device) {}
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070018
19// Methods from ::android::hardware::nfc::V1_0::INfc follow.
Ruchi Kandoi51068e02016-11-18 14:11:33 -080020::android::hardware::Return<NfcStatus> Nfc::open(const sp<INfcClientCallback>& clientCallback) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070021 mCallback = clientCallback;
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070022
23 if (mDevice == nullptr || mCallback == nullptr) {
24 return NfcStatus::FAILED;
25 }
Ruchi Kandoi461dedd2017-11-10 08:22:48 -080026 mCallback->linkToDeath(this, 0 /*cookie*/);
Ruchi Kandoi51068e02016-11-18 14:11:33 -080027 int ret = mDevice->open(mDevice, eventCallback, dataCallback);
28 return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070029}
30
Ruchi Kandoi51068e02016-11-18 14:11:33 -080031::android::hardware::Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070032 if (mDevice == nullptr) {
33 return -1;
34 }
Steven Morelanda74426d2016-09-26 12:41:23 -070035 return mDevice->write(mDevice, data.size(), &data[0]);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070036}
37
Ruchi Kandoi51068e02016-11-18 14:11:33 -080038::android::hardware::Return<NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070039 hidl_vec<uint8_t> copy = data;
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070040
41 if (mDevice == nullptr) {
42 return NfcStatus::FAILED;
43 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080044 int ret = mDevice->core_initialized(mDevice, &copy[0]);
45 return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070046}
47
Ruchi Kandoi51068e02016-11-18 14:11:33 -080048::android::hardware::Return<NfcStatus> Nfc::prediscover() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070049 if (mDevice == nullptr) {
50 return NfcStatus::FAILED;
51 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080052 return mDevice->pre_discover(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070053}
54
Ruchi Kandoi51068e02016-11-18 14:11:33 -080055::android::hardware::Return<NfcStatus> Nfc::close() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070056 if (mDevice == nullptr || mCallback == nullptr) {
57 return NfcStatus::FAILED;
58 }
Ruchi Kandoi461dedd2017-11-10 08:22:48 -080059 mCallback->unlinkToDeath(this);
Ruchi Kandoi51068e02016-11-18 14:11:33 -080060 return mDevice->close(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070061}
62
Ruchi Kandoi51068e02016-11-18 14:11:33 -080063::android::hardware::Return<NfcStatus> Nfc::controlGranted() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070064 if (mDevice == nullptr) {
65 return NfcStatus::FAILED;
66 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080067 return mDevice->control_granted(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070068}
69
Ruchi Kandoi51068e02016-11-18 14:11:33 -080070::android::hardware::Return<NfcStatus> Nfc::powerCycle() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070071 if (mDevice == nullptr) {
72 return NfcStatus::FAILED;
73 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080074 return mDevice->power_cycle(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070075}
76
77
Martijn Coenence2e6562017-01-12 11:37:25 +010078INfc* HIDL_FETCH_INfc(const char * /*name*/) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070079 nfc_nci_device_t* nfc_device;
80 int ret = 0;
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070081 const hw_module_t* hw_module = nullptr;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070082
Martijn Coenence2e6562017-01-12 11:37:25 +010083 ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070084 if (ret == 0) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070085 ret = nfc_nci_open (hw_module, &nfc_device);
86 if (ret != 0) {
Martijn Coenence2e6562017-01-12 11:37:25 +010087 ALOGE ("nfc_nci_open failed: %d", ret);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070088 }
89 }
90 else
Chris Phoenix3b06b222017-01-18 15:51:05 -080091 ALOGE ("hw_get_module %s failed: %d", NFC_NCI_HARDWARE_MODULE_ID, ret);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070092
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