Merge pull request #2554 from jbr7rr/dash-fix-race-condition-on-disconnect

DASH: Fix race condition on dash disconnect
This commit is contained in:
Milos Kozak 2023-06-01 14:31:42 +02:00 committed by GitHub
commit 29410ecb03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -96,7 +96,6 @@ class Connection(
connectionWaitCond.timeoutMs = newTimeout connectionWaitCond.timeoutMs = newTimeout
} }
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED
_connectionWaitCond = null
val discoverer = ServiceDiscoverer(aapsLogger, gatt, bleCommCallbacks, this) val discoverer = ServiceDiscoverer(aapsLogger, gatt, bleCommCallbacks, this)
val discovered = discoverer.discoverServices(connectionWaitCond) val discovered = discoverer.discoverServices(connectionWaitCond)
@ -122,21 +121,24 @@ class Connection(
cmdBleIO.hello() cmdBleIO.hello()
cmdBleIO.readyToRead() cmdBleIO.readyToRead()
dataBleIO.readyToRead() dataBleIO.readyToRead()
_connectionWaitCond = null
} }
@Synchronized @Synchronized
fun disconnect(closeGatt: Boolean) { fun disconnect(closeGatt: Boolean) {
aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting closeGatt=$closeGatt") aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting closeGatt=$closeGatt")
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED if (closeGatt == false && gattConnection != null) {
if (closeGatt) { // Disconnect first, then close gatt
gattConnection?.close()
gattConnection = null
} else {
gattConnection?.disconnect() gattConnection?.disconnect()
} else {
// Call with closeGatt=true only when ble is already disconnected or there is no connection
gattConnection?.close()
bleCommCallbacks.resetConnection()
gattConnection = null
session = null
msgIO = null
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
} }
bleCommCallbacks.resetConnection()
session = null
msgIO = null
} }
private fun waitForConnection(connectionWaitCond: ConnectionWaitCondition): ConnectionState { private fun waitForConnection(connectionWaitCond: ConnectionWaitCondition): ConnectionState {