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
23 #include <log/log.h>
24
25 #include "StNfc_hal_api.h"
26
27 #define CHK_STATUS(x) \
28 ((x) == NFCSTATUS_SUCCESS) ? (V1_0::NfcStatus::OK) : (V1_0::NfcStatus::FAILED)
29
30 bool nfc_debug_enabled = true;
31
32 namespace android {
33 namespace hardware {
34 namespace nfc {
35 namespace V1_1 {
36 namespace implementation {
37
38 sp<V1_1::INfcClientCallback> Nfc::mCallbackV1_1 = nullptr;
39 sp<V1_0::INfcClientCallback> Nfc::mCallbackV1_0 = nullptr;
40
open_1_1(const sp<V1_1::INfcClientCallback> & clientCallback)41 Return<V1_0::NfcStatus> Nfc::open_1_1(
42 const sp<V1_1::INfcClientCallback>& clientCallback) {
43 if (clientCallback == nullptr) {
44 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
45 return V1_0::NfcStatus::FAILED;
46 } else {
47 mCallbackV1_1 = clientCallback;
48 mCallbackV1_1->linkToDeath(this, 0 /*cookie*/);
49 }
50 return open(clientCallback);
51 }
52
53 // Methods from ::android::hardware::nfc::V1_0::INfc follow.
open(const sp<V1_0::INfcClientCallback> & clientCallback)54 Return<V1_0::NfcStatus> Nfc::open(
55 const sp<V1_0::INfcClientCallback>& clientCallback) {
56 ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
57 if (clientCallback == nullptr) {
58 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
59 return V1_0::NfcStatus::FAILED;
60 } else {
61 mCallbackV1_0 = clientCallback;
62 mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
63 }
64
65 int ret = StNfc_hal_open(eventCallback, dataCallback);
66 ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
67 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
68 }
69
write(const hidl_vec<uint8_t> & data)70 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
71 hidl_vec<uint8_t> copy = data;
72
73 return StNfc_hal_write(data.size(), &data[0]);
74 }
75
coreInitialized(const hidl_vec<uint8_t> & data)76 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
77 hidl_vec<uint8_t> copy = data;
78 int ret;
79
80 if (copy.size() == 0)
81 ret = 1;
82 else
83 ret = StNfc_hal_core_initialized(©[0]);
84 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
85 }
86
prediscover()87 Return<V1_0::NfcStatus> Nfc::prediscover() {
88 return StNfc_hal_pre_discover() ? V1_0::NfcStatus::FAILED
89 : V1_0::NfcStatus::OK;
90 }
91
close()92 Return<V1_0::NfcStatus> Nfc::close() {
93 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
94 return V1_0::NfcStatus::FAILED;
95 }
96
97 int ret = StNfc_hal_close(NFC_MODE_OFF);
98
99 if (mCallbackV1_1 != nullptr) {
100 mCallbackV1_1->unlinkToDeath(this);
101 mCallbackV1_1 = nullptr;
102 }
103 if (mCallbackV1_0 != nullptr) {
104 mCallbackV1_0->unlinkToDeath(this);
105 mCallbackV1_0 = nullptr;
106 }
107 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
108 }
109
controlGranted()110 Return<V1_0::NfcStatus> Nfc::controlGranted() {
111 return StNfc_hal_control_granted() ? V1_0::NfcStatus::FAILED
112 : V1_0::NfcStatus::OK;
113 }
114
powerCycle()115 Return<V1_0::NfcStatus> Nfc::powerCycle() {
116 return StNfc_hal_power_cycle() ? V1_0::NfcStatus::FAILED
117 : V1_0::NfcStatus::OK;
118 }
119
120 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()121 Return<void> Nfc::factoryReset() {
122 StNfc_hal_factoryReset();
123 return Void();
124 }
125
closeForPowerOffCase()126 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
127 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
128 return V1_0::NfcStatus::FAILED;
129 }
130
131 int ret = StNfc_hal_closeForPowerOffCase();
132
133 if (mCallbackV1_1 != nullptr) {
134 mCallbackV1_1->unlinkToDeath(this);
135 mCallbackV1_1 = nullptr;
136 }
137 if (mCallbackV1_0 != nullptr) {
138 mCallbackV1_0->unlinkToDeath(this);
139 mCallbackV1_0 = nullptr;
140 }
141 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
142 }
143
getConfig(getConfig_cb hidl_cb)144 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
145 NfcConfig nfcVendorConfig;
146 StNfc_hal_getConfig(nfcVendorConfig);
147 hidl_cb(nfcVendorConfig);
148 return Void();
149 }
150
151 } // namespace implementation
152 } // namespace V1_1
153 } // namespace nfc
154 } // namespace hardware
155 } // namespace android
156