From ff07dc2bb156d62f447bc3613d6df339fa09794c Mon Sep 17 00:00:00 2001 From: Martijn Coenen Date: Mon, 2 Apr 2012 13:20:33 -0700 Subject: Add support for making Mifare Classic readonly. Change-Id: I7572274dd74d8f9c3f0c80bdccf3d30707c91c3f --- jni/com_android_nfc_NativeNfcTag.cpp | 12 ++++++++---- src/com/android/nfc/nxp/NativeNfcManager.java | 3 ++- src/com/android/nfc/nxp/NativeNfcTag.java | 11 +++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/jni/com_android_nfc_NativeNfcTag.cpp b/jni/com_android_nfc_NativeNfcTag.cpp index f35dbf9..dbf8dc9 100644 --- a/jni/com_android_nfc_NativeNfcTag.cpp +++ b/jni/com_android_nfc_NativeNfcTag.cpp @@ -1162,12 +1162,13 @@ clean_and_return: return result; } -static jboolean com_android_nfc_NativeNfcTag_doMakeReadonly(JNIEnv *e, jobject o) +static jboolean com_android_nfc_NativeNfcTag_doMakeReadonly(JNIEnv *e, jobject o, jbyteArray key) { phLibNfc_Handle handle = 0; NFCSTATUS status; jboolean result = JNI_FALSE; struct nfc_jni_callback_data cb_data; + phNfc_sData_t keyBuffer; CONCURRENCY_LOCK(); @@ -1178,10 +1179,12 @@ static jboolean com_android_nfc_NativeNfcTag_doMakeReadonly(JNIEnv *e, jobject o } handle = nfc_jni_get_connected_handle(e, o); - + keyBuffer.buffer = (uint8_t *)e->GetByteArrayElements(key, NULL); + keyBuffer.length = e->GetArrayLength(key); TRACE("phLibNfc_ConvertToReadOnlyNdef()"); REENTRANCE_LOCK(); - status = phLibNfc_ConvertToReadOnlyNdef(handle, nfc_jni_readonly_callback, (void *)&cb_data); + status = phLibNfc_ConvertToReadOnlyNdef(handle, &keyBuffer, nfc_jni_readonly_callback, + (void *)&cb_data); REENTRANCE_UNLOCK(); if(status != NFCSTATUS_PENDING) @@ -1204,6 +1207,7 @@ static jboolean com_android_nfc_NativeNfcTag_doMakeReadonly(JNIEnv *e, jobject o } clean_and_return: + e->ReleaseByteArrayElements(key, (jbyte *)keyBuffer.buffer, JNI_ABORT); nfc_cb_data_deinit(&cb_data); CONCURRENCY_UNLOCK(); return result; @@ -1237,7 +1241,7 @@ static JNINativeMethod gMethods[] = (void *)com_android_nfc_NativeNfcTag_doIsIsoDepNdefFormatable}, {"doNdefFormat", "([B)Z", (void *)com_android_nfc_NativeNfcTag_doNdefFormat}, - {"doMakeReadonly", "()Z", + {"doMakeReadonly", "([B)Z", (void *)com_android_nfc_NativeNfcTag_doMakeReadonly}, }; diff --git a/src/com/android/nfc/nxp/NativeNfcManager.java b/src/com/android/nfc/nxp/NativeNfcManager.java index 9a62cc0..1a50805 100755 --- a/src/com/android/nfc/nxp/NativeNfcManager.java +++ b/src/com/android/nfc/nxp/NativeNfcManager.java @@ -238,7 +238,8 @@ public class NativeNfcManager implements DeviceHost { @Override public boolean canMakeReadOnly(int ndefType) { - return (ndefType == Ndef.TYPE_1 || ndefType == Ndef.TYPE_2); + return (ndefType == Ndef.TYPE_1 || ndefType == Ndef.TYPE_2 || + ndefType == Ndef.TYPE_MIFARE_CLASSIC); } @Override diff --git a/src/com/android/nfc/nxp/NativeNfcTag.java b/src/com/android/nfc/nxp/NativeNfcTag.java index 53e1312..8996dfb 100755 --- a/src/com/android/nfc/nxp/NativeNfcTag.java +++ b/src/com/android/nfc/nxp/NativeNfcTag.java @@ -21,6 +21,7 @@ import com.android.nfc.DeviceHost.TagEndpoint; import android.nfc.FormatException; import android.nfc.NdefMessage; import android.nfc.tech.IsoDep; +import android.nfc.tech.MifareClassic; import android.nfc.tech.MifareUltralight; import android.nfc.tech.Ndef; import android.nfc.tech.NfcA; @@ -350,13 +351,19 @@ public class NativeNfcTag implements TagEndpoint { return result; } - native boolean doMakeReadonly(); + native boolean doMakeReadonly(byte[] key); @Override public synchronized boolean makeReadOnly() { if (mWatchdog != null) { mWatchdog.pause(); } - boolean result = doMakeReadonly(); + boolean result; + if (hasTech(TagTechnology.MIFARE_CLASSIC)) { + result = doMakeReadonly(MifareClassic.KEY_DEFAULT); + } else { + // No key needed for other technologies + result = doMakeReadonly(new byte[] {}); + } if (mWatchdog != null) { mWatchdog.doResume(); } -- cgit v1.1