/*******************************************************************************
**
** Function:    NfcAdaptation::InitializeHalDeviceContext
**
** Description: Ask the generic Android HAL to find the Broadcom-specific HAL.
**
** Returns:     None.
**
*******************************************************************************/
void NfcAdaptation::InitializeHalDeviceContext ()
{
    const char* func = "NfcAdaptation::InitializeHalDeviceContext";
    ALOGD ("%s: enter", func);
    int ret = 0; //0 means success
    const hw_module_t* hw_module = NULL;

    mHalEntryFuncs.initialize = HalInitialize;
    mHalEntryFuncs.terminate = HalTerminate;
    mHalEntryFuncs.open = HalOpen;
    mHalEntryFuncs.close = HalClose;
    mHalEntryFuncs.core_initialized = HalCoreInitialized;
    mHalEntryFuncs.write = HalWrite;
    mHalEntryFuncs.prediscover = HalPrediscover;
    mHalEntryFuncs.control_granted = HalControlGranted;
    mHalEntryFuncs.power_cycle = HalPowerCycle;
    mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;

#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
    ret = hw_get_module ("nfc_nci_pn547", &hw_module);
#else
    ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
#endif
    if (ret == 0)
    {
        ret = nfc_nci_open (hw_module, &mHalDeviceContext);
        if (ret != 0)
            ALOGE ("%s: nfc_nci_open fail", func);
    }
    else
        ALOGE ("%s: fail hw_get_module", func);
    ALOGD ("%s: exit", func);
}
INfc* HIDL_FETCH_INfc(const char * /*name*/) {
    nfc_nci_device_t* nfc_device;
    int ret = 0;
    const hw_module_t* hw_module = nullptr;

    ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
    if (ret == 0) {
        ret = nfc_nci_open (hw_module, &nfc_device);
        if (ret != 0) {
            ALOGE ("nfc_nci_open failed: %d", ret);
        }
    }
    else
        ALOGE ("hw_get_module %s failed: %d", NFC_NCI_HARDWARE_MODULE_ID, ret);

    if (ret == 0) {
        return new Nfc(nfc_device);
    } else {
        ALOGE("Passthrough failed to load legacy HAL.");
        return nullptr;
    }
}
/*===========================================================================
FUNCTION   ftm_nfc_hal_open
DESCRIPTION
   This function will open the nfc hal for ftm nfc command processing.
DEPENDENCIES
  NIL
RETURN VALUE
  void
SIDE EFFECTS
  NONE
===============================================================================*/
uint8 ftm_nfc_hal_open(void)
{
    uint8 ret = 0;
    ret = hw_get_module(NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
    if(ret == 0)
    {
        dev = (nfc_nci_device_t*)malloc(sizeof(nfc_nci_device_t));
        if(!dev)
        {
            printf("NFC FTM : mem allocation failed \n");
            return FALSE;
        }
        else
        {
            ret = nfc_nci_open (hw_module, &dev);
            if(ret != 0)
            {
                printf("NFC FTM : nfc_nci_open fail \n");
                free(dev);
                return FALSE;
            }
            else
            {
                printf("NFC FTM : opening NCI HAL \n");
                dev->common.reserved[0] = FTM_MODE;
                dev->open (dev, nfc_ftm_cback, nfc_ftm_data_cback);
                sem_wait(&semaphore_halcmd_complete);
            }
        }
    }
    else
    {
       printf("NFC FTM : hw_get_module() call failed \n");
       return FALSE;
    }
    return TRUE;
}