diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java index 2eaacad6..f5c0f51c 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java @@ -251,18 +251,32 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { if (!BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { return; } - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); + // One notification value can carry several envelope frames back to + // back; reading only the first would silently drop the rest. + List frames = BleProtocol.splitFrames(data); + if (frames.size() > 1) { + emitLog("rx " + address + " value carries " + frames.size() + " frames (raw " + data.length + " bytes)"); + } + for (byte[] buf : frames) { + handleFrame(address, buf, data.length); + } + } + + private void handleFrame(String address, byte[] buf, int rawValueLength) { + BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); if (!frame.ok) { - emitLog("rx " + address + " unparseable frame len=" + data.length - + " head=" + FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(data, Math.min(16, data.length)))); + emitLog("rx " + address + " unparseable frame len=" + buf.length + " (raw value " + rawValueLength + ")" + + " head=" + FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(buf, Math.min(16, buf.length)))); return; } // Log every control frame while diagnosing 2.2.9: sid/flag/type/magic // plus a payload prefix is enough to reconstruct what the lens said. - emitLog(String.format("rx %s sid=0x%02x flag=0x%02x type=%d magic=%d frag=%d/%d len=%d pb=%s", + int declared = buf.length > 3 ? buf[3] & 0xff : 0; + String truncated = buf.length < 8 + declared ? " TRUNCATED(declared=" + declared + ")" : ""; + emitLog(String.format("rx %s sid=0x%02x flag=0x%02x type=%d magic=%d frag=%d/%d len=%d%s pb=%s", address, frame.sid, frame.flag, frame.msgType, frame.msgSeq, - data.length > 5 ? data[5] & 0xff : 0, data.length > 4 ? data[4] & 0xff : 0, frame.pb.length, - FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(frame.pb, Math.min(40, frame.pb.length))))); + buf.length > 5 ? buf[5] & 0xff : 0, buf.length > 4 ? buf[4] & 0xff : 0, frame.pb.length, truncated, + FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(frame.pb, Math.min(48, frame.pb.length))))); if (frame.sid == BleProtocol.SID_UI_SETTING && BleProtocol.parseSettingsFirmwareInfo(frame.pb) != null) { // Any settings frame carrying firmware versions answers the probe's diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java index 00ca695a..ee58dbab 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java @@ -513,12 +513,16 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { public void onNotification(String address, String characteristicUuid, byte[] data) { if (BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { // Control channel: only the security-auth response is awaited here. - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); - if (frame.ok && frame.sid == BleProtocol.SID_SECURITY_AUTH) { - synchronized (lock) { - if (authLatch != null && frame.msgSeq == pendingAuthMagic) { - authAckPb = frame.pb; - authLatch.countDown(); + // A value can carry several envelope frames back to back (e.g. the + // response packed with one of the lens's own sid-0x80 notifies). + for (byte[] buf : BleProtocol.splitFrames(data)) { + BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); + if (frame.ok && frame.sid == BleProtocol.SID_SECURITY_AUTH) { + synchronized (lock) { + if (authLatch != null && frame.msgSeq == pendingAuthMagic) { + authAckPb = frame.pb; + authLatch.countDown(); + } } } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java index fab525f7..6aba3b3d 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java @@ -343,7 +343,14 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { if (!BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { return; } - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); + // A value can carry several envelope frames back to back. + for (byte[] buf : BleProtocol.splitFrames(data)) { + handleFrame(address, buf); + } + } + + private void handleFrame(String address, byte[] buf) { + BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); if (!frame.ok) { return; } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java index fef51142..f1efc7f1 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java @@ -604,6 +604,35 @@ public class BleProtocol { } + /** + * Split one notification value into individual `aa21`/`aa12` envelope + * frames. A value normally holds exactly one frame, but the firmware can + * pack several (each self-describing via the length byte at offset 3), and + * a frame that only reads the first would silently drop the rest. A value + * that does not start with an envelope, or a trailing partial frame + * (truncated by the stack — observed on a Samsung tablet for values over + * 64 bytes), is returned as-is so the caller can log it. + */ + public static List splitFrames(byte[] value) { + List out = new ArrayList<>(); + if (value == null || value.length == 0) { + return out; + } + int offset = 0; + while (offset + 8 <= value.length + && value[offset] == (byte) 0xaa + && (value[offset + 1] == 0x21 || value[offset + 1] == 0x12)) { + int len = value[offset + 3] & 0xff; + int end = Math.min(value.length, offset + 8 + len); + out.add(Arrays.copyOfRange(value, offset, end)); + offset = end; + } + if (offset < value.length) { + out.add(Arrays.copyOfRange(value, offset, value.length)); + } + return out; + } + public static ParsedFrame parseFrame(byte[] buf) { if (buf == null || buf.length < 10 || buf[0] != (byte) 0xaa || (buf[1] != 0x21 && buf[1] != 0x12)) { return new ParsedFrame(false, 0, 0, new byte[0], -1, -1);