Add bluetooth settings options to bandwidth benchmark

This commit is contained in:
James Babcock committed 2026-09-10 12:21:01 -07:00
1 parent 50cbc3a756
commit 5a97b218e6
4 files changed
+85 -7

No files matched your search

@@ -192,6 +192,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private int benchmarkMessageSize;
private int benchmarkWindowSize;
private int benchmarkDurationMs;
private int benchmarkLinkMode;
private boolean benchmarkLinkPending;
private long benchmarkReadyAtMs;
private long benchmarkStartAtMs; // first benchmark write; 0 until then
private long benchmarkDeadlineAtMs; // start + duration; MAX_VALUE until first write
private long benchmarkLastAckAtMs;
@@ -620,6 +623,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
* traffic already queued ahead of the run doesn't count against it.
*/
public boolean startBandwidthBenchmark(int messageSize, int windowSize, int durationMs) {
return startBandwidthBenchmarkWithLinkMode(messageSize, windowSize, durationMs, 0);
}
// 0: current link; 1: re-request HIGH; 2: request 2M; 3: both.
public boolean startBandwidthBenchmarkWithLinkMode(int messageSize, int windowSize,
int durationMs, int linkMode) {
synchronized (lock) {
if (benchmarkActive || !running || !sessionReady || !fixedLayoutCreated
|| shutdownRequested || chargingMode) {
@@ -633,6 +642,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
benchmarkMessageSize = payload.length;
benchmarkWindowSize = Math.max(1, Math.min(windowSize, BENCHMARK_MAX_WINDOW));
benchmarkDurationMs = Math.max(1_000, durationMs);
benchmarkLinkMode = linkMode & 3;
benchmarkLinkPending = true;
benchmarkReadyAtMs = Long.MAX_VALUE;
benchmarkStartAtMs = 0;
benchmarkDeadlineAtMs = Long.MAX_VALUE;
benchmarkLastAckAtMs = 0;
@@ -645,7 +657,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
benchmarkAborted = false;
benchmarkActive = true;
logLine("bandwidth benchmark start: size=" + benchmarkMessageSize
+ "B window=" + benchmarkWindowSize + " duration=" + benchmarkDurationMs + "ms");
+ "B window=" + benchmarkWindowSize + " duration=" + benchmarkDurationMs
+ "ms linkMode=" + benchmarkLinkMode);
}
interruptibleSleep.interrupt();
return true;
@@ -679,6 +692,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
status.put("state", state);
status.put("messageSize", benchmarkMessageSize);
status.put("windowSize", benchmarkWindowSize);
status.put("linkMode", benchmarkLinkMode);
status.put("elapsedMs", elapsed);
status.put("messagesSent", benchmarkMessagesSent);
status.put("messagesAcked", benchmarkMessagesAcked);
@@ -715,6 +729,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
finishBenchmarkLocked(true, "session no longer ready");
return;
}
if (benchmarkLinkPending || now < benchmarkReadyAtMs) {
return;
}
if (benchmarkAborted || now >= benchmarkDeadlineAtMs) {
// The run is over: drop queued-but-unsent no-ops (sending them
// would stretch the run past its deadline) and finish once the
@@ -2175,6 +2192,33 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private long driveSession() {
//Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")");
while (true) {
// Run on the sender thread, outside `lock`: Bluetooth API calls can
// wait on the global GATT lock, and callbacks need the session lock.
// Keep negotiation outside the timed stream. A fixed settling period
// is only an experiment boundary; HCI must confirm actual parameters.
int linkMode = -1;
String[] benchmarkAddresses = null;
synchronized (lock) {
if (benchmarkActive && benchmarkLinkPending) {
benchmarkLinkPending = false;
linkMode = benchmarkLinkMode;
benchmarkAddresses = new String[] {leftAddress, rightAddress};
}
}
if (benchmarkAddresses != null) {
for (String address : benchmarkAddresses) {
try {
bleManager.prepareBenchmarkLink(address, linkMode);
} catch (RuntimeException error) {
logLine("benchmark link request failed: " + safeMessage(error));
}
}
synchronized (lock) {
// Cancellation/new-run races leave the new run pending; its
// own preparation will replace this deadline before sending.
benchmarkReadyAtMs = SystemClock.elapsedRealtime() + 1_000;
}
}
OutboundMessage messageToWrite = null;
OutboundMessage messageToPrewrite = null;
long now = SystemClock.elapsedRealtime();
@@ -160,6 +160,23 @@ public class FaceclawBleManager {
}
}
/** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */
public void prepareBenchmarkLink(String address, int mode) {
synchronized (gattLock(address)) {
BluetoothGatt gatt = requireGatt(address);
if ((mode & 1) != 0) {
boolean accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
Log.i(TAG, "benchmark HIGH address=" + address + " submitted=" + accepted);
}
if ((mode & 2) != 0) {
Log.i(TAG, "benchmark request 2M address=" + address);
gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK,
BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED);
}
gatt.readPhy();
}
}
public boolean requestMtu(String address, int mtu, int timeoutMs) {
synchronized (gattLock(address)) {
CountDownLatch latch = new CountDownLatch(1);
@@ -451,7 +468,14 @@ public class FaceclawBleManager {
@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
Log.i(TAG, "onPhyRead: txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
Log.i(TAG, "onPhyRead: address=" + gatt.getDevice().getAddress()
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
}
@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
Log.i(TAG, "onPhyUpdate: address=" + gatt.getDevice().getAddress()
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
}
@Override