1 /******************************************************************************
2  *
3  *  Copyright 2018 NXP
4  *  Copyright 2018 ST Microelectronics S.A.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 #define LOG_TAG "[email protected]"
21 #include "Nfc.h"
22 #include <log/log.h>
23 #include "StNfc_hal_api.h"
24 
25 #define CHK_STATUS(x) \
26   ((x) == NFCSTATUS_SUCCESS) ? (V1_0::NfcStatus::OK) : (V1_0::NfcStatus::FAILED)
27 
28 bool nfc_debug_enabled = true;
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   int ret = StNfc_hal_open(eventCallback, dataCallback);
64   ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
65   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
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 
71   return StNfc_hal_write(data.size(), &data[0]);
72 }
73 
coreInitialized(const hidl_vec<uint8_t> & data)74 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
75   hidl_vec<uint8_t> copy = data;
76 
77   int ret = StNfc_hal_core_initialized(&copy[0]);
78   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
79 }
80 
prediscover()81 Return<V1_0::NfcStatus> Nfc::prediscover() {
82   return StNfc_hal_pre_discover() ? V1_0::NfcStatus::FAILED
83                                   : V1_0::NfcStatus::OK;
84 }
85 
close()86 Return<V1_0::NfcStatus> Nfc::close() {
87   if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
88     return V1_0::NfcStatus::FAILED;
89   }
90 
91   int ret = StNfc_hal_close(NFC_MODE_OFF);
92 
93   if (mCallbackV1_1 != nullptr) {
94     mCallbackV1_1->unlinkToDeath(this);
95     mCallbackV1_1 = nullptr;
96   }
97   if (mCallbackV1_0 != nullptr) {
98     mCallbackV1_0->unlinkToDeath(this);
99     mCallbackV1_0 = nullptr;
100   }
101   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
102 }
103 
controlGranted()104 Return<V1_0::NfcStatus> Nfc::controlGranted() {
105   return StNfc_hal_control_granted() ? V1_0::NfcStatus::FAILED
106                                      : V1_0::NfcStatus::OK;
107 }
108 
powerCycle()109 Return<V1_0::NfcStatus> Nfc::powerCycle() {
110   return StNfc_hal_power_cycle() ? V1_0::NfcStatus::FAILED
111                                  : V1_0::NfcStatus::OK;
112 }
113 
114 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()115 Return<void> Nfc::factoryReset() {
116   StNfc_hal_factoryReset();
117   return Void();
118 }
119 
closeForPowerOffCase()120 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
121   if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
122     return V1_0::NfcStatus::FAILED;
123   }
124 
125   int ret = StNfc_hal_closeForPowerOffCase();
126 
127   if (mCallbackV1_1 != nullptr) {
128     mCallbackV1_1->unlinkToDeath(this);
129     mCallbackV1_1 = nullptr;
130   }
131   if (mCallbackV1_0 != nullptr) {
132     mCallbackV1_0->unlinkToDeath(this);
133     mCallbackV1_0 = nullptr;
134   }
135   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
136 }
137 
getConfig(getConfig_cb hidl_cb)138 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
139   NfcConfig nfcVendorConfig;
140   StNfc_hal_getConfig(nfcVendorConfig);
141   hidl_cb(nfcVendorConfig);
142   return Void();
143 }
144 
145 }  // namespace implementation
146 }  // namespace V1_1
147 }  // namespace nfc
148 }  // namespace hardware
149 }  // namespace android
150