From e6abd9d1a5ae9b51b7e47ea293bf2e3bbc98ed8c Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 24 May 2020 11:01:13 -0700 Subject: [PATCH] autobugs - some phones send bougs onMtuChanged when we are not setting mtu --- .../geeksville/mesh/service/SafeBluetooth.kt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt index d4a67a29..f4430630 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -263,7 +263,10 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) { // Alas, passing back an Int mtu isn't working and since I don't really care what MTU // the device was willing to let us have I'm just punting and returning Unit - completeWork(status, Unit) + if (isSettingMtu) + completeWork(status, Unit) + else + errormsg("Ignoring bogus onMtuChanged") } /** @@ -344,6 +347,8 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD } } + isSettingMtu = + false // Most work is not doing MTU stuff, the work that is will re set this flag logAssert(newWork.startWork()) } } @@ -395,7 +400,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD synchronized(workQueue) { val w = currentWork - ?: throw Exception("currentWork was null") // will throw if null, which is helpful (FIXME - throws in the field) + ?: throw Exception("currentWork was null") // will throw if null, which is helpful (FIXME - throws in the field - because of a bogus mtu completion gatt call) stopCurrentWork() // We are now no longer working on anything startNewWork() @@ -525,13 +530,21 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD fun discoverServices() = makeSync { queueDiscoverServices(it) } + /** + * On some phones we receive bogus mtu gatt callbacks, we need to ignore them if we weren't setting the mtu + */ + private var isSettingMtu = false + /** * mtu operations seem to hang sometimes. To cope with this we have a 5 second timeout before throwing an exception and cancelling the work */ private fun queueRequestMtu( len: Int, cont: Continuation - ) = queueWork("reqMtu", cont, 5 * 1000) { gatt!!.requestMtu(len) } + ) = queueWork("reqMtu", cont, 5 * 1000) { + isSettingMtu = true + gatt!!.requestMtu(len) + } fun asyncRequestMtu( len: Int,