Add cross-platform BLE transport and Bluetooth settings
Integrate shared C transport with desktop and Android frontends, add radio probes and detailed Windows diagnostics, and fix GUI build dependencies and early configuration startup errors.
This commit is contained in:
1 parent
25cea8ca4c
commit
f5bac950d1
82 files changed
+6608
-146
No files matched your search
@@ -0,0 +1,115 @@
|
||||
package com.utun.bletest
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Instrumentation
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.os.SystemClock
|
||||
import com.utun.ble.*
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
/** Два физических телефона: Binder API, duplex, заполнение RX, возобновление кредита и отказ политики. */
|
||||
class BleApiProbe : Instrumentation() {
|
||||
private lateinit var args: Bundle
|
||||
override fun onCreate(arguments: Bundle) {
|
||||
super.onCreate(arguments)
|
||||
args = arguments
|
||||
start()
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
val result = Bundle()
|
||||
var resultCode = Activity.RESULT_OK
|
||||
var bound = false
|
||||
var api: BleBroadcastService.LocalBinder? = null
|
||||
val boundLatch = CountDownLatch(1)
|
||||
val connection = object : ServiceConnection {
|
||||
override fun onServiceConnected(name: ComponentName, service: IBinder) {
|
||||
api = service as BleBroadcastService.LocalBinder
|
||||
boundLatch.countDown()
|
||||
}
|
||||
override fun onServiceDisconnected(name: ComponentName) { api = null }
|
||||
}
|
||||
try {
|
||||
val peer = java.lang.Long.parseUnsignedLong(checkNotNull(args.getString("peer")), 16)
|
||||
val initiator = args.getString("initiator") == "true"
|
||||
val reject = args.getString("reject") == "true"
|
||||
val activity = startActivitySync(Intent(targetContext, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
|
||||
bound = targetContext.bindService(Intent(targetContext, BleBroadcastService::class.java), connection, Context.BIND_AUTO_CREATE)
|
||||
check(bound && boundLatch.await(10, TimeUnit.SECONDS)) { "Binder unavailable" }
|
||||
val service = checkNotNull(api)
|
||||
service.connectionSnapshots().forEach { service.disconnect(it.nodeId) }
|
||||
val validations = AtomicInteger()
|
||||
service.setConnectionValidator { request ->
|
||||
validations.incrementAndGet()
|
||||
!reject && request.localNodeId == service.nodeId() && request.remoteNodeId == peer
|
||||
}
|
||||
runOnMainSync { BleBroadcastService.start(targetContext, "API-probe".toByteArray(), 1000) }
|
||||
val deadline = SystemClock.elapsedRealtime() + 180_000
|
||||
fun await(test: () -> Boolean) {
|
||||
while (!test()) {
|
||||
check(SystemClock.elapsedRealtime() < deadline) { "Timeout: ${service.connectionSnapshots()}" }
|
||||
Thread.sleep(50)
|
||||
}
|
||||
}
|
||||
await { service.snapshot().running }
|
||||
runOnMainSync { activity.moveTaskToBack(true) }
|
||||
if (initiator) check(service.connect(peer) == ConnectResult.REQUESTED)
|
||||
if (reject) {
|
||||
await {
|
||||
if (initiator) service.connectionSnapshots().any { it.lastError.contains("Remote validation rejected") }
|
||||
else validations.get() > 0
|
||||
}
|
||||
if (!initiator) Thread.sleep(1500) // Дать REJECT indication дойти до инициатора до cleanup.
|
||||
check(service.connectionSnapshots().none { it.state == "READY" }) { "Rejected peer became READY" }
|
||||
result.putString("result", "PASS rejection validations=${validations.get()}")
|
||||
} else {
|
||||
await { service.connectionSnapshots().any { it.nodeId == peer && it.state == "READY" } }
|
||||
var sent = 0
|
||||
var received = 0
|
||||
var blockedAt = 0L
|
||||
var blocked = false
|
||||
while (sent < 70 || received < 70) {
|
||||
check(SystemClock.elapsedRealtime() < deadline) { "Duplex timeout sent=$sent received=$received ${service.connectionSnapshots()}" }
|
||||
val stats = service.connectionSnapshots().single { it.nodeId == peer }
|
||||
check(stats.state == "READY") { "Disconnected during transfer: $stats" }
|
||||
if (sent < 70 && stats.txQueued < 64) {
|
||||
check(service.sendTo(peer, payload(sent)) == NodeSendResult.QUEUED)
|
||||
sent++
|
||||
}
|
||||
if (stats.rxBlocked && !blocked) {
|
||||
blocked = true
|
||||
blockedAt = SystemClock.elapsedRealtime()
|
||||
check(stats.rxQueued == 64) { "RX exceeded/undershot capacity: $stats" }
|
||||
}
|
||||
if (blocked && SystemClock.elapsedRealtime() - blockedAt >= 1000) {
|
||||
service.drainNodeReceived(peer).forEach { data ->
|
||||
check(data.contentEquals(payload(received))) { "Corrupt/out of order message=$received bytes=${data.size}" }
|
||||
received++
|
||||
}
|
||||
}
|
||||
Thread.sleep(10)
|
||||
}
|
||||
await { service.connectionSnapshots().single { it.nodeId == peer }.sent == 70L }
|
||||
check(blocked) { "RX backpressure was not exercised" }
|
||||
result.putString("result", "PASS duplex sent=$sent received=$received rxBlocked=$blocked ${service.connectionSnapshots()}")
|
||||
}
|
||||
} catch (error: Throwable) {
|
||||
resultCode = Activity.RESULT_CANCELED
|
||||
result.putString("result", "FAIL ${error.stackTraceToString()}")
|
||||
} finally {
|
||||
api?.connectionSnapshots()?.forEach { api?.disconnect(it.nodeId) }
|
||||
runOnMainSync { BleBroadcastService.stop(targetContext) }
|
||||
if (bound) targetContext.unbindService(connection)
|
||||
}
|
||||
finish(resultCode, result)
|
||||
}
|
||||
|
||||
private fun payload(index: Int): ByteArray = ByteArray(if (index == 69) 4096 else 512) { ((it * 31 + index) and 0xff).toByte() }
|
||||
}
|
||||
Reference in new issue
Block a user