blob: 42bf55f50d437f300db2a2a89007f85edf81392e [file] [log] [blame]
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001/******************************************************************************
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 Kandoid03c06e2017-01-26 15:32:03 -080018#include "_OverrideLog.h"
19
Martijn Coenene9e48e52016-09-06 16:05:25 +020020#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 Projecte9df6ba2012-12-13 14:55:37 -080024#include "NfcAdaptation.h"
Ruchi Kandoi1ebdcc22017-04-17 17:23:56 -070025#include "debug_nfcsnoop.h"
26#include "nfc_target.h"
27
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080028extern "C" {
29#include "gki.h"
30#include "nfa_api.h"
31#include "nfc_int.h"
32#include "vendor_cfg.h"
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080033}
Evan Chua24be4f2013-11-13 15:30:16 -050034#include "android_logmsg.h"
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080035#include "config.h"
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080036
Jizhou Liao65ebec52016-04-05 17:09:24 -070037#undef LOG_TAG
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080038#define LOG_TAG "NfcAdaptation"
39
Martijn Coenene9e48e52016-09-06 16:05:25 +020040using android::OK;
41using android::sp;
42using android::status_t;
43
44using android::hardware::ProcessState;
45using android::hardware::Return;
46using android::hardware::Void;
47using android::hardware::nfc::V1_0::INfc;
48using android::hardware::nfc::V1_0::INfcClientCallback;
49using android::hardware::hidl_vec;
50
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080051extern "C" void GKI_shutdown();
52extern void resetConfig();
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080053extern "C" void verify_stack_non_volatile_store();
54extern "C" void delete_stack_non_volatile_store(bool forceDelete);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080055
56NfcAdaptation* NfcAdaptation::mpInstance = NULL;
57ThreadMutex NfcAdaptation::sLock;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080058tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = NULL;
59tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = NULL;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -070060ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent;
61ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent;
Martijn Coenene9e48e52016-09-06 16:05:25 +020062sp<INfc> NfcAdaptation::mHal;
63INfcClientCallback* NfcAdaptation::mCallback;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080064
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080065uint32_t ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; // 0x017F00;
Ruchi Kandoi512ee632017-01-03 13:59:10 -080066uint8_t appl_trace_level = 0xff;
Love Khannaa5eb6e82017-06-02 19:55:05 +053067uint8_t appl_dta_mode_flag = 0x00;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080068char bcm_nfc_location[120];
69
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080070static uint8_t nfa_dm_cfg[sizeof(tNFA_DM_CFG)];
71static uint8_t nfa_proprietary_cfg[sizeof(tNFA_PROPRIETARY_CFG)];
72extern tNFA_DM_CFG* p_nfa_dm_cfg;
73extern tNFA_PROPRIETARY_CFG* p_nfa_proprietary_cfg;
Ruchi Kandoi512ee632017-01-03 13:59:10 -080074extern uint8_t nfa_ee_max_ee_cfg;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080075extern const uint8_t nfca_version_string[];
76extern const uint8_t nfa_version_string[];
77static uint8_t deviceHostWhiteList[NFA_HCI_MAX_HOST_IN_NETWORK];
Evan Chub7cb5672014-02-07 15:01:21 -050078static tNFA_HCI_CFG jni_nfa_hci_cfg;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080079extern tNFA_HCI_CFG* p_nfa_hci_cfg;
80extern bool nfa_poll_bail_out_mode;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -070081
Martijn Coenene9e48e52016-09-06 16:05:25 +020082class NfcClientCallback : public INfcClientCallback {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080083 public:
84 NfcClientCallback(tHAL_NFC_CBACK* eventCallback,
85 tHAL_NFC_DATA_CBACK dataCallback) {
86 mEventCallback = eventCallback;
87 mDataCallback = dataCallback;
88 };
89 virtual ~NfcClientCallback() = default;
90 Return<void> sendEvent(
91 ::android::hardware::nfc::V1_0::NfcEvent event,
92 ::android::hardware::nfc::V1_0::NfcStatus event_status) override {
93 mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)event_status);
94 return Void();
95 };
96 Return<void> sendData(
97 const ::android::hardware::nfc::V1_0::NfcData& data) override {
98 ::android::hardware::nfc::V1_0::NfcData copy = data;
99 mDataCallback(copy.size(), &copy[0]);
100 return Void();
101 };
102
103 private:
104 tHAL_NFC_CBACK* mEventCallback;
105 tHAL_NFC_DATA_CBACK* mDataCallback;
Martijn Coenene9e48e52016-09-06 16:05:25 +0200106};
107
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800108/*******************************************************************************
109**
110** Function: NfcAdaptation::NfcAdaptation()
111**
112** Description: class constructor
113**
114** Returns: none
115**
116*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800117NfcAdaptation::NfcAdaptation() {
118 memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800119}
120
121/*******************************************************************************
122**
123** Function: NfcAdaptation::~NfcAdaptation()
124**
125** Description: class destructor
126**
127** Returns: none
128**
129*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800130NfcAdaptation::~NfcAdaptation() { mpInstance = NULL; }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800131
132/*******************************************************************************
133**
134** Function: NfcAdaptation::GetInstance()
135**
136** Description: access class singleton
137**
138** Returns: pointer to the singleton object
139**
140*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800141NfcAdaptation& NfcAdaptation::GetInstance() {
142 AutoThreadMutex a(sLock);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800143
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800144 if (!mpInstance) mpInstance = new NfcAdaptation;
145 return *mpInstance;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800146}
147
148/*******************************************************************************
149**
150** Function: NfcAdaptation::Initialize()
151**
152** Description: class initializer
153**
154** Returns: none
155**
156*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800157void NfcAdaptation::Initialize() {
158 const char* func = "NfcAdaptation::Initialize";
159 ALOGD("%s: enter", func);
160 ALOGE("%s: ver=%s nfa=%s", func, nfca_version_string, nfa_version_string);
161 unsigned long num;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800162
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800163 if (GetNumValue(NAME_USE_RAW_NCI_TRACE, &num, sizeof(num))) {
164 if (num == 1) {
165 // display protocol traces in raw format
166 ProtoDispAdapterUseRawOutput(TRUE);
167 ALOGD("%s: logging protocol in raw format", func);
Evan Chua24be4f2013-11-13 15:30:16 -0500168 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800169 }
170 if (!GetStrValue(NAME_NFA_STORAGE, bcm_nfc_location,
171 sizeof(bcm_nfc_location))) {
172 strlcpy(bcm_nfc_location, "/data/nfc", sizeof(bcm_nfc_location));
173 }
Andres Moralese4ecc7d2014-10-01 17:46:04 -0700174
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800175 initializeProtocolLogLevel();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700176
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800177 if (GetStrValue(NAME_NFA_DM_CFG, (char*)nfa_dm_cfg, sizeof(nfa_dm_cfg)))
178 p_nfa_dm_cfg = (tNFA_DM_CFG*)&nfa_dm_cfg[0];
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700179
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800180 if (GetNumValue(NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof(num))) {
181 nfa_ee_max_ee_cfg = num;
182 ALOGD("%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d", func,
183 nfa_ee_max_ee_cfg);
184 }
185 if (GetNumValue(NAME_NFA_POLL_BAIL_OUT_MODE, &num, sizeof(num))) {
186 nfa_poll_bail_out_mode = num;
187 ALOGD("%s: Overriding NFA_POLL_BAIL_OUT_MODE to use %d", func,
188 nfa_poll_bail_out_mode);
189 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700190
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800191 if (GetStrValue(NAME_NFA_PROPRIETARY_CFG, (char*)nfa_proprietary_cfg,
192 sizeof(tNFA_PROPRIETARY_CFG))) {
193 p_nfa_proprietary_cfg = (tNFA_PROPRIETARY_CFG*)&nfa_proprietary_cfg[0];
194 }
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700195
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800196 // configure device host whitelist of HCI host ID's; see specification ETSI TS
197 // 102 622 V11.1.10
198 //(2012-10), section 6.1.3.1
199 num = GetStrValue(NAME_DEVICE_HOST_WHITE_LIST, (char*)deviceHostWhiteList,
200 sizeof(deviceHostWhiteList));
201 if (num) {
202 memmove(&jni_nfa_hci_cfg, p_nfa_hci_cfg, sizeof(jni_nfa_hci_cfg));
203 jni_nfa_hci_cfg.num_whitelist_host =
204 (uint8_t)num; // number of HCI host ID's in the whitelist
205 jni_nfa_hci_cfg.p_whitelist = deviceHostWhiteList; // array of HCI host
206 // ID's
207 p_nfa_hci_cfg = &jni_nfa_hci_cfg;
208 }
Evan Chub7cb5672014-02-07 15:01:21 -0500209
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800210 initializeGlobalAppLogLevel();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800211
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800212 verify_stack_non_volatile_store();
213 if (GetNumValue(NAME_PRESERVE_STORAGE, (char*)&num, sizeof(num)) &&
214 (num == 1))
215 ALOGD("%s: preserve stack NV store", __func__);
216 else {
217 delete_stack_non_volatile_store(FALSE);
218 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700219
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800220 GKI_init();
221 GKI_enable();
222 GKI_create_task((TASKPTR)NFCA_TASK, BTU_TASK, (int8_t*)"NFCA_TASK", 0, 0,
223 (pthread_cond_t*)NULL, NULL);
224 {
225 AutoThreadMutex guard(mCondVar);
226 GKI_create_task((TASKPTR)Thread, MMI_TASK, (int8_t*)"NFCA_THREAD", 0, 0,
227 (pthread_cond_t*)NULL, NULL);
228 mCondVar.wait();
229 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800230
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800231 mHalCallback = NULL;
232 memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
233 InitializeHalDeviceContext();
Ruchi Kandoi1ebdcc22017-04-17 17:23:56 -0700234 debug_nfcsnoop_init();
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800235 ALOGD("%s: exit", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800236}
237
238/*******************************************************************************
239**
240** Function: NfcAdaptation::Finalize()
241**
242** Description: class finalizer
243**
244** Returns: none
245**
246*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800247void NfcAdaptation::Finalize() {
248 const char* func = "NfcAdaptation::Finalize";
249 AutoThreadMutex a(sLock);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800250
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800251 ALOGD("%s: enter", func);
252 GKI_shutdown();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800253
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800254 resetConfig();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800255
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800256 mCallback = NULL;
257 memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800258
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800259 ALOGD("%s: exit", func);
260 delete this;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800261}
262
263/*******************************************************************************
264**
Ruchi Kandoi1ebdcc22017-04-17 17:23:56 -0700265** Function: NfcAdaptation::Dump
266**
267** Description: Native support for dumpsys function.
268**
269** Returns: None.
270**
271*******************************************************************************/
272void NfcAdaptation::Dump(int fd) { debug_nfcsnoop_dump(fd); }
273
274/*******************************************************************************
275**
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800276** Function: NfcAdaptation::signal()
277**
278** Description: signal the CondVar to release the thread that is waiting
279**
280** Returns: none
281**
282*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800283void NfcAdaptation::signal() { mCondVar.signal(); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800284
285/*******************************************************************************
286**
287** Function: NfcAdaptation::NFCA_TASK()
288**
289** Description: NFCA_TASK runs the GKI main task
290**
291** Returns: none
292**
293*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800294uint32_t NfcAdaptation::NFCA_TASK(uint32_t arg) {
295 const char* func = "NfcAdaptation::NFCA_TASK";
296 ALOGD("%s: enter", func);
297 GKI_run(0);
298 ALOGD("%s: exit", func);
299 return 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800300}
301
302/*******************************************************************************
303**
304** Function: NfcAdaptation::Thread()
305**
306** Description: Creates work threads
307**
308** Returns: none
309**
310*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800311uint32_t NfcAdaptation::Thread(uint32_t arg) {
312 const char* func = "NfcAdaptation::Thread";
313 ALOGD("%s: enter", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800314
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800315 {
316 ThreadCondVar CondVar;
317 AutoThreadMutex guard(CondVar);
318 GKI_create_task((TASKPTR)nfc_task, NFC_TASK, (int8_t*)"NFC_TASK", 0, 0,
319 (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
320 CondVar.wait();
321 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800322
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800323 NfcAdaptation::GetInstance().signal();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800324
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800325 GKI_exit_task(GKI_get_taskid());
326 ALOGD("%s: exit", func);
327 return 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800328}
329
330/*******************************************************************************
331**
332** Function: NfcAdaptation::GetHalEntryFuncs()
333**
334** Description: Get the set of HAL entry points.
335**
336** Returns: Functions pointers for HAL entry points.
337**
338*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800339tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs() { return &mHalEntryFuncs; }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800340
341/*******************************************************************************
342**
343** Function: NfcAdaptation::InitializeHalDeviceContext
344**
345** Description: Ask the generic Android HAL to find the Broadcom-specific HAL.
346**
347** Returns: None.
348**
349*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800350void NfcAdaptation::InitializeHalDeviceContext() {
351 const char* func = "NfcAdaptation::InitializeHalDeviceContext";
352 ALOGD("%s: enter", func);
353 int ret = 0; // 0 means success
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800354
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800355 mHalEntryFuncs.initialize = HalInitialize;
356 mHalEntryFuncs.terminate = HalTerminate;
357 mHalEntryFuncs.open = HalOpen;
358 mHalEntryFuncs.close = HalClose;
359 mHalEntryFuncs.core_initialized = HalCoreInitialized;
360 mHalEntryFuncs.write = HalWrite;
361 mHalEntryFuncs.prediscover = HalPrediscover;
362 mHalEntryFuncs.control_granted = HalControlGranted;
363 mHalEntryFuncs.power_cycle = HalPowerCycle;
364 mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;
365 ALOGI("%s: INfc::getService()", func);
366 mHal = INfc::getService();
367 LOG_FATAL_IF(mHal == nullptr, "Failed to retrieve the NFC HAL!");
368 ALOGI("%s: INfc::getService() returned %p (%s)", func, mHal.get(),
369 (mHal->isRemote() ? "remote" : "local"));
370 ALOGD("%s: exit", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800371}
372
373/*******************************************************************************
374**
375** Function: NfcAdaptation::HalInitialize
376**
377** Description: Not implemented because this function is only needed
378** within the HAL.
379**
380** Returns: None.
381**
382*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800383void NfcAdaptation::HalInitialize() {
384 const char* func = "NfcAdaptation::HalInitialize";
385 ALOGD("%s", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800386}
387
388/*******************************************************************************
389**
390** Function: NfcAdaptation::HalTerminate
391**
392** Description: Not implemented because this function is only needed
393** within the HAL.
394**
395** Returns: None.
396**
397*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800398void NfcAdaptation::HalTerminate() {
399 const char* func = "NfcAdaptation::HalTerminate";
400 ALOGD("%s", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800401}
402
403/*******************************************************************************
404**
405** Function: NfcAdaptation::HalOpen
406**
407** Description: Turn on controller, download firmware.
408**
409** Returns: None.
410**
411*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800412void NfcAdaptation::HalOpen(tHAL_NFC_CBACK* p_hal_cback,
413 tHAL_NFC_DATA_CBACK* p_data_cback) {
414 const char* func = "NfcAdaptation::HalOpen";
415 ALOGD("%s", func);
416 mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
417 mHal->open(mCallback);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800418}
419
420/*******************************************************************************
421**
422** Function: NfcAdaptation::HalClose
423**
424** Description: Turn off controller.
425**
426** Returns: None.
427**
428*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800429void NfcAdaptation::HalClose() {
430 const char* func = "NfcAdaptation::HalClose";
431 ALOGD("%s", func);
432 mHal->close();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800433}
434
435/*******************************************************************************
436**
437** Function: NfcAdaptation::HalDeviceContextCallback
438**
439** Description: Translate generic Android HAL's callback into Broadcom-specific
440** callback function.
441**
442** Returns: None.
443**
444*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800445void NfcAdaptation::HalDeviceContextCallback(nfc_event_t event,
446 nfc_status_t event_status) {
447 const char* func = "NfcAdaptation::HalDeviceContextCallback";
448 ALOGD("%s: event=%u", func, event);
449 if (mHalCallback) mHalCallback(event, (tHAL_NFC_STATUS)event_status);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800450}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800462void NfcAdaptation::HalDeviceContextDataCallback(uint16_t data_len,
463 uint8_t* p_data) {
464 const char* func = "NfcAdaptation::HalDeviceContextDataCallback";
465 ALOGD("%s: len=%u", func, data_len);
466 if (mHalDataCallback) mHalDataCallback(data_len, p_data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800467}
468
469/*******************************************************************************
470**
471** Function: NfcAdaptation::HalWrite
472**
473** Description: Write NCI message to the controller.
474**
475** Returns: None.
476**
477*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800478void NfcAdaptation::HalWrite(uint16_t data_len, uint8_t* p_data) {
479 const char* func = "NfcAdaptation::HalWrite";
480 ALOGD("%s", func);
481 ::android::hardware::nfc::V1_0::NfcData data;
482 data.setToExternal(p_data, data_len);
483 mHal->write(data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800484}
485
486/*******************************************************************************
487**
488** Function: NfcAdaptation::HalCoreInitialized
489**
490** Description: Adjust the configurable parameters in the controller.
491**
492** Returns: None.
493**
494*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800495void NfcAdaptation::HalCoreInitialized(uint16_t data_len,
496 uint8_t* p_core_init_rsp_params) {
497 const char* func = "NfcAdaptation::HalCoreInitialized";
498 ALOGD("%s", func);
499 hidl_vec<uint8_t> data;
500 data.setToExternal(p_core_init_rsp_params, data_len);
Martijn Coenene9e48e52016-09-06 16:05:25 +0200501
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800502 mHal->coreInitialized(data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800503}
504
505/*******************************************************************************
506**
507** Function: NfcAdaptation::HalPrediscover
508**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800509** Description: Perform any vendor-specific pre-discovery actions (if
510** needed) If any actions were performed TRUE will be returned,
511** and HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800512** completed.
513**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800514** Returns: TRUE if vendor-specific pre-discovery actions initialized
515** FALSE if no vendor-specific pre-discovery actions are
516** needed.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800517**
518*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800519bool NfcAdaptation::HalPrediscover() {
520 const char* func = "NfcAdaptation::HalPrediscover";
521 ALOGD("%s", func);
522 bool retval = FALSE;
523 mHal->prediscover();
524 return retval;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800525}
526
527/*******************************************************************************
528**
529** Function: HAL_NfcControlGranted
530**
531** Description: Grant control to HAL control for sending NCI commands.
532** Call in response to HAL_REQUEST_CONTROL_EVT.
533** Must only be called when there are no NCI commands pending.
534** HAL_RELEASE_CONTROL_EVT will notify when HAL no longer
535** needs control of NCI.
536**
537** Returns: void
538**
539*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800540void NfcAdaptation::HalControlGranted() {
541 const char* func = "NfcAdaptation::HalControlGranted";
542 ALOGD("%s", func);
543 mHal->controlGranted();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800544}
545
546/*******************************************************************************
547**
548** Function: NfcAdaptation::HalPowerCycle
549**
550** Description: Turn off and turn on the controller.
551**
552** Returns: None.
553**
554*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800555void NfcAdaptation::HalPowerCycle() {
556 const char* func = "NfcAdaptation::HalPowerCycle";
557 ALOGD("%s", func);
558 mHal->powerCycle();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800559}
560
Paul Chaissonb20cbf32013-07-12 13:25:09 -0400561/*******************************************************************************
562**
563** Function: NfcAdaptation::HalGetMaxNfcee
564**
565** Description: Turn off and turn on the controller.
566**
567** Returns: None.
568**
569*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800570uint8_t NfcAdaptation::HalGetMaxNfcee() {
571 const char* func = "NfcAdaptation::HalPowerCycle";
572 uint8_t maxNfcee = 0;
573 ALOGD("%s", func);
Evan Chua24be4f2013-11-13 15:30:16 -0500574
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800575 return nfa_ee_max_ee_cfg;
Paul Chaissonb20cbf32013-07-12 13:25:09 -0400576}
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700577
578/*******************************************************************************
579**
580** Function: NfcAdaptation::DownloadFirmware
581**
582** Description: Download firmware patch files.
583**
584** Returns: None.
585**
586*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800587void NfcAdaptation::DownloadFirmware() {
588 const char* func = "NfcAdaptation::DownloadFirmware";
589 ALOGD("%s: enter", func);
590 HalInitialize();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700591
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800592 mHalOpenCompletedEvent.lock();
593 ALOGD("%s: try open HAL", func);
594 HalOpen(HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback);
595 mHalOpenCompletedEvent.wait();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700596
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800597 mHalCloseCompletedEvent.lock();
598 ALOGD("%s: try close HAL", func);
599 HalClose();
600 mHalCloseCompletedEvent.wait();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700601
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800602 HalTerminate();
603 ALOGD("%s: exit", func);
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700604}
605
606/*******************************************************************************
607**
608** Function: NfcAdaptation::HalDownloadFirmwareCallback
609**
610** Description: Receive events from the HAL.
611**
612** Returns: None.
613**
614*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800615void NfcAdaptation::HalDownloadFirmwareCallback(nfc_event_t event,
616 nfc_status_t event_status) {
617 const char* func = "NfcAdaptation::HalDownloadFirmwareCallback";
618 ALOGD("%s: event=0x%X", func, event);
619 switch (event) {
620 case HAL_NFC_OPEN_CPLT_EVT: {
621 ALOGD("%s: HAL_NFC_OPEN_CPLT_EVT", func);
622 mHalOpenCompletedEvent.signal();
623 break;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700624 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800625 case HAL_NFC_CLOSE_CPLT_EVT: {
626 ALOGD("%s: HAL_NFC_CLOSE_CPLT_EVT", func);
627 mHalCloseCompletedEvent.signal();
628 break;
629 }
630 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700631}
632
633/*******************************************************************************
634**
635** Function: NfcAdaptation::HalDownloadFirmwareDataCallback
636**
637** Description: Receive data events from the HAL.
638**
639** Returns: None.
640**
641*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800642void NfcAdaptation::HalDownloadFirmwareDataCallback(uint16_t data_len,
643 uint8_t* p_data) {}
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700644
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800645/*******************************************************************************
646**
647** Function: ThreadMutex::ThreadMutex()
648**
649** Description: class constructor
650**
651** Returns: none
652**
653*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800654ThreadMutex::ThreadMutex() {
655 pthread_mutexattr_t mutexAttr;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800656
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800657 pthread_mutexattr_init(&mutexAttr);
658 pthread_mutex_init(&mMutex, &mutexAttr);
659 pthread_mutexattr_destroy(&mutexAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800660}
661
662/*******************************************************************************
663**
664** Function: ThreadMutex::~ThreadMutex()
665**
666** Description: class destructor
667**
668** Returns: none
669**
670*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800671ThreadMutex::~ThreadMutex() { pthread_mutex_destroy(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800672
673/*******************************************************************************
674**
675** Function: ThreadMutex::lock()
676**
677** Description: lock kthe mutex
678**
679** Returns: none
680**
681*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800682void ThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800683
684/*******************************************************************************
685**
686** Function: ThreadMutex::unblock()
687**
688** Description: unlock the mutex
689**
690** Returns: none
691**
692*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800693void ThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800694
695/*******************************************************************************
696**
697** Function: ThreadCondVar::ThreadCondVar()
698**
699** Description: class constructor
700**
701** Returns: none
702**
703*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800704ThreadCondVar::ThreadCondVar() {
705 pthread_condattr_t CondAttr;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800706
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800707 pthread_condattr_init(&CondAttr);
708 pthread_cond_init(&mCondVar, &CondAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800709
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800710 pthread_condattr_destroy(&CondAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800711}
712
713/*******************************************************************************
714**
715** Function: ThreadCondVar::~ThreadCondVar()
716**
717** Description: class destructor
718**
719** Returns: none
720**
721*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800722ThreadCondVar::~ThreadCondVar() { pthread_cond_destroy(&mCondVar); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800723
724/*******************************************************************************
725**
726** Function: ThreadCondVar::wait()
727**
728** Description: wait on the mCondVar
729**
730** Returns: none
731**
732*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800733void ThreadCondVar::wait() {
734 pthread_cond_wait(&mCondVar, *this);
735 pthread_mutex_unlock(*this);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800736}
737
738/*******************************************************************************
739**
740** Function: ThreadCondVar::signal()
741**
742** Description: signal the mCondVar
743**
744** Returns: none
745**
746*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800747void ThreadCondVar::signal() {
748 AutoThreadMutex a(*this);
749 pthread_cond_signal(&mCondVar);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800750}
751
752/*******************************************************************************
753**
754** Function: AutoThreadMutex::AutoThreadMutex()
755**
756** Description: class constructor, automatically lock the mutex
757**
758** Returns: none
759**
760*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800761AutoThreadMutex::AutoThreadMutex(ThreadMutex& m) : mm(m) { mm.lock(); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800762
763/*******************************************************************************
764**
765** Function: AutoThreadMutex::~AutoThreadMutex()
766**
767** Description: class destructor, automatically unlock the mutex
768**
769** Returns: none
770**
771*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800772AutoThreadMutex::~AutoThreadMutex() { mm.unlock(); }