simplify exceptions
This commit is contained in:
parent
4b49392200
commit
8d2d38163a
14 changed files with 31 additions and 40 deletions
|
@ -5,8 +5,7 @@ import android.bluetooth.BluetoothGattCharacteristic
|
||||||
import info.nightscout.androidaps.logging.AAPSLogger
|
import info.nightscout.androidaps.logging.AAPSLogger
|
||||||
import info.nightscout.androidaps.logging.LTag
|
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.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.ConnectException
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ServiceNotFoundException
|
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.CharacteristicType
|
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.CharacteristicType
|
||||||
import java.math.BigInteger
|
import java.math.BigInteger
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -20,18 +19,20 @@ class ServiceDiscoverer(
|
||||||
/***
|
/***
|
||||||
* This is first step after connection establishment
|
* This is first step after connection establishment
|
||||||
*/
|
*/
|
||||||
@Throws(InterruptedException::class, ServiceNotFoundException::class, CharacteristicNotFoundException::class)
|
|
||||||
fun discoverServices(): Map<CharacteristicType, BluetoothGattCharacteristic> {
|
fun discoverServices(): Map<CharacteristicType, BluetoothGattCharacteristic> {
|
||||||
logger.debug(LTag.PUMPBTCOMM, "Discovering services")
|
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)
|
bleCallbacks.waitForServiceDiscovery(DISCOVER_SERVICES_TIMEOUT_MS)
|
||||||
logger.debug(LTag.PUMPBTCOMM, "Services discovered")
|
logger.debug(LTag.PUMPBTCOMM, "Services discovered")
|
||||||
val service = gatt.getService(SERVICE_UUID.toUuid())
|
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)
|
val cmdChar = service.getCharacteristic(CharacteristicType.CMD.uuid)
|
||||||
?: throw CharacteristicNotFoundException(CharacteristicType.CMD.value)
|
?: throw ConnectException("Characteristic not found: ${CharacteristicType.CMD.value}")
|
||||||
val dataChar = service.getCharacteristic(CharacteristicType.DATA.uuid) // TODO: this is never used
|
val dataChar = service.getCharacteristic(CharacteristicType.DATA.uuid)
|
||||||
?: throw CharacteristicNotFoundException(CharacteristicType.DATA.value)
|
?: throw ConnectException("Characteristic not found: ${CharacteristicType.DATA.value}")
|
||||||
var chars = mapOf(
|
var chars = mapOf(
|
||||||
CharacteristicType.CMD to cmdChar,
|
CharacteristicType.CMD to cmdChar,
|
||||||
CharacteristicType.DATA to dataChar
|
CharacteristicType.DATA to dataChar
|
||||||
|
|
|
@ -41,14 +41,21 @@ class BleCommCallbacks(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(InterruptedException::class)
|
|
||||||
fun waitForConnection(timeoutMs: Int) {
|
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) {
|
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{
|
fun confirmWrite(expectedPayload: ByteArray, expectedUUID: String, timeoutMs: Long) : WriteConfirmation{
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
|
||||||
|
|
||||||
class CharacteristicNotFoundException(cmdCharacteristicUuid: String) : FailedToConnectException("characteristic not found: $cmdCharacteristicUuid")
|
|
|
@ -1,3 +1,3 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
||||||
|
|
||||||
class DescriptorNotFoundException : Exception()
|
class ConnectException(val msg: String) : Exception(msg)
|
|
@ -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)
|
|
|
@ -1,3 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
|
||||||
|
|
||||||
class CouldNotInitiateConnection(msg: String) : Exception(msg)
|
|
|
@ -1,3 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
|
||||||
|
|
||||||
class ScanFailNotFoundException : ScanException("No Pod found")
|
|
|
@ -1,3 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
|
||||||
|
|
||||||
class ServiceNotFoundException(serviceUuid: String) : FailedToConnectException("service not found: $serviceUuid")
|
|
|
@ -99,25 +99,27 @@ abstract class BleIO(
|
||||||
fun readyToRead(): BleSendResult {
|
fun readyToRead(): BleSendResult {
|
||||||
val notificationSet = gatt.setCharacteristicNotification(characteristic, true)
|
val notificationSet = gatt.setCharacteristicNotification(characteristic, true)
|
||||||
if (!notificationSet) {
|
if (!notificationSet) {
|
||||||
throw CouldNotInitiateConnection("Could not enable notifications")
|
throw ConnectException("Could not enable notifications")
|
||||||
}
|
}
|
||||||
val descriptors = characteristic.descriptors
|
val descriptors = characteristic.descriptors
|
||||||
if (descriptors.size != 1) {
|
if (descriptors.size != 1) {
|
||||||
throw CouldNotInitiateConnection("Expecting one descriptor, found: ${descriptors.size}")
|
throw ConnectException("Expecting one descriptor, found: ${descriptors.size}")
|
||||||
}
|
}
|
||||||
val descriptor = descriptors[0]
|
val descriptor = descriptors[0]
|
||||||
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
|
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
|
||||||
val wrote = gatt.writeDescriptor(descriptor)
|
val wrote = gatt.writeDescriptor(descriptor)
|
||||||
if (!wrote) {
|
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,
|
val confirmation = bleCommCallbacks.confirmWrite(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE,
|
||||||
descriptor.uuid.toString(),
|
descriptor.uuid.toString(),
|
||||||
DEFAULT_IO_TIMEOUT_MS)
|
DEFAULT_IO_TIMEOUT_MS)
|
||||||
if (confirmation is WriteConfirmationError) {
|
return when(confirmation) {
|
||||||
throw CouldNotInitiateConnection(confirmation.msg)
|
is WriteConfirmationError ->
|
||||||
|
throw ConnectException(confirmation.msg)
|
||||||
|
is WriteConfirmationSuccess ->
|
||||||
|
BleSendSuccess
|
||||||
}
|
}
|
||||||
return BleSendSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.scan
|
||||||
import android.bluetooth.le.ScanRecord
|
import android.bluetooth.le.ScanRecord
|
||||||
import android.bluetooth.le.ScanResult
|
import android.bluetooth.le.ScanResult
|
||||||
import android.os.ParcelUuid
|
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) {
|
class BleDiscoveredDevice(val scanResult: ScanResult, private val scanRecord: ScanRecord, private val podId: Long) {
|
||||||
|
|
||||||
|
|
|
@ -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
|
import android.os.ParcelUuid
|
||||||
|
|
|
@ -8,7 +8,6 @@ import info.nightscout.androidaps.logging.AAPSLogger
|
||||||
import info.nightscout.androidaps.logging.LTag
|
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.ScanException
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ScanFailFoundTooManyException
|
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.*
|
import java.util.*
|
||||||
|
|
||||||
class PodScanner(private val logger: AAPSLogger, private val bluetoothAdapter: BluetoothAdapter) {
|
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)
|
scanner.stopScan(scanCollector)
|
||||||
val collected = scanCollector.collect()
|
val collected = scanCollector.collect()
|
||||||
if (collected.isEmpty()) {
|
if (collected.isEmpty()) {
|
||||||
throw ScanFailNotFoundException()
|
throw ScanException("Not found")
|
||||||
} else if (collected.size > 1) {
|
} else if (collected.size > 1) {
|
||||||
throw ScanFailFoundTooManyException(collected)
|
throw ScanFailFoundTooManyException(collected)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import android.bluetooth.le.ScanCallback
|
||||||
import android.bluetooth.le.ScanResult
|
import android.bluetooth.le.ScanResult
|
||||||
import info.nightscout.androidaps.logging.AAPSLogger
|
import info.nightscout.androidaps.logging.AAPSLogger
|
||||||
import info.nightscout.androidaps.logging.LTag
|
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 info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ScanException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
|
@ -4,7 +4,6 @@ import info.nightscout.androidaps.logging.AAPSLogger
|
||||||
import info.nightscout.androidaps.logging.LTag
|
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.Id
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.Nonce
|
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.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.MessageIO
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessagePacket
|
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessagePacket
|
||||||
|
|
Loading…
Reference in a new issue