Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f54ad8da authored by Nick Pelly's avatar Nick Pelly
Browse files

Clean up transceive().

BasicTagTechnology.transceive(byte[] raw) should work for most child classes,
except those that want to disable (raw) transceive.

Current plan is not to add transceiveMifare() etc - use explicit methods
instead.

Add package scoped BasicTagTechnology.transceive(byte[] rata, boolean raw)
as a helper to remove code duplication.

Change-Id: Iaea161022751c99058d291e2ed0f8c475d1c7282
parent 06d19ef0
Loading
Loading
Loading
Loading
+17 −12
Original line number Diff line number Diff line
@@ -226,6 +226,22 @@ import android.util.Log;
        }
    }

    /** internal transceive */
    /*package*/ byte[] transceive(byte[] data, boolean raw) throws IOException {
        checkConnected();

        try {
            byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, raw);
            if (response == null) {
                throw new IOException("transceive failed");
            }
            return response;
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            throw new IOException("NFC service died");
        }
    }

    /**
     * Send data to a tag and receive the response.
     * <p>
@@ -238,17 +254,6 @@ import android.util.Log;
     * @throws IOException if the target is lost or connection closed
     */
    public byte[] transceive(byte[] data) throws IOException {
        checkConnected();

        try {
            byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, true);
            if (response == null) {
                throw new IOException("transceive failed");
            }
            return response;
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            throw new IOException("NFC service died");
        }
        return transceive(data, true);
    }
}
+7 −34
Original line number Diff line number Diff line
@@ -263,7 +263,7 @@ public final class MifareClassic extends BasicTagTechnology {
        System.arraycopy(key, 0, cmd, 6, 6);

        try {
            if ((transceive(cmd) != null)) {
            if ((transceive(cmd, false) != null)) {
                return true;
            }
        } catch (IOException e) {
@@ -308,7 +308,7 @@ public final class MifareClassic extends BasicTagTechnology {
        byte addr = (byte) block;
        byte[] blockread_cmd = { 0x30, addr };

        return transceive(blockread_cmd);
        return transceive(blockread_cmd, false);
    }

    /**
@@ -324,7 +324,7 @@ public final class MifareClassic extends BasicTagTechnology {
        blockwrite_cmd[1] = addr;
        System.arraycopy(data, 0, blockwrite_cmd, 2, data.length);

        transceive(blockwrite_cmd);
        transceive(blockwrite_cmd, false);
    }

    /**
@@ -345,7 +345,7 @@ public final class MifareClassic extends BasicTagTechnology {
        byte addr = (byte) block;
        byte[] incr_cmd = { (byte) 0xC1, (byte) block };

        transceive(incr_cmd);
        transceive(incr_cmd, false);
    }

    public void decrement(int block) throws IOException {
@@ -354,7 +354,7 @@ public final class MifareClassic extends BasicTagTechnology {
        byte addr = (byte) block;
        byte[] decr_cmd = { (byte) 0xC0, (byte) block };

        transceive(decr_cmd);
        transceive(decr_cmd, false);
    }

    public void transfer(int block) throws IOException {
@@ -363,7 +363,7 @@ public final class MifareClassic extends BasicTagTechnology {
        byte addr = (byte) block;
        byte[] trans_cmd = { (byte) 0xB0, (byte) block };

        transceive(trans_cmd);
        transceive(trans_cmd, false);
    }

    public void restore(int block) throws IOException {
@@ -372,33 +372,6 @@ public final class MifareClassic extends BasicTagTechnology {
        byte addr = (byte) block;
        byte[] rest_cmd = { (byte) 0xC2, (byte) block };

        transceive(rest_cmd);
    }

    /**
     * Send data to a tag and receive the response.
     * <p>
     * This method will block until the response is received. It can be canceled
     * with {@link #close}.
     * <p>Requires {@link android.Manifest.permission#NFC} permission.
     *
     * @param data bytes to send
     * @return bytes received in response
     * @throws IOException if the target is lost or connection closed
     */
    @Override
    public byte[] transceive(byte[] data) throws IOException {
        checkConnected();

        try {
            byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, false);
            if (response == null) {
                throw new IOException("transceive failed");
            }
            return response;
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            throw new IOException("NFC service died");
        }
        transceive(rest_cmd, false);
    }
}
+3 −31
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ public final class MifareUltralight extends BasicTagTechnology {
        checkConnected();

        byte[] blockread_cmd = { 0x30, (byte)block }; // phHal_eMifareRead
        return transceive(blockread_cmd);
        return transceive(blockread_cmd, false);
    }

    /**
@@ -89,7 +89,7 @@ public final class MifareUltralight extends BasicTagTechnology {
        pagewrite_cmd[1] = (byte) block;
        System.arraycopy(data, 0, pagewrite_cmd, 2, data.length);

        transceive(pagewrite_cmd);
        transceive(pagewrite_cmd, false);
    }

    public void writeBlock(int block, byte[] data) throws IOException {
@@ -100,34 +100,6 @@ public final class MifareUltralight extends BasicTagTechnology {
        blockwrite_cmd[1] = (byte) block;
        System.arraycopy(data, 0, blockwrite_cmd, 2, data.length);

        transceive(blockwrite_cmd);
        transceive(blockwrite_cmd, false);
    }

    /**
     * Send data to a tag and receive the response.
     * <p>
     * This method will block until the response is received. It can be canceled
     * with {@link #close}.
     * <p>Requires {@link android.Manifest.permission#NFC} permission.
     *
     * @param data bytes to send
     * @return bytes received in response
     * @throws IOException if the target is lost or connection closed
     */
    @Override
    public byte[] transceive(byte[] data) throws IOException {
        checkConnected();

        try {
            byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, false);
            if (response == null) {
                throw new IOException("transceive failed");
            }
            return response;
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            throw new IOException("NFC service died");
        }
    }

}