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