1 /******************************************************************************
2  *
3  *  Copyright 2018, 2023-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 
24 #include "NfcExtns.h"
25 #include "halimpl/inc/phNxpNciHal_Adaptation.h"
26 #include "phNfcStatus.h"
27 
28 #define CHK_STATUS(x) \
29   ((x) == NFCSTATUS_SUCCESS) ? (V1_0::NfcStatus::OK) : (V1_0::NfcStatus::FAILED)
30 
31 extern bool nfc_debug_enabled;
32 
33 namespace android {
34 namespace hardware {
35 namespace nfc {
36 namespace V1_1 {
37 namespace implementation {
38 
39 sp<V1_1::INfcClientCallback> Nfc::mCallbackV1_1 = nullptr;
40 sp<V1_0::INfcClientCallback> Nfc::mCallbackV1_0 = nullptr;
41 
open_1_1(const sp<V1_1::INfcClientCallback> & clientCallback)42 Return<V1_0::NfcStatus> Nfc::open_1_1(
43     const sp<V1_1::INfcClientCallback>& clientCallback) {
44   if (clientCallback == nullptr) {
45     ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
46     return V1_0::NfcStatus::FAILED;
47   } else {
48     mCallbackV1_1 = clientCallback;
49     mCallbackV1_1->linkToDeath(this, 0 /*cookie*/);
50   }
51   return open(clientCallback);
52 }
53 
54 // Methods from ::android::hardware::nfc::V1_0::INfc follow.
open(const sp<V1_0::INfcClientCallback> & clientCallback)55 Return<V1_0::NfcStatus> Nfc::open(
56     const sp<V1_0::INfcClientCallback>& clientCallback) {
57   ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
58   if (clientCallback == nullptr) {
59     ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
60     return V1_0::NfcStatus::FAILED;
61   } else {
62     mCallbackV1_0 = clientCallback;
63     mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
64   }
65 
66   NFCSTATUS status = phNxpNciHal_open(eventCallback, dataCallback);
67   ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
68   return CHK_STATUS(status);
69 }
70 
write(const hidl_vec<uint8_t> & data)71 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
72   hidl_vec<uint8_t> copy = data;
73   return phNxpNciHal_write(copy.size(), &copy[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   NFCSTATUS status = phNxpNciHal_core_initialized(copy.size(), &copy[0]);
79   return CHK_STATUS(status);
80 }
81 
prediscover()82 Return<V1_0::NfcStatus> Nfc::prediscover() { return V1_0::NfcStatus::OK; }
83 
close()84 Return<V1_0::NfcStatus> Nfc::close() {
85   if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
86     return V1_0::NfcStatus::FAILED;
87   }
88   NFCSTATUS status = phNxpNciHal_close(false);
89 
90   if (mCallbackV1_1 != nullptr) {
91     mCallbackV1_1->unlinkToDeath(this);
92     mCallbackV1_1 = nullptr;
93   }
94   if (mCallbackV1_0 != nullptr) {
95     mCallbackV1_0->unlinkToDeath(this);
96     mCallbackV1_0 = nullptr;
97   }
98   return CHK_STATUS(status);
99 }
100 
controlGranted()101 Return<V1_0::NfcStatus> Nfc::controlGranted() {
102   NFCSTATUS status = phNxpNciHal_control_granted();
103   return CHK_STATUS(status);
104 }
105 
powerCycle()106 Return<V1_0::NfcStatus> Nfc::powerCycle() {
107   NFCSTATUS status = phNxpNciHal_power_cycle();
108   return CHK_STATUS(status);
109 }
110 
111 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()112 Return<void> Nfc::factoryReset() {
113   phNxpNciHal_do_factory_reset();
114   return Void();
115 }
116 
closeForPowerOffCase()117 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
118   if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
119     return V1_0::NfcStatus::FAILED;
120   }
121   NFCSTATUS status = phNxpNciHal_configDiscShutdown();
122 
123   if (mCallbackV1_1 != nullptr) {
124     mCallbackV1_1->unlinkToDeath(this);
125     mCallbackV1_1 = nullptr;
126   }
127   if (mCallbackV1_0 != nullptr) {
128     mCallbackV1_0->unlinkToDeath(this);
129     mCallbackV1_0 = nullptr;
130   }
131   return CHK_STATUS(status);
132 }
133 
getConfig(getConfig_cb hidl_cb)134 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
135   NfcConfig nfcVendorConfig;
136   NfcExtns nfcExtns;
137   nfcExtns.getConfig(nfcVendorConfig);
138   hidl_cb(nfcVendorConfig);
139   return Void();
140 }
141 
142 }  // namespace implementation
143 }  // namespace V1_1
144 }  // namespace nfc
145 }  // namespace hardware
146 }  // namespace android
147