Compare commits

...

2 commits

Author SHA1 Message Date
d54fcf2589 refactor: Make EuiccChannel abstract
All checks were successful
/ build-debug (push) Successful in 4m13s
This allows wrapping to control reference lifetime outside of
EuiccChannelManager.
2024-10-26 15:32:31 -04:00
7cb872a664 Add new withEuiccChannel() method to EuiccChannelManager 2024-10-26 15:32:31 -04:00
6 changed files with 58 additions and 20 deletions

View file

@ -33,7 +33,7 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
Log.i(DefaultEuiccChannelManager.TAG, "Trying OMAPI for physical slot ${port.card.physicalSlotIndex}")
try {
return EuiccChannel(
return EuiccChannelImpl(
port,
OmapiApduInterface(
seService!!,
@ -61,7 +61,7 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
if (bulkIn == null || bulkOut == null) return null
val conn = usbManager.openDevice(usbDevice) ?: return null
if (!conn.claimInterface(usbInterface, true)) return null
return EuiccChannel(
return EuiccChannelImpl(
FakeUiccPortInfoCompat(FakeUiccCardInfoCompat(EuiccChannelManager.USB_CHANNEL_ID)),
UsbApduInterface(
conn,

View file

@ -159,6 +159,16 @@ open class DefaultEuiccChannelManager(
findEuiccChannelByPort(physicalSlotId, portId)
}
override suspend fun <R> withEuiccChannel(
physicalSlotId: Int,
portId: Int,
fn: (EuiccChannel) -> R
): R {
val channel = findEuiccChannelByPortBlocking(physicalSlotId, portId)
?: throw EuiccChannelManager.EuiccChannelNotFoundException()
return fn(channel)
}
override suspend fun waitForReconnect(physicalSlotId: Int, portId: Int, timeoutMillis: Long) {
if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) return

View file

@ -1,26 +1,18 @@
package im.angry.openeuicc.core
import im.angry.openeuicc.util.*
import kotlinx.coroutines.flow.Flow
import net.typeblog.lpac_jni.ApduInterface
import net.typeblog.lpac_jni.LocalProfileAssistant
import net.typeblog.lpac_jni.impl.HttpInterfaceImpl
import net.typeblog.lpac_jni.impl.LocalProfileAssistantImpl
class EuiccChannel(
val port: UiccPortInfoCompat,
apduInterface: ApduInterface,
verboseLoggingFlow: Flow<Boolean>
) {
val slotId = port.card.physicalSlotIndex // PHYSICAL slot
val logicalSlotId = port.logicalSlotIndex
val portId = port.portIndex
interface EuiccChannel {
val port: UiccPortInfoCompat
val lpa: LocalProfileAssistant =
LocalProfileAssistantImpl(apduInterface, HttpInterfaceImpl(verboseLoggingFlow))
val slotId: Int // PHYSICAL slot
val logicalSlotId: Int
val portId: Int
val lpa: LocalProfileAssistant
val valid: Boolean
get() = lpa.valid
fun close() = lpa.close()
}
fun close()
}

View file

@ -0,0 +1,26 @@
package im.angry.openeuicc.core
import im.angry.openeuicc.util.*
import kotlinx.coroutines.flow.Flow
import net.typeblog.lpac_jni.ApduInterface
import net.typeblog.lpac_jni.LocalProfileAssistant
import net.typeblog.lpac_jni.impl.HttpInterfaceImpl
import net.typeblog.lpac_jni.impl.LocalProfileAssistantImpl
class EuiccChannelImpl(
override val port: UiccPortInfoCompat,
apduInterface: ApduInterface,
verboseLoggingFlow: Flow<Boolean>
) : EuiccChannel {
override val slotId = port.card.physicalSlotIndex
override val logicalSlotId = port.logicalSlotIndex
override val portId = port.portIndex
override val lpa: LocalProfileAssistant =
LocalProfileAssistantImpl(apduInterface, HttpInterfaceImpl(verboseLoggingFlow))
override val valid: Boolean
get() = lpa.valid
override fun close() = lpa.close()
}

View file

@ -64,6 +64,16 @@ interface EuiccChannelManager {
suspend fun findEuiccChannelByPort(physicalSlotId: Int, portId: Int): EuiccChannel?
fun findEuiccChannelByPortBlocking(physicalSlotId: Int, portId: Int): EuiccChannel?
class EuiccChannelNotFoundException: Exception("EuiccChannel not found")
/**
* Find a EuiccChannel by its slot and port, then run a callback with a reference to it.
* The reference is not supposed to be held outside of the callback.
*
* If a channel for that slot / port is not found, EuiccChannelNotFoundException is thrown
*/
suspend fun <R> withEuiccChannel(physicalSlotId: Int, portId: Int, fn: (EuiccChannel) -> R): R
/**
* Invalidate all EuiccChannels previously cached by this Manager
*/

View file

@ -26,7 +26,7 @@ class PrivilegedEuiccChannelFactory(context: Context) : DefaultEuiccChannelFacto
"Trying TelephonyManager for slot ${port.card.physicalSlotIndex} port ${port.portIndex}"
)
try {
return EuiccChannel(
return EuiccChannelImpl(
port,
TelephonyManagerApduInterface(
port,