1 /******************************************************************************
2 *
3 * Copyright 2018 NXP
4 * Copyright 2018 ST Microelectronics S.A.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 #define LOG_TAG "android.hardware.nfc@1.1-impl"
21 #include "Nfc.h"
22 #include <log/log.h>
23 #include "StNfc_hal_api.h"
24
25 #define CHK_STATUS(x) \
26 ((x) == NFCSTATUS_SUCCESS) ? (V1_0::NfcStatus::OK) : (V1_0::NfcStatus::FAILED)
27
28 bool nfc_debug_enabled = true;
29
30 namespace android {
31 namespace hardware {
32 namespace nfc {
33 namespace V1_1 {
34 namespace implementation {
35
36 sp<V1_1::INfcClientCallback> Nfc::mCallbackV1_1 = nullptr;
37 sp<V1_0::INfcClientCallback> Nfc::mCallbackV1_0 = nullptr;
38
open_1_1(const sp<V1_1::INfcClientCallback> & clientCallback)39 Return<V1_0::NfcStatus> Nfc::open_1_1(
40 const sp<V1_1::INfcClientCallback>& clientCallback) {
41 if (clientCallback == nullptr) {
42 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
43 return V1_0::NfcStatus::FAILED;
44 } else {
45 mCallbackV1_1 = clientCallback;
46 mCallbackV1_1->linkToDeath(this, 0 /*cookie*/);
47 }
48 return open(clientCallback);
49 }
50
51 // Methods from ::android::hardware::nfc::V1_0::INfc follow.
open(const sp<V1_0::INfcClientCallback> & clientCallback)52 Return<V1_0::NfcStatus> Nfc::open(
53 const sp<V1_0::INfcClientCallback>& clientCallback) {
54 ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
55 if (clientCallback == nullptr) {
56 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
57 return V1_0::NfcStatus::FAILED;
58 } else {
59 mCallbackV1_0 = clientCallback;
60 mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
61 }
62
63 int ret = StNfc_hal_open(eventCallback, dataCallback);
64 ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
65 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
66 }
67
write(const hidl_vec<uint8_t> & data)68 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
69 hidl_vec<uint8_t> copy = data;
70
71 return StNfc_hal_write(data.size(), &data[0]);
72 }
73
coreInitialized(const hidl_vec<uint8_t> & data)74 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
75 hidl_vec<uint8_t> copy = data;
76 int ret;
77
78 if (copy.size() == 0)
79 ret = 1;
80 else
81 ret = StNfc_hal_core_initialized(©[0]);
82 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
83 }
84
prediscover()85 Return<V1_0::NfcStatus> Nfc::prediscover() {
86 return StNfc_hal_pre_discover() ? V1_0::NfcStatus::FAILED
87 : V1_0::NfcStatus::OK;
88 }
89
close()90 Return<V1_0::NfcStatus> Nfc::close() {
91 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
92 return V1_0::NfcStatus::FAILED;
93 }
94
95 int ret = StNfc_hal_close(NFC_MODE_OFF);
96
97 if (mCallbackV1_1 != nullptr) {
98 mCallbackV1_1->unlinkToDeath(this);
99 mCallbackV1_1 = nullptr;
100 }
101 if (mCallbackV1_0 != nullptr) {
102 mCallbackV1_0->unlinkToDeath(this);
103 mCallbackV1_0 = nullptr;
104 }
105 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
106 }
107
controlGranted()108 Return<V1_0::NfcStatus> Nfc::controlGranted() {
109 return StNfc_hal_control_granted() ? V1_0::NfcStatus::FAILED
110 : V1_0::NfcStatus::OK;
111 }
112
powerCycle()113 Return<V1_0::NfcStatus> Nfc::powerCycle() {
114 return StNfc_hal_power_cycle() ? V1_0::NfcStatus::FAILED
115 : V1_0::NfcStatus::OK;
116 }
117
118 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()119 Return<void> Nfc::factoryReset() {
120 StNfc_hal_factoryReset();
121 return Void();
122 }
123
closeForPowerOffCase()124 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
125 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
126 return V1_0::NfcStatus::FAILED;
127 }
128
129 int ret = StNfc_hal_closeForPowerOffCase();
130
131 if (mCallbackV1_1 != nullptr) {
132 mCallbackV1_1->unlinkToDeath(this);
133 mCallbackV1_1 = nullptr;
134 }
135 if (mCallbackV1_0 != nullptr) {
136 mCallbackV1_0->unlinkToDeath(this);
137 mCallbackV1_0 = nullptr;
138 }
139 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
140 }
141
getConfig(getConfig_cb hidl_cb)142 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
143 NfcConfig nfcVendorConfig;
144 StNfc_hal_getConfig(nfcVendorConfig);
145 hidl_cb(nfcVendorConfig);
146 return Void();
147 }
148
149 } // namespace implementation
150 } // namespace V1_1
151 } // namespace nfc
152 } // namespace hardware
153 } // namespace android
154