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