This commit is contained in:
Andrei Vereha 2021-04-02 16:31:27 +02:00
parent 20179781ef
commit c4291113a6
7 changed files with 38 additions and 14 deletions

View file

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

View file

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

View file

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

View file

@ -10,8 +10,8 @@ class IncomingPackets {
fun byCharacteristicType(char: CharacteristicType): BlockingQueue<ByteArray> {
return when (char) {
CharacteristicType.DATA -> cmdQueue
CharacteristicType.CMD -> dataQueue
CharacteristicType.DATA -> dataQueue
CharacteristicType.CMD -> cmdQueue
}
}
}

View file

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

View file

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

View file

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