improve usb driver

This commit is contained in:
septs 2025-02-26 14:08:02 +08:00
parent fee4989ad6
commit 8d9ad5270d
Signed by: septs
SSH key fingerprint: SHA256:ElK0p6DNkbsqYUdJ3I9QHDVf21SQD0c2r+hd7s/r5Co
4 changed files with 17 additions and 14 deletions

View file

@ -8,7 +8,8 @@ import android.se.omapi.SEService
import android.util.Log
import im.angry.openeuicc.common.R
import im.angry.openeuicc.core.usb.UsbApduInterface
import im.angry.openeuicc.core.usb.getBulkEndpoints
import im.angry.openeuicc.core.usb.bulkPair
import im.angry.openeuicc.core.usb.endpoints
import im.angry.openeuicc.util.*
import java.lang.IllegalArgumentException
@ -61,7 +62,7 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
}
override fun tryOpenUsbEuiccChannel(usbDevice: UsbDevice, usbInterface: UsbInterface): EuiccChannel? {
val (bulkIn, bulkOut) = usbInterface.getBulkEndpoints()
val (bulkIn, bulkOut) = usbInterface.endpoints.bulkPair
if (bulkIn == null || bulkOut == null) return null
val conn = usbManager.openDevice(usbDevice) ?: return null
if (!conn.claimInterface(usbInterface, true)) return null

View file

@ -5,7 +5,8 @@ import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import android.telephony.SubscriptionManager
import android.util.Log
import im.angry.openeuicc.core.usb.getSmartCardInterface
import im.angry.openeuicc.core.usb.smartCard
import im.angry.openeuicc.core.usb.interfaces
import im.angry.openeuicc.di.AppContainer
import im.angry.openeuicc.util.*
import kotlinx.coroutines.Dispatchers
@ -244,7 +245,7 @@ open class DefaultEuiccChannelManager(
withContext(Dispatchers.IO) {
usbManager.deviceList.values.forEach { device ->
Log.i(TAG, "Scanning USB device ${device.deviceId}:${device.vendorId}")
val iface = device.getSmartCardInterface() ?: return@forEach
val iface = device.interfaces.smartCard ?: return@forEach
// If we don't have permission, tell UI code that we found a candidate device, but we
// need permission to be able to do anything with it
if (!usbManager.hasPermission(device)) return@withContext Pair(device, false)

View file

@ -95,6 +95,7 @@ class UsbCcidTransceiver(
data class UsbCcidErrorException(val msg: String, val errorResponse: CcidDataBlock) :
Exception(msg)
@Suppress("ArrayInDataClass")
data class CcidDataBlock(
val dwLength: Int,
val bSlot: Byte,

View file

@ -11,17 +11,17 @@ class UsbTransportException(message: String) : Exception(message)
val UsbDevice.interfaces: Iterable<UsbInterface>
get() = (0 until interfaceCount).map(::getInterface)
fun UsbDevice.getSmartCardInterface() =
interfaces.find { it.interfaceClass == UsbConstants.USB_CLASS_CSCID }
val Iterable<UsbInterface>.smartCard: UsbInterface?
get() = find { it.interfaceClass == UsbConstants.USB_CLASS_CSCID }
val UsbInterface.endpoints: Iterable<UsbEndpoint>
get() = (0 until endpointCount).map(::getEndpoint)
fun UsbInterface.getBulkEndpoints(): Pair<UsbEndpoint?, UsbEndpoint?> {
val endpoints = endpoints
.filter { it.type == UsbConstants.USB_ENDPOINT_XFER_BULK }
return Pair(
endpoints.find { it.direction == UsbConstants.USB_DIR_IN },
endpoints.find { it.direction == UsbConstants.USB_DIR_OUT },
)
}
val Iterable<UsbEndpoint>.bulkPair: Pair<UsbEndpoint?, UsbEndpoint?>
get() {
val endpoints = filter { it.type == UsbConstants.USB_ENDPOINT_XFER_BULK }
return Pair(
endpoints.find { it.direction == UsbConstants.USB_DIR_IN },
endpoints.find { it.direction == UsbConstants.USB_DIR_OUT },
)
}