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 8567288348..e56fc13b53 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 @@ -108,7 +108,14 @@ class OmnipodDashBleManagerImpl @Inject constructor( ?: Connection(podDevice, aapsLogger, context) connection = conn if (conn.connectionState() is Connected) { - emitter.onNext(PodEvent.Connected) + if (conn.session == null) { + emitter.onNext(PodEvent.EstablishingSession) + establishSession(1.toByte()) + emitter.onNext(PodEvent.Connected) + + } else { + emitter.onNext(PodEvent.AlreadyConnected(podAddress)) + } emitter.onComplete() return@create } 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 2d399d9a07..0b613e1a93 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 @@ -22,7 +22,7 @@ class BleCommCallbacks( ) : BluetoothGattCallback() { private val serviceDiscoveryComplete: CountDownLatch = CountDownLatch(1) - private val connected: CountDownLatch = CountDownLatch(1) + private var connected: CountDownLatch = CountDownLatch(1) private val writeQueue: BlockingQueue = LinkedBlockingQueue(1) override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { @@ -98,7 +98,10 @@ class BleCommCallbacks( payload.toHex() ) - incomingPackets.byCharacteristicType(characteristicType).add(payload) + val insertResult = incomingPackets.byCharacteristicType(characteristicType).add(payload) + if (!insertResult) { + aapsLogger.warn(LTag.PUMPBTCOMM, "Could not insert read data to the incoming queue: ${characteristicType}") + } } override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) { @@ -148,6 +151,11 @@ class BleCommCallbacks( } } + fun resetConnection() { + connected = CountDownLatch(1) + flushConfirmationQueue() + } + companion object { private const val WRITE_CONFIRM_TIMEOUT_MS = 10 // the confirmation queue should be empty anyway 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 aa8f1f372b..fc7e1d3645 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 @@ -54,7 +54,7 @@ open class BleIO( * @param payload the data to send */ fun sendAndConfirmPacket(payload: ByteArray): BleSendResult { - aapsLogger.debug(LTag.PUMPBTCOMM, "BleIO: Sending data on ${payload.toHex()}") + aapsLogger.debug(LTag.PUMPBTCOMM, "BleIO: Sending on ${type.name}: ${payload.toHex()}") val set = characteristic.setValue(payload) if (!set) { return BleSendErrorSending("Could set setValue on ${type.name}") diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/IncomingPackets.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/IncomingPackets.kt index c18c68cc42..ae8bb034fe 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/IncomingPackets.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/io/IncomingPackets.kt @@ -10,8 +10,8 @@ class IncomingPackets { fun byCharacteristicType(char: CharacteristicType): BlockingQueue { return when (char) { - CharacteristicType.DATA -> cmdQueue - CharacteristicType.CMD -> dataQueue + CharacteristicType.DATA -> dataQueue + CharacteristicType.CMD -> cmdQueue } } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt index cd3b65c16b..730d6cb7a4 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessageIO.kt @@ -103,7 +103,8 @@ class MessageIO( maxMessageReadTries = joiner.fullFragments * 2 + 2 for (i in 1 until joiner.fullFragments + 1) { expected++ - val packet = expectBlePacket(expected) + val nackOnTimeout = !joiner.oneExtraPacket && i==joiner.fullFragments // last packet + val packet = expectBlePacket(expected, nackOnTimeout) if (packet !is PacketReceiveSuccess) { aapsLogger.warn(LTag.PUMPBTCOMM, "Error reading packet:$packet") return null @@ -112,7 +113,7 @@ class MessageIO( } if (joiner.oneExtraPacket) { expected++ - val packet = expectBlePacket(expected) + val packet = expectBlePacket(expected, true) if (packet !is PacketReceiveSuccess) { aapsLogger.warn(LTag.PUMPBTCOMM, "Error reading packet:$packet") return null @@ -186,7 +187,8 @@ class MessageIO( if (received == null || received.isEmpty()) { if (nackOnTimeout) cmdBleIO.sendAndConfirmPacket(BleCommandNack(index).data) - aapsLogger.info(LTag.PUMPBTCOMM, "Error reading index: $index. Received: $received") + aapsLogger.info(LTag.PUMPBTCOMM, "Error reading index: $index. Received: $received. NackOnTimeout: " + + "$nackOnTimeout") continue } if (received[0] == index) { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt index d3d8d1a27b..463be77b1a 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt @@ -26,7 +26,7 @@ sealed class ConnectionState object Connected : ConnectionState() object NotConnected : ConnectionState() -class Connection(val podDevice: BluetoothDevice, private val aapsLogger: AAPSLogger, private val context: Context) { +class Connection(val podDevice: BluetoothDevice, private val aapsLogger: AAPSLogger, context: Context) { private val incomingPackets = IncomingPackets() private val bleCommCallbacks = BleCommCallbacks(aapsLogger, incomingPackets) @@ -77,6 +77,9 @@ class Connection(val podDevice: BluetoothDevice, private val aapsLogger: AAPSLog } fun connect() { + // forces reconnection + disconnect() + if (!gattConnection.connect()) { throw FailedToConnectException("connect() returned false") } @@ -86,10 +89,14 @@ class Connection(val podDevice: BluetoothDevice, private val aapsLogger: AAPSLog } cmdBleIO.hello() + cmdBleIO.readyToRead() + dataBleIO.readyToRead() } fun disconnect() { + bleCommCallbacks.resetConnection() gattConnection.disconnect() + session = null } private fun waitForConnection(): ConnectionState { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt index a649f98c53..59b7fd825d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt @@ -73,11 +73,11 @@ class Session( var responseMsgPacket: MessagePacket? = null for (i in 0..MAX_TRIES) { val responseMsg = msgIO.receiveMessage() - if (responseMsg == null) { - aapsLogger.debug(LTag.PUMPBTCOMM, "Error receiving response: $responseMsg") - continue + if (responseMsg != null) { + responseMsgPacket = responseMsg + break } - responseMsgPacket = responseMsg + aapsLogger.debug(LTag.PUMPBTCOMM, "Error receiving response: $responseMsg") } responseMsgPacket