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