diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index 5d8aae9c..a7c91667 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -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(); diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java index 09f2fe55..56f4f729 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java @@ -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 diff --git a/app/apps/developer/bandwidth-benchmark.ts b/app/apps/developer/bandwidth-benchmark.ts index 0ca29295..8af80ceb 100644 --- a/app/apps/developer/bandwidth-benchmark.ts +++ b/app/apps/developer/bandwidth-benchmark.ts @@ -13,6 +13,7 @@ import { const MESSAGE_SIZES = [250, 500, 1000, 2000, 3800]; const WINDOW_SIZES = [1, 2, 3, 4, 6]; +const LINK_MODES = ["Current", "Request HIGH", "Request 2M", "HIGH + 2M"]; const DURATION_MS = 15_000; // After painting the "running" screen, wait for that frame to reach the // glasses before the benchmark blocks normal image sends — otherwise the @@ -37,9 +38,10 @@ type Phase = "config" | "arming" | "running" | "done"; */ export class BandwidthBenchmarkLayer implements Layer { private phase: Phase = "config"; - private selectedRow = 0; // 0 = message size, 1 = window size, 2 = start + private selectedRow = 0; // size, window, link preference, start private sizeIndex = 2; private windowIndex = 2; + private linkModeIndex = 0; private result: BandwidthBenchmarkStatus | null = null; private error: string | null = null; private armTimer: ReturnType | null = null; @@ -77,6 +79,7 @@ export class BandwidthBenchmarkLayer implements Layer { MESSAGE_SIZES[this.sizeIndex]!, WINDOW_SIZES[this.windowIndex]!, DURATION_MS, + this.linkModeIndex, ); if (!started) { this.phase = "config"; @@ -123,7 +126,7 @@ export class BandwidthBenchmarkLayer implements Layer { font, LIST_X, HEADER_HEIGHT + 30, - `${MESSAGE_SIZES[this.sizeIndex]} B messages, window ${WINDOW_SIZES[this.windowIndex]}`, + `${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}, ${LINK_MODES[this.linkModeIndex]}`, 150, ); image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110); @@ -134,6 +137,7 @@ export class BandwidthBenchmarkLayer implements Layer { const rows: Array<[string, string]> = [ ["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`], ["Window size", `${WINDOW_SIZES[this.windowIndex]}`], + ["Link request", LINK_MODES[this.linkModeIndex]!], [this.phase === "done" ? "Run again" : "Start", ""], ]; for (let index = 0; index < rows.length; index++) { @@ -161,6 +165,7 @@ export class BandwidthBenchmarkLayer implements Layer { `Throughput: ${(payloadRate / 1024).toFixed(2)} KB/s payload (${(wireRate / 1024).toFixed(2)} KB/s wire)`, `Messages: ${r.messagesAcked}/${r.messagesSent} acked, ${r.timeouts} timeouts`, `Elapsed: ${seconds.toFixed(1)} s` + (r.aborted ? " (aborted early)" : ""), + `Link request: ${LINK_MODES[r.linkMode] ?? "Current"}`, ]; for (const line of lines) { image.drawText(font, LIST_X, y, line, 190); @@ -184,13 +189,15 @@ export class BandwidthBenchmarkLayer implements Layer { this.selectedRow = Math.max(0, this.selectedRow - 1); return; case "scroll-down": - this.selectedRow = Math.min(2, this.selectedRow + 1); + this.selectedRow = Math.min(3, this.selectedRow + 1); return; case "click": if (this.selectedRow === 0) { this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length; } else if (this.selectedRow === 1) { this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length; + } else if (this.selectedRow === 2) { + this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length; } else { this.startRun(); } diff --git a/app/native/bandwidth-benchmark.ts b/app/native/bandwidth-benchmark.ts index fbd99dfb..c480c7d3 100644 --- a/app/native/bandwidth-benchmark.ts +++ b/app/native/bandwidth-benchmark.ts @@ -14,6 +14,7 @@ export type BandwidthBenchmarkStatus = { state: "idle" | "starting" | "running" | "done"; messageSize: number; windowSize: number; + linkMode: number; elapsedMs: number; messagesSent: number; messagesAcked: number; @@ -33,12 +34,14 @@ function activeCommunicator(): any { } /** Start a run. False if not connected or a run is already active. */ -export function startBandwidthBenchmark(messageSize: number, windowSize: number, durationMs: number): boolean { +export function startBandwidthBenchmark(messageSize: number, windowSize: number, durationMs: number, linkMode = 0): boolean { const active = activeCommunicator(); if (!active) return false; try { return Boolean( - active.startBandwidthBenchmark(Math.round(messageSize), Math.round(windowSize), Math.round(durationMs)), + active.startBandwidthBenchmarkWithLinkMode( + Math.round(messageSize), Math.round(windowSize), Math.round(durationMs), Math.round(linkMode), + ), ); } catch (error) { console.warn(`startBandwidthBenchmark failed: ${error}`);