Skip to content

Commit

Permalink
improve compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed Feb 11, 2025
1 parent c94ce3f commit b7f4324
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.8

* Improve compatibility

## 0.1.7

* Downgrade to Flutter 3.24
Expand Down
46 changes: 32 additions & 14 deletions android/src/main/kotlin/im/nfc/ccid/Ccid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,42 @@ class Ccid(

private fun receiveRawMessage(expectedSeq: Byte): CcidRdrToPcMessage {
var retries = 3
var bytesRead: Int
var bytesRead = 0
var message: CcidRdrToPcMessage? = null
val buffer = ByteArray(bulkIn.maxPacketSize)
do {
bytesRead = usbDeviceConnection.bulkTransfer(bulkIn, buffer, buffer.size, USB_TIMEOUT)
} while (bytesRead <= 0 && retries-- > 0)

if (bytesRead < HEADER_SIZE) {
throw CcidException("Incorrect header")
}
if (buffer[0] != MESSAGE_TYPE_RDR_TO_PC_DATABLOCK) {
throw CcidException("Unexpected message type")
var lastException: CcidException? = null
while (retries > 0) {
try {
bytesRead = usbDeviceConnection.bulkTransfer(bulkIn, buffer, buffer.size, USB_TIMEOUT)
if (bytesRead <= 0) {
throw CcidException("Failed to read data")
}
if (bytesRead < HEADER_SIZE) {
throw CcidException("Incorrect header")
}
if (buffer[0] != MESSAGE_TYPE_RDR_TO_PC_DATABLOCK) {
throw CcidException("Unexpected message type")
}
message = CcidRdrToPcMessage.parseHeader(buffer)
if (message.seq != expectedSeq) {
throw CcidException("Unexpected sequence number ${message.seq}, expected $expectedSeq")
}
lastException = null
break
} catch (e: CcidException) {
lastException = e
retries--
if (retries > 0) {
Thread.sleep(100)
}
}
}
val message = CcidRdrToPcMessage.parseHeader(buffer)
if (message.seq != expectedSeq) {
throw CcidException("Unexpected sequence number ${message.seq}, expected $expectedSeq")

if (lastException != null) {
throw lastException
}

val dataBuffer = ByteArray(message.length)
val dataBuffer = ByteArray(message!!.length)
var bytesBuffered = bytesRead - HEADER_SIZE
System.arraycopy(buffer, HEADER_SIZE, dataBuffer, 0, bytesBuffered)

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ccid
description: "A Flutter plugin for smart card reader using CCID protocol with PC/SC-like APIs."
version: 0.1.7
version: 0.1.8
homepage: https://github.com/nfcim/ccid

environment:
Expand Down

0 comments on commit b7f4324

Please sign in to comment.