static jbyteArray com_android_nfc_NativeNdefTag_doRead(JNIEnv *e, jobject o) { phLibNfc_Handle handle = 0; jbyteArray buf = NULL; NFCSTATUS status; CONCURRENCY_LOCK(); handle = nfc_jni_get_nfc_tag_handle(e, o); nfc_jni_ndef_rw.length = nfc_jni_ndef_buf_len; nfc_jni_ndef_rw.buffer = nfc_jni_ndef_buf; TRACE("phLibNfc_Ndef_Read()"); REENTRANCE_LOCK(); status = phLibNfc_Ndef_Read( handle, &nfc_jni_ndef_rw, phLibNfc_Ndef_EBegin, nfc_jni_tag_rw_callback, (void *)e); REENTRANCE_UNLOCK(); if(status != NFCSTATUS_PENDING) { LOGE("phLibNfc_Ndef_Read() returned 0x%04x[%s]", status, nfc_jni_get_status_name(status)); goto clean_and_return; } TRACE("phLibNfc_Ndef_Read() returned 0x%04x[%s]", status, nfc_jni_get_status_name(status)); /* Wait for callback response */ sem_wait(nfc_jni_ndef_tag_sem); if(nfc_jni_cb_status != NFCSTATUS_SUCCESS) { goto clean_and_return; } buf = e->NewByteArray(nfc_jni_ndef_rw.length); e->SetByteArrayRegion(buf, 0, nfc_jni_ndef_rw.length, (jbyte *)nfc_jni_ndef_rw.buffer); clean_and_return: CONCURRENCY_UNLOCK(); return buf; }
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); }