瀏覽代碼

fix: omapi apdu interface (#152)

Reviewed-on: https://gitea.angry.im/PeterCxy/OpenEUICC/pulls/152
Co-authored-by: septs <github@septs.pw>
Co-committed-by: septs <github@septs.pw>
septs 11 月之前
父節點
當前提交
99d9200c28
共有 1 個文件被更改,包括 9 次插入13 次删除
  1. 9 13
      app-common/src/main/java/im/angry/openeuicc/core/OmapiApduInterface.kt

+ 9 - 13
app-common/src/main/java/im/angry/openeuicc/core/OmapiApduInterface.kt

@@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.first
 import kotlinx.coroutines.runBlocking
 import net.typeblog.lpac_jni.ApduInterface
+import java.util.concurrent.atomic.AtomicInteger
 
 class OmapiApduInterface(
     private val service: SEService,
@@ -20,12 +21,8 @@ class OmapiApduInterface(
     }
 
     private lateinit var session: Session
-    private val channels = arrayOf<Channel?>(
-        null,
-        null,
-        null,
-        null,
-    )
+    private val index = AtomicInteger(0)
+    private val channels = mutableMapOf<Int, Channel>()
 
     override val valid: Boolean
         get() = service.isConnected && (this::session.isInitialized && !session.isClosed)
@@ -44,21 +41,20 @@ class OmapiApduInterface(
     override fun logicalChannelOpen(aid: ByteArray): Int {
         val channel = session.openLogicalChannel(aid)
         check(channel != null) { "Failed to open logical channel (${aid.encodeHex()})" }
-        val index = channels.indexOf(null)
-        check(index != -1) { "No free logical channel slots" }
-        synchronized(channels) { channels[index] = channel }
-        return index
+        val handle = index.incrementAndGet()
+        synchronized(channels) { channels[handle] = channel }
+        return handle
     }
 
     override fun logicalChannelClose(handle: Int) {
-        val channel = channels.getOrNull(handle)
+        val channel = channels[handle]
         check(channel != null) { "Invalid logical channel handle $handle" }
         if (channel.isOpen) channel.close()
-        synchronized(channels) { channels[handle] = null }
+        synchronized(channels) { channels.remove(handle) }
     }
 
     override fun transmit(handle: Int, tx: ByteArray): ByteArray {
-        val channel = channels.getOrNull(handle)
+        val channel = channels[handle]
         check(channel != null) { "Invalid logical channel handle $handle" }
 
         if (runBlocking { verboseLoggingFlow.first() }) {