• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Nfc.h"
22 
23 #include <log/log.h>
24 
25 #include "StNfc_hal_api.h"
26 
27 #define CHK_STATUS(x) \
28   ((x) == NFCSTATUS_SUCCESS) ? (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)",
73            (unsigned long long)mOpenCount);
74   pthread_mutex_unlock(&mLockOpenClose);
75   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
76 }
77 
write(const hidl_vec<uint8_t> & data)78 Return<uint32_t> Nfc::write(const hidl_vec<uint8_t>& data) {
79   hidl_vec<uint8_t> copy = data;
80 
81   return StNfc_hal_write(data.size(), &data[0]);
82 }
83 
coreInitialized(const hidl_vec<uint8_t> & data)84 Return<V1_0::NfcStatus> Nfc::coreInitialized(const hidl_vec<uint8_t>& data) {
85   hidl_vec<uint8_t> copy = data;
86   int ret;
87 
88   if (copy.size() == 0)
89     ret = 1;
90   else
91     ret = StNfc_hal_core_initialized(&copy[0]);
92   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
93 }
94 
prediscover()95 Return<V1_0::NfcStatus> Nfc::prediscover() {
96   return StNfc_hal_pre_discover() ? V1_0::NfcStatus::FAILED
97                                   : V1_0::NfcStatus::OK;
98 }
99 
close()100 Return<V1_0::NfcStatus> Nfc::close() {
101   pthread_mutex_lock(&mLockOpenClose);
102   int ret = StNfc_hal_close(NFC_MODE_OFF);
103 
104   if (mCallbackV1_1 != nullptr) {
105     mCallbackV1_1->unlinkToDeath(this);
106     mCallbackV1_1 = nullptr;
107   }
108   if (mCallbackV1_0 != nullptr) {
109     mCallbackV1_0->unlinkToDeath(this);
110     mCallbackV1_0 = nullptr;
111   }
112   pthread_mutex_unlock(&mLockOpenClose);
113   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
114 }
115 
controlGranted()116 Return<V1_0::NfcStatus> Nfc::controlGranted() {
117   return StNfc_hal_control_granted() ? V1_0::NfcStatus::FAILED
118                                      : V1_0::NfcStatus::OK;
119 }
120 
powerCycle()121 Return<V1_0::NfcStatus> Nfc::powerCycle() {
122   return StNfc_hal_power_cycle() ? V1_0::NfcStatus::FAILED
123                                  : V1_0::NfcStatus::OK;
124 }
125 
126 // Methods from ::android::hardware::nfc::V1_1::INfc follow.
factoryReset()127 Return<void> Nfc::factoryReset() {
128   StNfc_hal_factoryReset();
129   return Void();
130 }
131 
closeForPowerOffCase()132 Return<V1_0::NfcStatus> Nfc::closeForPowerOffCase() {
133   if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
134     return V1_0::NfcStatus::FAILED;
135   }
136 
137   pthread_mutex_lock(&mLockOpenClose);
138   int ret = StNfc_hal_closeForPowerOffCase();
139 
140   if (mCallbackV1_1 != nullptr) {
141     mCallbackV1_1->unlinkToDeath(this);
142     mCallbackV1_1 = nullptr;
143   }
144   if (mCallbackV1_0 != nullptr) {
145     mCallbackV1_0->unlinkToDeath(this);
146     mCallbackV1_0 = nullptr;
147   }
148   pthread_mutex_unlock(&mLockOpenClose);
149   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
150 }
151 
getConfig(getConfig_cb hidl_cb)152 Return<void> Nfc::getConfig(getConfig_cb hidl_cb) {
153   android::hardware::nfc::V1_1::NfcConfig nfcVendorConfig;
154   StNfc_hal_getConfig(nfcVendorConfig);
155   hidl_cb(nfcVendorConfig);
156   return Void();
157 }
158 
getConfig_1_2(getConfig_1_2_cb hidl_cb)159 Return<void> Nfc::getConfig_1_2(getConfig_1_2_cb hidl_cb) {
160   NfcConfig nfcVendorConfig;
161   StNfc_hal_getConfig_1_2(nfcVendorConfig);
162   hidl_cb(nfcVendorConfig);
163   return Void();
164 }
165 
166 }  // namespace implementation
167 }  // namespace V1_2
168 }  // namespace nfc
169 }  // namespace hardware
170 }  // namespace android
171