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