1 /******************************************************************************
2 *
3 * Copyright 2018 NXP
4 * Copyright 2019 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 "android.hardware.nfc@1.2-impl"
21 #include <log/log.h>
22 #include "Nfc.h"
23 #include "StNfc_hal_api.h"
24
25
26
27 #define CHK_STATUS(x) ((x) == NFCSTATUS_SUCCESS) \
28 ? (V1_0::NfcStatus::OK) : (V1_0::NfcStatus::FAILED)
29
30 bool nfc_debug_enabled = true;
31
32 namespace android {
33 namespace hardware {
34 namespace nfc {
35 namespace V1_2 {
36 namespace implementation {
37
38 sp<V1_1::INfcClientCallback> Nfc::mCallbackV1_1 = nullptr;
39 sp<V1_0::INfcClientCallback> Nfc::mCallbackV1_0 = nullptr;
40
open_1_1(const sp<V1_1::INfcClientCallback> & clientCallback)41 Return<V1_0::NfcStatus> Nfc::open_1_1(
42 const sp<V1_1::INfcClientCallback>& clientCallback) {
43 if (clientCallback == nullptr) {
44 ALOGD_IF(nfc_debug_enabled,"Nfc::open null callback");
45 return V1_0::NfcStatus::FAILED;
46 } else {
47 pthread_mutex_lock(&mLockOpenClose);
48 mOpenCount++;
49 mCallbackV1_1 = clientCallback;
50 mCallbackV1_1->linkToDeath(this, mOpenCount /*cookie*/);
51 pthread_mutex_unlock(&mLockOpenClose);
52 }
53 return open(clientCallback);
54 }
55
56 // Methods from ::android::hardware::nfc::V1_0::INfc follow.
open(const sp<V1_0::INfcClientCallback> & clientCallback)57 Return<V1_0::NfcStatus> Nfc::open(
58 const sp<V1_0::INfcClientCallback>& clientCallback) {
59 ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
60 pthread_mutex_lock(&mLockOpenClose);
61 if (mCallbackV1_1 == nullptr) mOpenCount++;
62 if (clientCallback == nullptr) {
63 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
64 pthread_mutex_unlock(&mLockOpenClose);
65 return V1_0::NfcStatus::FAILED;
66 } else {
67 mCallbackV1_0 = clientCallback;
68 mCallbackV1_0->linkToDeath(this, mOpenCount /*cookie*/);
69 }
70
71 int ret = StNfc_hal_open(eventCallback, dataCallback);
72 ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit (count:%llu)", (unsigned long long)mOpenCount);
73 pthread_mutex_unlock(&mLockOpenClose);
74 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
75 }
76
write(const hidl_vec<uint8_t> & data)77 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
78 hidl_vec<uint8_t> copy = data;
79
80 return StNfc_hal_write(data.size(), &data[0]);
81 }
82
coreInitialized(const hidl_vec<uint8_t> & data)83 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
84 hidl_vec<uint8_t> copy = data;
85 int ret;
86
87 if (copy.size() == 0)
88 ret = 1;
89 else
90 ret = StNfc_hal_core_initialized(©[0]);
91 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
92 }
93
prediscover()94 Return<V1_0::NfcStatus> Nfc::prediscover() {
95 return StNfc_hal_pre_discover() ? V1_0::NfcStatus::FAILED
96 : V1_0::NfcStatus::OK;
97 }
98
close()99 Return<V1_0::NfcStatus> Nfc::close() {
100 pthread_mutex_lock(&mLockOpenClose);
101 int ret = StNfc_hal_close(NFC_MODE_OFF);
102
103 if (mCallbackV1_1 != nullptr) {
104 mCallbackV1_1->unlinkToDeath(this);
105 mCallbackV1_1 = nullptr;
106 }
107 if (mCallbackV1_0 != nullptr) {
108 mCallbackV1_0->unlinkToDeath(this);
109 mCallbackV1_0 = nullptr;
110 }
111 pthread_mutex_unlock(&mLockOpenClose);
112 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
113 }
114
controlGranted()115 Return<V1_0::NfcStatus> Nfc::controlGranted() {
116 return StNfc_hal_control_granted() ? V1_0::NfcStatus::FAILED
117 : V1_0::NfcStatus::OK;
118 }
119
powerCycle()120 Return<V1_0::NfcStatus> Nfc::powerCycle() {
121 return StNfc_hal_power_cycle() ? V1_0::NfcStatus::FAILED
122 : V1_0::NfcStatus::OK;
123 }
124
125 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()126 Return<void> Nfc::factoryReset() {
127 StNfc_hal_factoryReset();
128 return Void();
129 }
130
closeForPowerOffCase()131 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
132 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
133 return V1_0::NfcStatus::FAILED;
134 }
135
136 pthread_mutex_lock(&mLockOpenClose);
137 int ret = StNfc_hal_closeForPowerOffCase();
138
139 if (mCallbackV1_1 != nullptr) {
140 mCallbackV1_1->unlinkToDeath(this);
141 mCallbackV1_1 = nullptr;
142 }
143 if (mCallbackV1_0 != nullptr) {
144 mCallbackV1_0->unlinkToDeath(this);
145 mCallbackV1_0 = nullptr;
146 }
147 pthread_mutex_unlock(&mLockOpenClose);
148 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
149 }
150
getConfig(getConfig_cb hidl_cb)151 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
152 android::hardware::nfc::V1_1::NfcConfig nfcVendorConfig;
153 StNfc_hal_getConfig(nfcVendorConfig);
154 hidl_cb(nfcVendorConfig);
155 return Void();
156 }
157
getConfig_1_2(getConfig_1_2_cb hidl_cb)158 Return<void> Nfc::getConfig_1_2(getConfig_1_2_cb hidl_cb) {
159 NfcConfig nfcVendorConfig;
160 StNfc_hal_getConfig_1_2(nfcVendorConfig);
161 hidl_cb(nfcVendorConfig);
162 return Void();
163 }
164
165 } // namespace implementation
166 } // namespace V1_2
167 } // namespace nfc
168 } // namespace hardware
169 } // namespace android
170