1 /******************************************************************************
2  *
3  *  Copyright 2018 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 #include <log/log.h>
22 #include "SN100x/halimpl/inc/phNxpNciHal_Adaptation.h"
23 #include "phNfcStatus.h"
24 
25 #define CHK_STATUS(x) \
26   ((x) == NFCSTATUS_SUCCESS) ? (V1_0::NfcStatus::OK) : (V1_0::NfcStatus::FAILED)
27 
28 extern bool nfc_debug_enabled;
29 
30 namespace android {
31 namespace hardware {
32 namespace nfc {
33 namespace V1_1 {
34 namespace implementation {
35 
36 sp<V1_1::INfcClientCallback> Nfc::mCallbackV1_1 = nullptr;
37 sp<V1_0::INfcClientCallback> Nfc::mCallbackV1_0 = nullptr;
38 
open_1_1(const sp<V1_1::INfcClientCallback> & clientCallback)39 Return<V1_0::NfcStatus> Nfc::open_1_1(
40     const sp<V1_1::INfcClientCallback>& clientCallback) {
41   if (clientCallback == nullptr) {
42     ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
43     return V1_0::NfcStatus::FAILED;
44   } else {
45     mCallbackV1_1 = clientCallback;
46     mCallbackV1_1->linkToDeath(this, 0 /*cookie*/);
47   }
48   return open(clientCallback);
49 }
50 
51 // Methods from ::android::hardware::nfc::V1_0::INfc follow.
open(const sp<V1_0::INfcClientCallback> & clientCallback)52 Return<V1_0::NfcStatus> Nfc::open(
53     const sp<V1_0::INfcClientCallback>& clientCallback) {
54   ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
55   if (clientCallback == nullptr) {
56     ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
57     return V1_0::NfcStatus::FAILED;
58   } else {
59     mCallbackV1_0 = clientCallback;
60     mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
61   }
62 
63   NFCSTATUS status = phNxpNciHal_open(eventCallback, dataCallback);
64   ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
65   return CHK_STATUS(status);
66 }
67 
write(const hidl_vec<uint8_t> & data)68 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
69   hidl_vec<uint8_t> copy = data;
70   return phNxpNciHal_write(copy.size(), &copy[0]);
71 }
72 
coreInitialized(const hidl_vec<uint8_t> & data)73 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
74   hidl_vec<uint8_t> copy = data;
75   NFCSTATUS status = phNxpNciHal_core_initialized(&copy[0]);
76   return CHK_STATUS(status);
77 }
78 
prediscover()79 Return<V1_0::NfcStatus> Nfc::prediscover() {
80   NFCSTATUS status = phNxpNciHal_pre_discover();
81   return CHK_STATUS(status);
82 }
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   phNxpNciHal_getVendorConfig(nfcVendorConfig);
137   hidl_cb(nfcVendorConfig);
138   return Void();
139 }
140 
141 }  // namespace implementation
142 }  // namespace V1_1
143 }  // namespace nfc
144 }  // namespace hardware
145 }  // namespace android
146