1 /******************************************************************************
2 *
3 * Copyright 2019-2023 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 "android.hardware.nfc@1.2-impl"
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() {
85 NFCSTATUS status = phNxpNciHal_pre_discover();
86 return CHK_STATUS(status);
87 }
88
close()89 Return<V1_0::NfcStatus> Nfc::close() {
90 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
91 return V1_0::NfcStatus::FAILED;
92 }
93 NFCSTATUS status = phNxpNciHal_close(false);
94
95 if (mCallbackV1_1 != nullptr) {
96 mCallbackV1_1->unlinkToDeath(this);
97 mCallbackV1_1 = nullptr;
98 }
99 if (mCallbackV1_0 != nullptr) {
100 mCallbackV1_0->unlinkToDeath(this);
101 mCallbackV1_0 = nullptr;
102 }
103 return CHK_STATUS(status);
104 }
105
controlGranted()106 Return<V1_0::NfcStatus> Nfc::controlGranted() {
107 NFCSTATUS status = phNxpNciHal_control_granted();
108 return CHK_STATUS(status);
109 }
110
powerCycle()111 Return<V1_0::NfcStatus> Nfc::powerCycle() {
112 NFCSTATUS status = phNxpNciHal_power_cycle();
113 return CHK_STATUS(status);
114 }
115
116 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()117 Return<void> Nfc::factoryReset() {
118 phNxpNciHal_do_factory_reset();
119 return Void();
120 }
121
closeForPowerOffCase()122 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
123 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
124 return V1_0::NfcStatus::FAILED;
125 }
126 NFCSTATUS status = phNxpNciHal_configDiscShutdown();
127
128 if (mCallbackV1_1 != nullptr) {
129 mCallbackV1_1->unlinkToDeath(this);
130 mCallbackV1_1 = nullptr;
131 }
132 if (mCallbackV1_0 != nullptr) {
133 mCallbackV1_0->unlinkToDeath(this);
134 mCallbackV1_0 = nullptr;
135 }
136 return CHK_STATUS(status);
137 }
138
getConfig(getConfig_cb hidl_cb)139 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
140 android::hardware::nfc::V1_1::NfcConfig nfcVendorConfig;
141 NfcExtns nfcExtns;
142 nfcExtns.getConfig(nfcVendorConfig);
143 hidl_cb(nfcVendorConfig);
144 return Void();
145 }
146
getConfig_1_2(getConfig_1_2_cb hidl_cb)147 Return<void> Nfc::getConfig_1_2(getConfig_1_2_cb hidl_cb) {
148 NfcConfig nfcVendorConfig;
149 NfcExtns nfcExtns;
150 nfcExtns.getConfig(nfcVendorConfig);
151 hidl_cb(nfcVendorConfig);
152 return Void();
153 }
154
serviceDied(uint64_t,const wp<IBase> &)155 void Nfc::serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
156 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
157 return;
158 }
159 phNxpNciHal_close(true);
160
161 if (mCallbackV1_1 != nullptr) {
162 mCallbackV1_1->unlinkToDeath(this);
163 mCallbackV1_1 = nullptr;
164 }
165 if (mCallbackV1_0 != nullptr) {
166 mCallbackV1_0->unlinkToDeath(this);
167 mCallbackV1_0 = nullptr;
168 }
169 }
170
debug(const hidl_handle &,const hidl_vec<hidl_string> &)171 Return<void> Nfc::debug(const hidl_handle& /* fd */,
172 const hidl_vec<hidl_string>& /* options */) {
173 ALOGD_IF(nfc_debug_enabled, "\n Nfc HAL MemoryLeak Info = %s \n",
174 android::GetUnreachableMemoryString(true, 10000).c_str());
175 return Void();
176 }
177
178 } // namespace implementation
179 } // namespace V1_2
180 } // namespace nfc
181 } // namespace hardware
182 } // namespace android
183