blob: 7ffa2283df7752b32efe1ebbb9e2b53690bbc08c [file] [log] [blame]
nxf24591c1cbeab2018-02-21 17:32:26 +05301/******************************************************************************
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 ******************************************************************************/
18#include <android-base/stringprintf.h>
19#include <android/hardware/nfc/1.1/INfc.h>
20#include <base/command_line.h>
21#include <base/logging.h>
22#include <cutils/properties.h>
23#include <hwbinder/ProcessState.h>
24
25#include "NfcAdaptation.h"
nxf24591c1cbeab2018-02-21 17:32:26 +053026#include "android_logmsg.h"
27#include "debug_nfcsnoop.h"
Ravishinde26233bb2018-08-06 22:16:08 +053028#if (NXP_EXTNS == TRUE)
29#include <vendor/nxp/nxpnfc/1.0/INxpNfc.h>
30#include "hal_nxpese.h"
31#endif
nxf24591c1cbeab2018-02-21 17:32:26 +053032#include "nfa_api.h"
Suraj Uday Kotharkard5af4382018-05-10 14:23:43 +053033#include "nfa_rw_api.h"
nxf24591c1cbeab2018-02-21 17:32:26 +053034#include "nfc_config.h"
35#include "nfc_int.h"
Arjun Thottingal Jayarajan4f71c362018-07-18 21:05:35 +053036#include <hidl/LegacySupport.h>
nxf24591c1cbeab2018-02-21 17:32:26 +053037
38using android::OK;
39using android::sp;
40using android::status_t;
41
42using android::base::StringPrintf;
43using android::hardware::ProcessState;
44using android::hardware::Return;
45using android::hardware::Void;
46using android::hardware::nfc::V1_0::INfc;
Suraj Uday Kotharkard5af4382018-05-10 14:23:43 +053047using android::hardware::nfc::V1_1::PresenceCheckAlgorithm;
nxf24591c1cbeab2018-02-21 17:32:26 +053048using INfcV1_1 = android::hardware::nfc::V1_1::INfc;
Suraj Uday Kotharkard5af4382018-05-10 14:23:43 +053049using NfcVendorConfig = android::hardware::nfc::V1_1::NfcConfig;
Suraj Uday Kotharkar10d9fc22018-05-09 15:05:56 +053050using android::hardware::nfc::V1_1::INfcClientCallback;
nxf24591c1cbeab2018-02-21 17:32:26 +053051using android::hardware::hidl_vec;
hariprasad nalacheruvu02f51032018-06-05 20:16:48 +053052using android::hardware::hidl_death_recipient;
Arjun Thottingal Jayarajan4f71c362018-07-18 21:05:35 +053053using android::hardware::configureRpcThreadpool;
Ravishinde26233bb2018-08-06 22:16:08 +053054#if (NXP_EXTNS == TRUE)
55using vendor::nxp::nxpnfc::V1_0::INxpNfc;
nxf24591c1cbeab2018-02-21 17:32:26 +053056
Ravishinde26233bb2018-08-06 22:16:08 +053057ThreadMutex NfcAdaptation::sIoctlLock;
58sp<INxpNfc> NfcAdaptation::mHalNxpNfc;
59#endif
nxf24591c1cbeab2018-02-21 17:32:26 +053060extern bool nfc_debug_enabled;
61
62extern void GKI_shutdown();
63extern void verify_stack_non_volatile_store();
64extern void delete_stack_non_volatile_store(bool forceDelete);
65
66NfcAdaptation* NfcAdaptation::mpInstance = NULL;
67ThreadMutex NfcAdaptation::sLock;
68tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = NULL;
69tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = NULL;
70ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent;
71ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent;
72sp<INfc> NfcAdaptation::mHal;
73sp<INfcV1_1> NfcAdaptation::mHal_1_1;
74INfcClientCallback* NfcAdaptation::mCallback;
hariprasad nalacheruvu02f51032018-06-05 20:16:48 +053075sp<NfcDeathRecipient> NfcAdaptation::mDeathRecipient = NULL;
nxf24591c1cbeab2018-02-21 17:32:26 +053076
77bool nfc_debug_enabled = false;
78std::string nfc_storage_path;
79uint8_t appl_dta_mode_flag = 0x00;
80
81extern tNFA_DM_CFG nfa_dm_cfg;
82extern tNFA_PROPRIETARY_CFG nfa_proprietary_cfg;
83extern tNFA_HCI_CFG nfa_hci_cfg;
84extern uint8_t nfa_ee_max_ee_cfg;
85extern bool nfa_poll_bail_out_mode;
86
nxf24591c1cbeab2018-02-21 17:32:26 +053087// Whitelist for hosts allowed to create a pipe
88// See ADM_CREATE_PIPE command in the ETSI test specification
89// ETSI TS 102 622, section 6.1.3.1
90static std::vector<uint8_t> host_whitelist;
91
92namespace {
93void initializeGlobalDebugEnabledFlag() {
94 nfc_debug_enabled =
95 (NfcConfig::getUnsigned(NAME_NFC_DEBUG_ENABLED, 0) != 0) ? true : false;
96
97 char valueStr[PROPERTY_VALUE_MAX] = {0};
98 int len = property_get("nfc.debug_enabled", valueStr, "");
99 if (len > 0) {
100 // let Android property override .conf variable
101 unsigned debug_enabled = 0;
102 sscanf(valueStr, "%u", &debug_enabled);
103 nfc_debug_enabled = (debug_enabled == 0) ? false : true;
104 }
105
106 DLOG_IF(INFO, nfc_debug_enabled)
107 << StringPrintf("%s: level=%u", __func__, nfc_debug_enabled);
108}
109} // namespace
110
111class NfcClientCallback : public INfcClientCallback {
112 public:
113 NfcClientCallback(tHAL_NFC_CBACK* eventCallback,
114 tHAL_NFC_DATA_CBACK dataCallback) {
115 mEventCallback = eventCallback;
116 mDataCallback = dataCallback;
117 };
118 virtual ~NfcClientCallback() = default;
Suraj Uday Kotharkar10d9fc22018-05-09 15:05:56 +0530119 Return<void> sendEvent_1_1(
120 ::android::hardware::nfc::V1_1::NfcEvent event,
121 ::android::hardware::nfc::V1_0::NfcStatus event_status) override {
122 mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)event_status);
123 return Void();
124 };
nxf24591c1cbeab2018-02-21 17:32:26 +0530125 Return<void> sendEvent(
126 ::android::hardware::nfc::V1_0::NfcEvent event,
127 ::android::hardware::nfc::V1_0::NfcStatus event_status) override {
128 mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)event_status);
129 return Void();
130 };
131 Return<void> sendData(
132 const ::android::hardware::nfc::V1_0::NfcData& data) override {
133 ::android::hardware::nfc::V1_0::NfcData copy = data;
134 mDataCallback(copy.size(), &copy[0]);
135 return Void();
136 };
137
138 private:
139 tHAL_NFC_CBACK* mEventCallback;
140 tHAL_NFC_DATA_CBACK* mDataCallback;
141};
142
hariprasad nalacheruvu02f51032018-06-05 20:16:48 +0530143class NfcDeathRecipient : public hidl_death_recipient {
144 public:
145 NfcDeathRecipient() {}
146
147 virtual void serviceDied(
148 uint64_t /* cookie */,
149 const android::wp<::android::hidl::base::V1_0::IBase>& /* who */) {
150 ALOGE("NfcDeathRecipient::serviceDied - Nfc Hal service died");
151 abort();
152 }
153};
154
nxf24591c1cbeab2018-02-21 17:32:26 +0530155/*******************************************************************************
156**
157** Function: NfcAdaptation::NfcAdaptation()
158**
159** Description: class constructor
160**
161** Returns: none
162**
163*******************************************************************************/
164NfcAdaptation::NfcAdaptation() {
165 memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
166}
167
168/*******************************************************************************
169**
170** Function: NfcAdaptation::~NfcAdaptation()
171**
172** Description: class destructor
173**
174** Returns: none
175**
176*******************************************************************************/
177NfcAdaptation::~NfcAdaptation() { mpInstance = NULL; }
178
179/*******************************************************************************
180**
181** Function: NfcAdaptation::GetInstance()
182**
183** Description: access class singleton
184**
185** Returns: pointer to the singleton object
186**
187*******************************************************************************/
188NfcAdaptation& NfcAdaptation::GetInstance() {
189 AutoThreadMutex a(sLock);
190
191 if (!mpInstance) {
192 mpInstance = new NfcAdaptation;
193 mpInstance->InitializeHalDeviceContext();
194 }
195 return *mpInstance;
196}
197
Suraj Uday Kotharkard5af4382018-05-10 14:23:43 +0530198void NfcAdaptation::GetVendorConfigs(
199 std::map<std::string, ConfigValue>& configMap) {
200 if (mHal_1_1) {
201 mHal_1_1->getConfig([&configMap](NfcVendorConfig config) {
202 std::vector<uint8_t> nfaPropCfg = {
203 config.nfaProprietaryCfg.protocol18092Active,
204 config.nfaProprietaryCfg.protocolBPrime,
205 config.nfaProprietaryCfg.protocolDual,
206 config.nfaProprietaryCfg.protocol15693,
207 config.nfaProprietaryCfg.protocolKovio,
208 config.nfaProprietaryCfg.protocolMifare,
209 config.nfaProprietaryCfg.discoveryPollKovio,
210 config.nfaProprietaryCfg.discoveryPollBPrime,
211 config.nfaProprietaryCfg.discoveryListenBPrime};
212 configMap.emplace(NAME_NFA_PROPRIETARY_CFG, ConfigValue(nfaPropCfg));
213 configMap.emplace(NAME_NFA_POLL_BAIL_OUT_MODE,
214 ConfigValue(config.nfaPollBailOutMode ? 1 : 0));
215 configMap.emplace(NAME_DEFAULT_OFFHOST_ROUTE,
216 ConfigValue(config.defaultOffHostRoute));
Suraj Uday Kotharkar00b12c22018-05-10 15:04:50 +0530217 configMap.emplace(NAME_DEFAULT_ROUTE, ConfigValue(config.defaultRoute));
Suraj Uday Kotharkard5af4382018-05-10 14:23:43 +0530218 configMap.emplace(NAME_DEFAULT_NFCF_ROUTE,
219 ConfigValue(config.defaultOffHostRouteFelica));
220 configMap.emplace(NAME_DEFAULT_SYS_CODE_ROUTE,
221 ConfigValue(config.defaultSystemCodeRoute));
Suraj Uday Kotharkardf5d02e2018-05-10 14:54:54 +0530222 configMap.emplace(NAME_DEFAULT_SYS_CODE_PWR_STATE,
223 ConfigValue(config.defaultSystemCodePowerState));
Suraj Uday Kotharkard5af4382018-05-10 14:23:43 +0530224 configMap.emplace(NAME_OFF_HOST_SIM_PIPE_ID,
225 ConfigValue(config.offHostSIMPipeId));
226 configMap.emplace(NAME_OFF_HOST_ESE_PIPE_ID,
227 ConfigValue(config.offHostESEPipeId));
228 configMap.emplace(NAME_ISO_DEP_MAX_TRANSCEIVE,
229 ConfigValue(config.maxIsoDepTransceiveLength));
230 if (config.hostWhitelist.size() != 0) {
231 configMap.emplace(NAME_DEVICE_HOST_WHITE_LIST,
232 ConfigValue(config.hostWhitelist));
233 }
234 /* For Backwards compatibility */
235 if (config.presenceCheckAlgorithm ==
236 PresenceCheckAlgorithm::ISO_DEP_NAK) {
237 configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
238 ConfigValue((uint32_t)NFA_RW_PRES_CHK_ISO_DEP_NAK));
239 } else {
240 configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
241 ConfigValue((uint32_t)config.presenceCheckAlgorithm));
242 }
243 });
244 }
245}
nxf24591c1cbeab2018-02-21 17:32:26 +0530246/*******************************************************************************
247**
248** Function: NfcAdaptation::Initialize()
249**
250** Description: class initializer
251**
252** Returns: none
253**
254*******************************************************************************/
255void NfcAdaptation::Initialize() {
256 const char* func = "NfcAdaptation::Initialize";
257 const char* argv[] = {"libnfc_nci"};
258 // Init log tag
259 base::CommandLine::Init(1, argv);
260
261 // Android already logs thread_id, proc_id, timestamp, so disable those.
262 logging::SetLogItems(false, false, false, false);
263
264 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
Suraj Uday Kotharkar1a0a0662018-05-15 16:23:32 +0530265
nxf24591c1cbeab2018-02-21 17:32:26 +0530266 nfc_storage_path = NfcConfig::getString(NAME_NFA_STORAGE, "/data/nfc");
267
268 if (NfcConfig::hasKey(NAME_NFA_DM_CFG)) {
269 std::vector<uint8_t> dm_config = NfcConfig::getBytes(NAME_NFA_DM_CFG);
270 if (dm_config.size() > 0) nfa_dm_cfg.auto_detect_ndef = dm_config[0];
271 if (dm_config.size() > 1) nfa_dm_cfg.auto_read_ndef = dm_config[1];
272 if (dm_config.size() > 2) nfa_dm_cfg.auto_presence_check = dm_config[2];
273 if (dm_config.size() > 3) nfa_dm_cfg.presence_check_option = dm_config[3];
274 // NOTE: The timeout value is not configurable here because the endianess
275 // of a byte array is ambiguous and needlessly difficult to configure.
276 // If this value needs to be configgurable, a numeric config option should
277 // be used.
278 }
279
280 if (NfcConfig::hasKey(NAME_NFA_MAX_EE_SUPPORTED)) {
281 nfa_ee_max_ee_cfg = NfcConfig::getUnsigned(NAME_NFA_MAX_EE_SUPPORTED);
282 DLOG_IF(INFO, nfc_debug_enabled)
283 << StringPrintf("%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d",
284 func, nfa_ee_max_ee_cfg);
285 }
286
287 if (NfcConfig::hasKey(NAME_NFA_POLL_BAIL_OUT_MODE)) {
288 nfa_poll_bail_out_mode =
289 NfcConfig::getUnsigned(NAME_NFA_POLL_BAIL_OUT_MODE);
290 DLOG_IF(INFO, nfc_debug_enabled)
291 << StringPrintf("%s: Overriding NFA_POLL_BAIL_OUT_MODE to use %d", func,
292 nfa_poll_bail_out_mode);
293 }
294
295 if (NfcConfig::hasKey(NAME_NFA_PROPRIETARY_CFG)) {
296 std::vector<uint8_t> p_config =
297 NfcConfig::getBytes(NAME_NFA_PROPRIETARY_CFG);
298 if (p_config.size() > 0)
299 nfa_proprietary_cfg.pro_protocol_18092_active = p_config[0];
300 if (p_config.size() > 1)
301 nfa_proprietary_cfg.pro_protocol_b_prime = p_config[1];
302 if (p_config.size() > 2)
303 nfa_proprietary_cfg.pro_protocol_dual = p_config[2];
304 if (p_config.size() > 3)
305 nfa_proprietary_cfg.pro_protocol_15693 = p_config[3];
306 if (p_config.size() > 4)
307 nfa_proprietary_cfg.pro_protocol_kovio = p_config[4];
308 if (p_config.size() > 5) nfa_proprietary_cfg.pro_protocol_mfc = p_config[5];
309 if (p_config.size() > 6)
310 nfa_proprietary_cfg.pro_discovery_kovio_poll = p_config[6];
311 if (p_config.size() > 7)
312 nfa_proprietary_cfg.pro_discovery_b_prime_poll = p_config[7];
313 if (p_config.size() > 8)
314 nfa_proprietary_cfg.pro_discovery_b_prime_listen = p_config[8];
315 }
316
317 // Configure whitelist of HCI host ID's
318 // See specification: ETSI TS 102 622, section 6.1.3.1
319 if (NfcConfig::hasKey(NAME_DEVICE_HOST_WHITE_LIST)) {
320 host_whitelist = NfcConfig::getBytes(NAME_DEVICE_HOST_WHITE_LIST);
321 nfa_hci_cfg.num_whitelist_host = host_whitelist.size();
322 nfa_hci_cfg.p_whitelist = &host_whitelist[0];
323 }
Suraj Uday Kotharkara25abd72018-05-10 14:44:59 +0530324
nxf24591c1cbeab2018-02-21 17:32:26 +0530325 initializeGlobalDebugEnabledFlag();
nxf24591193ee782018-06-06 14:26:10 +0530326#if(NXP_EXTNS == TRUE)
327 if (NfcConfig::hasKey(NAME_NXP_WM_MAX_WTX_COUNT)) {
328 nfa_hci_cfg.max_wtx_count = NfcConfig::getUnsigned(NAME_NXP_WM_MAX_WTX_COUNT);
329 DLOG_IF(INFO, nfc_debug_enabled)
330 << StringPrintf("%s: MAX_WTX_COUNT to wait for HCI response %d",
331 func, nfa_hci_cfg.max_wtx_count);
332 }
333#endif
nxf24591c1cbeab2018-02-21 17:32:26 +0530334 verify_stack_non_volatile_store();
335 if (NfcConfig::hasKey(NAME_PRESERVE_STORAGE) &&
336 NfcConfig::getUnsigned(NAME_PRESERVE_STORAGE) == 1) {
337 DLOG_IF(INFO, nfc_debug_enabled)
338 << StringPrintf("%s: preserve stack NV store", __func__);
339 } else {
340 delete_stack_non_volatile_store(FALSE);
341 }
342
343 GKI_init();
344 GKI_enable();
345 GKI_create_task((TASKPTR)NFCA_TASK, BTU_TASK, (int8_t*)"NFCA_TASK", 0, 0,
346 (pthread_cond_t*)NULL, NULL);
347 {
348 AutoThreadMutex guard(mCondVar);
349 GKI_create_task((TASKPTR)Thread, MMI_TASK, (int8_t*)"NFCA_THREAD", 0, 0,
350 (pthread_cond_t*)NULL, NULL);
351 mCondVar.wait();
352 }
353
354 debug_nfcsnoop_init();
Arjun Thottingal Jayarajan4f71c362018-07-18 21:05:35 +0530355 configureRpcThreadpool(2, false);
nxf24591c1cbeab2018-02-21 17:32:26 +0530356 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
357}
358
359/*******************************************************************************
360**
361** Function: NfcAdaptation::Finalize()
362**
363** Description: class finalizer
364**
365** Returns: none
366**
367*******************************************************************************/
368void NfcAdaptation::Finalize() {
369 const char* func = "NfcAdaptation::Finalize";
370 AutoThreadMutex a(sLock);
371
372 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
373 GKI_shutdown();
374
375 NfcConfig::clear();
376
377 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
378 delete this;
379}
380
nxf24591619d2162018-06-11 13:21:51 +0530381/*******************************************************************************
382**
383** Function: NfcAdaptation::FactoryReset
384**
385** Description: Native support for FactoryReset function.
386** It will delete the nfaStorage file and invoke the factory reset function
387** in HAL level to set the session ID to default value.
388**
389** Returns: None.
390**
391*******************************************************************************/
nxf24591c1cbeab2018-02-21 17:32:26 +0530392void NfcAdaptation::FactoryReset() {
Suraj Uday Kotharkar71fc5802018-06-01 16:57:34 +0530393#if(NXP_EXTNS == TRUE)
nxf24591619d2162018-06-11 13:21:51 +0530394 const char* func = "NfcAdaptation::FactoryReset";
395 int status;
396 const char config_eseinfo_path[] = "/data/nfc/nfaStorage.bin1";
Suraj Uday Kotharkar71fc5802018-06-01 16:57:34 +0530397#endif
nxf24591c1cbeab2018-02-21 17:32:26 +0530398 if (mHal_1_1 != nullptr) {
399 mHal_1_1->factoryReset();
Suraj Uday Kotharkar71fc5802018-06-01 16:57:34 +0530400#if(NXP_EXTNS == TRUE)
nxf24591619d2162018-06-11 13:21:51 +0530401 status=remove(config_eseinfo_path);
402 if(status==0){
403 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: Storage file deleted... ", func);
404 }
405 else{
406 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: Storage file delete failed... ", func);
407 }
Suraj Uday Kotharkar71fc5802018-06-01 16:57:34 +0530408#endif
nxf24591c1cbeab2018-02-21 17:32:26 +0530409 }
410}
411
412void NfcAdaptation::DeviceShutdown() {
413 if (mHal_1_1 != nullptr) {
414 mHal_1_1->closeForPowerOffCase();
415 }
416}
417
418/*******************************************************************************
419**
420** Function: NfcAdaptation::Dump
421**
422** Description: Native support for dumpsys function.
423**
424** Returns: None.
425**
426*******************************************************************************/
427void NfcAdaptation::Dump(int fd) { debug_nfcsnoop_dump(fd); }
428
429/*******************************************************************************
430**
431** Function: NfcAdaptation::signal()
432**
433** Description: signal the CondVar to release the thread that is waiting
434**
435** Returns: none
436**
437*******************************************************************************/
438void NfcAdaptation::signal() { mCondVar.signal(); }
439
440/*******************************************************************************
441**
442** Function: NfcAdaptation::NFCA_TASK()
443**
444** Description: NFCA_TASK runs the GKI main task
445**
446** Returns: none
447**
448*******************************************************************************/
449uint32_t NfcAdaptation::NFCA_TASK(__attribute__((unused)) uint32_t arg) {
450 const char* func = "NfcAdaptation::NFCA_TASK";
451 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
452 GKI_run(0);
453 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
454 return 0;
455}
456
457/*******************************************************************************
458**
459** Function: NfcAdaptation::Thread()
460**
461** Description: Creates work threads
462**
463** Returns: none
464**
465*******************************************************************************/
466uint32_t NfcAdaptation::Thread(__attribute__((unused)) uint32_t arg) {
467 const char* func = "NfcAdaptation::Thread";
468 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
469
470 {
471 ThreadCondVar CondVar;
472 AutoThreadMutex guard(CondVar);
473 GKI_create_task((TASKPTR)nfc_task, NFC_TASK, (int8_t*)"NFC_TASK", 0, 0,
474 (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
475 CondVar.wait();
476 }
477
478 NfcAdaptation::GetInstance().signal();
479
480 GKI_exit_task(GKI_get_taskid());
481 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
482 return 0;
483}
484
485/*******************************************************************************
486**
487** Function: NfcAdaptation::GetHalEntryFuncs()
488**
489** Description: Get the set of HAL entry points.
490**
491** Returns: Functions pointers for HAL entry points.
492**
493*******************************************************************************/
494tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs() { return &mHalEntryFuncs; }
495
496/*******************************************************************************
497**
498** Function: NfcAdaptation::InitializeHalDeviceContext
499**
500** Description: Ask the generic Android HAL to find the Broadcom-specific HAL.
501**
502** Returns: None.
503**
504*******************************************************************************/
505void NfcAdaptation::InitializeHalDeviceContext() {
506 const char* func = "NfcAdaptation::InitializeHalDeviceContext";
507
508 mHalEntryFuncs.initialize = HalInitialize;
509 mHalEntryFuncs.terminate = HalTerminate;
510 mHalEntryFuncs.open = HalOpen;
511 mHalEntryFuncs.close = HalClose;
512 mHalEntryFuncs.core_initialized = HalCoreInitialized;
513 mHalEntryFuncs.write = HalWrite;
514 mHalEntryFuncs.prediscover = HalPrediscover;
515 mHalEntryFuncs.control_granted = HalControlGranted;
516 mHalEntryFuncs.power_cycle = HalPowerCycle;
517 mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;
518 LOG(INFO) << StringPrintf("%s: INfc::getService()", func);
519 mHal = mHal_1_1 = INfcV1_1::getService();
520 if (mHal_1_1 == nullptr) {
521 mHal = INfc::getService();
522 }
523 LOG_FATAL_IF(mHal == nullptr, "Failed to retrieve the NFC HAL!");
524 LOG(INFO) << StringPrintf("%s: INfc::getService() returned %p (%s)", func,
525 mHal.get(),
526 (mHal->isRemote() ? "remote" : "local"));
527}
528
529/*******************************************************************************
530**
531** Function: NfcAdaptation::HalInitialize
532**
533** Description: Not implemented because this function is only needed
534** within the HAL.
535**
536** Returns: None.
537**
538*******************************************************************************/
539void NfcAdaptation::HalInitialize() {
540 const char* func = "NfcAdaptation::HalInitialize";
541 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
542}
543
544/*******************************************************************************
545**
546** Function: NfcAdaptation::HalTerminate
547**
548** Description: Not implemented because this function is only needed
549** within the HAL.
550**
551** Returns: None.
552**
553*******************************************************************************/
554void NfcAdaptation::HalTerminate() {
555 const char* func = "NfcAdaptation::HalTerminate";
556 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
557}
558
559/*******************************************************************************
560**
561** Function: NfcAdaptation::HalOpen
562**
563** Description: Turn on controller, download firmware.
564**
565** Returns: None.
566**
567*******************************************************************************/
568void NfcAdaptation::HalOpen(tHAL_NFC_CBACK* p_hal_cback,
569 tHAL_NFC_DATA_CBACK* p_data_cback) {
570 const char* func = "NfcAdaptation::HalOpen";
571 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
572 mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
hariprasad nalacheruvu02f51032018-06-05 20:16:48 +0530573 mDeathRecipient = new NfcDeathRecipient();
Suraj Uday Kotharkar10d9fc22018-05-09 15:05:56 +0530574 if (mHal_1_1 != nullptr) {
575 mHal_1_1->open_1_1(mCallback);
hariprasad nalacheruvu02f51032018-06-05 20:16:48 +0530576 mHal_1_1->linkToDeath(mDeathRecipient, 0);
Suraj Uday Kotharkar10d9fc22018-05-09 15:05:56 +0530577 } else {
578 mHal->open(mCallback);
hariprasad nalacheruvu02f51032018-06-05 20:16:48 +0530579 mHal->linkToDeath(mDeathRecipient, 0);
Suraj Uday Kotharkar10d9fc22018-05-09 15:05:56 +0530580 }
nxf24591c1cbeab2018-02-21 17:32:26 +0530581}
582
583/*******************************************************************************
584**
585** Function: NfcAdaptation::HalClose
586**
587** Description: Turn off controller.
588**
589** Returns: None.
590**
591*******************************************************************************/
592void NfcAdaptation::HalClose() {
593 const char* func = "NfcAdaptation::HalClose";
594 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
595 mHal->close();
hariprasad nalacheruvu02f51032018-06-05 20:16:48 +0530596 mHal->unlinkToDeath(mDeathRecipient);
nxf24591c1cbeab2018-02-21 17:32:26 +0530597}
598
599/*******************************************************************************
600**
601** Function: NfcAdaptation::HalDeviceContextCallback
602**
603** Description: Translate generic Android HAL's callback into Broadcom-specific
604** callback function.
605**
606** Returns: None.
607**
608*******************************************************************************/
609void NfcAdaptation::HalDeviceContextCallback(nfc_event_t event,
610 nfc_status_t event_status) {
611 const char* func = "NfcAdaptation::HalDeviceContextCallback";
612 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: event=%u", func, event);
613 if (mHalCallback) mHalCallback(event, (tHAL_NFC_STATUS)event_status);
614}
615
616/*******************************************************************************
617**
618** Function: NfcAdaptation::HalDeviceContextDataCallback
619**
620** Description: Translate generic Android HAL's callback into Broadcom-specific
621** callback function.
622**
623** Returns: None.
624**
625*******************************************************************************/
626void NfcAdaptation::HalDeviceContextDataCallback(uint16_t data_len,
627 uint8_t* p_data) {
628 const char* func = "NfcAdaptation::HalDeviceContextDataCallback";
629 DLOG_IF(INFO, nfc_debug_enabled)
630 << StringPrintf("%s: len=%u", func, data_len);
631 if (mHalDataCallback) mHalDataCallback(data_len, p_data);
632}
633
634/*******************************************************************************
635**
636** Function: NfcAdaptation::HalWrite
637**
638** Description: Write NCI message to the controller.
639**
640** Returns: None.
641**
642*******************************************************************************/
643void NfcAdaptation::HalWrite(uint16_t data_len, uint8_t* p_data) {
644 const char* func = "NfcAdaptation::HalWrite";
645 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
646 ::android::hardware::nfc::V1_0::NfcData data;
647 data.setToExternal(p_data, data_len);
648 mHal->write(data);
649}
Ravishinde26233bb2018-08-06 22:16:08 +0530650#if (NXP_EXTNS == TRUE)
651/*******************************************************************************
652**
653** Function: IoctlCallback
654**
655** Description: Callback from HAL stub for IOCTL api invoked.
656** Output data for IOCTL is sent as argument
657**
658** Returns: None.
659**
660*******************************************************************************/
nxf24591c1cbeab2018-02-21 17:32:26 +0530661
Ravishinde26233bb2018-08-06 22:16:08 +0530662void IoctlCallback(::android::hardware::nfc::V1_0::NfcData outputData) {
663 const char* func = "IoctlCallback";
664 nfc_nci_ExtnOutputData_t* pOutData =
665 (nfc_nci_ExtnOutputData_t*)&outputData[0];
666 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s Ioctl Type=%llu", func, (unsigned long long)pOutData->ioctlType);
667 NfcAdaptation* pAdaptation = (NfcAdaptation*)pOutData->context;
668 /*Output Data from stub->Proxy is copied back to output data
669 * This data will be sent back to libnfc*/
670 memcpy(&pAdaptation->mCurrentIoctlData->out, &outputData[0],
671 sizeof(nfc_nci_ExtnOutputData_t));
672}
673/*******************************************************************************
674**
675** Function: NfcAdaptation::HalIoctl
676**
677** Description: Calls ioctl to the Nfc driver.
678** If called with a arg value of 0x01 than wired access requested,
679** status of the requst would be updated to p_data.
680** If called with a arg value of 0x00 than wired access will be
681** released, status of the requst would be updated to p_data.
682** If called with a arg value of 0x02 than current p61 state would
683*be
684** updated to p_data.
685**
686** Returns: -1 or 0.
687**
688*******************************************************************************/
689
690int NfcAdaptation::HalIoctl(long arg, void* p_data) {
691 const char* func = "NfcAdaptation::HalIoctl";
692 ::android::hardware::nfc::V1_0::NfcData data;
693 AutoThreadMutex a(sIoctlLock);
694 nfc_nci_IoctlInOutData_t* pInpOutData = (nfc_nci_IoctlInOutData_t*)p_data;
695 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s arg=%ld", func, arg);
696 pInpOutData->inp.context = &NfcAdaptation::GetInstance();
697 NfcAdaptation::GetInstance().mCurrentIoctlData = pInpOutData;
698 data.setToExternal((uint8_t*)pInpOutData, sizeof(nfc_nci_IoctlInOutData_t));
699 /*Insert Transit config at the end of IOCTL data as transit buffer also
700 needs to be part of NfcData(hidl_vec)*/
701 if (arg == HAL_NFC_IOCTL_SET_TRANSIT_CONFIG) {
702 std::vector<uint8_t> tempStdVec(data);
703 tempStdVec.insert(
704 tempStdVec.end(), pInpOutData->inp.data.transitConfig.val,
705 pInpOutData->inp.data.transitConfig.val +
706 (pInpOutData->inp.data.transitConfig.len));
707 data = tempStdVec;
708 }
709 if(mHalNxpNfc != nullptr)
710 mHalNxpNfc->ioctl(arg, data, IoctlCallback);
711 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s Ioctl Completed for Type=%llu", func, (unsigned long long)pInpOutData->out.ioctlType);
712 return (pInpOutData->out.result);
713}
714#endif
nxf24591c1cbeab2018-02-21 17:32:26 +0530715/*******************************************************************************
716**
717** Function: NfcAdaptation::HalCoreInitialized
718**
719** Description: Adjust the configurable parameters in the controller.
720**
721** Returns: None.
722**
723*******************************************************************************/
724void NfcAdaptation::HalCoreInitialized(uint16_t data_len,
725 uint8_t* p_core_init_rsp_params) {
726 const char* func = "NfcAdaptation::HalCoreInitialized";
727 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
728 hidl_vec<uint8_t> data;
729 data.setToExternal(p_core_init_rsp_params, data_len);
730
731 mHal->coreInitialized(data);
732}
733
734/*******************************************************************************
735**
736** Function: NfcAdaptation::HalPrediscover
737**
738** Description: Perform any vendor-specific pre-discovery actions (if
739** needed) If any actions were performed TRUE will be returned,
740** and HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are
741** completed.
742**
743** Returns: TRUE if vendor-specific pre-discovery actions initialized
744** FALSE if no vendor-specific pre-discovery actions are
745** needed.
746**
747*******************************************************************************/
748bool NfcAdaptation::HalPrediscover() {
749 const char* func = "NfcAdaptation::HalPrediscover";
750 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
751 bool retval = FALSE;
752 mHal->prediscover();
753 return retval;
754}
755
756/*******************************************************************************
757**
758** Function: HAL_NfcControlGranted
759**
760** Description: Grant control to HAL control for sending NCI commands.
761** Call in response to HAL_REQUEST_CONTROL_EVT.
762** Must only be called when there are no NCI commands pending.
763** HAL_RELEASE_CONTROL_EVT will notify when HAL no longer
764** needs control of NCI.
765**
766** Returns: void
767**
768*******************************************************************************/
769void NfcAdaptation::HalControlGranted() {
770 const char* func = "NfcAdaptation::HalControlGranted";
771 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
772 mHal->controlGranted();
773}
774
775/*******************************************************************************
776**
777** Function: NfcAdaptation::HalPowerCycle
778**
779** Description: Turn off and turn on the controller.
780**
781** Returns: None.
782**
783*******************************************************************************/
784void NfcAdaptation::HalPowerCycle() {
785 const char* func = "NfcAdaptation::HalPowerCycle";
786 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
787 mHal->powerCycle();
788}
789
790/*******************************************************************************
791**
792** Function: NfcAdaptation::HalGetMaxNfcee
793**
794** Description: Turn off and turn on the controller.
795**
796** Returns: None.
797**
798*******************************************************************************/
799uint8_t NfcAdaptation::HalGetMaxNfcee() {
800 const char* func = "NfcAdaptation::HalPowerCycle";
801 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
802
803 return nfa_ee_max_ee_cfg;
804}
805
806/*******************************************************************************
807**
808** Function: NfcAdaptation::DownloadFirmware
809**
810** Description: Download firmware patch files.
811**
812** Returns: None.
813**
814*******************************************************************************/
815void NfcAdaptation::DownloadFirmware() {
816 const char* func = "NfcAdaptation::DownloadFirmware";
817 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
818 HalInitialize();
819
820 mHalOpenCompletedEvent.lock();
821 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: try open HAL", func);
822 HalOpen(HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback);
823 mHalOpenCompletedEvent.wait();
824
825 mHalCloseCompletedEvent.lock();
826 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: try close HAL", func);
827 HalClose();
828 mHalCloseCompletedEvent.wait();
829
830 HalTerminate();
831 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
832}
833
834/*******************************************************************************
835**
836** Function: NfcAdaptation::HalDownloadFirmwareCallback
837**
838** Description: Receive events from the HAL.
839**
840** Returns: None.
841**
842*******************************************************************************/
843void NfcAdaptation::HalDownloadFirmwareCallback(nfc_event_t event,
844 __attribute__((unused))
845 nfc_status_t event_status) {
846 const char* func = "NfcAdaptation::HalDownloadFirmwareCallback";
847 DLOG_IF(INFO, nfc_debug_enabled)
848 << StringPrintf("%s: event=0x%X", func, event);
849 switch (event) {
850 case HAL_NFC_OPEN_CPLT_EVT: {
851 DLOG_IF(INFO, nfc_debug_enabled)
852 << StringPrintf("%s: HAL_NFC_OPEN_CPLT_EVT", func);
853 mHalOpenCompletedEvent.signal();
854 break;
855 }
856 case HAL_NFC_CLOSE_CPLT_EVT: {
857 DLOG_IF(INFO, nfc_debug_enabled)
858 << StringPrintf("%s: HAL_NFC_CLOSE_CPLT_EVT", func);
859 mHalCloseCompletedEvent.signal();
860 break;
861 }
862 }
863}
864
865/*******************************************************************************
866**
867** Function: NfcAdaptation::HalDownloadFirmwareDataCallback
868**
869** Description: Receive data events from the HAL.
870**
871** Returns: None.
872**
873*******************************************************************************/
874void NfcAdaptation::HalDownloadFirmwareDataCallback(__attribute__((unused))
875 uint16_t data_len,
876 __attribute__((unused))
877 uint8_t* p_data) {}
878
879/*******************************************************************************
880**
881** Function: ThreadMutex::ThreadMutex()
882**
883** Description: class constructor
884**
885** Returns: none
886**
887*******************************************************************************/
888ThreadMutex::ThreadMutex() {
889 pthread_mutexattr_t mutexAttr;
890
891 pthread_mutexattr_init(&mutexAttr);
892 pthread_mutex_init(&mMutex, &mutexAttr);
893 pthread_mutexattr_destroy(&mutexAttr);
894}
895
896/*******************************************************************************
897**
898** Function: ThreadMutex::~ThreadMutex()
899**
900** Description: class destructor
901**
902** Returns: none
903**
904*******************************************************************************/
905ThreadMutex::~ThreadMutex() { pthread_mutex_destroy(&mMutex); }
906
907/*******************************************************************************
908**
909** Function: ThreadMutex::lock()
910**
911** Description: lock kthe mutex
912**
913** Returns: none
914**
915*******************************************************************************/
916void ThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
917
918/*******************************************************************************
919**
920** Function: ThreadMutex::unblock()
921**
922** Description: unlock the mutex
923**
924** Returns: none
925**
926*******************************************************************************/
927void ThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
928
929/*******************************************************************************
930**
931** Function: ThreadCondVar::ThreadCondVar()
932**
933** Description: class constructor
934**
935** Returns: none
936**
937*******************************************************************************/
938ThreadCondVar::ThreadCondVar() {
939 pthread_condattr_t CondAttr;
940
941 pthread_condattr_init(&CondAttr);
942 pthread_cond_init(&mCondVar, &CondAttr);
943
944 pthread_condattr_destroy(&CondAttr);
945}
946
947/*******************************************************************************
948**
949** Function: ThreadCondVar::~ThreadCondVar()
950**
951** Description: class destructor
952**
953** Returns: none
954**
955*******************************************************************************/
956ThreadCondVar::~ThreadCondVar() { pthread_cond_destroy(&mCondVar); }
957
958/*******************************************************************************
959**
960** Function: ThreadCondVar::wait()
961**
962** Description: wait on the mCondVar
963**
964** Returns: none
965**
966*******************************************************************************/
967void ThreadCondVar::wait() {
968 pthread_cond_wait(&mCondVar, *this);
969 pthread_mutex_unlock(*this);
970}
971
972/*******************************************************************************
973**
974** Function: ThreadCondVar::signal()
975**
976** Description: signal the mCondVar
977**
978** Returns: none
979**
980*******************************************************************************/
981void ThreadCondVar::signal() {
982 AutoThreadMutex a(*this);
983 pthread_cond_signal(&mCondVar);
984}
985
986/*******************************************************************************
987**
988** Function: AutoThreadMutex::AutoThreadMutex()
989**
990** Description: class constructor, automatically lock the mutex
991**
992** Returns: none
993**
994*******************************************************************************/
995AutoThreadMutex::AutoThreadMutex(ThreadMutex& m) : mm(m) { mm.lock(); }
996
997/*******************************************************************************
998**
999** Function: AutoThreadMutex::~AutoThreadMutex()
1000**
1001** Description: class destructor, automatically unlock the mutex
1002**
1003** Returns: none
1004**
1005*******************************************************************************/
1006AutoThreadMutex::~AutoThreadMutex() { mm.unlock(); }