fixes
This commit is contained in:
parent
20179781ef
commit
c4291113a6
7 changed files with 38 additions and 14 deletions
|
@ -108,7 +108,14 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
||||||
?: Connection(podDevice, aapsLogger, context)
|
?: Connection(podDevice, aapsLogger, context)
|
||||||
connection = conn
|
connection = conn
|
||||||
if (conn.connectionState() is Connected) {
|
if (conn.connectionState() is Connected) {
|
||||||
|
if (conn.session == null) {
|
||||||
|
emitter.onNext(PodEvent.EstablishingSession)
|
||||||
|
establishSession(1.toByte())
|
||||||
emitter.onNext(PodEvent.Connected)
|
emitter.onNext(PodEvent.Connected)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
emitter.onNext(PodEvent.AlreadyConnected(podAddress))
|
||||||
|
}
|
||||||
emitter.onComplete()
|
emitter.onComplete()
|
||||||
return@create
|
return@create
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ class BleCommCallbacks(
|
||||||
) : BluetoothGattCallback() {
|
) : BluetoothGattCallback() {
|
||||||
|
|
||||||
private val serviceDiscoveryComplete: CountDownLatch = CountDownLatch(1)
|
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)
|
private val writeQueue: BlockingQueue<WriteConfirmation> = LinkedBlockingQueue(1)
|
||||||
|
|
||||||
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
||||||
|
@ -98,7 +98,10 @@ class BleCommCallbacks(
|
||||||
payload.toHex()
|
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) {
|
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
|
||||||
|
@ -148,6 +151,11 @@ class BleCommCallbacks(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun resetConnection() {
|
||||||
|
connected = CountDownLatch(1)
|
||||||
|
flushConfirmationQueue()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
private const val WRITE_CONFIRM_TIMEOUT_MS = 10 // the confirmation queue should be empty anyway
|
private const val WRITE_CONFIRM_TIMEOUT_MS = 10 // the confirmation queue should be empty anyway
|
||||||
|
|
|
@ -54,7 +54,7 @@ open class BleIO(
|
||||||
* @param payload the data to send
|
* @param payload the data to send
|
||||||
*/
|
*/
|
||||||
fun sendAndConfirmPacket(payload: ByteArray): BleSendResult {
|
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)
|
val set = characteristic.setValue(payload)
|
||||||
if (!set) {
|
if (!set) {
|
||||||
return BleSendErrorSending("Could set setValue on ${type.name}")
|
return BleSendErrorSending("Could set setValue on ${type.name}")
|
||||||
|
|
|
@ -10,8 +10,8 @@ class IncomingPackets {
|
||||||
|
|
||||||
fun byCharacteristicType(char: CharacteristicType): BlockingQueue<ByteArray> {
|
fun byCharacteristicType(char: CharacteristicType): BlockingQueue<ByteArray> {
|
||||||
return when (char) {
|
return when (char) {
|
||||||
CharacteristicType.DATA -> cmdQueue
|
CharacteristicType.DATA -> dataQueue
|
||||||
CharacteristicType.CMD -> dataQueue
|
CharacteristicType.CMD -> cmdQueue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,7 +103,8 @@ class MessageIO(
|
||||||
maxMessageReadTries = joiner.fullFragments * 2 + 2
|
maxMessageReadTries = joiner.fullFragments * 2 + 2
|
||||||
for (i in 1 until joiner.fullFragments + 1) {
|
for (i in 1 until joiner.fullFragments + 1) {
|
||||||
expected++
|
expected++
|
||||||
val packet = expectBlePacket(expected)
|
val nackOnTimeout = !joiner.oneExtraPacket && i==joiner.fullFragments // last packet
|
||||||
|
val packet = expectBlePacket(expected, nackOnTimeout)
|
||||||
if (packet !is PacketReceiveSuccess) {
|
if (packet !is PacketReceiveSuccess) {
|
||||||
aapsLogger.warn(LTag.PUMPBTCOMM, "Error reading packet:$packet")
|
aapsLogger.warn(LTag.PUMPBTCOMM, "Error reading packet:$packet")
|
||||||
return null
|
return null
|
||||||
|
@ -112,7 +113,7 @@ class MessageIO(
|
||||||
}
|
}
|
||||||
if (joiner.oneExtraPacket) {
|
if (joiner.oneExtraPacket) {
|
||||||
expected++
|
expected++
|
||||||
val packet = expectBlePacket(expected)
|
val packet = expectBlePacket(expected, true)
|
||||||
if (packet !is PacketReceiveSuccess) {
|
if (packet !is PacketReceiveSuccess) {
|
||||||
aapsLogger.warn(LTag.PUMPBTCOMM, "Error reading packet:$packet")
|
aapsLogger.warn(LTag.PUMPBTCOMM, "Error reading packet:$packet")
|
||||||
return null
|
return null
|
||||||
|
@ -186,7 +187,8 @@ class MessageIO(
|
||||||
if (received == null || received.isEmpty()) {
|
if (received == null || received.isEmpty()) {
|
||||||
if (nackOnTimeout)
|
if (nackOnTimeout)
|
||||||
cmdBleIO.sendAndConfirmPacket(BleCommandNack(index).data)
|
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
|
continue
|
||||||
}
|
}
|
||||||
if (received[0] == index) {
|
if (received[0] == index) {
|
||||||
|
|
|
@ -26,7 +26,7 @@ sealed class ConnectionState
|
||||||
object Connected : ConnectionState()
|
object Connected : ConnectionState()
|
||||||
object NotConnected : 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 incomingPackets = IncomingPackets()
|
||||||
private val bleCommCallbacks = BleCommCallbacks(aapsLogger, incomingPackets)
|
private val bleCommCallbacks = BleCommCallbacks(aapsLogger, incomingPackets)
|
||||||
|
@ -77,6 +77,9 @@ class Connection(val podDevice: BluetoothDevice, private val aapsLogger: AAPSLog
|
||||||
}
|
}
|
||||||
|
|
||||||
fun connect() {
|
fun connect() {
|
||||||
|
// forces reconnection
|
||||||
|
disconnect()
|
||||||
|
|
||||||
if (!gattConnection.connect()) {
|
if (!gattConnection.connect()) {
|
||||||
throw FailedToConnectException("connect() returned false")
|
throw FailedToConnectException("connect() returned false")
|
||||||
}
|
}
|
||||||
|
@ -86,10 +89,14 @@ class Connection(val podDevice: BluetoothDevice, private val aapsLogger: AAPSLog
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdBleIO.hello()
|
cmdBleIO.hello()
|
||||||
|
cmdBleIO.readyToRead()
|
||||||
|
dataBleIO.readyToRead()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnect() {
|
fun disconnect() {
|
||||||
|
bleCommCallbacks.resetConnection()
|
||||||
gattConnection.disconnect()
|
gattConnection.disconnect()
|
||||||
|
session = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun waitForConnection(): ConnectionState {
|
private fun waitForConnection(): ConnectionState {
|
||||||
|
|
|
@ -73,11 +73,11 @@ class Session(
|
||||||
var responseMsgPacket: MessagePacket? = null
|
var responseMsgPacket: MessagePacket? = null
|
||||||
for (i in 0..MAX_TRIES) {
|
for (i in 0..MAX_TRIES) {
|
||||||
val responseMsg = msgIO.receiveMessage()
|
val responseMsg = msgIO.receiveMessage()
|
||||||
if (responseMsg == null) {
|
if (responseMsg != null) {
|
||||||
aapsLogger.debug(LTag.PUMPBTCOMM, "Error receiving response: $responseMsg")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
responseMsgPacket = responseMsg
|
responseMsgPacket = responseMsg
|
||||||
|
break
|
||||||
|
}
|
||||||
|
aapsLogger.debug(LTag.PUMPBTCOMM, "Error receiving response: $responseMsg")
|
||||||
}
|
}
|
||||||
|
|
||||||
responseMsgPacket
|
responseMsgPacket
|
||||||
|
|
Loading…
Reference in a new issue