simplify exceptions

This commit is contained in:
Andrei Vereha 2021-03-28 21:43:32 +02:00
parent 4b49392200
commit 8d2d38163a
14 changed files with 31 additions and 40 deletions

View file

@ -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<CharacteristicType, BluetoothGattCharacteristic> {
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

View file

@ -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{

View file

@ -1,3 +0,0 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
class CharacteristicNotFoundException(cmdCharacteristicUuid: String) : FailedToConnectException("characteristic not found: $cmdCharacteristicUuid")

View file

@ -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)

View file

@ -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)

View file

@ -1,3 +0,0 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
class CouldNotInitiateConnection(msg: String) : Exception(msg)

View file

@ -1,3 +0,0 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
class ScanFailNotFoundException : ScanException("No Pod found")

View file

@ -1,3 +0,0 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
class ServiceNotFoundException(serviceUuid: String) : FailedToConnectException("service not found: $serviceUuid")

View file

@ -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 {

View file

@ -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) {

View file

@ -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

View file

@ -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)
}

View file

@ -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

View file

@ -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