Compare commits

..

No commits in common. "multi-se" and "master" have entirely different histories.

9 changed files with 44 additions and 67 deletions

View file

@ -20,8 +20,7 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
override suspend fun tryOpenEuiccChannel(
port: UiccPortInfoCompat,
isdrAid: ByteArray,
seId: Int,
isdrAid: ByteArray
): EuiccChannel? {
if (port.portIndex != 0) {
Log.w(
@ -47,7 +46,6 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
context.preferenceRepository.verboseLoggingFlow
),
isdrAid,
seId,
context.preferenceRepository.verboseLoggingFlow,
context.preferenceRepository.ignoreTLSCertificateFlow,
).also {
@ -67,8 +65,7 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
override fun tryOpenUsbEuiccChannel(
ccidCtx: UsbCcidContext,
isdrAid: ByteArray,
seId: Int
isdrAid: ByteArray
): EuiccChannel? {
try {
return EuiccChannelImpl(
@ -79,7 +76,6 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
ccidCtx
),
isdrAid,
seId,
context.preferenceRepository.verboseLoggingFlow,
context.preferenceRepository.ignoreTLSCertificateFlow,
)

View file

@ -32,7 +32,7 @@ open class DefaultEuiccChannelManager(
private val channelCache = mutableListOf<EuiccChannel>()
private var usbChannels = mutableListOf<EuiccChannel>()
private var usbChannel: EuiccChannel? = null
private val lock = Mutex()
@ -51,17 +51,15 @@ open class DefaultEuiccChannelManager(
protected open val uiccCards: Collection<UiccCardInfoCompat>
get() = (0..<tm.activeModemCountCompat).map { FakeUiccCardInfoCompat(it) }
private suspend inline fun tryOpenChannelWithKnownAids(openFn: (ByteArray, Int) -> EuiccChannel?): List<EuiccChannel> {
private suspend inline fun tryOpenChannelFirstValidAid(openFn: (ByteArray) -> EuiccChannel?): EuiccChannel? {
val isdrAidList =
parseIsdrAidList(appContainer.preferenceRepository.isdrAidListFlow.first())
var seId = 0
return isdrAidList.mapNotNull {
Log.i(TAG, "Opening channel, trying ISDR AID ${it.encodeHex()}, this will be seId ${seId}")
return isdrAidList.firstNotNullOfOrNull {
Log.i(TAG, "Opening channel, trying ISDR AID ${it.encodeHex()}")
openFn(it, seId)?.let { channel ->
openFn(it)?.let { channel ->
if (channel.valid) {
seId += 1
channel
} else {
channel.close()
@ -71,15 +69,19 @@ open class DefaultEuiccChannelManager(
}
}
private suspend fun tryOpenEuiccChannel(port: UiccPortInfoCompat, seId: Int = 0): EuiccChannel? {
private suspend fun tryOpenEuiccChannel(port: UiccPortInfoCompat): EuiccChannel? {
lock.withLock {
if (port.card.physicalSlotIndex == EuiccChannelManager.USB_CHANNEL_ID) {
// We only compare seId because we assume we can only open 1 card from USB
return usbChannels.find { it.seId == seId }
return if (usbChannel != null && usbChannel!!.valid) {
usbChannel
} else {
usbChannel = null
null
}
}
val existing =
channelCache.find { it.slotId == port.card.physicalSlotIndex && it.portId == port.portIndex && it.seId == seId }
channelCache.find { it.slotId == port.card.physicalSlotIndex && it.portId == port.portIndex }
if (existing != null) {
if (existing.valid && port.logicalSlotIndex == existing.logicalSlotId) {
return existing
@ -94,12 +96,12 @@ open class DefaultEuiccChannelManager(
return null
}
val channels =
tryOpenChannelWithKnownAids { isdrAid, seId -> euiccChannelFactory.tryOpenEuiccChannel(port, isdrAid, seId) }
val channel =
tryOpenChannelFirstValidAid { euiccChannelFactory.tryOpenEuiccChannel(port, it) }
if (channels.isNotEmpty()) {
channelCache.addAll(channels)
return channels.find { it.seId == seId }
if (channel != null) {
channelCache.add(channel)
return channel
} else {
Log.i(
TAG,
@ -110,10 +112,10 @@ open class DefaultEuiccChannelManager(
}
}
protected suspend fun findEuiccChannelByLogicalSlot(logicalSlotId: Int, seId: Int = 0): EuiccChannel? =
protected suspend fun findEuiccChannelByLogicalSlot(logicalSlotId: Int): EuiccChannel? =
withContext(Dispatchers.IO) {
if (logicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
return@withContext usbChannels.find { it.seId == seId }
return@withContext usbChannel
}
for (card in uiccCards) {
@ -129,7 +131,7 @@ open class DefaultEuiccChannelManager(
private suspend fun findAllEuiccChannelsByPhysicalSlot(physicalSlotId: Int): List<EuiccChannel>? {
if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
return usbChannels.ifEmpty { null }
return usbChannel?.let { listOf(it) }
}
for (card in uiccCards) {
@ -140,14 +142,14 @@ open class DefaultEuiccChannelManager(
return null
}
private suspend fun findEuiccChannelByPort(physicalSlotId: Int, portId: Int, seId: Int = 0): EuiccChannel? =
private suspend fun findEuiccChannelByPort(physicalSlotId: Int, portId: Int): EuiccChannel? =
withContext(Dispatchers.IO) {
if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
return@withContext usbChannels.find { it.seId == seId }
return@withContext usbChannel
}
uiccCards.find { it.physicalSlotIndex == physicalSlotId }?.let { card ->
card.ports.find { it.portIndex == portId }?.let { tryOpenEuiccChannel(it, seId) }
card.ports.find { it.portIndex == portId }?.let { tryOpenEuiccChannel(it) }
}
}
@ -166,16 +168,15 @@ open class DefaultEuiccChannelManager(
return@withContext listOf(0)
}
findAllEuiccChannelsByPhysicalSlot(physicalSlotId)?.map { it.portId }?.toSet()?.toList() ?: listOf()
findAllEuiccChannelsByPhysicalSlot(physicalSlotId)?.map { it.portId } ?: listOf()
}
override suspend fun <R> withEuiccChannel(
physicalSlotId: Int,
portId: Int,
seId: Int,
fn: suspend (EuiccChannel) -> R
): R {
val channel = findEuiccChannelByPort(physicalSlotId, portId, seId)
val channel = findEuiccChannelByPort(physicalSlotId, portId)
?: throw EuiccChannelManager.EuiccChannelNotFoundException()
val wrapper = EuiccChannelWrapper(channel)
try {
@ -189,10 +190,9 @@ open class DefaultEuiccChannelManager(
override suspend fun <R> withEuiccChannel(
logicalSlotId: Int,
seId: Int,
fn: suspend (EuiccChannel) -> R
): R {
val channel = findEuiccChannelByLogicalSlot(logicalSlotId, seId)
val channel = findEuiccChannelByLogicalSlot(logicalSlotId)
?: throw EuiccChannelManager.EuiccChannelNotFoundException()
val wrapper = EuiccChannelWrapper(channel)
try {
@ -206,8 +206,8 @@ open class DefaultEuiccChannelManager(
override suspend fun waitForReconnect(physicalSlotId: Int, portId: Int, timeoutMillis: Long) {
if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
usbChannels.forEach { it.close() }
usbChannels.clear()
usbChannel?.close()
usbChannel = null
} else {
// If there is already a valid channel, we close it proactively
// Sometimes the current channel can linger on for a bit even after it should have become invalid
@ -223,7 +223,7 @@ open class DefaultEuiccChannelManager(
// tryOpenUsbEuiccChannel() will always try to reopen the channel, even if
// a USB channel already exists
tryOpenUsbEuiccChannel()
usbChannels.getOrNull(0)!!
usbChannel!!
} else {
// tryOpenEuiccChannel() will automatically dispose of invalid channels
// and recreate when needed
@ -280,13 +280,12 @@ open class DefaultEuiccChannelManager(
val ccidCtx = UsbCcidContext.createFromUsbDevice(context, device, iface) ?: return@forEach
try {
val channels = tryOpenChannelWithKnownAids { isdrAid, seId ->
euiccChannelFactory.tryOpenUsbEuiccChannel(ccidCtx, isdrAid, seId)
val channel = tryOpenChannelFirstValidAid {
euiccChannelFactory.tryOpenUsbEuiccChannel(ccidCtx, it)
}
if (channels.isNotEmpty() && channels[0].valid) {
if (channel != null && channel.lpa.valid) {
ccidCtx.allowDisconnect = true
usbChannels.clear()
usbChannels.addAll(channels)
usbChannel = channel
return@withContext Pair(device, true)
}
} catch (e: Exception) {
@ -310,8 +309,8 @@ open class DefaultEuiccChannelManager(
channel.close()
}
usbChannels.forEach { it.close() }
usbChannels.clear()
usbChannel?.close()
usbChannel = null
channelCache.clear()
euiccChannelFactory.cleanup()
}

View file

@ -13,16 +13,6 @@ interface EuiccChannel {
val logicalSlotId: Int
val portId: Int
/**
* Some chips support multiple SEs on one chip. The seId here is intended
* to distinguish channels opened from these different SEs.
*
* Note that this ID is arbitrary and heavily depends on the order in which
* we attempt to open the ISD-R AIDs. As such, it shall not be treated with
* any significance other than as a transient ID.
*/
val seId: Int
val lpa: LocalProfileAssistant
val valid: Boolean

View file

@ -6,12 +6,11 @@ import im.angry.openeuicc.util.*
// This class is here instead of inside DI because it contains a bit more logic than just
// "dumb" dependency injection.
interface EuiccChannelFactory {
suspend fun tryOpenEuiccChannel(port: UiccPortInfoCompat, isdrAid: ByteArray, seInt: Int): EuiccChannel?
suspend fun tryOpenEuiccChannel(port: UiccPortInfoCompat, isdrAid: ByteArray): EuiccChannel?
fun tryOpenUsbEuiccChannel(
ccidCtx: UsbCcidContext,
isdrAid: ByteArray,
seInt: Int
isdrAid: ByteArray
): EuiccChannel?
/**

View file

@ -14,7 +14,6 @@ class EuiccChannelImpl(
override val intrinsicChannelName: String?,
override val apduInterface: ApduInterface,
override val isdrAid: ByteArray,
override val seId: Int,
verboseLoggingFlow: Flow<Boolean>,
ignoreTLSCertificateFlow: Flow<Boolean>
) : EuiccChannel {

View file

@ -81,7 +81,6 @@ interface EuiccChannelManager {
suspend fun <R> withEuiccChannel(
physicalSlotId: Int,
portId: Int,
seId: Int = 0,
fn: suspend (EuiccChannel) -> R
): R
@ -90,7 +89,6 @@ interface EuiccChannelManager {
*/
suspend fun <R> withEuiccChannel(
logicalSlotId: Int,
seId: Int = 0,
fn: suspend (EuiccChannel) -> R
): R

View file

@ -26,8 +26,6 @@ class EuiccChannelWrapper(orig: EuiccChannel) : EuiccChannel {
get() = channel.logicalSlotId
override val portId: Int
get() = channel.portId
override val seId: Int
get() = channel.seId
private val lpaDelegate = lazy {
LocalProfileAssistantWrapper(channel.lpa)
}

View file

@ -92,7 +92,7 @@ class EuiccInfoActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
lifecycleScope.launch {
(infoList.adapter!! as EuiccInfoAdapter).euiccInfoItems =
euiccChannelManager.withEuiccChannel(logicalSlotId, fn = ::buildEuiccInfoItems)
euiccChannelManager.withEuiccChannel(logicalSlotId, ::buildEuiccInfoItems)
swipeRefresh.isRefreshing = false
}

View file

@ -16,14 +16,13 @@ class PrivilegedEuiccChannelFactory(context: Context) : DefaultEuiccChannelFacto
@Suppress("NAME_SHADOWING")
override suspend fun tryOpenEuiccChannel(
port: UiccPortInfoCompat,
isdrAid: ByteArray,
seId: Int,
isdrAid: ByteArray
): EuiccChannel? {
val port = port as RealUiccPortInfoCompat
if (port.card.isRemovable) {
// Attempt unprivileged (OMAPI) before TelephonyManager
// but still try TelephonyManager in case OMAPI is broken
super.tryOpenEuiccChannel(port, isdrAid, seId)?.let { return it }
super.tryOpenEuiccChannel(port, isdrAid)?.let { return it }
}
if (port.card.isEuicc || preferenceRepository.removableTelephonyManagerFlow.first()) {
@ -42,7 +41,6 @@ class PrivilegedEuiccChannelFactory(context: Context) : DefaultEuiccChannelFacto
context.preferenceRepository.verboseLoggingFlow
),
isdrAid,
seId,
context.preferenceRepository.verboseLoggingFlow,
context.preferenceRepository.ignoreTLSCertificateFlow,
)
@ -55,6 +53,6 @@ class PrivilegedEuiccChannelFactory(context: Context) : DefaultEuiccChannelFacto
}
}
return super.tryOpenEuiccChannel(port, isdrAid, seId)
return super.tryOpenEuiccChannel(port, isdrAid)
}
}