forked from Blendman974/faceclaw
Refactor message construction
This commit is contained in:
1 parent
af27ed6383
commit
831e927dc8
4 files changed
+240
-212
No files matched your search
+41
-196
@@ -38,7 +38,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
private static final int WRITE_TIMEOUT_MS = 2_000;
|
private static final int WRITE_TIMEOUT_MS = 2_000;
|
||||||
private static final int PRELUDE_TIMEOUT_MS = 2_000;
|
private static final int PRELUDE_TIMEOUT_MS = 2_000;
|
||||||
private static final int ACK_TIMEOUT_MS = 3_500;
|
private static final int ACK_TIMEOUT_MS = 3_500;
|
||||||
private static final int HEARTBEAT_TIMEOUT_MS = 1_500;
|
|
||||||
private static final int HEARTBEAT_FAILURE_DEADLINE_MS = 10_000;
|
private static final int HEARTBEAT_FAILURE_DEADLINE_MS = 10_000;
|
||||||
private static final int HEARTBEAT_READY_MS = 4_000;
|
private static final int HEARTBEAT_READY_MS = 4_000;
|
||||||
private static final int HEARTBEAT_URGENT_MS = 6_000;
|
private static final int HEARTBEAT_URGENT_MS = 6_000;
|
||||||
@@ -48,7 +47,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
private static final int IMAGE_FRAGMENT_SIZE = 3800;
|
private static final int IMAGE_FRAGMENT_SIZE = 3800;
|
||||||
private static final int IMAGE_RETRY_DELAY_MS = 10_000;
|
private static final int IMAGE_RETRY_DELAY_MS = 10_000;
|
||||||
private static final boolean IMAGE_FRAGMENT_NO_ACK = false;
|
private static final boolean IMAGE_FRAGMENT_NO_ACK = false;
|
||||||
private static final int WARMUP_FRAGMENT_TIMEOUT_MS = 3_000;
|
|
||||||
private static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8;
|
private static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8;
|
||||||
private static final int EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000;
|
private static final int EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000;
|
||||||
private static final int IDLE_SLEEP_MS = 100;
|
private static final int IDLE_SLEEP_MS = 100;
|
||||||
@@ -100,7 +98,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
private int lastAudioControlAckMagic = 0;
|
private int lastAudioControlAckMagic = 0;
|
||||||
|
|
||||||
private final BleMagicPool magicPool = new BleMagicPool();
|
private final BleMagicPool magicPool = new BleMagicPool();
|
||||||
private int nextTransportSeq = 0x40;
|
private MessageBuilder messageBuilder = new MessageBuilder(magicPool);
|
||||||
private int nextMapSessionId = 0;
|
private int nextMapSessionId = 0;
|
||||||
private int nextImageUpdateId = 1;
|
private int nextImageUpdateId = 1;
|
||||||
private int lastShutdownAckMagic = 0;
|
private int lastShutdownAckMagic = 0;
|
||||||
@@ -112,7 +110,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
|
|
||||||
private String displayedFingerprint = "";
|
private String displayedFingerprint = "";
|
||||||
private byte[][] displayedTileBmps = emptyTileSet();
|
private byte[][] displayedTileBmps = emptyTileSet();
|
||||||
private final Map<Integer, ImageUpdateStats> imageUpdateStats = new HashMap<>();
|
private final Map<Integer, BleImageOptimizer.ImageUpdateStats> imageUpdateStats = new HashMap<>();
|
||||||
|
|
||||||
private final Object desiredTilesLock = new Object();
|
private final Object desiredTilesLock = new Object();
|
||||||
private String desiredFingerprint = "";
|
private String desiredFingerprint = "";
|
||||||
@@ -201,8 +199,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
audioPacketListener = listener;
|
audioPacketListener = listener;
|
||||||
magic = magicPool.allocate();
|
OutboundMessage message = createAudioControlMessageLocked(true);
|
||||||
pendingMessages.addFirst(createAudioControlMessageLocked(true, magic));
|
magic = message.magic;
|
||||||
|
pendingMessages.addFirst(message);
|
||||||
logLine("queue G2 mic enable");
|
logLine("queue G2 mic enable");
|
||||||
lock.notifyAll();
|
lock.notifyAll();
|
||||||
}
|
}
|
||||||
@@ -217,8 +216,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
audioCaptureActive = false;
|
audioCaptureActive = false;
|
||||||
clearMessagesOfKindLocked("audio-control");
|
clearMessagesOfKindLocked("audio-control");
|
||||||
if (running && sessionReady) {
|
if (running && sessionReady) {
|
||||||
magic = magicPool.allocate();
|
OutboundMessage message = createAudioControlMessageLocked(false);
|
||||||
pendingMessages.addFirst(createAudioControlMessageLocked(false, magic));
|
magic = message.magic;
|
||||||
|
pendingMessages.addFirst(message);
|
||||||
logLine("queue G2 mic disable");
|
logLine("queue G2 mic disable");
|
||||||
lock.notifyAll();
|
lock.notifyAll();
|
||||||
}
|
}
|
||||||
@@ -279,23 +279,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
shutdownRequested = true;
|
shutdownRequested = true;
|
||||||
clearPendingMessagesLocked("shutdown requested");
|
clearPendingMessagesLocked("shutdown requested");
|
||||||
magic = magicPool.allocate();
|
OutboundMessage message = messageBuilder.shutdown(exitMode);
|
||||||
OutboundMessage message = new OutboundMessage(
|
magic = message.magic;
|
||||||
"shutdown",
|
|
||||||
"shutdown mode=" + exitMode,
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(
|
|
||||||
BleProtocol.buildShutdown(magic, exitMode),
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
BleProtocol.FLAG_REQUEST,
|
|
||||||
nextTransportSeq++
|
|
||||||
),
|
|
||||||
ACK_TIMEOUT_MS,
|
|
||||||
displayedFingerprint,
|
|
||||||
-1,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
lastShutdownAckMagic = message.magic;
|
lastShutdownAckMagic = message.magic;
|
||||||
fixedLayoutCreated = false;
|
fixedLayoutCreated = false;
|
||||||
@@ -593,15 +578,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
clearAllMessagesLocked("prelude");
|
clearAllMessagesLocked("prelude");
|
||||||
}
|
}
|
||||||
long now = SystemClock.elapsedRealtime();
|
long now = SystemClock.elapsedRealtime();
|
||||||
OutboundMessage prelude = new OutboundMessage(
|
OutboundMessage prelude = messageBuilder.prelude();
|
||||||
"prelude",
|
|
||||||
BleProtocol.PRELUDE_ACK_SID,
|
|
||||||
BleProtocol.PRELUDE_ACK_MAGIC,
|
|
||||||
CollectionUtils.singletonList(Arrays.copyOf(BleProtocol.PRELUDE_F5872, BleProtocol.PRELUDE_F5872.length)),
|
|
||||||
WRITE_TIMEOUT_MS,
|
|
||||||
0,
|
|
||||||
now + PRELUDE_TIMEOUT_MS
|
|
||||||
);
|
|
||||||
prelude.onAck = () -> {
|
prelude.onAck = () -> {
|
||||||
};
|
};
|
||||||
prelude.onTimeout = () -> {
|
prelude.onTimeout = () -> {
|
||||||
@@ -732,18 +709,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private OutboundMessage createHeartbeatMessageLocked() {
|
private OutboundMessage createHeartbeatMessageLocked() {
|
||||||
int magic = magicPool.allocate();
|
OutboundMessage message = messageBuilder.heartbeat();
|
||||||
OutboundMessage message = new OutboundMessage(
|
|
||||||
"heartbeat",
|
|
||||||
"heartbeat",
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(BleProtocol.buildHeartbeat(magic), BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, nextTransportSeq++),
|
|
||||||
HEARTBEAT_TIMEOUT_MS,
|
|
||||||
displayedFingerprint,
|
|
||||||
-1,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime();
|
lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime();
|
||||||
};
|
};
|
||||||
@@ -766,7 +732,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
inFlightMessages.addLast(message);
|
inFlightMessages.addLast(message);
|
||||||
}
|
}
|
||||||
if (message.imageUpdateId > 0 && message.imageMessageNumber == 1) {
|
if (message.imageUpdateId > 0 && message.imageMessageNumber == 1) {
|
||||||
ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId);
|
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId);
|
||||||
if (stats != null && stats.firstWriteStartedAtMs <= 0) {
|
if (stats != null && stats.firstWriteStartedAtMs <= 0) {
|
||||||
stats.firstWriteStartedAtMs = now;
|
stats.firstWriteStartedAtMs = now;
|
||||||
}
|
}
|
||||||
@@ -834,7 +800,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (message.imageMessageNumber == 1) {
|
if (message.imageMessageNumber == 1) {
|
||||||
ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId);
|
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId);
|
||||||
if (stats != null && stats.firstWriteStartedAtMs <= 0) {
|
if (stats != null && stats.firstWriteStartedAtMs <= 0) {
|
||||||
stats.firstWriteStartedAtMs = message.writeStartedAtMs > 0 ? message.writeStartedAtMs : message.sentAtMs;
|
stats.firstWriteStartedAtMs = message.writeStartedAtMs > 0 ? message.writeStartedAtMs : message.sentAtMs;
|
||||||
}
|
}
|
||||||
@@ -850,7 +816,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
long ackedAtMs = SystemClock.elapsedRealtime();
|
long ackedAtMs = SystemClock.elapsedRealtime();
|
||||||
ImageUpdateStats stats = imageUpdateStats.remove(message.imageUpdateId);
|
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.remove(message.imageUpdateId);
|
||||||
if (stats != null && stats.firstWriteStartedAtMs > 0) {
|
if (stats != null && stats.firstWriteStartedAtMs > 0) {
|
||||||
emitFrameMetrics(stats.paintMs, (int) Math.max(0, ackedAtMs - stats.firstWriteStartedAtMs), stats.tileCount);
|
emitFrameMetrics(stats.paintMs, (int) Math.max(0, ackedAtMs - stats.firstWriteStartedAtMs), stats.tileCount);
|
||||||
}
|
}
|
||||||
@@ -865,23 +831,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void enqueueCreateLayoutLocked() {
|
private void enqueueCreateLayoutLocked() {
|
||||||
int magic = magicPool.allocate();
|
OutboundMessage message = messageBuilder.createLayout(DASHBOARD_TILES);
|
||||||
OutboundMessage message = new OutboundMessage(
|
|
||||||
"create-layout",
|
|
||||||
"create-layout",
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(
|
|
||||||
BleProtocol.buildCreateMixedImagePage(magic, DASHBOARD_TILES),
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
BleProtocol.FLAG_REQUEST,
|
|
||||||
nextTransportSeq++
|
|
||||||
),
|
|
||||||
ACK_TIMEOUT_MS,
|
|
||||||
getDesiredFingerprint(),
|
|
||||||
-1,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
startupProbePending = false;
|
startupProbePending = false;
|
||||||
clearMessagesOfKindLocked("startup-text-probe");
|
clearMessagesOfKindLocked("startup-text-probe");
|
||||||
@@ -906,23 +856,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
private void enqueueStartupProbeLocked() {
|
private void enqueueStartupProbeLocked() {
|
||||||
enqueueCreateLayoutLocked();
|
enqueueCreateLayoutLocked();
|
||||||
|
|
||||||
int magic = magicPool.allocate();
|
OutboundMessage message = messageBuilder.startupTextProbe();
|
||||||
OutboundMessage message = new OutboundMessage(
|
|
||||||
"startup-text-probe",
|
|
||||||
"startup text probe",
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(
|
|
||||||
BleProtocol.buildDashboardTextUpgrade(magic),
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
BleProtocol.FLAG_REQUEST,
|
|
||||||
nextTransportSeq++
|
|
||||||
),
|
|
||||||
ACK_TIMEOUT_MS,
|
|
||||||
getDesiredFingerprint(),
|
|
||||||
-1,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
startupProbePending = false;
|
startupProbePending = false;
|
||||||
clearMessagesOfKindLocked("create-layout");
|
clearMessagesOfKindLocked("create-layout");
|
||||||
@@ -957,22 +891,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
int sessionId = nextMapSessionId();
|
int sessionId = nextMapSessionId();
|
||||||
List<BleProtocol.ImageFragment> fragments = BleImageOptimizer.planImageFragments(bmp, IMAGE_FRAGMENT_SIZE);
|
List<BleProtocol.ImageFragment> fragments = BleImageOptimizer.planImageFragments(bmp, IMAGE_FRAGMENT_SIZE);
|
||||||
for (BleProtocol.ImageFragment fragment : fragments) {
|
for (BleProtocol.ImageFragment fragment : fragments) {
|
||||||
int magic = magicPool.allocate();
|
OutboundMessage message = messageBuilder.imageWarmupFragment(tile, sessionId, fragment, bmp);
|
||||||
OutboundMessage message = new OutboundMessage(
|
|
||||||
"warmup",
|
|
||||||
"warmup " + tile.name + "#" + fragment.index,
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(
|
|
||||||
BleProtocol.buildImageRawData(tile, sessionId, bmp.length, fragment, magic),
|
|
||||||
BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST,
|
|
||||||
nextTransportSeq++
|
|
||||||
),
|
|
||||||
WARMUP_FRAGMENT_TIMEOUT_MS,
|
|
||||||
getDesiredFingerprint(),
|
|
||||||
0,
|
|
||||||
bmp
|
|
||||||
);
|
|
||||||
pendingMessages.addLast(message);
|
pendingMessages.addLast(message);
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
// The first tile warmup primes the image path but does not reliably
|
// The first tile warmup primes the image path but does not reliably
|
||||||
@@ -992,7 +911,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
|
|
||||||
private void enqueueDesiredImageLocked() {
|
private void enqueueDesiredImageLocked() {
|
||||||
String fingerprint = getDesiredFingerprint();
|
String fingerprint = getDesiredFingerprint();
|
||||||
List<TileImagePlan> changedTiles = new ArrayList<>();
|
List<BleImageOptimizer.TileImagePlan> changedTiles = new ArrayList<>();
|
||||||
byte[][] tileBmps;
|
byte[][] tileBmps;
|
||||||
boolean forceTiledCommit;
|
boolean forceTiledCommit;
|
||||||
int paintMs;
|
int paintMs;
|
||||||
@@ -1012,7 +931,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
if (Arrays.equals(bmp, displayedBmp)) {
|
if (Arrays.equals(bmp, displayedBmp)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
changedTiles.add(new TileImagePlan(i, tile, bmp, nextMapSessionId()));
|
changedTiles.add(new BleImageOptimizer.TileImagePlan(i, tile, bmp, nextMapSessionId()));
|
||||||
}
|
}
|
||||||
if (changedTiles.isEmpty()) {
|
if (changedTiles.isEmpty()) {
|
||||||
displayedFingerprint = fingerprint;
|
displayedFingerprint = fingerprint;
|
||||||
@@ -1024,35 +943,37 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
synchronizedCommits = false;
|
synchronizedCommits = false;
|
||||||
}
|
}
|
||||||
boolean reserveLastByte = false;
|
boolean reserveLastByte = false;
|
||||||
for (TileImagePlan plan : changedTiles) {
|
for (BleImageOptimizer.TileImagePlan plan : changedTiles) {
|
||||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.bmp, IMAGE_FRAGMENT_SIZE, reserveLastByte);
|
plan.fragments = BleImageOptimizer.planImageFragments(plan.bmp, IMAGE_FRAGMENT_SIZE, reserveLastByte);
|
||||||
}
|
}
|
||||||
|
|
||||||
int updateId = nextImageUpdateId++;
|
int updateId = nextImageUpdateId++;
|
||||||
imageUpdateStats.put(updateId, new ImageUpdateStats(paintMs, changedTiles.size()));
|
imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, changedTiles.size()));
|
||||||
int messageCount = 0;
|
int messageCount = 0;
|
||||||
for (TileImagePlan plan : changedTiles) {
|
for (BleImageOptimizer.TileImagePlan plan : changedTiles) {
|
||||||
messageCount += plan.fragments.size();
|
messageCount += plan.fragments.size();
|
||||||
}
|
}
|
||||||
int messageNumber = 1;
|
int messageNumber = 1;
|
||||||
if (!synchronizedCommits) {
|
if (!synchronizedCommits) {
|
||||||
for (TileImagePlan plan : changedTiles) {
|
for (BleImageOptimizer.TileImagePlan plan : changedTiles) {
|
||||||
for (BleProtocol.ImageFragment fragment : plan.fragments) {
|
for (int i = 0; i < plan.fragments.size() - 1; i++) {
|
||||||
//boolean requestAck = fragment.index % 2 == 1;
|
BleProtocol.ImageFragment fragment = plan.fragments.get(i);
|
||||||
boolean requestAck = true;
|
boolean requestAck = true;
|
||||||
enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, messageNumber++, messageCount, nextTransportSeq++, requestAck, true);
|
boolean isLast = i == plan.fragments.size() - 1;
|
||||||
|
enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, messageNumber++, messageCount, requestAck, isLast);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (TileImagePlan plan : changedTiles) {
|
for (BleImageOptimizer.TileImagePlan plan : changedTiles) {
|
||||||
for (int i = 0; i < plan.fragments.size() - 1; i++) {
|
for (int i = 0; i < plan.fragments.size() - 1; i++) {
|
||||||
//boolean requestAck = i % 2 == 1;
|
|
||||||
boolean requestAck = true;
|
boolean requestAck = true;
|
||||||
enqueueImageFragmentLocked(plan, plan.fragments.get(i), fingerprint, updateId, messageNumber++, messageCount, nextTransportSeq++, requestAck, false);
|
boolean isLast = false;
|
||||||
|
enqueueImageFragmentLocked(plan, plan.fragments.get(i), fingerprint, updateId, messageNumber++, messageCount, requestAck, isLast);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (TileImagePlan plan : changedTiles) {
|
for (BleImageOptimizer.TileImagePlan plan : changedTiles) {
|
||||||
enqueueImageFragmentLocked(plan, plan.fragments.get(plan.fragments.size() - 1), fingerprint, updateId, messageNumber++, messageCount, nextTransportSeq++, true, true);
|
boolean isLast = true;
|
||||||
|
enqueueImageFragmentLocked(plan, plan.fragments.get(plan.fragments.size() - 1), fingerprint, updateId, messageNumber++, messageCount, true, isLast);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1062,33 +983,16 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void enqueueImageFragmentLocked(
|
private void enqueueImageFragmentLocked(
|
||||||
TileImagePlan plan,
|
BleImageOptimizer.TileImagePlan plan,
|
||||||
BleProtocol.ImageFragment fragment,
|
BleProtocol.ImageFragment fragment,
|
||||||
String fingerprint,
|
String fingerprint,
|
||||||
int updateId,
|
int updateId,
|
||||||
int messageNumber,
|
int messageNumber,
|
||||||
int messageCount,
|
int messageCount,
|
||||||
int transportSeq,
|
|
||||||
boolean requestAck,
|
boolean requestAck,
|
||||||
boolean isLast
|
boolean isLast
|
||||||
) {
|
) {
|
||||||
int magic = requestAck ? magicPool.allocate() : 0;
|
OutboundMessage message = messageBuilder.imageFragment(fragment, plan, requestAck);
|
||||||
OutboundMessage message = new OutboundMessage(
|
|
||||||
"image",
|
|
||||||
"image " + plan.tile.name + "#" + fragment.index,
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(
|
|
||||||
BleProtocol.buildImageRawData(plan.tile, plan.sessionId, plan.bmp.length, fragment, magic),
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
BleProtocol.FLAG_REQUEST, //isLast ? 0 : BleProtocol.FLAG_REQUEST,
|
|
||||||
transportSeq
|
|
||||||
),
|
|
||||||
ACK_TIMEOUT_MS,
|
|
||||||
fingerprint,
|
|
||||||
plan.tileIndex,
|
|
||||||
plan.bmp
|
|
||||||
);
|
|
||||||
message.setImageUpdatePosition(updateId, messageNumber, messageCount);
|
message.setImageUpdatePosition(updateId, messageNumber, messageCount);
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
imageRetryAfterMs = 0;
|
imageRetryAfterMs = 0;
|
||||||
@@ -1112,7 +1016,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!imageStillQueued) {
|
if (!imageStillQueued) {
|
||||||
displayedFingerprint = message.fingerprint;
|
displayedFingerprint = fingerprint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1126,29 +1030,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
pendingMessages.addLast(message);
|
pendingMessages.addLast(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private OutboundMessage createAudioControlMessageLocked(boolean enable, int magic) {
|
private OutboundMessage createAudioControlMessageLocked(boolean enable) {
|
||||||
OutboundMessage message = new OutboundMessage(
|
OutboundMessage message = messageBuilder.enableOrDisableMic(enable);
|
||||||
"audio-control",
|
|
||||||
enable ? "G2 mic enable" : "G2 mic disable",
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(
|
|
||||||
BleProtocol.buildAudioControl(magic, enable),
|
|
||||||
BleProtocol.SID_EVENHUB,
|
|
||||||
BleProtocol.FLAG_REQUEST,
|
|
||||||
nextTransportSeq++
|
|
||||||
),
|
|
||||||
ACK_TIMEOUT_MS,
|
|
||||||
displayedFingerprint,
|
|
||||||
-1,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
lastAudioControlAckMagic = message.magic;
|
lastAudioControlAckMagic = message.magic;
|
||||||
audioCaptureActive = message.label != null && message.label.contains("enable");
|
audioCaptureActive = message.label != null && message.label.contains("enable");
|
||||||
logLine(audioCaptureActive ? "G2 mic enabled" : "G2 mic disabled");
|
logLine(audioCaptureActive ? "G2 mic enabled" : "G2 mic disabled");
|
||||||
lock.notifyAll();
|
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
message.onTimeout = () -> {
|
message.onTimeout = () -> {
|
||||||
handleTransportFailure("audio control ack timeout");
|
handleTransportFailure("audio control ack timeout");
|
||||||
@@ -1166,23 +1053,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private OutboundMessage createBatteryQueryMessageLocked() {
|
private OutboundMessage createBatteryQueryMessageLocked() {
|
||||||
int magic = magicPool.allocate();
|
OutboundMessage message = messageBuilder.batteryQuery();
|
||||||
OutboundMessage message = new OutboundMessage(
|
|
||||||
"battery",
|
|
||||||
"battery",
|
|
||||||
BleProtocol.SID_UI_SETTING,
|
|
||||||
magic,
|
|
||||||
BleProtocol.framePb(
|
|
||||||
BleProtocol.buildSettingsQuery(magic),
|
|
||||||
BleProtocol.SID_UI_SETTING,
|
|
||||||
BleProtocol.FLAG_REQUEST,
|
|
||||||
nextTransportSeq++
|
|
||||||
),
|
|
||||||
ACK_TIMEOUT_MS,
|
|
||||||
displayedFingerprint,
|
|
||||||
-1,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
message.onAck = () -> {
|
message.onAck = () -> {
|
||||||
BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(message.ackPayload);
|
BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(message.ackPayload);
|
||||||
if (snapshot != null) {
|
if (snapshot != null) {
|
||||||
@@ -1192,7 +1063,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
message.onTimeout = () -> {
|
message.onTimeout = () -> {
|
||||||
handleTransportFailure("battery ack timeout");
|
logLine("Battery query timed out");
|
||||||
};
|
};
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
@@ -1537,30 +1408,4 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
return desiredFingerprint;
|
return desiredFingerprint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class TileImagePlan {
|
|
||||||
final int tileIndex;
|
|
||||||
final BleProtocol.ImageTileOptions tile;
|
|
||||||
final byte[] bmp;
|
|
||||||
final int sessionId;
|
|
||||||
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
|
||||||
|
|
||||||
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId) {
|
|
||||||
this.tileIndex = tileIndex;
|
|
||||||
this.tile = tile;
|
|
||||||
this.bmp = BmpUtil.copyTileBmp(bmp);
|
|
||||||
this.sessionId = sessionId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final class ImageUpdateStats {
|
|
||||||
final int paintMs;
|
|
||||||
final int tileCount;
|
|
||||||
long firstWriteStartedAtMs;
|
|
||||||
|
|
||||||
ImageUpdateStats(int paintMs, int tileCount) {
|
|
||||||
this.paintMs = Math.max(0, paintMs);
|
|
||||||
this.tileCount = tileCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+26
@@ -67,4 +67,30 @@ public final class BleImageOptimizer {
|
|||||||
end - start
|
end - start
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class TileImagePlan {
|
||||||
|
final int tileIndex;
|
||||||
|
final BleProtocol.ImageTileOptions tile;
|
||||||
|
final byte[] bmp;
|
||||||
|
final int sessionId;
|
||||||
|
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
||||||
|
|
||||||
|
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId) {
|
||||||
|
this.tileIndex = tileIndex;
|
||||||
|
this.tile = tile;
|
||||||
|
this.bmp = BmpUtil.copyTileBmp(bmp);
|
||||||
|
this.sessionId = sessionId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class ImageUpdateStats {
|
||||||
|
final int paintMs;
|
||||||
|
final int tileCount;
|
||||||
|
long firstWriteStartedAtMs;
|
||||||
|
|
||||||
|
ImageUpdateStats(int paintMs, int tileCount) {
|
||||||
|
this.paintMs = Math.max(0, paintMs);
|
||||||
|
this.tileCount = tileCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
package com.faceclaw.app;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class MessageBuilder {
|
||||||
|
private static final int ACK_TIMEOUT_MS = 3_500;
|
||||||
|
private static final int HEARTBEAT_TIMEOUT_MS = 1_500;
|
||||||
|
private static final int WARMUP_FRAGMENT_TIMEOUT_MS = 3_000;
|
||||||
|
|
||||||
|
private BleMagicPool magicPool;
|
||||||
|
private int nextTransportSeq = 0x40;
|
||||||
|
|
||||||
|
public MessageBuilder(BleMagicPool magicPool) {
|
||||||
|
this.magicPool = magicPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage prelude() {
|
||||||
|
return new OutboundMessage(
|
||||||
|
"prelude", "prelude",
|
||||||
|
BleProtocol.PRELUDE_ACK_SID,
|
||||||
|
BleProtocol.PRELUDE_ACK_MAGIC,
|
||||||
|
CollectionUtils.singletonList(Arrays.copyOf(BleProtocol.PRELUDE_F5872, BleProtocol.PRELUDE_F5872.length)),
|
||||||
|
ACK_TIMEOUT_MS,
|
||||||
|
0,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage shutdown(int exitMode) {
|
||||||
|
int magic = magicPool.allocate();
|
||||||
|
return new OutboundMessage(
|
||||||
|
"shutdown",
|
||||||
|
"shutdown mode=" + exitMode,
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(
|
||||||
|
BleProtocol.buildShutdown(magic, exitMode),
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
BleProtocol.FLAG_REQUEST,
|
||||||
|
nextTransportSeq++
|
||||||
|
),
|
||||||
|
ACK_TIMEOUT_MS,
|
||||||
|
-1,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage imageWarmupFragment(BleProtocol.ImageTileOptions tile, int sessionId, BleProtocol.ImageFragment fragment, byte[] bmp) {
|
||||||
|
int magic = magicPool.allocate();
|
||||||
|
return new OutboundMessage(
|
||||||
|
"warmup",
|
||||||
|
"warmup " + tile.name + "#" + fragment.index,
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(
|
||||||
|
BleProtocol.buildImageRawData(tile, sessionId, bmp.length, fragment, magic),
|
||||||
|
BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST,
|
||||||
|
nextTransportSeq++
|
||||||
|
),
|
||||||
|
WARMUP_FRAGMENT_TIMEOUT_MS,
|
||||||
|
0,
|
||||||
|
bmp
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage imageFragment(BleProtocol.ImageFragment fragment, BleImageOptimizer.TileImagePlan plan, boolean requestAck) {
|
||||||
|
int magic = requestAck ? magicPool.allocate() : 0;
|
||||||
|
return new OutboundMessage(
|
||||||
|
"image",
|
||||||
|
"image " + plan.tile.name + "#" + fragment.index,
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(
|
||||||
|
BleProtocol.buildImageRawData(plan.tile, plan.sessionId, plan.bmp.length, fragment, magic),
|
||||||
|
BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST,
|
||||||
|
nextTransportSeq++
|
||||||
|
),
|
||||||
|
ACK_TIMEOUT_MS,
|
||||||
|
plan.tileIndex,
|
||||||
|
plan.bmp
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage enableOrDisableMic(boolean enable) {
|
||||||
|
int magic = magicPool.allocate();
|
||||||
|
return new OutboundMessage(
|
||||||
|
"audio-control",
|
||||||
|
enable ? "G2 mic enable" : "G2 mic disable",
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(
|
||||||
|
BleProtocol.buildAudioControl(magic, enable),
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
BleProtocol.FLAG_REQUEST,
|
||||||
|
nextTransportSeq++
|
||||||
|
),
|
||||||
|
ACK_TIMEOUT_MS,
|
||||||
|
-1,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage createLayout(BleProtocol.ImageTileOptions[] tiles) {
|
||||||
|
int magic = magicPool.allocate();
|
||||||
|
return new OutboundMessage(
|
||||||
|
"create-layout",
|
||||||
|
"create-layout",
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(
|
||||||
|
BleProtocol.buildCreateMixedImagePage(magic, tiles),
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
BleProtocol.FLAG_REQUEST,
|
||||||
|
nextTransportSeq++
|
||||||
|
),
|
||||||
|
ACK_TIMEOUT_MS,
|
||||||
|
-1,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage startupTextProbe() {
|
||||||
|
int magic = magicPool.allocate();
|
||||||
|
return new OutboundMessage(
|
||||||
|
"startup-text-probe",
|
||||||
|
"startup text probe",
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(
|
||||||
|
BleProtocol.buildDashboardTextUpgrade(magic),
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
BleProtocol.FLAG_REQUEST,
|
||||||
|
nextTransportSeq++
|
||||||
|
),
|
||||||
|
ACK_TIMEOUT_MS,
|
||||||
|
-1,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage heartbeat() {
|
||||||
|
int magic = magicPool.allocate();
|
||||||
|
return new OutboundMessage(
|
||||||
|
"heartbeat",
|
||||||
|
"heartbeat",
|
||||||
|
BleProtocol.SID_EVENHUB,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(BleProtocol.buildHeartbeat(magic), BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, nextTransportSeq++),
|
||||||
|
HEARTBEAT_TIMEOUT_MS,
|
||||||
|
-1,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutboundMessage batteryQuery() {
|
||||||
|
int magic = magicPool.allocate();
|
||||||
|
return new OutboundMessage(
|
||||||
|
"battery",
|
||||||
|
"battery",
|
||||||
|
BleProtocol.SID_UI_SETTING,
|
||||||
|
magic,
|
||||||
|
BleProtocol.framePb(
|
||||||
|
BleProtocol.buildSettingsQuery(magic),
|
||||||
|
BleProtocol.SID_UI_SETTING,
|
||||||
|
BleProtocol.FLAG_REQUEST,
|
||||||
|
nextTransportSeq++
|
||||||
|
),
|
||||||
|
ACK_TIMEOUT_MS,
|
||||||
|
-1,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-16
@@ -8,7 +8,6 @@ public final class OutboundMessage {
|
|||||||
final int magic;
|
final int magic;
|
||||||
final List<byte[]> frames;
|
final List<byte[]> frames;
|
||||||
final int ackTimeoutMs;
|
final int ackTimeoutMs;
|
||||||
final String fingerprint;
|
|
||||||
final int tileIndex;
|
final int tileIndex;
|
||||||
final byte[] tileBmp;
|
final byte[] tileBmp;
|
||||||
|
|
||||||
@@ -22,31 +21,17 @@ public final class OutboundMessage {
|
|||||||
Runnable onAck;
|
Runnable onAck;
|
||||||
Runnable onTimeout;
|
Runnable onTimeout;
|
||||||
|
|
||||||
OutboundMessage(String kind, String label, int sid, int magic, List<byte[]> frames, int ackTimeoutMs, String fingerprint, int tileIndex, byte[] tileBmp) {
|
OutboundMessage(String kind, String label, int sid, int magic, List<byte[]> frames, int ackTimeoutMs, int tileIndex, byte[] tileBmp) {
|
||||||
this.kind = kind;
|
this.kind = kind;
|
||||||
this.label = label;
|
this.label = label;
|
||||||
this.sid = sid;
|
this.sid = sid;
|
||||||
this.magic = magic;
|
this.magic = magic;
|
||||||
this.frames = frames;
|
this.frames = frames;
|
||||||
this.ackTimeoutMs = ackTimeoutMs;
|
this.ackTimeoutMs = ackTimeoutMs;
|
||||||
this.fingerprint = fingerprint == null ? "" : fingerprint;
|
|
||||||
this.tileIndex = tileIndex;
|
this.tileIndex = tileIndex;
|
||||||
this.tileBmp = BmpUtil.copyTileBmp(tileBmp);
|
this.tileBmp = BmpUtil.copyTileBmp(tileBmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
OutboundMessage(String label, int sid, int magic, List<byte[]> frames, int writeTimeoutMs, int ignored, long ackDeadlineAtMs) {
|
|
||||||
this.kind = "prelude";
|
|
||||||
this.label = label;
|
|
||||||
this.sid = sid;
|
|
||||||
this.magic = magic;
|
|
||||||
this.frames = frames;
|
|
||||||
this.ackTimeoutMs = writeTimeoutMs;
|
|
||||||
this.fingerprint = "";
|
|
||||||
this.tileIndex = -1;
|
|
||||||
this.tileBmp = new byte[0];
|
|
||||||
this.ackDeadlineAtMs = ackDeadlineAtMs;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setImageUpdatePosition(int updateId, int messageNumber, int messageCount) {
|
void setImageUpdatePosition(int updateId, int messageNumber, int messageCount) {
|
||||||
this.imageUpdateId = updateId;
|
this.imageUpdateId = updateId;
|
||||||
this.imageMessageNumber = messageNumber;
|
this.imageMessageNumber = messageNumber;
|
||||||
|
|||||||
Reference in new issue
Block a user