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