浏览代码

usb: Cleanup

Peter Cai 1 年之前
父节点
当前提交
ec5e5d0416

+ 3 - 3
app-common/src/main/java/im/angry/openeuicc/core/usb/UsbApduInterface.kt

@@ -129,7 +129,7 @@ class UsbApduInterface(
         // OR the channel mask into the CLA byte
         // OR the channel mask into the CLA byte
         realTx[0] = ((realTx[0].toInt() and 0xFC) or channel.toInt()).toByte()
         realTx[0] = ((realTx[0].toInt() and 0xFC) or channel.toInt()).toByte()
 
 
-        var resp = transceiver.sendXfrBlock(realTx)!!.data!!
+        var resp = transceiver.sendXfrBlock(realTx).data!!
 
 
         if (resp.size >= 2) {
         if (resp.size >= 2) {
             var sw1 = resp[resp.size - 2].toInt() and 0xFF
             var sw1 = resp[resp.size - 2].toInt() and 0xFF
@@ -137,14 +137,14 @@ class UsbApduInterface(
 
 
             if (sw1 == 0x6C) {
             if (sw1 == 0x6C) {
                 realTx[realTx.size - 1] = resp[resp.size - 1]
                 realTx[realTx.size - 1] = resp[resp.size - 1]
-                resp = transceiver.sendXfrBlock(realTx)!!.data!!
+                resp = transceiver.sendXfrBlock(realTx).data!!
             } else if (sw1 == 0x61) {
             } else if (sw1 == 0x61) {
                 do {
                 do {
                     val getResponseCmd = byteArrayOf(
                     val getResponseCmd = byteArrayOf(
                         realTx[0], 0xC0.toByte(), 0x00, 0x00, sw2.toByte()
                         realTx[0], 0xC0.toByte(), 0x00, 0x00, sw2.toByte()
                     )
                     )
 
 
-                    val tmp = transceiver.sendXfrBlock(getResponseCmd)!!.data!!
+                    val tmp = transceiver.sendXfrBlock(getResponseCmd).data!!
 
 
                     resp = resp.sliceArray(0 until (resp.size - 2)) + tmp
                     resp = resp.sliceArray(0 until (resp.size - 2)) + tmp
 
 

+ 1 - 0
app-common/src/main/java/im/angry/openeuicc/core/usb/UsbCcidDescription.kt

@@ -3,6 +3,7 @@ package im.angry.openeuicc.core.usb
 import java.nio.ByteBuffer
 import java.nio.ByteBuffer
 import java.nio.ByteOrder
 import java.nio.ByteOrder
 
 
+@Suppress("unused")
 data class UsbCcidDescription(
 data class UsbCcidDescription(
     private val bMaxSlotIndex: Byte,
     private val bMaxSlotIndex: Byte,
     private val bVoltageSupport: Byte,
     private val bVoltageSupport: Byte,

+ 10 - 16
app-common/src/main/java/im/angry/openeuicc/core/usb/UsbCcidTransceiver.kt

@@ -13,6 +13,7 @@ import java.nio.ByteOrder
  * Provides raw, APDU-agnostic transmission to the CCID reader
  * Provides raw, APDU-agnostic transmission to the CCID reader
  * Adapted from <https://github.com/open-keychain/open-keychain/blob/master/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/securitytoken/usb/CcidTransceiver.java>
  * Adapted from <https://github.com/open-keychain/open-keychain/blob/master/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/securitytoken/usb/CcidTransceiver.java>
  */
  */
+@Suppress("unused")
 class UsbCcidTransceiver(
 class UsbCcidTransceiver(
     private val usbConnection: UsbDeviceConnection,
     private val usbConnection: UsbDeviceConnection,
     private val usbBulkIn: UsbEndpoint,
     private val usbBulkIn: UsbEndpoint,
@@ -152,7 +153,7 @@ class UsbCcidTransceiver(
         }
         }
     }
     }
 
 
-    private fun receiveDataBlock(expectedSequenceNumber: Byte): CcidDataBlock? {
+    private fun receiveDataBlock(expectedSequenceNumber: Byte): CcidDataBlock {
         var response: CcidDataBlock?
         var response: CcidDataBlock?
         do {
         do {
             response = receiveDataBlockImmediate(expectedSequenceNumber)
             response = receiveDataBlockImmediate(expectedSequenceNumber)
@@ -163,7 +164,7 @@ class UsbCcidTransceiver(
         return response
         return response
     }
     }
 
 
-    private fun receiveDataBlockImmediate(expectedSequenceNumber: Byte): CcidDataBlock? {
+    private fun receiveDataBlockImmediate(expectedSequenceNumber: Byte): CcidDataBlock {
         /*
         /*
          * Some USB CCID devices (notably NitroKey 3) may time-out and need a subsequent poke to
          * Some USB CCID devices (notably NitroKey 3) may time-out and need a subsequent poke to
          * carry on communications.  No particular reason why the number 3 was chosen.  If we get a
          * carry on communications.  No particular reason why the number 3 was chosen.  If we get a
@@ -235,21 +236,11 @@ class UsbCcidTransceiver(
         } while (ignoredBytes > 0)
         } while (ignoredBytes > 0)
     }
     }
 
 
-    /**
-     * Transmits XfrBlock
-     * 6.1.4 PC_to_RDR_XfrBlock
-     *
-     * @param payload payload to transmit
-     */
-    fun sendXfrBlock(payload: ByteArray): CcidDataBlock? {
-        return sendXfrBlock(payload, LEVEL_PARAM_START_SINGLE_CMD_APDU)
-    }
-
     /**
     /**
      * Receives a continued XfrBlock. Should be called when a multiblock response is indicated
      * Receives a continued XfrBlock. Should be called when a multiblock response is indicated
      * 6.1.4 PC_to_RDR_XfrBlock
      * 6.1.4 PC_to_RDR_XfrBlock
      */
      */
-    fun receiveContinuedResponse(): CcidDataBlock? {
+    fun receiveContinuedResponse(): CcidDataBlock {
         return sendXfrBlock(ByteArray(0), LEVEL_PARAM_CONTINUE_RESPONSE)
         return sendXfrBlock(ByteArray(0), LEVEL_PARAM_CONTINUE_RESPONSE)
     }
     }
 
 
@@ -260,7 +251,10 @@ class UsbCcidTransceiver(
      * @param payload payload to transmit
      * @param payload payload to transmit
      * @param levelParam Level parameter
      * @param levelParam Level parameter
      */
      */
-    private fun sendXfrBlock(payload: ByteArray, levelParam: Short): CcidDataBlock? {
+    fun sendXfrBlock(
+        payload: ByteArray,
+        levelParam: Short = LEVEL_PARAM_START_SINGLE_CMD_APDU
+    ): CcidDataBlock {
         val startTime = SystemClock.elapsedRealtime()
         val startTime = SystemClock.elapsedRealtime()
         val l = payload.size
         val l = payload.size
         val sequenceNumber: Byte = currentSequenceNumber++
         val sequenceNumber: Byte = currentSequenceNumber++
@@ -279,7 +273,7 @@ class UsbCcidTransceiver(
         val data: ByteArray = headerData + payload
         val data: ByteArray = headerData + payload
         var sentBytes = 0
         var sentBytes = 0
         while (sentBytes < data.size) {
         while (sentBytes < data.size) {
-            val bytesToSend = Math.min(usbBulkOut.maxPacketSize, data.size - sentBytes)
+            val bytesToSend = usbBulkOut.maxPacketSize.coerceAtMost(data.size - sentBytes)
             sendRaw(data, sentBytes, bytesToSend)
             sendRaw(data, sentBytes, bytesToSend)
             sentBytes += bytesToSend
             sentBytes += bytesToSend
         }
         }
@@ -320,7 +314,7 @@ class UsbCcidTransceiver(
         return response
         return response
     }
     }
 
 
-    private fun iccPowerOnVoltage(voltage: Byte): CcidDataBlock? {
+    private fun iccPowerOnVoltage(voltage: Byte): CcidDataBlock {
         val sequenceNumber = currentSequenceNumber++
         val sequenceNumber = currentSequenceNumber++
         val iccPowerCommand = byteArrayOf(
         val iccPowerCommand = byteArrayOf(
             MESSAGE_TYPE_PC_TO_RDR_ICC_POWER_ON.toByte(),
             MESSAGE_TYPE_PC_TO_RDR_ICC_POWER_ON.toByte(),