OpenEUICC/app-common/src/main/java/im/angry/openeuicc/core/OmapiApduInterface.kt
Peter Cai e48f9aa828
All checks were successful
/ build-debug (push) Successful in 4m51s
refactor: Channel validity, and reconnection
* ApduInterfaces also need a concept of validity based on the underlying
  APDU channel. For example, OMAPI depends on SEService being still
  connected.
* We then rely on this validity to wait for reconnection; we do not need
  to manually remove all channels under a slot because the rest will be
  invalid anyway, and the next attempt at connection will lazily
  recreate the channel.
* We had to manage channels manually before during reconnect because
  `valid` may result in SIGSEGV's when the underlying APDU channel has
  become invalid. This is avoided by the validity concept added to APDU
  channels.
2024-03-22 21:08:59 -04:00

50 lines
1.3 KiB
Kotlin

package im.angry.openeuicc.core
import android.se.omapi.Channel
import android.se.omapi.SEService
import android.se.omapi.Session
import im.angry.openeuicc.util.*
import net.typeblog.lpac_jni.ApduInterface
class OmapiApduInterface(
private val service: SEService,
private val port: UiccPortInfoCompat
): ApduInterface {
private lateinit var session: Session
private lateinit var lastChannel: Channel
override val valid: Boolean
get() = service.isConnected && (this::session.isInitialized && !session.isClosed)
override fun connect() {
session = service.getUiccReaderCompat(port.logicalSlotIndex + 1).openSession()
}
override fun disconnect() {
session.close()
}
override fun logicalChannelOpen(aid: ByteArray): Int {
check(!this::lastChannel.isInitialized) {
"Can only open one channel"
}
lastChannel = session.openLogicalChannel(aid)!!;
return 1;
}
override fun logicalChannelClose(handle: Int) {
check(handle == 1 && !this::lastChannel.isInitialized) {
"Unknown channel"
}
lastChannel.close()
}
override fun transmit(tx: ByteArray): ByteArray {
check(this::lastChannel.isInitialized) {
"Unknown channel"
}
return lastChannel.transmit(tx)
}
}