From b3eb73b8a04abf714952482de85b23c85f840bf4 Mon Sep 17 00:00:00 2001 From: jdkbx Date: Fri, 9 Apr 2010 21:03:50 +0200 Subject: [PATCH] better logging --- src/nfc/BluetoothNFCReader.java | 72 ++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 16 deletions(-) mode change 100644 => 100755 src/nfc/BluetoothNFCReader.java diff --git a/src/nfc/BluetoothNFCReader.java b/src/nfc/BluetoothNFCReader.java old mode 100644 new mode 100755 index 79e6b65..115b84e --- a/src/nfc/BluetoothNFCReader.java +++ b/src/nfc/BluetoothNFCReader.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2008 jdkbx nfc@jdkbx.cjb.net + * Copyright (C) 2008 - 2010 jdkbx nfc@jdkbx.cjb.net * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,7 +38,6 @@ import javax.microedition.contactless.TargetListener; import javax.microedition.contactless.TargetProperties; import javax.microedition.contactless.TargetType; import javax.microedition.contactless.sc.ISO14443Connection; -import javax.microedition.io.Connection; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.io.StreamConnectionNotifier; @@ -106,6 +105,8 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, private Command cmdRemove; /** Show infos. */ private Command cmdAbout; + /** Show infos. */ + private Command cmdExtSend; /** uid from RFID card. */ private String uid; @@ -159,6 +160,9 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, /** whether the driver is waiting for an card detected event. */ private boolean isWaitingEvent = false; + /** Whether logging is enabled. */ + private boolean LOG = true; + /** holds the reader functionality. */ private Reader r = new Reader(); @@ -202,6 +206,7 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, cmdBack = new Command("Back", Command.BACK, 1); cmdRemove = new Command("Remove", Command.ITEM, 1); cmdAbout = new Command("About", Command.ITEM, 1); + cmdExtSend = new Command("send ext APDU", Command.ITEM, 1); } /** @@ -306,8 +311,11 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, formReader.append(tfReadIn); formReader.append(tfWriteOut); formReader.addCommand(cmdCancel); - formReader.addCommand(cmdLog); + if (LOG) + formReader.addCommand(cmdLog); formReader.addCommand(cmdRemove); + if (LOG) + formReader.addCommand(cmdExtSend); formReader.setCommandListener(this); } @@ -439,8 +447,24 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, resetFormError(); resetFormReader(); resetFormConnect(); + resetFormLog(); display.setCurrent(formConnect); - } + } else if (cmd == cmdExtSend) { + try { + byte[] resp = isoConn.exchangeData(new byte[] {0x0,(byte)0xb0, (byte)0x9c,0x0,0x0,0x0,(byte)0xf0}); + if (resp != null && resp.length > 0) { + formLog.append("recieved RAPDU with length " + resp.length + "\n"); + } else { + formLog.append("failed to get extended RAPDU\n"); + } + } catch (ContactlessException e) { + formLog.append(e.getMessage() + "\n"); + formLog.append("ContactlessException while sending 00b09c000000f0\n"); + } catch (IOException e) { + formLog.append(e.getMessage() + "\n"); + formLog.append("IOException while sending 00b09c000000f0\n"); + } + } } else if (displayForm == formLog) { if (cmd == cmdBack) { display.setCurrent(formReader); @@ -960,10 +984,10 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, readLen += len - 2; } - if (readLen != realLen) + if (LOG && readLen != realLen) formLog.append("error len in apdu:" + realLen + " recv:" + readLen + "\n"); - if (readLen > 255) + if (LOG && readLen > 255) formLog.append("extended APDU len: " + readLen + "\n"); } @@ -975,11 +999,17 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, // } // write messages to screen - if (len > 3) { // && len != READER_CMD_LENGTH) { + if (len > 2) { // && len != READER_CMD_LENGTH) { tfReadIn.setString(">> " + len + "\n" + toHexString(buffer, 2, 8)); tfWriteOut.setString(" \n "); } + + if (LOG) { + // log APDU + tfIn.setString(toHexString(buffer, 2, len > 255 ? 255 : len)); + tfOut.setString(" \n "); + } // handle cmds if (len == READER_CMD_LENGTH) { @@ -1064,16 +1094,15 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, //} else { //formLog.append("doing real shit\n"); } - - // log APDU - tfIn.setString(toHexString(buffer, 1, len > 255 ? len : 255)); - + if (isCardPresent) { + cardResponse = null; // relay APDU to card cardResponse = sendAPDU(buffer, offset, len); if (cardResponse != null && cardResponse.length > 0) { + response = null; if (USE_L2CAP == true) { if (USE_FRAGMENTATION == true) { if (cardResponse.length <= 48 - 2) { @@ -1144,12 +1173,19 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, tfWriteOut.setString("<< " + cardResponse.length + "\n" + toHexString(cardResponse, cardResponse.length - 2, 2)); - tfOut.setString(toHexString( - cardResponse, 0, cardResponse.length > 255 ? 255: cardResponse.length)); } else { tfWriteOut.setString("<< " + cardResponse.length + "\n" + toHexString(cardResponse, 0, 1)); } + if (LOG) { + if (response.length > 0) { + tfOut.setString(toHexString( + response, 0, response.length > 255 ? 255: response.length)); + } else { + tfOut.setString(toHexString( + response, 0, response.length)); + } + } } else { if (USE_L2CAP == true) { connectionL2CAP.send(NULL_BYTES); @@ -1230,14 +1266,18 @@ public class BluetoothNFCReader extends MIDlet implements TargetListener, e.printStackTrace(); formError.append(e.getMessage()); formError.append("!!NFC IO error\n"); - formLog.append(e.getMessage() + "\n"); - formLog.append("!!NFC IO error\n"); + if (LOG) { + formLog.append(e.getMessage() + "\n"); + formLog.append("!!NFC IO error\n"); + } } catch (ContactlessException e) { e.printStackTrace(); formError.append(e.getMessage()); formError.append("!!NFC contactless error\nrestart!\n"); - formLog.append("!!NFC contactless error\n"); + if (LOG) { + formLog.append("!!NFC contactless error\n"); + } disconnectCard(formError, true); display.setCurrent(formError); r.kill(); -- 2.11.4.GIT