diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/BleManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/BleManager.kt index a227adab18..16d6153b58 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/BleManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/BleManager.kt @@ -13,7 +13,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.command. import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.* import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.BleIO import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.scan.PodScanner -import java.util.* import java.util.concurrent.BlockingQueue import java.util.concurrent.LinkedBlockingDeque import java.util.concurrent.TimeoutException @@ -26,7 +25,7 @@ class BleManager @Inject constructor(private val context: Context, private val a private val bluetoothManager: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter - @Throws(InterruptedException::class, ScanFailException::class, FailedToConnectException::class, CouldNotSendBleException::class, BleIOBusyException::class, TimeoutException::class, CouldNotConfirmWrite::class, CouldNotEnableNotifications::class, DescriptorNotFoundException::class, CouldNotConfirmDescriptorWriteException::class) + @Throws(InterruptedException::class, ScanFailException::class, FailedToConnectException::class, CouldNotSendBleException::class, BleIOBusyException::class, TimeoutException::class, CouldNotConfirmWriteException::class, CouldNotEnableNotifications::class, DescriptorNotFoundException::class, CouldNotConfirmDescriptorWriteException::class) fun activateNewPod() { aapsLogger.info(LTag.PUMPBTCOMM, "starting new pod activation") val podScanner = PodScanner(aapsLogger, bluetoothAdapter) @@ -35,7 +34,7 @@ class BleManager @Inject constructor(private val context: Context, private val a connect(podAddress) } - @Throws(FailedToConnectException::class, CouldNotSendBleException::class, InterruptedException::class, BleIOBusyException::class, TimeoutException::class, CouldNotConfirmWrite::class, CouldNotEnableNotifications::class, DescriptorNotFoundException::class, CouldNotConfirmDescriptorWriteException::class) + @Throws(FailedToConnectException::class, CouldNotSendBleException::class, InterruptedException::class, BleIOBusyException::class, TimeoutException::class, CouldNotConfirmWriteException::class, CouldNotEnableNotifications::class, DescriptorNotFoundException::class, CouldNotConfirmDescriptorWriteException::class) private fun connect(podAddress: String) { // TODO: locking? val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) @@ -61,7 +60,7 @@ class BleManager @Inject constructor(private val context: Context, private val a val chars = discoverer.discoverServices() val bleIO = BleIO(aapsLogger, chars, incomingPackets, gatt, bleCommCallbacks) aapsLogger.debug(LTag.PUMPBTCOMM, "Saying hello to the pod") - bleIO.sendAndConfirmPacket(CharacteristicType.CMD, BleCommandHello(CONTROLLER_ID).asByteArray()) + bleIO.sendAndConfirmPacket(CharacteristicType.CMD, BleCommandHello(CONTROLLER_ID).data) bleIO.readyToRead() } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt index e6d9dd545e..daf3db26a4 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt @@ -10,7 +10,7 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.CharacteristicType import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.CharacteristicType.Companion.byValue import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotConfirmDescriptorWriteException -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotConfirmWrite +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotConfirmWriteException import java.util.concurrent.BlockingQueue import java.util.concurrent.CountDownLatch import java.util.concurrent.LinkedBlockingQueue @@ -50,23 +50,21 @@ class BleCommCallbacks(private val aapsLogger: AAPSLogger, private val incomingP serviceDiscoveryComplete.await(timeout_ms.toLong(), TimeUnit.MILLISECONDS) } - @Throws(InterruptedException::class, TimeoutException::class, CouldNotConfirmWrite::class) + @Throws(InterruptedException::class, TimeoutException::class, CouldNotConfirmWriteException::class) fun confirmWrite(expectedPayload: ByteArray, timeout_ms: Int) { val received: CharacteristicWriteConfirmation = writeQueue.poll(timeout_ms.toLong(), TimeUnit.MILLISECONDS) ?: throw TimeoutException() when (received) { is CharacteristicWriteConfirmationPayload -> confirmWritePayload(expectedPayload, received) - is CharacteristicWriteConfirmationError -> - aapsLogger.debug(LTag.PUMPBTCOMM, "Could not confirm write: status was ${received.status}") + is CharacteristicWriteConfirmationError -> throw CouldNotConfirmWriteException(received.status) } - } private fun confirmWritePayload(expectedPayload: ByteArray, received: CharacteristicWriteConfirmationPayload) { if (!expectedPayload.contentEquals(received.payload)) { - aapsLogger.warn(LTag.PUMPBTCOMM, "Could not confirm write. Got " + received.payload + ".Excepted: " + expectedPayload + ". Status: " + received.status) - throw CouldNotConfirmWrite(expectedPayload, received.payload) + aapsLogger.warn(LTag.PUMPBTCOMM, "Could not confirm write. Got " + received.payload + ".Excepted: " + expectedPayload) + throw CouldNotConfirmWriteException(expectedPayload, received.payload) } aapsLogger.debug(LTag.PUMPBTCOMM, "Confirmed write with value: " + received.payload) } @@ -74,8 +72,7 @@ class BleCommCallbacks(private val aapsLogger: AAPSLogger, private val incomingP override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) { super.onCharacteristicWrite(gatt, characteristic, status) val writeConfirmation = if (status == BluetoothGatt.GATT_SUCCESS) { - aapsLogger.debug(LTag.PUMPBTCOMM, "OnCharacteristicWrite value " + characteristic.getStringValue(0)) - CharacteristicWriteConfirmationPayload(characteristic.value, status) + CharacteristicWriteConfirmationPayload(characteristic.value) } else { CharacteristicWriteConfirmationError(status) } @@ -107,26 +104,33 @@ class BleCommCallbacks(private val aapsLogger: AAPSLogger, private val incomingP @Throws(InterruptedException::class, CouldNotConfirmDescriptorWriteException::class) fun confirmWriteDescriptor(descriptorUUID: String, timeout_ms: Int) { - val confirmed = descriptorWriteQueue.poll(timeout_ms.toLong(), TimeUnit.MILLISECONDS) - if (descriptorUUID != confirmed.uuid) { - aapsLogger.warn(LTag.PUMPBTCOMM, "Could not confirm descriptor write. Got " + confirmed.uuid + ".Expected: " + descriptorUUID + ". Status: " + confirmed.status) - throw CouldNotConfirmDescriptorWriteException(confirmed.uuid!!, descriptorUUID) + val confirmed: DescriptorWriteConfirmation = descriptorWriteQueue.poll(timeout_ms.toLong(), TimeUnit.MILLISECONDS) + ?: throw TimeoutException() + when (confirmed) { + is DescriptorWriteConfirmationError -> throw CouldNotConfirmWriteException(confirmed.status) + is DescriptorWriteConfirmationUUID -> if (confirmed.uuid != descriptorUUID) { + aapsLogger.warn(LTag.PUMPBTCOMM, "Could not confirm descriptor write. Got ${confirmed.uuid}. Expected: ${descriptorUUID}") + throw CouldNotConfirmDescriptorWriteException(descriptorUUID, confirmed.uuid) + } else { + aapsLogger.debug(LTag.PUMPBTCOMM, "Confirmed descriptor write : " + confirmed.uuid) + } } } override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) { super.onDescriptorWrite(gatt, descriptor, status) - var uuid: String? = null - if (status == BluetoothGatt.GATT_SUCCESS) { - uuid = descriptor.uuid.toString() + val writeConfirmation = if (status == BluetoothGatt.GATT_SUCCESS) { + aapsLogger.debug(LTag.PUMPBTCOMM, "OnDescriptor value " + descriptor.value) + DescriptorWriteConfirmationUUID(descriptor.uuid.toString()) + } else { + DescriptorWriteConfirmationError(status) } - val confirmation = DescriptorWriteConfirmation(status, uuid) try { if (descriptorWriteQueue.size > 0) { aapsLogger.warn(LTag.PUMPBTCOMM, "Descriptor write queue should be empty, found: " + descriptorWriteQueue.size) descriptorWriteQueue.clear() } - val offered = descriptorWriteQueue.offer(confirmation, WRITE_CONFIRM_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS) + val offered = descriptorWriteQueue.offer(writeConfirmation, WRITE_CONFIRM_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS) if (!offered) { aapsLogger.warn(LTag.PUMPBTCOMM, "Received delayed descriptor write confirmation") } @@ -136,7 +140,6 @@ class BleCommCallbacks(private val aapsLogger: AAPSLogger, private val incomingP } companion object { - - private const val WRITE_CONFIRM_TIMEOUT_MS = 10 // the other thread should be waiting for the exchange + private const val WRITE_CONFIRM_TIMEOUT_MS = 10 // the confirmation queue should be empty anyway } } \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/CharacteristicWriteConfirmation.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/CharacteristicWriteConfirmation.kt index f863fa29ec..31e3595050 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/CharacteristicWriteConfirmation.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/CharacteristicWriteConfirmation.kt @@ -2,6 +2,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.callbac sealed class CharacteristicWriteConfirmation -class CharacteristicWriteConfirmationPayload(val payload: ByteArray, val status: Int) : CharacteristicWriteConfirmation() +data class CharacteristicWriteConfirmationPayload(val payload: ByteArray) : CharacteristicWriteConfirmation() -class CharacteristicWriteConfirmationError(val status: Int) : CharacteristicWriteConfirmation() \ No newline at end of file +data class CharacteristicWriteConfirmationError(val status: Int) : CharacteristicWriteConfirmation() \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/DescriptorWriteConfirmation.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/DescriptorWriteConfirmation.kt index c975393d7c..3c7b4f4f9f 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/DescriptorWriteConfirmation.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/DescriptorWriteConfirmation.kt @@ -1,3 +1,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.callbacks -class DescriptorWriteConfirmation(var status: Int, var uuid: String?) \ No newline at end of file +sealed class DescriptorWriteConfirmation + +data class DescriptorWriteConfirmationUUID(val uuid: String): DescriptorWriteConfirmation() + +data class DescriptorWriteConfirmationError(val status: Int): DescriptorWriteConfirmation() \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommand.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommand.kt index d5483f3d37..47389b0a05 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommand.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommand.kt @@ -2,7 +2,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.command abstract class BleCommand { - private val data: ByteArray + val data: ByteArray constructor(type: BleCommandType) { data = byteArrayOf(type.value) @@ -14,8 +14,4 @@ abstract class BleCommand { data[0] = type.value System.arraycopy(payload, 0, data, 1, payload.size) } - - fun asByteArray(): ByteArray { - return data - } } \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmDescriptorWriteException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmDescriptorWriteException.kt index 0b49f74970..2bc86e92a7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmDescriptorWriteException.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmDescriptorWriteException.kt @@ -1,3 +1,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions -class CouldNotConfirmDescriptorWriteException(private val received: String, private val expected: String) : Exception() \ No newline at end of file +class CouldNotConfirmDescriptorWriteException(override val message: String?) : Exception(message) { + constructor(sent: String, confirmed: String): this("Could not confirm write. Sent: {$sent} .Received: ${confirmed}") + constructor(status: Int): this("Could not confirm write. Write status: ${status}") +} \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmWrite.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmWrite.kt deleted file mode 100644 index ab972d4344..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmWrite.kt +++ /dev/null @@ -1,3 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions - -class CouldNotConfirmWrite(private val sent: ByteArray, private val confirmed: ByteArray?) : Exception() \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmWriteException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmWriteException.kt new file mode 100644 index 0000000000..a3c15bfb05 --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmWriteException.kt @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions + +class CouldNotConfirmWriteException(override val message: String?) : Exception(message) { + constructor(sent: ByteArray, confirmed: ByteArray): this("Could not confirm write. Sent: {$sent} .Received: ${confirmed}") + constructor(status: Int): this("Could not confirm write. Write status: ${status}") +} \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DiscoveredInvalidPodException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DiscoveredInvalidPodException.kt index 9b8ff7488d..f7f6b08630 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DiscoveredInvalidPodException.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DiscoveredInvalidPodException.kt @@ -2,4 +2,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.excepti import android.os.ParcelUuid -class DiscoveredInvalidPodException(message: String, serviceUUIds: List) : Exception("$message service UUIDs: $serviceUUIds") \ No newline at end of file +class DiscoveredInvalidPodException: Exception { + constructor(message: String) : super(message) {} + constructor(message: String, serviceUUIds: List) : super("$message service UUIDs: $serviceUUIds"){} +} \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/BleIO.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/BleIO.kt index 397520820c..39cca93aea 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/BleIO.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/BleIO.kt @@ -41,7 +41,7 @@ class BleIO(private val aapsLogger: AAPSLogger, private val chars: Map