Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d38d71f00 | ||
|
|
b773242b02 | ||
|
|
d94701f564 |
No files matched your search
@@ -36,6 +36,8 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
|
||||
private var lizardOffSent = false
|
||||
private var reportedNone = false
|
||||
private var discoveries = 0
|
||||
private var subscribed = false
|
||||
private var linkChecks = 0
|
||||
private var outputSequence = 0L
|
||||
private var outputWritePending = false
|
||||
private var featureRead = 0L
|
||||
@@ -94,7 +96,7 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
|
||||
}
|
||||
val adapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager)?.adapter
|
||||
val bonded = runCatching { adapter?.takeIf(BluetoothAdapter::isEnabled)?.bondedDevices }
|
||||
.onFailure { Log.w(TAG, "paired devices unreadable", it) }
|
||||
.onFailure { if (!reportedNone) Log.w(TAG, "paired devices unreadable", it) }
|
||||
.getOrNull()
|
||||
val device = bonded?.firstOrNull(::isSteamController)
|
||||
if (device == null) {
|
||||
@@ -114,6 +116,8 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
|
||||
val g = gatt ?: return
|
||||
gatt = null
|
||||
discoveries = 0
|
||||
subscribed = false
|
||||
linkChecks = 0
|
||||
input = null
|
||||
lizardOffSent = false
|
||||
outputWritePending = false
|
||||
@@ -162,26 +166,18 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
|
||||
}
|
||||
|
||||
override fun onDescriptorWrite(g: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
|
||||
withSlot {
|
||||
if (g !== gatt || descriptor.characteristic.uuid != input) return
|
||||
Log.i(TAG, "subscribed to report 0x%02x (status %d)".format(reportId, status))
|
||||
connected = status == BluetoothGatt.GATT_SUCCESS
|
||||
FakeInputWriter.setTritonConnected(SLOT, connected)
|
||||
}
|
||||
listener?.invoke()
|
||||
if (connected) {
|
||||
refreshLizardMode()
|
||||
pollOutput()
|
||||
}
|
||||
if (g !== gatt || descriptor.characteristic.uuid != input) return
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) onSubscribed("status $status")
|
||||
else Log.w(TAG, "report 0x%02x subscription failed (status %d)".format(reportId, status))
|
||||
}
|
||||
|
||||
override fun onCharacteristicWrite(g: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
|
||||
if (g !== gatt) return
|
||||
if (characteristic.uuid == REPORT && !lizardOffSent) {
|
||||
lizardOffSent = true
|
||||
Log.i(TAG, "lizard mode off (status $status)")
|
||||
} else if (status != BluetoothGatt.GATT_SUCCESS) {
|
||||
if (status != BluetoothGatt.GATT_SUCCESS) {
|
||||
Log.w(TAG, "output report write failed for ${characteristic.uuid} (status $status)")
|
||||
} else if (characteristic.uuid == REPORT && !lizardOffSent) {
|
||||
lizardOffSent = true
|
||||
Log.i(TAG, "lizard mode off")
|
||||
}
|
||||
outputWritePending = false
|
||||
writeNextOutput()
|
||||
@@ -241,11 +237,40 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
|
||||
Log.i(TAG, "found Triton haptic reports: ${outputReports.keys.sorted().joinToString { "0x%02x".format(it) }}")
|
||||
input = chr.uuid
|
||||
g.setCharacteristicNotification(chr, true)
|
||||
val cccd = chr.getDescriptor(CCCD) ?: return disconnect("state report cannot notify")
|
||||
@Suppress("DEPRECATION")
|
||||
cccd.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
|
||||
@Suppress("DEPRECATION")
|
||||
g.writeDescriptor(cccd)
|
||||
watchLink(g, chr)
|
||||
}
|
||||
|
||||
private fun watchLink(g: BluetoothGatt, chr: BluetoothGattCharacteristic) {
|
||||
if (g !== gatt || lizardOffSent) return
|
||||
if (linkChecks++ == LINK_CHECKS) {
|
||||
disconnect(if (subscribed) "lizard mode off never acknowledged" else "report 0x%02x never subscribed".format(reportId))
|
||||
if (running) handler.postDelayed(::connect, RETRY_MS)
|
||||
return
|
||||
}
|
||||
if (!subscribed) {
|
||||
val cccd = chr.getDescriptor(CCCD) ?: return disconnect("state report cannot notify")
|
||||
@Suppress("DEPRECATION")
|
||||
cccd.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
|
||||
@Suppress("DEPRECATION")
|
||||
if (!g.writeDescriptor(cccd)) Log.w(TAG, "report 0x%02x subscription refused (attempt %d)".format(reportId, linkChecks))
|
||||
}
|
||||
handler.postDelayed({ watchLink(g, chr) }, LINK_CHECK_MS)
|
||||
}
|
||||
|
||||
private fun onSubscribed(how: String) {
|
||||
var fresh = false
|
||||
withSlot {
|
||||
if (subscribed || gatt == null) return@withSlot
|
||||
subscribed = true
|
||||
fresh = true
|
||||
Log.i(TAG, "subscribed to report 0x%02x (%s)".format(reportId, how))
|
||||
connected = true
|
||||
FakeInputWriter.setTritonConnected(SLOT, true)
|
||||
}
|
||||
if (!fresh) return
|
||||
listener?.invoke()
|
||||
refreshLizardMode()
|
||||
pollOutput()
|
||||
}
|
||||
|
||||
private fun refreshCache(g: BluetoothGatt) {
|
||||
@@ -261,11 +286,14 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
|
||||
handler.postDelayed(::refreshLizardMode, LIZARD_REFRESH_MS)
|
||||
}
|
||||
|
||||
private fun onState(data: ByteArray) = withSlot {
|
||||
if (data.size < STATE_BYTES) return@withSlot
|
||||
val timing = InputTiming.begin()
|
||||
FakeInputWriter.writeTritonState(SLOT, reportId, routedInput?.route(data) ?: data)
|
||||
InputTiming.end(timing)
|
||||
private fun onState(data: ByteArray) {
|
||||
withSlot {
|
||||
if (data.size < STATE_BYTES) return
|
||||
val timing = InputTiming.begin()
|
||||
FakeInputWriter.writeTritonState(SLOT, reportId, routedInput?.route(data) ?: data)
|
||||
InputTiming.end(timing)
|
||||
}
|
||||
if (!subscribed) onSubscribed("input before the subscription reply")
|
||||
}
|
||||
|
||||
private fun release() = synchronized(inputLock) {
|
||||
@@ -357,6 +385,8 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
|
||||
private const val SLOT = 0
|
||||
private const val RETRY_MS = 5_000L
|
||||
private const val LIZARD_REFRESH_MS = 3_000L
|
||||
private const val LINK_CHECK_MS = 1_000L
|
||||
private const val LINK_CHECKS = 3
|
||||
private const val OUTPUT_POLL_MS = 4L
|
||||
private const val OUTPUT_RETRY_MS = 10L
|
||||
private const val FEATURE_BYTES = 62
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.bluetooth.BluetoothGatt
|
||||
import android.bluetooth.BluetoothGattCallback
|
||||
import android.bluetooth.BluetoothGattCharacteristic
|
||||
import android.bluetooth.BluetoothGattDescriptor
|
||||
import android.bluetooth.BluetoothGattService
|
||||
import android.os.Handler
|
||||
import org.junit.After
|
||||
import org.junit.Assert.*
|
||||
@@ -18,11 +19,13 @@ import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.ShadowBluetoothGatt
|
||||
import java.time.Duration
|
||||
import java.util.UUID
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE, shadows = [SteamControllerBleTest.Writer::class])
|
||||
@Config(sdk = [34], manifest = Config.NONE, shadows = [SteamControllerBleTest.Writer::class, SteamControllerBleTest.BusyGatt::class])
|
||||
class SteamControllerBleTest {
|
||||
private val keys = ArrayList<Pair<Int, Boolean>>()
|
||||
private val router = InputRouter(object : InputOutput {
|
||||
@@ -153,15 +156,144 @@ class SteamControllerBleTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun unansweredSubscriptionDropsTheLink() {
|
||||
val link = ValveLink()
|
||||
link.start()
|
||||
assertEquals(1, link.shadow.descriptorWrites)
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(2_500))
|
||||
assertEquals(1, link.shadow.descriptorWrites)
|
||||
assertEquals(2, link.shadow.refused)
|
||||
assertSame(link.gatt, ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(1_000))
|
||||
assertNull(ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
assertFalse(SteamControllerBle.connected)
|
||||
assertEquals(0, link.shadow.lizardWrites)
|
||||
}
|
||||
|
||||
@Test fun failedSubscriptionIsWrittenAgain() {
|
||||
val link = ValveLink()
|
||||
link.start()
|
||||
link.answer(BluetoothGatt.GATT_FAILURE)
|
||||
assertFalse(SteamControllerBle.connected)
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(1_500))
|
||||
assertEquals(2, link.shadow.descriptorWrites)
|
||||
link.answer()
|
||||
assertTrue(SteamControllerBle.connected)
|
||||
assertEquals(1, link.shadow.lizardWrites)
|
||||
link.answer()
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(2_000))
|
||||
assertEquals(2, link.shadow.descriptorWrites)
|
||||
assertSame(link.gatt, ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
assertTrue(SteamControllerBle.connected)
|
||||
}
|
||||
|
||||
@Test fun inputBeforeTheSubscriptionReplyTurnsLizardModeOffOnceTheReplyArrives() {
|
||||
val link = ValveLink()
|
||||
link.start()
|
||||
link.input()
|
||||
assertTrue(SteamControllerBle.connected)
|
||||
assertTrue(Writer.connections.last())
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(500))
|
||||
assertEquals(0, link.shadow.lizardWrites)
|
||||
link.answer()
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(20))
|
||||
assertEquals(1, link.shadow.lizardWrites)
|
||||
link.answer()
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(3_000))
|
||||
assertEquals(1, link.shadow.descriptorWrites)
|
||||
assertSame(link.gatt, ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
assertTrue(SteamControllerBle.connected)
|
||||
}
|
||||
|
||||
@Test fun inputWithoutALizardModeReplyDropsTheLink() {
|
||||
val link = ValveLink()
|
||||
link.start()
|
||||
link.input()
|
||||
assertTrue(SteamControllerBle.connected)
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(2_500))
|
||||
assertEquals(1, link.shadow.descriptorWrites)
|
||||
assertEquals(0, link.shadow.lizardWrites)
|
||||
assertSame(link.gatt, ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(1_000))
|
||||
assertNull(ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
assertFalse(SteamControllerBle.connected)
|
||||
assertFalse(Writer.connections.last())
|
||||
}
|
||||
|
||||
@Test fun failedLizardModeWriteDropsTheLink() {
|
||||
val link = ValveLink()
|
||||
link.start()
|
||||
link.answer()
|
||||
assertEquals(1, link.shadow.lizardWrites)
|
||||
link.answer(BluetoothGatt.GATT_FAILURE)
|
||||
link.input()
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(2_500))
|
||||
assertSame(link.gatt, ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(1_000))
|
||||
assertNull(ReflectionHelpers.getField<Any?>(controller, "gatt"))
|
||||
assertFalse(SteamControllerBle.connected)
|
||||
assertFalse(Writer.connections.last())
|
||||
}
|
||||
|
||||
@Test fun answeredSubscriptionStartsOneLizardModeRefresh() {
|
||||
val link = ValveLink()
|
||||
link.start()
|
||||
link.answer()
|
||||
assertTrue(SteamControllerBle.connected)
|
||||
assertEquals(1, link.shadow.lizardWrites)
|
||||
link.answer()
|
||||
link.input()
|
||||
assertEquals(1, link.shadow.lizardWrites)
|
||||
shadowOf(handler.looper).idleFor(Duration.ofMillis(3_500))
|
||||
assertEquals(1, link.shadow.descriptorWrites)
|
||||
assertEquals(2, link.shadow.lizardWrites)
|
||||
}
|
||||
|
||||
private inner class ValveLink {
|
||||
val gatt: BluetoothGatt = ShadowBluetoothGatt.newInstance(
|
||||
BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:11:22:33:44:66"))
|
||||
val shadow = Shadow.extract<BusyGatt>(gatt)
|
||||
private val characteristic = BluetoothGattCharacteristic(INPUT, BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0).apply {
|
||||
addDescriptor(BluetoothGattDescriptor(CCCD, 0))
|
||||
}
|
||||
private val callback = ReflectionHelpers.getField<BluetoothGattCallback>(controller, "callback")
|
||||
|
||||
init {
|
||||
val service = BluetoothGattService(SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY)
|
||||
service.addCharacteristic(characteristic)
|
||||
service.addCharacteristic(BluetoothGattCharacteristic(REPORT,
|
||||
BluetoothGattCharacteristic.PROPERTY_WRITE or BluetoothGattCharacteristic.PROPERTY_READ, 0))
|
||||
shadow.addDiscoverableService(service)
|
||||
shadow.setGattCallback(object : BluetoothGattCallback() {})
|
||||
gatt.discoverServices()
|
||||
}
|
||||
|
||||
fun start() {
|
||||
ReflectionHelpers.setField(controller, "gatt", gatt)
|
||||
callback.onMtuChanged(gatt, 517, BluetoothGatt.GATT_SUCCESS)
|
||||
}
|
||||
|
||||
fun input() = callback.onCharacteristicChanged(gatt, characteristic, ByteArray(45))
|
||||
|
||||
fun answer(status: Int = BluetoothGatt.GATT_SUCCESS) {
|
||||
val op = shadow.pending
|
||||
shadow.pending = null
|
||||
when (op) {
|
||||
is BluetoothGattDescriptor -> callback.onDescriptorWrite(gatt, op, status)
|
||||
is BluetoothGattCharacteristic -> callback.onCharacteristicWrite(gatt, op, status)
|
||||
else -> fail("no write is waiting for a reply")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun subscribe(target: SteamControllerBle) {
|
||||
val device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:11:22:33:44:55")
|
||||
val gatt = ShadowBluetoothGatt.newInstance(device)
|
||||
val input = UUID.fromString("100F6C7A-1735-4313-B402-38567131E5F3")
|
||||
val characteristic = BluetoothGattCharacteristic(input, BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0)
|
||||
val descriptor = BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"), 0)
|
||||
val characteristic = BluetoothGattCharacteristic(INPUT, BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0)
|
||||
val descriptor = BluetoothGattDescriptor(CCCD, 0)
|
||||
characteristic.addDescriptor(descriptor)
|
||||
ReflectionHelpers.setField(target, "gatt", gatt)
|
||||
ReflectionHelpers.setField(target, "input", input)
|
||||
ReflectionHelpers.setField(target, "input", INPUT)
|
||||
ReflectionHelpers.setField(target, "reportId", 0x45)
|
||||
ReflectionHelpers.getField<BluetoothGattCallback>(target, "callback")
|
||||
.onDescriptorWrite(gatt, descriptor, BluetoothGatt.GATT_SUCCESS)
|
||||
@@ -170,6 +302,46 @@ class SteamControllerBleTest {
|
||||
private fun state(bytes: ByteArray, target: SteamControllerBle = controller) = ReflectionHelpers.callInstanceMethod<Unit>(target, "onState",
|
||||
ReflectionHelpers.ClassParameter.from(ByteArray::class.java, bytes))
|
||||
|
||||
private companion object {
|
||||
val SERVICE: UUID = UUID.fromString("100F6C32-1735-4313-B402-38567131E5F3")
|
||||
val INPUT: UUID = UUID.fromString("100F6C7A-1735-4313-B402-38567131E5F3")
|
||||
val REPORT: UUID = UUID.fromString("100F6C34-1735-4313-B402-38567131E5F3")
|
||||
val CCCD: UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
|
||||
}
|
||||
|
||||
@Implements(BluetoothGatt::class)
|
||||
class BusyGatt : ShadowBluetoothGatt() {
|
||||
var pending: Any? = null
|
||||
var descriptorWrites = 0
|
||||
var lizardWrites = 0
|
||||
var refused = 0
|
||||
|
||||
@Implementation
|
||||
override fun writeDescriptor(descriptor: BluetoothGattDescriptor): Boolean {
|
||||
if (!accept(descriptor)) return false
|
||||
descriptorWrites++
|
||||
return true
|
||||
}
|
||||
|
||||
@Implementation
|
||||
override fun writeCharacteristic(characteristic: BluetoothGattCharacteristic): Boolean {
|
||||
if (!accept(characteristic)) return false
|
||||
@Suppress("DEPRECATION")
|
||||
val value = characteristic.value
|
||||
if (characteristic.uuid == REPORT && value[0] == 0x87.toByte() && value[2] == 9.toByte()) lizardWrites++
|
||||
return true
|
||||
}
|
||||
|
||||
private fun accept(op: Any): Boolean {
|
||||
if (pending != null) {
|
||||
refused++
|
||||
return false
|
||||
}
|
||||
pending = op
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@Implements(FakeInputWriter::class)
|
||||
class Writer {
|
||||
companion object {
|
||||
|
||||
Reference in new issue
Block a user