From 8d2d38163a3da530682174d2b0203f38b9b70681 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 28 Mar 2021 21:43:32 +0200 Subject: [PATCH] simplify exceptions --- .../dash/driver/comm/ServiceDiscoverer.kt | 17 +++++++++-------- .../driver/comm/callbacks/BleCommCallbacks.kt | 15 +++++++++++---- .../CharacteristicNotFoundException.kt | 3 --- ...NotFoundException.kt => ConnectException.kt} | 2 +- .../CouldNotConfirmCommandException.kt | 3 --- .../exceptions/CouldNotInitiateConnection.kt | 3 --- .../exceptions/ScanFailNotFoundException.kt | 3 --- .../comm/exceptions/ServiceNotFoundException.kt | 3 --- .../pump/omnipod/dash/driver/comm/io/BleIO.kt | 14 ++++++++------ .../driver/comm/scan/BleDiscoveredDevice.kt | 1 - .../DiscoveredInvalidPodException.kt | 2 +- .../omnipod/dash/driver/comm/scan/PodScanner.kt | 3 +-- .../dash/driver/comm/scan/ScanCollector.kt | 1 - .../driver/comm/session/SessionEstablisher.kt | 1 - 14 files changed, 31 insertions(+), 40 deletions(-) delete mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CharacteristicNotFoundException.kt rename omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/{DescriptorNotFoundException.kt => ConnectException.kt} (59%) delete mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmCommandException.kt delete mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotInitiateConnection.kt delete mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ScanFailNotFoundException.kt delete mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ServiceNotFoundException.kt rename omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/{exceptions => scan}/DiscoveredInvalidPodException.kt (93%) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt index 280615adf2..284b6d975d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt @@ -5,8 +5,7 @@ import android.bluetooth.BluetoothGattCharacteristic import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.callbacks.BleCommCallbacks -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CharacteristicNotFoundException -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ServiceNotFoundException +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ConnectException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.CharacteristicType import java.math.BigInteger import java.util.* @@ -20,18 +19,20 @@ class ServiceDiscoverer( /*** * This is first step after connection establishment */ - @Throws(InterruptedException::class, ServiceNotFoundException::class, CharacteristicNotFoundException::class) fun discoverServices(): Map { logger.debug(LTag.PUMPBTCOMM, "Discovering services") - gatt.discoverServices() + val discover = gatt.discoverServices() + if (!discover) { + throw ConnectException("Could not start discovering services`") + } bleCallbacks.waitForServiceDiscovery(DISCOVER_SERVICES_TIMEOUT_MS) logger.debug(LTag.PUMPBTCOMM, "Services discovered") val service = gatt.getService(SERVICE_UUID.toUuid()) - ?: throw ServiceNotFoundException(SERVICE_UUID) + ?: throw ConnectException("Service not found: $SERVICE_UUID") val cmdChar = service.getCharacteristic(CharacteristicType.CMD.uuid) - ?: throw CharacteristicNotFoundException(CharacteristicType.CMD.value) - val dataChar = service.getCharacteristic(CharacteristicType.DATA.uuid) // TODO: this is never used - ?: throw CharacteristicNotFoundException(CharacteristicType.DATA.value) + ?: throw ConnectException("Characteristic not found: ${CharacteristicType.CMD.value}") + val dataChar = service.getCharacteristic(CharacteristicType.DATA.uuid) + ?: throw ConnectException("Characteristic not found: ${CharacteristicType.DATA.value}") var chars = mapOf( CharacteristicType.CMD to cmdChar, CharacteristicType.DATA to dataChar 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 cf0ca067ec..b8a59c33ea 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 @@ -41,14 +41,21 @@ class BleCommCallbacks( } } - @Throws(InterruptedException::class) fun waitForConnection(timeoutMs: Int) { - connected.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS) + try { + connected.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS) + } catch (e: InterruptedException) { + aapsLogger.warn(LTag.PUMPBTCOMM,"Interrupted while waiting for Connection") + + } } - @Throws(InterruptedException::class) fun waitForServiceDiscovery(timeoutMs: Int) { - serviceDiscoveryComplete.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS) + try { + serviceDiscoveryComplete.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS) + } catch (e: InterruptedException) { + aapsLogger.warn(LTag.PUMPBTCOMM,"Interrupted while waiting for ServiceDiscovery") + } } fun confirmWrite(expectedPayload: ByteArray, expectedUUID: String, timeoutMs: Long) : WriteConfirmation{ diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CharacteristicNotFoundException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CharacteristicNotFoundException.kt deleted file mode 100644 index dedd9399ab..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CharacteristicNotFoundException.kt +++ /dev/null @@ -1,3 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions - -class CharacteristicNotFoundException(cmdCharacteristicUuid: String) : FailedToConnectException("characteristic not found: $cmdCharacteristicUuid") diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DescriptorNotFoundException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ConnectException.kt similarity index 59% rename from omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DescriptorNotFoundException.kt rename to omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ConnectException.kt index 4a87e667e9..2ccd7fb72a 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DescriptorNotFoundException.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ConnectException.kt @@ -1,3 +1,3 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions -class DescriptorNotFoundException : Exception() +class ConnectException(val msg: String) : Exception(msg) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmCommandException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmCommandException.kt deleted file mode 100644 index e2a8520fb9..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotConfirmCommandException.kt +++ /dev/null @@ -1,3 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions - -class CouldNotConfirmCommandException(val msg: String="Could not confirm command") : Exception(msg) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotInitiateConnection.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotInitiateConnection.kt deleted file mode 100644 index 84ab11d5df..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotInitiateConnection.kt +++ /dev/null @@ -1,3 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions - -class CouldNotInitiateConnection(msg: String) : Exception(msg) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ScanFailNotFoundException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ScanFailNotFoundException.kt deleted file mode 100644 index 0f9883093c..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ScanFailNotFoundException.kt +++ /dev/null @@ -1,3 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions - -class ScanFailNotFoundException : ScanException("No Pod found") diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ServiceNotFoundException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ServiceNotFoundException.kt deleted file mode 100644 index 16c19f65a9..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/ServiceNotFoundException.kt +++ /dev/null @@ -1,3 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions - -class ServiceNotFoundException(serviceUuid: String) : FailedToConnectException("service not found: $serviceUuid") 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 186537835f..cca09fca38 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 @@ -99,25 +99,27 @@ abstract class BleIO( fun readyToRead(): BleSendResult { val notificationSet = gatt.setCharacteristicNotification(characteristic, true) if (!notificationSet) { - throw CouldNotInitiateConnection("Could not enable notifications") + throw ConnectException("Could not enable notifications") } val descriptors = characteristic.descriptors if (descriptors.size != 1) { - throw CouldNotInitiateConnection("Expecting one descriptor, found: ${descriptors.size}") + throw ConnectException("Expecting one descriptor, found: ${descriptors.size}") } val descriptor = descriptors[0] descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE val wrote = gatt.writeDescriptor(descriptor) if (!wrote) { - throw CouldNotInitiateConnection("Could not enable indications on descriptor") + throw ConnectException("Could not enable indications on descriptor") } val confirmation = bleCommCallbacks.confirmWrite(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE, descriptor.uuid.toString(), DEFAULT_IO_TIMEOUT_MS) - if (confirmation is WriteConfirmationError) { - throw CouldNotInitiateConnection(confirmation.msg) + return when(confirmation) { + is WriteConfirmationError -> + throw ConnectException(confirmation.msg) + is WriteConfirmationSuccess -> + BleSendSuccess } - return BleSendSuccess } companion object { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt index 33e567faf4..0b88b00a97 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.scan import android.bluetooth.le.ScanRecord import android.bluetooth.le.ScanResult import android.os.ParcelUuid -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.DiscoveredInvalidPodException class BleDiscoveredDevice(val scanResult: ScanResult, private val scanRecord: ScanRecord, private val podId: Long) { 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/scan/DiscoveredInvalidPodException.kt similarity index 93% rename from omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/DiscoveredInvalidPodException.kt rename to omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/DiscoveredInvalidPodException.kt index 5606ceb3ad..1c5db479d7 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/scan/DiscoveredInvalidPodException.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.scan import android.os.ParcelUuid diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/PodScanner.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/PodScanner.kt index e9a436d17b..bbb8d720ab 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/PodScanner.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/PodScanner.kt @@ -8,7 +8,6 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ScanException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ScanFailFoundTooManyException -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ScanFailNotFoundException import java.util.* class PodScanner(private val logger: AAPSLogger, private val bluetoothAdapter: BluetoothAdapter) { @@ -32,7 +31,7 @@ class PodScanner(private val logger: AAPSLogger, private val bluetoothAdapter: B scanner.stopScan(scanCollector) val collected = scanCollector.collect() if (collected.isEmpty()) { - throw ScanFailNotFoundException() + throw ScanException("Not found") } else if (collected.size > 1) { throw ScanFailFoundTooManyException(collected) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt index 5087d32141..a6acdb4f40 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt @@ -4,7 +4,6 @@ import android.bluetooth.le.ScanCallback import android.bluetooth.le.ScanResult import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.DiscoveredInvalidPodException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ScanException import java.util.* import java.util.concurrent.ConcurrentHashMap diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt index 2e0cc1e365..8681d7c950 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt @@ -4,7 +4,6 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.Nonce -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotInitiateConnection import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.SessionEstablishmentException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessagePacket