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 mCallbackV1_1 = clientCallback;
48 mCallbackV1_1->linkToDeath(this, 0 /*cookie*/);
49 }
50 return open(clientCallback);
51 }
52
53 // Methods from ::android::hardware::nfc::V1_0::INfc follow.
open(const sp<V1_0::INfcClientCallback> & clientCallback)54 Return<V1_0::NfcStatus> Nfc::open(
55 const sp<V1_0::INfcClientCallback>& clientCallback) {
56 ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
57 if (clientCallback == nullptr) {
58 ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
59 return V1_0::NfcStatus::FAILED;
60 } else {
61 mCallbackV1_0 = clientCallback;
62 mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
63 }
64
65 int ret = StNfc_hal_open(eventCallback, dataCallback);
66 ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
67 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
68 }
69
write(const hidl_vec<uint8_t> & data)70 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
71 hidl_vec<uint8_t> copy = data;
72
73 return StNfc_hal_write(data.size(), &data[0]);
74 }
75
coreInitialized(const hidl_vec<uint8_t> & data)76 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
77 hidl_vec<uint8_t> copy = data;
78 int ret;
79
80 if (copy.size() == 0)
81 ret = 1;
82 else
83 ret = StNfc_hal_core_initialized(©[0]);
84 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
85 }
86
prediscover()87 Return<V1_0::NfcStatus> Nfc::prediscover() {
88 return StNfc_hal_pre_discover() ? V1_0::NfcStatus::FAILED
89 : V1_0::NfcStatus::OK;
90 }
91
close()92 Return<V1_0::NfcStatus> Nfc::close() {
93 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
94 return V1_0::NfcStatus::FAILED;
95 }
96 int ret = StNfc_hal_close(NFC_MODE_OFF);
97
98 if (mCallbackV1_1 != nullptr) {
99 mCallbackV1_1->unlinkToDeath(this);
100 mCallbackV1_1 = nullptr;
101 }
102 if (mCallbackV1_0 != nullptr) {
103 mCallbackV1_0->unlinkToDeath(this);
104 mCallbackV1_0 = nullptr;
105 }
106 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
107 }
108
controlGranted()109 Return<V1_0::NfcStatus> Nfc::controlGranted() {
110 return StNfc_hal_control_granted() ? V1_0::NfcStatus::FAILED
111 : V1_0::NfcStatus::OK;
112 }
113
powerCycle()114 Return<V1_0::NfcStatus> Nfc::powerCycle() {
115 return StNfc_hal_power_cycle() ? V1_0::NfcStatus::FAILED
116 : V1_0::NfcStatus::OK;
117 }
118
119 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()120 Return<void> Nfc::factoryReset() {
121 StNfc_hal_factoryReset();
122 return Void();
123 }
124
closeForPowerOffCase()125 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
126 if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
127 return V1_0::NfcStatus::FAILED;
128 }
129
130 int ret = StNfc_hal_closeForPowerOffCase();
131
132 if (mCallbackV1_1 != nullptr) {
133 mCallbackV1_1->unlinkToDeath(this);
134 mCallbackV1_1 = nullptr;
135 }
136 if (mCallbackV1_0 != nullptr) {
137 mCallbackV1_0->unlinkToDeath(this);
138 mCallbackV1_0 = nullptr;
139 }
140 return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
141 }
142
getConfig(getConfig_cb hidl_cb)143 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
144 android::hardware::nfc::V1_1::NfcConfig nfcVendorConfig;
145 StNfc_hal_getConfig(nfcVendorConfig);
146 hidl_cb(nfcVendorConfig);
147 return Void();
148 }
149
getConfig_1_2(getConfig_1_2_cb hidl_cb)150 Return<void> Nfc::getConfig_1_2(getConfig_1_2_cb hidl_cb) {
151 NfcConfig nfcVendorConfig;
152 StNfc_hal_getConfig_1_2(nfcVendorConfig);
153 hidl_cb(nfcVendorConfig);
154 return Void();
155 }
156
157 } // namespace implementation
158 } // namespace V1_2
159 } // namespace nfc
160 } // namespace hardware
161 } // namespace android
162