NFCSTATUS phDal4Nfc_ConfigRelease(void *pHwRef) { NFCSTATUS result = NFCSTATUS_SUCCESS; void * pThreadReturn; DAL_PRINT("phDal4Nfc_ConfigRelease "); if (gDalContext.hw_valid == TRUE) { /* Signal the read and write threads to exit. NOTE: there actually is no write thread! :) */ DAL_PRINT("Stop Reader Thread"); gReadWriteContext.nReadThreadAlive = 0; gReadWriteContext.nWriteThreadAlive = 0; /* Wake up the read thread so it can exit */ DAL_PRINT("Release Read Semaphore"); sem_post(&nfc_read_sem); DAL_DEBUG("phDal4Nfc_ConfigRelease - doing pthread_join(%d)", gReadWriteContext.nReadThread); if (pthread_join(gReadWriteContext.nReadThread, &pThreadReturn) != 0) { result = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED); DAL_PRINT("phDal4Nfc_ConfigRelease KO"); } /* Close the message queue */ #ifdef USE_MQ_MESSAGE_QUEUE mq_close(nDeferedCallMessageQueueId); #endif /* Shutdown NFC Chip */ phDal4Nfc_Reset(0); /* Close the link */ gLinkFunc.close(); if (gDalContext.pDev != NULL) { nfc_pn544_close(gDalContext.pDev); } /* Reset the Read Writer context to NULL */ memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext)); /* Reset the DAL context values to NULL */ memset((void *)&gDalContext,0,sizeof(gDalContext)); } gDalContext.hw_valid = FALSE; DAL_DEBUG("phDal4Nfc_ConfigRelease(): %04x\n", result); return result; }
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(®istryInfo, 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); }