Extract magic numbers to named constants
This commit is contained in:
parent
2bea06d14b
commit
fffc5165a4
2 changed files with 16 additions and 8 deletions
|
|
@ -37,6 +37,10 @@ class SettingsActivity : AppCompatActivity() {
|
|||
private const val CDC_DATA_INTERFACE_CLASS = 0x0A
|
||||
private const val KEY_TIMEOUT_MS = 5000L
|
||||
private const val SET_KEY_TIMEOUT_MS = 5000L
|
||||
private const val MAX_KEY_RETRIES = 5
|
||||
private const val KEY_SETTLE_MS = 1000L
|
||||
private const val RESPONSE_WAIT_MS = 500L
|
||||
private const val USB_BULK_TIMEOUT_MS = 1000
|
||||
private const val ACTION_USB_PERMISSION = "com.eddy.atomrelay.USB_PERMISSION"
|
||||
private const val STYLE_SPINNER = 0
|
||||
private const val STYLE_HORIZONTAL = 1
|
||||
|
|
@ -347,20 +351,20 @@ class SettingsActivity : AppCompatActivity() {
|
|||
try {
|
||||
setupCdcControl(connection, device)
|
||||
|
||||
for (attempt in 1..5) {
|
||||
Thread.sleep(1000L)
|
||||
for (attempt in 1..MAX_KEY_RETRIES) {
|
||||
Thread.sleep(KEY_SETTLE_MS)
|
||||
|
||||
flushReadBuffer(connection, epIn)
|
||||
|
||||
val cmd = "KEY?\n".toByteArray(StandardCharsets.US_ASCII)
|
||||
val written = connection.bulkTransfer(epOut, cmd, cmd.size, 1000)
|
||||
val written = connection.bulkTransfer(epOut, cmd, cmd.size, USB_BULK_TIMEOUT_MS)
|
||||
Log.d(TAG, "Attempt $attempt: Sent KEY? ($written bytes)")
|
||||
if (written < 0) {
|
||||
Log.w(TAG, "Write failed on attempt $attempt")
|
||||
continue
|
||||
}
|
||||
|
||||
Thread.sleep(500)
|
||||
Thread.sleep(RESPONSE_WAIT_MS)
|
||||
|
||||
val response = readResponse(connection, epIn, KEY_TIMEOUT_MS)
|
||||
Log.d(TAG, "Attempt $attempt response: ${response.take(300)}")
|
||||
|
|
@ -380,13 +384,13 @@ class SettingsActivity : AppCompatActivity() {
|
|||
val newKey = generateRandomHexKey()
|
||||
|
||||
val setKeyCmd = "SET_KEY:$newKey\n".toByteArray(StandardCharsets.US_ASCII)
|
||||
val w = connection.bulkTransfer(epOut, setKeyCmd, setKeyCmd.size, 1000)
|
||||
val w = connection.bulkTransfer(epOut, setKeyCmd, setKeyCmd.size, USB_BULK_TIMEOUT_MS)
|
||||
Log.d(TAG, "Sent SET_KEY ($w bytes)")
|
||||
if (w < 0) {
|
||||
return KeyResult.Error("SET_KEY schrijven mislukt")
|
||||
}
|
||||
|
||||
Thread.sleep(500)
|
||||
Thread.sleep(RESPONSE_WAIT_MS)
|
||||
|
||||
val okResponse = readResponse(connection, epIn, SET_KEY_TIMEOUT_MS)
|
||||
Log.d(TAG, "SET_KEY response: ${okResponse.take(200)}")
|
||||
|
|
|
|||
|
|
@ -13,14 +13,18 @@ class UsbSerialTransport(
|
|||
private val epIn: UsbEndpoint
|
||||
) : SerialTransport {
|
||||
|
||||
companion object {
|
||||
private const val USB_TIMEOUT_MS = 5000
|
||||
}
|
||||
|
||||
override fun read(buffer: ByteArray, length: Int): Int {
|
||||
val n = connection.bulkTransfer(epIn, buffer, length, 5000)
|
||||
val n = connection.bulkTransfer(epIn, buffer, length, USB_TIMEOUT_MS)
|
||||
if (n < 0) return 0
|
||||
return n
|
||||
}
|
||||
|
||||
override fun write(buffer: ByteArray, length: Int) {
|
||||
val n = connection.bulkTransfer(epOut, buffer, length, 5000)
|
||||
val n = connection.bulkTransfer(epOut, buffer, length, USB_TIMEOUT_MS)
|
||||
if (n < 0) throw IOException("USB write failed")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue