Beispiel #1
0
void testNfc(int readNdefMessages)
{
    printf("Starting test_nfc.\n");

    const hw_module_t *hwModule = 0;
    nfc_pn544_device_t *nfcDevice = 0;

    printf("Finding NFC hardware module.\n");
    hw_get_module(NFC_HARDWARE_MODULE_ID, &hwModule);
    assert(hwModule != NULL);

    printf("Opening NFC device.\n");
    assert(nfc_pn544_open(hwModule, &nfcDevice) == 0);
    assert(nfcDevice != 0);

    assert(nfcDevice->num_eeprom_settings != 0);
    assert(nfcDevice->eeprom_settings);

    printf("Configuring NFC driver.\n");
    phLibNfc_sConfig_t driverConfig;
    driverConfig.nClientId = phDal4Nfc_msgget(0, 0600);
    assert(driverConfig.nClientId);

    void *hwRef;
    NFCSTATUS status = phLibNfc_Mgt_ConfigureDriver(&driverConfig, &hwRef);
    assert(hwRef);
    assert(status == NFCSTATUS_SUCCESS);

    pthread_t messageThread = createMessageThread(&driverConfig.nClientId);

    printf("Initializing NFC stack.\n");
    NFCSTATUS callbackStatus = 0xFFFF;
    status = phLibNfc_Mgt_Initialize(hwRef, initializeCallback, &callbackStatus);
    assert(status == NFCSTATUS_PENDING);

    pthread_mutex_lock(&mut);
    while (callbackStatus == 0xFFFF)
        pthread_cond_wait(&cond, &mut);
    pthread_mutex_unlock(&mut);

    assert(callbackStatus == NFCSTATUS_SUCCESS);

    printf("Getting NFC stack capabilities.\n");
    phLibNfc_StackCapabilities_t capabilities;
    status = phLibNfc_Mgt_GetstackCapabilities(&capabilities, &callbackStatus);
    assert(status == NFCSTATUS_SUCCESS);

    printf("NFC capabilities:\n"
           "\tHAL version: %u\n"
           "\tFW version: %u\n"
           "\tHW version: %u\n"
           "\tModel: %u\n"
           "\tHCI version: %u\n"
           "\tVendor: %s\n"
           "\tFull version: %u %u\n"
           "\tFW Update: %u\n",
           capabilities.psDevCapabilities.hal_version, capabilities.psDevCapabilities.fw_version,
           capabilities.psDevCapabilities.hw_version, capabilities.psDevCapabilities.model_id,
           capabilities.psDevCapabilities.hci_version,
           capabilities.psDevCapabilities.vendor_name,
           capabilities.psDevCapabilities.full_version[NXP_FULL_VERSION_LEN-1],
           capabilities.psDevCapabilities.full_version[NXP_FULL_VERSION_LEN-2],
           capabilities.psDevCapabilities.firmware_update_info);

    if (readNdefMessages) {
        /* Start tag discovery */
        phLibNfc_Registry_Info_t registryInfo;

        registryInfo.MifareUL = 1;
        registryInfo.MifareStd = 1;
        registryInfo.ISO14443_4A = 1;
        registryInfo.ISO14443_4B = 1;
        registryInfo.Jewel = 1;
        registryInfo.Felica = 1;
        registryInfo.NFC = 1;
        registryInfo.ISO15693 = 1;

        int context;
        status = phLibNfc_RemoteDev_NtfRegister(&registryInfo, discoveryNotificationCallback, &context);
        assert(status == NFCSTATUS_SUCCESS);

        phLibNfc_sADD_Cfg_t discoveryConfig;
        discoveryConfig.NfcIP_Mode = phNfc_eP2P_ALL;
#if (ANDROID_VERSION_MAJOR == 4 && ANDROID_VERSION_MINOR >= 1) || (ANDROID_VERSION_MAJOR >= 5)
        discoveryConfig.NfcIP_Target_Mode = 0x0E;
#endif
        discoveryConfig.Duration = 300000;
        discoveryConfig.NfcIP_Tgt_Disable = 0;
        discoveryConfig.PollDevInfo.PollCfgInfo.EnableIso14443A = 1;
        discoveryConfig.PollDevInfo.PollCfgInfo.EnableIso14443B = 1;
        discoveryConfig.PollDevInfo.PollCfgInfo.EnableFelica212 = 1;
        discoveryConfig.PollDevInfo.PollCfgInfo.EnableFelica424 = 1;
        discoveryConfig.PollDevInfo.PollCfgInfo.EnableIso15693 = 1;
        discoveryConfig.PollDevInfo.PollCfgInfo.EnableNfcActive = 1;
        discoveryConfig.PollDevInfo.PollCfgInfo.DisableCardEmulation = 1;

        targetStatus = 0xFFFF;
        status = phLibNfc_Mgt_ConfigureDiscovery(NFC_DISCOVERY_CONFIG, discoveryConfig,
                                                 discoveryCallback, &context);

        for (;;) {
            pthread_mutex_lock(&mut);
            while (targetStatus == 0xFFFF)
                pthread_cond_wait(&cond, &mut);
            pthread_mutex_unlock(&mut);

            fprintf(stderr, "Discovered %d targets\n", numberOfDiscoveredTargets);
            if (numberOfDiscoveredTargets > 0) {
                targetStatus = 0xFFFF;
                status = phLibNfc_RemoteDev_Connect(discoveredTargets[0].hTargetDev,
                        remoteDevConnectCallback, &context);
                if (status == NFCSTATUS_PENDING) {
                    pthread_mutex_lock(&mut);
                    while (targetStatus == 0xFFFF)
                        pthread_cond_wait(&cond, &mut);
                    pthread_mutex_unlock(&mut);

                    if (targetStatus == NFCSTATUS_SUCCESS) {
                        targetStatus = 0xFFFF;
                        status = phLibNfc_Ndef_CheckNdef(discoveredTargets[0].hTargetDev,
                                remoteDevNdefReadCheckCallback, &context);

                        pthread_mutex_lock(&mut);
                        while (targetStatus == 0xFFFF)
                            pthread_cond_wait(&cond, &mut);
                        pthread_mutex_unlock(&mut);

                        if (targetStatus == NFCSTATUS_SUCCESS &&
                            (ndefInfo.NdefCardState == PHLIBNFC_NDEF_CARD_READ_WRITE ||
                             ndefInfo.NdefCardState == PHLIBNFC_NDEF_CARD_READ_ONLY)) {
                            phLibNfc_Data_t ndefBuffer;
                            ndefBuffer.length = ndefInfo.MaxNdefMsgLength;
                            ndefBuffer.buffer = malloc(ndefBuffer.length);

                            targetStatus = 0xFFFF;
                            status = phLibNfc_Ndef_Read(discoveredTargets[0].hTargetDev, &ndefBuffer,
                                    phLibNfc_Ndef_EBegin, remoteDevNdefReadCallback, &context);

                            pthread_mutex_lock(&mut);
                            while (targetStatus == 0xFFFF)
                                pthread_cond_wait(&cond, &mut);
                            pthread_mutex_unlock(&mut);

                            if (targetStatus == NFCSTATUS_SUCCESS) {
                                int i;
                                fprintf(stderr, "NDEF: ");
                                for (i = 0; i < ndefBuffer.length; ++i)
                                    fprintf(stderr, "%02x", ndefBuffer.buffer[i]);
                                fprintf(stderr, "\n");
                            }

                            free(ndefBuffer.buffer);
                        }
                    }
                }
            }

            if (status == NFCSTATUS_FAILED) {
                fprintf(stderr, "Failed to connect to remote device\n");
                break;
            }

            targetStatus = 0xFFFF;
            status = phLibNfc_Mgt_ConfigureDiscovery(NFC_DISCOVERY_RESUME, discoveryConfig,
                                                     discoveryCallback, &context);
            assert(status == NFCSTATUS_SUCCESS || status == NFCSTATUS_PENDING);
        }
    }

    printf("Deinitializing NFC stack.\n");
    callbackStatus = 0xFFFF;
    status = phLibNfc_Mgt_DeInitialize(hwRef, initializeCallback, &callbackStatus);
    assert(status == NFCSTATUS_PENDING);

    pthread_mutex_lock(&mut);
    while (callbackStatus == 0xFFFF)
        pthread_cond_wait(&cond, &mut);
    pthread_mutex_unlock(&mut);

    assert(callbackStatus == NFCSTATUS_SUCCESS);

    terminateMessageThread(driverConfig.nClientId);
    pthread_join(messageThread, NULL);

    printf("Unconfiguring NFC driver.\n");
    status = phLibNfc_Mgt_UnConfigureDriver(hwRef);
    assert(status == NFCSTATUS_SUCCESS);

    int result = phDal4Nfc_msgctl(driverConfig.nClientId, 0, 0);
    assert(result == 0);

    printf("Closing NFC device.\n");
    nfc_pn544_close(nfcDevice);
}
Beispiel #2
0
/*-----------------------------------------------------------------------------

FUNCTION: phDal4Nfc_Config

PURPOSE: Configure the serial port.

-----------------------------------------------------------------------------*/
NFCSTATUS phDal4Nfc_Config(pphDal4Nfc_sConfig_t config,void **phwref)
{
   NFCSTATUS                       retstatus = NFCSTATUS_SUCCESS;
   const hw_module_t* hw_module;
   nfc_pn544_device_t* pn544_dev;
   uint8_t num_eeprom_settings;
   uint8_t* eeprom_settings;
   int ret;

   /* Retrieve the hw module from the Android NFC HAL */
   ret = hw_get_module(NFC_HARDWARE_MODULE_ID, &hw_module);
   if (ret) {
       ALOGE("hw_get_module() failed");
       return NFCSTATUS_FAILED;
   }
   ret = nfc_pn544_open(hw_module, &pn544_dev);
   if (ret) {
       ALOGE("Could not open pn544 hw_module");
       return NFCSTATUS_FAILED;
   }
   config->deviceNode = pn544_dev->device_node;
   if (config->deviceNode == NULL) {
       ALOGE("deviceNode NULL");
       return NFCSTATUS_FAILED;
   }

   DAL_PRINT("phDal4Nfc_Config");

   if ((config == NULL) || (phwref == NULL))
      return NFCSTATUS_INVALID_PARAMETER;

   /* Register the link callbacks */
   memset(&gLinkFunc, 0, sizeof(phDal4Nfc_link_cbk_interface_t));
   switch(pn544_dev->linktype)
   {
      case PN544_LINK_TYPE_UART:
      case PN544_LINK_TYPE_USB:
      {
	 DAL_PRINT("UART link Config");
         /* Uart link interface */
         gLinkFunc.init               = phDal4Nfc_uart_initialize;
         gLinkFunc.open_from_handle   = phDal4Nfc_uart_set_open_from_handle;
         gLinkFunc.is_opened          = phDal4Nfc_uart_is_opened;
         gLinkFunc.flush              = phDal4Nfc_uart_flush;
         gLinkFunc.close              = phDal4Nfc_uart_close;
         gLinkFunc.open_and_configure = phDal4Nfc_uart_open_and_configure;
         gLinkFunc.read               = phDal4Nfc_uart_read;
         gLinkFunc.write              = phDal4Nfc_uart_write;
         gLinkFunc.reset              = phDal4Nfc_uart_reset;
      }
      break;

      case PN544_LINK_TYPE_I2C:
      {
	 DAL_PRINT("I2C link Config");
         /* i2c link interface */
         gLinkFunc.init               = phDal4Nfc_i2c_initialize;
         gLinkFunc.open_from_handle   = phDal4Nfc_i2c_set_open_from_handle;
         gLinkFunc.is_opened          = phDal4Nfc_i2c_is_opened;
         gLinkFunc.flush              = phDal4Nfc_i2c_flush;
         gLinkFunc.close              = phDal4Nfc_i2c_close;
         gLinkFunc.open_and_configure = phDal4Nfc_i2c_open_and_configure;
         gLinkFunc.read               = phDal4Nfc_i2c_read;
         gLinkFunc.write              = phDal4Nfc_i2c_write;
         gLinkFunc.reset              = phDal4Nfc_i2c_reset;
         break;
      }

      default:
      {
         /* Shound not happen : Bad parameter */
         return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_PARAMETER);
      }
   }

   gLinkFunc.init(); /* So that link interface can initialize its internal state */
   retstatus = gLinkFunc.open_and_configure(config, phwref);
   if (retstatus != NFCSTATUS_SUCCESS)
      return retstatus;

   /* Iniatilize the DAL context */
   (void)memset(&gDalContext,0,sizeof(phDal4Nfc_SContext_t));
   pgDalContext = &gDalContext;
   
   /* Reset the Reader Thread values to NULL */
   memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext));
   gReadWriteContext.nReadThreadAlive     = TRUE;
   gReadWriteContext.nWriteBusy = FALSE;
   gReadWriteContext.nWaitingOnWrite = FALSE;
   
   /* Prepare the message queue for the defered calls */
#ifdef USE_MQ_MESSAGE_QUEUE
   nDeferedCallMessageQueueId = mq_open(MQ_NAME_IDENTIFIER, O_CREAT|O_RDWR, 0666, &MQ_QUEUE_ATTRIBUTES);
#else
   nDeferedCallMessageQueueId = config->nClientId;
#endif

   gDalContext.pDev = pn544_dev;

   /* Start Read and Write Threads */
   if(NFCSTATUS_SUCCESS != phDal4Nfc_StartThreads())
   {
      return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED);
   }

   gDalContext.hw_valid = TRUE;
   phDal4Nfc_Reset(1);
   phDal4Nfc_Reset(0);
   phDal4Nfc_Reset(1);

   return NFCSTATUS_SUCCESS;
}