blob: c61040658789cbc0a8e34be310d5f275ae956cfe [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
15sp<INfcClientCallback> Nfc::mCallback = NULL;
16
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;
Martijn Coenen85ca1302017-02-23 17:24:20 +010024 mCallback->linkToDeath(mDeathRecipient, 0 /*cookie*/);
Ruchi Kandoi51068e02016-11-18 14:11:33 -080025 int ret = mDevice->open(mDevice, eventCallback, dataCallback);
26 return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070027}
28
Ruchi Kandoi51068e02016-11-18 14:11:33 -080029::android::hardware::Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
Steven Morelanda74426d2016-09-26 12:41:23 -070030 return mDevice->write(mDevice, data.size(), &data[0]);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070031}
32
Ruchi Kandoi51068e02016-11-18 14:11:33 -080033::android::hardware::Return<NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070034 hidl_vec<uint8_t> copy = data;
Ruchi Kandoi51068e02016-11-18 14:11:33 -080035 int ret = mDevice->core_initialized(mDevice, &copy[0]);
36 return ret == 0 ? NfcStatus::OK : NfcStatus::FAILED;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070037}
38
Ruchi Kandoi51068e02016-11-18 14:11:33 -080039::android::hardware::Return<NfcStatus> Nfc::prediscover() {
40 return mDevice->pre_discover(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070041}
42
Ruchi Kandoi51068e02016-11-18 14:11:33 -080043::android::hardware::Return<NfcStatus> Nfc::close() {
Martijn Coenen85ca1302017-02-23 17:24:20 +010044 mCallback->unlinkToDeath(mDeathRecipient);
Ruchi Kandoi51068e02016-11-18 14:11:33 -080045 return mDevice->close(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070046}
47
Ruchi Kandoi51068e02016-11-18 14:11:33 -080048::android::hardware::Return<NfcStatus> Nfc::controlGranted() {
49 return mDevice->control_granted(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070050}
51
Ruchi Kandoi51068e02016-11-18 14:11:33 -080052::android::hardware::Return<NfcStatus> Nfc::powerCycle() {
53 return mDevice->power_cycle(mDevice) ? NfcStatus::FAILED : NfcStatus::OK;
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070054}
55
56
Martijn Coenence2e6562017-01-12 11:37:25 +010057INfc* HIDL_FETCH_INfc(const char * /*name*/) {
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070058 nfc_nci_device_t* nfc_device;
59 int ret = 0;
60 const hw_module_t* hw_module = NULL;
61
Martijn Coenence2e6562017-01-12 11:37:25 +010062 ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070063 if (ret == 0)
64 {
65 ret = nfc_nci_open (hw_module, &nfc_device);
66 if (ret != 0) {
Martijn Coenence2e6562017-01-12 11:37:25 +010067 ALOGE ("nfc_nci_open failed: %d", ret);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070068 }
69 }
70 else
Chris Phoenix3b06b222017-01-18 15:51:05 -080071 ALOGE ("hw_get_module %s failed: %d", NFC_NCI_HARDWARE_MODULE_ID, ret);
Iliyan Malchev678fa1f2016-09-22 15:53:53 -070072
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