This commit is contained in:
Andrei Vereha 2021-03-30 23:33:59 +02:00
parent 5209386fc1
commit 89bcd52994
8 changed files with 20 additions and 17 deletions

View file

@ -60,7 +60,7 @@ class BleCommCallbacks(
fun confirmWrite(expectedPayload: ByteArray, expectedUUID: String, timeoutMs: Long): WriteConfirmation { fun confirmWrite(expectedPayload: ByteArray, expectedUUID: String, timeoutMs: Long): WriteConfirmation {
try { try {
return when (val received = writeQueue.poll(timeoutMs, TimeUnit.MILLISECONDS)) { return when (val received = writeQueue.poll(timeoutMs, TimeUnit.MILLISECONDS)) {
null -> return WriteConfirmationError("Timeout waiting for writeConfirmation") null -> WriteConfirmationError("Timeout waiting for writeConfirmation")
is WriteConfirmationSuccess -> is WriteConfirmationSuccess ->
if (expectedPayload.contentEquals(received.payload) && if (expectedPayload.contentEquals(received.payload) &&
expectedUUID == received.uuid expectedUUID == received.uuid

View file

@ -71,8 +71,8 @@ sealed class BleCommand(val data: ByteArray) {
return BleCommandIncorrect("Incorrect command: empty payload", payload) return BleCommandIncorrect("Incorrect command: empty payload", payload)
} }
try { return try {
return when (BleCommandType.byValue(payload[0])) { when (BleCommandType.byValue(payload[0])) {
BleCommandType.RTS -> BleCommandType.RTS ->
BleCommandRTS BleCommandRTS
BleCommandType.CTS -> BleCommandType.CTS ->
@ -91,7 +91,7 @@ sealed class BleCommand(val data: ByteArray) {
BleCommandIncorrect("Incorrect command received", payload) BleCommandIncorrect("Incorrect command received", payload)
} }
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
return BleCommandIncorrect("Incorrect command payload", payload) BleCommandIncorrect("Incorrect command payload", payload)
} }
} }
} }

View file

@ -25,7 +25,7 @@ object BleSendSuccess : BleSendResult()
data class BleSendErrorSending(val msg: String, val cause: Throwable? = null) : BleSendResult() data class BleSendErrorSending(val msg: String, val cause: Throwable? = null) : BleSendResult()
data class BleSendErrorConfirming(val msg: String, val cause: Throwable? = null) : BleSendResult() data class BleSendErrorConfirming(val msg: String, val cause: Throwable? = null) : BleSendResult()
abstract class BleIO( open class BleIO(
private val aapsLogger: AAPSLogger, private val aapsLogger: AAPSLogger,
private val characteristic: BluetoothGattCharacteristic, private val characteristic: BluetoothGattCharacteristic,
private val incomingPackets: BlockingQueue<ByteArray>, private val incomingPackets: BlockingQueue<ByteArray>,
@ -40,12 +40,12 @@ abstract class BleIO(
* @return a byte array with the received data or error * @return a byte array with the received data or error
*/ */
fun receivePacket(timeoutMs: Long = DEFAULT_IO_TIMEOUT_MS): BleReceiveResult { fun receivePacket(timeoutMs: Long = DEFAULT_IO_TIMEOUT_MS): BleReceiveResult {
try { return try {
val ret = incomingPackets.poll(timeoutMs, TimeUnit.MILLISECONDS) val packet = incomingPackets.poll(timeoutMs, TimeUnit.MILLISECONDS)
?: return BleReceiveError("Timeout") if (packet == null) BleReceiveError("Timeout")
return BleReceivePayload(ret) else BleReceivePayload(packet)
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
return BleReceiveError("Interrupted", cause = e) BleReceiveError("Interrupted", cause = e)
} }
} }

View file

@ -143,9 +143,9 @@ class MessageIO(
sendResult is BleSendSuccess -> sendResult is BleSendSuccess ->
MessageSendSuccess MessageSendSuccess
index == packets.size - 1 && sendResult is BleSendErrorConfirming -> index == packets.size - 1 && sendResult is BleSendErrorConfirming ->
return MessageSendErrorConfirming("Error confirming last DATA packet $sendResult") MessageSendErrorConfirming("Error confirming last DATA packet $sendResult")
else -> else ->
return MessageSendErrorSending("Error sending DATA: $sendResult") MessageSendErrorSending("Error sending DATA: $sendResult")
} }
} }

View file

@ -14,7 +14,7 @@ class StringLengthPrefixEncoding private constructor() {
private const val LENGTH_BYTES = 2 private const val LENGTH_BYTES = 2
fun parseKeys(keys: Array<String>, payload: ByteArray): Array<ByteArray> { fun parseKeys(keys: Array<String>, payload: ByteArray): Array<ByteArray> {
val ret = Array(keys.size, { ByteArray(0) }) val ret = Array(keys.size) { ByteArray(0) }
var remaining = payload var remaining = payload
for ((index, key) in keys.withIndex()) { for ((index, key) in keys.withIndex()) {
when { when {

View file

@ -31,7 +31,10 @@ internal class PayloadSplitter(private val payload: ByteArray) {
} }
val middleFragments = (payload.size - FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS) / MiddleBlePacket.CAPACITY val middleFragments = (payload.size - FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS) / MiddleBlePacket.CAPACITY
val rest = val rest =
((payload.size - middleFragments * MiddleBlePacket.CAPACITY) - FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS).toByte() (
(payload.size - middleFragments * MiddleBlePacket.CAPACITY) -
FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS
).toByte()
ret.add( ret.add(
FirstBlePacket( FirstBlePacket(
fullFragments = middleFragments + 1, fullFragments = middleFragments + 1,
@ -75,7 +78,9 @@ internal class PayloadSplitter(private val payload: ByteArray) {
index = (middleFragments + 2).toByte(), index = (middleFragments + 2).toByte(),
size = (rest - LastBlePacket.CAPACITY).toByte(), size = (rest - LastBlePacket.CAPACITY).toByte(),
payload = payload.copyOfRange( payload = payload.copyOfRange(
middleFragments * MiddleBlePacket.CAPACITY + FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS + LastBlePacket.CAPACITY, middleFragments * MiddleBlePacket.CAPACITY +
FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS +
LastBlePacket.CAPACITY,
payload.size payload.size
) )
) )

View file

@ -65,7 +65,6 @@ internal class LTKExchanger(
} }
// No exception throwing after this point. It is possible that the pod saved the LTK // No exception throwing after this point. It is possible that the pod saved the LTK
//
val p0 = msgIO.receiveMessage() val p0 = msgIO.receiveMessage()
if (p0 is MessageReceiveSuccess) { if (p0 is MessageReceiveSuccess) {
validateP0(p0.msg) validateP0(p0.msg)

View file

@ -3,6 +3,5 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.scan
import android.os.ParcelUuid import android.os.ParcelUuid
class DiscoveredInvalidPodException : Exception { class DiscoveredInvalidPodException : Exception {
constructor(message: String) : super(message)
constructor(message: String, serviceUUIds: List<ParcelUuid?>) : super("$message service UUIDs: $serviceUUIds") constructor(message: String, serviceUUIds: List<ParcelUuid?>) : super("$message service UUIDs: $serviceUUIds")
} }