From 38a1d9c6bc2735f7e06d0c60ecf35b66d54f9731 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 28 Mar 2021 23:09:44 +0200 Subject: [PATCH] add BusyException --- .../driver/comm/OmnipodDashBleManagerImpl.kt | 75 ++++++++++++------- .../driver/comm/callbacks/BleCommCallbacks.kt | 1 - .../driver/comm/command/BleCommandType.kt | 1 - .../driver/comm/exceptions/BusyException.kt | 3 + .../comm/{message => packet}/PayloadJoiner.kt | 2 +- .../{message => packet}/PayloadSplitter.kt | 2 +- .../driver/comm/scan/BleDiscoveredDevice.kt | 1 - 7 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/BusyException.kt rename omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/{message => packet}/PayloadJoiner.kt (99%) rename omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/{message => packet}/PayloadSplitter.kt (99%) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt index 17bf919381..e7e5d3faa8 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt @@ -19,6 +19,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.Omn import info.nightscout.androidaps.utils.extensions.toHex import io.reactivex.Observable import java.util.concurrent.TimeoutException +import java.util.concurrent.atomic.AtomicBoolean import javax.inject.Inject import javax.inject.Singleton @@ -28,8 +29,7 @@ class OmnipodDashBleManagerImpl @Inject constructor( private val aapsLogger: AAPSLogger, private val podState: OmnipodDashPodStateManager ) : OmnipodDashBleManager { - - // TODO: add busy AtomicBoolean + private val busy = AtomicBoolean(false); private val bluetoothManager: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter @@ -41,38 +41,47 @@ class OmnipodDashBleManagerImpl @Inject constructor( ?: myId.increment() // pod not activated override fun sendCommand(cmd: Command): Observable = Observable.create { emitter -> - val conn = connection ?: throw NotConnectedException("Not connected") - - val session = conn.session ?: throw NotConnectedException("Missing session") - - emitter.onNext(PodEvent.CommandSending(cmd)) - - val sendResult = session.sendCommand(cmd) - when(sendResult) { - is CommandSendErrorSending -> { - emitter.tryOnError(CouldNotSendCommandException()) - return@create - } - is CommandSendSuccess -> - emitter.onNext(PodEvent.CommandSent(cmd)) - is CommandSendErrorConfirming -> - emitter.onNext(PodEvent.CommandSendNotConfirmed(cmd)) + if (!busy.compareAndSet(false, true)) { + throw BusyException() } + try { + val conn = connection ?: throw NotConnectedException("Not connected") - val readResult = session.readAndAckCommandResponse() - when (readResult){ - is CommandReceiveSuccess -> - emitter.onNext(PodEvent.ResponseReceived(readResult.result)) + val session = conn.session ?: throw NotConnectedException("Missing session") - is CommandAckError -> - emitter.onNext(PodEvent.ResponseReceived(readResult.result)) + emitter.onNext(PodEvent.CommandSending(cmd)) - is CommandReceiveError -> { - emitter.tryOnError(MessageIOException("Could not read response: $readResult")) - return@create + when (session.sendCommand(cmd)) { + is CommandSendErrorSending -> { + emitter.tryOnError(CouldNotSendCommandException()) + return@create + } + + is CommandSendSuccess -> + emitter.onNext(PodEvent.CommandSent(cmd)) + is CommandSendErrorConfirming -> + emitter.onNext(PodEvent.CommandSendNotConfirmed(cmd)) } + + when (val readResult = session.readAndAckCommandResponse()) { + is CommandReceiveSuccess -> + emitter.onNext(PodEvent.ResponseReceived(readResult.result)) + + is CommandAckError -> + emitter.onNext(PodEvent.ResponseReceived(readResult.result)) + + is CommandReceiveError -> { + emitter.tryOnError(MessageIOException("Could not read response: $readResult")) + return@create + } + } + emitter.onComplete() + } catch (ex: Exception) { + disconnect() + emitter.tryOnError(ex) + } finally { + busy.set(false) } - emitter.onComplete() } override fun getStatus(): ConnectionStatus { @@ -84,6 +93,9 @@ class OmnipodDashBleManagerImpl @Inject constructor( } override fun connect(): Observable = Observable.create { emitter -> + if (!busy.compareAndSet(false, true)) { + throw BusyException() + } try { emitter.onNext(PodEvent.BluetoothConnecting) @@ -110,6 +122,8 @@ class OmnipodDashBleManagerImpl @Inject constructor( } catch (ex: Exception) { disconnect() emitter.tryOnError(ex) + } finally { + busy.set(false) } } @@ -126,6 +140,9 @@ class OmnipodDashBleManagerImpl @Inject constructor( } override fun pairNewPod(): Observable = Observable.create { emitter -> + if (!busy.compareAndSet(false, true)) { + throw BusyException() + } try { if (podState.ltk != null) { @@ -170,6 +187,8 @@ class OmnipodDashBleManagerImpl @Inject constructor( } catch (ex: Exception) { disconnect() emitter.tryOnError(ex) + } finally { + busy.set(false) } } 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 b8a59c33ea..03a36cc612 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 @@ -46,7 +46,6 @@ class BleCommCallbacks( connected.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS) } catch (e: InterruptedException) { aapsLogger.warn(LTag.PUMPBTCOMM,"Interrupted while waiting for Connection") - } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt index 2a85db74d7..ae0873c15c 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt @@ -12,7 +12,6 @@ enum class BleCommandType(val value: Byte) { companion object { - @JvmStatic fun byValue(value: Byte): BleCommandType = values().firstOrNull { it.value == value } ?: throw IllegalArgumentException("Unknown BleCommandType: $value") diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/BusyException.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/BusyException.kt new file mode 100644 index 0000000000..0fdb5e84a9 --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/BusyException.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions + +class BusyException(): Exception("Bluetooth busy") \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadJoiner.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/packet/PayloadJoiner.kt similarity index 99% rename from omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadJoiner.kt rename to omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/packet/PayloadJoiner.kt index f1d1f23850..c9e699d30c 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadJoiner.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/packet/PayloadJoiner.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.CrcMismatchException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.IncorrectPacketException diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitter.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/packet/PayloadSplitter.kt similarity index 99% rename from omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitter.kt rename to omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/packet/PayloadSplitter.kt index fea2a9a20f..45c30ebc5d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitter.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/packet/PayloadSplitter.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet.BlePacket import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet.FirstBlePacket 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 0b88b00a97..07a207dad5 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 @@ -35,7 +35,6 @@ class BleDiscoveredDevice(val scanResult: ScanResult, private val scanRecord: Sc } @Throws(DiscoveredInvalidPodException::class) - private fun validatePodId() { val serviceUUIDs = scanRecord.serviceUuids val hexPodId = extractUUID16(serviceUUIDs[3]) + extractUUID16(serviceUUIDs[4])