aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
diff options
context:
space:
mode:
authorNikita Mikhailov <nikita.s.mikhailov@gmail.com>2016-04-09 13:13:02 +0600
committerNikita Mikhailov <nikita.s.mikhailov@gmail.com>2016-04-14 22:48:01 +0600
commit4d9ce8e95b4604f753aa5f49fe2c243dc73a13a9 (patch)
tree7ae338e2452f3d624e01a9b345fae44dca53d958 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
parentb5eb6468fecfc16ea041eb0f4bf48c37ec2e81f2 (diff)
downloadopen-keychain-4d9ce8e95b4604f753aa5f49fe2c243dc73a13a9.tar.gz
open-keychain-4d9ce8e95b4604f753aa5f49fe2c243dc73a13a9.tar.bz2
open-keychain-4d9ce8e95b4604f753aa5f49fe2c243dc73a13a9.zip
OTG: Refactor persistent connections, naming
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java61
1 files changed, 40 insertions, 21 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
index 2d435ccbe..2a21c10dd 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
@@ -15,6 +15,7 @@ import org.bouncycastle.util.encoders.Hex;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.util.Log;
+import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -24,30 +25,15 @@ public class UsbTransport implements Transport {
private final UsbManager mUsbManager;
private final UsbDevice mUsbDevice;
- private final UsbInterface mUsbInterface;
- private final UsbEndpoint mBulkIn;
- private final UsbEndpoint mBulkOut;
- private final UsbDeviceConnection mConnection;
+ private UsbInterface mUsbInterface;
+ private UsbEndpoint mBulkIn;
+ private UsbEndpoint mBulkOut;
+ private UsbDeviceConnection mConnection;
private byte mCounter = 0;
- public UsbTransport(final UsbDevice usbDevice, final UsbManager usbManager) throws TransportIoException {
+ public UsbTransport(final UsbDevice usbDevice, final UsbManager usbManager) {
mUsbDevice = usbDevice;
mUsbManager = usbManager;
-
- mUsbInterface = getSmartCardInterface(mUsbDevice);
- // throw if mUsbInterface == null
- final Pair<UsbEndpoint, UsbEndpoint> ioEndpoints = getIoEndpoints(mUsbInterface);
- mBulkIn = ioEndpoints.first;
- mBulkOut = ioEndpoints.second;
- // throw if any endpoint is null
-
- mConnection = mUsbManager.openDevice(mUsbDevice);
- // throw if connection is null
- mConnection.claimInterface(mUsbInterface, true);
- // check result
-
- powerOn();
- Log.d(Constants.TAG, "Usb transport connected");
}
private void powerOff() throws TransportIoException {
@@ -120,7 +106,7 @@ public class UsbTransport implements Transport {
@Override
public boolean isConnected() {
// TODO: redo
- return mUsbManager.getDeviceList().containsValue(mUsbDevice);
+ return mConnection != null && mUsbManager.getDeviceList().containsValue(mUsbDevice);
}
@Override
@@ -129,6 +115,24 @@ public class UsbTransport implements Transport {
}
@Override
+ public void connect() throws IOException {
+ mUsbInterface = getSmartCardInterface(mUsbDevice);
+ // throw if mUsbInterface == null
+ final Pair<UsbEndpoint, UsbEndpoint> ioEndpoints = getIoEndpoints(mUsbInterface);
+ mBulkIn = ioEndpoints.first;
+ mBulkOut = ioEndpoints.second;
+ // throw if any endpoint is null
+
+ mConnection = mUsbManager.openDevice(mUsbDevice);
+ // throw if connection is null
+ mConnection.claimInterface(mUsbInterface, true);
+ // check result
+
+ powerOn();
+ Log.d(Constants.TAG, "Usb transport connected");
+ }
+
+ @Override
public byte[] sendAndReceive(byte[] data) throws TransportIoException {
send(data);
byte[] bytes;
@@ -208,4 +212,19 @@ public class UsbTransport implements Transport {
public UsbDevice getUsbDevice() {
return mUsbDevice;
}
+
+ @Override
+ public boolean equals(final Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ final UsbTransport that = (UsbTransport) o;
+
+ return mUsbDevice != null ? mUsbDevice.equals(that.mUsbDevice) : that.mUsbDevice == null;
+ }
+
+ @Override
+ public int hashCode() {
+ return mUsbDevice != null ? mUsbDevice.hashCode() : 0;
+ }
}