The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 1999-2012 Broadcom Corporation |
| 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 | ******************************************************************************/ |
Ruchi Kandoi | d03c06e | 2017-01-26 15:32:03 -0800 | [diff] [blame] | 18 | #include "_OverrideLog.h" |
| 19 | |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 20 | #include <android/hardware/nfc/1.0/INfc.h> |
| 21 | #include <android/hardware/nfc/1.0/INfcClientCallback.h> |
| 22 | #include <hwbinder/ProcessState.h> |
| 23 | #include <pthread.h> |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 24 | #include "NfcAdaptation.h" |
| 25 | extern "C" |
| 26 | { |
| 27 | #include "gki.h" |
| 28 | #include "nfa_api.h" |
| 29 | #include "nfc_int.h" |
Jizhou Liao | acf1ec0 | 2015-06-30 10:25:41 -0700 | [diff] [blame] | 30 | #include "vendor_cfg.h" |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 31 | } |
| 32 | #include "config.h" |
Evan Chu | a24be4f | 2013-11-13 15:30:16 -0500 | [diff] [blame] | 33 | #include "android_logmsg.h" |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 34 | |
Jizhou Liao | 65ebec5 | 2016-04-05 17:09:24 -0700 | [diff] [blame] | 35 | #undef LOG_TAG |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 36 | #define LOG_TAG "NfcAdaptation" |
| 37 | |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 38 | using android::OK; |
| 39 | using android::sp; |
| 40 | using android::status_t; |
| 41 | |
| 42 | using android::hardware::ProcessState; |
| 43 | using android::hardware::Return; |
| 44 | using android::hardware::Void; |
| 45 | using android::hardware::nfc::V1_0::INfc; |
| 46 | using android::hardware::nfc::V1_0::INfcClientCallback; |
| 47 | using android::hardware::hidl_vec; |
| 48 | |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 49 | extern "C" void GKI_shutdown(); |
| 50 | extern void resetConfig(); |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 51 | extern "C" void verify_stack_non_volatile_store (); |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 52 | extern "C" void delete_stack_non_volatile_store (bool forceDelete); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 53 | |
| 54 | NfcAdaptation* NfcAdaptation::mpInstance = NULL; |
| 55 | ThreadMutex NfcAdaptation::sLock; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 56 | tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = NULL; |
| 57 | tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = NULL; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 58 | ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent; |
| 59 | ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent; |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 60 | sp<INfc> NfcAdaptation::mHal; |
| 61 | INfcClientCallback* NfcAdaptation::mCallback; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 62 | |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 63 | uint32_t ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; //0x017F00; |
| 64 | uint8_t appl_trace_level = 0xff; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 65 | char bcm_nfc_location[120]; |
| 66 | |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 67 | static uint8_t nfa_dm_cfg[sizeof ( tNFA_DM_CFG ) ]; |
| 68 | static uint8_t nfa_proprietary_cfg[sizeof ( tNFA_PROPRIETARY_CFG )]; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 69 | extern tNFA_DM_CFG *p_nfa_dm_cfg; |
Jizhou Liao | acf1ec0 | 2015-06-30 10:25:41 -0700 | [diff] [blame] | 70 | extern tNFA_PROPRIETARY_CFG *p_nfa_proprietary_cfg; |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 71 | extern uint8_t nfa_ee_max_ee_cfg; |
| 72 | extern const uint8_t nfca_version_string []; |
| 73 | extern const uint8_t nfa_version_string []; |
| 74 | static uint8_t deviceHostWhiteList [NFA_HCI_MAX_HOST_IN_NETWORK]; |
Evan Chu | b7cb567 | 2014-02-07 15:01:21 -0500 | [diff] [blame] | 75 | static tNFA_HCI_CFG jni_nfa_hci_cfg; |
| 76 | extern tNFA_HCI_CFG *p_nfa_hci_cfg; |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 77 | extern bool nfa_poll_bail_out_mode; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 78 | |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 79 | class NfcClientCallback : public INfcClientCallback { |
| 80 | public: |
| 81 | NfcClientCallback(tHAL_NFC_CBACK* eventCallback, tHAL_NFC_DATA_CBACK dataCallback) { |
| 82 | mEventCallback = eventCallback; |
| 83 | mDataCallback = dataCallback; |
| 84 | }; |
| 85 | virtual ~NfcClientCallback() = default; |
| 86 | Return<void> sendEvent( |
Steven Moreland | 98cd45d | 2016-09-29 11:18:21 -0700 | [diff] [blame] | 87 | ::android::hardware::nfc::V1_0::NfcEvent event, |
| 88 | ::android::hardware::nfc::V1_0::NfcStatus event_status) override { |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 89 | mEventCallback ((uint8_t)event, (tHAL_NFC_STATUS) event_status); |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 90 | return Void(); |
| 91 | }; |
Steven Moreland | 98cd45d | 2016-09-29 11:18:21 -0700 | [diff] [blame] | 92 | Return<void> sendData(const ::android::hardware::nfc::V1_0::NfcData &data ) override { |
| 93 | ::android::hardware::nfc::V1_0::NfcData copy = data; |
| 94 | mDataCallback(copy.size(), ©[0]); |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 95 | return Void(); |
| 96 | }; |
| 97 | private: |
| 98 | tHAL_NFC_CBACK* mEventCallback; |
| 99 | tHAL_NFC_DATA_CBACK* mDataCallback; |
| 100 | }; |
| 101 | |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 102 | /******************************************************************************* |
| 103 | ** |
| 104 | ** Function: NfcAdaptation::NfcAdaptation() |
| 105 | ** |
| 106 | ** Description: class constructor |
| 107 | ** |
| 108 | ** Returns: none |
| 109 | ** |
| 110 | *******************************************************************************/ |
| 111 | NfcAdaptation::NfcAdaptation() |
| 112 | { |
Evan Chu | 1569641 | 2014-07-16 16:40:22 -0400 | [diff] [blame] | 113 | memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs)); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /******************************************************************************* |
| 117 | ** |
| 118 | ** Function: NfcAdaptation::~NfcAdaptation() |
| 119 | ** |
| 120 | ** Description: class destructor |
| 121 | ** |
| 122 | ** Returns: none |
| 123 | ** |
| 124 | *******************************************************************************/ |
| 125 | NfcAdaptation::~NfcAdaptation() |
| 126 | { |
| 127 | mpInstance = NULL; |
| 128 | } |
| 129 | |
| 130 | /******************************************************************************* |
| 131 | ** |
| 132 | ** Function: NfcAdaptation::GetInstance() |
| 133 | ** |
| 134 | ** Description: access class singleton |
| 135 | ** |
| 136 | ** Returns: pointer to the singleton object |
| 137 | ** |
| 138 | *******************************************************************************/ |
| 139 | NfcAdaptation& NfcAdaptation::GetInstance() |
| 140 | { |
| 141 | AutoThreadMutex a(sLock); |
| 142 | |
| 143 | if (!mpInstance) |
| 144 | mpInstance = new NfcAdaptation; |
| 145 | return *mpInstance; |
| 146 | } |
| 147 | |
| 148 | /******************************************************************************* |
| 149 | ** |
| 150 | ** Function: NfcAdaptation::Initialize() |
| 151 | ** |
| 152 | ** Description: class initializer |
| 153 | ** |
| 154 | ** Returns: none |
| 155 | ** |
| 156 | *******************************************************************************/ |
| 157 | void NfcAdaptation::Initialize () |
| 158 | { |
| 159 | const char* func = "NfcAdaptation::Initialize"; |
Evan Chu | 0976d37 | 2013-04-30 14:49:29 -0400 | [diff] [blame] | 160 | ALOGD("%s: enter", func); |
| 161 | ALOGE("%s: ver=%s nfa=%s", func, nfca_version_string, nfa_version_string); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 162 | unsigned long num; |
| 163 | |
Evan Chu | a24be4f | 2013-11-13 15:30:16 -0500 | [diff] [blame] | 164 | if ( GetNumValue ( NAME_USE_RAW_NCI_TRACE, &num, sizeof ( num ) ) ) |
| 165 | { |
| 166 | if (num == 1) |
| 167 | { |
| 168 | // display protocol traces in raw format |
| 169 | ProtoDispAdapterUseRawOutput (TRUE); |
| 170 | ALOGD("%s: logging protocol in raw format", func); |
| 171 | } |
| 172 | } |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 173 | if ( !GetStrValue ( NAME_NFA_STORAGE, bcm_nfc_location, sizeof ( bcm_nfc_location ) ) ) |
Evan Chu | d8f9332 | 2013-05-10 09:59:20 -0400 | [diff] [blame] | 174 | { |
Martijn Coenen | 077f48f | 2014-07-24 17:32:51 -0700 | [diff] [blame] | 175 | strlcpy (bcm_nfc_location, "/data/nfc", sizeof(bcm_nfc_location)); |
Evan Chu | d8f9332 | 2013-05-10 09:59:20 -0400 | [diff] [blame] | 176 | } |
Andres Morales | e4ecc7d | 2014-10-01 17:46:04 -0700 | [diff] [blame] | 177 | |
| 178 | initializeProtocolLogLevel (); |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 179 | |
| 180 | if ( GetStrValue ( NAME_NFA_DM_CFG, (char*)nfa_dm_cfg, sizeof ( nfa_dm_cfg ) ) ) |
| 181 | p_nfa_dm_cfg = ( tNFA_DM_CFG * ) &nfa_dm_cfg[0]; |
| 182 | |
| 183 | if ( GetNumValue ( NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof ( num ) ) ) |
| 184 | { |
| 185 | nfa_ee_max_ee_cfg = num; |
| 186 | ALOGD("%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d", func, nfa_ee_max_ee_cfg); |
| 187 | } |
Jizhou Liao | 2ef0896 | 2015-06-08 16:11:04 -0700 | [diff] [blame] | 188 | if ( GetNumValue ( NAME_NFA_POLL_BAIL_OUT_MODE, &num, sizeof ( num ) ) ) |
| 189 | { |
| 190 | nfa_poll_bail_out_mode = num; |
| 191 | ALOGD("%s: Overriding NFA_POLL_BAIL_OUT_MODE to use %d", func, nfa_poll_bail_out_mode); |
| 192 | } |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 193 | |
Jizhou Liao | acf1ec0 | 2015-06-30 10:25:41 -0700 | [diff] [blame] | 194 | if ( GetStrValue ( NAME_NFA_PROPRIETARY_CFG, (char*)nfa_proprietary_cfg, sizeof ( tNFA_PROPRIETARY_CFG ) ) ) |
| 195 | { |
| 196 | p_nfa_proprietary_cfg = (tNFA_PROPRIETARY_CFG*) &nfa_proprietary_cfg[0]; |
| 197 | } |
| 198 | |
Evan Chu | b7cb567 | 2014-02-07 15:01:21 -0500 | [diff] [blame] | 199 | //configure device host whitelist of HCI host ID's; see specification ETSI TS 102 622 V11.1.10 |
| 200 | //(2012-10), section 6.1.3.1 |
| 201 | num = GetStrValue ( NAME_DEVICE_HOST_WHITE_LIST, (char*) deviceHostWhiteList, sizeof ( deviceHostWhiteList ) ); |
| 202 | if (num) |
| 203 | { |
| 204 | memmove (&jni_nfa_hci_cfg, p_nfa_hci_cfg, sizeof(jni_nfa_hci_cfg)); |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 205 | jni_nfa_hci_cfg.num_whitelist_host = (uint8_t) num; //number of HCI host ID's in the whitelist |
Evan Chu | b7cb567 | 2014-02-07 15:01:21 -0500 | [diff] [blame] | 206 | jni_nfa_hci_cfg.p_whitelist = deviceHostWhiteList; //array of HCI host ID's |
| 207 | p_nfa_hci_cfg = &jni_nfa_hci_cfg; |
| 208 | } |
| 209 | |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 210 | initializeGlobalAppLogLevel (); |
| 211 | |
Evan Chu | 5a7e857 | 2013-05-01 10:10:31 -0400 | [diff] [blame] | 212 | verify_stack_non_volatile_store (); |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 213 | if ( GetNumValue ( NAME_PRESERVE_STORAGE, (char*)&num, sizeof ( num ) ) && |
| 214 | (num == 1) ) |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 215 | ALOGD ("%s: preserve stack NV store", __func__); |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 216 | else |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 217 | { |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 218 | delete_stack_non_volatile_store (FALSE); |
| 219 | } |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 220 | |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 221 | GKI_init (); |
| 222 | GKI_enable (); |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 223 | GKI_create_task ((TASKPTR)NFCA_TASK, BTU_TASK, (int8_t*)"NFCA_TASK", 0, 0, (pthread_cond_t*)NULL, NULL); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 224 | { |
| 225 | AutoThreadMutex guard(mCondVar); |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 226 | GKI_create_task ((TASKPTR)Thread, MMI_TASK, (int8_t*)"NFCA_THREAD", 0, 0, (pthread_cond_t*)NULL, NULL); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 227 | mCondVar.wait(); |
| 228 | } |
| 229 | |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 230 | mHalCallback = NULL; |
| 231 | memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs)); |
| 232 | InitializeHalDeviceContext (); |
| 233 | ALOGD ("%s: exit", func); |
| 234 | } |
| 235 | |
| 236 | /******************************************************************************* |
| 237 | ** |
| 238 | ** Function: NfcAdaptation::Finalize() |
| 239 | ** |
| 240 | ** Description: class finalizer |
| 241 | ** |
| 242 | ** Returns: none |
| 243 | ** |
| 244 | *******************************************************************************/ |
| 245 | void NfcAdaptation::Finalize() |
| 246 | { |
| 247 | const char* func = "NfcAdaptation::Finalize"; |
| 248 | AutoThreadMutex a(sLock); |
| 249 | |
| 250 | ALOGD ("%s: enter", func); |
| 251 | GKI_shutdown (); |
| 252 | |
| 253 | resetConfig(); |
| 254 | |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 255 | mCallback = NULL; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 256 | memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs)); |
| 257 | |
| 258 | ALOGD ("%s: exit", func); |
| 259 | delete this; |
| 260 | } |
| 261 | |
| 262 | /******************************************************************************* |
| 263 | ** |
| 264 | ** Function: NfcAdaptation::signal() |
| 265 | ** |
| 266 | ** Description: signal the CondVar to release the thread that is waiting |
| 267 | ** |
| 268 | ** Returns: none |
| 269 | ** |
| 270 | *******************************************************************************/ |
| 271 | void NfcAdaptation::signal () |
| 272 | { |
| 273 | mCondVar.signal(); |
| 274 | } |
| 275 | |
| 276 | /******************************************************************************* |
| 277 | ** |
| 278 | ** Function: NfcAdaptation::NFCA_TASK() |
| 279 | ** |
| 280 | ** Description: NFCA_TASK runs the GKI main task |
| 281 | ** |
| 282 | ** Returns: none |
| 283 | ** |
| 284 | *******************************************************************************/ |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 285 | uint32_t NfcAdaptation::NFCA_TASK (uint32_t arg) |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 286 | { |
| 287 | const char* func = "NfcAdaptation::NFCA_TASK"; |
| 288 | ALOGD ("%s: enter", func); |
| 289 | GKI_run (0); |
| 290 | ALOGD ("%s: exit", func); |
Martijn Coenen | 0a85f65 | 2014-02-27 18:15:21 -0800 | [diff] [blame] | 291 | return 0; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /******************************************************************************* |
| 295 | ** |
| 296 | ** Function: NfcAdaptation::Thread() |
| 297 | ** |
| 298 | ** Description: Creates work threads |
| 299 | ** |
| 300 | ** Returns: none |
| 301 | ** |
| 302 | *******************************************************************************/ |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 303 | uint32_t NfcAdaptation::Thread (uint32_t arg) |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 304 | { |
| 305 | const char* func = "NfcAdaptation::Thread"; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 306 | ALOGD ("%s: enter", func); |
| 307 | |
| 308 | { |
| 309 | ThreadCondVar CondVar; |
| 310 | AutoThreadMutex guard(CondVar); |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 311 | GKI_create_task ((TASKPTR)nfc_task, NFC_TASK, (int8_t*)"NFC_TASK", 0, 0, (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 312 | CondVar.wait(); |
| 313 | } |
| 314 | |
| 315 | NfcAdaptation::GetInstance().signal(); |
| 316 | |
| 317 | GKI_exit_task (GKI_get_taskid ()); |
| 318 | ALOGD ("%s: exit", func); |
Martijn Coenen | 0a85f65 | 2014-02-27 18:15:21 -0800 | [diff] [blame] | 319 | return 0; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /******************************************************************************* |
| 323 | ** |
| 324 | ** Function: NfcAdaptation::GetHalEntryFuncs() |
| 325 | ** |
| 326 | ** Description: Get the set of HAL entry points. |
| 327 | ** |
| 328 | ** Returns: Functions pointers for HAL entry points. |
| 329 | ** |
| 330 | *******************************************************************************/ |
| 331 | tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs () |
| 332 | { |
| 333 | return &mHalEntryFuncs; |
| 334 | } |
| 335 | |
| 336 | /******************************************************************************* |
| 337 | ** |
| 338 | ** Function: NfcAdaptation::InitializeHalDeviceContext |
| 339 | ** |
| 340 | ** Description: Ask the generic Android HAL to find the Broadcom-specific HAL. |
| 341 | ** |
| 342 | ** Returns: None. |
| 343 | ** |
| 344 | *******************************************************************************/ |
| 345 | void NfcAdaptation::InitializeHalDeviceContext () |
| 346 | { |
| 347 | const char* func = "NfcAdaptation::InitializeHalDeviceContext"; |
| 348 | ALOGD ("%s: enter", func); |
| 349 | int ret = 0; //0 means success |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 350 | |
| 351 | mHalEntryFuncs.initialize = HalInitialize; |
| 352 | mHalEntryFuncs.terminate = HalTerminate; |
| 353 | mHalEntryFuncs.open = HalOpen; |
| 354 | mHalEntryFuncs.close = HalClose; |
| 355 | mHalEntryFuncs.core_initialized = HalCoreInitialized; |
| 356 | mHalEntryFuncs.write = HalWrite; |
| 357 | mHalEntryFuncs.prediscover = HalPrediscover; |
| 358 | mHalEntryFuncs.control_granted = HalControlGranted; |
| 359 | mHalEntryFuncs.power_cycle = HalPowerCycle; |
Paul Chaisson | b20cbf3 | 2013-07-12 13:25:09 -0400 | [diff] [blame] | 360 | mHalEntryFuncs.get_max_ee = HalGetMaxNfcee; |
Chris Phoenix | 950638a | 2017-01-14 10:29:11 -0800 | [diff] [blame] | 361 | ALOGI("%s: INfc::getService()", func); |
| 362 | mHal = INfc::getService(); |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 363 | LOG_FATAL_IF(mHal == nullptr, "Failed to retrieve the NFC HAL!"); |
Chris Phoenix | 950638a | 2017-01-14 10:29:11 -0800 | [diff] [blame] | 364 | ALOGI("%s: INfc::getService() returned %p (%s)", func, |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 365 | mHal.get(), (mHal->isRemote() ? "remote" : "local")); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 366 | ALOGD ("%s: exit", func); |
| 367 | } |
| 368 | |
| 369 | /******************************************************************************* |
| 370 | ** |
| 371 | ** Function: NfcAdaptation::HalInitialize |
| 372 | ** |
| 373 | ** Description: Not implemented because this function is only needed |
| 374 | ** within the HAL. |
| 375 | ** |
| 376 | ** Returns: None. |
| 377 | ** |
| 378 | *******************************************************************************/ |
| 379 | void NfcAdaptation::HalInitialize () |
| 380 | { |
| 381 | const char* func = "NfcAdaptation::HalInitialize"; |
| 382 | ALOGD ("%s", func); |
| 383 | } |
| 384 | |
| 385 | /******************************************************************************* |
| 386 | ** |
| 387 | ** Function: NfcAdaptation::HalTerminate |
| 388 | ** |
| 389 | ** Description: Not implemented because this function is only needed |
| 390 | ** within the HAL. |
| 391 | ** |
| 392 | ** Returns: None. |
| 393 | ** |
| 394 | *******************************************************************************/ |
| 395 | void NfcAdaptation::HalTerminate () |
| 396 | { |
| 397 | const char* func = "NfcAdaptation::HalTerminate"; |
| 398 | ALOGD ("%s", func); |
| 399 | } |
| 400 | |
| 401 | /******************************************************************************* |
| 402 | ** |
| 403 | ** Function: NfcAdaptation::HalOpen |
| 404 | ** |
| 405 | ** Description: Turn on controller, download firmware. |
| 406 | ** |
| 407 | ** Returns: None. |
| 408 | ** |
| 409 | *******************************************************************************/ |
| 410 | void NfcAdaptation::HalOpen (tHAL_NFC_CBACK *p_hal_cback, tHAL_NFC_DATA_CBACK* p_data_cback) |
| 411 | { |
| 412 | const char* func = "NfcAdaptation::HalOpen"; |
| 413 | ALOGD ("%s", func); |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 414 | mCallback = new NfcClientCallback(p_hal_cback, p_data_cback); |
| 415 | mHal->open(mCallback); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /******************************************************************************* |
| 419 | ** |
| 420 | ** Function: NfcAdaptation::HalClose |
| 421 | ** |
| 422 | ** Description: Turn off controller. |
| 423 | ** |
| 424 | ** Returns: None. |
| 425 | ** |
| 426 | *******************************************************************************/ |
| 427 | void NfcAdaptation::HalClose () |
| 428 | { |
| 429 | const char* func = "NfcAdaptation::HalClose"; |
| 430 | ALOGD ("%s", func); |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 431 | mHal->close(); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /******************************************************************************* |
| 435 | ** |
| 436 | ** Function: NfcAdaptation::HalDeviceContextCallback |
| 437 | ** |
| 438 | ** Description: Translate generic Android HAL's callback into Broadcom-specific |
| 439 | ** callback function. |
| 440 | ** |
| 441 | ** Returns: None. |
| 442 | ** |
| 443 | *******************************************************************************/ |
| 444 | void NfcAdaptation::HalDeviceContextCallback (nfc_event_t event, nfc_status_t event_status) |
| 445 | { |
| 446 | const char* func = "NfcAdaptation::HalDeviceContextCallback"; |
| 447 | ALOGD ("%s: event=%u", func, event); |
| 448 | if (mHalCallback) |
| 449 | mHalCallback (event, (tHAL_NFC_STATUS) event_status); |
| 450 | } |
| 451 | |
| 452 | /******************************************************************************* |
| 453 | ** |
| 454 | ** Function: NfcAdaptation::HalDeviceContextDataCallback |
| 455 | ** |
| 456 | ** Description: Translate generic Android HAL's callback into Broadcom-specific |
| 457 | ** callback function. |
| 458 | ** |
| 459 | ** Returns: None. |
| 460 | ** |
| 461 | *******************************************************************************/ |
| 462 | void NfcAdaptation::HalDeviceContextDataCallback (uint16_t data_len, uint8_t* p_data) |
| 463 | { |
| 464 | const char* func = "NfcAdaptation::HalDeviceContextDataCallback"; |
| 465 | ALOGD ("%s: len=%u", func, data_len); |
| 466 | if (mHalDataCallback) |
| 467 | mHalDataCallback (data_len, p_data); |
| 468 | } |
| 469 | |
| 470 | /******************************************************************************* |
| 471 | ** |
| 472 | ** Function: NfcAdaptation::HalWrite |
| 473 | ** |
| 474 | ** Description: Write NCI message to the controller. |
| 475 | ** |
| 476 | ** Returns: None. |
| 477 | ** |
| 478 | *******************************************************************************/ |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 479 | void NfcAdaptation::HalWrite (uint16_t data_len, uint8_t* p_data) |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 480 | { |
| 481 | const char* func = "NfcAdaptation::HalWrite"; |
| 482 | ALOGD ("%s", func); |
Steven Moreland | 98cd45d | 2016-09-29 11:18:21 -0700 | [diff] [blame] | 483 | ::android::hardware::nfc::V1_0::NfcData data; |
| 484 | data.setToExternal(p_data, data_len); |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 485 | mHal->write(data); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /******************************************************************************* |
| 489 | ** |
| 490 | ** Function: NfcAdaptation::HalCoreInitialized |
| 491 | ** |
| 492 | ** Description: Adjust the configurable parameters in the controller. |
| 493 | ** |
| 494 | ** Returns: None. |
| 495 | ** |
| 496 | *******************************************************************************/ |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 497 | void NfcAdaptation::HalCoreInitialized (uint16_t data_len, uint8_t* p_core_init_rsp_params) |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 498 | { |
| 499 | const char* func = "NfcAdaptation::HalCoreInitialized"; |
| 500 | ALOGD ("%s", func); |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 501 | hidl_vec<uint8_t> data; |
| 502 | data.setToExternal(p_core_init_rsp_params, data_len); |
| 503 | |
Steven Moreland | 98cd45d | 2016-09-29 11:18:21 -0700 | [diff] [blame] | 504 | mHal->coreInitialized(data); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /******************************************************************************* |
| 508 | ** |
| 509 | ** Function: NfcAdaptation::HalPrediscover |
| 510 | ** |
| 511 | ** Description: Perform any vendor-specific pre-discovery actions (if needed) |
| 512 | ** If any actions were performed TRUE will be returned, and |
| 513 | ** HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are |
| 514 | ** completed. |
| 515 | ** |
| 516 | ** Returns: TRUE if vendor-specific pre-discovery actions initialized |
| 517 | ** FALSE if no vendor-specific pre-discovery actions are needed. |
| 518 | ** |
| 519 | *******************************************************************************/ |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 520 | bool NfcAdaptation::HalPrediscover () |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 521 | { |
| 522 | const char* func = "NfcAdaptation::HalPrediscover"; |
| 523 | ALOGD ("%s", func); |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 524 | bool retval = FALSE; |
Steven Moreland | 98cd45d | 2016-09-29 11:18:21 -0700 | [diff] [blame] | 525 | mHal->prediscover(); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 526 | return retval; |
| 527 | } |
| 528 | |
| 529 | /******************************************************************************* |
| 530 | ** |
| 531 | ** Function: HAL_NfcControlGranted |
| 532 | ** |
| 533 | ** Description: Grant control to HAL control for sending NCI commands. |
| 534 | ** Call in response to HAL_REQUEST_CONTROL_EVT. |
| 535 | ** Must only be called when there are no NCI commands pending. |
| 536 | ** HAL_RELEASE_CONTROL_EVT will notify when HAL no longer |
| 537 | ** needs control of NCI. |
| 538 | ** |
| 539 | ** Returns: void |
| 540 | ** |
| 541 | *******************************************************************************/ |
| 542 | void NfcAdaptation::HalControlGranted () |
| 543 | { |
| 544 | const char* func = "NfcAdaptation::HalControlGranted"; |
| 545 | ALOGD ("%s", func); |
Steven Moreland | 98cd45d | 2016-09-29 11:18:21 -0700 | [diff] [blame] | 546 | mHal->controlGranted(); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /******************************************************************************* |
| 550 | ** |
| 551 | ** Function: NfcAdaptation::HalPowerCycle |
| 552 | ** |
| 553 | ** Description: Turn off and turn on the controller. |
| 554 | ** |
| 555 | ** Returns: None. |
| 556 | ** |
| 557 | *******************************************************************************/ |
| 558 | void NfcAdaptation::HalPowerCycle () |
| 559 | { |
| 560 | const char* func = "NfcAdaptation::HalPowerCycle"; |
| 561 | ALOGD ("%s", func); |
Steven Moreland | 98cd45d | 2016-09-29 11:18:21 -0700 | [diff] [blame] | 562 | mHal->powerCycle(); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Paul Chaisson | b20cbf3 | 2013-07-12 13:25:09 -0400 | [diff] [blame] | 565 | /******************************************************************************* |
| 566 | ** |
| 567 | ** Function: NfcAdaptation::HalGetMaxNfcee |
| 568 | ** |
| 569 | ** Description: Turn off and turn on the controller. |
| 570 | ** |
| 571 | ** Returns: None. |
| 572 | ** |
| 573 | *******************************************************************************/ |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 574 | uint8_t NfcAdaptation::HalGetMaxNfcee() |
Paul Chaisson | b20cbf3 | 2013-07-12 13:25:09 -0400 | [diff] [blame] | 575 | { |
| 576 | const char* func = "NfcAdaptation::HalPowerCycle"; |
Ruchi Kandoi | 512ee63 | 2017-01-03 13:59:10 -0800 | [diff] [blame] | 577 | uint8_t maxNfcee = 0; |
Evan Chu | a24be4f | 2013-11-13 15:30:16 -0500 | [diff] [blame] | 578 | ALOGD ("%s", func); |
Evan Chu | a24be4f | 2013-11-13 15:30:16 -0500 | [diff] [blame] | 579 | |
Martijn Coenen | e9e48e5 | 2016-09-06 16:05:25 +0200 | [diff] [blame] | 580 | return nfa_ee_max_ee_cfg; |
Paul Chaisson | b20cbf3 | 2013-07-12 13:25:09 -0400 | [diff] [blame] | 581 | } |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 582 | |
Evan Chu | a24be4f | 2013-11-13 15:30:16 -0500 | [diff] [blame] | 583 | |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 584 | /******************************************************************************* |
| 585 | ** |
| 586 | ** Function: NfcAdaptation::DownloadFirmware |
| 587 | ** |
| 588 | ** Description: Download firmware patch files. |
| 589 | ** |
| 590 | ** Returns: None. |
| 591 | ** |
| 592 | *******************************************************************************/ |
| 593 | void NfcAdaptation::DownloadFirmware () |
| 594 | { |
| 595 | const char* func = "NfcAdaptation::DownloadFirmware"; |
| 596 | ALOGD ("%s: enter", func); |
| 597 | HalInitialize (); |
| 598 | |
| 599 | mHalOpenCompletedEvent.lock (); |
| 600 | ALOGD ("%s: try open HAL", func); |
| 601 | HalOpen (HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback); |
| 602 | mHalOpenCompletedEvent.wait (); |
| 603 | |
| 604 | mHalCloseCompletedEvent.lock (); |
| 605 | ALOGD ("%s: try close HAL", func); |
| 606 | HalClose (); |
| 607 | mHalCloseCompletedEvent.wait (); |
| 608 | |
| 609 | HalTerminate (); |
| 610 | ALOGD ("%s: exit", func); |
| 611 | } |
| 612 | |
| 613 | /******************************************************************************* |
| 614 | ** |
| 615 | ** Function: NfcAdaptation::HalDownloadFirmwareCallback |
| 616 | ** |
| 617 | ** Description: Receive events from the HAL. |
| 618 | ** |
| 619 | ** Returns: None. |
| 620 | ** |
| 621 | *******************************************************************************/ |
| 622 | void NfcAdaptation::HalDownloadFirmwareCallback (nfc_event_t event, nfc_status_t event_status) |
| 623 | { |
| 624 | const char* func = "NfcAdaptation::HalDownloadFirmwareCallback"; |
| 625 | ALOGD ("%s: event=0x%X", func, event); |
| 626 | switch (event) |
| 627 | { |
| 628 | case HAL_NFC_OPEN_CPLT_EVT: |
| 629 | { |
| 630 | ALOGD ("%s: HAL_NFC_OPEN_CPLT_EVT", func); |
| 631 | mHalOpenCompletedEvent.signal (); |
| 632 | break; |
| 633 | } |
| 634 | case HAL_NFC_CLOSE_CPLT_EVT: |
| 635 | { |
| 636 | ALOGD ("%s: HAL_NFC_CLOSE_CPLT_EVT", func); |
| 637 | mHalCloseCompletedEvent.signal (); |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /******************************************************************************* |
| 644 | ** |
| 645 | ** Function: NfcAdaptation::HalDownloadFirmwareDataCallback |
| 646 | ** |
| 647 | ** Description: Receive data events from the HAL. |
| 648 | ** |
| 649 | ** Returns: None. |
| 650 | ** |
| 651 | *******************************************************************************/ |
| 652 | void NfcAdaptation::HalDownloadFirmwareDataCallback (uint16_t data_len, uint8_t* p_data) |
| 653 | { |
| 654 | } |
| 655 | |
| 656 | |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 657 | /******************************************************************************* |
| 658 | ** |
| 659 | ** Function: ThreadMutex::ThreadMutex() |
| 660 | ** |
| 661 | ** Description: class constructor |
| 662 | ** |
| 663 | ** Returns: none |
| 664 | ** |
| 665 | *******************************************************************************/ |
| 666 | ThreadMutex::ThreadMutex() |
| 667 | { |
| 668 | pthread_mutexattr_t mutexAttr; |
| 669 | |
| 670 | pthread_mutexattr_init(&mutexAttr); |
| 671 | pthread_mutex_init(&mMutex, &mutexAttr); |
| 672 | pthread_mutexattr_destroy(&mutexAttr); |
| 673 | } |
| 674 | |
| 675 | /******************************************************************************* |
| 676 | ** |
| 677 | ** Function: ThreadMutex::~ThreadMutex() |
| 678 | ** |
| 679 | ** Description: class destructor |
| 680 | ** |
| 681 | ** Returns: none |
| 682 | ** |
| 683 | *******************************************************************************/ |
| 684 | ThreadMutex::~ThreadMutex() |
| 685 | { |
| 686 | pthread_mutex_destroy(&mMutex); |
| 687 | } |
| 688 | |
| 689 | /******************************************************************************* |
| 690 | ** |
| 691 | ** Function: ThreadMutex::lock() |
| 692 | ** |
| 693 | ** Description: lock kthe mutex |
| 694 | ** |
| 695 | ** Returns: none |
| 696 | ** |
| 697 | *******************************************************************************/ |
| 698 | void ThreadMutex::lock() |
| 699 | { |
| 700 | pthread_mutex_lock(&mMutex); |
| 701 | } |
| 702 | |
| 703 | /******************************************************************************* |
| 704 | ** |
| 705 | ** Function: ThreadMutex::unblock() |
| 706 | ** |
| 707 | ** Description: unlock the mutex |
| 708 | ** |
| 709 | ** Returns: none |
| 710 | ** |
| 711 | *******************************************************************************/ |
| 712 | void ThreadMutex::unlock() |
| 713 | { |
| 714 | pthread_mutex_unlock(&mMutex); |
| 715 | } |
| 716 | |
| 717 | /******************************************************************************* |
| 718 | ** |
| 719 | ** Function: ThreadCondVar::ThreadCondVar() |
| 720 | ** |
| 721 | ** Description: class constructor |
| 722 | ** |
| 723 | ** Returns: none |
| 724 | ** |
| 725 | *******************************************************************************/ |
| 726 | ThreadCondVar::ThreadCondVar() |
| 727 | { |
| 728 | pthread_condattr_t CondAttr; |
| 729 | |
| 730 | pthread_condattr_init(&CondAttr); |
| 731 | pthread_cond_init(&mCondVar, &CondAttr); |
| 732 | |
| 733 | pthread_condattr_destroy(&CondAttr); |
| 734 | } |
| 735 | |
| 736 | /******************************************************************************* |
| 737 | ** |
| 738 | ** Function: ThreadCondVar::~ThreadCondVar() |
| 739 | ** |
| 740 | ** Description: class destructor |
| 741 | ** |
| 742 | ** Returns: none |
| 743 | ** |
| 744 | *******************************************************************************/ |
| 745 | ThreadCondVar::~ThreadCondVar() |
| 746 | { |
| 747 | pthread_cond_destroy(&mCondVar); |
| 748 | } |
| 749 | |
| 750 | /******************************************************************************* |
| 751 | ** |
| 752 | ** Function: ThreadCondVar::wait() |
| 753 | ** |
| 754 | ** Description: wait on the mCondVar |
| 755 | ** |
| 756 | ** Returns: none |
| 757 | ** |
| 758 | *******************************************************************************/ |
| 759 | void ThreadCondVar::wait() |
| 760 | { |
| 761 | pthread_cond_wait(&mCondVar, *this); |
| 762 | pthread_mutex_unlock(*this); |
| 763 | } |
| 764 | |
| 765 | /******************************************************************************* |
| 766 | ** |
| 767 | ** Function: ThreadCondVar::signal() |
| 768 | ** |
| 769 | ** Description: signal the mCondVar |
| 770 | ** |
| 771 | ** Returns: none |
| 772 | ** |
| 773 | *******************************************************************************/ |
| 774 | void ThreadCondVar::signal() |
| 775 | { |
| 776 | AutoThreadMutex a(*this); |
| 777 | pthread_cond_signal(&mCondVar); |
| 778 | } |
| 779 | |
| 780 | /******************************************************************************* |
| 781 | ** |
| 782 | ** Function: AutoThreadMutex::AutoThreadMutex() |
| 783 | ** |
| 784 | ** Description: class constructor, automatically lock the mutex |
| 785 | ** |
| 786 | ** Returns: none |
| 787 | ** |
| 788 | *******************************************************************************/ |
| 789 | AutoThreadMutex::AutoThreadMutex(ThreadMutex &m) |
| 790 | : mm(m) |
| 791 | { |
| 792 | mm.lock(); |
| 793 | } |
| 794 | |
| 795 | /******************************************************************************* |
| 796 | ** |
| 797 | ** Function: AutoThreadMutex::~AutoThreadMutex() |
| 798 | ** |
| 799 | ** Description: class destructor, automatically unlock the mutex |
| 800 | ** |
| 801 | ** Returns: none |
| 802 | ** |
| 803 | *******************************************************************************/ |
| 804 | AutoThreadMutex::~AutoThreadMutex() |
| 805 | { |
| 806 | mm.unlock(); |
| 807 | } |