1 /******************************************************************************
2 *
3 * Copyright 2019-2024 NXP
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #define LOG_TAG "[email protected]"
20 #include "Nfc.h"
21
22 #include <log/log.h>
23 #include <memunreachable/memunreachable.h>
24
25 #include "NfcExtns.h"
26 #include "halimpl/inc/phNxpNciHal_Adaptation.h"
27 #include "phNfcStatus.h"
28 #include "phNxpNciHal_ext.h"
29
30 #define CHK_STATUS(x) \
31 ((x) == NFCSTATUS_SUCCESS) ? (V1_0::NfcStatus::OK) : (V1_0::NfcStatus::FAILED)
32
33 extern bool nfc_debug_enabled;
34
35 namespace android {
36 namespace hardware {
37 namespace nfc {
38 namespace V1_2 {
39 namespace implementation {
40
41 sp<V1_1::INfcClientCallback> Nfc::mCallbackV1_1 = nullptr;
42 sp<V1_0::INfcClientCallback> Nfc::mCallbackV1_0 = nullptr;
43
open_1_1(const sp<V1_1::INfcClientCallback> & clientCallback)44 Return<V1_0::NfcStatus> Nfc::open_1_1(
45 const sp<V1_1::INfcClientCallback>& clientCallback) {
46 if (clientCallback == nullptr) {
47 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
48 return V1_0::NfcStatus::FAILED;
49 } else {
50 mCallbackV1_1 = clientCallback;
51 mCallbackV1_1->linkToDeath(this, 0 /*cookie*/);
52 }
53 return open(clientCallback);
54 }
55
56 // Methods from ::android::hardware::nfc::V1_0::INfc follow.
open(const sp<V1_0::INfcClientCallback> & clientCallback)57 Return<V1_0::NfcStatus> Nfc::open(
58 const sp<V1_0::INfcClientCallback>& clientCallback) {
59 ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
60 if (clientCallback == nullptr) {
61 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
62 return V1_0::NfcStatus::FAILED;
63 } else {
64 mCallbackV1_0 = clientCallback;
65 mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
66 }
67 printNfcMwVersion();
68 NFCSTATUS status = phNxpNciHal_open(eventCallback, dataCallback);
69 ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
70 return CHK_STATUS(status);
71 }
72
write(const hidl_vec<uint8_t> & data)73 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
74 hidl_vec<uint8_t> copy = data;
75 return phNxpNciHal_write(copy.size(), ©[0]);
76 }
77
coreInitialized(const hidl_vec<uint8_t> & data)78 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
79 hidl_vec<uint8_t> copy = data;
80 NFCSTATUS status = phNxpNciHal_core_initialized(copy.size(), ©[0]);
81 return CHK_STATUS(status);
82 }
83
prediscover()84 Return<V1_0::NfcStatus> Nfc::prediscover() { return V1_0::NfcStatus::OK; }
85
close()86 Return<V1_0::NfcStatus> Nfc::close() {
87 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
88 return V1_0::NfcStatus::FAILED;
89 }
90 NFCSTATUS status = phNxpNciHal_close(false);
91
92 if (mCallbackV1_1 != nullptr) {
93 mCallbackV1_1->unlinkToDeath(this);
94 mCallbackV1_1 = nullptr;
95 }
96 if (mCallbackV1_0 != nullptr) {
97 mCallbackV1_0->unlinkToDeath(this);
98 mCallbackV1_0 = nullptr;
99 }
100 return CHK_STATUS(status);
101 }
102
controlGranted()103 Return<V1_0::NfcStatus> Nfc::controlGranted() {
104 NFCSTATUS status = phNxpNciHal_control_granted();
105 return CHK_STATUS(status);
106 }
107
powerCycle()108 Return<V1_0::NfcStatus> Nfc::powerCycle() {
109 NFCSTATUS status = phNxpNciHal_power_cycle();
110 return CHK_STATUS(status);
111 }
112
113 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()114 Return<void> Nfc::factoryReset() {
115 phNxpNciHal_do_factory_reset();
116 return Void();
117 }
118
closeForPowerOffCase()119 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
120 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
121 return V1_0::NfcStatus::FAILED;
122 }
123 NFCSTATUS status = phNxpNciHal_configDiscShutdown();
124
125 if (mCallbackV1_1 != nullptr) {
126 mCallbackV1_1->unlinkToDeath(this);
127 mCallbackV1_1 = nullptr;
128 }
129 if (mCallbackV1_0 != nullptr) {
130 mCallbackV1_0->unlinkToDeath(this);
131 mCallbackV1_0 = nullptr;
132 }
133 return CHK_STATUS(status);
134 }
135
getConfig(getConfig_cb hidl_cb)136 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
137 android::hardware::nfc::V1_1::NfcConfig nfcVendorConfig;
138 NfcExtns nfcExtns;
139 nfcExtns.getConfig(nfcVendorConfig);
140 hidl_cb(nfcVendorConfig);
141 return Void();
142 }
143
getConfig_1_2(getConfig_1_2_cb hidl_cb)144 Return<void> Nfc::getConfig_1_2(getConfig_1_2_cb hidl_cb) {
145 NfcConfig nfcVendorConfig;
146 NfcExtns nfcExtns;
147 nfcExtns.getConfig(nfcVendorConfig);
148 hidl_cb(nfcVendorConfig);
149 return Void();
150 }
151
serviceDied(uint64_t,const wp<IBase> &)152 void Nfc::serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
153 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
154 return;
155 }
156 phNxpNciHal_close(true);
157
158 if (mCallbackV1_1 != nullptr) {
159 mCallbackV1_1->unlinkToDeath(this);
160 mCallbackV1_1 = nullptr;
161 }
162 if (mCallbackV1_0 != nullptr) {
163 mCallbackV1_0->unlinkToDeath(this);
164 mCallbackV1_0 = nullptr;
165 }
166 }
167
debug(const hidl_handle &,const hidl_vec<hidl_string> &)168 Return<void> Nfc::debug(const hidl_handle& /* fd */,
169 const hidl_vec<hidl_string>& /* options */) {
170 ALOGD_IF(nfc_debug_enabled, "\n Nfc HAL MemoryLeak Info = %s \n",
171 android::GetUnreachableMemoryString(true, 10000).c_str());
172 return Void();
173 }
174
175 } // namespace implementation
176 } // namespace V1_2
177 } // namespace nfc
178 } // namespace hardware
179 } // namespace android
180