Compare commits
1 commit
5e7db370b0
...
3a349bf0ca
Author | SHA1 | Date | |
---|---|---|---|
3a349bf0ca |
3 changed files with 34 additions and 9 deletions
|
@ -40,20 +40,24 @@ class OmapiApduInterface(
|
||||||
|
|
||||||
override fun logicalChannelOpen(aid: ByteArray): Int {
|
override fun logicalChannelOpen(aid: ByteArray): Int {
|
||||||
val channel = session.openLogicalChannel(aid)!!
|
val channel = session.openLogicalChannel(aid)!!
|
||||||
val id = index.getAndAdd(1)
|
val id = index.addAndGet(1)
|
||||||
channels[id] = channel
|
channels[id] = channel
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun logicalChannelClose(handle: Int) {
|
override fun logicalChannelClose(handle: Int) {
|
||||||
val channel = channels[handle]
|
val channel = channels[handle]
|
||||||
check(channel != null) { "Channel $handle does not exist" }
|
check(channel != null) {
|
||||||
|
"Invalid logical channel handle $handle"
|
||||||
|
}
|
||||||
channel.close()
|
channel.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun transmit(handle: Int, tx: ByteArray): ByteArray {
|
override fun transmit(handle: Int, tx: ByteArray): ByteArray {
|
||||||
val channel = channels[handle]
|
val channel = channels[handle]
|
||||||
check(channel != null) { "Channel $handle does not exist" }
|
check(channel != null) {
|
||||||
|
"Invalid logical channel handle $handle"
|
||||||
|
}
|
||||||
|
|
||||||
if (runBlocking { verboseLoggingFlow.first() }) {
|
if (runBlocking { verboseLoggingFlow.first() }) {
|
||||||
Log.d(TAG, "OMAPI APDU: ${tx.encodeHex()}")
|
Log.d(TAG, "OMAPI APDU: ${tx.encodeHex()}")
|
||||||
|
|
|
@ -23,6 +23,11 @@ class UsbApduInterface(
|
||||||
|
|
||||||
override var atr: ByteArray? = null
|
override var atr: ByteArray? = null
|
||||||
|
|
||||||
|
override val valid: Boolean
|
||||||
|
get() = channels.isNotEmpty()
|
||||||
|
|
||||||
|
private var channels = mutableSetOf<Int>()
|
||||||
|
|
||||||
override fun connect() {
|
override fun connect() {
|
||||||
ccidDescription = UsbCcidDescription.fromRawDescriptors(conn.rawDescriptors)!!
|
ccidDescription = UsbCcidDescription.fromRawDescriptors(conn.rawDescriptors)!!
|
||||||
|
|
||||||
|
@ -76,10 +81,15 @@ class UsbApduInterface(
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
channels.add(channelId)
|
||||||
|
|
||||||
return channelId
|
return channelId
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun logicalChannelClose(handle: Int) {
|
override fun logicalChannelClose(handle: Int) {
|
||||||
|
check(channels.contains(handle)) {
|
||||||
|
"Invalid logical channel handle $handle"
|
||||||
|
}
|
||||||
// CLOSE LOGICAL CHANNEL
|
// CLOSE LOGICAL CHANNEL
|
||||||
val req = manageChannelCmd(false, handle.toByte())
|
val req = manageChannelCmd(false, handle.toByte())
|
||||||
val resp = transmitApduByChannel(req, handle.toByte())
|
val resp = transmitApduByChannel(req, handle.toByte())
|
||||||
|
@ -87,15 +97,16 @@ class UsbApduInterface(
|
||||||
if (!isSuccessResponse(resp)) {
|
if (!isSuccessResponse(resp)) {
|
||||||
Log.d(TAG, "CLOSE LOGICAL CHANNEL failed: ${resp.encodeHex()}")
|
Log.d(TAG, "CLOSE LOGICAL CHANNEL failed: ${resp.encodeHex()}")
|
||||||
}
|
}
|
||||||
|
channels.remove(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun transmit(handle: Int, tx: ByteArray): ByteArray {
|
override fun transmit(handle: Int, tx: ByteArray): ByteArray {
|
||||||
|
check(channels.contains(handle)) {
|
||||||
|
"Invalid logical channel handle $handle"
|
||||||
|
}
|
||||||
return transmitApduByChannel(tx, handle.toByte())
|
return transmitApduByChannel(tx, handle.toByte())
|
||||||
}
|
}
|
||||||
|
|
||||||
override val valid: Boolean
|
|
||||||
get() = atr != null
|
|
||||||
|
|
||||||
private fun isSuccessResponse(resp: ByteArray): Boolean =
|
private fun isSuccessResponse(resp: ByteArray): Boolean =
|
||||||
resp.size >= 2 && resp[resp.size - 2] == 0x90.toByte() && resp[resp.size - 1] == 0x00.toByte()
|
resp.size >= 2 && resp[resp.size - 2] == 0x90.toByte() && resp[resp.size - 1] == 0x00.toByte()
|
||||||
|
|
||||||
|
|
|
@ -23,9 +23,13 @@ class TelephonyManagerApduInterface(
|
||||||
|
|
||||||
private var channels = mutableSetOf<Int>()
|
private var channels = mutableSetOf<Int>()
|
||||||
|
|
||||||
|
override fun connect() {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
override fun connect() {}
|
}
|
||||||
override fun disconnect() {}
|
|
||||||
|
override fun disconnect() {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
override fun logicalChannelOpen(aid: ByteArray): Int {
|
override fun logicalChannelOpen(aid: ByteArray): Int {
|
||||||
val hex = aid.encodeHex()
|
val hex = aid.encodeHex()
|
||||||
|
@ -38,11 +42,17 @@ class TelephonyManagerApduInterface(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun logicalChannelClose(handle: Int) {
|
override fun logicalChannelClose(handle: Int) {
|
||||||
|
check(channels.contains(handle)) {
|
||||||
|
"Invalid logical channel handle $handle"
|
||||||
|
}
|
||||||
tm.iccCloseLogicalChannelByPortCompat(port.card.physicalSlotIndex, port.portIndex, handle)
|
tm.iccCloseLogicalChannelByPortCompat(port.card.physicalSlotIndex, port.portIndex, handle)
|
||||||
channels.remove(handle)
|
channels.remove(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun transmit(handle: Int, tx: ByteArray): ByteArray {
|
override fun transmit(handle: Int, tx: ByteArray): ByteArray {
|
||||||
|
check(channels.contains(handle)) {
|
||||||
|
"Invalid logical channel handle $handle"
|
||||||
|
}
|
||||||
if (runBlocking { verboseLoggingFlow.first() }) {
|
if (runBlocking { verboseLoggingFlow.first() }) {
|
||||||
Log.d(TAG, "TelephonyManager APDU: ${tx.encodeHex()}")
|
Log.d(TAG, "TelephonyManager APDU: ${tx.encodeHex()}")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue