blob: d337a36e29e41dde582982bdd51c56f8080f64e2 [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
Martijn Coenen85ca1302017-02-23 17:24:20 +010017Nfc::Nfc(nfc_nci_device_t* device) : mDevice(device),
18 mDeathRecipient(new NfcDeathRecipient(this)) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070019}
20
21// Methods from ::android::hardware::nfc::V1_0::INfc follow.
Ruchi Kandoi51068e02016-11-18 14:11:33 -080022::android::hardware::Return<NfcStatus> Nfc::open(const sp<INfcClientCallback>& clientCallback) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070023 mCallback = clientCallback;
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070024
25 if (mDevice == nullptr || mCallback == nullptr) {
26 return NfcStatus::FAILED;
27 }
Martijn Coenen85ca1302017-02-23 17:24:20 +010028 mCallback->linkToDeath(mDeathRecipient, 0 /*cookie*/);
Ruchi Kandoi51068e02016-11-18 14:11:33 -080029 int ret = mDevice->open(mDevice, eventCallback, dataCallback);
30 return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070031}
32
Ruchi Kandoi51068e02016-11-18 14:11:33 -080033::android::hardware::Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070034 if (mDevice == nullptr) {
35 return -1;
36 }
Steven Morelanda74426d2016-09-26 12:41:23 -070037 return mDevice->write(mDevice, data.size(), &data[0]);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070038}
39
Ruchi Kandoi51068e02016-11-18 14:11:33 -080040::android::hardware::Return<NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070041 hidl_vec<uint8_t> copy = data;
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070042
43 if (mDevice == nullptr) {
44 return NfcStatus::FAILED;
45 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080046 int ret = mDevice->core_initialized(mDevice, &copy[0]);
47 return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070048}
49
Ruchi Kandoi51068e02016-11-18 14:11:33 -080050::android::hardware::Return<NfcStatus> Nfc::prediscover() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070051 if (mDevice == nullptr) {
52 return NfcStatus::FAILED;
53 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080054 return mDevice->pre_discover(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070055}
56
Ruchi Kandoi51068e02016-11-18 14:11:33 -080057::android::hardware::Return<NfcStatus> Nfc::close() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070058 if (mDevice == nullptr || mCallback == nullptr) {
59 return NfcStatus::FAILED;
60 }
Martijn Coenen85ca1302017-02-23 17:24:20 +010061 mCallback->unlinkToDeath(mDeathRecipient);
Ruchi Kandoi51068e02016-11-18 14:11:33 -080062 return mDevice->close(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070063}
64
Ruchi Kandoi51068e02016-11-18 14:11:33 -080065::android::hardware::Return<NfcStatus> Nfc::controlGranted() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070066 if (mDevice == nullptr) {
67 return NfcStatus::FAILED;
68 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080069 return mDevice->control_granted(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070070}
71
Ruchi Kandoi51068e02016-11-18 14:11:33 -080072::android::hardware::Return<NfcStatus> Nfc::powerCycle() {
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070073 if (mDevice == nullptr) {
74 return NfcStatus::FAILED;
75 }
Ruchi Kandoi51068e02016-11-18 14:11:33 -080076 return mDevice->power_cycle(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070077}
78
79
Martijn Coenence2e6562017-01-12 11:37:25 +010080INfc* HIDL_FETCH_INfc(const char * /*name*/) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070081 nfc_nci_device_t* nfc_device;
82 int ret = 0;
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070083 const hw_module_t* hw_module = nullptr;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070084
Martijn Coenence2e6562017-01-12 11:37:25 +010085 ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
Ruchi Kandoiee8314c2017-03-13 14:58:36 -070086 if (ret == 0) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070087 ret = nfc_nci_open (hw_module, &nfc_device);
88 if (ret != 0) {
Martijn Coenence2e6562017-01-12 11:37:25 +010089 ALOGE ("nfc_nci_open failed: %d", ret);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070090 }
91 }
92 else
Chris Phoenix3b06b222017-01-18 15:51:05 -080093 ALOGE ("hw_get_module %s failed: %d", NFC_NCI_HARDWARE_MODULE_ID, ret);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070094
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