1 /******************************************************************************
2  *
3  *  Copyright 2018, 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.1-impl"
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   if (mIsServiceStarted) {
58     ALOGD_IF(nfc_debug_enabled, "Nfc::open service is already started");
59     return V1_0::NfcStatus::OK;
60   }
61   ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
62   if (clientCallback == nullptr) {
63     ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
64     return V1_0::NfcStatus::FAILED;
65   } else {
66     mCallbackV1_0 = clientCallback;
67     mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
68   }
69 
70   NFCSTATUS status = phNxpNciHal_open(eventCallback, dataCallback);
71   mIsServiceStarted = true;
72   ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
73   return CHK_STATUS(status);
74 }
75 
write(const hidl_vec<uint8_t> & data)76 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
77   hidl_vec<uint8_t> copy = data;
78   return phNxpNciHal_write(copy.size(), &copy[0]);
79 }
80 
coreInitialized(const hidl_vec<uint8_t> & data)81 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
82   hidl_vec<uint8_t> copy = data;
83   NFCSTATUS status = phNxpNciHal_core_initialized(copy.size(), &copy[0]);
84   return CHK_STATUS(status);
85 }
86 
prediscover()87 Return<V1_0::NfcStatus> Nfc::prediscover() { return V1_0::NfcStatus::OK; }
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   mIsServiceStarted = false;
95 
96   if (mCallbackV1_1 != nullptr) {
97     mCallbackV1_1->unlinkToDeath(this);
98     mCallbackV1_1 = nullptr;
99   }
100   if (mCallbackV1_0 != nullptr) {
101     mCallbackV1_0->unlinkToDeath(this);
102     mCallbackV1_0 = nullptr;
103   }
104   return CHK_STATUS(status);
105 }
106 
controlGranted()107 Return<V1_0::NfcStatus> Nfc::controlGranted() {
108   NFCSTATUS status = phNxpNciHal_control_granted();
109   return CHK_STATUS(status);
110 }
111 
powerCycle()112 Return<V1_0::NfcStatus> Nfc::powerCycle() {
113   NFCSTATUS status = phNxpNciHal_power_cycle();
114   return CHK_STATUS(status);
115 }
116 
117 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()118 Return<void> Nfc::factoryReset() {
119   phNxpNciHal_do_factory_reset();
120   return Void();
121 }
122 
closeForPowerOffCase()123 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
124   if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
125     return V1_0::NfcStatus::FAILED;
126   }
127   NFCSTATUS status = phNxpNciHal_configDiscShutdown();
128   mIsServiceStarted = false;
129 
130   if (mCallbackV1_1 != nullptr) {
131     mCallbackV1_1->unlinkToDeath(this);
132     mCallbackV1_1 = nullptr;
133   }
134   if (mCallbackV1_0 != nullptr) {
135     mCallbackV1_0->unlinkToDeath(this);
136     mCallbackV1_0 = nullptr;
137   }
138   return CHK_STATUS(status);
139 }
140 
getConfig(getConfig_cb hidl_cb)141 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
142   NfcConfig nfcVendorConfig;
143   NfcExtns nfcExtns;
144   nfcExtns.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