From 68810197a8896dabea9c1e90bc33bdaddb820a52 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 18 Sep 2026 22:48:20 -0700 Subject: [PATCH] Refactor Java to shared kotlin --- .gitignore | 4 + App_Resources/Android/app.gradle | 19 + App_Resources/Android/before-plugins.gradle | 4 +- .../com/faceclaw/app/AndroidNoiseEffects.java | 59 + .../faceclaw/app/FaceclawAlarmListener.java | 6 - .../app/FaceclawAmbientLightListener.java | 10 - .../app/FaceclawAudioPacketListener.java | 5 - .../faceclaw/app/FaceclawBleCommunicator.java | 18 +- .../app/FaceclawBleCommunicatorListener.java | 15 - .../com/faceclaw/app/FaceclawBleListener.java | 6 - .../com/faceclaw/app/FaceclawBleManager.java | 2 +- .../app/FaceclawCaptionEngineListener.java | 17 - .../faceclaw/app/FaceclawCompassListener.java | 7 - .../app/FaceclawDeviceDiscoveryListener.java | 18 - .../faceclaw/app/FaceclawDeviceInfoProbe.java | 4 +- .../faceclaw/app/FaceclawEvenHubListener.java | 18 - .../faceclaw/app/FaceclawFirmwareFlasher.java | 2 +- .../app/FaceclawFirmwareFlasherListener.java | 24 - .../faceclaw/app/FaceclawFirmwareUtil.java | 57 - .../app/FaceclawFlashPromptCommunicator.java | 2 +- .../app/FaceclawFlashPromptListener.java | 23 - .../com/faceclaw/app/FaceclawImuListener.java | 10 - .../faceclaw/app/FaceclawLlamaListener.java | 16 - .../app/FaceclawLocationListener.java | 8 - .../app/FaceclawLocationTrackerListener.java | 13 - .../app/FaceclawMediaBrowserListener.java | 10 - .../app/FaceclawMediaControllerListener.java | 23 - .../app/FaceclawMicStatusListener.java | 10 - .../app/FaceclawModelDownloaderListener.java | 11 - .../faceclaw/app/FaceclawNoiseSuppressor.java | 266 --- .../app/FaceclawNotificationListener.java | 5 - .../app/FaceclawPreviewCompositor.java | 3 +- .../app/FaceclawQrScannerListener.java | 10 - .../app/FaceclawRingEventDecoder.java | 126 -- .../app/FaceclawSettingsListener.java | 6 - .../app/FaceclawVoiceControllerListener.java | 47 - .../com/faceclaw/app/FaceclawWavRecorder.java | 100 -- .../faceclaw/app/FaceclawWearListener.java | 19 - .../app/FaceclawWebSocketListener.java | 9 - .../java/com/faceclaw/app/FwGlyphAtlas.java | 83 - .../com/faceclaw/app/GifScreenRecorder.java | 269 +-- .../java/com/faceclaw/app/GlyphAtlas.java | 267 --- .../java/com/faceclaw/app/ImageAtlas.java | 115 -- .../java/com/faceclaw/app/LvglFontFile.java | 229 --- .../java/com/faceclaw/app/ScreenshotUtil.java | 81 +- .../com/faceclaw/app/SurfaceCompositor.java | 509 ------ .../app/g2protocol/BleImageOptimizer.java | 555 ------- .../faceclaw/app/g2protocol/BleMagicPool.java | 69 - .../faceclaw/app/g2protocol/BleProtocol.java | 1240 -------------- .../app/g2protocol/CfwMessageWindow.java | 38 - .../faceclaw/app/g2protocol/CfwTransport.java | 141 -- .../app/g2protocol/ConnectionOptions.java | 75 - .../com/faceclaw/app/g2protocol/G2Event.java | 116 -- .../app/g2protocol/MessageBuilder.java | 281 ---- .../app/g2protocol/OutboundMessage.java | 71 - .../app/g2protocol/TextureCacheState.java | 179 -- .../app/g2protocol/TexturePlanner.java | 721 -------- .../java/com/faceclaw/app/util/BmpUtil.java | 133 -- .../faceclaw/app/util/CollectionUtils.java | 26 - app/g2/ble-image-optimizer.ios.ts | 15 + app/g2/ble-protocol.ios.ts | 66 + app/g2/cfw-transport.ios.ts | 21 + app/graphics/glyph-wire.ts | 8 +- app/graphics/surface-compositor.ios.ts | 30 + app/native/kotlin-data.ts | 12 + app/native/kotlin-platform.android.ts | 2 + app/native/kotlin-platform.ios.ts | 2 + app/native/kotlin-protocol-smoke.ios.ts | 36 + app/native/kotlin-protocol-smoke.ts | 20 + app/native/lvgl-font.ios.ts | 21 +- hooks/before-prepare/faceclaw-kotlin.js | 8 + native/kotlin/README.md | 79 + native/kotlin/android-java-boundaries.json | 136 ++ native/kotlin/build.gradle.kts | 4 + native/kotlin/migrated-java-sources.json | 198 +++ native/kotlin/plugin/index.js | 1 + native/kotlin/plugin/package.json | 7 + .../plugin/platforms/android/include.gradle | 3 + native/kotlin/settings.gradle.kts | 4 + native/kotlin/shared/build.gradle.kts | 25 + .../com/faceclaw/app/AndroidByteReader.kt | 20 + .../com/faceclaw/app/FaceclawFirmwareUtil.kt | 31 + .../com/faceclaw/app/FaceclawWavRecorder.kt | 72 + .../faceclaw/app/PlatformServices.android.kt | 41 + .../app/g2protocol/AndroidProtocolPlatform.kt | 48 + .../faceclaw/shared/PlatformInfo.android.kt | 11 + .../com/faceclaw/app/PlatformServices.kt | 10 + .../app/audio/FaceclawNoiseSuppressor.kt | 263 +++ .../app/callbacks/FaceclawAlarmListener.kt | 6 + .../callbacks/FaceclawAmbientLightListener.kt | 10 + .../callbacks/FaceclawAudioPacketListener.kt | 5 + .../FaceclawBleCommunicatorListener.kt | 37 + .../app/callbacks/FaceclawBleListener.kt | 7 + .../FaceclawCaptionEngineListener.kt | 22 + .../app/callbacks/FaceclawCompassListener.kt | 14 + .../FaceclawDeviceDiscoveryListener.kt | 17 + .../FaceclawDeviceInfoProbeListener.kt | 12 +- .../app/callbacks/FaceclawEvenHubListener.kt | 18 + .../FaceclawFirmwareFlasherListener.kt | 30 + .../callbacks/FaceclawFlashPromptListener.kt | 25 + .../app/callbacks/FaceclawImuListener.kt | 9 + .../app/callbacks/FaceclawLlamaListener.kt | 16 + .../app/callbacks/FaceclawLocationListener.kt | 13 + .../FaceclawLocationTrackerListener.kt | 19 + .../callbacks/FaceclawMediaBrowserListener.kt | 10 + .../FaceclawMediaControllerListener.kt | 23 + .../callbacks/FaceclawMicStatusListener.kt | 10 + .../FaceclawModelDownloaderListener.kt | 11 + .../callbacks/FaceclawNotificationListener.kt | 5 + .../callbacks/FaceclawQrScannerListener.kt | 10 + .../app/callbacks/FaceclawSettingsListener.kt | 6 + .../app/callbacks/FaceclawSseListener.kt | 15 +- .../FaceclawVoiceControllerListener.kt | 44 + .../app/callbacks/FaceclawWearListener.kt | 17 + .../callbacks/FaceclawWebSocketListener.kt | 12 + .../app/g2protocol/BleImageOptimizer.kt | 735 ++++++++ .../faceclaw/app/g2protocol/BleMagicPool.kt | 43 + .../faceclaw/app/g2protocol/BleProtocol.kt | 1480 +++++++++++++++++ .../app/g2protocol/CfwMessageWindow.kt | 44 + .../faceclaw/app/g2protocol/CfwTransport.kt | 190 +++ .../app/g2protocol/ConnectionOptions.kt | 78 + .../g2protocol/FaceclawRingEventDecoder.kt | 145 ++ .../com/faceclaw/app/g2protocol/G2Event.kt | 141 ++ .../faceclaw/app/g2protocol/MessageBuilder.kt | 292 ++++ .../app/g2protocol/MessageReceiver.kt | 127 ++ .../app/g2protocol/OutboundMessage.kt | 68 + .../app/g2protocol/ProtocolPlatform.kt | 46 + .../app/g2protocol/TextureCacheState.kt | 191 +++ .../faceclaw/app/g2protocol/TexturePlanner.kt | 987 +++++++++++ .../com/faceclaw/app/graphics/FwGlyphAtlas.kt | 89 + .../com/faceclaw/app/graphics/GlyphAtlas.kt | 325 ++++ .../com/faceclaw/app/graphics/ImageAtlas.kt | 134 ++ .../com/faceclaw/app/graphics/LvglFontFile.kt | 250 +++ .../app/graphics/SharedGifScreenRecorder.kt | 270 +++ .../app/graphics/SharedScreenshots.kt | 55 + .../app/graphics/SurfaceCompositor.kt | 702 ++++++++ .../com/faceclaw/app/util/BinaryEncoding.kt | 45 + .../kotlin/com/faceclaw/app/util/BmpUtil.kt | 150 ++ .../com/faceclaw/app/util/ByteReader.kt | 43 + .../kotlin/com/faceclaw/app/util/ByteSink.kt | 44 + .../com/faceclaw/app/util/CollectionUtils.kt | 31 + .../com/faceclaw/shared/KotlinBridge.kt | 35 + .../kotlin/com/faceclaw/app/IosNativeApi.kt | 264 +++ .../com/faceclaw/app/PlatformServices.ios.kt | 72 + .../app/g2protocol/IosProtocolPlatform.kt | 129 ++ .../com/faceclaw/shared/PlatformInfo.ios.kt | 13 + package.json | 5 +- scripts/kotlin-build.cjs | 73 + scripts/test-kotlin.sh | 8 + tests/README.md | 18 +- tests/cfw-transport.test.cjs | 16 +- tests/compass-protocol.test.cjs | 19 - tests/compass-texture-cache.test.cjs | 2 +- tests/faceclaw-gesture-event.test.cjs | 19 - tests/fixtures/CfwTransportVectors.java | 19 - tests/fixtures/cfw-java-golden.json | 7 + tests/kotlin-build.test.cjs | 43 + tests/kotlin/build.gradle.kts | 40 + tests/kotlin/settings.gradle.kts | 3 + .../faceclaw/app}/CompassProtocolTest.java | 0 .../app}/FaceclawGestureEventTest.java | 0 .../app}/RingBatteryProtocolTest.java | 0 .../app}/TextureCacheProtocolTest.java | 0 .../com/faceclaw/app/AndroidProtocolTest.kt | 117 ++ .../com/faceclaw/app/JavaCompatibilityTest.kt | 27 + .../com/faceclaw/app/AudioRecordingTest.kt | 44 + .../kotlin/com/faceclaw/app/FontTest.kt | 22 + .../kotlin/com/faceclaw/app/ProtocolTest.kt | 283 ++++ .../com/faceclaw/app/SharedGraphicsTest.kt | 136 ++ .../com/faceclaw/app/IosNativeApiTest.kt | 38 + .../com/faceclaw/app/IosProtocolTest.kt | 81 + tests/ring-battery-protocol.test.cjs | 19 - tests/texture-cache-protocol.test.cjs | 23 - 173 files changed, 9855 insertions(+), 6252 deletions(-) create mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAmbientLightListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioPacketListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngineListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCompassListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscoveryListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasherListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareUtil.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawImuListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTrackerListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowserListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaControllerListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMicStatusListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloaderListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNoiseSuppressor.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNotificationListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRingEventDecoder.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceControllerListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWavRecorder.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocketListener.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleMagicPool.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwMessageWindow.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/G2Event.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/OutboundMessage.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java delete mode 100644 App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java create mode 100644 app/g2/ble-image-optimizer.ios.ts create mode 100644 app/g2/ble-protocol.ios.ts create mode 100644 app/g2/cfw-transport.ios.ts create mode 100644 app/graphics/surface-compositor.ios.ts create mode 100644 app/native/kotlin-data.ts create mode 100644 app/native/kotlin-protocol-smoke.ios.ts create mode 100644 app/native/kotlin-protocol-smoke.ts create mode 100644 hooks/before-prepare/faceclaw-kotlin.js create mode 100644 native/kotlin/README.md create mode 100644 native/kotlin/android-java-boundaries.json create mode 100644 native/kotlin/build.gradle.kts create mode 100644 native/kotlin/migrated-java-sources.json create mode 100644 native/kotlin/plugin/index.js create mode 100644 native/kotlin/plugin/package.json create mode 100644 native/kotlin/plugin/platforms/android/include.gradle create mode 100644 native/kotlin/settings.gradle.kts create mode 100644 native/kotlin/shared/build.gradle.kts create mode 100644 native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/AndroidByteReader.kt create mode 100644 native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawFirmwareUtil.kt create mode 100644 native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawWavRecorder.kt create mode 100644 native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformServices.android.kt create mode 100644 native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/g2protocol/AndroidProtocolPlatform.kt create mode 100644 native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/shared/PlatformInfo.android.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/FaceclawNoiseSuppressor.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAlarmListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAmbientLightListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAudioPacketListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCaptionEngineListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCompassListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceDiscoveryListener.kt rename App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbeListener.java => native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceInfoProbeListener.kt (52%) create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawEvenHubListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFirmwareFlasherListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFlashPromptListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLlamaListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationTrackerListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaBrowserListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaControllerListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMicStatusListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawModelDownloaderListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawNotificationListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawQrScannerListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSettingsListener.kt rename App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseListener.java => native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSseListener.kt (65%) create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWearListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWebSocketListener.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleMagicPool.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageWindow.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FaceclawRingEventDecoder.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/G2Event.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageReceiver.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/OutboundMessage.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ProtocolPlatform.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TextureCacheState.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/FwGlyphAtlas.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GlyphAtlas.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/ImageAtlas.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/LvglFontFile.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedGifScreenRecorder.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedScreenshots.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SurfaceCompositor.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BinaryEncoding.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BmpUtil.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteReader.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteSink.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/CollectionUtils.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/shared/KotlinBridge.kt create mode 100644 native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt create mode 100644 native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformServices.ios.kt create mode 100644 native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/g2protocol/IosProtocolPlatform.kt create mode 100644 native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/shared/PlatformInfo.ios.kt create mode 100644 scripts/kotlin-build.cjs create mode 100644 scripts/test-kotlin.sh delete mode 100644 tests/compass-protocol.test.cjs delete mode 100644 tests/faceclaw-gesture-event.test.cjs delete mode 100644 tests/fixtures/CfwTransportVectors.java create mode 100644 tests/fixtures/cfw-java-golden.json create mode 100644 tests/kotlin-build.test.cjs create mode 100644 tests/kotlin/build.gradle.kts create mode 100644 tests/kotlin/settings.gradle.kts rename tests/{fixtures => kotlin/src/androidHostTest/java/com/faceclaw/app}/CompassProtocolTest.java (100%) rename tests/{fixtures => kotlin/src/androidHostTest/java/com/faceclaw/app}/FaceclawGestureEventTest.java (100%) rename tests/{fixtures => kotlin/src/androidHostTest/java/com/faceclaw/app}/RingBatteryProtocolTest.java (100%) rename tests/{fixtures => kotlin/src/androidHostTest/java/com/faceclaw/app}/TextureCacheProtocolTest.java (100%) create mode 100644 tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/AndroidProtocolTest.kt create mode 100644 tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/JavaCompatibilityTest.kt create mode 100644 tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/AudioRecordingTest.kt create mode 100644 tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FontTest.kt create mode 100644 tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ProtocolTest.kt create mode 100644 tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/SharedGraphicsTest.kt create mode 100644 tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt create mode 100644 tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosProtocolTest.kt delete mode 100644 tests/ring-battery-protocol.test.cjs delete mode 100644 tests/texture-cache-protocol.test.cjs diff --git a/.gitignore b/.gitignore index 9efee48b..9331ae0a 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ native/kotlin/plugin/platforms/ios/ !native/kotlin/plugin/platforms/ !native/kotlin/plugin/platforms/android/ !native/kotlin/plugin/platforms/android/include.gradle + +tests/kotlin/.gradle/ +tests/kotlin/.kotlin/ +tests/kotlin/build/ diff --git a/App_Resources/Android/app.gradle b/App_Resources/Android/app.gradle index 0e24319a..832823ad 100644 --- a/App_Resources/Android/app.gradle +++ b/App_Resources/Android/app.gradle @@ -262,6 +262,7 @@ android { sourceSets.main { jniLibs.srcDirs += ["$projectDir/src/main/jniLibs"] + // Shared Kotlin is packaged by the local @faceclaw/kotlin plugin. } defaultConfig { @@ -288,7 +289,25 @@ android { } } +// NativeScript can retain deleted App_Resources sources in its generated +// project. Remove the former copy before Kotlin compilation after this move. +def removeLegacyKotlinBridgeSource = tasks.register("removeLegacyKotlinBridgeSource", Delete) { + delete file("$projectDir/src/main/java/com/faceclaw/shared/KotlinBridge.kt") + // Explicit, reviewed source moves: never infer deletions from class names. + def migrations = new groovy.json.JsonSlurper().parse(file("$rootDir/../../native/kotlin/migrated-java-sources.json")) + migrations.each { migration -> + if (!file("$rootDir/../../native/kotlin/${migration.kotlin}").isFile()) { + throw new GradleException("Missing Kotlin replacement: ${migration.kotlin}") + } + delete file("$projectDir/src/main/java/${migration.java}") + } +} +tasks.matching { it.name.startsWith("compile") && it.name.endsWith("Kotlin") }.configureEach { + dependsOn removeLegacyKotlinBridgeSource +} + tasks.matching { it.name == "preBuild" }.configureEach { + dependsOn removeLegacyKotlinBridgeSource dependsOn tasks.named("prepareFaceclawNativeLibs") dependsOn tasks.named("prepareFaceclawLlama") } diff --git a/App_Resources/Android/before-plugins.gradle b/App_Resources/Android/before-plugins.gradle index 8ebcb235..da763a5b 100644 --- a/App_Resources/Android/before-plugins.gradle +++ b/App_Resources/Android/before-plugins.gradle @@ -10,7 +10,7 @@ project.ext { // androidXTransition = "1.4.1" // androidXViewPager = "1.0.0" - // Keep the host compiler/runtime compatible with native/kotlin/build.gradle.kts. - // NativeScript 9 already applies kotlin-android. + // NativeScript 9 already applies kotlin-android. Match the shared KMP compiler + // for our .kt sources; no separate Kotlin installation is required. kotlinVersion = "2.4.20" } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.java b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.java new file mode 100644 index 00000000..72757990 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.java @@ -0,0 +1,59 @@ +package com.faceclaw.app; + +import android.media.audiofx.*; +import java.util.ArrayList; + +/** Android AudioRecord effect attachment; PCM processing lives in shared Kotlin. */ +public final class AndroidNoiseEffects { + /** Whether this Android build ships the built-in capture noise suppressor. */ + public static boolean platformSuppressorAvailable() { + try { + return NoiseSuppressor.isAvailable(); + } catch (Throwable t) { + return false; + } + } + + /** Short engine name for the UI. */ + public static String engineDescription() { + return "spectral (phone DSP)"; + } + + /** + * Attach the platform's built-in capture-effect chain (NoiseSuppressor, + * AcousticEchoCanceler, AutomaticGainControl — whichever this Android + * build provides) to a phone AudioRecord session. Only applicable when the + * phone itself captures the audio; the BLE glasses stream must go through + * {@link #process(byte[])} instead. The caller keeps the returned effects + * for the life of the recording and release()s them with it. + */ + public static AudioEffect[] attachPlatformEffects(int audioSessionId) { + ArrayList effects = new ArrayList<>(); + try { + if (NoiseSuppressor.isAvailable()) { + NoiseSuppressor ns = NoiseSuppressor.create(audioSessionId); + if (ns != null) { + ns.setEnabled(true); + effects.add(ns); + } + } + if (AcousticEchoCanceler.isAvailable()) { + AcousticEchoCanceler aec = AcousticEchoCanceler.create(audioSessionId); + if (aec != null) { + aec.setEnabled(true); + effects.add(aec); + } + } + if (AutomaticGainControl.isAvailable()) { + AutomaticGainControl agc = AutomaticGainControl.create(audioSessionId); + if (agc != null) { + agc.setEnabled(true); + effects.add(agc); + } + } + } catch (Throwable t) { + // A missing effect just means this tier is unavailable. + } + return effects.toArray(new AudioEffect[0]); + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmListener.java deleted file mode 100644 index 4d9008f0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.faceclaw.app; - -/** Phone-side alarm actions (dismiss / snooze), delivered to the JS engine; implemented in JS. */ -public interface FaceclawAlarmListener { - void onPhoneAction(long id, String action, int minutes); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAmbientLightListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAmbientLightListener.java deleted file mode 100644 index bc6bc272..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAmbientLightListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawAmbientLightListener { - /** - * A CFW ambient-light report (settings-channel field 105) from the master - * temple. body is the raw 24-byte ['A','L',ver,reason,...] record; it is - * decoded on the TS side (app/native/ambient-light.ts). - */ - void onAmbientLight(byte[] body); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioPacketListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioPacketListener.java deleted file mode 100644 index 16aa0702..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioPacketListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawAudioPacketListener { - void onAudioPacket(byte[] data, String arm, long arrivalMs); -} 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 0d38e59a..7b5436da 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 @@ -154,7 +154,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private int lastAudioControlAckMagic = 0; private ConnectionOptions connectionOptions = new ConnectionOptions(); - private final BleMagicPool magicPool = new BleMagicPool(); + private final BleMagicPool magicPool = new BleMagicPool(AndroidProtocolPlatform.INSTANCE); private MessageBuilder messageBuilder = new MessageBuilder(magicPool); private int nextTransportSeq = 0x40; private int nextMapSessionId = 0; @@ -267,7 +267,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private final TextureCacheState textureCache = new TextureCacheState(); private final ArrayDeque pendingMessages = new ArrayDeque<>(); - private final CfwTransport[] cfwTransports = { new CfwTransport(), new CfwTransport() }; + private final CfwTransport[] cfwTransports = { new CfwTransport(AndroidProtocolPlatform.INSTANCE), new CfwTransport(AndroidProtocolPlatform.INSTANCE) }; private final ArrayDeque inFlightMessages = new ArrayDeque<>(); private OutboundMessage prewrittenMessage; private List prewrittenFrames = Collections.emptyList(); @@ -360,6 +360,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { activeInstance = null; } disconnect(); + for (CfwTransport transport : cfwTransports) transport.close(); if (phoneLockReceiverRegistered) { phoneLockReceiverRegistered = false; appContext.unregisterReceiver(phoneLockReceiver); @@ -1231,7 +1232,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { + (glyphs == null ? " (no glyph draws)" : "")); FrameTimings.getInstance().spanStart(frameId, "composite"); SurfaceCompositor.Composite composite = compositor.applyAndComposite( - surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs); + surfaceId, new AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint, + glyphs == null ? null : new AndroidByteReader(glyphs)); FrameTimings.getInstance().spanEnd(frameId, "composite"); // Pack the composited 8bpp buffer down to the headerless 4bpp frame // format the wire planners consume; BMP framing is added later only for @@ -2622,7 +2624,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { writeAddress, BleProtocol.WRITE_CHAR_UUID, frames, - ConnectionOptions.WRITE_TYPE, + AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS ); @@ -2669,7 +2671,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { writeAddress, BleProtocol.WRITE_CHAR_UUID, prefixFrames, - ConnectionOptions.WRITE_TYPE, + AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS ); if (!result) { @@ -2705,7 +2707,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { writeAddress, BleProtocol.WRITE_CHAR_UUID, Collections.singletonList(finalFrame), - ConnectionOptions.WRITE_TYPE, + AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS ); } @@ -3433,7 +3435,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { if (operation == BleProtocol.FACECLAW_WAKE_OP_ACQUIRE) { lastFaceclawWakeLeaseQueuedAtMs = SystemClock.elapsedRealtime(); } - Runnable onSent = () -> { + MessageCallback onSent = () -> { if (faceclawWakeControlGeneration == generation) { faceclawWakeControlSentCount += 1; } @@ -3489,7 +3491,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { if (operation == BleProtocol.FACECLAW_FB_OP_ACQUIRE) { lastFaceclawFramebufferLeaseQueuedAtMs = SystemClock.elapsedRealtime(); } - Runnable onSent = () -> { + MessageCallback onSent = () -> { if (faceclawFramebufferControlGeneration == generation) { faceclawFramebufferControlSentCount += 1; } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java deleted file mode 100644 index 8a6ed3d0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawBleCommunicatorListener { - void onLog(String line); - void onStateChange(String phase, String status); - void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId); - void onBatteryState(int headsetBattery, int headsetCharging, int ringBattery, int ringCharging); - void onSilentMode(boolean silent); - void onWearState(boolean wearing); - void onPhoneLockState(boolean locked); - void onEvenAppConflict(String message); - void onFrameMetrics(int paintMs, int transmitMs, int tileCount); - void onFrameFinished(int frameId, String outcome); - void onFirmwareInfo(String leftVersion, String rightVersion, String extension); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleListener.java deleted file mode 100644 index 2a64c2fd..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawBleListener { - void onNotification(String address, String characteristicUuid, byte[] data); - void onConnectionStateChange(String address, boolean connected); -} 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 b8ecc1df..7531a50e 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 @@ -268,7 +268,7 @@ public class FaceclawBleManager { return false; } - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(BleProtocol.CCCD_UUID); + BluetoothGattDescriptor descriptor = characteristic.getDescriptor(java.util.UUID.fromString(BleProtocol.CCCD_UUID)); if (descriptor == null) { descriptorLatches.remove(address); return true; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngineListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngineListener.java deleted file mode 100644 index 24b58a85..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngineListener.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawCaptionEngineListener { - /** - * One finished utterance. text may be empty when no ASR model is loaded - * or recognition produced nothing; embedding is the L2-normalized - * voice-print or null when the speaker model is unavailable or the - * utterance was too short. startMs/endMs are on the engine's sample - * clock (milliseconds of audio since start()). - */ - void onUtterance(String text, float[] embedding, long startMs, long endMs, double peakRms); - - /** A speech onset was detected; the utterance is now being collected. */ - void onSpeechStart(long startMs); - - void onStatus(String status); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCompassListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCompassListener.java deleted file mode 100644 index 1d6bb06e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCompassListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.faceclaw.app; - -/** Receives stock compass heading and calibration notifications on the thread that subscribed. */ -public interface FaceclawCompassListener { - void onCompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies, - int orientationSource, int diagnosticFlags, long sampleTimeMs); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscoveryListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscoveryListener.java deleted file mode 100644 index 922e7adb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscoveryListener.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks from FaceclawDeviceDiscovery's live scan to the TypeScript layer. */ -public interface FaceclawDeviceDiscoveryListener { - /** - * One advertisement (or one bonded device) as a JSON object: - * {address, name, manufacturerData (hex, company id included), rssi|null, - * txPower|null, connectable|null, bonded, source, seenAtMs}. - * Protocol decoding happens in TypeScript (see app/g2/even-advertisement.ts). - */ - void onAdvertisement(String json); - - /** The platform refused to start or continue the scan. */ - void onScanFailed(int errorCode, String message); - - /** Diagnostic line for the pairing log. */ - void onLog(String line); -} 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 c4ad3efe..7e7e5991 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 @@ -362,7 +362,7 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { boolean written; try { written = bleManager.writeFrames( - address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS); + address, BleProtocol.WRITE_CHAR_UUID, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS); } catch (IllegalStateException e) { // "Not connected" — the link went away underneath us. written = false; @@ -475,7 +475,7 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { address, sid, flag, magic, seq, payload.length)); List frames = BleProtocol.framePb(payload, sid, flag, seq); boolean written = bleManager.writeFrames( - address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS); + address, BleProtocol.WRITE_CHAR_UUID, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS); if (!written) { emitLog("tx write FAILED sid=0x" + Integer.toHexString(sid)); synchronized (lock) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java deleted file mode 100644 index 56ff2d61..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.faceclaw.app; - -/** - * Callbacks from a hosted EvenHub app's WebView to the TypeScript host. - * All methods are delivered on the Android main thread (NativeScript's JS - * thread), regardless of which WebView-internal thread produced them. - */ -public interface FaceclawEvenHubListener { - /** - * The app invoked window.flutter_inappwebview.callHandler(handlerName, ...args). - * argsJson is the JSON-encoded argument array; callId identifies the - * promise to resolve via window.__fcResolve(callId, ok, value). - */ - void onEvenAppMessage(String handlerName, String argsJson, int callId); - - /** The WebView finished loading the app's page. */ - void onPageFinished(String url); -} 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 84d66a4f..3a941ac6 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 @@ -301,7 +301,7 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { private boolean writeOta(String address, String writeChar, int sid, byte[] payload, int seq) { List frames = BleProtocol.framePb(payload, sid, FLAG_OTA, seq); return bleManager.writeFrames( - address, writeChar, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS); + address, writeChar, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS); } private int waitAck(int wantOp, int timeoutMs) throws TimeoutException { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasherListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasherListener.java deleted file mode 100644 index 2cd24e20..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasherListener.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks from FaceclawFirmwareFlasher to the TypeScript layer. */ -public interface FaceclawFirmwareFlasherListener { - void onLog(String line); - - /** - * Fine-grained progress for the UI bar. `lens` is "left"/"right"; - * component/block indices are 1-based against their counts. `bytesSent` / - * `bytesTotal` cover the whole lens image, so a bar driven by them moves - * evenly even though the components differ wildly in size. - */ - void onProgress(String lens, int componentIndex, int componentCount, int blockIndex, int blockCount, - long bytesSent, long bytesTotal); - - /** - * Coarse lifecycle. `state` is one of: validating, connecting, flashing, - * rebooting, done, error. `detail` carries the lens name or a message. - */ - void onState(String state, String detail); - - /** Terminal callback: success or failure, with an explanatory message. */ - void onComplete(boolean success, String detail); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareUtil.java deleted file mode 100644 index 32ce5dc1..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareUtil.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.faceclaw.app; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * Native helpers for the on-device custom-firmware build: SHA-256 hashing and - * binary file writing. Both take a java.nio.ByteBuffer so NativeScript can - * marshal a JS ArrayBuffer straight across without a per-element copy of the - * ~4 MB firmware image (and with no overload ambiguity). Also home of the one - * shared byte→hex encoder for com.faceclaw.app. - */ -public final class FaceclawFirmwareUtil { - private FaceclawFirmwareUtil() {} - - /** Lowercase hex of the bytes. The TS-side decoder is case-insensitive. */ - public static String bytesToHex(byte[] bytes) { - if (bytes == null || bytes.length == 0) { - return ""; - } - StringBuilder sb = new StringBuilder(bytes.length * 2); - for (byte b : bytes) { - int v = b & 0xff; - sb.append(Character.forDigit(v >>> 4, 16)); - sb.append(Character.forDigit(v & 0x0f, 16)); - } - return sb.toString(); - } - - /** Lowercase hex SHA-256 of the buffer's remaining bytes. */ - public static String sha256Hex(ByteBuffer data) { - try { - MessageDigest md = MessageDigest.getInstance("SHA-256"); - md.update(data.duplicate()); - return bytesToHex(md.digest()); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException("SHA-256 unavailable", e); - } - } - - /** Write the buffer's remaining bytes to `path`, overwriting any existing file. */ - public static void writeFile(String path, ByteBuffer data) { - try (FileOutputStream fos = new FileOutputStream(path); - FileChannel channel = fos.getChannel()) { - ByteBuffer view = data.duplicate(); - while (view.hasRemaining()) { - channel.write(view); - } - } catch (IOException e) { - throw new RuntimeException("firmware write failed: " + e.getMessage(), e); - } - } -} 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 239e290f..ef342706 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 @@ -362,7 +362,7 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { address, BleProtocol.WRITE_CHAR_UUID, frames, - ConnectionOptions.WRITE_TYPE, + AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS); } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptListener.java deleted file mode 100644 index a7ac2da3..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptListener.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks from FaceclawFlashPromptCommunicator to the TypeScript layer. */ -public interface FaceclawFlashPromptListener { - void onLog(String line); - - /** - * Lifecycle updates. `state` is one of: connecting, connected, prompting, - * battery, result, cancelled, timeout, disconnected, error. `detail` carries the - * "approved"/"declined" text for result, or an error/explanation message. - */ - void onState(String state, String detail); - - /** - * Battery percent of each arm, read after the user approved (or right - * after auth when the prompt is skipped); -1 when an arm didn't answer. - * Always fires before onResult(true). - */ - void onBattery(int rightPercent, int leftPercent); - - /** Fires once the user picks a menu row (or, with skipPrompt, once the battery is read): true = flash, false = cancel. */ - void onResult(boolean approved); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawImuListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawImuListener.java deleted file mode 100644 index 2be8bb81..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawImuListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -/** - * Receives IMU (accelerometer) readings decoded from the glasses' sys-event - * stream. Registered on FaceclawBleCommunicator via addImuListener; callbacks - * are delivered on the Android main thread. - */ -public interface FaceclawImuListener { - void onImuData(double x, double y, double z, int eventSource); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaListener.java deleted file mode 100644 index f5419b84..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaListener.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.faceclaw.app; - -/** - * Callbacks for one on-phone LLM generation. Like the other bridge listeners, - * implementations are typically created on the NativeScript JS thread and - * FaceclawLlamaRunner posts these callbacks back to that thread's Looper. - */ -public interface FaceclawLlamaListener { - /** A piece of generated text (may be a partial word). */ - void onToken(String piece); - - /** Generation finished: "stop" (EOG), "length", or "cancelled". */ - void onDone(String stopReason); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationListener.java deleted file mode 100644 index 84aead80..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.faceclaw.app; - -/** One-shot Android location callbacks for the TypeScript Weather bridge. */ -public interface FaceclawLocationListener { - void onLocation(double latitude, double longitude, float accuracyMeters, long timestampMs); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTrackerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTrackerListener.java deleted file mode 100644 index ff0fd1aa..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTrackerListener.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.faceclaw.app; - -/** Continuous location callbacks for the TypeScript navigation bridge. */ -public interface FaceclawLocationTrackerListener { - /** - * A new fix. bearingDeg/speedMps are -1 when the fix doesn't carry them - * (common when stationary); accuracyMeters is -1 when unknown. - */ - void onLocation(double latitude, double longitude, float accuracyMeters, - float bearingDeg, float speedMps, long timestampMs); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowserListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowserListener.java deleted file mode 100644 index d25c6758..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowserListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawMediaBrowserListener { - void onConnectResult(int requestId, boolean connected, String rootId, String error); - - void onBrowseResult(int requestId, String childrenJson, String error); - - /** The service connection dropped after connecting (player crashed or was killed). */ - void onDisconnected(); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaControllerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaControllerListener.java deleted file mode 100644 index 75909f0e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaControllerListener.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawMediaControllerListener { - /** All observed session owners, before filtering or choosing the active player. */ - void onSessionAppsChanged(String appsJson); - - void onStateChange( - String playbackState, - String packageName, - String appName, - String title, - String artist, - String album, - long positionMs, - long durationMs, - float playbackSpeed, - boolean canPlayPause, - boolean canSkipNext, - boolean canSkipPrevious, - boolean accessEnabled, - String status - ); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMicStatusListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMicStatusListener.java deleted file mode 100644 index e739ea34..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMicStatusListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawMicStatusListener { - /** - * A CFW mic_control field-104 status record from one temple. body is the - * raw 21-byte ['M','C',ver,...] record (decoded on the TS side); arm is - * "L" or "R" for the temple's own BLE link, "?" if unmatched. - */ - void onMicStatus(byte[] body, String arm); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloaderListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloaderListener.java deleted file mode 100644 index 90bf9380..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloaderListener.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.faceclaw.app; - -/** Progress callbacks for FaceclawModelDownloader, posted to the creating thread's Looper. */ -public interface FaceclawModelDownloaderListener { - void onProgress(long bytesDownloaded, long totalBytes); - - /** The file is fully downloaded, verified, and moved to its final path. */ - void onDone(String path); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNoiseSuppressor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNoiseSuppressor.java deleted file mode 100644 index 5d9514dc..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNoiseSuppressor.java +++ /dev/null @@ -1,266 +0,0 @@ -package com.faceclaw.app; - -import android.media.audiofx.AcousticEchoCanceler; -import android.media.audiofx.AudioEffect; -import android.media.audiofx.AutomaticGainControl; -import android.media.audiofx.NoiseSuppressor; - -import java.util.ArrayList; -import java.util.Arrays; - -/** - * Phone-side noise suppression for the microphone stream. - * - * Android's built-in noise cancellation (the {@link NoiseSuppressor} capture - * effect) only attaches to AudioRecord sessions on the phone's own microphones. Faceclaw's audio - * arrives as raw PCM over BLE from the glasses, which that effect chain never - * sees, so this class picks the best engine that can actually reach the - * samples: - * - * - Audio captured by the phone itself: {@link #attachPlatformEffects(int)} - * enables the device's built-in NS/AEC/AGC chain (hardware or OEM software, - * whatever the Android build ships) on the AudioRecord session. - * - Audio from the glasses (the normal case): {@link #process(byte[])} runs a - * WebRTC-NS-family spectral suppressor here on the phone — 16 ms sqrt-Hann - * frames at 50% overlap, per-bin asymmetric noise-floor tracking, and an - * over-subtracted Wiener gain with a residual floor so speech never pumps - * in and out of silence. - */ -public final class FaceclawNoiseSuppressor { - - private static final int FFT_SIZE = 256; - private static final int HOP = FFT_SIZE / 2; - private static final float OVERSUBTRACTION = 1.6f; - /** About -16 dB of residual: keeps the noise bed natural instead of gated. */ - private static final float GAIN_FLOOR = 0.15f; - private static final float GAIN_SMOOTHING = 0.6f; - /** Frames of pure noise-floor learning after start/reset (~100 ms). */ - private static final int NOISE_WARMUP_FRAMES = 12; - - private final int sampleRate; - private final float[] window = new float[FFT_SIZE]; - private final float[] history = new float[FFT_SIZE]; - private final float[] overlap = new float[HOP]; - private final float[] noise = new float[FFT_SIZE / 2 + 1]; - private final float[] gain = new float[FFT_SIZE / 2 + 1]; - private final float[] re = new float[FFT_SIZE]; - private final float[] im = new float[FFT_SIZE]; - private final short[] pending = new short[HOP]; - private int pendingCount = 0; - private int warmupFrames = 0; - - public FaceclawNoiseSuppressor(int sampleRate) { - this.sampleRate = sampleRate; - // sqrt-Hann (sine) analysis+synthesis windows: exact COLA at 50% overlap. - for (int i = 0; i < FFT_SIZE; i++) { - window[i] = (float) Math.sin(Math.PI * (i + 0.5) / FFT_SIZE); - } - reset(); - } - - public int getSampleRate() { - return sampleRate; - } - - /** Forget the learned noise floor (e.g. after the suppressor was toggled off). */ - public void reset() { - Arrays.fill(history, 0f); - Arrays.fill(overlap, 0f); - Arrays.fill(noise, 0f); - Arrays.fill(gain, 1f); - pendingCount = 0; - warmupFrames = 0; - } - - /** - * Suppress noise in 16-bit little-endian mono PCM. Streaming: input not - * filling a whole 8 ms hop is carried into the next call, so the output - * may be up to one hop shorter than the input (and longer on a later - * call); total latency is one hop. - */ - public byte[] process(byte[] pcm16le) { - if (pcm16le == null || pcm16le.length < 2) { - return pcm16le; - } - int sampleCount = pcm16le.length / 2; - int hops = (pendingCount + sampleCount) / HOP; - byte[] out = new byte[hops * HOP * 2]; - int outPos = 0; - int inPos = 0; - while (inPos < sampleCount) { - int take = Math.min(HOP - pendingCount, sampleCount - inPos); - for (int i = 0; i < take; i++) { - int lo = pcm16le[(inPos + i) * 2] & 0xff; - int hi = pcm16le[(inPos + i) * 2 + 1]; - pending[pendingCount + i] = (short) ((hi << 8) | lo); - } - pendingCount += take; - inPos += take; - if (pendingCount == HOP) { - outPos = processHop(out, outPos); - pendingCount = 0; - } - } - return out; - } - - private int processHop(byte[] out, int outPos) { - System.arraycopy(history, HOP, history, 0, FFT_SIZE - HOP); - for (int i = 0; i < HOP; i++) { - history[FFT_SIZE - HOP + i] = pending[i] / 32768f; - } - for (int i = 0; i < FFT_SIZE; i++) { - re[i] = history[i] * window[i]; - im[i] = 0f; - } - fft(re, im, false); - - boolean warming = warmupFrames < NOISE_WARMUP_FRAMES; - if (warming) { - warmupFrames++; - } - for (int b = 0; b <= FFT_SIZE / 2; b++) { - float power = re[b] * re[b] + im[b] * im[b]; - if (warming) { - noise[b] = warmupFrames == 1 ? power : noise[b] + (power - noise[b]) / warmupFrames; - } else if (power < noise[b]) { - // Track the floor down quickly; drift up slowly so speech - // energy never gets learned as noise. - noise[b] += (power - noise[b]) * 0.2f; - } else { - noise[b] = Math.min(power, noise[b] * 1.006f + 1e-10f); - } - float clean = power - OVERSUBTRACTION * noise[b]; - float g = clean > 0f ? clean / power : 0f; - if (g < GAIN_FLOOR) { - g = GAIN_FLOOR; - } - g = GAIN_SMOOTHING * gain[b] + (1f - GAIN_SMOOTHING) * g; - gain[b] = g; - re[b] *= g; - im[b] *= g; - if (b > 0 && b < FFT_SIZE / 2) { - re[FFT_SIZE - b] *= g; - im[FFT_SIZE - b] *= g; - } - } - - fft(re, im, true); - for (int i = 0; i < HOP; i++) { - float value = (overlap[i] + re[i] * window[i]) * 32767f; - int sample = Math.round(value); - if (sample > 32767) { - sample = 32767; - } - if (sample < -32768) { - sample = -32768; - } - out[outPos + i * 2] = (byte) (sample & 0xff); - out[outPos + i * 2 + 1] = (byte) ((sample >> 8) & 0xff); - } - for (int i = 0; i < HOP; i++) { - overlap[i] = re[HOP + i] * window[HOP + i]; - } - return outPos + HOP * 2; - } - - /** In-place iterative radix-2 FFT (inverse includes the 1/N scale). */ - private static void fft(float[] re, float[] im, boolean inverse) { - int n = re.length; - for (int i = 1, j = 0; i < n; i++) { - int bit = n >> 1; - for (; (j & bit) != 0; bit >>= 1) { - j ^= bit; - } - j ^= bit; - if (i < j) { - float t = re[i]; - re[i] = re[j]; - re[j] = t; - t = im[i]; - im[i] = im[j]; - im[j] = t; - } - } - for (int len = 2; len <= n; len <<= 1) { - double ang = 2 * Math.PI / len * (inverse ? 1 : -1); - float wRe = (float) Math.cos(ang); - float wIm = (float) Math.sin(ang); - for (int i = 0; i < n; i += len) { - float curRe = 1f; - float curIm = 0f; - for (int k = 0; k < len / 2; k++) { - int a = i + k; - int b = a + len / 2; - float xr = re[b] * curRe - im[b] * curIm; - float xi = re[b] * curIm + im[b] * curRe; - re[b] = re[a] - xr; - im[b] = im[a] - xi; - re[a] += xr; - im[a] += xi; - float nextRe = curRe * wRe - curIm * wIm; - curIm = curRe * wIm + curIm * wRe; - curRe = nextRe; - } - } - } - if (inverse) { - for (int i = 0; i < n; i++) { - re[i] /= n; - im[i] /= n; - } - } - } - - /** Whether this Android build ships the built-in capture noise suppressor. */ - public static boolean platformSuppressorAvailable() { - try { - return NoiseSuppressor.isAvailable(); - } catch (Throwable t) { - return false; - } - } - - /** Short engine name for the UI. */ - public static String engineDescription() { - return "spectral (phone DSP)"; - } - - /** - * Attach the platform's built-in capture-effect chain (NoiseSuppressor, - * AcousticEchoCanceler, AutomaticGainControl — whichever this Android - * build provides) to a phone AudioRecord session. Only applicable when the - * phone itself captures the audio; the BLE glasses stream must go through - * {@link #process(byte[])} instead. The caller keeps the returned effects - * for the life of the recording and release()s them with it. - */ - public static AudioEffect[] attachPlatformEffects(int audioSessionId) { - ArrayList effects = new ArrayList<>(); - try { - if (NoiseSuppressor.isAvailable()) { - NoiseSuppressor ns = NoiseSuppressor.create(audioSessionId); - if (ns != null) { - ns.setEnabled(true); - effects.add(ns); - } - } - if (AcousticEchoCanceler.isAvailable()) { - AcousticEchoCanceler aec = AcousticEchoCanceler.create(audioSessionId); - if (aec != null) { - aec.setEnabled(true); - effects.add(aec); - } - } - if (AutomaticGainControl.isAvailable()) { - AutomaticGainControl agc = AutomaticGainControl.create(audioSessionId); - if (agc != null) { - agc.setEnabled(true); - effects.add(agc); - } - } - } catch (Throwable t) { - // A missing effect just means this tier is unavailable. - } - return effects.toArray(new AudioEffect[0]); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNotificationListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNotificationListener.java deleted file mode 100644 index 5dd6687a..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNotificationListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawNotificationListener { - void onNotificationPosted(String key); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java index e0aaae58..b95f9061 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java @@ -105,7 +105,8 @@ public final class FaceclawPreviewCompositor { java.nio.ByteBuffer glyphs ) { compositor.applyAndComposite( - surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs); + surfaceId, new AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint, + glyphs == null ? null : new AndroidByteReader(glyphs)); FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)"); Runnable listener = frameListener; if (listener != null) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java deleted file mode 100644 index b255991e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks for one FaceclawQrScanner.scan; exactly one method fires. */ -public interface FaceclawQrScannerListener { - void onResult(String text); - - void onCancelled(); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRingEventDecoder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRingEventDecoder.java deleted file mode 100644 index 9cdc44f0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRingEventDecoder.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.faceclaw.app; - -import java.util.Locale; - -final class FaceclawRingEventDecoder { - private FaceclawRingEventDecoder() { - } - - static DirectRingEvent decode(byte[] raw) { - if (raw == null || raw.length == 0) { - return null; - } - - DirectRingEvent chargerGesture = decodeChargerGesture(raw); - if (chargerGesture != null) { - return chargerGesture; - } - - return decodeDirectGesture(raw); - } - - private static DirectRingEvent decodeChargerGesture(byte[] raw) { - if (raw.length != 11) { - return null; - } - if (u8(raw[0]) != 0x00 || u8(raw[1]) != 0x09 || u8(raw[2]) != 0x61 || u8(raw[3]) != 0x00) { - return null; - } - - int code = u8(raw[4]); - int param = u8(raw[5]) | (u8(raw[6]) << 8); - long tick = (u8(raw[7]) | (u8(raw[8]) << 8) | (u8(raw[9]) << 16) | (u8(raw[10]) << 24)) & 0xffffffffL; - String detail = String.format(Locale.US, "code=0x%02x(%s) param=0x%04x tick=%d", code, chargerCodeName(code), param, tick); - - switch (code) { - case 0x00: - return event(BleProtocol.EVENT_RING_LONG_PRESS, "LONG_PRESS", detail); - case 0x01: - return event(BleProtocol.EVENT_CLICK, "TAP", detail); - case 0x02: - return event(BleProtocol.EVENT_DOUBLE_CLICK, "DOUBLE_TAP", detail); - case 0x04: - return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_UP", detail); - case 0x05: - return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_DOWN", detail); - case 0x08: - return event(BleProtocol.EVENT_RING_LONG_PRESS_RELEASE, "LONG_PRESS_RELEASE", detail); - default: - return null; - } - } - - private static DirectRingEvent decodeDirectGesture(byte[] raw) { - if (raw.length != 3 || u8(raw[0]) != 0xff) { - return null; - } - - int type = u8(raw[1]); - int param = u8(raw[2]); - String detail = String.format(Locale.US, "type=0x%02x param=0x%02x", type, param); - - if (type == 0x03 && param == 0x20) { - return event(BleProtocol.EVENT_RING_LONG_PRESS, "HOLD", detail); - } - if (type == 0x04 && param == 0x01) { - return event(BleProtocol.EVENT_CLICK, "TAP_SINGLE", detail); - } - if (type == 0x04 && param == 0x02) { - return event(BleProtocol.EVENT_DOUBLE_CLICK, "TAP_DOUBLE", detail); - } - if (type == 0x05) { - if (param <= 0x01) { - return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_FORWARD", detail); - } - return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_BACKWARD", detail); - } - - return null; - } - - private static DirectRingEvent event(int eventType, String label, String detail) { - G2Event event = new G2Event( - "sys-event", - "", - eventType, - BleProtocol.EVENT_SOURCE_RING, - 0 - ); - return new DirectRingEvent(event, label, detail); - } - - private static String chargerCodeName(int code) { - switch (code) { - case 0x00: - return "LONG_PRESS"; - case 0x01: - return "TAP"; - case 0x02: - return "DOUBLE_TAP"; - case 0x04: - return "SWIPE_UP"; - case 0x05: - return "SWIPE_DOWN"; - case 0x08: - return "LONG_PRESS_RELEASE"; - default: - return "unknown"; - } - } - - private static int u8(byte value) { - return value & 0xff; - } - - static final class DirectRingEvent { - final G2Event event; - final String label; - final String detail; - - DirectRingEvent(G2Event event, String label, String detail) { - this.event = event; - this.label = label; - this.detail = detail; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsListener.java deleted file mode 100644 index a34bd7ab..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.faceclaw.app; - -/** Change-notification callback for FaceclawSettings; implemented in JS. */ -public interface FaceclawSettingsListener { - void onSettingChanged(String key); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceControllerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceControllerListener.java deleted file mode 100644 index 95c5826f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceControllerListener.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawVoiceControllerListener { - void onStatus(String status); - - /** - * Best transcript of the current utterance so far. REPLACE semantics: text - * is the complete transcript, not a delta — display it verbatim, replacing - * any previous partial. isFinal marks the end of the utterance. - */ - void onTranscript(String text, boolean isFinal); - - /** Capture has finished, after the final transcript or a startup/recognition error. */ - void onStopped(int captureId); - - /** - * Decoded microphone audio: 16 kHz mono signed 16-bit little-endian PCM. - * Emitted in every mode so raw-PCM consumers (levels, recording, captions) - * keep working while the onboard recognizer owns the transcript. - */ - void onPcm(byte[] pcm16le); - - /** - * Per-packet DSP metadata the glasses firmware appends to each 50 ms audio - * packet, computed on the raw stereo capture before its mono downmix: the - * direction-of-arrival in signed degrees (0 = straight ahead, positive to - * the wearer's right) and a signal-strength ratio that serves as a - * confidence/energy proxy for the angle. - */ - void onFrameMeta(int angleDegrees, int ssr); - - /** - * The speaker stopped, in a hands-free session that has no button release - * to end it. Only fires when endpointing was enabled for the session; see - * FaceclawVoiceController.setEndpointing. - */ - void onSpeechEnd(); - - /** - * Result of speaker verification against the enrolled wearer voice-print, - * emitted once per capture session just before the final transcript. - * Only fires when verification was configured for the session; fails - * open (isWearer=true) when the utterance was too short to verify or the - * speaker model could not be loaded. - */ - void onSpeakerVerified(boolean isWearer, float similarity); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWavRecorder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWavRecorder.java deleted file mode 100644 index ed08d388..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWavRecorder.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; - -/** - * Streaming WAV writer for conversation recordings: the header is written up - * front with zero sizes and patched on finish, so a crash mid-session leaves - * a recoverable file (data length can be derived from the file size). - */ -public class FaceclawWavRecorder { - private static final String TAG = "FaceclawWavRecorder"; - - private final String path; - private final int sampleRate; - private final int channels; - private RandomAccessFile file; - private long dataBytes; - - public FaceclawWavRecorder(String path, int sampleRate, int channels) throws IOException { - this.path = path; - this.sampleRate = sampleRate; - this.channels = channels; - File parent = new File(path).getParentFile(); - if (parent != null && !parent.exists() && !parent.mkdirs()) { - throw new IOException("could not create " + parent); - } - file = new RandomAccessFile(path, "rw"); - file.setLength(0); - file.write(buildHeader(0)); - } - - public synchronized void append(byte[] pcm16le) { - if (file == null || pcm16le == null || pcm16le.length == 0) { - return; - } - try { - file.write(pcm16le); - dataBytes += pcm16le.length; - } catch (IOException e) { - Log.w(TAG, "append failed", e); - } - } - - public synchronized long getDurationMs() { - return dataBytes * 1000L / (sampleRate * channels * 2L); - } - - public String getPath() { - return path; - } - - /** Patch the header and close. Returns the PCM byte count (0 = empty). */ - public synchronized long finish() { - RandomAccessFile current = file; - file = null; - if (current == null) { - return dataBytes; - } - try { - current.seek(0); - current.write(buildHeader((int) Math.min(dataBytes, Integer.MAX_VALUE))); - current.close(); - } catch (IOException e) { - Log.w(TAG, "finish failed", e); - } - return dataBytes; - } - - /** Delete the (finished or abandoned) recording. */ - public synchronized void discard() { - finish(); - new File(path).delete(); - } - - private byte[] buildHeader(int pcmBytes) { - int byteRate = sampleRate * channels * 2; - ByteBuffer b = ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN); - b.put("RIFF".getBytes(StandardCharsets.US_ASCII)); - b.putInt(36 + pcmBytes); - b.put("WAVE".getBytes(StandardCharsets.US_ASCII)); - b.put("fmt ".getBytes(StandardCharsets.US_ASCII)); - b.putInt(16); - b.putShort((short) 1); - b.putShort((short) channels); - b.putInt(sampleRate); - b.putInt(byteRate); - b.putShort((short) (channels * 2)); - b.putShort((short) 16); - b.put("data".getBytes(StandardCharsets.US_ASCII)); - b.putInt(pcmBytes); - return b.array(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListener.java deleted file mode 100644 index 590c87d6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListener.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.faceclaw.app; - -/** - * Callbacks from the Android Wear (Wear OS) bridge; implemented in JS. Every - * call is delivered on the thread that registered the listener (see - * FaceclawWearBridge.setListener). - */ -public interface FaceclawWearListener { - /** - * A message from the watch app. `path` is the Data Layer message path - * (e.g. "/faceclaw/input"); `json` is the UTF-8 payload (always a JSON - * object, "{}" when the watch sent none); `nodeId` identifies the watch - * so a reply can be addressed to it. - */ - void onMessage(String path, String json, String nodeId); - - /** The set of reachable watches running the Faceclaw watch app changed. */ - void onWatchConnection(boolean reachable, String watchName); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocketListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocketListener.java deleted file mode 100644 index f8e8733f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocketListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks for FaceclawWebSocket, delivered on the Android main thread. */ -public interface FaceclawWebSocketListener { - void onOpen(); - void onTextMessage(String message); - void onClosed(int code, String reason); - void onFailure(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java deleted file mode 100644 index c6535c0c..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.HashMap; -import java.util.Map; - -/** - * Process-wide registry of the glasses' builtin 20 px font's glyph rasters, - * fed from the TS side (EvenHubFont's extracted firmware font data). Unlike - * GlyphAtlas/ImageAtlas entries, these are never uploaded: the firmware - * already owns the font, and mode 15 draws it directly. The planner needs - * the rasters purely phone-side — for the "would this draw land correctly" - * check against the composite and for punching drawn ink out of delta rects. - * - * Placement model matches the firmware's mode-15 math: a glyph draws at - * (penX + ofsX, lineY + inkTop) where inkTop already folds in the root - * font's line height and baseline. The TS side only registers glyphs whose - * on-glasses rendering it predicts exactly (native-table glyphs; baseline- - * shifted fallback symbols stay baked). - */ -public final class FwGlyphAtlas { - private FwGlyphAtlas() {} - - public static final class Entry { - /** Draw x = pen x + ofsX. */ - public final int ofsX; - /** First raster row relative to the run's line-top y. */ - public final int inkTop; - public final int boxW; - public final int boxH; - /** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */ - final byte[] nibbles; - - Entry(int ofsX, int inkTop, int boxW, int boxH, byte[] nibbles) { - this.ofsX = ofsX; - this.inkTop = inkTop; - this.boxW = boxW; - this.boxH = boxH; - this.nibbles = nibbles; - } - - /** The 4bpp source value at (col, row); the draw skips 0 (transparent). */ - public int nibbleAt(int col, int row) { - int b = nibbles[row * ((boxW + 1) >> 1) + (col >> 1)] & 0xff; - return (col & 1) != 0 ? (b & 0x0f) : (b >> 4); - } - } - - private static final Object lock = new Object(); - private static final Map entries = new HashMap<>(); - - /** - * Register glyph rasters. Little-endian buffer of records: - * [cp u32][ofsX s16][inkTop s16][boxW u8][boxH u8] - * [boxH x ceil(boxW/2) nibble bytes] - * Re-registration of a codepoint is ignored (the builtin font is fixed). - */ - public static void register(ByteBuffer buffer) { - if (buffer == null) return; - ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN); - synchronized (lock) { - while (in.remaining() >= 10) { - int cp = in.getInt(); - int ofsX = in.getShort(); - int inkTop = in.getShort(); - int boxW = in.get() & 0xff; - int boxH = in.get() & 0xff; - byte[] nibbles = new byte[((boxW + 1) >> 1) * boxH]; - in.get(nibbles); - if (boxW > 0 && boxH > 0 && !entries.containsKey(cp)) { - entries.put(cp, new Entry(ofsX, inkTop, boxW, boxH, nibbles)); - } - } - } - } - - public static Entry get(int cp) { - synchronized (lock) { - return entries.get(cp); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java index cfba7631..1346b07e 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java @@ -1,273 +1,26 @@ package com.faceclaw.app; import android.content.Context; - -import java.io.BufferedOutputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.zip.DataFormatException; -import java.util.zip.Inflater; -/** - * Accumulates composited screen frames and saves them as a looping animated - * GIF with a 16-entry grayscale palette. Stored frames are deflate-compressed - * (a typical UI frame shrinks from 166KB of indices to a few KB) so long - * recordings stay affordable in memory; consecutive identical frames are - * deduplicated, with the elapsed time folded into the next frame's delay. - */ +/** Android storage adapter for the shared GIF recorder. */ public final class GifScreenRecorder { - /** Backstop against unbounded growth if a recording is left running. */ - private static final int MAX_FRAMES = 3600; - /** Displayed duration of the final frame, which has no successor. */ - private static final int LAST_FRAME_DELAY_CS = 100; - /** Below this, GIF renderers historically clamp or ignore the delay. */ - private static final int MIN_FRAME_DELAY_CS = 2; + private final SharedGifScreenRecorder recorder = new SharedGifScreenRecorder(); - private static final class Frame { - final byte[] deflatedIndices; - final long timestampMs; - - Frame(byte[] deflatedIndices, long timestampMs) { - this.deflatedIndices = deflatedIndices; - this.timestampMs = timestampMs; - } + public void addFrame(byte[] gray, int width, int height, long timestampMs) { + recorder.addFrame(gray, width, height, timestampMs); } - private final List frames = new ArrayList<>(); - /** Undeflated copy of the newest frame, kept for duplicate detection. */ - private byte[] lastIndices; - private int width; - private int height; - private boolean overflowed; + public boolean isOverflowed() { return recorder.isOverflowed(); } - /** - * Append the current screen. gray is 8bpp; only the top nibble (the - * display's native depth, and the palette index) is kept. - */ - public synchronized void addFrame(byte[] gray, int width, int height, long timestampMs) { - if (gray == null || width <= 0 || height <= 0 || gray.length < width * height) { - return; - } - if (frames.isEmpty()) { - this.width = width; - this.height = height; - } else if (width != this.width || height != this.height) { - return; - } - byte[] indices = new byte[width * height]; - for (int i = 0; i < indices.length; i++) { - indices[i] = (byte) ((gray[i] & 0xff) >>> 4); - } - if (lastIndices != null && Arrays.equals(indices, lastIndices)) { - return; - } - if (frames.size() >= MAX_FRAMES) { - overflowed = true; - return; - } - frames.add(new Frame(ScreenshotUtil.zlibDeflate(indices), timestampMs)); - lastIndices = indices; - } - - /** True if frames were dropped because the recording hit MAX_FRAMES. */ - public synchronized boolean isOverflowed() { - return overflowed; - } - - /** - * Encode the recording as an animated GIF in the screenshots directory. - * Returns the absolute path, or "" if no frames were captured. - */ - public synchronized String save(Context context) throws IOException { - if (frames.isEmpty()) { - return ""; - } - File file = new File(ScreenshotUtil.ensureScreenshotsDir(context), "recording-" + ScreenshotUtil.timestamp() + ".gif"); - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { - writeGif(out); - } + public String save(Context context) throws IOException { + byte[] gif = recorder.encode(); + if (gif.length == 0) return ""; + File file = new File(ScreenshotUtil.ensureScreenshotsDir(context), + "recording-" + ScreenshotUtil.timestamp() + ".gif"); + try (FileOutputStream out = new FileOutputStream(file)) { out.write(gif); } return file.getAbsolutePath(); } - - private void writeGif(OutputStream out) throws IOException { - out.write('G'); - out.write('I'); - out.write('F'); - out.write('8'); - out.write('9'); - out.write('a'); - // Logical screen descriptor: global color table present, 8 bits of - // color resolution, 2^(3+1)=16 palette entries. - writeShortLE(out, width); - writeShortLE(out, height); - out.write(0x80 | 0x70 | 0x03); - out.write(0); // background color index - out.write(0); // pixel aspect ratio - for (int i = 0; i < 16; i++) { - int v = i * 17; - out.write(v); - out.write(v); - out.write(v); - } - // Netscape application extension: loop forever. - out.write(0x21); - out.write(0xff); - out.write(11); - out.write("NETSCAPE2.0".getBytes("US-ASCII")); - out.write(3); - out.write(1); - writeShortLE(out, 0); - out.write(0); - - for (int i = 0; i < frames.size(); i++) { - Frame frame = frames.get(i); - // Each frame displays until the next one was captured; the last - // frame has no successor, so give it a nominal one-second hold. - int delayCs = LAST_FRAME_DELAY_CS; - if (i + 1 < frames.size()) { - long deltaMs = frames.get(i + 1).timestampMs - frame.timestampMs; - delayCs = (int) Math.max(MIN_FRAME_DELAY_CS, Math.min(0xffff, Math.round(deltaMs / 10.0))); - } - // Graphic control extension: no disposal, no transparency. - out.write(0x21); - out.write(0xf9); - out.write(4); - out.write(0x04); // disposal method 1 (do not dispose) - writeShortLE(out, delayCs); - out.write(0); // transparent color index (unused) - out.write(0); - // Image descriptor: full frame, no local color table. - out.write(0x2c); - writeShortLE(out, 0); - writeShortLE(out, 0); - writeShortLE(out, width); - writeShortLE(out, height); - out.write(0); - writeLzwImageData(out, inflate(frame.deflatedIndices, width * height)); - } - out.write(0x3b); // trailer - } - - private static byte[] inflate(byte[] deflated, int size) throws IOException { - Inflater inflater = new Inflater(); - inflater.setInput(deflated); - byte[] data = new byte[size]; - try { - int offset = 0; - while (offset < size && !inflater.finished()) { - int n = inflater.inflate(data, offset, size - offset); - if (n == 0) break; - offset += n; - } - if (offset != size) { - throw new IOException("recorded frame truncated: " + offset + " of " + size); - } - } catch (DataFormatException e) { - throw new IOException("recorded frame corrupt", e); - } finally { - inflater.end(); - } - return data; - } - - /** GIF LZW compression of a 4-bit index stream (minimum code size 4). */ - private static void writeLzwImageData(OutputStream out, byte[] indices) throws IOException { - final int minCodeSize = 4; - out.write(minCodeSize); - SubBlockBitWriter writer = new SubBlockBitWriter(out); - final int clearCode = 1 << minCodeSize; - final int eoiCode = clearCode + 1; - int codeSize = minCodeSize + 1; - int nextCode = eoiCode + 1; - HashMap table = new HashMap<>(); - writer.writeCode(clearCode, codeSize); - int prefix = indices[0] & 0xff; - for (int i = 1; i < indices.length; i++) { - int k = indices[i] & 0xff; - int key = (prefix << 8) | k; - Integer code = table.get(key); - if (code != null) { - prefix = code; - continue; - } - writer.writeCode(prefix, codeSize); - if (nextCode == 0x1000) { - writer.writeCode(clearCode, codeSize); - table.clear(); - codeSize = minCodeSize + 1; - nextCode = eoiCode + 1; - } else { - if (nextCode >= (1 << codeSize)) { - codeSize++; - } - table.put(key, nextCode++); - } - prefix = k; - } - writer.writeCode(prefix, codeSize); - writer.writeCode(eoiCode, codeSize); - writer.finish(); - } - - /** - * Packs LZW codes LSB-first into bytes and the bytes into the 255-byte - * data sub-blocks GIF image data is carried in. - */ - private static final class SubBlockBitWriter { - private final OutputStream out; - private final byte[] block = new byte[255]; - private int blockLength; - private int bitBuffer; - private int bitCount; - - SubBlockBitWriter(OutputStream out) { - this.out = out; - } - - void writeCode(int code, int codeSize) throws IOException { - bitBuffer |= code << bitCount; - bitCount += codeSize; - while (bitCount >= 8) { - writeByte(bitBuffer & 0xff); - bitBuffer >>>= 8; - bitCount -= 8; - } - } - - void finish() throws IOException { - if (bitCount > 0) { - writeByte(bitBuffer & 0xff); - bitBuffer = 0; - bitCount = 0; - } - flushBlock(); - out.write(0); // block terminator - } - - private void writeByte(int b) throws IOException { - block[blockLength++] = (byte) b; - if (blockLength == 255) { - flushBlock(); - } - } - - private void flushBlock() throws IOException { - if (blockLength == 0) return; - out.write(blockLength); - out.write(block, 0, blockLength); - blockLength = 0; - } - } - - private static void writeShortLE(OutputStream out, int value) throws IOException { - out.write(value & 0xff); - out.write((value >>> 8) & 0xff); - } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java deleted file mode 100644 index c075c230..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java +++ /dev/null @@ -1,267 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; - -/** - * Process-wide registry of glyph rasters, fed from the TS side so glyph - * identity survives to the BLE encoder (see notes/texture-cache-display-list- - * design.md). A frame's glyph list references entries here by (fontId, - * encoding); the texture-cache planner uses the raster three ways: - * - * - as the ink mask for the "would this draw land correctly" check against - * the composited frame, - * - to punch the ink pixels out of the baked delta rect it replaces, - * - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded - * via mode 12 and drawn via mode 14 with a top-color LUT. 1bpp (BDF) - * glyphs store ink at 15 so the LUT maps them to exactly the requested - * level; AA (TTF) glyphs store true coverage nibbles the LUT scales. - * - * Fonts are identified by a stable string key (the embedded font name), NOT a - * per-JS-context counter: worker threads register independently and must agree - * on ids. Glyph rasters are immutable once registered; re-registration of the - * same (fontId, encoding) is ignored. - * - * Cell model: a cached glyph image is bbxWidth x cellHeight (the font's line - * height), with the ink rows at inkTop — so every glyph of a line is drawn at - * the same y (the line top), which is what lets one mode-14 string carry a - * whole run. The horizontal bearing (bbxX) is applied phone-side when - * computing the draw x. Vertical padding is nearly free: it RLE-encodes to a - * couple of tokens. - */ -public final class GlyphAtlas { - private GlyphAtlas() {} - - public static final class Glyph { - /** Cached image width in pixels (the glyph's tight bbox width). */ - public final int width; - /** Cached image height in pixels (the font's line height). */ - public final int cellHeight; - /** First row of ink within the cell. */ - public final int inkTop; - /** Number of ink rows. */ - public final int inkHeight; - /** Horizontal bearing: draw x = pen x + bbxX. */ - public final int bbxX; - /** 1bpp ink rows (inkHeight entries), bit (rowBitWidth-1-col) = ink; null for AA glyphs. */ - final int[] rows; - final int rowBitWidth; - /** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */ - final byte[] coverage; - /** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */ - public final byte[] cachedBytes; - - Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX, - int[] rows, byte[] coverage) { - this.width = width; - this.cellHeight = cellHeight; - this.inkTop = inkTop; - this.inkHeight = inkHeight; - this.bbxX = bbxX; - this.rows = rows; - this.coverage = coverage; - this.rowBitWidth = ((width + 7) >> 3) << 3; - this.cachedBytes = encodeCachedImage(); - } - - /** - * The cell pixel's 4bpp source value at (col, row), row cell-relative. - * 1bpp glyphs are stored at 15 so the draw-time LUT (source*top/15) - * maps them to exactly the requested top color; AA glyphs carry their - * true coverage level. - */ - public int nibbleAt(int col, int row) { - if (col < 0 || col >= width) return 0; - int inkRow = row - inkTop; - if (inkRow < 0 || inkRow >= inkHeight) return 0; - if (coverage != null) return coverage[inkRow * width + col]; - return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0 ? 15 : 0; - } - - /** Whether the cell pixel at (col, row) has any ink (row is cell-relative). */ - public boolean inkAt(int col, int row) { - return nibbleAt(col, row) != 0; - } - - /** - * The firmware cached-image encoding: [width][height][RLE tokens] - * covering exactly width*cellHeight pixels (no row padding), each - * pixel's stored color being its nibbleAt value. - */ - private byte[] encodeCachedImage() { - int total = width * cellHeight; - // Worst case one token per pixel (1 byte each) + 2 header bytes. - byte[] out = new byte[2 + total]; - out[0] = (byte) width; - out[1] = (byte) cellHeight; - int o = 2; - int i = 0; - while (i < total) { - int color = nibbleAt(i % width, i / width); - int j = i + 1; - while (j < total && nibbleAt(j % width, j / width) == color) j++; - int run = j - i; - while (run > 0) { - int c = Math.min(run, 0xffff); - if (c <= 15) { - out[o++] = (byte) ((c << 4) | color); - } else if (c <= 255) { - // Worst case can exceed the sizing above only via escape - // forms, which occur for runs >= 16 (net win); safe. - out[o++] = (byte) color; - out[o++] = (byte) c; - } else { - out[o++] = (byte) color; - out[o++] = 0; - out[o++] = (byte) (c & 0xff); - out[o++] = (byte) (c >> 8); - } - run -= c; - } - i = j; - } - byte[] trimmed = new byte[o]; - System.arraycopy(out, 0, trimmed, 0, o); - return trimmed; - } - } - - private static final Object lock = new Object(); - private static final Map fontIds = new HashMap<>(); - private static final Map glyphs = new HashMap<>(); - private static int nextFontId = 1; - - /** Stable id for a font key; assigns one on first use. */ - public static int fontId(String key) { - synchronized (lock) { - Integer id = fontIds.get(key); - if (id == null) { - id = nextFontId++; - fontIds.put(key, id); - } - return id; - } - } - - public static Glyph get(int fontId, int encoding) { - synchronized (lock) { - return glyphs.get(key(fontId, encoding)); - } - } - - private static long key(int fontId, int encoding) { - return ((long) fontId << 32) | (encoding & 0xffffffffL); - } - - /** - * Register a batch of glyph rasters. Little-endian buffer, a sequence of - * font groups: - * [keyLen u8][key utf8][cellHeight u8][count u16] - * each followed by count glyph records: - * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] - * [inkHeight x rows u32] (bit (ceil(width/8)*8 - 1 - col) = ink) - * Records for an already-registered (font, encoding) are skipped (glyph - * rasters are immutable for a given font key). Malformed buffers throw: - * they indicate a phone-side marshalling bug, never device state. - */ - public static void register(ByteBuffer buffer) { - if (buffer == null) return; - ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN); - synchronized (lock) { - while (in.remaining() > 0) { - int keyLen = in.get() & 0xff; - byte[] keyBytes = new byte[keyLen]; - in.get(keyBytes); - String fontKey = new String(keyBytes, StandardCharsets.UTF_8); - int cellHeight = in.get() & 0xff; - int count = in.getShort() & 0xffff; - Integer idBoxed = fontIds.get(fontKey); - int id; - if (idBoxed == null) { - id = nextFontId++; - fontIds.put(fontKey, id); - } else { - id = idBoxed; - } - for (int g = 0; g < count; g++) { - int encoding = in.getInt(); - int bbxX = in.get(); - int inkTop = in.get() & 0xff; - int width = in.get() & 0xff; - int inkHeight = in.get() & 0xff; - int[] rows = new int[inkHeight]; - for (int r = 0; r < inkHeight; r++) { - rows[r] = in.getInt(); - } - long k = key(id, encoding); - if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0 - && inkTop + inkHeight <= cellHeight) { - glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, - rows, null)); - } - } - } - } - } - - /** - * Register a batch of antialiased (4bpp) glyph rasters, e.g. TTF renders. - * Same font-group framing as register(), but each glyph record carries - * packed coverage instead of 1bpp rows: - * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] - * [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel] - * Nibble values are the final 4bpp source levels (the draw-time LUT - * scales them by the top color). Already-registered (font, encoding) - * records are skipped, as in register(). - */ - public static void registerAa(ByteBuffer buffer) { - if (buffer == null) return; - ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN); - synchronized (lock) { - while (in.remaining() > 0) { - int keyLen = in.get() & 0xff; - byte[] keyBytes = new byte[keyLen]; - in.get(keyBytes); - String fontKey = new String(keyBytes, StandardCharsets.UTF_8); - int cellHeight = in.get() & 0xff; - int count = in.getShort() & 0xffff; - Integer idBoxed = fontIds.get(fontKey); - int id; - if (idBoxed == null) { - id = nextFontId++; - fontIds.put(fontKey, id); - } else { - id = idBoxed; - } - for (int g = 0; g < count; g++) { - int encoding = in.getInt(); - int bbxX = in.get(); - int inkTop = in.get() & 0xff; - int width = in.get() & 0xff; - int inkHeight = in.get() & 0xff; - int stride = (width + 1) >> 1; - byte[] coverage = new byte[width * inkHeight]; - for (int r = 0; r < inkHeight; r++) { - for (int b = 0; b < stride; b++) { - int packed = in.get() & 0xff; - int col = b * 2; - coverage[r * width + col] = (byte) (packed >> 4); - if (col + 1 < width) { - coverage[r * width + col + 1] = (byte) (packed & 0x0f); - } - } - } - long k = key(id, encoding); - if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0 - && inkTop + inkHeight <= cellHeight) { - glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, - null, coverage)); - } - } - } - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java deleted file mode 100644 index 206bae6b..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Process-wide registry of icon/image rasters for the texture-cache pipeline, - * the image counterpart of GlyphAtlas. Entries are content-addressed: the TS - * side keys each image by a hash of its dimensions and pixels, so the same - * icon registered from any thread or rendered at any time dedupes to one - * entry, and an icon whose content changes is simply a new entry. - * - * Unlike glyphs (1-bit ink recolored at draw time), images keep their exact - * 4bpp values: the cached bytes quantize the registered 8bpp pixels with the - * same GRAY_TO_NIBBLE table the composite is packed with, and the planner - * draws them via mode 13 with an identity LUT (top color 15) and the - * transparent bit — the draw writes exactly the nonzero-nibble pixels, which - * is what the eligibility check and hole punching are defined over. - */ -public final class ImageAtlas { - private ImageAtlas() {} - - public static final class Entry { - public final int width; - public final int height; - /** One 4bpp value per pixel, row-major. */ - final byte[] nibbles; - /** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */ - public final byte[] cachedBytes; - - Entry(int width, int height, byte[] nibbles) { - this.width = width; - this.height = height; - this.nibbles = nibbles; - this.cachedBytes = encodeCachedImage(); - } - - /** The 4bpp value the mode-13 draw would write at (col, row); 0 = skipped. */ - public int nibbleAt(int col, int row) { - return nibbles[row * width + col] & 0xff; - } - - private byte[] encodeCachedImage() { - int total = width * height; - byte[] out = new byte[2 + total]; - out[0] = (byte) width; - out[1] = (byte) height; - int o = 2; - int i = 0; - while (i < total) { - int color = nibbles[i] & 0xff; - int j = i + 1; - while (j < total && (nibbles[j] & 0xff) == color) j++; - int run = j - i; - while (run > 0) { - int c = Math.min(run, 0xffff); - if (c <= 15) { - out[o++] = (byte) ((c << 4) | color); - } else if (c <= 255) { - out[o++] = (byte) color; - out[o++] = (byte) c; - } else { - out[o++] = (byte) color; - out[o++] = 0; - out[o++] = (byte) (c & 0xff); - out[o++] = (byte) (c >> 8); - } - run -= c; - } - i = j; - } - byte[] trimmed = new byte[o]; - System.arraycopy(out, 0, trimmed, 0, o); - return trimmed; - } - } - - private static final Object lock = new Object(); - private static final Map ids = new HashMap<>(); - private static final List entries = new ArrayList<>(); - - /** - * Register an image (8bpp grayscale pixels, width*height bytes, row-major) - * under a content key, returning its id — or the existing id when the key - * is already registered (the pixels are then ignored: same key means same - * content). Ids are positive and stable for the process lifetime. - */ - public static int ensure(String key, int width, int height, ByteBuffer pixels8bpp) { - if (key == null || width <= 0 || width > 255 || height <= 0 || height > 255 - || pixels8bpp == null || pixels8bpp.remaining() < width * height) { - throw new IllegalArgumentException("bad image registration " + width + "x" + height); - } - synchronized (lock) { - Integer existing = ids.get(key); - if (existing != null) return existing; - byte[] nibbles = new byte[width * height]; - for (int i = 0; i < nibbles.length; i++) { - nibbles[i] = (byte) BmpUtil.nibbleForGray(pixels8bpp.get(i) & 0xff); - } - entries.add(new Entry(width, height, nibbles)); - int id = entries.size(); // ids start at 1 - ids.put(key, id); - return id; - } - } - - public static Entry get(int id) { - synchronized (lock) { - return id >= 1 && id <= entries.size() ? entries.get(id - 1) : null; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java b/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java deleted file mode 100644 index d728cc6b..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java +++ /dev/null @@ -1,229 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * Reader for the serialized LVGL font image in the G2's CJK flash partition. - * Faceclaw bundles that font because it is Source Han Sans SC Light under the - * SIL OFL. Proprietary fonts embedded in the main firmware never pass through - * this class and are instead extracted on the user's phone during CFW install. - */ -public final class LvglFontFile { - private static final String TAG = "LvglFontFile"; - private static final int MAPPED_BASE = 0x80100000; - private static final int FONT_CACHE_SIZE = 2; - - private static final Map fontCache = - new LinkedHashMap(FONT_CACHE_SIZE, 0.75f, true) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > FONT_CACHE_SIZE; - } - }; - - private LvglFontFile() {} - - /** - * Returns [lineHeight, baseline] or an empty array when the font is invalid. - * The baseline is measured down from the top of the line. - */ - public static byte[] getMetrics(String path) { - FontData font = load(path); - if (font == null) return new byte[0]; - return new byte[] { (byte) font.lineHeight, (byte) font.baseline }; - } - - /** - * Returns one packed glyph: - * u16 boxW, u16 boxH, i16 ofsX, i16 ofsY, then aligned 4bpp bitmap bytes. - * Returns an empty array when the code point is not mapped. - */ - public static byte[] getGlyph(String path, int codePoint) { - FontData font = load(path); - if (font == null) return new byte[0]; - Integer glyphId = font.codePointToGlyph.get(codePoint); - if (glyphId == null || glyphId <= 0) return new byte[0]; - try { - int descriptor = font.glyphDescriptors + glyphId * 16; - int bitmapIndex = font.u32(descriptor); - int boxWidth = font.u16(descriptor + 8); - int boxHeight = font.u16(descriptor + 10); - int offsetX = font.i16(descriptor + 12); - int offsetY = font.i16(descriptor + 14); - int stride = boxWidth / 2 + 1; - int bitmapLength = boxWidth > 0 && boxHeight > 0 ? stride * boxHeight : 0; - font.check(font.glyphBitmaps + bitmapIndex, bitmapLength); - - byte[] result = new byte[8 + bitmapLength]; - putU16(result, 0, boxWidth); - putU16(result, 2, boxHeight); - putU16(result, 4, offsetX); - putU16(result, 6, offsetY); - System.arraycopy(font.bytes, font.glyphBitmaps + bitmapIndex, result, 8, bitmapLength); - return result; - } catch (RuntimeException e) { - Log.w(TAG, "invalid glyph U+" + Integer.toHexString(codePoint) + " in " + path, e); - return new byte[0]; - } - } - - private static synchronized FontData load(String path) { - if (path == null || path.isEmpty()) return null; - FontData cached = fontCache.get(path); - if (cached != null) return cached; - try { - FontData font = FontData.read(path); - fontCache.put(path, font); - return font; - } catch (IOException | RuntimeException e) { - Log.w(TAG, "could not load LVGL font " + path, e); - return null; - } - } - - private static void putU16(byte[] destination, int offset, int value) { - destination[offset] = (byte) (value & 0xff); - destination[offset + 1] = (byte) ((value >>> 8) & 0xff); - } - - private static final class FontData { - final byte[] bytes; - final ByteBuffer view; - final int lineHeight; - final int baseline; - final int glyphBitmaps; - final int glyphDescriptors; - final Map codePointToGlyph; - - private FontData(byte[] bytes) { - this.bytes = bytes; - this.view = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); - if (bytes.length < 0x40 || bytes[0] != 'Z' || bytes[1] != 'Z' || bytes[2] != 'Z' || bytes[3] != 'Z') { - throw new IllegalArgumentException("missing ZZZZ font header"); - } - - lineHeight = u16(0x38); - int baseLine = u16(0x3a); - baseline = lineHeight - baseLine; - - int descriptor = pointerOffset(u32(0x34)); - glyphBitmaps = pointerOffset(u32(descriptor)); - glyphDescriptors = pointerOffset(u32(descriptor + 4)); - int cmaps = pointerOffset(u32(descriptor + 8)); - int packed = u16(descriptor + 18); - int cmapCount = packed & 0x1ff; - int bitsPerPixel = (packed >>> 9) & 0xf; - int bitmapFormat = (packed >>> 14) & 0x3; - if (lineHeight <= 0 || baseline < 0 || cmapCount <= 0 || - bitsPerPixel != 4 || bitmapFormat != 3) { - throw new IllegalArgumentException("unsupported LVGL font metadata"); - } - - codePointToGlyph = new HashMap<>(); - for (int index = 0; index < cmapCount; index++) { - readCmap(cmaps + index * 20); - } - } - - static FontData read(String path) throws IOException { - File file = new File(path); - if (!file.isFile() || !file.canRead() || file.length() > Integer.MAX_VALUE) { - throw new IOException("font is not a readable file"); - } - byte[] bytes = new byte[(int) file.length()]; - try (RandomAccessFile input = new RandomAccessFile(file, "r")) { - input.readFully(bytes); - } - return new FontData(bytes); - } - - void readCmap(int address) { - int rangeStart = u32(address); - int rangeLength = u16(address + 4); - int glyphIdStart = u16(address + 6); - int unicodeListPointer = u32(address + 8); - int glyphIdOffsetsPointer = u32(address + 12); - int listLength = u16(address + 16); - int type = u8(address + 18); - - if (type == 2) { - for (int relative = 0; relative < rangeLength; relative++) { - codePointToGlyph.put(rangeStart + relative, glyphIdStart + relative); - } - return; - } - - if (type == 0) { - int offsets = pointerOffset(glyphIdOffsetsPointer); - for (int relative = 0; relative < rangeLength; relative++) { - int offset = u8(offsets + relative); - if (relative != 0 && offset == 0) continue; - codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset); - } - return; - } - - if (type == 3 || type == 1) { - int unicodeList = pointerOffset(unicodeListPointer); - int offsets = glyphIdOffsetsPointer == 0 ? -1 : pointerOffset(glyphIdOffsetsPointer); - for (int index = 0; index < listLength; index++) { - int relative = u16(unicodeList + index * 2); - int offset = index; - if (offsets >= 0) { - offset = type == 1 ? u16(offsets + index * 2) : u8(offsets + index); - if (index != 0 && offset == 0) continue; - } - codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset); - } - return; - } - - throw new IllegalArgumentException("unknown cmap type " + type); - } - - int u8(int offset) { - check(offset, 1); - return bytes[offset] & 0xff; - } - - int u16(int offset) { - check(offset, 2); - return view.getShort(offset) & 0xffff; - } - - int i16(int offset) { - check(offset, 2); - return view.getShort(offset); - } - - int u32(int offset) { - check(offset, 4); - return view.getInt(offset); - } - - int pointerOffset(int pointer) { - long unsignedPointer = pointer & 0xffffffffL; - long offset = unsignedPointer - (MAPPED_BASE & 0xffffffffL); - if (offset < 0 || offset > Integer.MAX_VALUE) { - throw new IllegalArgumentException("font pointer is outside the mapped partition"); - } - check((int) offset, 1); - return (int) offset; - } - - void check(int offset, int length) { - if (offset < 0 || length < 0 || (long) offset + length > bytes.length) { - throw new IllegalArgumentException("font range is out of bounds"); - } - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java index 9ffdcd1b..313abece 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java @@ -2,15 +2,12 @@ package com.faceclaw.app; import android.content.Context; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; -import java.util.zip.CRC32; -import java.util.zip.Deflater; /** * Saves screenshots of the composited screen as 4-bit grayscale PNGs (the @@ -25,7 +22,7 @@ public final class ScreenshotUtil { if (gray == null || width <= 0 || height <= 0 || gray.length < width * height) { throw new IllegalArgumentException("invalid screenshot buffer"); } - byte[] png = encode4BitGrayPng(gray, width, height); + byte[] png = SharedScreenshots.encode4BitGrayPng(gray, width, height); File file = new File(ensureScreenshotsDir(context), "screen-" + timestamp() + ".png"); try (FileOutputStream out = new FileOutputStream(file)) { out.write(png); @@ -45,80 +42,4 @@ public final class ScreenshotUtil { return new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(new Date()); } - /** - * Encode an 8bpp grayscale buffer as a bit-depth-4 grayscale PNG, keeping - * the top nibble of each pixel (the same quantization the wire format uses). - */ - static byte[] encode4BitGrayPng(byte[] gray, int width, int height) { - // Raw scanlines: one filter byte (0 = None) then two pixels per byte, - // leftmost pixel in the high nibble. - int rowBytes = (width + 1) / 2; - byte[] raw = new byte[height * (1 + rowBytes)]; - int p = 0; - for (int y = 0; y < height; y++) { - raw[p++] = 0; - int row = y * width; - for (int x = 0; x < width; x += 2) { - int hi = (gray[row + x] & 0xff) >>> 4; - int lo = (x + 1 < width) ? (gray[row + x + 1] & 0xff) >>> 4 : 0; - raw[p++] = (byte) ((hi << 4) | lo); - } - } - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - out.write(0x89); - out.write('P'); - out.write('N'); - out.write('G'); - out.write(0x0d); - out.write(0x0a); - out.write(0x1a); - out.write(0x0a); - - ByteArrayOutputStream ihdr = new ByteArrayOutputStream(); - writeIntBE(ihdr, width); - writeIntBE(ihdr, height); - ihdr.write(4); // bit depth - ihdr.write(0); // color type: grayscale - ihdr.write(0); // compression - ihdr.write(0); // filter - ihdr.write(0); // interlace - writeChunk(out, "IHDR", ihdr.toByteArray()); - writeChunk(out, "IDAT", zlibDeflate(raw)); - writeChunk(out, "IEND", new byte[0]); - return out.toByteArray(); - } - - static byte[] zlibDeflate(byte[] data) { - Deflater deflater = new Deflater(); - deflater.setInput(data); - deflater.finish(); - ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 8)); - byte[] buffer = new byte[8192]; - while (!deflater.finished()) { - out.write(buffer, 0, deflater.deflate(buffer)); - } - deflater.end(); - return out.toByteArray(); - } - - private static void writeChunk(ByteArrayOutputStream out, String type, byte[] data) { - writeIntBE(out, data.length); - CRC32 crc = new CRC32(); - for (int i = 0; i < 4; i++) { - int b = type.charAt(i); - out.write(b); - crc.update(b); - } - out.write(data, 0, data.length); - crc.update(data); - writeIntBE(out, (int) crc.getValue()); - } - - private static void writeIntBE(ByteArrayOutputStream out, int value) { - out.write((value >>> 24) & 0xff); - out.write((value >>> 16) & 0xff); - out.write((value >>> 8) & 0xff); - out.write(value & 0xff); - } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java deleted file mode 100644 index 1a86e8cb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java +++ /dev/null @@ -1,509 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Retained-surface compositor: the phone-side model of what is on the glasses - * screen. - * - * Each surface is an 8bpp grayscale buffer retained at its last-submitted - * contents, positioned on the screen with a z-order and a transparency mode. - * Sources on the TS side (today the dashboard; later the shell and per-app - * worker threads) submit updates to their surface, and the compositor - * recombines the retained surfaces into a full-screen frame that feeds the - * existing dedupe/compress/transmit pipeline. Because every surface is - * retained, a full screen frame can be regenerated at any time (frame drops, - * reconnects, previews, screenshots) without asking sources to repaint. - * - * Surface format contract (mirrored by the TS side): - * - Pixels are 8bpp grayscale, row-major, one byte per pixel. The wire - * pipeline quantizes to 4bpp downstream, so the low nibble is not visible. - * - TRANSPARENCY_COLOR_KEY surfaces treat pixel value 0 as fully transparent - * and value 1 as black. Quantization collapses 1 into the same 4bpp level - * as 0, so reserving 0 costs no visible shade; painters of color-key - * surfaces must clamp intentional black to 1. - * - An update may cover any rect of its surface; the rest is retained. The - * update buffer holds exactly rectWidth*rectHeight bytes, row-major. - * - Surfaces composite in ascending z-order onto a black (0) background. - * - Surface geometry changes take effect when the next frame composites; - * they do not trigger a recomposite by themselves. - * - * Thread safety: all methods are safe to call from any thread. Apply and - * composite happen atomically under an internal lock, and each composite - * carries a monotonic sequence number so callers can detect when a composite - * was superseded by a concurrent one before being acted on. - */ -public final class SurfaceCompositor { - public static final int TRANSPARENCY_OPAQUE = 0; - public static final int TRANSPARENCY_COLOR_KEY = 1; - - /** - * One deferred draw (a text glyph or an icon image) within a frame, in - * screen coordinates. The draw's pixels are already baked into the - * composited gray buffer (the TS side bakes before submitting); this - * record preserves the draw's identity so the texture-cache planner can - * replay it as an on-glasses cached draw instead of image bytes. - * - * Glyphs: x/y are the pen position and line top; the raster (and its - * bearing/cell placement) comes from GlyphAtlas under (fontId, encoding). - * Images: x/y are the blit's top-left; the raster comes from ImageAtlas - * under imageId. - */ - public static final class ScreenDraw { - public static final int KIND_GLYPH = 0; - public static final int KIND_IMAGE = 1; - /** A firmware-builtin-font text run (CFW mode 15). */ - public static final int KIND_FWTEXT = 2; - - public final int kind; - /** Glyph draws only. */ - public final int fontId; - /** Glyph draws only. */ - public final int encoding; - /** Image draws only. */ - public final int imageId; - public final int x; - public final int y; - /** Glyph and fw-text draws: 8-bit brightness. */ - public final int value; - /** Fw-text runs only: member codepoints, in text order. */ - public final int[] fwCps; - /** Fw-text runs only: member pen offsets relative to x. */ - public final int[] fwDx; - /** Fw-text runs only: whether each member has visible pixels. */ - public final boolean[] fwInk; - - ScreenDraw(int kind, int fontId, int encoding, int imageId, int x, int y, int value, - int[] fwCps, int[] fwDx, boolean[] fwInk) { - this.kind = kind; - this.fontId = fontId; - this.encoding = encoding; - this.imageId = imageId; - this.x = x; - this.y = y; - this.value = value; - this.fwCps = fwCps; - this.fwDx = fwDx; - this.fwInk = fwInk; - } - - static ScreenDraw glyph(int fontId, int encoding, int penX, int lineY, int value) { - return new ScreenDraw(KIND_GLYPH, fontId, encoding, 0, penX, lineY, value, null, null, null); - } - - static ScreenDraw image(int imageId, int x, int y) { - return new ScreenDraw(KIND_IMAGE, 0, 0, imageId, x, y, 0, null, null, null); - } - - static ScreenDraw fwText(int x, int y, int value, int[] cps, int[] dx, boolean[] ink) { - return new ScreenDraw(KIND_FWTEXT, 0, 0, 0, x, y, value, cps, dx, ink); - } - } - - private static final ScreenDraw[] NO_DRAWS = new ScreenDraw[0]; - - /** One composited full-screen frame plus the metadata the pipeline needs. */ - public static final class Composite { - /** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */ - public final byte[] gray; - public final int width; - public final int height; - /** - * Stable identifier of screen content, combining every surface's - * geometry and content fingerprint; equal fingerprints mean equal - * composited pixels. - */ - public final String fingerprint; - /** Monotonic: a Composite with a higher seq contains strictly newer state. */ - public final long seq; - /** - * Screen-space deferred draws of every visible surface, in composite - * order (surface z ascending, then each surface's draw order). Their - * pixels are already baked into gray. - */ - public final ScreenDraw[] draws; - - Composite(byte[] gray, int width, int height, String fingerprint, long seq, ScreenDraw[] draws) { - this.gray = gray; - this.width = width; - this.height = height; - this.fingerprint = fingerprint; - this.seq = seq; - this.draws = draws == null ? NO_DRAWS : draws; - } - } - - private static final class Surface { - final String id; - int x; - int y; - int width; - int height; - int zOrder; - int transparency; - boolean visible = true; - byte[] pixels; - String fingerprint = ""; - /** Surface-local deferred draws of the retained content (already baked into pixels). */ - ScreenDraw[] draws = NO_DRAWS; - - Surface(String id) { - this.id = id; - } - } - - private final Object lock = new Object(); - private int screenWidth; - private int screenHeight; - private boolean blanked; - /** See setUnderlayDim: surfaces with zOrder below this are dimmed by underlayDim/256. */ - private int underlayDimBelowZOrder = Integer.MIN_VALUE; - private int underlayDim = 256; - private final Map surfaces = new HashMap<>(); - private long nextCompositeSeq = 1; - - /** Set the output frame size. Must be called before any surface work. */ - public void configureScreen(int width, int height) { - if (width <= 0 || height <= 0) { - throw new IllegalArgumentException("bad screen size " + width + "x" + height); - } - synchronized (lock) { - this.screenWidth = width; - this.screenHeight = height; - } - } - - /** - * Create a surface or update an existing one's geometry. A resize discards - * the surface's retained pixels (reset to 0). - */ - public void configureSurface(String id, int x, int y, int width, int height, int zOrder, int transparency) { - if (id == null || id.isEmpty()) { - throw new IllegalArgumentException("surface id must be non-empty"); - } - if (width <= 0 || height <= 0) { - throw new IllegalArgumentException("bad surface size " + width + "x" + height + " for " + id); - } - if (transparency != TRANSPARENCY_OPAQUE && transparency != TRANSPARENCY_COLOR_KEY) { - throw new IllegalArgumentException("bad transparency mode " + transparency + " for " + id); - } - synchronized (lock) { - requireScreenConfiguredLocked(); - Surface surface = surfaces.get(id); - if (surface == null) { - surface = new Surface(id); - surfaces.put(id, surface); - } - if (surface.pixels == null || surface.width != width || surface.height != height) { - surface.pixels = new byte[width * height]; - surface.fingerprint = ""; - surface.draws = NO_DRAWS; - } - surface.x = x; - surface.y = y; - surface.width = width; - surface.height = height; - surface.zOrder = zOrder; - surface.transparency = transparency; - } - } - - public void removeSurface(String id) { - synchronized (lock) { - surfaces.remove(id); - } - } - - /** - * Dim every surface whose zOrder is below belowZOrder to factor256/256 of - * its brightness (256 = no dimming): how a shell overlay that dims what it - * covers (the TS Layer.dimUnderneath) reaches the window surfaces beneath - * the shell surface, which the shell's own layer stack cannot paint. - * Visible pixels stay at least 1 (the color-key black); glyph and - * firmware-text draws keep their cached-draw form with a dimmed value; - * image draws leave the composite's draw list (their pixels, already - * baked into the surface, dim as raster). Takes effect when the next - * frame composites. - */ - public void setUnderlayDim(int belowZOrder, int factor256) { - synchronized (lock) { - underlayDimBelowZOrder = belowZOrder; - underlayDim = Math.max(0, Math.min(256, factor256)); - } - } - - /** The dim factor (256 = none) that applies to a surface. */ - private int dimForLocked(Surface surface) { - return surface.zOrder < underlayDimBelowZOrder ? underlayDim : 256; - } - - private static int dimValue(int value, int dim) { - return Math.max(1, (value * dim + 128) >> 8); - } - - /** - * Hidden surfaces keep their retained pixels and accept updates, but are - * excluded from the composite and its fingerprint (used for background - * windows). Takes effect when the next frame composites. - */ - public void setSurfaceVisible(String id, boolean visible) { - synchronized (lock) { - Surface surface = surfaces.get(id); - if (surface == null) { - throw new IllegalArgumentException("unknown surface " + id); - } - surface.visible = visible; - } - } - - /** - * While blanked (screen off), composites are all-zero regardless of - * surface content; retained state is untouched, so unblanking restores - * the screen without asking sources to repaint. - */ - public void setBlanked(boolean blanked) { - synchronized (lock) { - this.blanked = blanked; - } - } - - /** - * Apply an update to one surface and composite the whole screen, as one - * atomic step. The update covers the rect (rectX, rectY, rectWidth, - * rectHeight) in surface-local coordinates; pixels must hold exactly - * rectWidth*rectHeight bytes. contentFingerprint identifies the surface's - * full content after this update. - */ - public Composite applyAndComposite( - String surfaceId, - ByteBuffer pixels, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint - ) { - return applyAndComposite(surfaceId, pixels, rectX, rectY, rectWidth, rectHeight, - contentFingerprint, null); - } - - /** - * As above, with the frame's deferred draws: a little-endian buffer of - * tagged records - * [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph) - * [1][imageId u32][x s16][y s16] (image) - * in surface-local coordinates and draw order. The list describes the - * surface's FULL retained content and replaces the previous list, so it is - * only meaningful for full-surface updates (which is what every submitter - * sends); pass null to clear. Draw pixels must already be baked into pixels. - */ - public Composite applyAndComposite( - String surfaceId, - ByteBuffer pixels, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - ByteBuffer glyphs - ) { - ScreenDraw[] parsed = parseDraws(glyphs); - synchronized (lock) { - requireScreenConfiguredLocked(); - Surface surface = surfaces.get(surfaceId); - if (surface == null) { - throw new IllegalArgumentException("unknown surface " + surfaceId); - } - if (rectX < 0 || rectY < 0 || rectWidth <= 0 || rectHeight <= 0 - || rectX + rectWidth > surface.width || rectY + rectHeight > surface.height) { - throw new IllegalArgumentException("update rect " + rectWidth + "x" + rectHeight - + "+" + rectX + "+" + rectY + " outside surface " + surfaceId - + " (" + surface.width + "x" + surface.height + ")"); - } - int expectedBytes = rectWidth * rectHeight; - if (pixels == null || pixels.remaining() != expectedBytes) { - throw new IllegalArgumentException("update buffer for " + surfaceId + " has " - + (pixels == null ? 0 : pixels.remaining()) + " bytes, expected " + expectedBytes); - } - for (int row = 0; row < rectHeight; row++) { - int dstOffset = (rectY + row) * surface.width + rectX; - pixels.get(surface.pixels, dstOffset, rectWidth); - } - surface.fingerprint = contentFingerprint == null ? "" : contentFingerprint; - surface.draws = parsed; - return compositeLocked(); - } - } - - private static ScreenDraw[] parseDraws(ByteBuffer draws) { - if (draws == null || draws.remaining() < 1) { - return NO_DRAWS; - } - ByteBuffer in = draws.order(java.nio.ByteOrder.LITTLE_ENDIAN); - List out = new ArrayList<>(); - while (in.remaining() >= 1) { - int kind = in.get() & 0xff; - if (kind == ScreenDraw.KIND_GLYPH && in.remaining() >= 11) { - int fontId = in.getShort() & 0xffff; - int encoding = in.getInt(); - int penX = in.getShort(); - int lineY = in.getShort(); - int value = in.get() & 0xff; - out.add(ScreenDraw.glyph(fontId, encoding, penX, lineY, value)); - } else if (kind == ScreenDraw.KIND_IMAGE && in.remaining() >= 8) { - int imageId = in.getInt(); - int x = in.getShort(); - int y = in.getShort(); - out.add(ScreenDraw.image(imageId, x, y)); - } else if (kind == ScreenDraw.KIND_FWTEXT && in.remaining() >= 6) { - int x = in.getShort(); - int y = in.getShort(); - int value = in.get() & 0xff; - int count = in.get() & 0xff; - if (in.remaining() < count * 7) { - break; // malformed tail: keep what parsed cleanly - } - int[] cps = new int[count]; - int[] dx = new int[count]; - boolean[] ink = new boolean[count]; - for (int i = 0; i < count; i++) { - cps[i] = in.getInt(); - dx[i] = in.getShort(); - ink[i] = in.get() != 0; - } - out.add(ScreenDraw.fwText(x, y, value, cps, dx, ink)); - } else { - break; // malformed tail: keep what parsed cleanly - } - } - return out.toArray(new ScreenDraw[0]); - } - - /** Composite the current retained state without applying an update. */ - public Composite composite() { - synchronized (lock) { - requireScreenConfiguredLocked(); - return compositeLocked(); - } - } - - /** - * Current composited pixels for the phone-side preview / screenshot, or - * null before the screen is configured. Does not consume a sequence - * number (it is never stored as the desired frame). - */ - public Composite previewComposite() { - synchronized (lock) { - if (screenWidth <= 0 || screenHeight <= 0) { - return null; - } - byte[] gray = buildGrayLocked(); - return new Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS); - } - } - - private byte[] buildGrayLocked() { - byte[] gray = new byte[screenWidth * screenHeight]; - if (blanked) { - return gray; - } - List ordered = new ArrayList<>(surfaces.values()); - ordered.sort(Comparator - .comparingInt((Surface s) -> s.zOrder) - .thenComparing(s -> s.id)); - for (Surface surface : ordered) { - if (surface.visible) { - blendLocked(gray, surface); - } - } - return gray; - } - - private Composite compositeLocked() { - byte[] gray = new byte[screenWidth * screenHeight]; - if (blanked) { - return new Composite(gray, screenWidth, screenHeight, - "blanked:" + screenWidth + "x" + screenHeight, nextCompositeSeq++, NO_DRAWS); - } - List ordered = new ArrayList<>(surfaces.values()); - ordered.sort(Comparator - .comparingInt((Surface s) -> s.zOrder) - .thenComparing(s -> s.id)); - StringBuilder fingerprint = new StringBuilder(); - fingerprint.append(screenWidth).append('x').append(screenHeight); - List draws = new ArrayList<>(); - for (Surface surface : ordered) { - if (!surface.visible) continue; - blendLocked(gray, surface); - int dim = dimForLocked(surface); - for (ScreenDraw draw : surface.draws) { - if (dim < 256 && draw.kind == ScreenDraw.KIND_IMAGE) continue; - int value = dim < 256 ? dimValue(draw.value, dim) : draw.value; - draws.add(new ScreenDraw(draw.kind, draw.fontId, draw.encoding, draw.imageId, - draw.x + surface.x, draw.y + surface.y, value, - draw.fwCps, draw.fwDx, draw.fwInk)); - } - fingerprint.append('|').append(surface.id) - .append('@').append(surface.x).append(',').append(surface.y) - .append('+').append(surface.width).append('x').append(surface.height) - .append('#').append(surface.zOrder) - .append(':').append(surface.transparency) - .append(':').append(surface.fingerprint); - if (dim < 256) { - fingerprint.append(":dim").append(dim); - } - } - return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++, - draws.toArray(new ScreenDraw[0])); - } - - private void blendLocked(byte[] gray, Surface surface) { - // Clip the surface rect to the screen. - int srcX = Math.max(0, -surface.x); - int srcY = Math.max(0, -surface.y); - int dstX = Math.max(0, surface.x); - int dstY = Math.max(0, surface.y); - int copyWidth = Math.min(surface.width - srcX, screenWidth - dstX); - int copyHeight = Math.min(surface.height - srcY, screenHeight - dstY); - if (copyWidth <= 0 || copyHeight <= 0) { - return; - } - int dim = dimForLocked(surface); - for (int row = 0; row < copyHeight; row++) { - int srcOffset = (srcY + row) * surface.width + srcX; - int dstOffset = (dstY + row) * screenWidth + dstX; - if (dim < 256) { - // Dimmed: opaque or color-keyed, 0 stays 0 (black / transparent) - // and everything visible scales, never below 1. - for (int col = 0; col < copyWidth; col++) { - int value = surface.pixels[srcOffset + col] & 0xff; - if (value != 0) { - gray[dstOffset + col] = (byte) dimValue(value, dim); - } else if (surface.transparency == TRANSPARENCY_OPAQUE) { - gray[dstOffset + col] = 0; - } - } - } else if (surface.transparency == TRANSPARENCY_OPAQUE) { - System.arraycopy(surface.pixels, srcOffset, gray, dstOffset, copyWidth); - } else { - for (int col = 0; col < copyWidth; col++) { - byte value = surface.pixels[srcOffset + col]; - if (value != 0) { - gray[dstOffset + col] = value; - } - } - } - } - } - - private void requireScreenConfiguredLocked() { - if (screenWidth <= 0 || screenHeight <= 0) { - throw new IllegalStateException("configureScreen must be called first"); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java deleted file mode 100644 index b9336cee..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java +++ /dev/null @@ -1,555 +0,0 @@ -package com.faceclaw.app; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import android.util.Log; - -public final class BleImageOptimizer { - private BleImageOptimizer() {} - private static final String TAG = "BleImageOptimizer"; - - /** - * Split an image into fragments (packets), taking advantage of the fact that each packet can declare - * a logical size longer than its byte payload to get free trailing zero-padding. - * - * Each fragment's logical size stays within maxFragmentSize, while trailing zeros inside that span can - * be omitted from the payload. - */ - public static List planImageFragments(byte[] bmp, int maxFragmentSize) { - return planImageFragments(bmp, maxFragmentSize, false); - } - - public static List planImageFragments( - byte[] bmp, - int maxFragmentSize, - boolean reserveFinalByte - ) { - if (bmp == null || bmp.length == 0) { - return Collections.singletonList(new BleProtocol.ImageFragment(0, new byte[0], 0)); - } - if (maxFragmentSize <= 0) { - throw new IllegalArgumentException("maxFragmentSize must be positive"); - } - - final int bulkLength = reserveFinalByte ? bmp.length - 1 : bmp.length; - final int fragmentCount = ((bulkLength + maxFragmentSize - 1) / maxFragmentSize) - + (bulkLength < bmp.length ? 1 : 0); - final List fragments = new ArrayList<>(fragmentCount); - for (int index = 0; index * maxFragmentSize < bulkLength; index++) { - int start = index * maxFragmentSize; - int end = Math.min(start + maxFragmentSize, bulkLength); - fragments.add(buildFragment(index, bmp, start, end)); - } - if (bulkLength < bmp.length) { - fragments.add(new BleProtocol.ImageFragment( - fragments.size(), - Arrays.copyOfRange(bmp, bulkLength, bmp.length), - bmp.length - bulkLength - )); - } - Log.i(TAG, "planImageFragments: fragments=" + fragments.size() + ", bmp.length=" + bmp.length + ", maxFragmentSize=" + maxFragmentSize); - return fragments; - } - - private static BleProtocol.ImageFragment buildFragment(int index, byte[] bmp, int start, int end) { - int dataEnd = end; - while (dataEnd > start && bmp[dataEnd - 1] == 0) { - dataEnd -= 1; - } - if (dataEnd == start) { - return new BleProtocol.ImageFragment(index, new byte[0], end - start); - } - return new BleProtocol.ImageFragment( - index, - Arrays.copyOfRange(bmp, start, dataEnd), - end - start - ); - } - - public static final class TileImagePlan { - final int tileIndex; - final BleProtocol.ImageTileOptions tile; - // Headerless packed 4bpp frame (top-down, stride ceil(width/2)), kept for - // the displayed-tile dedup cache. Frames are immutable by convention: - // producers always allocate a fresh buffer, so no defensive copy here. - public final byte[] packed; - public final int width; - public final int height; - final byte[] payload; // bytes actually streamed: mode-6 rle(4bpp) - final int sessionId; - List fragments = Collections.emptyList(); - - TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId) { - this.tileIndex = tileIndex; - this.tile = tile; - this.packed = packed == null ? new byte[0] : packed; - this.width = width; - this.height = height; - this.payload = maybeCompress(this.packed, width, height); - this.sessionId = sessionId; - } - - /** - * Plan with an explicit wire payload (e.g. a mode-3 incremental update). - * packed must still be the FULL new frame: it feeds the displayed-frame - * dedup cache, which after an applied incremental equals the full frame. - */ - TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId, byte[] payloadOverride) { - this.tileIndex = tileIndex; - this.tile = tile; - this.packed = packed == null ? new byte[0] : packed; - this.width = width; - this.height = height; - this.payload = payloadOverride; - this.sessionId = sessionId; - } - } - - /** - * Build a mode-3 bounding-box incremental payload, or null when a full - * update should be sent instead (frames not comparable, or the changed - * region spans the whole screen). Wire format: - * [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][rle(headerless 4bpp region)] - * The region is top-down rows of the NEW frame, stride width/2 bytes. The - * box is aligned so left/width are multiples of 4 pixels and top/height - * multiples of 2 rows, letting each coordinate fit one byte and avoiding - * 4bpp byte-boundary corner cases. Identical frames yield null too; the - * caller's dedup check should normally catch that first. - * - * frameId is a little-endian uint16 the CFW uses to detect reordered, - * skipped or duplicated deltas (its diagnostic overlay); the caller advances - * it by 1 per emitted delta so consecutive deltas are consecutive. - */ - /** - * A mode-3 incremental payload plus diagnostics about how tightly the - * bounding box fit the actual change. {@code changedBytes} is how many - * packed bytes actually differ; {@code boxBytes} is how many the box sends. - * A large box with few changed bytes / multiple clusters means distant - * small edits were merged into one oversized box (which can spill past the - * fragment size into an extra serialized BLE message). - */ - public static final class IncrementalPlan { - public final byte[] payload; - public final int changedBytes; - public final int boxBytes; - public final int clusterCount; - - IncrementalPlan(byte[] payload, int changedBytes, int boxBytes, int clusterCount) { - this.payload = payload; - this.changedBytes = changedBytes; - this.boxBytes = boxBytes; - this.clusterCount = clusterCount; - } - } - - public static IncrementalPlan buildIncrementalImagePayload(byte[] previous, byte[] next, int width, int height, int frameId) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - - // Full diff scan in packed-byte coordinates (1 byte = 2 pixels): the - // changed bounding box plus diagnostics (total changed bytes, and how - // many disjoint horizontal column-clusters the change splits into). - int minByteX = stride; - int maxByteX = -1; - int minY = height; - int maxY = -1; - int changedBytes = 0; - boolean[] columnChanged = new boolean[stride]; - for (int y = 0; y < height; y++) { - int rowOffset = y * stride; - for (int x = 0; x < stride; x++) { - if (previous[rowOffset + x] != next[rowOffset + x]) { - changedBytes++; - columnChanged[x] = true; - if (x < minByteX) minByteX = x; - if (x > maxByteX) maxByteX = x; - if (y < minY) minY = y; - maxY = y; - } - } - } - if (maxY < 0) { - return null; // identical - } - int clusterCount = 0; - boolean inCluster = false; - for (int x = minByteX; x <= maxByteX; x++) { - if (columnChanged[x]) { - if (!inCluster) clusterCount++; - inCluster = true; - } else { - inCluster = false; - } - } - - // Pixel-space box, aligned: left/width to multiples of 4, top/height to - // multiples of 2, clamped to the frame. - int left = (minByteX * 2) & ~3; - int rightExclusive = Math.min(width, (((maxByteX + 1) * 2) + 3) & ~3); - int top = minY & ~1; - int bottomExclusive = Math.min(height, (maxY + 2) & ~1); - int boxWidth = rightExclusive - left; - int boxHeight = bottomExclusive - top; - if (boxWidth >= width && boxHeight >= height) { - return null; // whole screen changed; full update is strictly better - } - - byte[] out = encodeMode3Rect(next, stride, left, top, boxWidth, boxHeight, frameId); - return new IncrementalPlan(out, changedBytes, (boxWidth >> 1) * boxHeight, clusterCount); - } - - /** - * The aligned changed bounding box between two same-size packed frames, or - * null when they are identical: pixel-space {left, top, width, height} with - * left/width multiples of 4 and top/height multiples of 2 (mode-3 header - * quantization). Exposed so the texture-cache planner can reuse the exact - * rect geometry the single-bbox path would send. - */ - public static int[] computeChangedBox(byte[] previous, byte[] next, int width, int height) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - int minByteX = stride; - int maxByteX = -1; - int minY = height; - int maxY = -1; - for (int y = 0; y < height; y++) { - int rowOffset = y * stride; - for (int x = 0; x < stride; x++) { - if (previous[rowOffset + x] != next[rowOffset + x]) { - if (x < minByteX) minByteX = x; - if (x > maxByteX) maxByteX = x; - if (y < minY) minY = y; - maxY = y; - } - } - } - if (maxY < 0) { - return null; - } - int left = (minByteX * 2) & ~3; - int rightExclusive = Math.min(width, (((maxByteX + 1) * 2) + 3) & ~3); - int top = minY & ~1; - int bottomExclusive = Math.min(height, (maxY + 2) & ~1); - return new int[]{left, top, rightExclusive - left, bottomExclusive - top}; - } - - /** - * The multi-rect split of the changed region (see computeSplitRects), or null - * when it is one region / too fragmented / the frames aren't comparable. - * Exposed for the texture-cache planner. - */ - public static List computeChangedRects(byte[] previous, byte[] next, int width, int height, int maxRects) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - List rects = computeSplitRects(previous, next, stride, width, height, maxRects); - return (rects == null || rects.size() < 2) ? null : rects; - } - - /** - * Encode one CFW mode-3 rectangle delta: - * [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][rle(box pixels)] - * left/width must be multiples of 4 (=> left>>1, width>>1 are whole bytes), top/ - * height multiples of 2. The box pixels are top-down rows of `next` (4bpp packed, - * width>>1 bytes/row), run-length encoded before deflate. Shared by the single-bbox - * path, each rect of a mode-8 multi-rect batch, and the texture-cache planner. - */ - static byte[] encodeMode3Rect(byte[] next, int stride, int left, int top, - int boxWidth, int boxHeight, int fid) { - int regionStride = boxWidth >> 1; - byte[] region = new byte[regionStride * boxHeight]; - for (int y = 0; y < boxHeight; y++) { - System.arraycopy(next, (top + y) * stride + (left >> 1), region, y * regionStride, regionStride); - } - byte[] compressed = rleEncode(region); - byte[] out = new byte[7 + compressed.length]; - out[0] = 3; - out[1] = (byte) (left / 4); - out[2] = (byte) (top / 2); - out[3] = (byte) (boxWidth / 4); - out[4] = (byte) (boxHeight / 2); - out[5] = (byte) (fid & 0xff); // fid_lo - out[6] = (byte) ((fid >> 8) & 0xff); // fid_hi - System.arraycopy(compressed, 0, out, 7, compressed.length); - return out; - } - - /** A full repaint in independently decodable commands below the uint16 record limit. */ - static List encodeFullFrameBands(byte[] packed, int width, int height, int firstFid) { - if (width <= 0 || width > 640 || (width & 3) != 0 || height <= 0 || height > 480 - || (height & 1) != 0 || packed.length != width * height / 2) { - throw new IllegalArgumentException("unsupported custom framebuffer dimensions"); - } - List commands = new ArrayList<>(); - int fid = firstFid; - for (int top = 0; top < height; top += 64) { - commands.add(encodeMode3Rect(packed, width / 2, 0, top, width, - Math.min(64, height - top), fid)); - fid = fid >= 0xfffe ? 1 : fid + 1; - } - return commands; - } - - /** - * A CFW mode-8 multi-segment payload (`[8][count]` then per-rect - * `[len16][mode-3 submsg]`) carrying several tight rectangle deltas instead of - * one bounding box, plus diagnostics. Each rect is applied to the firmware - * shadow with the panel push deferred, then presented once. Used when the change - * splits into regions whose bounding box wastes bytes (distant edits, or a sparse - * box); the caller falls back to the single-rect path when this returns null or - * isn't smaller. - */ - public static final class MultiRectPlan { - public final byte[] payload; - public final int rectCount; - public final int coveredBytes; // sum of rect region bytes, pre-compression - public final int nextFid; // fid to resume from (rectCount consumed) - - MultiRectPlan(byte[] payload, int rectCount, int coveredBytes, int nextFid) { - this.payload = payload; - this.rectCount = rectCount; - this.coveredBytes = coveredBytes; - this.nextFid = nextFid; - } - } - - // Break-even tuning for the rect splitter. Splitting adds ~15 fixed bytes per - // rect (seglen + mode-3 header + fid), so only split across gaps big enough to pay for that. - // Gaps are in the changed-mask's native units: whole 4bpp bytes (2px) for - // columns, rows for the vertical bands. Kept above the 4px/2px box alignment so - // aligned rects never overlap. - private static final int MULTI_RECT_V_GAP_ROWS = 4; - private static final int MULTI_RECT_H_GAP_BYTES = 6; - - /** - * Build a mode-8 multi-rect payload for the change between two same-size 4bpp - * frames, assigning consecutive frame ids starting at fidStart (each rect needs a - * distinct fid — the CFW skips duplicate fids). Returns null when the change is a - * single region (use the single-rect path), when it fragments past maxRects, or - * when the frames aren't comparable. - */ - public static MultiRectPlan buildMultiRectImagePayload(byte[] previous, byte[] next, int width, int height, int fidStart, int maxRects) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - - List rects = computeSplitRects(previous, next, stride, width, height, maxRects); - if (rects == null || rects.size() < 2) { - return null; // single region (or too fragmented): caller uses the bbox path - } - - List subs = new ArrayList<>(rects.size()); - int coveredBytes = 0; - int fid = fidStart; - int total = 2; // [8][count] - for (int[] r : rects) { - byte[] sub = encodeMode3Rect(next, stride, r[0], r[1], r[2], r[3], fid); - subs.add(sub); - coveredBytes += (r[2] >> 1) * r[3]; - total += 2 + sub.length; // len16 + submsg - fid = (fid >= 0xfffe) ? 1 : fid + 1; - } - - byte[] out = new byte[total]; - out[0] = 8; - out[1] = (byte) rects.size(); - int pos = 2; - for (byte[] sub : subs) { - out[pos] = (byte) (sub.length & 0xff); - out[pos + 1] = (byte) ((sub.length >> 8) & 0xff); - pos += 2; - System.arraycopy(sub, 0, out, pos, sub.length); - pos += sub.length; - } - return new MultiRectPlan(out, rects.size(), coveredBytes, fid); - } - - /** - * Split the changed region between two packed 4bpp frames into a small set of - * aligned rectangles that together cover every changed pixel: maximal vertical - * bands of changed rows (merging gaps < V_GAP), each split into horizontal column - * clusters (merging gaps < H_GAP), with each cluster's rows tightened to where it - * actually changes. Rects are pixel-space {left,top,width,height}, left/width - * aligned to 4px and top/height to 2px. Returns null if there's no change or the - * split exceeds maxRects (caller falls back to a single bounding box). - */ - private static List computeSplitRects(byte[] previous, byte[] next, int stride, int width, int height, int maxRects) { - boolean[] rowChanged = new boolean[height]; - boolean anyChange = false; - for (int y = 0; y < height; y++) { - int off = y * stride; - for (int x = 0; x < stride; x++) { - if (previous[off + x] != next[off + x]) { - rowChanged[y] = true; - anyChange = true; - break; - } - } - } - if (!anyChange) { - return null; - } - - List rects = new ArrayList<>(); - boolean[] col = new boolean[stride]; - int y = 0; - while (y < height) { - if (!rowChanged[y]) { y++; continue; } - // Extend a vertical band across changed rows, hopping unchanged gaps < V_GAP. - int bandTop = y; - int bandBot = y; - int cur = y + 1; - while (cur < height) { - if (rowChanged[cur]) { bandBot = cur; cur++; continue; } - int g = cur; - while (g < height && !rowChanged[g]) g++; - if (g < height && (g - cur) < MULTI_RECT_V_GAP_ROWS) { cur = g; } else break; - } - - // Columns changed anywhere in the band. - Arrays.fill(col, false); - for (int yy = bandTop; yy <= bandBot; yy++) { - if (!rowChanged[yy]) continue; - int off = yy * stride; - for (int x = 0; x < stride; x++) { - if (previous[off + x] != next[off + x]) col[x] = true; - } - } - - // Split the band into horizontal clusters, hopping column gaps < H_GAP. - int x = 0; - while (x < stride) { - if (!col[x]) { x++; continue; } - int cL = x; - int cR = x; - int cx = x + 1; - while (cx < stride) { - if (col[cx]) { cR = cx; cx++; continue; } - int g = cx; - while (g < stride && !col[g]) g++; - if (g < stride && (g - cx) < MULTI_RECT_H_GAP_BYTES) { cx = g; } else break; - } - - // Tighten this cluster's rows to those that change within [cL,cR]. - int tTop = height; - int tBot = -1; - for (int yy = bandTop; yy <= bandBot; yy++) { - if (!rowChanged[yy]) continue; - int off = yy * stride; - for (int xx = cL; xx <= cR; xx++) { - if (previous[off + xx] != next[off + xx]) { - if (yy < tTop) tTop = yy; - tBot = yy; - break; - } - } - } - if (tBot >= 0) { - int left = (cL * 2) & ~3; - int rightExclusive = Math.min(width, (((cR + 1) * 2) + 3) & ~3); - int top = tTop & ~1; - int bottomExclusive = Math.min(height, (tBot + 2) & ~1); - rects.add(new int[]{left, top, rightExclusive - left, bottomExclusive - top}); - if (rects.size() > maxRects) { - return null; // too fragmented: a single bounding box is simpler - } - } - x = cR + 1; - } - y = bandBot + 1; - } - return rects; - } - - /** - * Run-length encode headerless 4bpp pixels for CFW load_image_z mode 6. - * Wire format: [6][rle(4bpp pixels)]. Always use mode 6: the logical - * image can be larger than its EvenHub carrier, so a raw BMP fallback would - * carry dimensions the legacy container loader cannot accept. - */ - static byte[] maybeCompress(byte[] packed, int width, int height) { - if (packed == null || packed.length == 0) { - return packed; - } - byte[] z = rleEncode(packed); - byte[] out = new byte[z.length + 1]; - out[0] = 6; - System.arraycopy(z, 0, out, 1, z.length); - return out; - } - - /** - * Run-length encode packed 4bpp pixels for CFW modes 3 and 6, whose payload is - * RLE tokens; deflate belongs to the transport. Runs are over the pixel NIBBLES of - * {@code pix} in wire order (high nibble = left pixel), including the pad nibble - * that ends each row at odd widths — i.e. {@code pix} read as 2*length nibbles. - * One token is: - *
-     *   [cnt4|color4]                 cnt 1..15
-     *   [0|color4][cnt8]              cnt 1..255
-     *   [0|color4][0][cntLo][cntHi]   cnt 1..65535, little-endian
-     * 
- * with the low nibble always the color; runs longer than 65535 split across - * tokens. Worst case (every nibble its own run) is exactly one byte per nibble, - * which is what the buffer is sized for. - */ - static byte[] rleEncode(byte[] pix) { - int n = pix.length * 2; - byte[] out = new byte[n]; - int o = 0; - int i = 0; - while (i < n) { - int v = nibbleAt(pix, i); - int j = i + 1; - while (j < n && nibbleAt(pix, j) == v) j++; - int run = j - i; - while (run > 0) { - int c = Math.min(run, 0xffff); - if (c <= 15) { - out[o++] = (byte) ((c << 4) | v); - } else if (c <= 255) { - out[o++] = (byte) v; // high nibble 0 = escape to an 8-bit count - out[o++] = (byte) c; - } else { - out[o++] = (byte) v; - out[o++] = 0; // 8-bit count 0 = escape to a 16-bit count - out[o++] = (byte) (c & 0xff); - out[o++] = (byte) (c >> 8); - } - run -= c; - } - i = j; - } - return Arrays.copyOf(out, o); - } - - private static int nibbleAt(byte[] pix, int i) { - int b = pix[i >> 1] & 0xff; - return (i & 1) != 0 ? (b & 0x0f) : (b >> 4); - } - - public static final class ImageUpdateStats { - final int paintMs; - final int tileCount; - final int frameId; - long firstWriteStartedAtMs; - - ImageUpdateStats(int paintMs, int tileCount, int frameId) { - this.paintMs = Math.max(0, paintMs); - this.tileCount = tileCount; - this.frameId = frameId; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleMagicPool.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleMagicPool.java deleted file mode 100644 index bd6afd79..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleMagicPool.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.faceclaw.app; -import java.util.ArrayDeque; -import java.util.HashMap; -import java.util.Map; -import android.os.SystemClock; - -/** - * Protobuf messages contain a 1-byte identifier that is used to identify - * which message is referred to in ACK messages. We keep track of which - * identifiers are in use for inflight messages, with some metadata about - * what the message was. We allocate identifiers with an LRU policy so that - * if we release a message's identifier because it timed out, we can still - * detect late ACKs if it hasn't been reused yet. - */ -public final class BleMagicPool { - static final int MIN_MAGIC = 100; - static final int MAX_MAGIC = 255; - - private final ArrayDeque available = new ArrayDeque<>(); - private final boolean[] allocated = new boolean[256]; - private final Map releaseRecords = new HashMap<>(); - - BleMagicPool() { - for (int magic = MIN_MAGIC; magic <= MAX_MAGIC; magic++) { - available.addLast(magic); - } - } - - synchronized int allocate() { - Integer magic = available.pollFirst(); - if (magic == null) { - throw new IllegalStateException("no BLE magic values available"); - } - allocated[magic] = true; - return magic; - } - - synchronized void release(int sid, int magic, String label, String reason) { - if (magic < MIN_MAGIC || magic > MAX_MAGIC) { - return; - } - releaseRecords.put(key(sid, magic), new ReleaseRecord(label, reason, SystemClock.elapsedRealtime())); - if (!allocated[magic]) { - return; - } - allocated[magic] = false; - available.addLast(magic); - } - - synchronized ReleaseRecord getReleaseRecord(int sid, int magic) { - return releaseRecords.get(key(sid, magic)); - } - - private static String key(int sid, int magic) { - return sid + ":" + magic; - } - - public static final class ReleaseRecord { - final String label; - final String reason; - final long releasedAtMs; - - ReleaseRecord(String label, String reason, long releasedAtMs) { - this.label = label == null ? "" : label; - this.reason = reason == null ? "" : reason; - this.releasedAtMs = releasedAtMs; - } - } -} 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 deleted file mode 100644 index 6031f24d..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java +++ /dev/null @@ -1,1240 +0,0 @@ -package com.faceclaw.app; - -import java.io.ByteArrayOutputStream; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.UUID; - -/** - * BleProtocol: Functions for generating and parsing messages when speaking to - * the Even Realities G2. Does _not_ include code for actually sending anything; - * for that, see FaceclawBleCommunicator. - * - * Most message formats are protobufs. For schemas, check g2-kit-unofficial. - */ -public class BleProtocol { - public static final String WRITE_CHAR_UUID = "00002760-08c2-11e1-9073-0e8ac72e5401"; - public static final String NOTIFY_CHAR_UUID = "00002760-08c2-11e1-9073-0e8ac72e5402"; - // Firmware-data service (svc ...e1001): the OTA transfer channel, distinct - // from the EvenHub write/notify chars above (which the flasher reuses only - // for the keepalive heartbeat). - public static final String OTA_DATA_WRITE_UUID = "00002760-08c2-11e1-9073-0e8ac72e0001"; - public static final String OTA_DATA_NOTIFY_UUID = "00002760-08c2-11e1-9073-0e8ac72e0002"; - public static final String RENDER_NOTIFY_UUID = "00002760-08c2-11e1-9073-0e8ac72e6402"; - public static final String R1_PHONE_NOTIFY_CHAR_UUID = "bae80011-4f05-4503-8e65-3af1f7329d1f"; - public static final String R1_NOTIFY_CHAR_UUID = "bae80013-4f05-4503-8e65-3af1f7329d1f"; - - public static final int PRELUDE_ACK_SID = 0x01; - public static final int PRELUDE_ACK_MAGIC = 156; - /** - * DEVICE_SETTINGS security-auth channel. Firmware 2.2.9 enforces completing - * this exchange shortly after a GATT connection opens (a ~30 s deadline - * closes unauthenticated links) and gates query responses on it; the - * message itself exists back to the 2.2.4 era, so sending it is safe on all - * stock versions. See ../notes/ble-connections-2.2.9.md. - */ - public static final int SID_SECURITY_AUTH = 0x80; - public static final int FLAG_SECURITY_AUTH = 0x00; - private static final int SECURITY_AUTH_CMD = 4; - public static final int SID_APP_LAUNCH = 0x01; - public static final int SID_EVENHUB = 0xe0; - public static final int SID_UI_SETTING = 0x09; - public static final int SID_STATE_CHANGE = 0x0d; - public static final int SID_ONBOARDING = 0x10; - /** G2SettingPackage.commandId for device-initiated pushes (vs. 2 = app read). */ - public static final int SETTINGS_CMD_DEVICE_SEND_TO_APP = 3; - // Private Faceclaw/CFW wake-takeover protocol. Both payloads are unknown - // protobuf fields to stock implementations, so they are safely ignored. - public static final int FACECLAW_WAKE_CONTROL_FIELD = 101; - public static final int FACECLAW_WAKE_EVENT_FIELD = 102; - /** - * CFW mic_control: host writes an ['M','C',ver,op,...] record as settings - * field 103; each temple - * reports a 21-byte ['M','C',ver,...] status as field 104, both appended - * to settings read responses and as standalone commandId=3 pushes. - */ - public static final int FACECLAW_MIC_CONTROL_FIELD = 103; - public static final int FACECLAW_MIC_STATUS_FIELD = 104; - /** - * CFW als_sensor: ambient-light reports on settings-channel field 105, 24-byte ['A','L',ver,reason,...] records from - * the master temple (decoded in app/native/ambient-light.ts). - */ - public static final int FACECLAW_ALS_REPORT_FIELD = 105; - public static final int FACECLAW_WAKE_OP_ACQUIRE = 1; - public static final int FACECLAW_WAKE_OP_RELEASE = 2; - public static final int FACECLAW_WAKE_OP_CLAIM = 3; - public static final int FACECLAW_WAKE_OP_READY = 4; - public static final int FACECLAW_FB_OP_ACQUIRE = 5; - public static final int FACECLAW_FB_OP_RELEASE = 6; - public static final int FACECLAW_WEAR_OP_QUERY = 7; - public static final int CFW_IMAGE_MODE_CLEANUP = 11; - private static final int FACECLAW_WAKE_EVENT = 1; - // Field-102 deferred wake from the IMU head-up (Faceclaw firmware revision - // 3+); same nonce handshake as the double-tap wake event 1. - public static final int FACECLAW_WAKE_EVENT_HEAD_UP = 5; - // Field-102 idle-gesture events (Faceclaw firmware revision 2+): gestures the - // stock display thread drops while no app is on screen, forwarded while the - // wake lease is held. Their last two bytes are the raw touch source and 0. - public static final int FACECLAW_GESTURE_EVENT_TAP = 2; - public static final int FACECLAW_GESTURE_EVENT_LONG_PRESS = 3; - public static final int FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE = 4; - // The display thread's raw touch sources, as the firmware reports them. - private static final int FACECLAW_RAW_SOURCE_LEFT_TEMPLE = 0; - private static final int FACECLAW_RAW_SOURCE_RIGHT_TEMPLE = 1; - private static final int FACECLAW_RAW_SOURCE_RING = 4; - private static final int FACECLAW_WAKE_PROTOCOL_VERSION = 1; - // UI_FOREGROUND_EVEN_AI_ID: the stock "Even AI" assistant app. It is a - // FOREGROUND app, whereas EvenHub (and therefore faceclaw) is the - // BACKGROUND app -- which is why an Even AI launch draws over us. - // Payload is g2.even_ai.EvenAIDataPackage (see - // g2-kit-unofficial/ble/gen/even_ai_pb.ts). - public static final int SID_EVEN_AI = 0x07; - public static final int SID_NAVIGATION = 0x08; - public static final int NAV_CMD_COMPASS_CHANGED = 15; - public static final int NAV_CMD_COMPASS_CALIBRATION_STARTED = 16; - public static final int NAV_CMD_COMPASS_CALIBRATION_COMPLETE = 17; - // eEvenAICommandId - public static final int EVEN_AI_CMD_CTRL = 1; - // eEvenAIStatus, carried in EvenAIDataPackage.ctrl.status (field 3, sub 1). - // WAKE_UP is the on-glasses "Hey Even" wakeword firing; ENTER is a manual - // entry (touch/double-tap), so the two are distinguishable. - public static final int EVEN_AI_STATUS_WAKE_UP = 1; - public static final int EVEN_AI_STATUS_ENTER = 2; - public static final int EVEN_AI_STATUS_EXIT = 3; - public static final int FLAG_REQUEST = 0x20; - public static final int FLAG_NOTIFY = 0x01; - public static final int FLAG_NOTIFY_ALT = 0x06; - public static final int CMD_AUDIO_CONTROL = 15; - // EvenHub_Cmd_List.APP_REQUEST_OPEN_IMU_PACKET; the accelerometer stream is - // gated behind this control message (ImuCtrl, field 22 of the main ctx). - public static final int CMD_OPEN_IMU = 19; - - public static final int EVENT_CLICK = 0; - public static final int EVENT_SCROLL_TOP = 1; - public static final int EVENT_SCROLL_BOTTOM = 2; - public static final int EVENT_DOUBLE_CLICK = 3; - public static final int EVENT_FOREGROUND_ENTER = 4; - public static final int EVENT_FOREGROUND_EXIT = 5; - public static final int EVENT_ABNORMAL_EXIT = 6; - public static final int EVENT_SYSTEM_EXIT = 7; - public static final int EVENT_IMU_DATA_REPORT = 8; - public static final int EVENT_RING_LONG_PRESS = 9; - public static final int EVENT_RING_LONG_PRESS_RELEASE = 10; - // CFW extension: G2 2.2.9 tap-then-hold gesture forwarded to the phone. - public static final int EVENT_SHORT_THEN_LONG_PRESS = 11; - // CFW extension (firmware revision 3+): the IMU head-up, forwarded as a - // sys-event while an EvenHub page is on screen (soft sleep). - public static final int EVENT_HEAD_UP = 12; - - public static final int EVENT_SOURCE_GLASSES_R = 1; - public static final int EVENT_SOURCE_RING = 2; - public static final int EVENT_SOURCE_GLASSES_L = 3; - - public static final UUID CCCD_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); - - // App-launch message. Protobuf contents come from a capture (done in g2-kit-unofficial) - // so the actual meaning of most fields isn't known. The standard protobuf - // wire format still lets us identify field numbers, wire types, and lengths. - public static final byte[] PRELUDE_F5872_PAYLOAD = buildPreludeF5872Payload(); - public static final byte[] PRELUDE_F5872 = - framePb(PRELUDE_F5872_PAYLOAD, SID_APP_LAUNCH, FLAG_REQUEST, 0x92).getFirst(); - - private static byte[] buildPreludeF5872Payload() { - byte[] field4 = encodeMessageField(3, - encodeMessageField(2, - encodeMessageField(2, concat(CollectionUtils.listOf( - encodeVarintField(1, 0), - encodeVarintField(2, 0) - ))) - ) - ); - return concat(CollectionUtils.listOf( - encodeVarintField(1, 2), // known app-launch type/Cmd - encodeVarintField(2, PRELUDE_ACK_MAGIC), - encodeMessageField(4, field4) - )); - } - - public static List framePb(byte[] pb, int sid, int flag, int seq) { - int chunkSize = 232; - byte[] crc = crcBytesLe(pb); - int totalWithCrc = pb.length + 2; - int totalFrags = Math.max(1, (int) Math.ceil(totalWithCrc / (double) chunkSize)); - List frames = new ArrayList<>(totalFrags); - byte[] payload = Arrays.copyOf(pb, totalWithCrc); - payload[pb.length] = crc[0]; - payload[pb.length + 1] = crc[1]; - for (int i = 0; i < totalFrags; i++) { - int offset = i * chunkSize; - byte[] chunk = Arrays.copyOfRange(payload, offset, Math.min(offset + chunkSize, payload.length)); - byte[] frame = new byte[8 + chunk.length]; - frame[0] = (byte) 0xaa; - frame[1] = 0x21; - frame[2] = (byte) (seq & 0xff); - frame[3] = (byte) (chunk.length & 0xff); - frame[4] = (byte) (totalFrags & 0xff); - frame[5] = (byte) ((i + 1) & 0xff); - frame[6] = (byte) (sid & 0xff); - frame[7] = (byte) (flag & 0xff); - System.arraycopy(chunk, 0, frame, 8, chunk.length); - frames.add(frame); - } - return frames; - } - - - /** Retain the text container for forwarded inputs, without an image container. */ - public static byte[] buildCreateInputPage(int magic) { - List innerParts = new ArrayList<>(); - innerParts.add(encodeVarintField(1, 1)); - innerParts.add(encodeMessageField(3, encodeTextObject("dashboard", 1, 0, 0, 576, 288, " ", true))); - innerParts.add(encodeVarintField(5, 10000)); - byte[] inner = concat(innerParts); - return wrapEvenHub(0, magic, 3, inner); - } - - public static byte[] buildDashboardTextUpgrade(int magic) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, 1)); - inner.add(encodeStringField(2, "dashboard")); - inner.add(encodeVarintField(3, 0)); - inner.add(encodeVarintField(4, 1)); - inner.add(encodeStringField(5, " ")); - return wrapEvenHub(5, magic, 9, concat(inner)); - } - - public static byte[] buildImageRawData(ImageTileOptions tile, int sessionId, int totalSize, ImageFragment fragment, int magic) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, tile.containerId)); - inner.add(encodeStringField(2, tile.name)); - //inner.add(encodeVarintField(3, sessionId)); - inner.add(encodeVarintField(3, tile.containerId)); - inner.add(encodeVarintField(4, totalSize)); - // CompressMode stays 0: the CFW's zlib path is detected from the buffer's - // zlib header at BMP-load time, and frag_write must copy our deflated bytes - // VERBATIM (CompressMode!=0 would route them through the 1bpp expander). - inner.add(encodeVarintField(5, 0)); //compression - inner.add(encodeVarintField(6, fragment.index)); - inner.add(encodeVarintField(7, fragment.size)); - inner.add(encodeBytesField(8, fragment.data)); - return wrapEvenHub(3, magic, 5, concat(inner)); - } - - public static byte[] buildHeartbeat(int magic) { - return wrapEvenHub(12, magic, 14, encodeVarintField(1, 0)); - } - - /** - * Cmd=0 CreateStartUpPage carrying a text container (the message) plus a - * list container (the selectable rows) in one page. Uses stock-firmware-safe - * geometry (each container within the stock ~280x130 container-size cap; the - * CFW is what lifts that to 576x288), so this works before flashing. The - * selected row comes back as an async list event — see parseListSelection. - */ - public static byte[] buildCreatePromptPage( - int magic, - String textName, - int textContainerId, - String warningText, - String listName, - int listContainerId, - String[] items - ) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, 2)); // ContainerTotalNum: 1 text + 1 list - inner.add(encodeMessageField(2, encodeListObject(listName, listContainerId, 0, 150, 280, 120, items, true))); - inner.add(encodeMessageField(3, encodeTextObject(textName, textContainerId, 0, 0, 280, 130, warningText, false))); - inner.add(encodeVarintField(5, 10000)); // widgetId - return wrapEvenHub(0, magic, 3, concat(inner)); - } - - public static byte[] encodeListObject( - String name, - int containerId, - int x, - int y, - int width, - int height, - String[] items, - boolean captureEvents - ) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, x)); - parts.add(encodeVarintField(2, y)); - parts.add(encodeVarintField(3, width)); - parts.add(encodeVarintField(4, height)); - parts.add(encodeVarintField(9, containerId)); - parts.add(encodeStringField(10, name)); - parts.add(encodeMessageField(11, encodeListItemContainer(items))); - if (captureEvents) { - parts.add(encodeVarintField(12, 1)); - } - return concat(parts); - } - - private static byte[] encodeListItemContainer(String[] items) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, items.length)); // ItemCount - parts.add(encodeVarintField(3, 1)); // IsItemSelectBorderEn - for (String item : items) { - parts.add(encodeStringField(4, item)); // repeated ItemName - } - return concat(parts); - } - - /** - * Decode a list-selection async event (sid=0xe0, flag=0x01/0x06). Returns - * null if the frame is not a list event. `itemIndex` is 0-based into the - * ItemName array the list was built with; `eventType` is EVENT_CLICK for a - * confirmed selection (scroll/highlight changes arrive with other types). - */ - public static ListSelection parseListSelection(ParsedFrame frame) { - byte[] pb = stripTrailingCrc(frame.pb); - byte[] deviceEvent = readFieldBytes(pb, 13); - if (deviceEvent == null) { - return null; - } - byte[] listEvent = readFieldBytes(deviceEvent, 1); - if (listEvent == null) { - return null; - } - return new ListSelection( - readStringFieldValue(listEvent, 2), - readStringFieldValue(listEvent, 3), - readVarintFieldValue(listEvent, 4, -1), - readVarintFieldValue(listEvent, 5, EVENT_CLICK) - ); - } - - public static byte[] buildAudioControl(int magic, boolean enable) { - return wrapEvenHub(CMD_AUDIO_CONTROL, magic, 18, encodeVarintField(1, enable ? 1 : 0)); - } - - public static byte[] buildShutdown(int magic, int exitMode) { - return wrapEvenHub(9, magic, 11, encodeVarintField(1, exitMode)); - } - - /** - * IMU control (ImuCtrl = field 22 of the main ctx). IMU_CtrlCmd carries - * IMUReportEn (field 1) and reportFrq (field 2). reportFrq is only - * meaningful when enabling; on disable we send just the enable flag. - */ - public static byte[] buildImuControl(int magic, boolean enable, int reportFrq) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, enable ? 1 : 0)); - if (enable && reportFrq > 0) { - inner.add(encodeVarintField(2, reportFrq)); - } - return wrapEvenHub(CMD_OPEN_IMU, magic, 22, concat(inner)); - } - - /** - * Set the lens brightness: G2SettingPackage{commandId=1 (DeviceReceiveInfo), - * deviceReceiveInfoFromApp(3){deviceReceiveBrightness(1){autoAdjust(1), - * brightnessLevel(2)}}} on sid 0x09. brightnessLevel is 0-100 (nonlinear; - * 0 is dim-but-visible, not off). When autoAdjust is set the ambient-light - * sensor drives brightness, so the level field is omitted; otherwise the - * level is always encoded (explicit zero included) so brightnessLevel=0 - * reaches the wire. - */ - public static byte[] buildSetBrightness(int magic, boolean autoAdjust, int brightnessLevel) { - List brightness = new ArrayList<>(); - brightness.add(encodeVarintField(1, autoAdjust ? 1 : 0)); - if (!autoAdjust) { - brightness.add(encodeVarintField(2, brightnessLevel)); - } - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, magic), - encodeMessageField(3, encodeMessageField(1, concat(brightness))) - )); - } - - /** Enable the firmware's wear detector (DeviceReceiveInfoFromAPP field 5). */ - public static byte[] buildSetWearDetection(int magic, boolean enabled) { - byte[] wear = encodeVarintField(1, enabled ? 1 : 0); - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, magic), - encodeMessageField(3, encodeMessageField(5, wear)) - )); - } - - /** - * The application authentication request: DEVICE_SETTINGS/AUTHENTICATION(4) - * on sid 0x80, flag 0x00. Carries no key material — it arms the firmware's - * "security auth" flag; the firmware sends the success notification once - * the flag is set AND the BLE link is encrypted (SMP pairing/bond - * restoration, handled by the OS below GATT, possibly after a pairing - * prompt). Protobuf: f1=4 (command), f2=magic, f3={f1=1, f2=4}. - */ - public static byte[] buildAuthenticationRequest(int magic) { - byte[] auth = concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, SECURITY_AUTH_CMD) - )); - return concat(CollectionUtils.listOf( - encodeVarintField(1, SECURITY_AUTH_CMD), - encodeVarintField(2, magic), - encodeMessageField(3, auth) - )); - } - - /** - * True when `pb` (trailing CRC still attached) is the success notification - * for the authentication request sent with `magic`: command 4, the same - * magic, and an EMPTY field-3 result message (`1a 00` — the omitted result - * code defaults to zero). A GATT write completing is NOT success; only this - * notification is. - */ - public static boolean isAuthenticationSuccess(byte[] pb, int magic) { - if (pb == null) { - return false; - } - byte[] root = stripTrailingCrc(pb); - if (readVarintFieldValue(root, 1, -1) != SECURITY_AUTH_CMD) { - return false; - } - if (readVarintFieldValue(root, 2, -1) != magic) { - return false; - } - byte[] result = readFieldBytes(root, 3); - return result != null && result.length == 0; - } - - public static byte[] buildSettingsQuery(int magic) { - byte[] request = encodeVarintField(1, 1); - return concat(CollectionUtils.listOf( - encodeVarintField(1, 2), - encodeVarintField(2, magic), - encodeMessageField(4, request) - )); - } - - /** - * Build the CFW-only wake lease control package. Magic zero deliberately - * makes this fire-and-forget: the firmware consumes field 101 before its - * stock nanopb decoder discards the unknown field. - */ - public static byte[] buildFaceclawWakeControl(int operation, int nonce) { - byte[] control = new byte[] { - 'F', - 'C', - (byte) FACECLAW_WAKE_PROTOCOL_VERSION, - (byte) operation, - (byte) (nonce & 0xff), - (byte) ((nonce >>> 8) & 0xff) - }; - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, 0), - encodeBytesField(FACECLAW_WAKE_CONTROL_FIELD, control) - )); - } - - /** - * Wrap an already-encoded ['M','C',ver,op,...] mic-control record as a - * settings write carrying CFW field 103. Magic zero makes it - * fire-and-forget like the wake lease: the firmware consumes the field - * before its stock nanopb decoder discards the unknown tag, and the - * "ack" is the field-104 status notify that follows. - */ - public static byte[] buildFaceclawMicControl(byte[] record) { - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, 0), - encodeBytesField(FACECLAW_MIC_CONTROL_FIELD, record) - )); - } - - /** - * Extract the CFW mic status record (field 104) from a settings frame, or - * null when absent. The 21-byte body layout is decoded on the TS side; - * here we only validate the 'M','C',version prelude. - */ - public static byte[] parseFaceclawMicStatus(byte[] pb) { - if (pb == null) { - return null; - } - byte[] body = readFieldBytes(stripTrailingCrc(pb), FACECLAW_MIC_STATUS_FIELD); - if (body == null - || body.length < 3 - || body[0] != 'M' - || body[1] != 'C') { - return null; - } - return body; - } - - /** - * Return the raw CFW ambient-light report from a sid-0x09 frame, or null - * when this is an ordinary settings frame. Only the 'A','L',version prelude - * is validated here; the fields are decoded on the TS side. - */ - public static byte[] parseFaceclawAlsReport(byte[] pb) { - if (pb == null) { - return null; - } - byte[] body = readFieldBytes(stripTrailingCrc(pb), FACECLAW_ALS_REPORT_FIELD); - if (body == null - || body.length < 24 - || body[0] != 'A' - || body[1] != 'L' - || (body[2] & 0xff) != 1) { - return null; - } - return body; - } - - /** Field 106: RB/version 1/flags/percentage, from the CFW ring-battery cache. */ - public static RingBatterySnapshot parseRingBattery(byte[] pb) { - if (pb == null) return null; - byte[] body = readFieldBytes(stripTrailingCrc(pb), 106); - if (body == null || body.length != 5 || body[0] != 'R' || body[1] != 'B' - || body[2] != 1) return null; - int flags = body[3] & 0xff; - int level = body[4] & 0xff; - if ((flags & ~7) != 0) return null; - if ((flags & 2) != 0) { - if ((flags & 1) == 0 || level > 100) return null; - return new RingBatterySnapshot(level, (flags & 4) != 0 ? 1 : 0); - } - if (level != 255 || (flags & 4) != 0) return null; - return new RingBatterySnapshot(-1, -1); - } - - public static final class RingBatterySnapshot { - final int battery; - final int charging; - RingBatterySnapshot(int battery, int charging) { - this.battery = battery; - this.charging = charging; - } - } - - /** - * Return the uint16 wake nonce from a CFW field-102 deferred-wake - * notification (double tap or head-up), or -1 when this is an ordinary - * settings frame (idle-gesture events included). - */ - public static int parseFaceclawWakeEvent(byte[] pb) { - byte[] event = faceclawWakeEventBytes(pb); - return event == null ? -1 : (event[4] & 0xff) | ((event[5] & 0xff) << 8); - } - - /** - * Which gesture a CFW deferred-wake notification reports: 1 (double tap) - * or FACECLAW_WAKE_EVENT_HEAD_UP; -1 when the frame is not a wake event. - */ - public static int parseFaceclawWakeEventCode(byte[] pb) { - byte[] event = faceclawWakeEventBytes(pb); - return event == null ? -1 : (event[3] & 0xff); - } - - private static byte[] faceclawWakeEventBytes(byte[] pb) { - if (pb == null) { - return null; - } - byte[] event = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD); - if (event == null - || event.length != 6 - || event[0] != 'F' - || event[1] != 'C' - || (event[2] & 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION) { - return null; - } - int code = event[3] & 0xff; - return code == FACECLAW_WAKE_EVENT || code == FACECLAW_WAKE_EVENT_HEAD_UP ? event : null; - } - - /** An idle gesture from a CFW field-102 notification, in EvenHub sys-event terms. */ - public static final class FaceclawGestureEvent { - public final int eventType; - public final int eventSource; - - FaceclawGestureEvent(int eventType, int eventSource) { - this.eventType = eventType; - this.eventSource = eventSource; - } - } - - /** - * Decode a CFW field-102 idle-gesture event (tap, long press, release - * while no app is on screen) into the EvenHub sys-event type and source - * the same gesture would carry from a live page, or null when the frame - * is not one (ordinary settings frames and the double-tap wake event - * included). - */ - public static FaceclawGestureEvent parseFaceclawGestureEvent(byte[] pb) { - if (pb == null) { - return null; - } - byte[] event = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD); - if (event == null - || event.length != 6 - || event[0] != 'F' - || event[1] != 'C' - || (event[2] & 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION) { - return null; - } - int eventType; - switch (event[3] & 0xff) { - case FACECLAW_GESTURE_EVENT_TAP: - eventType = EVENT_CLICK; - break; - case FACECLAW_GESTURE_EVENT_LONG_PRESS: - eventType = EVENT_RING_LONG_PRESS; - break; - case FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE: - eventType = EVENT_RING_LONG_PRESS_RELEASE; - break; - default: - return null; - } - int eventSource; - switch (event[4] & 0xff) { - case FACECLAW_RAW_SOURCE_LEFT_TEMPLE: - eventSource = EVENT_SOURCE_GLASSES_L; - break; - case FACECLAW_RAW_SOURCE_RIGHT_TEMPLE: - eventSource = EVENT_SOURCE_GLASSES_R; - break; - case FACECLAW_RAW_SOURCE_RING: - eventSource = EVENT_SOURCE_RING; - break; - default: - eventSource = 0; - break; - } - return new FaceclawGestureEvent(eventType, eventSource); - } - - /** - * Decode OnboardingDataPackage EVENT/GLS_WEAR_STATUS. Unlike most async - * G2 traffic, stock firmware sends this event with flag 0x00, so callers - * must recognize it by sid and protobuf shape rather than notify flag. - * Returns 0/1, or -1 when the frame is not a wear event. - */ - public static int parseWearState(ParsedFrame frame) { - if (frame == null || !frame.ok || frame.sid != SID_ONBOARDING) { - return -1; - } - byte[] root = stripTrailingCrc(frame.pb); - if (readVarintFieldValue(root, 1, -1) != 3) { - return -1; - } - byte[] event = readFieldBytes(root, 5); - if (event == null || readVarintFieldValue(event, 1, -1) != 1) { - return -1; - } - int wearStatus = readVarintFieldValue(event, 2, -1); - return wearStatus == 0 || wearStatus == 1 ? wearStatus : -1; - } - - public static BatterySnapshot parseSettingsBattery(byte[] pb) { - byte[] root = stripTrailingCrc(pb); - byte[] request = readFieldBytes(root, 4); - if (request == null) { - return null; - } - int battery = readVarintFieldValue(request, 12, -1); - int charging = readVarintFieldValue(request, 13, -1); - int silentMode = readVarintFieldValue(request, 14, -1); - if (battery < 0) { - return null; - } - return new BatterySnapshot(battery, charging, silentMode); - } - - /** - * Silent mode from an unsolicited settings push. The wearer toggles silent - * mode by long-pressing both touchpads at once; the firmware then sends - * G2SettingPackage{commandId=3 (DeviceSendToAPP), deviceSendInfoToApp(5){ - * silentModeSwitch(2)}} on sid 0x09, with a magic it picked itself, so this - * arrives outside the request/ack flow. - * - * Returns -1 when the frame is not such a push. Ordinary settings-read acks - * (the battery poll) carry commandId=2 and deviceReceiveRequestFromApp in - * field 4, so they never match and stay on the normal ack path. - */ - public static int parseSilentModePush(byte[] pb) { - if (pb == null) { - return -1; - } - byte[] root = stripTrailingCrc(pb); - if (readVarintFieldValue(root, 1, -1) != SETTINGS_CMD_DEVICE_SEND_TO_APP) { - return -1; - } - byte[] info = readFieldBytes(root, 5); - if (info == null) { - return -1; - } - return readVarintFieldValue(info, 2, -1); - } - - /** - * Firmware versions and the firmware-extension string from a settings - * READ ack. Versions are fields 5/6 of the deviceReceiveRequestFromApp - * submessage (field 4); Faceclaw's custom firmware additionally appends - * top-level field 100 with its revision ("Faceclaw/"; older builds sent - * "EVENCFW/ "), which stock firmware never sends. Returns null - * when the ack carries none of it. Compatibility is judged on the TS side - * (app/g2/firmware-compat.ts); Java only needs to know whether the firmware - * is ours at all. - */ - public static FirmwareInfo parseSettingsFirmwareInfo(byte[] pb) { - byte[] root = stripTrailingCrc(pb); - byte[] request = readFieldBytes(root, 4); - if (request == null) { - return null; - } - String leftVersion = readStringFieldValue(request, 5); - String rightVersion = readStringFieldValue(request, 6); - String extension = readStringFieldValue(root, 100); - if (leftVersion.isEmpty() && rightVersion.isEmpty() && extension.isEmpty()) { - return null; - } - return new FirmwareInfo(leftVersion, rightVersion, extension); - } - - public static byte[] wrapEvenHub(int cmd, int magic, int innerFieldNumber, byte[] inner) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, cmd)); - parts.add(encodeVarintField(2, magic)); - parts.add(encodeMessageField(innerFieldNumber, inner)); - return concat(parts); - } - - public static byte[] encodeTextObject(String name, int containerId, int x, int y, int width, int height, String text, boolean captureEvents) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, x)); - parts.add(encodeVarintField(2, y)); - parts.add(encodeVarintField(3, width)); - parts.add(encodeVarintField(4, height)); - parts.add(encodeVarintField(9, containerId)); - parts.add(encodeStringField(10, name)); - if (captureEvents) { - parts.add(encodeVarintField(11, 1)); - } - parts.add(encodeStringField(12, text)); - return concat(parts); - } - - public static byte[] encodeImageObject(ImageTileOptions tile) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, tile.x)); - parts.add(encodeVarintField(2, tile.y)); - parts.add(encodeVarintField(3, tile.width)); - parts.add(encodeVarintField(4, tile.height)); - parts.add(encodeVarintField(5, tile.containerId)); - parts.add(encodeStringField(6, tile.name)); - return concat(parts); - } - - private static byte[] concat(List parts) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - for (byte[] part : parts) { - if (part != null && part.length > 0) { - out.write(part, 0, part.length); - } - } - return out.toByteArray(); - } - - private static byte[] encodeVarintField(int fieldNumber, int value) { - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 0), encodeVarint(value))); - } - - private static byte[] encodeStringField(int fieldNumber, String value) { - byte[] bytes = value == null ? new byte[0] : value.getBytes(StandardCharsets.UTF_8); - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.length), bytes)); - } - - private static byte[] encodeBytesField(int fieldNumber, byte[] value) { - byte[] bytes = value == null ? new byte[0] : value; - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.length), bytes)); - } - - private static byte[] encodeMessageField(int fieldNumber, byte[] value) { - byte[] bytes = value == null ? new byte[0] : value; - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.length), bytes)); - } - - private static byte[] encodeKey(int fieldNumber, int wireType) { - return encodeVarint((fieldNumber << 3) | wireType); - } - - private static byte[] encodeVarint(int value) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - int v = value >>> 0; - while (v >= 0x80) { - out.write((v & 0x7f) | 0x80); - v >>>= 7; - } - out.write(v); - return out.toByteArray(); - } - - private static byte[] crcBytesLe(byte[] data) { - int crc = 0xffff; - for (byte b : data) { - crc ^= (b & 0xff) << 8; - for (int i = 0; i < 8; i++) { - if ((crc & 0x8000) != 0) { - crc = ((crc << 1) ^ 0x1021) & 0xffff; - } else { - crc = (crc << 1) & 0xffff; - } - } - } - return new byte[] {(byte) (crc & 0xff), (byte) ((crc >>> 8) & 0xff)}; - } - - - /** - * 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); - } - int sid = buf[6] & 0xff; - int flag = buf[7] & 0xff; - int len = buf[3] & 0xff; - int end = Math.min(buf.length, 8 + len); - byte[] pb = Arrays.copyOfRange(buf, 8, end); - - int msgType = -1; - int msgSeq = -1; - int offset = 0; - for (int i = 0; i < 8 && offset < pb.length; i++) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - break; - } - offset = key.next; - int tag = key.value >> 3; - int wire = key.value & 7; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - break; - } - if (tag == 1) { - msgType = value.value; - } else if (tag == 2) { - msgSeq = value.value; - } - offset = value.next; - } else if (wire == 2) { - VarintResult length = readVarint(pb, offset); - if (length == null) { - break; - } - offset = length.next + length.value; - } else { - break; - } - if (msgType >= 0 && msgSeq >= 0) { - break; - } - } - return new ParsedFrame(true, sid, flag, pb, msgType, msgSeq); - } - - public static byte[] stripTrailingCrc(byte[] pb) { - if (pb == null || pb.length < 2) { - return pb == null ? new byte[0] : pb; - } - return Arrays.copyOf(pb, pb.length - 2); - } - - /** Decode the stock firmware's compass navigation notifications (sid 0x08). */ - public static CompassEvent parseCompassEvent(ParsedFrame frame) { - if (frame == null - || !frame.ok - || frame.sid != SID_NAVIGATION - || (frame.flag != FLAG_NOTIFY && frame.flag != FLAG_NOTIFY_ALT)) { - return null; - } - byte[] root = stripTrailingCrc(frame.pb); - int command = readVarintFieldValue(root, 1, -1); - if (command == NAV_CMD_COMPASS_CHANGED) { - byte[] compass = readFieldBytes(root, 10); - if (compass == null) return null; - int heading = readVarintFieldValue(compass, 1, -1); - if (heading < 0 || heading >= 360) return null; - byte[] diagnostic = readFieldBytes(root, 100); - if (diagnostic != null && diagnostic.length == 12 - && diagnostic[0] == 'C' && diagnostic[1] == 'M' && diagnostic[2] == 1 - && diagnostic[7] == 0 - && ((diagnostic[3] & 0xff) <= 3 || diagnostic[3] == (byte) 255) - && ((diagnostic[4] & 0xff) <= 2 || diagnostic[4] == (byte) 255) - && (diagnostic[5] & 0xff) <= 3) { - long sampleTimeMs = 0; - for (int i = 0; i < 4; i++) sampleTimeMs |= (long) (diagnostic[8 + i] & 0xff) << (8 * i); - return new CompassEvent(command, heading, - diagnostic[3] == (byte) 255 ? -1 : diagnostic[3] & 0xff, - diagnostic[4] == (byte) 255 ? -1 : diagnostic[4] & 0xff, - diagnostic[5] & 0xff, diagnostic[6] & 0xff, sampleTimeMs); - } - // Stock/older CFW and unknown extensions still supply usable headings. - return new CompassEvent(command, heading); - } - if (command == NAV_CMD_COMPASS_CALIBRATION_STARTED - || command == NAV_CMD_COMPASS_CALIBRATION_COMPLETE) { - return new CompassEvent(command, -1); - } - return null; - } - - /** - * Display wake notification observed after a ring or right-arm double tap - * while no EvenHub page is running: - * - * sid=0x0d, flag=notify, payload f1=1, f3={f1=1} - * - * Other state-change shapes (wear, charging, head-up, etc.) are deliberately - * excluded. The caller additionally gates this on an intentional EvenHub - * suspension, so normal state traffic cannot become application input. - */ - public static boolean isDisplayWakeStateChange(ParsedFrame frame) { - if (frame == null - || !frame.ok - || frame.sid != SID_STATE_CHANGE - || (frame.flag != FLAG_NOTIFY && frame.flag != FLAG_NOTIFY_ALT)) { - return false; - } - byte[] root = stripTrailingCrc(frame.pb); - if (readVarintFieldValue(root, 1, -1) != 1) { - return false; - } - byte[] state = readFieldBytes(root, 3); - return state != null - && state.length == 2 - && readVarintFieldValue(state, 1, -1) == 1; - } - - public static byte[] readFieldBytes(byte[] pb, int fieldNumber) { - int offset = 0; - while (offset < pb.length) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - return null; - } - offset = key.next; - int field = key.value >> 3; - int wire = key.value & 0x07; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - return null; - } - offset = value.next; - continue; - } - if (wire == 1) { - offset += 8; - continue; - } - if (wire == 5) { - offset += 4; - continue; - } - if (wire != 2) { - return null; - } - VarintResult length = readVarint(pb, offset); - if (length == null) { - return null; - } - offset = length.next; - int end = offset + length.value; - if (end > pb.length) { - return null; - } - byte[] bytes = Arrays.copyOfRange(pb, offset, end); - offset = end; - if (field == fieldNumber) { - return bytes; - } - } - return null; - } - - public static int readVarintFieldValue(byte[] pb, int fieldNumber, int defaultValue) { - int offset = 0; - while (offset < pb.length) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - return defaultValue; - } - offset = key.next; - int field = key.value >> 3; - int wire = key.value & 0x07; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - return defaultValue; - } - offset = value.next; - if (field == fieldNumber) { - return value.value; - } - continue; - } - if (wire == 1) { - offset += 8; - continue; - } - if (wire == 5) { - offset += 4; - continue; - } - if (wire != 2) { - return defaultValue; - } - VarintResult length = readVarint(pb, offset); - if (length == null) { - return defaultValue; - } - offset = length.next + length.value; - } - return defaultValue; - } - - /** - * Read a `float` (protobuf wire type 5, little-endian fixed32) field. The - * g2-kit proto declares IMU_Report_Data's x/y/z as `double`, but the - * firmware actually sends them as fixed32 floats (confirmed from captures). - * Returns defaultValue if absent. - */ - public static float readFloatFieldValue(byte[] pb, int fieldNumber, float defaultValue) { - int offset = 0; - while (offset < pb.length) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - return defaultValue; - } - offset = key.next; - int field = key.value >> 3; - int wire = key.value & 0x07; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - return defaultValue; - } - offset = value.next; - } else if (wire == 1) { - offset += 8; - } else if (wire == 5) { - if (offset + 4 > pb.length) { - return defaultValue; - } - if (field == fieldNumber) { - int bits = 0; - for (int i = 0; i < 4; i++) { - bits |= (pb[offset + i] & 0xff) << (8 * i); - } - return Float.intBitsToFloat(bits); - } - offset += 4; - } else if (wire == 2) { - VarintResult length = readVarint(pb, offset); - if (length == null) { - return defaultValue; - } - offset = length.next + length.value; - } else { - return defaultValue; - } - } - return defaultValue; - } - - public static String readStringFieldValue(byte[] pb, int fieldNumber) { - byte[] bytes = readFieldBytes(pb, fieldNumber); - if (bytes == null) { - return ""; - } - return new String(bytes, StandardCharsets.UTF_8); - } - - public static VarintResult readVarint(byte[] pb, int offset) { - int value = 0; - int shift = 0; - int cursor = offset; - while (cursor < pb.length) { - int b = pb[cursor++] & 0xff; - value |= (b & 0x7f) << shift; - if ((b & 0x80) == 0) { - return new VarintResult(value, cursor); - } - shift += 7; - } - return null; - } - - - public static final class ParsedFrame { - final boolean ok; - final int sid; - final int flag; - final byte[] pb; - final int msgType; - final int msgSeq; - - ParsedFrame(boolean ok, int sid, int flag, byte[] pb, int msgType, int msgSeq) { - this.ok = ok; - this.sid = sid; - this.flag = flag; - this.pb = pb; - this.msgType = msgType; - this.msgSeq = msgSeq; - } - } - - private static final class VarintResult { - final int value; - final int next; - - VarintResult(int value, int next) { - this.value = value; - this.next = next; - } - } - - public static final class ListSelection { - public final String containerName; - public final String itemName; - public final int itemIndex; - public final int eventType; - - ListSelection(String containerName, String itemName, int itemIndex, int eventType) { - this.containerName = containerName == null ? "" : containerName; - this.itemName = itemName == null ? "" : itemName; - this.itemIndex = itemIndex; - this.eventType = eventType; - } - } - - public static final class CompassEvent { - public final int command; - public final int headingDegrees; - /** -1 means unavailable; source: 0 unknown, 1 GRV, 2 GMRV, 3 RV. */ - public final int magneticAccuracy, magneticAnomalies, orientationSource, diagnosticFlags; - public final long sampleTimeMs; - - CompassEvent(int command, int headingDegrees) { - this(command, headingDegrees, -1, -1, -1, -1, -1); - } - - CompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies, - int orientationSource, int diagnosticFlags, long sampleTimeMs) { - this.command = command; - this.headingDegrees = headingDegrees; - this.magneticAccuracy = magneticAccuracy; - this.magneticAnomalies = magneticAnomalies; - this.orientationSource = orientationSource; - this.diagnosticFlags = diagnosticFlags; - this.sampleTimeMs = sampleTimeMs; - } - } - - public static final class ImageTileOptions { - final String name; - final int containerId; - final int x; - final int y; - final int width; - final int height; - - ImageTileOptions(String name, int containerId, int x, int y, int width, int height) { - this.name = name; - this.containerId = containerId; - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - } - - public static final class ImageFragment { - final int index; - final byte[] data; - final int size; - - ImageFragment(int index, byte[] data, int size) { - this.index = index; - this.data = data; - this.size = size; - } - } - - public static final class BatterySnapshot { - final int battery; - final int charging; - /** 1 = silent mode on, 0 = off, -1 = the ack didn't carry the field. */ - final int silentMode; - - BatterySnapshot(int battery, int charging, int silentMode) { - this.battery = battery; - this.charging = charging; - this.silentMode = silentMode; - } - } - - public static final class FirmwareInfo { - /** Prefix of the firmware-extension string on Faceclaw's custom firmware. */ - public static final String FACECLAW_EXTENSION_PREFIX = "Faceclaw/"; - - final String leftVersion; - final String rightVersion; - /** Raw field-100 string ("" on stock firmware). */ - final String extension; - - FirmwareInfo(String leftVersion, String rightVersion, String extension) { - this.leftVersion = leftVersion; - this.rightVersion = rightVersion; - this.extension = extension == null ? "" : extension; - } - - /** - * True when the glasses run Faceclaw's custom firmware (any revision). - * Whether the revision is the one this app needs is decided on the TS - * side, which disconnects on a mismatch; this only guards the private - * modes against stock or third-party firmware in the meantime. - */ - public boolean isFaceclawFirmware() { - return extension.trim().startsWith(FACECLAW_EXTENSION_PREFIX); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwMessageWindow.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwMessageWindow.java deleted file mode 100644 index c342b066..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwMessageWindow.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.faceclaw.app; - -import java.util.ArrayList; -import java.util.List; - -/** Ordered completion and go-back-N recovery for the custom-message window. */ -final class CfwMessageWindow { - // Flappy capture: p99 ACK 63ms, max 77ms. Leave room for full-frame - // processing while recovering a lost reply well before the stock timeout. - static final int ACK_TIMEOUT_MS = 500; - static final int MAX_RETRIES = 3; - - private CfwMessageWindow() {} - - static OutboundMessage acknowledgedHead(Iterable messages) { - for (OutboundMessage message : messages) { - if (message.sid != CfwTransport.SID) continue; - return !message.cfwRetryPending && message.cfwAckLenses == CfwTransport.BOTH ? message : null; - } - return null; - } - - static List replayWindow(Iterable messages, long nowMs) { - List replay = new ArrayList<>(); - boolean failed = false; - for (OutboundMessage message : messages) { - if (message.sid != CfwTransport.SID) continue; - failed |= message.cfwRetryPending || (message.cfwAckLenses != CfwTransport.BOTH - && message.ackDeadlineAtMs > 0 && message.ackDeadlineAtMs <= nowMs); - replay.add(message); - } - // A lost ACK, first fragment, or NACK can leave an older attempt - // unresolved. Replaying only the named failure strands that older - // attempt at the head until its timeout. Rewind the unresolved window. - if (!failed) replay.clear(); - return replay; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java deleted file mode 100644 index e921b3fb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.faceclaw.app; - -import java.io.ByteArrayOutputStream; -import java.util.zip.Deflater; -import java.util.ArrayList; -import java.util.List; - -/** Faceclaw/10 records: clear flags, wire length and decoded CRC; persistent zlib body. */ -public final class CfwTransport { - public static final int SID = 0xf0; - public static final int BOTH = 3; - public static final int MAX_MESSAGE = 65535; - public static final int COMPRESSED = 4, RESET_CONTEXT = 8; - private final Deflater deflater = new Deflater(); - private boolean resetPending = true; - private int previousLenses; - - public CfwTransport() {} - - public synchronized void reset() { - deflater.reset(); - resetPending = true; - previousLenses = 0; - } - - /** Compress only when writing, in exactly the order seen by the receiver. */ - public synchronized List encode(byte[] message, int streamId, int lenses, int mtu) { - validate(message, streamId, lenses, mtu); - if (previousLenses != lenses) reset(); - boolean reset = resetPending; - deflater.setInput(message); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] chunk = new byte[4096]; - int count; - do { - count = deflater.deflate(chunk, 0, chunk.length, Deflater.SYNC_FLUSH); - out.write(chunk, 0, count); - } while (count == chunk.length); - byte[] body = out.toByteArray(); - int flags = lenses | COMPRESSED | (reset ? RESET_CONTEXT : 0); - if (body.length == 0) flags &= ~COMPRESSED; // repeated empty sync flush - if (body.length > MAX_MESSAGE) { - // Incompressible maximum-size records still fit as raw records. - // The attempted deflate advanced history: reset both ends before reuse. - reset(); - body = message; - flags = lenses | RESET_CONTEXT; - } else { - resetPending = false; - previousLenses = lenses; - } - return frameRecord(body, crc(message), streamId, flags, mtu); - } - - private static void validate(byte[] message, int streamId, int lenses, int mtu) { - if (message == null || message.length > MAX_MESSAGE || streamId < 0 || streamId > 255 - || lenses < 1 || lenses > BOTH || mtu < 23 || mtu > 517) - throw new IllegalArgumentException("invalid CFW stream parameters"); - } - - /** One logical command per stream for now. Packets are bounded by actual ATT MTU. */ - public static List frame(byte[] message, int streamId, int lenses, int mtu) { - validate(message, streamId, lenses, mtu); - return frameRecord(message, crc(message), streamId, lenses | RESET_CONTEXT, mtu); - } - - private static List frameRecord(byte[] body, int checksum, int streamId, int flags, int mtu) { - int lenses = flags & BOTH; - int capacity = Math.min(252, mtu - 14); - byte[] stream = new byte[body.length + 5]; - stream[0] = (byte) flags; - stream[1] = (byte) body.length; - stream[2] = (byte) (body.length >>> 8); - stream[3] = (byte) checksum; - stream[4] = (byte) (checksum >>> 8); - System.arraycopy(body, 0, stream, 5, body.length); - List packets = new ArrayList<>(); - for (int offset = 0, index = 0; offset < stream.length; offset += capacity, index++) { - int count = Math.min(capacity, stream.length - offset); - byte[] packet = new byte[count + 11]; - packet[0] = (byte) 0xaa; - packet[1] = 0x21; - packet[2] = (byte) (streamId + index); - packet[3] = (byte) (count + 3); - packet[4] = packet[5] = 1; - packet[6] = (byte) SID; - packet[8] = (byte) (lenses | (offset == 0 ? 0x80 : 0) - | (offset + count == stream.length ? 0x40 : 0)); - System.arraycopy(stream, offset, packet, 9, count); - int crc = crc(packet, 8, count + 1); - packet[packet.length - 2] = (byte) crc; - packet[packet.length - 1] = (byte) (crc >>> 8); - packets.add(packet); - } - return packets; - } - - public static int crc(byte[] data) { return crc(data, 0, data.length); } - private static int crc(byte[] data, int offset, int count) { - int crc = 0xffff; - for (int i = offset; i < offset + count; i++) { - crc ^= (data[i] & 255) << 8; - for (int bit = 0; bit < 8; bit++) - crc = ((crc << 1) ^ ((crc & 0x8000) != 0 ? 0x1021 : 0)) & 65535; - } - return crc; - } - private static int u16(byte[] data, int offset) { - return (data[offset] & 255) | ((data[offset + 1] & 255) << 8); - } - public static Ack parseAck(byte[] packet) { - Ack[] acks = parseAcks(packet); - return acks == null ? null : acks[0]; - } - /** Primary ACK followed by up to three explicit preceding successes from - * the same processing lens. Validate the entire packet before applying any. */ - public static Ack[] parseAcks(byte[] packet) { - if (packet == null || packet.length < 19 || packet.length > 40 - || (packet.length - 19) % 7 != 0 || (packet[0] & 255) != 0xaa - || packet[1] != 0x12 || (packet[3] & 255) != packet.length - 8 || packet[4] != 1 || packet[5] != 1 - || (packet[6] & 255) != SID || packet[7] != 0 || (packet[8] != 1 && packet[8] != 3) - || (packet[12] != 1 && packet[12] != 2) - || (packet[8] == 3 && packet.length != 19) - || crc(packet, 8, packet.length - 10) != u16(packet, packet.length - 2)) return null; - Ack[] acks = new Ack[1 + (packet.length - 19) / 7]; - acks[0] = new Ack(packet[8] == 3, packet[9] & 255, u16(packet, 10), packet[12], u16(packet, 13), u16(packet, 15)); - for (int i = 1, offset = 17; i < acks.length; i++, offset += 7) - acks[i] = new Ack(false, packet[offset] & 255, u16(packet, offset + 1), - packet[12], u16(packet, offset + 3), u16(packet, offset + 5)); - return acks; - } - public static final class Ack { - public final boolean nack; - public final int streamId, messageId, lens, size, checksum; - Ack(boolean nack, int streamId, int messageId, int lens, int size, int checksum) { - this.nack = nack; - this.streamId = streamId; this.messageId = messageId; this.lens = lens; - this.size = size; this.checksum = checksum; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java deleted file mode 100644 index a358dba9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.faceclaw.app; -import android.bluetooth.BluetoothGattCharacteristic; - -public class ConnectionOptions { - public static final int WRITE_TYPE = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE; - public static final int DESIRED_MTU = 512; - - public static final int CONNECT_TIMEOUT_MS = 5_000; - public static final int SERVICES_TIMEOUT_MS = 5_000; - public static final int DESCRIPTOR_TIMEOUT_MS = 5_000; - public static final int RING_DESIRED_MTU = 247; - public static final int RING_RECONNECT_DELAY_MS = 2_000; - public static final int WRITE_TIMEOUT_MS = 2_000; - public static final int PRELUDE_TIMEOUT_MS = 2_000; - // Application security-auth exchange (sid 0x80). The success notification - // only arrives once the BLE link is encrypted, which on a first-time - // connection means SMP pairing — possibly with an OS dialog the user has - // to accept — so the stock-firmware paths wait generously. - public static final int SECURITY_AUTH_TIMEOUT_MS = 30_000; - // The main communicator's wait is soft (it proceeds on timeout so firmware - // that never answers can't stall every reconnect), so it is kept short. - public static final int SECURITY_AUTH_SOFT_TIMEOUT_MS = 6_000; - public static final int ACK_TIMEOUT_MS = 3_500; - public static final int HEARTBEAT_FAILURE_DEADLINE_MS = 10_000; - public static final int HEARTBEAT_READY_MS = 4_000; - public static final int HEARTBEAT_URGENT_MS = 6_000; - public static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000; - public static final int BATTERY_INPUT_QUIET_MS = 5_000; - // Poll cadence while in charging mode; also bounds how quickly we notice - // the glasses coming back out of the case. - public static final int CHARGING_BATTERY_POLL_MS = 30_000; - public static final int IMAGE_FRAGMENT_SIZE = 3800; - public static final int IMAGE_RETRY_DELAY_MS = 2_000; - public static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8; - public static final int EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000; - public static final int IDLE_SLEEP_MS = 100; - public static final int RECONNECT_DELAY_MS = 2_000; - - final boolean sendImagesToLeft = true; - final boolean skipSessionIds = true; - // Max messages in flight (awaiting ack) at once — full pipelining. >1 lets a - // frame be sent before the previous frame's ack returns, hiding the BLE ack - // round-trip that otherwise serialized every update. Requires firmware that - // accepts pipelined image messages (the CFW snapshot/deferred FIFO). At 1 the - // communicator behaves exactly as before (serial sends + last-packet prewrite). - final int WINDOW_SIZE = 3; - // Send changed-region (mode 3 bounding box) updates instead of full frames - // when the previous frame is known to be displayed. Requires firmware with - // the experimental bbox-incremental mode. Disabled: the firmware-side - // display buffer is not always the previous frame (occasionally two frames - // back, apparently display-driver buffer swapping), so partial updates - // composite onto stale content per-lens. Re-enable once the firmware - // guarantees the compositing base. - final boolean INCREMENTAL_FRAMES = true; - - // Send several tight rectangle deltas as one CFW mode-8 multi-segment message - // instead of a single bounding box, when the change splits into regions whose - // bounding box wastes bytes (distant edits, or a sparse box). Requires firmware - // with mode-8 multi-rect support; needs INCREMENTAL_FRAMES (same seeded shadow). - // Falls back to the single bounding box whenever multi-rect isn't smaller. - final boolean MULTI_RECT_FRAMES = true; - // Cap on rects per batch. Each rect consumes a distinct CFW frame id, and the - // firmware's duplicate-fid ring holds 16, so keep several batches of history - // within it. Above this the split is abandoned for a single bounding box. - public static final int MULTI_RECT_MAX_RECTS = 6; - // Don't bother splitting when the single bounding box already compresses this - // small (one tight rect); only attempt multi-rect above it or when the box - // spans multiple horizontal clusters. - public static final int MULTI_RECT_MIN_PAYLOAD = 900; - - // Ship text as on-glasses cached-glyph draws (CFW modes 12/14) instead of - // pixels, punching glyph ink out of the baked deltas (see TexturePlanner). - // Also gated at runtime on the firmware advertising texcache12 + texstr14. - final boolean TEXTURE_CACHE_FRAMES = true; -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/G2Event.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/G2Event.java deleted file mode 100644 index 7d5c57bb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/G2Event.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.faceclaw.app; - -public final class G2Event { - final String kind; - final String containerName; - final int eventType; - final int eventSource; - final int systemExitReasonCode; - // IMU_Report_Data (Sys_ItemEvent.IMUData), populated when the firmware - // attaches an accelerometer reading to a sys-event. - final boolean hasImu; - final double imuX; - final double imuY; - final double imuZ; - - public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode) { - this(kind, containerName, eventType, eventSource, systemExitReasonCode, false, 0, 0, 0); - } - - public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, - boolean hasImu, double imuX, double imuY, double imuZ) { - this.kind = kind; - this.containerName = containerName; - this.eventType = eventType; - this.eventSource = eventSource; - this.systemExitReasonCode = systemExitReasonCode; - this.hasImu = hasImu; - this.imuX = imuX; - this.imuY = imuY; - this.imuZ = imuZ; - } - - public static G2Event decode(BleProtocol.ParsedFrame frame) { - byte[] pb = BleProtocol.stripTrailingCrc(frame.pb); - - // sid 0x07 is the stock Even AI app, not EvenHub, so it carries an - // EvenAIDataPackage rather than the device-event wrapper below. - // EvenAIDataPackage { command_id = 1, magic_random = 2, ctrl = 3 }, - // EvenAIControl { status = 1 }. The event we care about is - // command_id=CTRL with status=WAKE_UP, which the firmware emits when the - // on-glasses "Hey Even" wakeword fires. - if (frame.sid == BleProtocol.SID_EVEN_AI) { - int commandId = BleProtocol.readVarintFieldValue(pb, 1, -1); - if (commandId != BleProtocol.EVEN_AI_CMD_CTRL) { - return null; - } - byte[] ctrl = BleProtocol.readFieldBytes(pb, 3); - if (ctrl == null) { - return null; - } - int status = BleProtocol.readVarintFieldValue(ctrl, 1, -1); - if (status < 0) { - return null; - } - return new G2Event("even-ai", "", status, 0, 0); - } - - byte[] deviceEvent = BleProtocol.readFieldBytes(pb, 13); - if (deviceEvent == null) { - return null; - } - - byte[] listEvent = BleProtocol.readFieldBytes(deviceEvent, 1); - if (listEvent != null) { - return new G2Event( - "list-click", - BleProtocol.readStringFieldValue(listEvent, 2), - BleProtocol.readVarintFieldValue(listEvent, 5, BleProtocol.EVENT_CLICK), - 0, - 0 - ); - } - - byte[] textEvent = BleProtocol.readFieldBytes(deviceEvent, 2); - if (textEvent != null) { - return new G2Event( - "text-click", - BleProtocol.readStringFieldValue(textEvent, 2), - BleProtocol.readVarintFieldValue(textEvent, 3, BleProtocol.EVENT_CLICK), - 0, - 0 - ); - } - - byte[] sysEvent = BleProtocol.readFieldBytes(deviceEvent, 3); - if (sysEvent != null) { - int eventType = BleProtocol.readVarintFieldValue(sysEvent, 1, BleProtocol.EVENT_CLICK); - int eventSource = BleProtocol.readVarintFieldValue(sysEvent, 2, 0); - int exitReason = BleProtocol.readVarintFieldValue(sysEvent, 4, 0); - // IMUData (field 3) is IMU_Report_Data { double x=1, y=2, z=3 }. It - // may accompany any sys-event, or arrive as a standalone - // IMU_DATA_REPORT sample. - byte[] imu = BleProtocol.readFieldBytes(sysEvent, 3); - if (imu != null) { - double x = BleProtocol.readFloatFieldValue(imu, 1, 0); - double y = BleProtocol.readFloatFieldValue(imu, 2, 0); - double z = BleProtocol.readFloatFieldValue(imu, 3, 0); - return new G2Event("sys-event", "", eventType, eventSource, exitReason, true, x, y, z); - } - return new G2Event("sys-event", "", eventType, eventSource, exitReason); - } - return null; - } - - private static String hex(byte[] data) { - if (data == null) { - return "null"; - } - StringBuilder sb = new StringBuilder(data.length * 2); - for (byte b : data) { - sb.append(String.format("%02x", b & 0xff)); - } - return sb.toString(); - } - -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java deleted file mode 100644 index 8377dcc4..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java +++ /dev/null @@ -1,281 +0,0 @@ -package com.faceclaw.app; - -public class MessageBuilder { - private static final int ACK_TIMEOUT_MS = 3_500; - private static final int HEARTBEAT_TIMEOUT_MS = 1_500; - - private BleMagicPool magicPool; - - public MessageBuilder(BleMagicPool magicPool) { - this.magicPool = magicPool; - } - - public OutboundMessage prelude() { - return new OutboundMessage( - "prelude", "prelude", - BleProtocol.PRELUDE_ACK_SID, - BleProtocol.FLAG_REQUEST, - BleProtocol.PRELUDE_ACK_MAGIC, - BleProtocol.PRELUDE_F5872_PAYLOAD, - ACK_TIMEOUT_MS, - -1, - false - ); - } - - /** The per-connection sid-0x80 security-auth request (one per arm). */ - public OutboundMessage securityAuth(boolean leftArm) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "security-auth", - "security auth " + (leftArm ? "L" : "R"), - BleProtocol.SID_SECURITY_AUTH, - BleProtocol.FLAG_SECURITY_AUTH, - magic, - BleProtocol.buildAuthenticationRequest(magic), - ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS, - -1, - leftArm - ); - } - - public OutboundMessage shutdown(int exitMode) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "shutdown", - "shutdown mode=" + exitMode, - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildShutdown(magic, exitMode), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage imagePayload( - BleProtocol.ImageTileOptions tile, - int sessionId, - byte[] payload, - String label, - boolean leftArm - ) { - return imagePayload("sound", tile, sessionId, payload, label, leftArm); - } - - public OutboundMessage imagePayload( - String kind, - BleProtocol.ImageTileOptions tile, - int sessionId, - byte[] payload, - String label, - boolean leftArm - ) { - // Keep these arguments for source compatibility with existing callers; - // the private command no longer carries a tile, session, or image header. - return customMessage(kind, payload, label, -1, leftArm); - } - - public OutboundMessage customMessage(String kind, byte[] payload, String label, - int tileIndex, boolean leftArm) { - if (payload == null || payload.length > CfwTransport.MAX_MESSAGE) - throw new IllegalArgumentException("CFW message exceeds uint16 length"); - int streamId = magicPool.allocate(); - return new OutboundMessage(kind, label, CfwTransport.SID, 0, streamId, payload, - CfwMessageWindow.ACK_TIMEOUT_MS, tileIndex, leftArm); - } - - public OutboundMessage cfwCleanup( - BleProtocol.ImageTileOptions tile, - int sessionId, - boolean leftArm - ) { - byte[] payload = new byte[] { (byte) BleProtocol.CFW_IMAGE_MODE_CLEANUP }; - return imagePayload( - "cfw-cleanup", - tile, - sessionId, - payload, - "CFW cleanup", - leftArm - ); - } - - public OutboundMessage enableOrDisableMic(boolean enable) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "audio-control", - enable ? "G2 mic enable" : "G2 mic disable", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildAudioControl(magic, enable), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage enableOrDisableImu(boolean enable, int reportFrq) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "imu-control", - enable ? "IMU enable" : "IMU disable", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildImuControl(magic, enable, reportFrq), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage setBrightness(boolean autoAdjust, int brightnessLevel) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "brightness-control", - autoAdjust ? "brightness auto" : "brightness level=" + brightnessLevel, - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSetBrightness(magic, autoAdjust, brightnessLevel), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage setWearDetection(boolean enabled) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "wear-detection-control", - "wear detection " + (enabled ? "enable" : "disable"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSetWearDetection(magic, enabled), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage createLayout() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "create-layout", - "create-layout", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildCreateInputPage(magic), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage startupTextProbe() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "startup-text-probe", - "startup text probe", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildDashboardTextUpgrade(magic), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage heartbeat() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "heartbeat", - "heartbeat", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildHeartbeat(magic), - HEARTBEAT_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage batteryQuery() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "battery", - "battery", - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSettingsQuery(magic), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage faceclawWakeControl(int operation, int nonce, boolean leftArm) { - return new OutboundMessage( - "wake-lease-control", - "wake lease op=" + operation + " nonce=" + nonce + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawWakeControl(operation, nonce), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } - - public OutboundMessage faceclawFramebufferControl(int operation, boolean leftArm) { - return new OutboundMessage( - "framebuffer-lease-control", - "framebuffer lease op=" + operation + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawWakeControl(operation, 0), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } - - /** CFW mic_control write (settings field 103) targeted at one temple. */ - public OutboundMessage faceclawMicControl(byte[] record, String label, boolean leftArm) { - return new OutboundMessage( - "mic-control", - "mic control " + label + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawMicControl(record), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } - - public OutboundMessage faceclawWearQuery(boolean leftArm) { - return new OutboundMessage( - "wear-query-control", - "wear state query" + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawWakeControl(BleProtocol.FACECLAW_WEAR_OP_QUERY, 0), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/OutboundMessage.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/OutboundMessage.java deleted file mode 100644 index bfdcd5cf..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/OutboundMessage.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.faceclaw.app; -import java.util.Arrays; - -public final class OutboundMessage { - final String kind; - final String label; - final int sid; - final int flag; - int magic; - final byte[] message; - final int ackTimeoutMs; - final int tileIndex; - final boolean isLeftArmMessage; - - int cfwAckLenses, cfwRetries; - boolean cfwRetryPending; - final int cfwChecksum; - - int imageUpdateId; - int imageMessageNumber; - int imageMessageCount; - long sentAtMs; - long writeStartedAtMs; - long ackDeadlineAtMs; - byte[] ackPayload = new byte[0]; - Runnable onSent; - Runnable onAck; - Runnable onTimeout; - - OutboundMessage(String kind, String label, int sid, int flag, int magic, byte[] message, int ackTimeoutMs, int tileIndex, boolean isLeftArmMessage) { - this.kind = kind; - this.label = label; - this.sid = sid; - this.flag = flag; - this.magic = magic; - this.message = message == null ? new byte[0] : Arrays.copyOf(message, message.length); - this.cfwChecksum = sid == CfwTransport.SID ? CfwTransport.crc(this.message) : 0; - this.ackTimeoutMs = ackTimeoutMs; - this.tileIndex = tileIndex; - this.isLeftArmMessage = isLeftArmMessage; - } - - /** NACK names this attempt's stream/message sequence, even without a decoded size. */ - boolean acceptCfwAck(CfwTransport.Ack ack) { - if (ack == null || sid != CfwTransport.SID || magic != ack.streamId - || ack.messageId != 0 || (ack.lens != 1 && ack.lens != 2)) return false; - if (ack.nack) { - cfwRetryPending = true; - return false; - } - if (message.length != ack.size || cfwChecksum != ack.checksum) return false; - cfwAckLenses |= ack.lens; - return !cfwRetryPending && cfwAckLenses == CfwTransport.BOTH; - } - - /** Replace an attempt atomically under the communicator lock. */ - void prepareCfwReplay(int streamId) { - magic = streamId; - ++cfwRetries; - cfwAckLenses = 0; - cfwRetryPending = false; - ackDeadlineAtMs = 0; - ackPayload = new byte[0]; - } - - void setImageUpdatePosition(int updateId, int messageNumber, int messageCount) { - this.imageUpdateId = updateId; - this.imageMessageNumber = messageNumber; - this.imageMessageCount = messageCount; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java deleted file mode 100644 index 23cebc1d..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java +++ /dev/null @@ -1,179 +0,0 @@ -package com.faceclaw.app; - -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Phone-side model of the CFW's flat 256 KiB texture cache (zlib_glue mode 18). - * The firmware holds only bytes; every structural decision — where font - * tables and glyph images live, what is resident, when to start over — is - * made here. Layout per font: a 96-entry uint32 offset table (chars 32..127) - * whose entries are written lazily as each glyph is uploaded; glyph images - * are the GlyphAtlas cached bytes ([w][h][4bpp RLE]). - * - * Allocation is a bump pointer with reset-on-full: the realistic working set - * (a few fonts' tables plus ~hundreds of glyphs at tens of bytes each) is far - * below 256 KiB, so eviction sophistication buys nothing. After a reset the - * firmware cache still holds stale bytes; that is safe because a draw only - * ever references (font table entry, glyph image) pairs uploaded in the - * current generation — stale table entries are never named in a mode-20 - * string. - * - * Upload entries accumulate as complete [offset u32][len u16][data] records - * and drain into one or more standalone mode-18 payloads, each independently - * valid, enqueued ahead of the image message that references them (the - * transport is FIFO, so no ack round-trip is needed before use). - * - * Not thread-safe by itself: the communicator calls it under its own lock. - */ -public final class TextureCacheState { - public static final int CACHE_SIZE = 262144; - private static final int FONT_TABLE_BYTES = 96 * 4; - - private int allocEnd = 0; - private final Map fontTableOffsets = new HashMap<>(); - private final Map glyphOffsets = new HashMap<>(); - private final Map imageOffsets = new HashMap<>(); - private final List pendingEntries = new ArrayList<>(); - private int pendingBytes = 0; - private int generation = 0; - - /** - * Forget everything: the on-glasses cache is gone (session teardown, lease - * loss) or no longer trustworthy (upload timeout), or we ran out of space. - * Glyphs and images re-upload lazily on next use. - */ - public void reset() { - allocEnd = 0; - fontTableOffsets.clear(); - glyphOffsets.clear(); - imageOffsets.clear(); - pendingEntries.clear(); - pendingBytes = 0; - generation++; - } - - /** Bumped by every reset; lets a planner detect a mid-plan reset. */ - public int generation() { - return generation; - } - - /** - * Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a - * sudden drop in the frame log means a reset-on-full re-upload cycle — - * the signal that the 256 KiB budget is being outgrown). - */ - public int usedBytes() { - return allocEnd; - } - - /** The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit. */ - public int fontTableOffset(int fontId) { - Integer existing = fontTableOffsets.get(fontId); - return existing == null ? -1 : existing; - } - - /** - * Ensure a glyph image (and its font table slot) is resident, queuing the - * mode-18 upload entries for anything newly placed. Returns the glyph's - * image offset, or -1 when the cache is full (caller resets and retries) - * or the encoding is outside the mode-20 table (32..127). - */ - public int ensureGlyph(int fontId, int encoding, GlyphAtlas.Glyph glyph) { - if (glyph == null || encoding < 32 || encoding > 127) return -1; - long key = ((long) fontId << 32) | (encoding & 0xffffffffL); - Integer existing = glyphOffsets.get(key); - if (existing != null) return existing; - - Integer table = fontTableOffsets.get(fontId); - int need = glyph.cachedBytes.length + (table == null ? FONT_TABLE_BYTES : 0); - if (allocEnd + need > CACHE_SIZE) return -1; - - if (table == null) { - table = allocEnd; - allocEnd += FONT_TABLE_BYTES; - fontTableOffsets.put(fontId, table); - // No upload for the table itself: entries are written per glyph, - // and only uploaded entries are ever referenced by a draw. - } - int offset = allocEnd; - allocEnd += glyph.cachedBytes.length; - glyphOffsets.put(key, offset); - - addEntry(offset, glyph.cachedBytes); - addEntry(table + 4 * (encoding - 32), - new byte[]{(byte) offset, (byte) (offset >> 8), - (byte) (offset >> 16), (byte) (offset >> 24)}); - return offset; - } - - /** - * Ensure an ImageAtlas entry is resident (mode-19 draws reference it by - * raw cache offset; no table involved). Returns the image offset, or -1 - * when the cache is full (caller resets and retries). - */ - public int ensureImage(int imageId, ImageAtlas.Entry image) { - if (image == null) return -1; - Integer existing = imageOffsets.get(imageId); - if (existing != null) return existing; - if (allocEnd + image.cachedBytes.length > CACHE_SIZE) return -1; - int offset = allocEnd; - allocEnd += image.cachedBytes.length; - imageOffsets.put(imageId, offset); - addEntry(offset, image.cachedBytes); - return offset; - } - - private void addEntry(int offset, byte[] data) { - byte[] entry = new byte[6 + data.length]; - entry[0] = (byte) (offset & 0xff); - entry[1] = (byte) ((offset >> 8) & 0xff); - entry[2] = (byte) ((offset >> 16) & 0xff); - entry[3] = (byte) ((offset >> 24) & 0xff); - entry[4] = (byte) (data.length & 0xff); - entry[5] = (byte) ((data.length >> 8) & 0xff); - System.arraycopy(data, 0, entry, 6, data.length); - pendingEntries.add(entry); - pendingBytes += entry.length; - } - - public boolean hasPendingUploads() { - return !pendingEntries.isEmpty(); - } - - /** Total queued upload bytes (entry framing included), for logging. */ - public int pendingUploadBytes() { - return pendingBytes; - } - - /** - * Drain the queued entries into complete mode-18 payloads ([18][entries]), - * each at most maxPayloadBytes so a payload fits one BLE image message - * without fragmentation. A single entry larger than the cap still gets its - * own payload (the transport can fragment it). - */ - public List drainUploadPayloads(int maxPayloadBytes) { - List payloads = new ArrayList<>(); - ByteArrayOutputStream current = null; - for (byte[] entry : pendingEntries) { - if (current != null && current.size() + entry.length > maxPayloadBytes) { - payloads.add(current.toByteArray()); - current = null; - } - if (current == null) { - current = new ByteArrayOutputStream(Math.min(maxPayloadBytes, 4 + entry.length)); - current.write(18); - } - current.write(entry, 0, entry.length); - } - if (current != null) { - payloads.add(current.toByteArray()); - } - pendingEntries.clear(); - pendingBytes = 0; - return payloads; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java deleted file mode 100644 index ac700cc2..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java +++ /dev/null @@ -1,721 +0,0 @@ -package com.faceclaw.app; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * Plans a screen update that ships text and icons as on-glasses cached draws - * instead of pixels (CFW modes 18/19/20; see g2flash/patches/zlib_glue.c and - * texture_cache.c). - * - * Approach: dirty rects come from comparing the fully composited old and new - * frames, exactly as the plain incremental path does. For each deferred draw - * (glyph or image) whose pixels intersect a sent rect, check that every pixel - * the on-glasses draw would write lands correct — i.e. the new composite's - * 4bpp value at each in-panel nonzero-source pixel equals what the draw - * produces (this one check subsumes plane occlusion, surface - * occlusion/clipping, and draw-on-draw overlap). A draw that passes and is - * (or can be made) resident in the texture cache is replayed on-glasses: its - * written pixels are punched to 0 in the delta rect content — which is what - * makes text and icons nearly free to compress — and re-emitted as a mode-20 - * string or mode-19 image sub-message in the same atomic mode-8 batch, so - * the shadow after apply equals the full composite exactly. Everything else - * stays baked. - * - * Fallback shape: when nothing qualifies, plan() returns null and the caller - * uses the plain single-bbox/multi-rect/full-frame paths unchanged. - */ -public final class TexturePlanner { - private TexturePlanner() {} - - /** Cap on mode-20 string sub-messages (mode-8 count is a u8). */ - private static final int MAX_GLYPH_RUNS = 180; - /** - * Soft cap on cached-glyph selection per update, before run grouping. - * Sized above a full-screen 6x12 terminal repaint (~4,240 cells) so dense - * screens draw everything; the run budget is the real bound. - */ - private static final int MAX_SELECTED_GLYPHS = 4600; - /** Cap on mode-19 image sub-messages per update. */ - private static final int MAX_IMAGE_DRAWS = 60; - /** Cap on mode-15 builtin-font string sub-messages per update. */ - private static final int MAX_FWTEXT_RUNS = 80; - /** Mode-18 payload size that fits one BLE image message comfortably. */ - private static final int UPLOAD_PAYLOAD_MAX = 3600; - /** Max x-adjust control bytes between two glyphs before starting a new run. */ - private static final int MAX_ADJUST_BYTES = 4; - /** Options: identity LUT (top 15) + transparent, for mode-19 image draws. */ - private static final int IMAGE_DRAW_OPTIONS = 0x1f; - - public static final class Result { - /** Complete image payload: a mode-8 batch of rect deltas + cached draws. */ - public final byte[] payload; - /** Mode-18 upload payloads to enqueue BEFORE the image message. */ - public final List uploads; - /** Next mode-3 frame id (deltas consumed some). */ - public final int nextFid; - public final int rectCount; - public final int drawnGlyphs; - public final int runCount; - public final int drawnImages; - public final int fwGlyphs; - public final int fwRuns; - public final int bakedCandidates; - public final int uploadBytes; - public final boolean fullFrame; - /** - * Where the planning time went, in ms. This runs on the send thread, - * directly in the input-to-display path, so the split is reported in - * the frame log: rects = finding the changed region, match = checking - * each draw against the composite, cache = residency/uploads plus run - * building, punch = clearing replayed draws out of a copy of the - * frame, encode = compressing the leftover pixels. - */ - public int rectsMs; - public int matchMs; - public int cacheMs; - public int punchMs; - public int encodeMs; - - Result(byte[] payload, List uploads, int nextFid, int rectCount, - int drawnGlyphs, int runCount, int drawnImages, int fwGlyphs, int fwRuns, - int bakedCandidates, int uploadBytes, boolean fullFrame) { - this.payload = payload; - this.uploads = uploads; - this.nextFid = nextFid; - this.rectCount = rectCount; - this.drawnGlyphs = drawnGlyphs; - this.runCount = runCount; - this.drawnImages = drawnImages; - this.fwGlyphs = fwGlyphs; - this.fwRuns = fwRuns; - this.bakedCandidates = bakedCandidates; - this.uploadBytes = uploadBytes; - this.fullFrame = fullFrame; - } - } - - /** One draw selected for on-glasses replay. */ - private static final class Selected { - final SurfaceCompositor.ScreenDraw draw; - final GlyphAtlas.Glyph glyphAtlas; // glyph draws - final ImageAtlas.Entry imageAtlas; // image draws - final int gx; // screen x of the cached image (pen + bearing / blit left) - final int top; // glyph draws: 4bpp top color - - Selected(SurfaceCompositor.ScreenDraw draw, GlyphAtlas.Glyph glyphAtlas, - ImageAtlas.Entry imageAtlas, int gx, int top) { - this.draw = draw; - this.glyphAtlas = glyphAtlas; - this.imageAtlas = imageAtlas; - this.gx = gx; - this.top = top; - } - } - - /** - * Plan a cached-draw update. previous is the delta base (the frame the - * shadow currently holds), or null/mismatched for a full-frame keyframe. - * Returns null when the plain paths should run instead (no replayable - * draws, or identical frames). - */ - public static Result plan( - byte[] previous, byte[] next, int width, int height, - SurfaceCompositor.ScreenDraw[] draws, - TextureCacheState cache, int fidStart, - boolean allowMultiRect, int maxRects) { - if (next == null || draws == null || draws.length == 0 || width <= 0 || height <= 0) { - return null; - } - int stride = (width + 1) >> 1; - if (next.length != stride * height) { - return null; - } - long planStartedAtMs = android.os.SystemClock.elapsedRealtime(); - - // Rect geometry, matching what the plain paths would send. - boolean fullFrame = previous == null || previous.length != next.length; - List rects; - if (!fullFrame) { - int[] box = BleImageOptimizer.computeChangedBox(previous, next, width, height); - if (box == null) { - return null; // identical frames; caller's dedup normally catches this - } - if (box[2] >= width && box[3] >= height) { - fullFrame = true; - rects = Collections.singletonList(new int[]{0, 0, width, height}); - } else { - List split = allowMultiRect - ? BleImageOptimizer.computeChangedRects(previous, next, width, height, maxRects) - : null; - rects = split != null ? split : Collections.singletonList(box); - } - } else { - rects = Collections.singletonList(new int[]{0, 0, width, height}); - } - - long matchStartedAtMs = android.os.SystemClock.elapsedRealtime(); - - // Candidates: draws whose written pixels intersect a sent rect, are - // representable on the wire, and land correct against the composite. - List selected = new ArrayList<>(); - int bakedCandidates = 0; - int selectedImages = 0; - // Builtin-font (mode 15) runs: no cache residency, no uploads — the - // firmware owns the font. Processed first because it has no cache - // side effects; the counters feed the shared stats. - List fwSubs = new ArrayList<>(); - List fwPunches = new ArrayList<>(); - int fwGlyphCount = 0; - int fwBaked = 0; - for (SurfaceCompositor.ScreenDraw draw : draws) { - if (draw.kind != SurfaceCompositor.ScreenDraw.KIND_FWTEXT) continue; - fwBaked += planFwRun(draw, rects, next, stride, width, height, fwSubs, fwPunches); - } - fwGlyphCount = fwPunches.size(); - bakedCandidates += fwBaked; - for (SurfaceCompositor.ScreenDraw draw : draws) { - if (draw.kind == SurfaceCompositor.ScreenDraw.KIND_GLYPH) { - GlyphAtlas.Glyph atlas = GlyphAtlas.get(draw.fontId, draw.encoding); - if (atlas == null) continue; // unregistered: stays baked, not a "candidate" - int gx = draw.x + atlas.bbxX; - int inkTop = draw.y + atlas.inkTop; - if (!intersectsAny(rects, gx, inkTop, atlas.width, atlas.inkHeight)) { - continue; - } - if (draw.encoding < 32 || draw.encoding > 127 - || gx < 0 || draw.y < 0 || gx > 0xffff || draw.y > 0xffff) { - bakedCandidates++; - continue; - } - int top = BmpUtil.nibbleForGray(draw.value); - if (!glyphMatchesComposite(next, stride, width, height, atlas, gx, draw.y, top)) { - bakedCandidates++; - continue; - } - if (selected.size() >= MAX_SELECTED_GLYPHS) { - bakedCandidates++; - continue; - } - selected.add(new Selected(draw, atlas, null, gx, top)); - } else { - ImageAtlas.Entry atlas = ImageAtlas.get(draw.imageId); - if (atlas == null) continue; - if (!intersectsAny(rects, draw.x, draw.y, atlas.width, atlas.height)) { - continue; - } - if (draw.x < 0 || draw.y < 0 || draw.x > 0xffff || draw.y > 0xffff - || selectedImages >= MAX_IMAGE_DRAWS) { - bakedCandidates++; - continue; - } - if (!imageMatchesComposite(next, stride, width, height, atlas, draw.x, draw.y)) { - bakedCandidates++; - continue; - } - selected.add(new Selected(draw, null, atlas, draw.x, 0)); - selectedImages++; - } - } - if (selected.isEmpty() && fwSubs.isEmpty()) { - return null; - } - long cacheStartedAtMs = android.os.SystemClock.elapsedRealtime(); - - // Residency: ensure every selected draw is in the cache, retrying once - // after a reset when the bump allocator fills. A draw that still fails - // falls back to baked. IMPORTANT: once ensure* has run, every bail-out - // to the plain path must cache.reset() — ensure* marked entries - // resident and queued uploads that only a returned Result would - // enqueue. (Fw-text needs no residency, so a fw-only plan skips this.) - List drawable = selected.isEmpty() - ? Collections.emptyList() - : ensureResident(cache, selected); - bakedCandidates += selected.size() - drawable.size(); - - // Group glyphs into mode-20 runs; leftover-budget glyphs bake. - List glyphDrawable = new ArrayList<>(); - List imageDrawable = new ArrayList<>(); - for (Selected sel : drawable) { - if (sel.glyphAtlas != null) glyphDrawable.add(sel); - else imageDrawable.add(sel); - } - List runs = new ArrayList<>(); - List drawnGlyphs = buildRuns(cache, glyphDrawable, runs); - bakedCandidates += glyphDrawable.size() - drawnGlyphs.size(); - List drawn = new ArrayList<>(drawnGlyphs); - drawn.addAll(imageDrawable); - if ((drawn.isEmpty() && fwSubs.isEmpty()) - || rects.size() + runs.size() + imageDrawable.size() + fwSubs.size() > 255) { - if (!selected.isEmpty()) { - cache.reset(); - } - return null; - } - - // Punch the drawn pixels out of a copy of the composite, then encode - // the rect deltas from the punched content. - long punchStartedAtMs = android.os.SystemClock.elapsedRealtime(); - byte[] punched = next.clone(); - for (Selected sel : drawn) { - punch(punched, stride, width, height, sel); - } - for (FwPunch fw : fwPunches) { - punchFw(punched, stride, width, height, fw); - } - - long encodeStartedAtMs = android.os.SystemClock.elapsedRealtime(); - List subs = new ArrayList<>(); - int fid = fidStart; - if (fullFrame) { - subs.add(BleImageOptimizer.maybeCompress(punched, width, height)); - } else { - for (int[] r : rects) { - subs.add(BleImageOptimizer.encodeMode3Rect(punched, stride, r[0], r[1], r[2], r[3], fid)); - fid = (fid >= 0xfffe) ? 1 : fid + 1; - } - } - for (Selected sel : imageDrawable) { - subs.add(encodeImageDraw(cache, sel)); - } - subs.addAll(runs); - subs.addAll(fwSubs); - byte[] payload = assembleMode8(subs); - - int uploadBytes = cache.pendingUploadBytes(); - List uploads = cache.drainUploadPayloads(UPLOAD_PAYLOAD_MAX); - Result result = new Result(payload, uploads, fid, rects.size(), drawnGlyphs.size(), runs.size(), - imageDrawable.size(), fwGlyphCount, fwSubs.size(), - bakedCandidates, uploadBytes, fullFrame); - long doneAtMs = android.os.SystemClock.elapsedRealtime(); - result.rectsMs = (int) (matchStartedAtMs - planStartedAtMs); - result.matchMs = (int) (cacheStartedAtMs - matchStartedAtMs); - result.cacheMs = (int) (punchStartedAtMs - cacheStartedAtMs); - result.punchMs = (int) (encodeStartedAtMs - punchStartedAtMs); - result.encodeMs = (int) (doneAtMs - encodeStartedAtMs); - return result; - } - - /** One punch task for an emitted firmware-font glyph. */ - private static final class FwPunch { - final FwGlyphAtlas.Entry entry; - final int gx; - final int gy; - - FwPunch(FwGlyphAtlas.Entry entry, int gx, int gy) { - this.entry = entry; - this.gx = gx; - this.gy = gy; - } - } - - /** - * Plan one firmware-font run (mode 15): classify every member, and when - * any correct ink member intersects a sent rect, emit sub-messages over - * the maximal correct stretches. A stretch never spans an incorrect - * member — the sub-message boundary restarts the firmware's advance walk - * at an explicit pen x, so occluded or unknown glyphs simply stay baked - * without disturbing their neighbors' positions. Emitted ink members are - * queued for punching. Returns how many ink members stay baked. - */ - private static int planFwRun( - SurfaceCompositor.ScreenDraw draw, List rects, - byte[] next, int stride, int width, int height, - List outSubs, List outPunches) { - int n = draw.fwCps.length; - if (n == 0 || draw.y < 0 || draw.y > 0xffff) { - return countInk(draw); - } - int top = BmpUtil.nibbleForGray(draw.value); - boolean[] ok = new boolean[n]; - FwGlyphAtlas.Entry[] entries = new FwGlyphAtlas.Entry[n]; - boolean relevant = false; - int baked = 0; - for (int i = 0; i < n; i++) { - if (!draw.fwInk[i]) { - ok[i] = true; // inkless connector (space): draws nothing - continue; - } - FwGlyphAtlas.Entry entry = FwGlyphAtlas.get(draw.fwCps[i]); - if (entry == null) { - baked++; - continue; - } - int gx = draw.x + draw.fwDx[i] + entry.ofsX; - int gy = draw.y + entry.inkTop; - if (!fwGlyphMatchesComposite(next, stride, width, height, entry, gx, gy, top)) { - baked++; - continue; - } - ok[i] = true; - entries[i] = entry; - if (intersectsAny(rects, gx, gy, entry.boxW, entry.boxH)) { - relevant = true; - } - } - if (!relevant) { - return 0; // nothing in a sent rect: leave the run's pixels alone - } - - int i = 0; - while (i < n && outSubs.size() < MAX_FWTEXT_RUNS) { - while (i < n && !(ok[i] && draw.fwInk[i])) i++; - if (i >= n) break; - int startX = draw.x + draw.fwDx[i]; - if (startX < 0 || startX > 0xffff) { - baked++; - i++; - continue; - } - // Extend across correct members while the UTF-8 payload fits, - // tracking the last ink member so trailing connectors trim away. - int j = i; - int lastInk = -1; - int bytesToLastInk = 0; - int bytesSoFar = 0; - while (j < n && ok[j]) { - int encodedLength = utf8Length(draw.fwCps[j]); - if (bytesSoFar + encodedLength > 255) break; - bytesSoFar += encodedLength; - if (draw.fwInk[j]) { - lastInk = j; - bytesToLastInk = bytesSoFar; - } - j++; - } - if (lastInk < i) { // a single over-long codepoint; not realistic - baked++; - i++; - continue; - } - byte[] sub = new byte[7 + bytesToLastInk]; - sub[0] = 15; - sub[1] = (byte) (startX & 0xff); - sub[2] = (byte) ((startX >> 8) & 0xff); - sub[3] = (byte) (draw.y & 0xff); - sub[4] = (byte) ((draw.y >> 8) & 0xff); - sub[5] = (byte) (top | 0x10); // top color + transparent - sub[6] = (byte) bytesToLastInk; - int pos = 7; - for (int k = i; k <= lastInk; k++) { - byte[] encoded = new StringBuilder().appendCodePoint(draw.fwCps[k]) - .toString().getBytes(java.nio.charset.StandardCharsets.UTF_8); - System.arraycopy(encoded, 0, sub, pos, encoded.length); - pos += encoded.length; - } - outSubs.add(sub); - for (int k = i; k <= lastInk; k++) { - if (draw.fwInk[k]) { - outPunches.add(new FwPunch(entries[k], - draw.x + draw.fwDx[k] + entries[k].ofsX, - draw.y + entries[k].inkTop)); - } - } - i = lastInk + 1; - } - return baked; - } - - private static int utf8Length(int cp) { - if (cp < 0x80) return 1; - if (cp < 0x800) return 2; - if (cp < 0x10000) return 3; - return 4; - } - - private static int countInk(SurfaceCompositor.ScreenDraw draw) { - int count = 0; - for (boolean ink : draw.fwInk) { - if (ink) count++; - } - return count; - } - - /** - * Whether every in-panel nonzero pixel of the builtin-font glyph lands - * correct: the composite's 4bpp value must equal the firmware's LUT - * output src*top/15 (integer division — a general LUT check, since these - * glyphs are anti-aliased). - */ - private static boolean fwGlyphMatchesComposite(byte[] packed, int stride, int width, int height, - FwGlyphAtlas.Entry entry, int gx, int gy, int top) { - for (int row = 0; row < entry.boxH; row++) { - int y = gy + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < entry.boxW; col++) { - int source = entry.nibbleAt(col, row); - if (source == 0) continue; - int x = gx + col; - if (x < 0 || x >= width) continue; - if (nibbleAt(packed, stride, x, y) != (source * top) / 15) { - return false; - } - } - } - return true; - } - - private static void punchFw(byte[] packed, int stride, int width, int height, FwPunch fw) { - for (int row = 0; row < fw.entry.boxH; row++) { - int y = fw.gy + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < fw.entry.boxW; col++) { - if (fw.entry.nibbleAt(col, row) == 0) continue; - punchPixel(packed, stride, width, fw.gx + col, y); - } - } - } - - private static boolean intersectsAny(List rects, int x, int y, int w, int h) { - if (w <= 0 || h <= 0) return false; - for (int[] r : rects) { - if (x < r[0] + r[2] && x + w > r[0] && y < r[1] + r[3] && y + h > r[1]) { - return true; - } - } - return false; - } - - /** - * Whether every in-panel ink pixel of the glyph lands correct in the - * packed new frame: the composite's 4bpp value must equal the firmware's - * LUT output source*top/15 (integer). For 1bpp glyphs (source 15) this is - * exactly `== top`; AA glyphs get the general check. Ink outside the - * panel is clipped by the firmware and irrelevant. - */ - private static boolean glyphMatchesComposite(byte[] packed, int stride, int width, int height, - GlyphAtlas.Glyph atlas, int gx, int lineY, int top) { - for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) { - int y = lineY + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - int source = atlas.nibbleAt(col, row); - if (source == 0) continue; - int x = gx + col; - if (x < 0 || x >= width) continue; - if (nibbleAt(packed, stride, x, y) != (source * top) / 15) { - return false; - } - } - } - return true; - } - - /** - * Whether every in-panel nonzero pixel of the cached image equals the - * packed new frame — the mode-19 transparent draw writes exactly those. - */ - private static boolean imageMatchesComposite(byte[] packed, int stride, int width, int height, - ImageAtlas.Entry atlas, int left, int top) { - for (int row = 0; row < atlas.height; row++) { - int y = top + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - int source = atlas.nibbleAt(col, row); - if (source == 0) continue; - int x = left + col; - if (x < 0 || x >= width) continue; - if (nibbleAt(packed, stride, x, y) != source) { - return false; - } - } - } - return true; - } - - private static int nibbleAt(byte[] packed, int stride, int x, int y) { - int b = packed[y * stride + (x >> 1)] & 0xff; - return (x & 1) != 0 ? (b & 0x0f) : (b >> 4); - } - - private static void punch(byte[] packed, int stride, int width, int height, Selected sel) { - if (sel.glyphAtlas != null) { - GlyphAtlas.Glyph atlas = sel.glyphAtlas; - for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) { - int y = sel.draw.y + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - if (!atlas.inkAt(col, row)) continue; - punchPixel(packed, stride, width, sel.gx + col, y); - } - } - } else { - ImageAtlas.Entry atlas = sel.imageAtlas; - for (int row = 0; row < atlas.height; row++) { - int y = sel.draw.y + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - if (atlas.nibbleAt(col, row) == 0) continue; - punchPixel(packed, stride, width, sel.gx + col, y); - } - } - } - } - - private static void punchPixel(byte[] packed, int stride, int width, int x, int y) { - if (x < 0 || x >= width) return; - int index = y * stride + (x >> 1); - if ((x & 1) != 0) { - packed[index] = (byte) (packed[index] & 0xf0); - } else { - packed[index] = (byte) (packed[index] & 0x0f); - } - } - - /** - * Ensure residency for all of `selected`, resetting the cache once if the - * allocator fills mid-plan (a reset invalidates offsets handed out earlier - * in the same pass, so the whole pass reruns). Returns the draws that are - * resident afterwards. - */ - private static List ensureResident(TextureCacheState cache, List selected) { - for (int attempt = 0; attempt < 2; attempt++) { - List resident = new ArrayList<>(selected.size()); - boolean full = false; - for (Selected sel : selected) { - int offset = sel.glyphAtlas != null - ? cache.ensureGlyph(sel.draw.fontId, sel.draw.encoding, sel.glyphAtlas) - : cache.ensureImage(sel.draw.imageId, sel.imageAtlas); - if (offset >= 0) { - resident.add(sel); - } else { - full = true; - } - } - if (!full || attempt == 1) { - return resident; - } - cache.reset(); - } - return Collections.emptyList(); // unreachable - } - - /** Mode-19 cached-image draw: [19][offset u32][x u16][y u16][options u8]. */ - private static byte[] encodeImageDraw(TextureCacheState cache, Selected sel) { - int offset = cache.ensureImage(sel.draw.imageId, sel.imageAtlas); // resident: returns the offset - byte[] sub = new byte[10]; - sub[0] = 19; - sub[1] = (byte) (offset & 0xff); - sub[2] = (byte) ((offset >> 8) & 0xff); - sub[3] = (byte) ((offset >> 16) & 0xff); - sub[4] = (byte) ((offset >> 24) & 0xff); - sub[5] = (byte) (sel.draw.x & 0xff); - sub[6] = (byte) ((sel.draw.x >> 8) & 0xff); - sub[7] = (byte) (sel.draw.y & 0xff); - sub[8] = (byte) ((sel.draw.y >> 8) & 0xff); - sub[9] = (byte) IMAGE_DRAW_OPTIONS; - return sub; - } - - /** - * Group selected glyphs into mode-20 string sub-messages: - * [20][fontTable u32][x u16][y u16][options u8][strlen u8][string] - * One run per (font, line y, top color) span; within a run, control bytes - * 1..31 adjust x by -10..+20 to hit each glyph's exact position, and each - * glyph advances x by its cached width. Order across runs is free: every - * drawn glyph's ink equals the composite, so overlaps write equal values. - * Returns the glyphs actually emitted (run budget can drop stragglers). - */ - private static List buildRuns(TextureCacheState cache, List drawable, List outRuns) { - List sorted = new ArrayList<>(drawable); - sorted.sort(Comparator - .comparingInt((Selected s) -> s.draw.fontId) - .thenComparingInt(s -> s.draw.y) - .thenComparingInt(s -> s.top) - .thenComparingInt(s -> s.gx)); - - List drawn = new ArrayList<>(sorted.size()); - int i = 0; - while (i < sorted.size() && outRuns.size() < MAX_GLYPH_RUNS) { - Selected first = sorted.get(i); - int fontTable = cache.fontTableOffset(first.draw.fontId); - if (fontTable < 0) { // shouldn't happen: residency ensured above - i++; - continue; - } - java.io.ByteArrayOutputStream string = new java.io.ByteArrayOutputStream(); - List runGlyphs = new ArrayList<>(); - int cursor = first.gx; // the run's wire x starts exactly at the first glyph - int j = i; - while (j < sorted.size()) { - Selected sel = sorted.get(j); - if (sel.draw.fontId != first.draw.fontId - || sel.draw.y != first.draw.y - || sel.top != first.top) { - break; - } - int delta = sel.gx - cursor; - int adjustBytes = adjustByteCount(delta); - if (adjustBytes > MAX_ADJUST_BYTES && j > i) { - break; // far gap: cheaper to start a fresh run - } - if (string.size() + adjustBytes + 1 > 255) { - break; // strlen is a u8 - } - emitAdjust(string, delta); - string.write(sel.draw.encoding); - runGlyphs.add(sel); - cursor = sel.gx + sel.glyphAtlas.width; - j++; - } - if (!runGlyphs.isEmpty()) { - byte[] stringBytes = string.toByteArray(); - byte[] run = new byte[11 + stringBytes.length]; - run[0] = 20; - run[1] = (byte) (fontTable & 0xff); - run[2] = (byte) ((fontTable >> 8) & 0xff); - run[3] = (byte) ((fontTable >> 16) & 0xff); - run[4] = (byte) ((fontTable >> 24) & 0xff); - run[5] = (byte) (first.gx & 0xff); - run[6] = (byte) ((first.gx >> 8) & 0xff); - run[7] = (byte) (first.draw.y & 0xff); - run[8] = (byte) ((first.draw.y >> 8) & 0xff); - run[9] = (byte) (first.top | 0x10); // top color + transparent - run[10] = (byte) stringBytes.length; - System.arraycopy(stringBytes, 0, run, 11, stringBytes.length); - outRuns.add(run); - drawn.addAll(runGlyphs); - i = j; - } else { - i++; - } - } - return drawn; - } - - /** How many 1..31 control bytes are needed to move the cursor by delta. */ - private static int adjustByteCount(int delta) { - if (delta == 0) return 0; - return delta > 0 ? (delta + 19) / 20 : (-delta + 9) / 10; - } - - private static void emitAdjust(java.io.ByteArrayOutputStream out, int delta) { - while (delta != 0) { - int step = delta > 0 ? Math.min(delta, 20) : Math.max(delta, -10); - out.write(step + 11); // byte 1..31 adjusts x by (byte - 11) - delta -= step; - } - } - - /** [8][count]([len u16][sub])* — one atomic present for deltas + draws. */ - private static byte[] assembleMode8(List subs) { - int total = 2; - for (byte[] sub : subs) { - total += 2 + sub.length; - } - byte[] out = new byte[total]; - out[0] = 8; - out[1] = (byte) subs.size(); - int pos = 2; - for (byte[] sub : subs) { - out[pos] = (byte) (sub.length & 0xff); - out[pos + 1] = (byte) ((sub.length >> 8) & 0xff); - pos += 2; - System.arraycopy(sub, 0, out, pos, sub.length); - pos += sub.length; - } - return out; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java deleted file mode 100644 index 23898347..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.faceclaw.app; -import java.util.Arrays; - -public class BmpUtil { - public static byte[] copyTileBmp(byte[] bmp) { - if (bmp == null || bmp.length == 0) { - return new byte[0]; - } - return Arrays.copyOf(bmp, bmp.length); - } - - // Quantize an 8-bit gray value to a 4-bit nibble. Truncate rather than round - // up at the bottom: the shell's color-key convention paints intentional - // black as 1 (0 being transparent), which must land on nibble 0, not a - // faintly-visible level 1. - private static final byte[] GRAY_TO_NIBBLE = new byte[256]; - static { - for (int v = 1; v < 256; v++) { - GRAY_TO_NIBBLE[v] = (byte) Math.min(15, (v + 8) >> 4); - } - } - - /** - * The 4bpp level an 8-bit gray value packs to. Public so the texture-cache - * planner computes a glyph draw's top color with the exact quantization - * the composited frame was packed with. - */ - public static int nibbleForGray(int gray) { - return GRAY_TO_NIBBLE[gray & 0xff] & 0xff; - } - - /** - * Pack a full-resolution 8bpp grayscale buffer (row-major, top-to-bottom, - * one byte per pixel) into the headerless 4bpp wire format used by CFW - * load_image_z modes 3/6/8: top-down rows, stride ceil(width/2), high - * nibble = left pixel. This is the canonical in-memory frame format; BMP - * framing (needed only when an uncompressed full frame goes on the wire) - * is added on demand by build4bppBmpFromPacked. - */ - public static byte[] pack4bppFromGray8(byte[] gray8, int width, int height) { - if (width <= 0 || height <= 0) { - return new byte[0]; - } - int stride = (width + 1) >> 1; - byte[] out = new byte[stride * height]; - if (gray8 == null || gray8.length < width * height) { - return out; // short buffer: treat missing pixels as black - } - int pairs = width >> 1; - int src = 0; - int dst = 0; - for (int y = 0; y < height; y++) { - for (int i = 0; i < pairs; i++) { - out[dst++] = (byte) ((GRAY_TO_NIBBLE[gray8[src] & 0xff] << 4) - | GRAY_TO_NIBBLE[gray8[src + 1] & 0xff]); - src += 2; - } - if ((width & 1) != 0) { - out[dst++] = (byte) (GRAY_TO_NIBBLE[gray8[src++] & 0xff] << 4); - } - } - return out; - } - - /** - * Wrap a packed 4bpp frame in BMP framing (BITMAPINFOHEADER + 16-entry gray - * palette, bottom-up rows padded to a 4-byte stride). Only the full-frame - * fallback used when mode-6 compression does not shrink the frame still - * speaks BMP on the wire; everything else uses the headerless packed format - * directly. - */ - public static byte[] build4bppBmpFromPacked(byte[] packed, int width, int height) { - int packedStride = (width + 1) >> 1; - if (width <= 0 || height <= 0 - || packed == null || packed.length < packedStride * height) { - return new byte[0]; - } - int rowStride = (packedStride + 3) & ~3; - int pixelDataSize = rowStride * height; - - int fileHeaderSize = 14; - int dibHeaderSize = 40; - int paletteSize = 16 * 4; - int pixelOffset = fileHeaderSize + dibHeaderSize + paletteSize; - int fileSize = pixelOffset + pixelDataSize; - - byte[] buf = new byte[fileSize]; - buf[0] = 0x42; // 'B' - buf[1] = 0x4d; // 'M' - putUint32Le(buf, 2, fileSize); - putUint32Le(buf, 10, pixelOffset); - putUint32Le(buf, 14, dibHeaderSize); - putInt32Le(buf, 18, width); - putInt32Le(buf, 22, height); - putUint16Le(buf, 26, 1); // planes - putUint16Le(buf, 28, 4); // bits per pixel - putUint32Le(buf, 30, 0); // compression (BI_RGB) - putUint32Le(buf, 34, pixelDataSize); - putUint32Le(buf, 46, 16); // palette entry count - - for (int i = 0; i < 16; i++) { - int v = i * 17; - int base = fileHeaderSize + dibHeaderSize + i * 4; - buf[base] = (byte) v; - buf[base + 1] = (byte) v; - buf[base + 2] = (byte) v; - buf[base + 3] = 0; - } - - for (int bmpRow = 0; bmpRow < height; bmpRow++) { - int srcY = height - 1 - bmpRow; // BMP rows run bottom-up - System.arraycopy(packed, srcY * packedStride, - buf, pixelOffset + bmpRow * rowStride, packedStride); - } - return buf; - } - - private static void putUint16Le(byte[] buf, int offset, int value) { - buf[offset] = (byte) (value & 0xff); - buf[offset + 1] = (byte) ((value >>> 8) & 0xff); - } - - private static void putUint32Le(byte[] buf, int offset, int value) { - buf[offset] = (byte) (value & 0xff); - buf[offset + 1] = (byte) ((value >>> 8) & 0xff); - buf[offset + 2] = (byte) ((value >>> 16) & 0xff); - buf[offset + 3] = (byte) ((value >>> 24) & 0xff); - } - - private static void putInt32Le(byte[] buf, int offset, int value) { - putUint32Le(buf, offset, value); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java deleted file mode 100644 index c99e29ea..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.faceclaw.app; -import java.util.ArrayList; -import java.util.List; - -public class CollectionUtils { - public static List singletonList(byte[] value) { - List list = new ArrayList<>(1); - list.add(value); - return list; - } - - public static List listOf(byte[] a, byte[] b) { - List list = new ArrayList<>(2); - list.add(a); - list.add(b); - return list; - } - - public static List listOf(byte[] a, byte[] b, byte[] c) { - List list = new ArrayList<>(3); - list.add(a); - list.add(b); - list.add(c); - return list; - } -} diff --git a/app/g2/ble-image-optimizer.ios.ts b/app/g2/ble-image-optimizer.ios.ts new file mode 100644 index 00000000..989ca1c2 --- /dev/null +++ b/app/g2/ble-image-optimizer.ios.ts @@ -0,0 +1,15 @@ +import { fromData, toData, nativeArray } from '../native/kotlin-data' +declare const FaceclawKitIosProtocol: any +const protocol = FaceclawKitIosProtocol.new() +export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, width: number, height: number, frameId: number): Uint8Array | null { + if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 || + width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 || + !previous || previous.length !== width * height / 2 || next.length !== previous.length) return null + const result = protocol.boundingBoxPreviousNextWidthHeightFrameId(toData(previous), toData(next), width, height, frameId) + return result ? fromData(result) : null +} +export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] { + if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2) + throw new Error('Invalid framebuffer dimensions') + return nativeArray(protocol.fullFrameBandsDataWidthHeightFirstId(toData(packed), width, height, firstId), fromData) +} diff --git a/app/g2/ble-protocol.ios.ts b/app/g2/ble-protocol.ios.ts new file mode 100644 index 00000000..e22945dc --- /dev/null +++ b/app/g2/ble-protocol.ios.ts @@ -0,0 +1,66 @@ +/** iOS binary bridge to the same Kotlin protocol used by Android. */ +import { fromData, toData, nativeArray } from '../native/kotlin-data' +import { OsEventTypeList } from './events' +declare const FaceclawKitIosProtocol: any, FaceclawKitIosMessageReceiver: any +const protocol = FaceclawKitIosProtocol.new() +export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401' +export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402' +export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402' +export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f'] +export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const +export function concat(...parts: Uint8Array[]): Uint8Array { + const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)) + let offset = 0 + for (const part of parts) { output.set(part, offset); offset += part.length } + return output +} +export const integer = (field: number, value: number): Uint8Array => fromData(protocol.integerFieldValue(field, value)) +export const bytes = (field: number, value: Uint8Array): Uint8Array => fromData(protocol.bytesFieldData(field, toData(value))) +export const authentication = (magic: number) => fromData(protocol.authenticationMagic(magic)) +export const prelude = () => fromData(protocol.prelude()) +export const heartbeat = (magic: number) => fromData(protocol.heartbeatMagic(magic)) +export const audioControl = (magic: number, enabled: boolean) => fromData(protocol.audioControlMagicEnabled(magic, enabled)) +export const settingsQuery = (magic: number) => fromData(protocol.settingsQueryMagic(magic)) +export const shutdown = (magic: number) => fromData(protocol.shutdownMagic(magic)) +export const framebufferLease = (acquire: boolean) => fromData(protocol.framebufferLeaseAcquire(acquire)) +export const createLayout = (magic: number) => fromData(protocol.createLayoutMagic(magic)) +export const crc16 = (data: Uint8Array): number => protocol.crcData(toData(data)) +export function frameMessage(payload: Uint8Array, sid: number, flag: number, sequence: number, maxWrite = 240): Uint8Array[] { + const capacity = Math.min(232, Math.floor(maxWrite) - 8) + if (capacity < 12 || !Number.isFinite(capacity)) throw new Error('Negotiated BLE write size is too small') + if (Math.ceil((payload.length + 2) / capacity) > 255) throw new Error('Protocol message exceeds the fragment limit') + return nativeArray(protocol.frameDataSidFlagSequenceMaxWrite(toData(payload), sid, flag, sequence, Math.floor(maxWrite)), fromData) +} +export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array } +export class MessageReceiver { + private native = FaceclawKitIosMessageReceiver.new() + clear(): void { this.native.clear() } + receive(link: string, data: Uint8Array, now = Date.now()): ProtocolMessage[] { + return nativeArray(this.native.receiveLinkDataNow(link, toData(data), now), (message: any) => ({ + sid: message.sid, flag: message.flag, payload: fromData(message.payload), command: message.command, + magic: message.magic, ...(message.packet ? { packet: fromData(message.packet) } : {}), + })) + } +} +export const readInteger = (data: Uint8Array, field: number, fallback = 0): number => protocol.readIntegerDataFieldFallback(toData(data), field, fallback) +export const readBytes = (data: Uint8Array, field: number): Uint8Array | undefined => { + const value = protocol.readBytesDataField(toData(data), field) + return value ? fromData(value) : undefined +} +export const readString = (data: Uint8Array, field: number): string => protocol.readStringDataField(toData(data), field) +export function authenticationSucceeded(message: ProtocolMessage, magic: number): boolean { + return message.sid === SID.auth && message.command === 4 && message.magic === magic && readBytes(message.payload, 3)?.length === 0 +} +export type GlassesInput = { kind: 'sys-event' | 'list-click' | 'text-click' | 'display-wake'; eventType: number; eventSource: number; systemExitReasonCode: number; containerName: string; frameId: number } +function input(event: any): GlassesInput | null { + return event ? { kind: event.kind === 'sys-event' && event.eventType === OsEventTypeList.HEAD_UP_EVENT ? 'display-wake' : event.kind, + eventType: event.eventType, eventSource: event.eventSource, systemExitReasonCode: event.systemExitReasonCode, + containerName: event.containerName, frameId: 0 } : null +} +export const decodeGlassesInput = (message: ProtocolMessage): GlassesInput | null => input(protocol.glassesInputDataSidFlag(toData(message.payload), message.sid, message.flag)) +export const decodeRingInput = (data: Uint8Array): GlassesInput | null => input(protocol.ringInputData(toData(data))) +export function packGray4(gray: Uint8Array, width: number, height: number): Uint8Array { + if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || gray.length !== width * height) throw new Error('Invalid display frame') + return fromData(protocol.packDataWidthHeight(toData(gray), width, height)) +} +export const rle4 = (packed: Uint8Array): Uint8Array => fromData(protocol.rleData(toData(packed))) diff --git a/app/g2/cfw-transport.ios.ts b/app/g2/cfw-transport.ios.ts new file mode 100644 index 00000000..135f31d1 --- /dev/null +++ b/app/g2/cfw-transport.ios.ts @@ -0,0 +1,21 @@ +import { fromData, toData, nativeArray } from '../native/kotlin-data' +declare const FaceclawKitIosCfwTransport: any, FaceclawKitIosProtocol: any +export const CFW_MAX_MESSAGE = 65535 +export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number } +/** maxWrite is the ATT value limit; Kotlin owns the persistent zlib stream. */ +export class CfwTransport { + private native = FaceclawKitIosCfwTransport.new() + reset(): void { this.native.reset() } + encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] { + if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 || + ![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514) + throw new Error('Invalid CFW stream parameters') + return nativeArray(this.native.encodeDataStreamIdLensesMaxWrite(toData(message), streamId, lenses, maxWrite), fromData) + } +} +const protocol = FaceclawKitIosProtocol.new() +export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null { + const acks = protocol.acksData(toData(packet)) + return acks ? nativeArray(acks, (ack: any) => ({ nack: ack.nack, streamId: ack.streamId, messageId: ack.messageId, + lens: ack.lens, size: ack.size, checksum: ack.checksum })) : null +} diff --git a/app/graphics/glyph-wire.ts b/app/graphics/glyph-wire.ts index 3842c499..adbf533f 100644 --- a/app/graphics/glyph-wire.ts +++ b/app/graphics/glyph-wire.ts @@ -116,7 +116,7 @@ function prepareFwTextRun(placed: PlacedFwText): boolean { bytes.set(entry.data.nibbles, offset + 10); offset += 10 + entry.data.nibbles.length; } - com.faceclaw.app.FwGlyphAtlas.register(buffer); + com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer)); } return true; } @@ -130,7 +130,7 @@ function imageId(placed: PlacedImage): number | null { let id: number | null = null; if (source.width > 0 && source.width <= 255 && source.height > 0 && source.height <= 255) { const key = `img:${source.width}x${source.height}:${source.contentHash32().toString(16)}`; - id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, source.pixels.buffer); + id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, new com.faceclaw.app.AndroidByteReader(source.pixels.buffer)); if (!(typeof id === "number") || id <= 0) id = null; } imageIds.set(source, id); @@ -184,9 +184,9 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | } if (registration) { const plainBuffer = buildRegistrationBuffer(registration); - if (plainBuffer) com.faceclaw.app.GlyphAtlas.register(plainBuffer); + if (plainBuffer) com.faceclaw.app.GlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(plainBuffer)); const aaBuffer = buildAaRegistrationBuffer(registration); - if (aaBuffer) com.faceclaw.app.GlyphAtlas.registerAa(aaBuffer); + if (aaBuffer) com.faceclaw.app.GlyphAtlas.registerAa(new com.faceclaw.app.AndroidByteReader(aaBuffer)); } if (bytes === 0) return null; diff --git a/app/graphics/surface-compositor.ios.ts b/app/graphics/surface-compositor.ios.ts new file mode 100644 index 00000000..8cb04f50 --- /dev/null +++ b/app/graphics/surface-compositor.ios.ts @@ -0,0 +1,30 @@ +import { fromData, toData } from '../native/kotlin-data' +import type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' +export type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' +declare const FaceclawKitIosSurfaceCompositor: any +/** Retained pixels, clipping, layers and composition live in shared Kotlin. */ +export class SurfaceCompositor { + private native: any + constructor(readonly width: number, readonly height: number) { + if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) throw new Error('Invalid compositor size') + this.native = FaceclawKitIosSurfaceCompositor.alloc().initWithWidthHeight(width, height) + } + configureSurface(id: string, options: SurfaceConfiguration): void { + if (![options.x, options.y, options.width, options.height, options.zOrder].every(Number.isInteger) + || options.width <= 0 || options.height <= 0) throw new Error('Invalid surface geometry') + this.native.configureIdXYWidthHeightZOrderTransparent(id, options.x, options.y, options.width, options.height, options.zOrder, options.transparency === 'color-key') + } + removeSurface(id: string): void { this.native.removeId(id) } + setSurfaceVisible(id: string, visible: boolean): void { this.native.visibleIdVisible(id, visible) } + setUnderlayDim(belowZOrder: number, factor: number): void { + if (!Number.isFinite(factor)) throw new Error('Invalid dim factor') + this.native.dimBelowFactor(belowZOrder, factor) + } + setScreenBlanked(blanked: boolean): void { this.native.blankBlanked(blanked) } + submitSurfaceFrame(id: string, pixels: Uint8Array, rect: SurfaceRect): void { + if (![rect.x, rect.y, rect.width, rect.height].every(Number.isInteger) + || rect.width <= 0 || rect.height <= 0 || pixels.length !== rect.width * rect.height) throw new Error('Invalid frame buffer or rectangle') + this.native.submitIdDataXYWidthHeight(id, toData(pixels), rect.x, rect.y, rect.width, rect.height) + } + composite(): Uint8Array { return fromData(this.native.composite()) } +} diff --git a/app/native/kotlin-data.ts b/app/native/kotlin-data.ts new file mode 100644 index 00000000..a3729fb6 --- /dev/null +++ b/app/native/kotlin-data.ts @@ -0,0 +1,12 @@ +/** Bulk binary bridge; slices are normalized without per-byte native calls. */ +export function toData(bytes: Uint8Array): NSData { + const buffer = bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength + ? bytes.buffer : bytes.slice().buffer + return NSData.dataWithBytesLength(interop.handleof(buffer), bytes.byteLength) +} +export function fromData(data: NSData): Uint8Array { + return new Uint8Array(interop.bufferFromData(data)).slice() +} +export function nativeArray(items: NSArray, convert: (item: T) => R): R[] { + return Array.from({ length: items.count }, (_, i) => convert(items.objectAtIndex(i))) +} diff --git a/app/native/kotlin-platform.android.ts b/app/native/kotlin-platform.android.ts index 7ca6ba1c..3ece2aa1 100644 --- a/app/native/kotlin-platform.android.ts +++ b/app/native/kotlin-platform.android.ts @@ -1,3 +1,4 @@ +import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke'; import type { KotlinBridge } from './kotlin-platform'; // Handwritten declarations for the small native surface, checked against the @@ -17,6 +18,7 @@ declare namespace com.faceclaw.shared { } export function createKotlinBridge(): KotlinBridge { + runKotlinProtocolSmokeTest(); const bridge = new com.faceclaw.shared.KotlinBridge(); return { platform: () => bridge.platform(), diff --git a/app/native/kotlin-platform.ios.ts b/app/native/kotlin-platform.ios.ts index 24e4ea2c..f137f627 100644 --- a/app/native/kotlin-platform.ios.ts +++ b/app/native/kotlin-platform.ios.ts @@ -1,3 +1,4 @@ +import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke.ios'; import type { KotlinBridge } from './kotlin-platform'; // Handwritten declarations matching FaceclawKit's Objective-C header. @@ -26,6 +27,7 @@ class KotlinCallback extends NSObject implements FaceclawKitKotlinBridgeListener } export function createKotlinBridge(): KotlinBridge { + runKotlinProtocolSmokeTest(); const bridge = FaceclawKitKotlinBridge.alloc().init(); return { platform: () => bridge.platform(), diff --git a/app/native/kotlin-protocol-smoke.ios.ts b/app/native/kotlin-protocol-smoke.ios.ts new file mode 100644 index 00000000..91c3ad21 --- /dev/null +++ b/app/native/kotlin-protocol-smoke.ios.ts @@ -0,0 +1,36 @@ +import * as protocol from '../g2/ble-protocol.ios' +import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios' +import { SurfaceCompositor } from '../graphics/surface-compositor.ios' +import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios' +import { lvglMetrics } from './lvgl-font.ios' + +/** Development-only checks of real NativeScript selectors and bulk data crossings. */ +export function runKotlinProtocolSmokeTest(): void { + const hex = (data: Uint8Array) => Array.from(data, byte => byte.toString(16).padStart(2, '0')).join('') + const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) } + const auth = protocol.authentication(101) + assert(hex(auth) === '080410651a0408011004', 'authentication') + assert(protocol.crc16(new Uint8Array([1, 2, 3])) === 0xadad, 'CRC') + const receiver = new protocol.MessageReceiver() + const frame = protocol.frameMessage(auth, 128, 0, 64)[0] + const received = receiver.receive('test', frame) + assert(received.length === 1 && received[0].magic === 101 && hex(received[0].payload) === hex(auth), 'reassembly') + const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2)))) + const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 }) + assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event') + const transport = new CfwTransport() + const packets = transport.encode(new Uint8Array(128).fill(9), 255, 3, 185) + assert(packets.length > 0 && packets.every(p => p.length <= 185), 'compression') + transport.reset() + assert(parseCfwAcks(new Uint8Array()) === null, 'invalid ACK') + const c = new SurfaceCompositor(3, 2) + c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' }) + c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 }) + assert(hex(c.composite()) === '0010ff203040', 'composition') + assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing') + c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking') + const changed = new Uint8Array(16); changed[0] = 255 + assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update') + assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge') + console.log('FACECLAW_KOTLIN_PROTOCOL_PASS iOS') +} diff --git a/app/native/kotlin-protocol-smoke.ts b/app/native/kotlin-protocol-smoke.ts new file mode 100644 index 00000000..a99cc1e2 --- /dev/null +++ b/app/native/kotlin-protocol-smoke.ts @@ -0,0 +1,20 @@ +declare const com: any + +/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */ +export function runKotlinProtocolSmokeTest(): void { + const native = com.faceclaw.app + const hex = (data: any) => Array.from({ length: data.length }, (_, i) => (data[i] & 255).toString(16).padStart(2, '0')).join('') + const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) } + const auth = native.BleProtocol.buildAuthenticationRequest(101) + assert(hex(auth) === '080410651a0408011004', 'authentication') + const compositor = new native.SurfaceCompositor() + compositor.configureScreen(3, 2) + compositor.configureSurface('test', 0, 0, 3, 2, 0, 0) + const pixels = new Uint8Array([0, 16, 255, 32, 48, 64]) + compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1') + assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer') + assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing') + const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer)) + assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer') + console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android') +} diff --git a/app/native/lvgl-font.ios.ts b/app/native/lvgl-font.ios.ts index 99dbdd01..2f84f401 100644 --- a/app/native/lvgl-font.ios.ts +++ b/app/native/lvgl-font.ios.ts @@ -1,16 +1,5 @@ -import { LvglFont } from '../graphics/lvgl-font' -import { readBinaryFile } from './file-access' -const cache = new Map() -function load(path: string): LvglFont { - let font = cache.get(path) - if (!font) { - const bytes = readBinaryFile(path) - if (!bytes) throw new Error('Cannot read LVGL font') - font = new LvglFont(bytes) - if (cache.size >= 2) cache.delete(cache.keys().next().value!) - cache.set(path, font) - } - return font -} -export function lvglMetrics(path: string): Uint8Array { return load(path).metrics } -export function lvglGlyph(path: string, codePoint: number): Uint8Array { return load(path).glyph(codePoint) } +import { fromData } from './kotlin-data' +declare const FaceclawKitIosProtocol: any +const protocol = FaceclawKitIosProtocol.new() +export function lvglMetrics(path: string): Uint8Array { return fromData(protocol.fontMetricsPath(path)) } +export function lvglGlyph(path: string, codePoint: number): Uint8Array { return fromData(protocol.fontGlyphPathCodePoint(path, codePoint)) } diff --git a/hooks/before-prepare/faceclaw-kotlin.js b/hooks/before-prepare/faceclaw-kotlin.js new file mode 100644 index 00000000..435b315f --- /dev/null +++ b/hooks/before-prepare/faceclaw-kotlin.js @@ -0,0 +1,8 @@ +const { buildKotlin, checkAndroidRuntime } = require('../../scripts/kotlin-build.cjs'); + +module.exports = function (hookArgs) { + // NativeScript parses hooks with an older JavaScript parser; avoid optional chaining. + const { platform, release } = hookArgs.prepareData; + if (platform === 'android') checkAndroidRuntime(); + return buildKotlin(platform, release ? 'release' : 'debug'); +}; diff --git a/native/kotlin/README.md b/native/kotlin/README.md new file mode 100644 index 00000000..441b6639 --- /dev/null +++ b/native/kotlin/README.md @@ -0,0 +1,79 @@ +# Shared Kotlin + +Both `main` and `ios-port` build the same Kotlin Multiplatform sources in +`native/kotlin/shared/src`. NativeScript's `before-prepare` hook builds an AAR +for Android or a static `FaceclawKit.xcframework` for iOS, then stages it in the +local `@faceclaw/kotlin` plugin. Generated binaries and Gradle caches are ignored. + +- `commonMain/kotlin`: wire protocol, fragmentation/reassembly, event decoding, + image/RLE/texture planning, retained surface composition, glyph/image atlases, + LVGL font parsing, spectral noise suppression, PNG/GIF/WAV encodings and + callback interfaces. +- `androidMain/kotlin`: monotonic clock, reentrant locks, JVM zlib and file I/O, + zero-copy `ByteBuffer` readers, firmware hashing/file writes and WAV file storage. +- `iosMain/kotlin`: monotonic clock, recursive locks, native zlib and POSIX file + reads, plus `NSData` facades for NativeScript's Objective-C bridge. + +The `com.faceclaw.app` package, `@JvmStatic` methods and `@JvmField` properties +preserve existing Java/NativeScript callers. Android's `java.nio.ByteBuffer` +entry points for atlases/composition now take `AndroidByteReader`, keeping the +bulk buffer crossing without putting Java APIs in common code. iOS uses +`IosByteReader` and `NSData`; binary bridges avoid per-element JS/native calls. + +## Platform boundaries + +`ProtocolPlatform` supplies clocks, locks and persistent deflaters. `expect` / +`actual` functions select default platform services at compilation. Stateful +transports and caches retain reentrant locking; callbacks still run on their +caller's thread. The migration does not introduce implicit UI-thread dispatch. +`OutboundMessage` and `CfwMessageWindow` require their owner's synchronization. + +`CfwTransport` uses ordered `SYNC_FLUSH` records with one persistent compression +context. Lens changes and replay resets reset that context. Owners can close it +explicitly; iOS also cleans up abandoned native allocations. GATT write modes +are mapped by platform adapters. + +`migrated-java-sources.json` lists each removed Java source and its Kotlin +replacement. Android's build removes precisely those stale generated copies +before compilation. `android-java-boundaries.json` inventories every retained +Java file and its Android API boundary: services/receivers, GATT, media, +WebView, SQLite, rendering, networking and JNI. The sherpa-onnx Java classes +remain the vendored Android JNI ABI. PNG/GIF adapters retain only Android +storage responsibilities; their codecs are shared. + +On `ios-port`, platform adapters route the existing TypeScript session and UI +logic through shared Kotlin for protocol builders, CRC/framing/reassembly, +event decoding, CFW compression/ACKs, changed-image regions, surface composition, +and LVGL glyphs. The original pure TypeScript implementations remain as Node +test references; NativeScript selects the `.ios.ts` native adapters. Features +not yet present in the iOS app can use the shared audio/recording and texture +classes without another Java port. + +## Build and validation + +Requires JDK 21, Android SDK 35 and the existing `wear/gradlew` wrapper. iOS +additionally requires macOS, Xcode and an arm64 simulator. Configure paths in +`build_paths.sh`. Both production and tests use Kotlin 2.4.20 and Android KMP +plugin 8.13.2. + +```sh +npm ci +# Once when upgrading an existing generated Android platform to runtime 9.1.1: +npx nativescript platform clean android +npm run test:kotlin +npm run test:kotlin:ios +./build.sh +# In ios-port: +./scripts/ios.sh build ios --emulator +``` + +`tests/kotlin` compiles production sources directly. Common tests run on JVM +and iOS: wire vectors, framing/corruption/expiry, streaming compression, replay, +composition, PNG pixels, font glyphs, and Java SHA-256 golden outputs for DSP +and GIF. Android additionally runs the existing Java protocol fixtures and +concurrency checks; iOS exercises `NSData` and compositor adapters. Debug app +startup checks real NativeScript metadata and bidirectional callbacks, logging +`FACECLAW_KOTLIN_BRIDGE_PASS` and `FACECLAW_KOTLIN_PROTOCOL_PASS`. + +These checks require no BLE hardware. Physical glasses/ring behavior still +needs device testing, particularly sustained rendering and microphone traffic. diff --git a/native/kotlin/android-java-boundaries.json b/native/kotlin/android-java-boundaries.json new file mode 100644 index 00000000..9bef130a --- /dev/null +++ b/native/kotlin/android-java-boundaries.json @@ -0,0 +1,136 @@ +[ + { + "reason": "BluetoothGatt, BluetoothLeScanner, Android callback threads", + "files": [ + "com/faceclaw/app/FaceclawBleCommunicator.java", + "com/faceclaw/app/FaceclawBleManager.java", + "com/faceclaw/app/FaceclawDeviceDiscovery.java", + "com/faceclaw/app/FaceclawDeviceInfoProbe.java", + "com/faceclaw/app/FaceclawFirmwareFlasher.java", + "com/faceclaw/app/FaceclawFlashPromptCommunicator.java" + ] + }, + { + "reason": "AlarmManager, PendingIntent, Android activities/services/receivers", + "files": [ + "com/faceclaw/app/FaceclawAlarmActivity.java", + "com/faceclaw/app/FaceclawAlarmReceiver.java", + "com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java", + "com/faceclaw/app/FaceclawAlarmService.java", + "com/faceclaw/app/FaceclawAlarms.java", + "com/faceclaw/app/FaceclawForegroundService.java" + ] + }, + { + "reason": "Android providers, sensors, permissions and preferences", + "files": [ + "com/faceclaw/app/FaceclawCalendarProvider.java", + "com/faceclaw/app/FaceclawLocationProvider.java", + "com/faceclaw/app/FaceclawLocationTracker.java", + "com/faceclaw/app/FaceclawSettings.java", + "com/faceclaw/app/FaceclawSettingsPortReceiver.java", + "com/faceclaw/app/FaceclawResourceUsage.java" + ] + }, + { + "reason": "Android MediaSession, notifications, status icons and wearable services", + "files": [ + "com/faceclaw/app/FaceclawMediaBrowser.java", + "com/faceclaw/app/FaceclawMediaController.java", + "com/faceclaw/app/FaceclawMediaNotificationListenerService.java", + "com/faceclaw/app/FaceclawSystemStatusIconProvider.java", + "com/faceclaw/app/FaceclawWearBridge.java", + "com/faceclaw/app/FaceclawWearListenerService.java" + ] + }, + { + "reason": "Android WebView/JavascriptInterface, package manager and intent integration", + "files": [ + "com/faceclaw/app/FaceclawEvenAppDetector.java", + "com/faceclaw/app/FaceclawEvenHubDocumentStart.java", + "com/faceclaw/app/FaceclawEvenHubJsBridge.java", + "com/faceclaw/app/FaceclawEvenHubWebView.java", + "com/faceclaw/app/FaceclawEvenHubWebViewClient.java", + "com/faceclaw/app/FaceclawEvenHubWebViewHost.java" + ] + }, + { + "reason": "Android AudioRecord/AudioTrack/MediaCodec, Handler delivery and JNI speech/model engines", + "files": [ + "com/faceclaw/app/AndroidNoiseEffects.java", + "com/faceclaw/app/FaceclawAudioTranscoder.java", + "com/faceclaw/app/FaceclawCaptionEngine.java", + "com/faceclaw/app/FaceclawDiarizer.java", + "com/faceclaw/app/FaceclawLc3Decoder.java", + "com/faceclaw/app/FaceclawLlamaRunner.java", + "com/faceclaw/app/FaceclawSpeakerId.java", + "com/faceclaw/app/FaceclawVoiceController.java" + ] + }, + { + "reason": "Android SQLite persistence and cursor/content APIs", + "files": [ + "com/faceclaw/app/FaceclawConversationStore.java" + ] + }, + { + "reason": "Android OkHttp backend, Handler/Log delivery, download lifecycle", + "files": [ + "com/faceclaw/app/FaceclawHttp.java", + "com/faceclaw/app/FaceclawModelDownloader.java", + "com/faceclaw/app/FaceclawSseRequest.java", + "com/faceclaw/app/FaceclawWebSocket.java" + ] + }, + { + "reason": "Android Bitmap/Canvas/Paint/typeface, SVG rasterization and camera scanning", + "files": [ + "com/faceclaw/app/FontFileRenderer.java", + "com/faceclaw/app/IconRenderer.java", + "com/faceclaw/app/ImageFileLoader.java", + "com/faceclaw/app/PreviewBitmapUtil.java", + "com/faceclaw/app/FaceclawQrScanner.java" + ] + }, + { + "reason": "Android preview/UI delivery and app-scoped export paths; codecs/composition are shared", + "files": [ + "com/faceclaw/app/FaceclawPreviewCompositor.java", + "com/faceclaw/app/GifScreenRecorder.java", + "com/faceclaw/app/ScreenshotUtil.java" + ] + }, + { + "reason": "Android elapsedRealtime, worker wait/notify and Context-backed trace export", + "files": [ + "com/faceclaw/app/FrameTimings.java", + "com/faceclaw/app/util/InterruptibleSleep.java" + ] + }, + { + "reason": "Vendored sherpa-onnx JNI ABI: Java classes/fields are consumed by the Android native library; iOS uses its native engine APIs.", + "files": [ + "com/k2fsa/sherpa/onnx/FastClusteringConfig.java", + "com/k2fsa/sherpa/onnx/FeatureConfig.java", + "com/k2fsa/sherpa/onnx/HomophoneReplacerConfig.java", + "com/k2fsa/sherpa/onnx/OfflineModelConfig.java", + "com/k2fsa/sherpa/onnx/OfflineMoonshineModelConfig.java", + "com/k2fsa/sherpa/onnx/OfflineRecognizer.java", + "com/k2fsa/sherpa/onnx/OfflineRecognizerConfig.java", + "com/k2fsa/sherpa/onnx/OfflineRecognizerResult.java", + "com/k2fsa/sherpa/onnx/OfflineSpeakerDiarization.java", + "com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationCallback.java", + "com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationConfig.java", + "com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationSegment.java", + "com/k2fsa/sherpa/onnx/OfflineSpeakerSegmentationModelConfig.java", + "com/k2fsa/sherpa/onnx/OfflineSpeakerSegmentationPyannoteModelConfig.java", + "com/k2fsa/sherpa/onnx/OfflineStream.java", + "com/k2fsa/sherpa/onnx/OfflineWhisperModelConfig.java", + "com/k2fsa/sherpa/onnx/OnlineStream.java", + "com/k2fsa/sherpa/onnx/QnnConfig.java", + "com/k2fsa/sherpa/onnx/SpeakerEmbeddingExtractor.java", + "com/k2fsa/sherpa/onnx/SpeakerEmbeddingExtractorConfig.java", + "com/k2fsa/sherpa/onnx/SpeakerEmbeddingManager.java" + ] + } +] diff --git a/native/kotlin/build.gradle.kts b/native/kotlin/build.gradle.kts new file mode 100644 index 00000000..fca20d61 --- /dev/null +++ b/native/kotlin/build.gradle.kts @@ -0,0 +1,4 @@ +plugins { + kotlin("multiplatform") version "2.4.20" apply false + id("com.android.kotlin.multiplatform.library") version "8.13.2" apply false +} diff --git a/native/kotlin/migrated-java-sources.json b/native/kotlin/migrated-java-sources.json new file mode 100644 index 00000000..2342cdf2 --- /dev/null +++ b/native/kotlin/migrated-java-sources.json @@ -0,0 +1,198 @@ +[ + { + "java": "com/faceclaw/app/LvglFontFile.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/LvglFontFile.kt" + }, + { + "java": "com/faceclaw/app/FaceclawAlarmListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAlarmListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawAmbientLightListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAmbientLightListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawAudioPacketListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAudioPacketListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawBleCommunicatorListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawBleListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawCaptionEngineListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCaptionEngineListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawCompassListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCompassListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawDeviceDiscoveryListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceDiscoveryListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawDeviceInfoProbeListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceInfoProbeListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawEvenHubListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawEvenHubListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawFirmwareFlasherListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFirmwareFlasherListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawFlashPromptListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFlashPromptListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawImuListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawLlamaListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLlamaListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawLocationListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawLocationTrackerListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationTrackerListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawMediaBrowserListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaBrowserListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawMediaControllerListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaControllerListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawMicStatusListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMicStatusListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawModelDownloaderListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawModelDownloaderListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawNoiseSuppressor.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/audio/FaceclawNoiseSuppressor.kt" + }, + { + "java": "com/faceclaw/app/FaceclawNotificationListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawNotificationListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawQrScannerListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawQrScannerListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawRingEventDecoder.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FaceclawRingEventDecoder.kt" + }, + { + "java": "com/faceclaw/app/FaceclawSettingsListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSettingsListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawSseListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSseListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawVoiceControllerListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawWearListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWearListener.kt" + }, + { + "java": "com/faceclaw/app/FaceclawWebSocketListener.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWebSocketListener.kt" + }, + { + "java": "com/faceclaw/app/FwGlyphAtlas.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/FwGlyphAtlas.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/BleImageOptimizer.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/BleMagicPool.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleMagicPool.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/BleProtocol.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/CfwMessageWindow.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageWindow.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/CfwTransport.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/ConnectionOptions.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/G2Event.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/G2Event.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/MessageBuilder.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/OutboundMessage.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/OutboundMessage.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/TextureCacheState.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TextureCacheState.kt" + }, + { + "java": "com/faceclaw/app/g2protocol/TexturePlanner.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt" + }, + { + "java": "com/faceclaw/app/GlyphAtlas.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GlyphAtlas.kt" + }, + { + "java": "com/faceclaw/app/ImageAtlas.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/ImageAtlas.kt" + }, + { + "java": "com/faceclaw/app/SurfaceCompositor.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SurfaceCompositor.kt" + }, + { + "java": "com/faceclaw/app/util/BmpUtil.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/util/BmpUtil.kt" + }, + { + "java": "com/faceclaw/app/util/CollectionUtils.java", + "kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/util/CollectionUtils.kt" + }, + { + "java": "com/faceclaw/app/FaceclawFirmwareUtil.java", + "kotlin": "shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawFirmwareUtil.kt" + }, + { + "java": "com/faceclaw/app/FaceclawWavRecorder.java", + "kotlin": "shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawWavRecorder.kt" + } +] diff --git a/native/kotlin/plugin/index.js b/native/kotlin/plugin/index.js new file mode 100644 index 00000000..6e600195 --- /dev/null +++ b/native/kotlin/plugin/index.js @@ -0,0 +1 @@ +// Native APIs are exposed by NativeScript's platform metadata. diff --git a/native/kotlin/plugin/package.json b/native/kotlin/plugin/package.json new file mode 100644 index 00000000..0ed01162 --- /dev/null +++ b/native/kotlin/plugin/package.json @@ -0,0 +1,7 @@ +{ + "name": "@faceclaw/kotlin", + "version": "0.1.0", + "private": true, + "main": "index.js", + "nativescript": { "platforms": { "android": "9.1.1", "ios": "9.0.3" } } +} diff --git a/native/kotlin/plugin/platforms/android/include.gradle b/native/kotlin/plugin/platforms/android/include.gradle new file mode 100644 index 00000000..99f51576 --- /dev/null +++ b/native/kotlin/plugin/platforms/android/include.gradle @@ -0,0 +1,3 @@ +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:2.4.20" +} diff --git a/native/kotlin/settings.gradle.kts b/native/kotlin/settings.gradle.kts new file mode 100644 index 00000000..b1c53109 --- /dev/null +++ b/native/kotlin/settings.gradle.kts @@ -0,0 +1,4 @@ +pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } } +dependencyResolutionManagement { repositories { google(); mavenCentral() } } +rootProject.name = "FaceclawKotlin" +include(":shared") diff --git a/native/kotlin/shared/build.gradle.kts b/native/kotlin/shared/build.gradle.kts new file mode 100644 index 00000000..cdcf1f87 --- /dev/null +++ b/native/kotlin/shared/build.gradle.kts @@ -0,0 +1,25 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework + +plugins { + kotlin("multiplatform") + id("com.android.kotlin.multiplatform.library") +} +kotlin { + android { + namespace = "com.faceclaw.shared" + compileSdk = 35 + minSdk = 24 + compilerOptions { jvmTarget.set(JvmTarget.JVM_17) } + withHostTestBuilder {}.configure {} + } + val framework = XCFramework("FaceclawKit") + listOf(iosArm64(), iosSimulatorArm64()).forEach { target -> + target.binaries.framework { + baseName = "FaceclawKit" + isStatic = true + binaryOption("bundleId", "com.faceclaw.shared") + framework.add(this) + } + } +} diff --git a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/AndroidByteReader.kt b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/AndroidByteReader.kt new file mode 100644 index 00000000..cd9e6710 --- /dev/null +++ b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/AndroidByteReader.kt @@ -0,0 +1,20 @@ +package com.faceclaw.app + +import java.nio.ByteBuffer + +/** NativeScript's ArrayBuffer-backed ByteBuffer stays on the JVM side. */ +class AndroidByteReader(buffer: ByteBuffer) : ByteReader() { + // NativeScript can reuse the same ByteBuffer for an ArrayBuffer. Reading one + // frame must not advance the cursor used by a later atlas/preview crossing. + private val buffer = buffer.duplicate() + override fun remaining(): Int = buffer.remaining() + + override fun get(): Byte = buffer.get() + + override fun get(index: Int): Byte = buffer.get(index) + + override fun get(bytes: ByteArray, offset: Int, length: Int): ByteReader { + buffer.get(bytes, offset, length) + return this + } +} diff --git a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawFirmwareUtil.kt b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawFirmwareUtil.kt new file mode 100644 index 00000000..79b5f3a1 --- /dev/null +++ b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawFirmwareUtil.kt @@ -0,0 +1,31 @@ +package com.faceclaw.app + +import java.io.FileOutputStream +import java.nio.ByteBuffer +import java.security.MessageDigest + +/** Android's bulk ByteBuffer boundary avoids per-byte NativeScript crossings. */ +object FaceclawFirmwareUtil { + @JvmStatic fun bytesToHex(bytes: ByteArray?): String = BinaryEncoding.bytesToHex(bytes) + + @JvmStatic + fun sha256Hex(data: ByteBuffer): String { + val digest = MessageDigest.getInstance("SHA-256") + digest.update(data.duplicate()) + return bytesToHex(digest.digest()) + } + + @JvmStatic + fun writeFile(path: String, data: ByteBuffer) { + try { + FileOutputStream(path).use { file -> + file.channel.use { channel -> + val view = data.duplicate() + while (view.hasRemaining()) channel.write(view) + } + } + } catch (e: java.io.IOException) { + throw RuntimeException("firmware write failed: ${e.message}", e) + } + } +} diff --git a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawWavRecorder.kt b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawWavRecorder.kt new file mode 100644 index 00000000..8e0d8482 --- /dev/null +++ b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawWavRecorder.kt @@ -0,0 +1,72 @@ +package com.faceclaw.app + +import android.util.Log +import java.io.File +import java.io.IOException +import java.io.RandomAccessFile + +/** Android streaming file adapter; the PCM/WAV format is shared Kotlin. */ +class FaceclawWavRecorder +@Throws(IOException::class) +constructor( + private val path: String, + private val sampleRate: Int, + private val channels: Int, +) { + private var file: RandomAccessFile? + private var dataBytes = 0L + + init { + val parent = File(path).parentFile + if (parent != null && !parent.exists() && !parent.mkdirs()) + throw IOException("could not create $parent") + file = + RandomAccessFile(path, "rw").also { + it.setLength(0) + it.write(BinaryEncoding.wavHeader(0, sampleRate, channels)) + } + } + + @Synchronized + fun append(pcm16le: ByteArray?) { + val current = file ?: return + if (pcm16le == null || pcm16le.isEmpty()) return + try { + current.write(pcm16le) + dataBytes += pcm16le.size + } catch (e: IOException) { + Log.w("FaceclawWavRecorder", "append failed", e) + } + } + + @Synchronized fun getDurationMs(): Long = dataBytes * 1000L / (sampleRate * channels * 2L) + + fun getPath(): String = path + + @Synchronized + fun finish(): Long { + val current = file ?: return dataBytes + file = null + try { + current.use { + it.seek(0) + it.write( + BinaryEncoding.wavHeader( + minOf(dataBytes, Int.MAX_VALUE.toLong()).toInt(), + sampleRate, + channels, + ) + ) + } + } catch (e: IOException) { + Log.w("FaceclawWavRecorder", "finish failed", e) + } + return dataBytes + } + + @Synchronized + fun discard() { + finish() + File(path).delete() + } +} diff --git a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformServices.android.kt b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformServices.android.kt new file mode 100644 index 00000000..9aa0f905 --- /dev/null +++ b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/PlatformServices.android.kt @@ -0,0 +1,41 @@ +package com.faceclaw.app + +internal actual fun protocolPlatform(): ProtocolPlatform = AndroidProtocolPlatform + +internal actual fun readFileData(path: String): ByteArray { + val file = java.io.File(path) + require(file.isFile && file.canRead() && file.length() <= Int.MAX_VALUE) + return file.readBytes() +} + +internal actual fun deflateData(bytes: ByteArray): ByteArray { + val encoder = java.util.zip.Deflater() + try { + encoder.setInput(bytes) + encoder.finish() + val out = ByteSink() + val buffer = ByteArray(8192) + while (!encoder.finished()) out.write(buffer, 0, encoder.deflate(buffer)) + return out.toByteArray() + } finally { + encoder.end() + } +} + +internal actual fun inflateData(bytes: ByteArray, size: Int): ByteArray { + val decoder = java.util.zip.Inflater() + try { + decoder.setInput(bytes) + val result = ByteArray(size) + var offset = 0 + while (offset < size && !decoder.finished()) { + val count = decoder.inflate(result, offset, size - offset) + if (count == 0) break + offset += count + } + check(offset == size) { "recorded frame truncated: $offset of $size" } + return result + } finally { + decoder.end() + } +} diff --git a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/g2protocol/AndroidProtocolPlatform.kt b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/g2protocol/AndroidProtocolPlatform.kt new file mode 100644 index 00000000..b5667e1d --- /dev/null +++ b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/app/g2protocol/AndroidProtocolPlatform.kt @@ -0,0 +1,48 @@ +package com.faceclaw.app + +import android.bluetooth.BluetoothGattCharacteristic +import android.os.SystemClock +import java.io.ByteArrayOutputStream +import java.util.concurrent.locks.ReentrantLock +import java.util.zip.Deflater + +object AndroidProtocolPlatform : ProtocolPlatform { + override fun elapsedRealtimeMs(): Long = SystemClock.elapsedRealtime() + + override fun createLock(): ProtocolLock = + object : ProtocolLock { + private val lock = ReentrantLock() + + override fun lock() = lock.lock() + + override fun unlock() = lock.unlock() + } + + override fun createDeflater(): ProtocolDeflater = AndroidDeflater() + + @JvmStatic + fun writeType(mode: GattWriteMode): Int = + when (mode) { + GattWriteMode.WITH_RESPONSE -> BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT + GattWriteMode.WITHOUT_RESPONSE -> BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE + } +} + +private class AndroidDeflater : ProtocolDeflater { + private val deflater = Deflater() + + override fun reset() = deflater.reset() + + override fun syncFlush(message: ByteArray): ByteArray { + deflater.setInput(message) + val out = ByteArrayOutputStream() + val chunk = ByteArray(4096) + do { + val count = deflater.deflate(chunk, 0, chunk.size, Deflater.SYNC_FLUSH) + out.write(chunk, 0, count) + } while (count == chunk.size) + return out.toByteArray() + } + + override fun close() = deflater.end() +} diff --git a/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/shared/PlatformInfo.android.kt b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/shared/PlatformInfo.android.kt new file mode 100644 index 00000000..08e3fbdd --- /dev/null +++ b/native/kotlin/shared/src/androidMain/kotlin/com/faceclaw/shared/PlatformInfo.android.kt @@ -0,0 +1,11 @@ +package com.faceclaw.shared + +import android.os.Looper + +internal actual object PlatformInfo { + actual val name: String = "Android" + + actual fun threadName(): String = Thread.currentThread().name + + actual fun isMainThread(): Boolean = Looper.myLooper() == Looper.getMainLooper() +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt new file mode 100644 index 00000000..9d0f0dca --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/PlatformServices.kt @@ -0,0 +1,10 @@ +package com.faceclaw.app + +/** Platform selection happens at compilation; protocol algorithms stay shared. */ +internal expect fun protocolPlatform(): ProtocolPlatform + +internal expect fun deflateData(bytes: ByteArray): ByteArray + +internal expect fun inflateData(bytes: ByteArray, size: Int): ByteArray + +internal expect fun readFileData(path: String): ByteArray diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/FaceclawNoiseSuppressor.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/FaceclawNoiseSuppressor.kt new file mode 100644 index 00000000..db247853 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/audio/FaceclawNoiseSuppressor.kt @@ -0,0 +1,263 @@ +package com.faceclaw.app + +import kotlin.math.roundToInt + +/** + * Spectral suppression for PCM from glasses microphones: 16 ms sqrt-Hann frames at 50% overlap, + * asymmetric noise-floor tracking and a smoothed Wiener gain. Android capture-session effects live + * in AndroidNoiseEffects. This processor has no platform dependencies and must be owned by a single + * audio worker. + */ +class FaceclawNoiseSuppressor { + companion object { + private const val FFT_SIZE: Int = 256 + + private val HOP: Int = (FFT_SIZE / 2) + + private val OVERSUBTRACTION: Float = 1.6f + + /** About -16 dB of residual: keeps the noise bed natural instead of gated. */ + private val GAIN_FLOOR: Float = 0.15f + + private val GAIN_SMOOTHING: Float = 0.6f + + /** Frames of pure noise-floor learning after start/reset (~100 ms). */ + private const val NOISE_WARMUP_FRAMES: Int = 12 + + /** In-place iterative radix-2 FFT (inverse includes the 1/N scale). */ + private fun fft(re: FloatArray, im: FloatArray, inverse: Boolean): Unit { + var n: Int = re.size + run { + var i: Int = 1 + var j: Int = 0 + while ((i < n)) { + var bit: Int = (n shr 1) + run { + while (((j and bit) != 0)) { + j = (j xor bit) + bit = (bit shr 1) + } + } + j = (j xor bit) + if ((i < j)) { + var t: Float = re[i] + re[i] = re[j] + re[j] = t + t = im[i] + im[i] = im[j] + im[j] = t + } + i++ + } + } + run { + var len: Int = 2 + while ((len <= n)) { + var ang: Double = (((2 * kotlin.math.PI) / len) * (if (inverse) 1 else -1)) + var wRe: Float = (kotlin.math.cos(ang)).toFloat() + var wIm: Float = (kotlin.math.sin(ang)).toFloat() + run { + var i: Int = 0 + while ((i < n)) { + var curRe: Float = 1f + var curIm: Float = 0f + run { + var k: Int = 0 + while ((k < (len / 2))) { + var a: Int = (i + k) + var b: Int = (a + (len / 2)) + var xr: Float = ((re[b] * curRe) - (im[b] * curIm)) + var xi: Float = ((re[b] * curIm) + (im[b] * curRe)) + re[b] = (re[a] - xr) + im[b] = (im[a] - xi) + re[a] += xr + im[a] += xi + var nextRe: Float = ((curRe * wRe) - (curIm * wIm)) + curIm = ((curRe * wIm) + (curIm * wRe)) + curRe = nextRe + k++ + } + } + i += len + } + } + len = (len shl 1) + } + } + if (inverse) { + run { + var i: Int = 0 + while ((i < n)) { + re[i] /= n + im[i] /= n + i++ + } + } + } + } + } + + private val sampleRate: Int + + private val window: FloatArray = FloatArray(FFT_SIZE) + + private val history: FloatArray = FloatArray(FFT_SIZE) + + private val overlap: FloatArray = FloatArray(HOP) + + private val noise: FloatArray = FloatArray(((FFT_SIZE / 2) + 1)) + + private val gain: FloatArray = FloatArray(((FFT_SIZE / 2) + 1)) + + private val re: FloatArray = FloatArray(FFT_SIZE) + + private val im: FloatArray = FloatArray(FFT_SIZE) + + private val pending: ShortArray = ShortArray(HOP) + + private var pendingCount: Int = 0 + + private var warmupFrames: Int = 0 + + constructor(sampleRate: Int) { + this.sampleRate = sampleRate + run { + var i: Int = 0 + while ((i < FFT_SIZE)) { + window[i] = (kotlin.math.sin(((kotlin.math.PI * (i + 0.5)) / FFT_SIZE))).toFloat() + i++ + } + } + reset() + } + + fun getSampleRate(): Int { + return sampleRate + } + + /** Forget the learned noise floor (e.g. after the suppressor was toggled off). */ + fun reset(): Unit { + history.fill(0f) + overlap.fill(0f) + noise.fill(0f) + gain.fill(1f) + pendingCount = 0 + warmupFrames = 0 + } + + /** + * Suppress noise in 16-bit little-endian mono PCM. Streaming: input not filling a whole 8 ms + * hop is carried into the next call, so the output may be up to one hop shorter than the input + * (and longer on a later call); total latency is one hop. + */ + fun process(pcm16le: ByteArray?): ByteArray? { + if (((pcm16le == null) || (pcm16le.size < 2))) { + return pcm16le + } + var sampleCount: Int = (pcm16le.size / 2) + var hops: Int = ((pendingCount + sampleCount) / HOP) + var out: ByteArray = ByteArray(((hops * HOP) * 2)) + var outPos: Int = 0 + var inPos: Int = 0 + while ((inPos < sampleCount)) { + var take: Int = minOf((HOP - pendingCount), (sampleCount - inPos)) + run { + var i: Int = 0 + while ((i < take)) { + var lo: Int = (pcm16le[((inPos + i) * 2)] and 0xff) + var hi: Int = pcm16le[(((inPos + i) * 2) + 1)].toInt() + pending[(pendingCount + i)] = (((hi shl 8) or lo)).toShort() + i++ + } + } + pendingCount += take + inPos += take + if ((pendingCount == HOP)) { + outPos = processHop(out, outPos) + pendingCount = 0 + } + } + return out + } + + private fun processHop(out: ByteArray, outPos: Int): Int { + var outPos = outPos + history.copyInto(history, 0, HOP, HOP + (FFT_SIZE - HOP)) + run { + var i: Int = 0 + while ((i < HOP)) { + history[((FFT_SIZE - HOP) + i)] = (pending[i] / 32768f) + i++ + } + } + run { + var i: Int = 0 + while ((i < FFT_SIZE)) { + re[i] = (history[i] * window[i]) + im[i] = 0f + i++ + } + } + fft(re, im, false) + var warming: Boolean = (warmupFrames < NOISE_WARMUP_FRAMES) + if (warming) { + warmupFrames++ + } + run { + var b: Int = 0 + while ((b <= (FFT_SIZE / 2))) { + var power: Float = ((re[b] * re[b]) + (im[b] * im[b])) + if (warming) { + noise[b] = + (if ((warmupFrames == 1)) power + else (noise[b] + ((power - noise[b]) / warmupFrames))) + } else { + if ((power < noise[b])) { + noise[b] += ((power - noise[b]) * 0.2f) + } else { + noise[b] = minOf(power, ((noise[b] * 1.006f) + 1e-10f)) + } + } + var clean: Float = (power - (OVERSUBTRACTION * noise[b])) + var g: Float = (if ((clean > 0f)) (clean / power) else 0f) + if ((g < GAIN_FLOOR)) { + g = GAIN_FLOOR + } + g = ((GAIN_SMOOTHING * gain[b]) + ((1f - GAIN_SMOOTHING) * g)) + gain[b] = g + re[b] *= g + im[b] *= g + if (((b > 0) && (b < (FFT_SIZE / 2)))) { + re[(FFT_SIZE - b)] *= g + im[(FFT_SIZE - b)] *= g + } + b++ + } + } + fft(re, im, true) + run { + var i: Int = 0 + while ((i < HOP)) { + var value: Float = ((overlap[i] + (re[i] * window[i])) * 32767f) + var sample: Int = value.roundToInt() + if ((sample > 32767)) { + sample = 32767 + } + if ((sample < -32768)) { + sample = -32768 + } + out[(outPos + (i * 2))] = ((sample and 0xff)).toByte() + out[((outPos + (i * 2)) + 1)] = (((sample shr 8) and 0xff)).toByte() + i++ + } + } + run { + var i: Int = 0 + while ((i < HOP)) { + overlap[i] = (re[(HOP + i)] * window[(HOP + i)]) + i++ + } + } + return (outPos + (HOP * 2)) + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAlarmListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAlarmListener.kt new file mode 100644 index 00000000..63bcd1c4 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAlarmListener.kt @@ -0,0 +1,6 @@ +package com.faceclaw.app + +/** Phone-side alarm actions (dismiss / snooze), delivered to the JS engine; implemented in JS. */ +interface FaceclawAlarmListener { + fun onPhoneAction(id: Long, action: String?, minutes: Int): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAmbientLightListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAmbientLightListener.kt new file mode 100644 index 00000000..edf3cddd --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAmbientLightListener.kt @@ -0,0 +1,10 @@ +package com.faceclaw.app + +interface FaceclawAmbientLightListener { + /** + * A CFW ambient-light report (settings-channel field 105) from the master temple. body is the + * raw 24-byte ['A','L',ver,reason,...] record; it is decoded on the TS side + * (app/native/ambient-light.ts). + */ + fun onAmbientLight(body: ByteArray?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAudioPacketListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAudioPacketListener.kt new file mode 100644 index 00000000..fb5364eb --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAudioPacketListener.kt @@ -0,0 +1,5 @@ +package com.faceclaw.app + +interface FaceclawAudioPacketListener { + fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt new file mode 100644 index 00000000..59a79b07 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt @@ -0,0 +1,37 @@ +package com.faceclaw.app + +interface FaceclawBleCommunicatorListener { + fun onLog(line: String?): Unit + + fun onStateChange(phase: String?, status: String?): Unit + + fun onRingEvent( + kind: String?, + containerName: String?, + eventType: Int, + eventSource: Int, + systemExitReasonCode: Int, + frameId: Int, + ): Unit + + fun onBatteryState( + headsetBattery: Int, + headsetCharging: Int, + ringBattery: Int, + ringCharging: Int, + ): Unit + + fun onSilentMode(silent: Boolean): Unit + + fun onWearState(wearing: Boolean): Unit + + fun onPhoneLockState(locked: Boolean): Unit + + fun onEvenAppConflict(message: String?): Unit + + fun onFrameMetrics(paintMs: Int, transmitMs: Int, tileCount: Int): Unit + + fun onFrameFinished(frameId: Int, outcome: String?): Unit + + fun onFirmwareInfo(leftVersion: String?, rightVersion: String?, extension: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleListener.kt new file mode 100644 index 00000000..ed676588 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleListener.kt @@ -0,0 +1,7 @@ +package com.faceclaw.app + +interface FaceclawBleListener { + fun onNotification(address: String?, characteristicUuid: String?, data: ByteArray?): Unit + + fun onConnectionStateChange(address: String?, connected: Boolean): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCaptionEngineListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCaptionEngineListener.kt new file mode 100644 index 00000000..857492de --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCaptionEngineListener.kt @@ -0,0 +1,22 @@ +package com.faceclaw.app + +interface FaceclawCaptionEngineListener { + /** + * One finished utterance. text may be empty when no ASR model is loaded or recognition produced + * nothing; embedding is the L2-normalized voice-print or null when the speaker model is + * unavailable or the utterance was too short. startMs/endMs are on the engine's sample clock + * (milliseconds of audio since start()). + */ + fun onUtterance( + text: String?, + embedding: FloatArray?, + startMs: Long, + endMs: Long, + peakRms: Double, + ): Unit + + /** A speech onset was detected; the utterance is now being collected. */ + fun onSpeechStart(startMs: Long): Unit + + fun onStatus(status: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCompassListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCompassListener.kt new file mode 100644 index 00000000..b9f3b3d2 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCompassListener.kt @@ -0,0 +1,14 @@ +package com.faceclaw.app + +/** Receives stock compass heading and calibration notifications on the thread that subscribed. */ +interface FaceclawCompassListener { + fun onCompassEvent( + command: Int, + headingDegrees: Int, + magneticAccuracy: Int, + magneticAnomalies: Int, + orientationSource: Int, + diagnosticFlags: Int, + sampleTimeMs: Long, + ): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceDiscoveryListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceDiscoveryListener.kt new file mode 100644 index 00000000..b2a6137f --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceDiscoveryListener.kt @@ -0,0 +1,17 @@ +package com.faceclaw.app + +/** Callbacks from FaceclawDeviceDiscovery's live scan to the TypeScript layer. */ +interface FaceclawDeviceDiscoveryListener { + /** + * One advertisement (or one bonded device) as a JSON object: {address, name, manufacturerData + * (hex, company id included), rssi|null, txPower|null, connectable|null, bonded, source, + * seenAtMs}. Protocol decoding happens in TypeScript (see app/g2/even-advertisement.ts). + */ + fun onAdvertisement(json: String?): Unit + + /** The platform refused to start or continue the scan. */ + fun onScanFailed(errorCode: Int, message: String?): Unit + + /** Diagnostic line for the pairing log. */ + fun onLog(line: String?): Unit +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbeListener.java b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceInfoProbeListener.kt similarity index 52% rename from App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbeListener.java rename to native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceInfoProbeListener.kt index fe02f55d..19ec10ad 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbeListener.java +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceInfoProbeListener.kt @@ -1,15 +1,15 @@ -package com.faceclaw.app; +package com.faceclaw.app /** Callbacks from FaceclawDeviceInfoProbe to the TypeScript layer. */ -public interface FaceclawDeviceInfoProbeListener { - void onLog(String line); +interface FaceclawDeviceInfoProbeListener { + fun onLog(line: String?): Unit /** Lifecycle: connecting/authenticating for each arm, then querying. */ - void onState(String state, String detail); + fun onState(state: String?, detail: String?): Unit /** Terminal success: firmware versions and the firmware-extension string (empty on stock). */ - void onResult(String leftVersion, String rightVersion, String extension); + fun onResult(leftVersion: String?, rightVersion: String?, extension: String?): Unit /** Terminal failure (couldn't connect or read the version). */ - void onError(String message); + fun onError(message: String?): Unit } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawEvenHubListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawEvenHubListener.kt new file mode 100644 index 00000000..a1b3ee97 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawEvenHubListener.kt @@ -0,0 +1,18 @@ +package com.faceclaw.app + +/** + * Callbacks from a hosted EvenHub app's WebView to the TypeScript host. All methods are delivered + * on the Android main thread (NativeScript's JS thread), regardless of which WebView-internal + * thread produced them. + */ +interface FaceclawEvenHubListener { + /** + * The app invoked window.flutter_inappwebview.callHandler(handlerName, ...args). argsJson is + * the JSON-encoded argument array; callId identifies the promise to resolve via + * window.__fcResolve(callId, ok, value). + */ + fun onEvenAppMessage(handlerName: String?, argsJson: String?, callId: Int): Unit + + /** The WebView finished loading the app's page. */ + fun onPageFinished(url: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFirmwareFlasherListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFirmwareFlasherListener.kt new file mode 100644 index 00000000..a918fc37 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFirmwareFlasherListener.kt @@ -0,0 +1,30 @@ +package com.faceclaw.app + +/** Callbacks from FaceclawFirmwareFlasher to the TypeScript layer. */ +interface FaceclawFirmwareFlasherListener { + fun onLog(line: String?): Unit + + /** + * Fine-grained progress for the UI bar. `lens` is "left"/"right"; component/block indices are + * 1-based against their counts. `bytesSent` / `bytesTotal` cover the whole lens image, so a bar + * driven by them moves evenly even though the components differ wildly in size. + */ + fun onProgress( + lens: String?, + componentIndex: Int, + componentCount: Int, + blockIndex: Int, + blockCount: Int, + bytesSent: Long, + bytesTotal: Long, + ): Unit + + /** + * Coarse lifecycle. `state` is one of: validating, connecting, flashing, rebooting, done, + * error. `detail` carries the lens name or a message. + */ + fun onState(state: String?, detail: String?): Unit + + /** Terminal callback: success or failure, with an explanatory message. */ + fun onComplete(success: Boolean, detail: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFlashPromptListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFlashPromptListener.kt new file mode 100644 index 00000000..04dc0531 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFlashPromptListener.kt @@ -0,0 +1,25 @@ +package com.faceclaw.app + +/** Callbacks from FaceclawFlashPromptCommunicator to the TypeScript layer. */ +interface FaceclawFlashPromptListener { + fun onLog(line: String?): Unit + + /** + * Lifecycle updates. `state` is one of: connecting, connected, prompting, battery, result, + * cancelled, timeout, disconnected, error. `detail` carries the "approved"/"declined" text for + * result, or an error/explanation message. + */ + fun onState(state: String?, detail: String?): Unit + + /** + * Battery percent of each arm, read after the user approved (or right after auth when the + * prompt is skipped); -1 when an arm didn't answer. Always fires before onResult(true). + */ + fun onBattery(rightPercent: Int, leftPercent: Int): Unit + + /** + * Fires once the user picks a menu row (or, with skipPrompt, once the battery is read): true = + * flash, false = cancel. + */ + fun onResult(approved: Boolean): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt new file mode 100644 index 00000000..3dd17667 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt @@ -0,0 +1,9 @@ +package com.faceclaw.app + +/** + * Receives IMU (accelerometer) readings decoded from the glasses' sys-event stream. Registered on + * FaceclawBleCommunicator via addImuListener; callbacks are delivered on the Android main thread. + */ +interface FaceclawImuListener { + fun onImuData(x: Double, y: Double, z: Double, eventSource: Int): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLlamaListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLlamaListener.kt new file mode 100644 index 00000000..31027dd7 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLlamaListener.kt @@ -0,0 +1,16 @@ +package com.faceclaw.app + +/** + * Callbacks for one on-phone LLM generation. Like the other bridge listeners, implementations are + * typically created on the NativeScript JS thread and FaceclawLlamaRunner posts these callbacks + * back to that thread's Looper. + */ +interface FaceclawLlamaListener { + /** A piece of generated text (may be a partial word). */ + fun onToken(piece: String?): Unit + + /** Generation finished: "stop" (EOG), "length", or "cancelled". */ + fun onDone(stopReason: String?): Unit + + fun onError(message: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationListener.kt new file mode 100644 index 00000000..f74ac723 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationListener.kt @@ -0,0 +1,13 @@ +package com.faceclaw.app + +/** One-shot Android location callbacks for the TypeScript Weather bridge. */ +interface FaceclawLocationListener { + fun onLocation( + latitude: Double, + longitude: Double, + accuracyMeters: Float, + timestampMs: Long, + ): Unit + + fun onError(message: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationTrackerListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationTrackerListener.kt new file mode 100644 index 00000000..488dc09a --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationTrackerListener.kt @@ -0,0 +1,19 @@ +package com.faceclaw.app + +/** Continuous location callbacks for the TypeScript navigation bridge. */ +interface FaceclawLocationTrackerListener { + /** + * A new fix. bearingDeg/speedMps are -1 when the fix doesn't carry them (common when + * stationary); accuracyMeters is -1 when unknown. + */ + fun onLocation( + latitude: Double, + longitude: Double, + accuracyMeters: Float, + bearingDeg: Float, + speedMps: Float, + timestampMs: Long, + ): Unit + + fun onError(message: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaBrowserListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaBrowserListener.kt new file mode 100644 index 00000000..f71b6b2d --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaBrowserListener.kt @@ -0,0 +1,10 @@ +package com.faceclaw.app + +interface FaceclawMediaBrowserListener { + fun onConnectResult(requestId: Int, connected: Boolean, rootId: String?, error: String?): Unit + + fun onBrowseResult(requestId: Int, childrenJson: String?, error: String?): Unit + + /** The service connection dropped after connecting (player crashed or was killed). */ + fun onDisconnected(): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaControllerListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaControllerListener.kt new file mode 100644 index 00000000..8b887dae --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaControllerListener.kt @@ -0,0 +1,23 @@ +package com.faceclaw.app + +interface FaceclawMediaControllerListener { + /** All observed session owners, before filtering or choosing the active player. */ + fun onSessionAppsChanged(appsJson: String?): Unit + + fun onStateChange( + playbackState: String?, + packageName: String?, + appName: String?, + title: String?, + artist: String?, + album: String?, + positionMs: Long, + durationMs: Long, + playbackSpeed: Float, + canPlayPause: Boolean, + canSkipNext: Boolean, + canSkipPrevious: Boolean, + accessEnabled: Boolean, + status: String?, + ): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMicStatusListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMicStatusListener.kt new file mode 100644 index 00000000..da9cd687 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMicStatusListener.kt @@ -0,0 +1,10 @@ +package com.faceclaw.app + +interface FaceclawMicStatusListener { + /** + * A CFW mic_control field-104 status record from one temple. body is the raw 21-byte + * ['M','C',ver,...] record (decoded on the TS side); arm is "L" or "R" for the temple's own BLE + * link, "?" if unmatched. + */ + fun onMicStatus(body: ByteArray?, arm: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawModelDownloaderListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawModelDownloaderListener.kt new file mode 100644 index 00000000..f2811569 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawModelDownloaderListener.kt @@ -0,0 +1,11 @@ +package com.faceclaw.app + +/** Progress callbacks for FaceclawModelDownloader, posted to the creating thread's Looper. */ +interface FaceclawModelDownloaderListener { + fun onProgress(bytesDownloaded: Long, totalBytes: Long): Unit + + /** The file is fully downloaded, verified, and moved to its final path. */ + fun onDone(path: String?): Unit + + fun onError(message: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawNotificationListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawNotificationListener.kt new file mode 100644 index 00000000..35cf0523 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawNotificationListener.kt @@ -0,0 +1,5 @@ +package com.faceclaw.app + +interface FaceclawNotificationListener { + fun onNotificationPosted(key: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawQrScannerListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawQrScannerListener.kt new file mode 100644 index 00000000..60a2f29d --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawQrScannerListener.kt @@ -0,0 +1,10 @@ +package com.faceclaw.app + +/** Callbacks for one FaceclawQrScanner.scan; exactly one method fires. */ +interface FaceclawQrScannerListener { + fun onResult(text: String?): Unit + + fun onCancelled(): Unit + + fun onError(message: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSettingsListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSettingsListener.kt new file mode 100644 index 00000000..d453a8ac --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSettingsListener.kt @@ -0,0 +1,6 @@ +package com.faceclaw.app + +/** Change-notification callback for FaceclawSettings; implemented in JS. */ +interface FaceclawSettingsListener { + fun onSettingChanged(key: String?): Unit +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseListener.java b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSseListener.kt similarity index 65% rename from App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseListener.java rename to native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSseListener.kt index 8780d601..fde0d60a 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseListener.java +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSseListener.kt @@ -1,13 +1,16 @@ -package com.faceclaw.app; +package com.faceclaw.app /** Callbacks for FaceclawSseRequest, delivered on the constructing thread's Looper. */ -public interface FaceclawSseListener { +interface FaceclawSseListener { /** One line of a successful (2xx) streaming response body, without the newline. */ - void onLine(String line); + fun onLine(line: String?): Unit + /** The server answered with a non-2xx status; body is the full error body. */ - void onHttpError(int code, String body); + fun onHttpError(code: Int, body: String?): Unit + /** The 2xx response body ended normally. */ - void onComplete(); + fun onComplete(): Unit + /** Network-level failure (connect, TLS, mid-stream drop). Not called after cancel(). */ - void onFailure(String message); + fun onFailure(message: String?): Unit } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt new file mode 100644 index 00000000..e3b4fd75 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt @@ -0,0 +1,44 @@ +package com.faceclaw.app + +interface FaceclawVoiceControllerListener { + fun onStatus(status: String?): Unit + + /** + * Best transcript of the current utterance so far. REPLACE semantics: text is the complete + * transcript, not a delta — display it verbatim, replacing any previous partial. isFinal marks + * the end of the utterance. + */ + fun onTranscript(text: String?, isFinal: Boolean): Unit + + /** Capture has finished, after the final transcript or a startup/recognition error. */ + fun onStopped(captureId: Int): Unit + + /** + * Decoded microphone audio: 16 kHz mono signed 16-bit little-endian PCM. Emitted in every mode + * so raw-PCM consumers (levels, recording, captions) keep working while the onboard recognizer + * owns the transcript. + */ + fun onPcm(pcm16le: ByteArray?): Unit + + /** + * Per-packet DSP metadata the glasses firmware appends to each 50 ms audio packet, computed on + * the raw stereo capture before its mono downmix: the direction-of-arrival in signed degrees (0 + * = straight ahead, positive to the wearer's right) and a signal-strength ratio that serves as + * a confidence/energy proxy for the angle. + */ + fun onFrameMeta(angleDegrees: Int, ssr: Int): Unit + + /** + * The speaker stopped, in a hands-free session that has no button release to end it. Only fires + * when endpointing was enabled for the session; see FaceclawVoiceController.setEndpointing. + */ + fun onSpeechEnd(): Unit + + /** + * Result of speaker verification against the enrolled wearer voice-print, emitted once per + * capture session just before the final transcript. Only fires when verification was configured + * for the session; fails open (isWearer=true) when the utterance was too short to verify or the + * speaker model could not be loaded. + */ + fun onSpeakerVerified(isWearer: Boolean, similarity: Float): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWearListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWearListener.kt new file mode 100644 index 00000000..420865ee --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWearListener.kt @@ -0,0 +1,17 @@ +package com.faceclaw.app + +/** + * Callbacks from the Android Wear (Wear OS) bridge; implemented in JS. Every call is delivered on + * the thread that registered the listener (see FaceclawWearBridge.setListener). + */ +interface FaceclawWearListener { + /** + * A message from the watch app. `path` is the Data Layer message path (e.g. "/faceclaw/input"); + * `json` is the UTF-8 payload (always a JSON object, "{}" when the watch sent none); `nodeId` + * identifies the watch so a reply can be addressed to it. + */ + fun onMessage(path: String?, json: String?, nodeId: String?): Unit + + /** The set of reachable watches running the Faceclaw watch app changed. */ + fun onWatchConnection(reachable: Boolean, watchName: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWebSocketListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWebSocketListener.kt new file mode 100644 index 00000000..b613f7e1 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWebSocketListener.kt @@ -0,0 +1,12 @@ +package com.faceclaw.app + +/** Callbacks for FaceclawWebSocket, delivered on the Android main thread. */ +interface FaceclawWebSocketListener { + fun onOpen(): Unit + + fun onTextMessage(message: String?): Unit + + fun onClosed(code: Int, reason: String?): Unit + + fun onFailure(message: String?): Unit +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt new file mode 100644 index 00000000..c2fa50a0 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt @@ -0,0 +1,735 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +class BleImageOptimizer { + companion object { + private const val TAG: String = "BleImageOptimizer" + + /** + * Split an image into fragments (packets), taking advantage of the fact that each packet + * can declare a logical size longer than its byte payload to get free trailing + * zero-padding. + * + * Each fragment's logical size stays within maxFragmentSize, while trailing zeros inside + * that span can be omitted from the payload. + */ + @JvmStatic + fun planImageFragments( + bmp: ByteArray, + maxFragmentSize: Int, + ): MutableList { + return planImageFragments(bmp, maxFragmentSize, false) + } + + @JvmStatic + fun planImageFragments( + bmp: ByteArray?, + maxFragmentSize: Int, + reserveFinalByte: Boolean, + ): MutableList { + if (((bmp == null) || (bmp.size == 0))) { + return mutableListOf(BleProtocol.ImageFragment(0, ByteArray(0), 0)) + } + if ((maxFragmentSize <= 0)) { + throw IllegalArgumentException("maxFragmentSize must be positive") + } + val bulkLength: Int = (if (reserveFinalByte) (bmp.size - 1) else bmp.size) + val fragmentCount: Int = + ((((bulkLength + maxFragmentSize) - 1) / maxFragmentSize) + + (if ((bulkLength < bmp.size)) 1 else 0)) + val fragments: MutableList = ArrayList(fragmentCount) + run { + var index: Int = 0 + while (((index * maxFragmentSize) < bulkLength)) { + var start: Int = (index * maxFragmentSize) + var end: Int = minOf((start + maxFragmentSize), bulkLength) + fragments.add(buildFragment(index, bmp, start, end)) + index++ + } + } + if ((bulkLength < bmp.size)) { + fragments.add( + BleProtocol.ImageFragment( + fragments.size, + bmp.copyOfRange(bulkLength, bmp.size), + (bmp.size - bulkLength), + ) + ) + } + + return fragments + } + + private fun buildFragment( + index: Int, + bmp: ByteArray, + start: Int, + end: Int, + ): BleProtocol.ImageFragment { + var dataEnd: Int = end + while (((dataEnd > start) && (bmp[(dataEnd - 1)].toInt() == 0))) { + dataEnd -= 1 + } + if ((dataEnd == start)) { + return BleProtocol.ImageFragment(index, ByteArray(0), (end - start)) + } + return BleProtocol.ImageFragment(index, bmp.copyOfRange(start, dataEnd), (end - start)) + } + + @JvmStatic + fun buildIncrementalImagePayload( + previous: ByteArray?, + next: ByteArray?, + width: Int, + height: Int, + frameId: Int, + ): IncrementalPlan? { + var stride: Int = ((width + 1) shr 1) + if ( + ((((((width <= 0) || (height <= 0)) || (previous == null)) || (next == null)) || + (previous.size != (stride * height))) || (next.size != (stride * height))) + ) { + return null + } + var minByteX: Int = stride + var maxByteX: Int = -1 + var minY: Int = height + var maxY: Int = -1 + var changedBytes: Int = 0 + var columnChanged: BooleanArray = BooleanArray(stride) + run { + var y: Int = 0 + while ((y < height)) { + var rowOffset: Int = (y * stride) + run { + var x: Int = 0 + while ((x < stride)) { + if ((previous[(rowOffset + x)] != next[(rowOffset + x)])) { + changedBytes++ + columnChanged[x] = true + if ((x < minByteX)) { + minByteX = x + } + if ((x > maxByteX)) { + maxByteX = x + } + if ((y < minY)) { + minY = y + } + maxY = y + } + x++ + } + } + y++ + } + } + if ((maxY < 0)) { + return null + } + var clusterCount: Int = 0 + var inCluster: Boolean = false + run { + var x: Int = minByteX + while ((x <= maxByteX)) { + if (columnChanged[x]) { + if (!inCluster) { + clusterCount++ + } + inCluster = true + } else { + inCluster = false + } + x++ + } + } + var left: Int = ((minByteX * 2) and 3.inv()) + var rightExclusive: Int = minOf(width, ((((maxByteX + 1) * 2) + 3) and 3.inv())) + var top: Int = (minY and 1.inv()) + var bottomExclusive: Int = minOf(height, ((maxY + 2) and 1.inv())) + var boxWidth: Int = (rightExclusive - left) + var boxHeight: Int = (bottomExclusive - top) + if (((boxWidth >= width) && (boxHeight >= height))) { + return null + } + var out: ByteArray = + encodeMode3Rect(next, stride, left, top, boxWidth, boxHeight, frameId) + return IncrementalPlan(out, changedBytes, ((boxWidth shr 1) * boxHeight), clusterCount) + } + + /** + * The aligned changed bounding box between two same-size packed frames, or null when they + * are identical: pixel-space {left, top, width, height} with left/width multiples of 4 and + * top/height multiples of 2 (mode-3 header quantization). Exposed so the texture-cache + * planner can reuse the exact rect geometry the single-bbox path would send. + */ + @JvmStatic + fun computeChangedBox( + previous: ByteArray?, + next: ByteArray?, + width: Int, + height: Int, + ): IntArray? { + var stride: Int = ((width + 1) shr 1) + if ( + ((((((width <= 0) || (height <= 0)) || (previous == null)) || (next == null)) || + (previous.size != (stride * height))) || (next.size != (stride * height))) + ) { + return null + } + var minByteX: Int = stride + var maxByteX: Int = -1 + var minY: Int = height + var maxY: Int = -1 + run { + var y: Int = 0 + while ((y < height)) { + var rowOffset: Int = (y * stride) + run { + var x: Int = 0 + while ((x < stride)) { + if ((previous[(rowOffset + x)] != next[(rowOffset + x)])) { + if ((x < minByteX)) { + minByteX = x + } + if ((x > maxByteX)) { + maxByteX = x + } + if ((y < minY)) { + minY = y + } + maxY = y + } + x++ + } + } + y++ + } + } + if ((maxY < 0)) { + return null + } + var left: Int = ((minByteX * 2) and 3.inv()) + var rightExclusive: Int = minOf(width, ((((maxByteX + 1) * 2) + 3) and 3.inv())) + var top: Int = (minY and 1.inv()) + var bottomExclusive: Int = minOf(height, ((maxY + 2) and 1.inv())) + return intArrayOf(left, top, (rightExclusive - left), (bottomExclusive - top)) + } + + /** + * The multi-rect split of the changed region (see computeSplitRects), or null when it is + * one region / too fragmented / the frames aren't comparable. Exposed for the texture-cache + * planner. + */ + @JvmStatic + fun computeChangedRects( + previous: ByteArray?, + next: ByteArray?, + width: Int, + height: Int, + maxRects: Int, + ): MutableList? { + var stride: Int = ((width + 1) shr 1) + if ( + ((((((width <= 0) || (height <= 0)) || (previous == null)) || (next == null)) || + (previous.size != (stride * height))) || (next.size != (stride * height))) + ) { + return null + } + var rects: MutableList? = + computeSplitRects(previous, next, stride, width, height, maxRects) + return (if (((rects == null) || (rects.size < 2))) null else rects) + } + + /** + * Encode one CFW mode-3 rectangle delta: + * [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][rle(box pixels)] left/width must be + * multiples of 4 (=> left>>1, width>>1 are whole bytes), top/ height multiples of 2. The + * box pixels are top-down rows of `next` (4bpp packed, width>>1 bytes/row), run-length + * encoded before deflate. Shared by the single-bbox path, each rect of a mode-8 multi-rect + * batch, and the texture-cache planner. + */ + @JvmStatic + fun encodeMode3Rect( + next: ByteArray, + stride: Int, + left: Int, + top: Int, + boxWidth: Int, + boxHeight: Int, + fid: Int, + ): ByteArray { + var regionStride: Int = (boxWidth shr 1) + var region: ByteArray = ByteArray((regionStride * boxHeight)) + run { + var y: Int = 0 + while ((y < boxHeight)) { + next.copyInto( + region, + (y * regionStride), + (((top + y) * stride) + (left shr 1)), + (((top + y) * stride) + (left shr 1)) + regionStride, + ) + y++ + } + } + var compressed: ByteArray = rleEncode(region) + var out: ByteArray = ByteArray((7 + compressed.size)) + out[0] = 3 + out[1] = ((left / 4)).toByte() + out[2] = ((top / 2)).toByte() + out[3] = ((boxWidth / 4)).toByte() + out[4] = ((boxHeight / 2)).toByte() + out[5] = ((fid and 0xff)).toByte() + out[6] = (((fid shr 8) and 0xff)).toByte() + compressed.copyInto(out, 7, 0, 0 + compressed.size) + return out + } + + /** A full repaint in independently decodable commands below the uint16 record limit. */ + @JvmStatic + fun encodeFullFrameBands( + packed: ByteArray, + width: Int, + height: Int, + firstFid: Int, + ): MutableList { + if ( + (((((((width <= 0) || (width > 640)) || ((width and 3) != 0)) || (height <= 0)) || + (height > 480)) || ((height and 1) != 0)) || + (packed.size != ((width * height) / 2))) + ) { + throw IllegalArgumentException("unsupported custom framebuffer dimensions") + } + var commands: MutableList = ArrayList() + var fid: Int = firstFid + run { + var top: Int = 0 + while ((top < height)) { + commands.add( + encodeMode3Rect( + packed, + (width / 2), + 0, + top, + width, + minOf(64, (height - top)), + fid, + ) + ) + fid = (if ((fid >= 0xfffe)) 1 else (fid + 1)) + top += 64 + } + } + return commands + } + + private const val MULTI_RECT_V_GAP_ROWS: Int = 4 + + private const val MULTI_RECT_H_GAP_BYTES: Int = 6 + + /** + * Build a mode-8 multi-rect payload for the change between two same-size 4bpp frames, + * assigning consecutive frame ids starting at fidStart (each rect needs a distinct fid — + * the CFW skips duplicate fids). Returns null when the change is a single region (use the + * single-rect path), when it fragments past maxRects, or when the frames aren't comparable. + */ + @JvmStatic + fun buildMultiRectImagePayload( + previous: ByteArray?, + next: ByteArray?, + width: Int, + height: Int, + fidStart: Int, + maxRects: Int, + ): MultiRectPlan? { + var stride: Int = ((width + 1) shr 1) + if ( + ((((((width <= 0) || (height <= 0)) || (previous == null)) || (next == null)) || + (previous.size != (stride * height))) || (next.size != (stride * height))) + ) { + return null + } + var rects: MutableList? = + computeSplitRects(previous, next, stride, width, height, maxRects) + if (((rects == null) || (rects.size < 2))) { + return null + } + var subs: MutableList = ArrayList(rects.size) + var coveredBytes: Int = 0 + var fid: Int = fidStart + var total: Int = 2 + for (r in rects) { + var sub: ByteArray = encodeMode3Rect(next, stride, r[0], r[1], r[2], r[3], fid) + subs.add(sub) + coveredBytes += ((r[2] shr 1) * r[3]) + total += (2 + sub.size) + fid = (if ((fid >= 0xfffe)) 1 else (fid + 1)) + } + var out: ByteArray = ByteArray(total) + out[0] = 8 + out[1] = (rects.size).toByte() + var pos: Int = 2 + for (sub in subs) { + out[pos] = ((sub.size and 0xff)).toByte() + out[(pos + 1)] = (((sub.size shr 8) and 0xff)).toByte() + pos += 2 + sub.copyInto(out, pos, 0, 0 + sub.size) + pos += sub.size + } + return MultiRectPlan(out, rects.size, coveredBytes, fid) + } + + /** + * Split the changed region between two packed 4bpp frames into a small set of aligned + * rectangles that together cover every changed pixel: maximal vertical bands of changed + * rows (merging gaps < V_GAP), each split into horizontal column clusters (merging gaps < + * H_GAP), with each cluster's rows tightened to where it actually changes. Rects are + * pixel-space {left,top,width,height}, left/width aligned to 4px and top/height to 2px. + * Returns null if there's no change or the split exceeds maxRects (caller falls back to a + * single bounding box). + */ + private fun computeSplitRects( + previous: ByteArray, + next: ByteArray, + stride: Int, + width: Int, + height: Int, + maxRects: Int, + ): MutableList? { + var rowChanged: BooleanArray = BooleanArray(height) + var anyChange: Boolean = false + run { + var y: Int = 0 + while ((y < height)) { + var off: Int = (y * stride) + run { + var x: Int = 0 + while ((x < stride)) { + if ((previous[(off + x)] != next[(off + x)])) { + rowChanged[y] = true + anyChange = true + break + } + x++ + } + } + y++ + } + } + if (!anyChange) { + return null + } + var rects: MutableList = ArrayList() + var col: BooleanArray = BooleanArray(stride) + var y: Int = 0 + while ((y < height)) { + if (!rowChanged[y]) { + y++ + continue + } + var bandTop: Int = y + var bandBot: Int = y + var cur: Int = (y + 1) + while ((cur < height)) { + if (rowChanged[cur]) { + bandBot = cur + cur++ + continue + } + var g: Int = cur + while (((g < height) && !rowChanged[g])) { + g++ + } + if (((g < height) && ((g - cur) < MULTI_RECT_V_GAP_ROWS))) { + cur = g + } else { + break + } + } + col.fill(false) + run { + var yy: Int = bandTop + while ((yy <= bandBot)) { + if (!rowChanged[yy]) { + yy++ + continue + } + var off: Int = (yy * stride) + run { + var x: Int = 0 + while ((x < stride)) { + if ((previous[(off + x)] != next[(off + x)])) { + col[x] = true + } + x++ + } + } + yy++ + } + } + var x: Int = 0 + while ((x < stride)) { + if (!col[x]) { + x++ + continue + } + var cL: Int = x + var cR: Int = x + var cx: Int = (x + 1) + while ((cx < stride)) { + if (col[cx]) { + cR = cx + cx++ + continue + } + var g: Int = cx + while (((g < stride) && !col[g])) { + g++ + } + if (((g < stride) && ((g - cx) < MULTI_RECT_H_GAP_BYTES))) { + cx = g + } else { + break + } + } + var tTop: Int = height + var tBot: Int = -1 + run { + var yy: Int = bandTop + while ((yy <= bandBot)) { + if (!rowChanged[yy]) { + yy++ + continue + } + var off: Int = (yy * stride) + run { + var xx: Int = cL + while ((xx <= cR)) { + if ((previous[(off + xx)] != next[(off + xx)])) { + if ((yy < tTop)) { + tTop = yy + } + tBot = yy + break + } + xx++ + } + } + yy++ + } + } + if ((tBot >= 0)) { + var left: Int = ((cL * 2) and 3.inv()) + var rightExclusive: Int = minOf(width, ((((cR + 1) * 2) + 3) and 3.inv())) + var top: Int = (tTop and 1.inv()) + var bottomExclusive: Int = minOf(height, ((tBot + 2) and 1.inv())) + rects.add( + intArrayOf(left, top, (rightExclusive - left), (bottomExclusive - top)) + ) + if ((rects.size > maxRects)) { + return null + } + } + x = (cR + 1) + } + y = (bandBot + 1) + } + return rects + } + + /** + * Run-length encode headerless 4bpp pixels for CFW load_image_z mode 6. Wire format: + * [6][rle(4bpp pixels)]. Always use mode 6: the logical image can be larger than its + * EvenHub carrier, so a raw BMP fallback would carry dimensions the legacy container loader + * cannot accept. + */ + @JvmStatic + fun maybeCompress(packed: ByteArray?, width: Int, height: Int): ByteArray { + if (((packed == null) || (packed.size == 0))) { + return packed ?: ByteArray(0) + } + var z: ByteArray = rleEncode(packed) + var out: ByteArray = ByteArray((z.size + 1)) + out[0] = 6 + z.copyInto(out, 1, 0, 0 + z.size) + return out + } + + /** + * Run-length encode packed 4bpp pixels for CFW modes 3 and 6, whose payload is RLE tokens; + * deflate belongs to the transport. Runs are over the pixel NIBBLES of {@code pix} in wire + * order (high nibble = left pixel), including the pad nibble that ends each row at odd + * widths — i.e. {@code pix} read as 2*length nibbles. One token is: + *
+         *   [cnt4|color4]                 cnt 1..15
+         *   [0|color4][cnt8]              cnt 1..255
+         *   [0|color4][0][cntLo][cntHi]   cnt 1..65535, little-endian
+         * 
+ * with the low nibble always the color; runs longer than 65535 split across tokens. Worst + * case (every nibble its own run) is exactly one byte per nibble, which is what the buffer + * is sized for. + */ + @JvmStatic + fun rleEncode(pix: ByteArray): ByteArray { + var n: Int = (pix.size * 2) + var out: ByteArray = ByteArray(n) + var o: Int = 0 + var i: Int = 0 + while ((i < n)) { + var v: Int = nibbleAt(pix, i) + var j: Int = (i + 1) + while (((j < n) && (nibbleAt(pix, j) == v))) { + j++ + } + var run: Int = (j - i) + while ((run > 0)) { + var c: Int = minOf(run, 0xffff) + if ((c <= 15)) { + out[o++] = (((c shl 4) or v)).toByte() + } else { + if ((c <= 255)) { + out[o++] = (v).toByte() + out[o++] = (c).toByte() + } else { + out[o++] = (v).toByte() + out[o++] = 0 + out[o++] = ((c and 0xff)).toByte() + out[o++] = ((c shr 8)).toByte() + } + } + run -= c + } + i = j + } + return out.copyOf(o) + } + + private fun nibbleAt(pix: ByteArray, i: Int): Int { + var b: Int = (pix[(i shr 1)] and 0xff) + return (if (((i and 1) != 0)) (b and 0x0f) else (b shr 4)) + } + } + + constructor() {} + + class TileImagePlan { + @JvmField val tileIndex: Int + + @JvmField val tile: BleProtocol.ImageTileOptions + + @JvmField val packed: ByteArray? + + @JvmField val width: Int + + @JvmField val height: Int + + @JvmField val payload: ByteArray + + @JvmField val sessionId: Int + + @JvmField var fragments: MutableList = mutableListOf() + + constructor( + tileIndex: Int, + tile: BleProtocol.ImageTileOptions, + packed: ByteArray?, + width: Int, + height: Int, + sessionId: Int, + ) { + this.tileIndex = tileIndex + this.tile = tile + this.packed = (if ((packed == null)) ByteArray(0) else packed) + this.width = width + this.height = height + this.payload = maybeCompress(this.packed, width, height) + this.sessionId = sessionId + } + + constructor( + tileIndex: Int, + tile: BleProtocol.ImageTileOptions, + packed: ByteArray?, + width: Int, + height: Int, + sessionId: Int, + payloadOverride: ByteArray, + ) { + this.tileIndex = tileIndex + this.tile = tile + this.packed = (if ((packed == null)) ByteArray(0) else packed) + this.width = width + this.height = height + this.payload = payloadOverride + this.sessionId = sessionId + } + } + + /** + * A mode-3 incremental payload plus diagnostics about how tightly the bounding box fit the + * actual change. {@code changedBytes} is how many packed bytes actually differ; {@code + * boxBytes} is how many the box sends. A large box with few changed bytes / multiple clusters + * means distant small edits were merged into one oversized box (which can spill past the + * fragment size into an extra serialized BLE message). + */ + class IncrementalPlan { + @JvmField val payload: ByteArray + + @JvmField val changedBytes: Int + + @JvmField val boxBytes: Int + + @JvmField val clusterCount: Int + + constructor(payload: ByteArray, changedBytes: Int, boxBytes: Int, clusterCount: Int) { + this.payload = payload + this.changedBytes = changedBytes + this.boxBytes = boxBytes + this.clusterCount = clusterCount + } + } + + /** + * A CFW mode-8 multi-segment payload (`[8][count]` then per-rect `[len16][mode-3 submsg]`) + * carrying several tight rectangle deltas instead of one bounding box, plus diagnostics. Each + * rect is applied to the firmware shadow with the panel push deferred, then presented once. + * Used when the change splits into regions whose bounding box wastes bytes (distant edits, or a + * sparse box); the caller falls back to the single-rect path when this returns null or isn't + * smaller. + */ + class MultiRectPlan { + @JvmField val payload: ByteArray + + @JvmField val rectCount: Int + + @JvmField val coveredBytes: Int + + @JvmField val nextFid: Int + + constructor(payload: ByteArray, rectCount: Int, coveredBytes: Int, nextFid: Int) { + this.payload = payload + this.rectCount = rectCount + this.coveredBytes = coveredBytes + this.nextFid = nextFid + } + } + + class ImageUpdateStats { + @JvmField val paintMs: Int + + @JvmField val tileCount: Int + + @JvmField val frameId: Int + + @JvmField var firstWriteStartedAtMs: Long = 0L + + constructor(paintMs: Int, tileCount: Int, frameId: Int) { + this.paintMs = maxOf(0, paintMs) + this.tileCount = tileCount + this.frameId = frameId + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleMagicPool.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleMagicPool.kt new file mode 100644 index 00000000..354ff1c1 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleMagicPool.kt @@ -0,0 +1,43 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField + +/** + * Allocates one-byte message identifiers in LRU order. Release records survive reuse so the + * communicator can diagnose late ACKs after a timeout. + */ +class BleMagicPool(private val platform: ProtocolPlatform) { + companion object { + const val MIN_MAGIC = 100 + const val MAX_MAGIC = 255 + } + + private val lock = platform.createLock() + private val available = ArrayDeque((MIN_MAGIC..MAX_MAGIC).toList()) + private val allocated = BooleanArray(256) + private val releaseRecords = mutableMapOf() + + fun allocate(): Int = lock.withLock { + check(available.isNotEmpty()) { "no BLE magic values available" } + val magic = available.removeFirst() + allocated[magic] = true + magic + } + + fun release(sid: Int, magic: Int, label: String?, reason: String?) = lock.withLock { + if (magic !in MIN_MAGIC..MAX_MAGIC) return@withLock + releaseRecords["$sid:$magic"] = ReleaseRecord(label, reason, platform.elapsedRealtimeMs()) + if (!allocated[magic]) return@withLock + allocated[magic] = false + available.addLast(magic) + } + + fun getReleaseRecord(sid: Int, magic: Int): ReleaseRecord? = lock.withLock { + releaseRecords["$sid:$magic"] + } + + class ReleaseRecord(label: String?, reason: String?, @JvmField val releasedAtMs: Long) { + @JvmField val label: String = label ?: "" + @JvmField val reason: String = reason ?: "" + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt new file mode 100644 index 00000000..4227cdb5 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt @@ -0,0 +1,1480 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmOverloads +import kotlin.jvm.JvmStatic + +/** + * BleProtocol: Functions for generating and parsing messages when speaking to the Even Realities + * G2. Does _not_ include code for actually sending anything; for that, see FaceclawBleCommunicator. + * + * Most message formats are protobufs. For schemas, check g2-kit-unofficial. + */ +class BleProtocol { + companion object { + const val WRITE_CHAR_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5401" + + const val NOTIFY_CHAR_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5402" + + const val OTA_DATA_WRITE_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e0001" + + const val OTA_DATA_NOTIFY_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e0002" + + const val RENDER_NOTIFY_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e6402" + + const val R1_PHONE_NOTIFY_CHAR_UUID: String = "bae80011-4f05-4503-8e65-3af1f7329d1f" + + const val R1_NOTIFY_CHAR_UUID: String = "bae80013-4f05-4503-8e65-3af1f7329d1f" + + const val PRELUDE_ACK_SID: Int = 0x01 + + const val PRELUDE_ACK_MAGIC: Int = 156 + + /** + * DEVICE_SETTINGS security-auth channel. Firmware 2.2.9 enforces completing this exchange + * shortly after a GATT connection opens (a ~30 s deadline closes unauthenticated links) and + * gates query responses on it; the message itself exists back to the 2.2.4 era, so sending + * it is safe on all stock versions. See ../notes/ble-connections-2.2.9.md. + */ + const val SID_SECURITY_AUTH: Int = 0x80 + + const val FLAG_SECURITY_AUTH: Int = 0x00 + + private const val SECURITY_AUTH_CMD: Int = 4 + + const val SID_APP_LAUNCH: Int = 0x01 + + const val SID_EVENHUB: Int = 0xe0 + + const val SID_UI_SETTING: Int = 0x09 + + const val SID_STATE_CHANGE: Int = 0x0 + + const val SID_ONBOARDING: Int = 0x10 + + /** G2SettingPackage.commandId for device-initiated pushes (vs. 2 = app read). */ + const val SETTINGS_CMD_DEVICE_SEND_TO_APP: Int = 3 + + const val FACECLAW_WAKE_CONTROL_FIELD: Int = 101 + + const val FACECLAW_WAKE_EVENT_FIELD: Int = 102 + + /** + * CFW mic_control: host writes an [77.toByte(),67.toByte(),ver,op,...] record as settings + * field 103; each temple reports a 21-byte [77.toByte(),67.toByte(),ver,...] status as + * field 104, both appended to settings read responses and as standalone commandId=3 pushes. + */ + const val FACECLAW_MIC_CONTROL_FIELD: Int = 103 + + const val FACECLAW_MIC_STATUS_FIELD: Int = 104 + + /** + * CFW als_sensor: ambient-light reports on settings-channel field 105, 24-byte + * [65.toByte(),76.toByte(),ver,reason,...] records from the master temple (decoded in + * app/native/ambient-light.ts). + */ + const val FACECLAW_ALS_REPORT_FIELD: Int = 105 + + const val FACECLAW_WAKE_OP_ACQUIRE: Int = 1 + + const val FACECLAW_WAKE_OP_RELEASE: Int = 2 + + const val FACECLAW_WAKE_OP_CLAIM: Int = 3 + + const val FACECLAW_WAKE_OP_READY: Int = 4 + + const val FACECLAW_FB_OP_ACQUIRE: Int = 5 + + const val FACECLAW_FB_OP_RELEASE: Int = 6 + + const val FACECLAW_WEAR_OP_QUERY: Int = 7 + + const val CFW_IMAGE_MODE_CLEANUP: Int = 11 + + private const val FACECLAW_WAKE_EVENT: Int = 1 + + const val FACECLAW_WAKE_EVENT_HEAD_UP: Int = 5 + + const val FACECLAW_GESTURE_EVENT_TAP: Int = 2 + + const val FACECLAW_GESTURE_EVENT_LONG_PRESS: Int = 3 + + const val FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE: Int = 4 + + private const val FACECLAW_RAW_SOURCE_LEFT_TEMPLE: Int = 0 + + private const val FACECLAW_RAW_SOURCE_RIGHT_TEMPLE: Int = 1 + + private const val FACECLAW_RAW_SOURCE_RING: Int = 4 + + private const val FACECLAW_WAKE_PROTOCOL_VERSION: Int = 1 + + const val SID_EVEN_AI: Int = 0x07 + + const val SID_NAVIGATION: Int = 0x08 + + const val NAV_CMD_COMPASS_CHANGED: Int = 15 + + const val NAV_CMD_COMPASS_CALIBRATION_STARTED: Int = 16 + + const val NAV_CMD_COMPASS_CALIBRATION_COMPLETE: Int = 17 + + const val EVEN_AI_CMD_CTRL: Int = 1 + + const val EVEN_AI_STATUS_WAKE_UP: Int = 1 + + const val EVEN_AI_STATUS_ENTER: Int = 2 + + const val EVEN_AI_STATUS_EXIT: Int = 3 + + const val FLAG_REQUEST: Int = 0x20 + + const val FLAG_NOTIFY: Int = 0x01 + + const val FLAG_NOTIFY_ALT: Int = 0x06 + + const val CMD_AUDIO_CONTROL: Int = 15 + + const val CMD_OPEN_IMU: Int = 19 + + const val EVENT_CLICK: Int = 0 + + const val EVENT_SCROLL_TOP: Int = 1 + + const val EVENT_SCROLL_BOTTOM: Int = 2 + + const val EVENT_DOUBLE_CLICK: Int = 3 + + const val EVENT_FOREGROUND_ENTER: Int = 4 + + const val EVENT_FOREGROUND_EXIT: Int = 5 + + const val EVENT_ABNORMAL_EXIT: Int = 6 + + const val EVENT_SYSTEM_EXIT: Int = 7 + + const val EVENT_IMU_DATA_REPORT: Int = 8 + + const val EVENT_RING_LONG_PRESS: Int = 9 + + const val EVENT_RING_LONG_PRESS_RELEASE: Int = 10 + + const val EVENT_SHORT_THEN_LONG_PRESS: Int = 11 + + const val EVENT_HEAD_UP: Int = 12 + + const val EVENT_SOURCE_GLASSES_R: Int = 1 + + const val EVENT_SOURCE_RING: Int = 2 + + const val EVENT_SOURCE_GLASSES_L: Int = 3 + + const val CCCD_UUID: String = "00002902-0000-1000-8000-00805f9b34fb" + + @JvmField val PRELUDE_F5872_PAYLOAD: ByteArray = buildPreludeF5872Payload() + + @JvmField + val PRELUDE_F5872: ByteArray = + framePb(PRELUDE_F5872_PAYLOAD, SID_APP_LAUNCH, FLAG_REQUEST, 0x92).first() + + private fun buildPreludeF5872Payload(): ByteArray { + var field4: ByteArray = + encodeMessageField( + 3, + encodeMessageField( + 2, + encodeMessageField( + 2, + concat( + CollectionUtils.listOf( + encodeVarintField(1, 0), + encodeVarintField(2, 0), + ) + ), + ), + ), + ) + return concat( + CollectionUtils.listOf( + encodeVarintField(1, 2), + encodeVarintField(2, PRELUDE_ACK_MAGIC), + encodeMessageField(4, field4), + ) + ) + } + + @JvmStatic + @JvmOverloads + fun framePb( + pb: ByteArray, + sid: Int, + flag: Int, + seq: Int, + maxWrite: Int = 240, + ): MutableList { + val chunkSize = minOf(232, maxWrite - 8) + require(chunkSize >= 12) { "Negotiated BLE write size is too small" } + var crc: ByteArray = crcBytesLe(pb) + var totalWithCrc: Int = (pb.size + 2) + var totalFrags: Int = + maxOf(1, (kotlin.math.ceil((totalWithCrc / (chunkSize).toDouble()))).toInt()) + require(totalFrags <= 255) { "Protocol message exceeds the fragment limit" } + var frames: MutableList = ArrayList(totalFrags) + var payload: ByteArray = pb.copyOf(totalWithCrc) + payload[pb.size] = crc[0] + payload[(pb.size + 1)] = crc[1] + run { + var i: Int = 0 + while ((i < totalFrags)) { + var offset: Int = (i * chunkSize) + var chunk: ByteArray = + payload.copyOfRange(offset, minOf((offset + chunkSize), payload.size)) + var frame: ByteArray = ByteArray((8 + chunk.size)) + frame[0] = (0xaa).toByte() + frame[1] = 0x21 + frame[2] = ((seq and 0xff)).toByte() + frame[3] = ((chunk.size and 0xff)).toByte() + frame[4] = ((totalFrags and 0xff)).toByte() + frame[5] = (((i + 1) and 0xff)).toByte() + frame[6] = ((sid and 0xff)).toByte() + frame[7] = ((flag and 0xff)).toByte() + chunk.copyInto(frame, 8, 0, 0 + chunk.size) + frames.add(frame) + i++ + } + } + return frames + } + + /** Retain the text container for forwarded inputs, without an image container. */ + @JvmStatic + fun buildCreateInputPage(magic: Int): ByteArray { + var innerParts: MutableList = ArrayList() + innerParts.add(encodeVarintField(1, 1)) + innerParts.add( + encodeMessageField(3, encodeTextObject("dashboard", 1, 0, 0, 576, 288, " ", true)) + ) + innerParts.add(encodeVarintField(5, 10000)) + var inner: ByteArray = concat(innerParts) + return wrapEvenHub(0, magic, 3, inner) + } + + @JvmStatic + fun buildDashboardTextUpgrade(magic: Int): ByteArray { + var inner: MutableList = ArrayList() + inner.add(encodeVarintField(1, 1)) + inner.add(encodeStringField(2, "dashboard")) + inner.add(encodeVarintField(3, 0)) + inner.add(encodeVarintField(4, 1)) + inner.add(encodeStringField(5, " ")) + return wrapEvenHub(5, magic, 9, concat(inner)) + } + + @JvmStatic + fun buildImageRawData( + tile: ImageTileOptions, + sessionId: Int, + totalSize: Int, + fragment: ImageFragment, + magic: Int, + ): ByteArray { + var inner: MutableList = ArrayList() + inner.add(encodeVarintField(1, tile.containerId)) + inner.add(encodeStringField(2, tile.name)) + inner.add(encodeVarintField(3, tile.containerId)) + inner.add(encodeVarintField(4, totalSize)) + inner.add(encodeVarintField(5, 0)) + inner.add(encodeVarintField(6, fragment.index)) + inner.add(encodeVarintField(7, fragment.size)) + inner.add(encodeBytesField(8, fragment.data)) + return wrapEvenHub(3, magic, 5, concat(inner)) + } + + @JvmStatic + fun buildHeartbeat(magic: Int): ByteArray { + return wrapEvenHub(12, magic, 14, encodeVarintField(1, 0)) + } + + /** + * Cmd=0 CreateStartUpPage carrying a text container (the message) plus a list container + * (the selectable rows) in one page. Uses stock-firmware-safe geometry (each container + * within the stock ~280x130 container-size cap; the CFW is what lifts that to 576x288), so + * this works before flashing. The selected row comes back as an async list event — see + * parseListSelection. + */ + @JvmStatic + fun buildCreatePromptPage( + magic: Int, + textName: String, + textContainerId: Int, + warningText: String, + listName: String, + listContainerId: Int, + items: Array, + ): ByteArray { + var inner: MutableList = ArrayList() + inner.add(encodeVarintField(1, 2)) + inner.add( + encodeMessageField( + 2, + encodeListObject(listName, listContainerId, 0, 150, 280, 120, items, true), + ) + ) + inner.add( + encodeMessageField( + 3, + encodeTextObject(textName, textContainerId, 0, 0, 280, 130, warningText, false), + ) + ) + inner.add(encodeVarintField(5, 10000)) + return wrapEvenHub(0, magic, 3, concat(inner)) + } + + @JvmStatic + fun encodeListObject( + name: String, + containerId: Int, + x: Int, + y: Int, + width: Int, + height: Int, + items: Array, + captureEvents: Boolean, + ): ByteArray { + var parts: MutableList = ArrayList() + parts.add(encodeVarintField(1, x)) + parts.add(encodeVarintField(2, y)) + parts.add(encodeVarintField(3, width)) + parts.add(encodeVarintField(4, height)) + parts.add(encodeVarintField(9, containerId)) + parts.add(encodeStringField(10, name)) + parts.add(encodeMessageField(11, encodeListItemContainer(items))) + if (captureEvents) { + parts.add(encodeVarintField(12, 1)) + } + return concat(parts) + } + + private fun encodeListItemContainer(items: Array): ByteArray { + var parts: MutableList = ArrayList() + parts.add(encodeVarintField(1, items.size)) + parts.add(encodeVarintField(3, 1)) + for (item in items) { + parts.add(encodeStringField(4, item)) + } + return concat(parts) + } + + /** + * Decode a list-selection async event (sid=0xe0, flag=0x01/0x06). Returns null if the frame + * is not a list event. `itemIndex` is 0-based into the ItemName array the list was built + * with; `eventType` is EVENT_CLICK for a confirmed selection (scroll/highlight changes + * arrive with other types). + */ + @JvmStatic + fun parseListSelection(frame: ParsedFrame): ListSelection? { + var pb: ByteArray = stripTrailingCrc(frame.pb) + var deviceEvent: ByteArray? = readFieldBytes(pb, 13) + if ((deviceEvent == null)) { + return null + } + var listEvent: ByteArray? = readFieldBytes(deviceEvent, 1) + if ((listEvent == null)) { + return null + } + return ListSelection( + readStringFieldValue(listEvent, 2), + readStringFieldValue(listEvent, 3), + readVarintFieldValue(listEvent, 4, -1), + readVarintFieldValue(listEvent, 5, EVENT_CLICK), + ) + } + + @JvmStatic + fun buildAudioControl(magic: Int, enable: Boolean): ByteArray { + return wrapEvenHub( + CMD_AUDIO_CONTROL, + magic, + 18, + encodeVarintField(1, (if (enable) 1 else 0)), + ) + } + + @JvmStatic + fun buildShutdown(magic: Int, exitMode: Int): ByteArray { + return wrapEvenHub(9, magic, 11, encodeVarintField(1, exitMode)) + } + + /** + * IMU control (ImuCtrl = field 22 of the main ctx). IMU_CtrlCmd carries IMUReportEn + * (field 1) and reportFrq (field 2). reportFrq is only meaningful when enabling; on disable + * we send just the enable flag. + */ + @JvmStatic + fun buildImuControl(magic: Int, enable: Boolean, reportFrq: Int): ByteArray { + var inner: MutableList = ArrayList() + inner.add(encodeVarintField(1, (if (enable) 1 else 0))) + if ((enable && (reportFrq > 0))) { + inner.add(encodeVarintField(2, reportFrq)) + } + return wrapEvenHub(CMD_OPEN_IMU, magic, 22, concat(inner)) + } + + /** + * Set the lens brightness: G2SettingPackage{commandId=1 (DeviceReceiveInfo), + * deviceReceiveInfoFromApp(3){deviceReceiveBrightness(1){autoAdjust(1), + * brightnessLevel(2)}}} on sid 0x09. brightnessLevel is 0-100 (nonlinear; 0 is + * dim-but-visible, not off). When autoAdjust is set the ambient-light sensor drives + * brightness, so the level field is omitted; otherwise the level is always encoded + * (explicit zero included) so brightnessLevel=0 reaches the wire. + */ + @JvmStatic + fun buildSetBrightness(magic: Int, autoAdjust: Boolean, brightnessLevel: Int): ByteArray { + var brightness: MutableList = ArrayList() + brightness.add(encodeVarintField(1, (if (autoAdjust) 1 else 0))) + if (!autoAdjust) { + brightness.add(encodeVarintField(2, brightnessLevel)) + } + return concat( + CollectionUtils.listOf( + encodeVarintField(1, 1), + encodeVarintField(2, magic), + encodeMessageField(3, encodeMessageField(1, concat(brightness))), + ) + ) + } + + /** Enable the firmware's wear detector (DeviceReceiveInfoFromAPP field 5). */ + @JvmStatic + fun buildSetWearDetection(magic: Int, enabled: Boolean): ByteArray { + var wear: ByteArray = encodeVarintField(1, (if (enabled) 1 else 0)) + return concat( + CollectionUtils.listOf( + encodeVarintField(1, 1), + encodeVarintField(2, magic), + encodeMessageField(3, encodeMessageField(5, wear)), + ) + ) + } + + /** + * The application authentication request: DEVICE_SETTINGS/AUTHENTICATION(4) on sid 0x80, + * flag 0x00. Carries no key material — it arms the firmware's "security auth" flag; the + * firmware sends the success notification once the flag is set AND the BLE link is + * encrypted (SMP pairing/bond restoration, handled by the OS below GATT, possibly after a + * pairing prompt). Protobuf: f1=4 (command), f2=magic, f3={f1=1, f2=4}. + */ + @JvmStatic + fun buildAuthenticationRequest(magic: Int): ByteArray { + var auth: ByteArray = + concat( + CollectionUtils.listOf( + encodeVarintField(1, 1), + encodeVarintField(2, SECURITY_AUTH_CMD), + ) + ) + return concat( + CollectionUtils.listOf( + encodeVarintField(1, SECURITY_AUTH_CMD), + encodeVarintField(2, magic), + encodeMessageField(3, auth), + ) + ) + } + + /** + * True when `pb` (trailing CRC still attached) is the success notification for the + * authentication request sent with `magic`: command 4, the same magic, and an EMPTY field-3 + * result message (`1a 00` — the omitted result code defaults to zero). A GATT write + * completing is NOT success; only this notification is. + */ + @JvmStatic + fun isAuthenticationSuccess(pb: ByteArray?, magic: Int): Boolean { + if ((pb == null)) { + return false + } + var root: ByteArray = stripTrailingCrc(pb) + if ((readVarintFieldValue(root, 1, -1) != SECURITY_AUTH_CMD)) { + return false + } + if ((readVarintFieldValue(root, 2, -1) != magic)) { + return false + } + var result: ByteArray? = readFieldBytes(root, 3) + return ((result != null) && (result.size == 0)) + } + + @JvmStatic + fun buildSettingsQuery(magic: Int): ByteArray { + var request: ByteArray = encodeVarintField(1, 1) + return concat( + CollectionUtils.listOf( + encodeVarintField(1, 2), + encodeVarintField(2, magic), + encodeMessageField(4, request), + ) + ) + } + + /** + * Build the CFW-only wake lease control package. Magic zero deliberately makes this + * fire-and-forget: the firmware consumes field 101 before its stock nanopb decoder discards + * the unknown field. + */ + @JvmStatic + fun buildFaceclawWakeControl(operation: Int, nonce: Int): ByteArray { + var control: ByteArray = + byteArrayOf( + 70.toByte(), + 67.toByte(), + (FACECLAW_WAKE_PROTOCOL_VERSION).toByte(), + (operation).toByte(), + ((nonce and 0xff)).toByte(), + (((nonce ushr 8) and 0xff)).toByte(), + ) + return concat( + CollectionUtils.listOf( + encodeVarintField(1, 1), + encodeVarintField(2, 0), + encodeBytesField(FACECLAW_WAKE_CONTROL_FIELD, control), + ) + ) + } + + /** + * Wrap an already-encoded [77.toByte(),67.toByte(),ver,op,...] mic-control record as a + * settings write carrying CFW field 103. Magic zero makes it fire-and-forget like the wake + * lease: the firmware consumes the field before its stock nanopb decoder discards the + * unknown tag, and the "ack" is the field-104 status notify that follows. + */ + @JvmStatic + fun buildFaceclawMicControl(record: ByteArray): ByteArray { + return concat( + CollectionUtils.listOf( + encodeVarintField(1, 1), + encodeVarintField(2, 0), + encodeBytesField(FACECLAW_MIC_CONTROL_FIELD, record), + ) + ) + } + + /** + * Extract the CFW mic status record (field 104) from a settings frame, or null when absent. + * The 21-byte body layout is decoded on the TS side; here we only validate the + * 77.toByte(),67.toByte(),version prelude. + */ + @JvmStatic + fun parseFaceclawMicStatus(pb: ByteArray?): ByteArray? { + if ((pb == null)) { + return null + } + var body: ByteArray? = readFieldBytes(stripTrailingCrc(pb), FACECLAW_MIC_STATUS_FIELD) + if ( + ((((body == null) || (body.size < 3)) || (body[0].toInt() != 77)) || + (body[1].toInt() != 67)) + ) { + return null + } + return body + } + + /** + * Return the raw CFW ambient-light report from a sid-0x09 frame, or null when this is an + * ordinary settings frame. Only the 65.toByte(),76.toByte(),version prelude is validated + * here; the fields are decoded on the TS side. + */ + @JvmStatic + fun parseFaceclawAlsReport(pb: ByteArray?): ByteArray? { + if ((pb == null)) { + return null + } + var body: ByteArray? = readFieldBytes(stripTrailingCrc(pb), FACECLAW_ALS_REPORT_FIELD) + if ( + (((((body == null) || (body.size < 24)) || (body[0].toInt() != 65)) || + (body[1].toInt() != 76)) || ((body[2] and 0xff) != 1)) + ) { + return null + } + return body + } + + /** Field 106: RB/version 1/flags/percentage, from the CFW ring-battery cache. */ + @JvmStatic + fun parseRingBattery(pb: ByteArray?): RingBatterySnapshot? { + if ((pb == null)) { + return null + } + var body: ByteArray? = readFieldBytes(stripTrailingCrc(pb), 106) + if ( + (((((body == null) || (body.size != 5)) || (body[0].toInt() != 82)) || + (body[1].toInt() != 66)) || (body[2].toInt() != 1)) + ) { + return null + } + var flags: Int = (body[3] and 0xff) + var level: Int = (body[4] and 0xff) + if (((flags and 7.inv()) != 0)) { + return null + } + if (((flags and 2) != 0)) { + if ((((flags and 1) == 0) || (level > 100))) { + return null + } + return RingBatterySnapshot(level, (if (((flags and 4) != 0)) 1 else 0)) + } + if (((level != 255) || ((flags and 4) != 0))) { + return null + } + return RingBatterySnapshot(-1, -1) + } + + /** + * Return the uint16 wake nonce from a CFW field-102 deferred-wake notification (double tap + * or head-up), or -1 when this is an ordinary settings frame (idle-gesture events + * included). + */ + @JvmStatic + fun parseFaceclawWakeEvent(pb: ByteArray): Int { + var event: ByteArray? = faceclawWakeEventBytes(pb) + return (if ((event == null)) -1 + else ((event[4] and 0xff) or ((event[5] and 0xff) shl 8))) + } + + /** + * Which gesture a CFW deferred-wake notification reports: 1 (double tap) or + * FACECLAW_WAKE_EVENT_HEAD_UP; -1 when the frame is not a wake event. + */ + @JvmStatic + fun parseFaceclawWakeEventCode(pb: ByteArray): Int { + var event: ByteArray? = faceclawWakeEventBytes(pb) + return (if ((event == null)) -1 else (event[3] and 0xff)) + } + + private fun faceclawWakeEventBytes(pb: ByteArray?): ByteArray? { + if ((pb == null)) { + return null + } + var event: ByteArray? = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD) + if ( + (((((event == null) || (event.size != 6)) || (event[0].toInt() != 70)) || + (event[1].toInt() != 67)) || + ((event[2] and 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION)) + ) { + return null + } + var code: Int = (event[3] and 0xff) + return (if (((code == FACECLAW_WAKE_EVENT) || (code == FACECLAW_WAKE_EVENT_HEAD_UP))) + event + else null) + } + + /** + * Decode a CFW field-102 idle-gesture event (tap, long press, release while no app is on + * screen) into the EvenHub sys-event type and source the same gesture would carry from a + * live page, or null when the frame is not one (ordinary settings frames and the double-tap + * wake event included). + */ + @JvmStatic + fun parseFaceclawGestureEvent(pb: ByteArray?): FaceclawGestureEvent? { + if ((pb == null)) { + return null + } + var event: ByteArray? = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD) + if ( + (((((event == null) || (event.size != 6)) || (event[0].toInt() != 70)) || + (event[1].toInt() != 67)) || + ((event[2] and 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION)) + ) { + return null + } + var eventType: Int + when ((event[3] and 0xff)) { + FACECLAW_GESTURE_EVENT_TAP -> { + eventType = EVENT_CLICK + } + FACECLAW_GESTURE_EVENT_LONG_PRESS -> { + eventType = EVENT_RING_LONG_PRESS + } + FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE -> { + eventType = EVENT_RING_LONG_PRESS_RELEASE + } + else -> { + return null + } + } + var eventSource: Int + when ((event[4] and 0xff)) { + FACECLAW_RAW_SOURCE_LEFT_TEMPLE -> { + eventSource = EVENT_SOURCE_GLASSES_L + } + FACECLAW_RAW_SOURCE_RIGHT_TEMPLE -> { + eventSource = EVENT_SOURCE_GLASSES_R + } + FACECLAW_RAW_SOURCE_RING -> { + eventSource = EVENT_SOURCE_RING + } + else -> { + eventSource = 0 + } + } + return FaceclawGestureEvent(eventType, eventSource) + } + + /** + * Decode OnboardingDataPackage EVENT/GLS_WEAR_STATUS. Unlike most async G2 traffic, stock + * firmware sends this event with flag 0x00, so callers must recognize it by sid and + * protobuf shape rather than notify flag. Returns 0/1, or -1 when the frame is not a wear + * event. + */ + @JvmStatic + fun parseWearState(frame: ParsedFrame?): Int { + if ((((frame == null) || !frame.ok) || (frame.sid != SID_ONBOARDING))) { + return -1 + } + var root: ByteArray = stripTrailingCrc(frame.pb) + if ((readVarintFieldValue(root, 1, -1) != 3)) { + return -1 + } + var event: ByteArray? = readFieldBytes(root, 5) + if (((event == null) || (readVarintFieldValue(event, 1, -1) != 1))) { + return -1 + } + var wearStatus: Int = readVarintFieldValue(event, 2, -1) + return (if (((wearStatus == 0) || (wearStatus == 1))) wearStatus else -1) + } + + @JvmStatic + fun parseSettingsBattery(pb: ByteArray): BatterySnapshot? { + var root: ByteArray = stripTrailingCrc(pb) + var request: ByteArray? = readFieldBytes(root, 4) + if ((request == null)) { + return null + } + var battery: Int = readVarintFieldValue(request, 12, -1) + var charging: Int = readVarintFieldValue(request, 13, -1) + var silentMode: Int = readVarintFieldValue(request, 14, -1) + if ((battery < 0)) { + return null + } + return BatterySnapshot(battery, charging, silentMode) + } + + /** + * Silent mode from an unsolicited settings push. The wearer toggles silent mode by + * long-pressing both touchpads at once; the firmware then sends + * G2SettingPackage{commandId=3 (DeviceSendToAPP), deviceSendInfoToApp(5){ + * silentModeSwitch(2)}} on sid 0x09, with a magic it picked itself, so this arrives outside + * the request/ack flow. + * + * Returns -1 when the frame is not such a push. Ordinary settings-read acks (the battery + * poll) carry commandId=2 and deviceReceiveRequestFromApp in field 4, so they never match + * and stay on the normal ack path. + */ + @JvmStatic + fun parseSilentModePush(pb: ByteArray?): Int { + if ((pb == null)) { + return -1 + } + var root: ByteArray = stripTrailingCrc(pb) + if ((readVarintFieldValue(root, 1, -1) != SETTINGS_CMD_DEVICE_SEND_TO_APP)) { + return -1 + } + var info: ByteArray? = readFieldBytes(root, 5) + if ((info == null)) { + return -1 + } + return readVarintFieldValue(info, 2, -1) + } + + /** + * Firmware versions and the firmware-extension string from a settings READ ack. Versions + * are fields 5/6 of the deviceReceiveRequestFromApp submessage (field 4); Faceclaw's custom + * firmware additionally appends top-level field 100 with its revision ("Faceclaw/"; + * older builds sent "EVENCFW/ "), which stock firmware never sends. Returns + * null when the ack carries none of it. Compatibility is judged on the TS side + * (app/g2/firmware-compat.ts); Java only needs to know whether the firmware is ours at all. + */ + @JvmStatic + fun parseSettingsFirmwareInfo(pb: ByteArray): FirmwareInfo? { + var root: ByteArray = stripTrailingCrc(pb) + var request: ByteArray? = readFieldBytes(root, 4) + if ((request == null)) { + return null + } + var leftVersion: String = readStringFieldValue(request, 5) + var rightVersion: String = readStringFieldValue(request, 6) + var extension: String = readStringFieldValue(root, 100) + if (((leftVersion.isEmpty() && rightVersion.isEmpty()) && extension.isEmpty())) { + return null + } + return FirmwareInfo(leftVersion, rightVersion, extension) + } + + @JvmStatic + fun wrapEvenHub(cmd: Int, magic: Int, innerFieldNumber: Int, inner: ByteArray): ByteArray { + var parts: MutableList = ArrayList() + parts.add(encodeVarintField(1, cmd)) + parts.add(encodeVarintField(2, magic)) + parts.add(encodeMessageField(innerFieldNumber, inner)) + return concat(parts) + } + + @JvmStatic + fun encodeTextObject( + name: String, + containerId: Int, + x: Int, + y: Int, + width: Int, + height: Int, + text: String, + captureEvents: Boolean, + ): ByteArray { + var parts: MutableList = ArrayList() + parts.add(encodeVarintField(1, x)) + parts.add(encodeVarintField(2, y)) + parts.add(encodeVarintField(3, width)) + parts.add(encodeVarintField(4, height)) + parts.add(encodeVarintField(9, containerId)) + parts.add(encodeStringField(10, name)) + if (captureEvents) { + parts.add(encodeVarintField(11, 1)) + } + parts.add(encodeStringField(12, text)) + return concat(parts) + } + + @JvmStatic + fun encodeImageObject(tile: ImageTileOptions): ByteArray { + var parts: MutableList = ArrayList() + parts.add(encodeVarintField(1, tile.x)) + parts.add(encodeVarintField(2, tile.y)) + parts.add(encodeVarintField(3, tile.width)) + parts.add(encodeVarintField(4, tile.height)) + parts.add(encodeVarintField(5, tile.containerId)) + parts.add(encodeStringField(6, tile.name)) + return concat(parts) + } + + private fun concat(parts: MutableList): ByteArray { + var out: ByteSink = ByteSink() + for (part in parts) { + if (((part != null) && (part.size > 0))) { + out.write(part, 0, part.size) + } + } + return out.toByteArray() + } + + internal fun encodeVarintField(fieldNumber: Int, value: Int): ByteArray { + return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 0), encodeVarint(value))) + } + + private fun encodeStringField(fieldNumber: Int, value: String?): ByteArray { + var bytes: ByteArray = + (if ((value == null)) ByteArray(0) else value.encodeToByteArray()) + return concat( + CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.size), bytes) + ) + } + + internal fun encodeBytesField(fieldNumber: Int, value: ByteArray?): ByteArray { + var bytes: ByteArray = (if ((value == null)) ByteArray(0) else value) + return concat( + CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.size), bytes) + ) + } + + private fun encodeMessageField(fieldNumber: Int, value: ByteArray?): ByteArray { + var bytes: ByteArray = (if ((value == null)) ByteArray(0) else value) + return concat( + CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.size), bytes) + ) + } + + private fun encodeKey(fieldNumber: Int, wireType: Int): ByteArray { + return encodeVarint(((fieldNumber shl 3) or wireType)) + } + + private fun encodeVarint(value: Int): ByteArray { + var out: ByteSink = ByteSink() + var v: Int = (value ushr 0) + while ((v >= 0x80)) { + out.write(((v and 0x7f) or 0x80)) + v = (v ushr 7) + } + out.write(v) + return out.toByteArray() + } + + private fun crcBytesLe(data: ByteArray): ByteArray { + var crc: Int = 0xffff + for (b in data) { + crc = (crc xor ((b and 0xff) shl 8)) + run { + var i: Int = 0 + while ((i < 8)) { + if (((crc and 0x8000) != 0)) { + crc = (((crc shl 1) xor 0x1021) and 0xffff) + } else { + crc = ((crc shl 1) and 0xffff) + } + i++ + } + } + } + return byteArrayOf(((crc and 0xff)).toByte(), (((crc ushr 8) and 0xff)).toByte()) + } + + /** + * 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. + */ + @JvmStatic + fun splitFrames(value: ByteArray?): MutableList { + var out: MutableList = ArrayList() + if (((value == null) || (value.size == 0))) { + return out + } + var offset: Int = 0 + while ( + ((((offset + 8) <= value.size) && (value[offset] == (0xaa).toByte())) && + ((value[(offset + 1)].toInt() == 0x21) || + (value[(offset + 1)].toInt() == 0x12))) + ) { + var len: Int = (value[(offset + 3)] and 0xff) + var end: Int = minOf(value.size, ((offset + 8) + len)) + out.add(value.copyOfRange(offset, end)) + offset = end + } + if ((offset < value.size)) { + out.add(value.copyOfRange(offset, value.size)) + } + return out + } + + @JvmStatic + fun parseFrame(buf: ByteArray?): ParsedFrame { + if ( + ((((buf == null) || (buf.size < 10)) || (buf[0] != (0xaa).toByte())) || + ((buf[1].toInt() != 0x21) && (buf[1].toInt() != 0x12))) + ) { + return ParsedFrame(false, 0, 0, ByteArray(0), -1, -1) + } + var sid: Int = (buf[6] and 0xff) + var flag: Int = (buf[7] and 0xff) + var len: Int = (buf[3] and 0xff) + var end: Int = minOf(buf.size, (8 + len)) + var pb: ByteArray = buf.copyOfRange(8, end) + var msgType: Int = -1 + var msgSeq: Int = -1 + var offset: Int = 0 + run { + var i: Int = 0 + while (((i < 8) && (offset < pb.size))) { + var key: VarintResult? = readVarint(pb, offset) + if ((key == null)) { + break + } + offset = key.next + var tag: Int = (key.value shr 3) + var wire: Int = (key.value and 7) + if ((wire == 0)) { + var value: VarintResult? = readVarint(pb, offset) + if ((value == null)) { + break + } + if ((tag == 1)) { + msgType = value.value + } else { + if ((tag == 2)) { + msgSeq = value.value + } + } + offset = value.next + } else { + if ((wire == 2)) { + var length: VarintResult? = readVarint(pb, offset) + if ((length == null)) { + break + } + offset = (length.next + length.value) + } else { + break + } + } + if (((msgType >= 0) && (msgSeq >= 0))) { + break + } + i++ + } + } + return ParsedFrame(true, sid, flag, pb, msgType, msgSeq) + } + + @JvmStatic + fun stripTrailingCrc(pb: ByteArray?): ByteArray { + if (((pb == null) || (pb.size < 2))) { + return (if ((pb == null)) ByteArray(0) else pb) + } + return pb.copyOf((pb.size - 2)) + } + + /** Decode the stock firmware's compass navigation notifications (sid 0x08). */ + @JvmStatic + fun parseCompassEvent(frame: ParsedFrame?): CompassEvent? { + if ( + ((((frame == null) || !frame.ok) || (frame.sid != SID_NAVIGATION)) || + ((frame.flag != FLAG_NOTIFY) && (frame.flag != FLAG_NOTIFY_ALT))) + ) { + return null + } + var root: ByteArray = stripTrailingCrc(frame.pb) + var command: Int = readVarintFieldValue(root, 1, -1) + if ((command == NAV_CMD_COMPASS_CHANGED)) { + var compass: ByteArray? = readFieldBytes(root, 10) + if ((compass == null)) { + return null + } + var heading: Int = readVarintFieldValue(compass, 1, -1) + if (((heading < 0) || (heading >= 360))) { + return null + } + var diagnostic: ByteArray? = readFieldBytes(root, 100) + if ( + (((((((((diagnostic != null) && (diagnostic.size == 12)) && + (diagnostic[0].toInt() == 67)) && (diagnostic[1].toInt() == 77)) && + (diagnostic[2].toInt() == 1)) && (diagnostic[7].toInt() == 0)) && + (((diagnostic[3] and 0xff) <= 3) || (diagnostic[3] == (255).toByte()))) && + (((diagnostic[4] and 0xff) <= 2) || (diagnostic[4] == (255).toByte()))) && + ((diagnostic[5] and 0xff) <= 3)) + ) { + var sampleTimeMs: Long = 0 + run { + var i: Int = 0 + while ((i < 4)) { + sampleTimeMs = + (sampleTimeMs or + (((diagnostic[(8 + i)] and 0xff)).toLong() shl (8 * i))) + i++ + } + } + return CompassEvent( + command, + heading, + (if ((diagnostic[3] == (255).toByte())) -1 else (diagnostic[3] and 0xff)), + (if ((diagnostic[4] == (255).toByte())) -1 else (diagnostic[4] and 0xff)), + (diagnostic[5] and 0xff), + (diagnostic[6] and 0xff), + sampleTimeMs, + ) + } + return CompassEvent(command, heading) + } + if ( + ((command == NAV_CMD_COMPASS_CALIBRATION_STARTED) || + (command == NAV_CMD_COMPASS_CALIBRATION_COMPLETE)) + ) { + return CompassEvent(command, -1) + } + return null + } + + /** + * Display wake notification observed after a ring or right-arm double tap while no EvenHub + * page is running: + * + * sid=0x0d, flag=notify, payload f1=1, f3={f1=1} + * + * Other state-change shapes (wear, charging, head-up, etc.) are deliberately excluded. The + * caller additionally gates this on an intentional EvenHub suspension, so normal state + * traffic cannot become application input. + */ + @JvmStatic + fun isDisplayWakeStateChange(frame: ParsedFrame?): Boolean { + if ( + ((((frame == null) || !frame.ok) || (frame.sid != SID_STATE_CHANGE)) || + ((frame.flag != FLAG_NOTIFY) && (frame.flag != FLAG_NOTIFY_ALT))) + ) { + return false + } + var root: ByteArray = stripTrailingCrc(frame.pb) + if ((readVarintFieldValue(root, 1, -1) != 1)) { + return false + } + var state: ByteArray? = readFieldBytes(root, 3) + return (((state != null) && (state.size == 2)) && + (readVarintFieldValue(state, 1, -1) == 1)) + } + + @JvmStatic + fun readFieldBytes(pb: ByteArray, fieldNumber: Int): ByteArray? { + var offset: Int = 0 + while ((offset < pb.size)) { + var key: VarintResult? = readVarint(pb, offset) + if ((key == null)) { + return null + } + offset = key.next + var field: Int = (key.value shr 3) + var wire: Int = (key.value and 0x07) + if ((wire == 0)) { + var value: VarintResult? = readVarint(pb, offset) + if ((value == null)) { + return null + } + offset = value.next + continue + } + if ((wire == 1)) { + offset += 8 + continue + } + if ((wire == 5)) { + offset += 4 + continue + } + if ((wire != 2)) { + return null + } + var length: VarintResult? = readVarint(pb, offset) + if ((length == null)) { + return null + } + offset = length.next + var end: Int = (offset + length.value) + if ((end > pb.size)) { + return null + } + var bytes: ByteArray = pb.copyOfRange(offset, end) + offset = end + if ((field == fieldNumber)) { + return bytes + } + } + return null + } + + @JvmStatic + fun readVarintFieldValue(pb: ByteArray, fieldNumber: Int, defaultValue: Int): Int { + var offset: Int = 0 + while ((offset < pb.size)) { + var key: VarintResult? = readVarint(pb, offset) + if ((key == null)) { + return defaultValue + } + offset = key.next + var field: Int = (key.value shr 3) + var wire: Int = (key.value and 0x07) + if ((wire == 0)) { + var value: VarintResult? = readVarint(pb, offset) + if ((value == null)) { + return defaultValue + } + offset = value.next + if ((field == fieldNumber)) { + return value.value + } + continue + } + if ((wire == 1)) { + offset += 8 + continue + } + if ((wire == 5)) { + offset += 4 + continue + } + if ((wire != 2)) { + return defaultValue + } + var length: VarintResult? = readVarint(pb, offset) + if ((length == null)) { + return defaultValue + } + offset = (length.next + length.value) + } + return defaultValue + } + + /** + * Read a `float` (protobuf wire type 5, little-endian fixed32) field. The g2-kit proto + * declares IMU_Report_Data's x/y/z as `double`, but the firmware actually sends them as + * fixed32 floats (confirmed from captures). Returns defaultValue if absent. + */ + @JvmStatic + fun readFloatFieldValue(pb: ByteArray, fieldNumber: Int, defaultValue: Float): Float { + var offset: Int = 0 + while ((offset < pb.size)) { + var key: VarintResult? = readVarint(pb, offset) + if ((key == null)) { + return defaultValue + } + offset = key.next + var field: Int = (key.value shr 3) + var wire: Int = (key.value and 0x07) + if ((wire == 0)) { + var value: VarintResult? = readVarint(pb, offset) + if ((value == null)) { + return defaultValue + } + offset = value.next + } else { + if ((wire == 1)) { + offset += 8 + } else { + if ((wire == 5)) { + if (((offset + 4) > pb.size)) { + return defaultValue + } + if ((field == fieldNumber)) { + var bits: Int = 0 + run { + var i: Int = 0 + while ((i < 4)) { + bits = (bits or ((pb[(offset + i)] and 0xff) shl (8 * i))) + i++ + } + } + return Float.fromBits(bits) + } + offset += 4 + } else { + if ((wire == 2)) { + var length: VarintResult? = readVarint(pb, offset) + if ((length == null)) { + return defaultValue + } + offset = (length.next + length.value) + } else { + return defaultValue + } + } + } + } + } + return defaultValue + } + + @JvmStatic + fun readStringFieldValue(pb: ByteArray, fieldNumber: Int): String { + var bytes: ByteArray? = readFieldBytes(pb, fieldNumber) + if ((bytes == null)) { + return "" + } + return bytes.decodeToString() + } + + @JvmStatic + fun readVarint(pb: ByteArray, offset: Int): VarintResult? { + var value: Int = 0 + var shift: Int = 0 + var cursor: Int = offset + while ((cursor < pb.size)) { + var b: Int = (pb[cursor++] and 0xff) + value = (value or ((b and 0x7f) shl shift)) + if (((b and 0x80) == 0)) { + return VarintResult(value, cursor) + } + shift += 7 + } + return null + } + } + + class RingBatterySnapshot { + @JvmField val battery: Int + + @JvmField val charging: Int + + constructor(battery: Int, charging: Int) { + this.battery = battery + this.charging = charging + } + } + + /** An idle gesture from a CFW field-102 notification, in EvenHub sys-event terms. */ + class FaceclawGestureEvent { + @JvmField val eventType: Int + + @JvmField val eventSource: Int + + constructor(eventType: Int, eventSource: Int) { + this.eventType = eventType + this.eventSource = eventSource + } + } + + class ParsedFrame { + @JvmField val ok: Boolean + + @JvmField val sid: Int + + @JvmField val flag: Int + + @JvmField val pb: ByteArray + + @JvmField val msgType: Int + + @JvmField val msgSeq: Int + + constructor(ok: Boolean, sid: Int, flag: Int, pb: ByteArray, msgType: Int, msgSeq: Int) { + this.ok = ok + this.sid = sid + this.flag = flag + this.pb = pb + this.msgType = msgType + this.msgSeq = msgSeq + } + } + + class VarintResult { + @JvmField val value: Int + + @JvmField val next: Int + + constructor(value: Int, next: Int) { + this.value = value + this.next = next + } + } + + class ListSelection { + @JvmField val containerName: String? + + @JvmField val itemName: String? + + @JvmField val itemIndex: Int + + @JvmField val eventType: Int + + constructor(containerName: String?, itemName: String?, itemIndex: Int, eventType: Int) { + this.containerName = (if ((containerName == null)) "" else containerName) + this.itemName = (if ((itemName == null)) "" else itemName) + this.itemIndex = itemIndex + this.eventType = eventType + } + } + + class CompassEvent { + @JvmField val command: Int + + @JvmField val headingDegrees: Int + + /** -1 means unavailable; source: 0 unknown, 1 GRV, 2 GMRV, 3 RV. */ + @JvmField val magneticAccuracy: Int + @JvmField val magneticAnomalies: Int + @JvmField val orientationSource: Int + @JvmField val diagnosticFlags: Int + + @JvmField val sampleTimeMs: Long + + constructor( + command: Int, + headingDegrees: Int, + ) : this(command, headingDegrees, -1, -1, -1, -1, -1) {} + + constructor( + command: Int, + headingDegrees: Int, + magneticAccuracy: Int, + magneticAnomalies: Int, + orientationSource: Int, + diagnosticFlags: Int, + sampleTimeMs: Long, + ) { + this.command = command + this.headingDegrees = headingDegrees + this.magneticAccuracy = magneticAccuracy + this.magneticAnomalies = magneticAnomalies + this.orientationSource = orientationSource + this.diagnosticFlags = diagnosticFlags + this.sampleTimeMs = sampleTimeMs + } + } + + class ImageTileOptions { + @JvmField val name: String + + @JvmField val containerId: Int + + @JvmField val x: Int + + @JvmField val y: Int + + @JvmField val width: Int + + @JvmField val height: Int + + constructor(name: String, containerId: Int, x: Int, y: Int, width: Int, height: Int) { + this.name = name + this.containerId = containerId + this.x = x + this.y = y + this.width = width + this.height = height + } + } + + class ImageFragment { + @JvmField val index: Int + + @JvmField val data: ByteArray + + @JvmField val size: Int + + constructor(index: Int, data: ByteArray, size: Int) { + this.index = index + this.data = data + this.size = size + } + } + + class BatterySnapshot { + @JvmField val battery: Int + + @JvmField val charging: Int + + /** 1 = silent mode on, 0 = off, -1 = the ack didn't carry the field. */ + @JvmField val silentMode: Int + + constructor(battery: Int, charging: Int, silentMode: Int) { + this.battery = battery + this.charging = charging + this.silentMode = silentMode + } + } + + class FirmwareInfo { + companion object { + /** Prefix of the firmware-extension string on Faceclaw's custom firmware. */ + const val FACECLAW_EXTENSION_PREFIX: String = "Faceclaw/" + } + + @JvmField val leftVersion: String + + @JvmField val rightVersion: String + + /** Raw field-100 string ("" on stock firmware). */ + @JvmField val extension: String + + constructor(leftVersion: String, rightVersion: String, extension: String?) { + this.leftVersion = leftVersion + this.rightVersion = rightVersion + this.extension = (if ((extension == null)) "" else extension) + } + + /** + * True when the glasses run Faceclaw's custom firmware (any revision). Whether the revision + * is the one this app needs is decided on the TS side, which disconnects on a mismatch; + * this only guards the private modes against stock or third-party firmware in the meantime. + */ + fun isFaceclawFirmware(): Boolean { + return extension.trim().startsWith(FACECLAW_EXTENSION_PREFIX) + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageWindow.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageWindow.kt new file mode 100644 index 00000000..74868596 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageWindow.kt @@ -0,0 +1,44 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmStatic + +/** Ordered completion and go-back-N recovery for the custom-message window. */ +class CfwMessageWindow private constructor() { + companion object { + // Flappy capture: p99 ACK 63ms, max 77ms. Allow full-frame processing + // while recovering a lost reply well before the stock timeout. + const val ACK_TIMEOUT_MS = 500 + const val MAX_RETRIES = 3 + + @JvmStatic + fun acknowledgedHead(messages: Iterable): OutboundMessage? { + for (message in messages) { + if (message.sid != CfwTransport.SID) continue + return if (!message.cfwRetryPending && message.cfwAckLenses == CfwTransport.BOTH) + message + else null + } + return null + } + + @JvmStatic + fun replayWindow(messages: Iterable, nowMs: Long): List { + val replay = mutableListOf() + var failed = false + for (message in messages) { + if (message.sid != CfwTransport.SID) continue + failed = + failed || + message.cfwRetryPending || + (message.cfwAckLenses != CfwTransport.BOTH && + message.ackDeadlineAtMs > 0 && + message.ackDeadlineAtMs <= nowMs) + replay.add(message) + } + // Replaying only the named failure can strand an older unresolved + // attempt at the head. Rewind the entire unresolved custom window. + if (!failed) replay.clear() + return replay + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt new file mode 100644 index 00000000..0ce887fd --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt @@ -0,0 +1,190 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** Faceclaw/10 records: clear flags, wire length and decoded CRC; persistent zlib body. */ +class CfwTransport(private val platform: ProtocolPlatform) { + private val lock = platform.createLock() + private var deflater: ProtocolDeflater? = null + private var resetPending = true + private var previousLenses = 0 + + fun reset() = lock.withLock { + deflater?.reset() + resetPending = true + previousLenses = 0 + } + + /** Release native compression resources. A later encode starts a fresh stream. */ + fun close() = lock.withLock { + deflater?.close() + deflater = null + resetPending = true + previousLenses = 0 + } + + /** Compress only when writing, in exactly the order seen by the receiver. */ + fun encode(message: ByteArray?, streamId: Int, lenses: Int, mtu: Int): List = + lock.withLock { + validate(message, streamId, lenses, mtu) + val input = message!! + if (previousLenses != lenses) reset() + val reset = resetPending + val encoder = deflater ?: platform.createDeflater().also { deflater = it } + var body = encoder.syncFlush(input) + var flags = lenses or COMPRESSED or (if (reset) RESET_CONTEXT else 0) + if (body.isEmpty()) flags = flags and COMPRESSED.inv() // repeated empty sync flush + if (body.size > MAX_MESSAGE) { + // The attempted deflate advanced history: reset both ends before reuse. + reset() + body = input + flags = lenses or RESET_CONTEXT + } else { + resetPending = false + previousLenses = lenses + } + frameRecord(body, crc(input), streamId, flags, mtu) + } + + companion object { + const val SID = 0xf0 + const val BOTH = 3 + const val MAX_MESSAGE = 65535 + const val COMPRESSED = 4 + const val RESET_CONTEXT = 8 + + private fun validate(message: ByteArray?, streamId: Int, lenses: Int, mtu: Int) { + require( + message != null && + message.size <= MAX_MESSAGE && + streamId in 0..255 && + lenses in 1..BOTH && + mtu in 23..517 + ) { + "invalid CFW stream parameters" + } + } + + /** One logical command per stream. Packets are bounded by actual ATT MTU. */ + @JvmStatic + fun frame(message: ByteArray?, streamId: Int, lenses: Int, mtu: Int): List { + validate(message, streamId, lenses, mtu) + return frameRecord(message!!, crc(message), streamId, lenses or RESET_CONTEXT, mtu) + } + + private fun frameRecord( + body: ByteArray, + checksum: Int, + streamId: Int, + flags: Int, + mtu: Int, + ): List { + val lenses = flags and BOTH + val capacity = minOf(252, mtu - 14) + val stream = ByteArray(body.size + 5) + stream[0] = flags.toByte() + stream[1] = body.size.toByte() + stream[2] = (body.size ushr 8).toByte() + stream[3] = checksum.toByte() + stream[4] = (checksum ushr 8).toByte() + body.copyInto(stream, 5) + val packets = mutableListOf() + for (offset in stream.indices step capacity) { + val count = minOf(capacity, stream.size - offset) + val packet = ByteArray(count + 11) + packet[0] = 0xaa.toByte() + packet[1] = 0x21 + packet[2] = (streamId + packets.size).toByte() + packet[3] = (count + 3).toByte() + packet[4] = 1 + packet[5] = 1 + packet[6] = SID.toByte() + packet[8] = + (lenses or + (if (offset == 0) 0x80 else 0) or + (if (offset + count == stream.size) 0x40 else 0)) + .toByte() + stream.copyInto(packet, 9, offset, offset + count) + val crc = crc(packet, 8, count + 1) + packet[packet.size - 2] = crc.toByte() + packet[packet.size - 1] = (crc ushr 8).toByte() + packets.add(packet) + } + return packets + } + + @JvmStatic fun crc(data: ByteArray): Int = crc(data, 0, data.size) + + private fun crc(data: ByteArray, offset: Int, count: Int): Int { + var crc = 0xffff + for (i in offset until offset + count) { + crc = crc xor ((data[i].toInt() and 255) shl 8) + repeat(8) { + crc = ((crc shl 1) xor (if (crc and 0x8000 != 0) 0x1021 else 0)) and 65535 + } + } + return crc + } + + private fun u16(data: ByteArray, offset: Int): Int = + (data[offset].toInt() and 255) or ((data[offset + 1].toInt() and 255) shl 8) + + @JvmStatic fun parseAck(packet: ByteArray?): Ack? = parseAcks(packet)?.first() + + /** + * Validate the entire primary ACK and up to three preceding successes before applying any. + */ + @JvmStatic + fun parseAcks(packet: ByteArray?): Array? { + if ( + packet == null || + packet.size !in 19..40 || + (packet.size - 19) % 7 != 0 || + packet[0].toInt() and 255 != 0xaa || + packet[1].toInt() != 0x12 || + packet[3].toInt() and 255 != packet.size - 8 || + packet[4].toInt() != 1 || + packet[5].toInt() != 1 || + packet[6].toInt() and 255 != SID || + packet[7].toInt() != 0 || + (packet[8].toInt() != 1 && packet[8].toInt() != 3) || + (packet[12].toInt() != 1 && packet[12].toInt() != 2) || + (packet[8].toInt() == 3 && packet.size != 19) || + crc(packet, 8, packet.size - 10) != u16(packet, packet.size - 2) + ) + return null + return Array(1 + (packet.size - 19) / 7) { i -> + if (i == 0) + Ack( + packet[8].toInt() == 3, + packet[9].toInt() and 255, + u16(packet, 10), + packet[12].toInt(), + u16(packet, 13), + u16(packet, 15), + ) + else { + val offset = 17 + (i - 1) * 7 + Ack( + false, + packet[offset].toInt() and 255, + u16(packet, offset + 1), + packet[12].toInt(), + u16(packet, offset + 3), + u16(packet, offset + 5), + ) + } + } + } + } + + class Ack( + @JvmField val nack: Boolean, + @JvmField val streamId: Int, + @JvmField val messageId: Int, + @JvmField val lens: Int, + @JvmField val size: Int, + @JvmField val checksum: Int, + ) +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt new file mode 100644 index 00000000..caba97a1 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt @@ -0,0 +1,78 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField + +class ConnectionOptions { + companion object { + @JvmField val WRITE_MODE = GattWriteMode.WITHOUT_RESPONSE + const val DESIRED_MTU = 512 + + const val CONNECT_TIMEOUT_MS = 5_000 + const val SERVICES_TIMEOUT_MS = 5_000 + const val DESCRIPTOR_TIMEOUT_MS = 5_000 + const val RING_DESIRED_MTU = 247 + const val RING_RECONNECT_DELAY_MS = 2_000 + const val WRITE_TIMEOUT_MS = 2_000 + const val PRELUDE_TIMEOUT_MS = 2_000 + // Application security-auth exchange (sid 0x80). The success notification + // only arrives once the BLE link is encrypted, which on a first-time + // connection means SMP pairing — possibly with an OS dialog the user has + // to accept — so the stock-firmware paths wait generously. + const val SECURITY_AUTH_TIMEOUT_MS = 30_000 + // The main communicator's wait is soft (it proceeds on timeout so firmware + // that never answers can't stall every reconnect), so it is kept short. + const val SECURITY_AUTH_SOFT_TIMEOUT_MS = 6_000 + const val ACK_TIMEOUT_MS = 3_500 + const val HEARTBEAT_FAILURE_DEADLINE_MS = 10_000 + const val HEARTBEAT_READY_MS = 4_000 + const val HEARTBEAT_URGENT_MS = 6_000 + const val BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000 + const val BATTERY_INPUT_QUIET_MS = 5_000 + // Poll cadence while in charging mode; also bounds how quickly we notice + // the glasses coming back out of the case. + const val CHARGING_BATTERY_POLL_MS = 30_000 + const val IMAGE_FRAGMENT_SIZE = 3800 + const val IMAGE_RETRY_DELAY_MS = 2_000 + const val MAX_CONSECUTIVE_ACK_TIMEOUTS = 8 + const val EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000 + const val IDLE_SLEEP_MS = 100 + const val RECONNECT_DELAY_MS = 2_000 + // Cap on rects per batch. Each rect consumes a distinct CFW frame id, and the + // firmware's duplicate-fid ring holds 16, so keep several batches of history + // within it. Above this the split is abandoned for a single bounding box. + const val MULTI_RECT_MAX_RECTS = 6 + // Don't bother splitting when the single bounding box already compresses this + // small (one tight rect); only attempt multi-rect above it or when the box + // spans multiple horizontal clusters. + const val MULTI_RECT_MIN_PAYLOAD = 900 + } + + @JvmField val sendImagesToLeft = true + @JvmField val skipSessionIds = true + // Max messages in flight (awaiting ack) at once — full pipelining. >1 lets a + // frame be sent before the previous frame's ack returns, hiding the BLE ack + // round-trip that otherwise serialized every update. Requires firmware that + // accepts pipelined image messages (the CFW snapshot/deferred FIFO). At 1 the + // communicator behaves exactly as before (serial sends + last-packet prewrite). + @JvmField val WINDOW_SIZE = 3 + // Send changed-region (mode 3 bounding box) updates instead of full frames + // when the previous frame is known to be displayed. Requires firmware with + // the experimental bbox-incremental mode. Disabled: the firmware-side + // display buffer is not always the previous frame (occasionally two frames + // back, apparently display-driver buffer swapping), so partial updates + // composite onto stale content per-lens. Re-enable once the firmware + // guarantees the compositing base. + @JvmField val INCREMENTAL_FRAMES = true + + // Send several tight rectangle deltas as one CFW mode-8 multi-segment message + // instead of a single bounding box, when the change splits into regions whose + // bounding box wastes bytes (distant edits, or a sparse box). Requires firmware + // with mode-8 multi-rect support; needs INCREMENTAL_FRAMES (same seeded shadow). + // Falls back to the single bounding box whenever multi-rect isn't smaller. + @JvmField val MULTI_RECT_FRAMES = true + + // Ship text as on-glasses cached-glyph draws (CFW modes 12/14) instead of + // pixels, punching glyph ink out of the baked deltas (see TexturePlanner). + // Also gated at runtime on the firmware advertising texcache12 + texstr14. + @JvmField val TEXTURE_CACHE_FRAMES = true +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FaceclawRingEventDecoder.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FaceclawRingEventDecoder.kt new file mode 100644 index 00000000..651f18a9 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FaceclawRingEventDecoder.kt @@ -0,0 +1,145 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +class FaceclawRingEventDecoder { + companion object { + @JvmStatic + fun decode(raw: ByteArray?): DirectRingEvent? { + if (((raw == null) || (raw.size == 0))) { + return null + } + var chargerGesture: DirectRingEvent? = decodeChargerGesture(raw) + if ((chargerGesture != null)) { + return chargerGesture + } + return decodeDirectGesture(raw) + } + + private fun decodeChargerGesture(raw: ByteArray): DirectRingEvent? { + if ((raw.size != 11)) { + return null + } + if ( + ((((u8(raw[0]) != 0x00) || (u8(raw[1]) != 0x09)) || (u8(raw[2]) != 0x61)) || + (u8(raw[3]) != 0x00)) + ) { + return null + } + var code: Int = u8(raw[4]) + var param: Int = (u8(raw[5]) or (u8(raw[6]) shl 8)) + var tick: Long = + ((((u8(raw[7]) or (u8(raw[8]) shl 8)) or (u8(raw[9]) shl 16)) or + (u8(raw[10]) shl 24)) + .toLong() and 0xffffffffL) + var detail: String = + "code=0x${code.toString(16).padStart(2, '0')}(${chargerCodeName(code)}) param=0x${param.toString(16).padStart(4, '0')} tick=$tick" + when (code) { + 0x00 -> { + return event(BleProtocol.EVENT_RING_LONG_PRESS, "LONG_PRESS", detail) + } + 0x01 -> { + return event(BleProtocol.EVENT_CLICK, "TAP", detail) + } + 0x02 -> { + return event(BleProtocol.EVENT_DOUBLE_CLICK, "DOUBLE_TAP", detail) + } + 0x04 -> { + return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_UP", detail) + } + 0x05 -> { + return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_DOWN", detail) + } + 0x08 -> { + return event( + BleProtocol.EVENT_RING_LONG_PRESS_RELEASE, + "LONG_PRESS_RELEASE", + detail, + ) + } + else -> { + return null + } + } + } + + private fun decodeDirectGesture(raw: ByteArray): DirectRingEvent? { + if (((raw.size != 3) || (u8(raw[0]) != 0xff))) { + return null + } + var type: Int = u8(raw[1]) + var param: Int = u8(raw[2]) + var detail: String = + "type=0x${type.toString(16).padStart(2, '0')} param=0x${param.toString(16).padStart(2, '0')}" + if (((type == 0x03) && (param == 0x20))) { + return event(BleProtocol.EVENT_RING_LONG_PRESS, "HOLD", detail) + } + if (((type == 0x04) && (param == 0x01))) { + return event(BleProtocol.EVENT_CLICK, "TAP_SINGLE", detail) + } + if (((type == 0x04) && (param == 0x02))) { + return event(BleProtocol.EVENT_DOUBLE_CLICK, "TAP_DOUBLE", detail) + } + if ((type == 0x05)) { + if ((param <= 0x01)) { + return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_FORWARD", detail) + } + return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_BACKWARD", detail) + } + return null + } + + private fun event(eventType: Int, label: String, detail: String): DirectRingEvent { + var event: G2Event = + G2Event("sys-event", "", eventType, BleProtocol.EVENT_SOURCE_RING, 0) + return DirectRingEvent(event, label, detail) + } + + private fun chargerCodeName(code: Int): String { + when (code) { + 0x00 -> { + return "LONG_PRESS" + } + 0x01 -> { + return "TAP" + } + 0x02 -> { + return "DOUBLE_TAP" + } + 0x04 -> { + return "SWIPE_UP" + } + 0x05 -> { + return "SWIPE_DOWN" + } + 0x08 -> { + return "LONG_PRESS_RELEASE" + } + else -> { + return "unknown" + } + } + } + + private fun u8(value: Byte): Int { + return (value and 0xff) + } + } + + constructor() {} + + class DirectRingEvent { + @JvmField val event: G2Event + + @JvmField val label: String + + @JvmField val detail: String + + constructor(event: G2Event, label: String, detail: String) { + this.event = event + this.label = label + this.detail = detail + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/G2Event.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/G2Event.kt new file mode 100644 index 00000000..0bc9ee9c --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/G2Event.kt @@ -0,0 +1,141 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +class G2Event { + companion object { + @JvmStatic + fun decode(frame: BleProtocol.ParsedFrame): G2Event? { + return decodePayload(frame.sid, BleProtocol.stripTrailingCrc(frame.pb)) + } + + /** Decode an already validated payload, without the envelope's trailing CRC. */ + @JvmStatic + fun decodePayload(sid: Int, pb: ByteArray): G2Event? { + if ((sid == BleProtocol.SID_EVEN_AI)) { + var commandId: Int = BleProtocol.readVarintFieldValue(pb, 1, -1) + if ((commandId != BleProtocol.EVEN_AI_CMD_CTRL)) { + return null + } + var ctrl: ByteArray? = BleProtocol.readFieldBytes(pb, 3) + if ((ctrl == null)) { + return null + } + var status: Int = BleProtocol.readVarintFieldValue(ctrl, 1, -1) + if ((status < 0)) { + return null + } + return G2Event("even-ai", "", status, 0, 0) + } + var deviceEvent: ByteArray? = BleProtocol.readFieldBytes(pb, 13) + if ((deviceEvent == null)) { + return null + } + var listEvent: ByteArray? = BleProtocol.readFieldBytes(deviceEvent, 1) + if ((listEvent != null)) { + return G2Event( + "list-click", + BleProtocol.readStringFieldValue(listEvent, 2), + BleProtocol.readVarintFieldValue(listEvent, 5, BleProtocol.EVENT_CLICK), + 0, + 0, + ) + } + var textEvent: ByteArray? = BleProtocol.readFieldBytes(deviceEvent, 2) + if ((textEvent != null)) { + return G2Event( + "text-click", + BleProtocol.readStringFieldValue(textEvent, 2), + BleProtocol.readVarintFieldValue(textEvent, 3, BleProtocol.EVENT_CLICK), + 0, + 0, + ) + } + var sysEvent: ByteArray? = BleProtocol.readFieldBytes(deviceEvent, 3) + if ((sysEvent != null)) { + var eventType: Int = + BleProtocol.readVarintFieldValue(sysEvent, 1, BleProtocol.EVENT_CLICK) + var eventSource: Int = BleProtocol.readVarintFieldValue(sysEvent, 2, 0) + var exitReason: Int = BleProtocol.readVarintFieldValue(sysEvent, 4, 0) + var imu: ByteArray? = BleProtocol.readFieldBytes(sysEvent, 3) + if ((imu != null)) { + var x: Double = BleProtocol.readFloatFieldValue(imu, 1, 0f).toDouble() + var y: Double = BleProtocol.readFloatFieldValue(imu, 2, 0f).toDouble() + var z: Double = BleProtocol.readFloatFieldValue(imu, 3, 0f).toDouble() + return G2Event( + "sys-event", + "", + eventType, + eventSource, + exitReason, + true, + x, + y, + z, + ) + } + return G2Event("sys-event", "", eventType, eventSource, exitReason) + } + return null + } + } + + @JvmField val kind: String + + @JvmField val containerName: String + + @JvmField val eventType: Int + + @JvmField val eventSource: Int + + @JvmField val systemExitReasonCode: Int + + @JvmField val hasImu: Boolean + + @JvmField val imuX: Double + + @JvmField val imuY: Double + + @JvmField val imuZ: Double + + constructor( + kind: String, + containerName: String, + eventType: Int, + eventSource: Int, + systemExitReasonCode: Int, + ) : this( + kind, + containerName, + eventType, + eventSource, + systemExitReasonCode, + false, + 0.0, + 0.0, + 0.0, + ) {} + + constructor( + kind: String, + containerName: String, + eventType: Int, + eventSource: Int, + systemExitReasonCode: Int, + hasImu: Boolean, + imuX: Double, + imuY: Double, + imuZ: Double, + ) { + this.kind = kind + this.containerName = containerName + this.eventType = eventType + this.eventSource = eventSource + this.systemExitReasonCode = systemExitReasonCode + this.hasImu = hasImu + this.imuX = imuX + this.imuY = imuY + this.imuZ = imuZ + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt new file mode 100644 index 00000000..218c7d4c --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt @@ -0,0 +1,292 @@ +package com.faceclaw.app + +class MessageBuilder { + companion object { + private const val ACK_TIMEOUT_MS: Int = 3_500 + + private const val HEARTBEAT_TIMEOUT_MS: Int = 1_500 + } + + private var magicPool: BleMagicPool + + constructor(magicPool: BleMagicPool) { + this.magicPool = magicPool + } + + fun prelude(): OutboundMessage { + return OutboundMessage( + "prelude", + "prelude", + BleProtocol.PRELUDE_ACK_SID, + BleProtocol.FLAG_REQUEST, + BleProtocol.PRELUDE_ACK_MAGIC, + BleProtocol.PRELUDE_F5872_PAYLOAD, + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + /** The per-connection sid-0x80 security-auth request (one per arm). */ + fun securityAuth(leftArm: Boolean): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "security-auth", + ("security auth " + (if (leftArm) "L" else "R")), + BleProtocol.SID_SECURITY_AUTH, + BleProtocol.FLAG_SECURITY_AUTH, + magic, + BleProtocol.buildAuthenticationRequest(magic), + ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS, + -1, + leftArm, + ) + } + + fun shutdown(exitMode: Int): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "shutdown", + ("shutdown mode=" + exitMode), + BleProtocol.SID_EVENHUB, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildShutdown(magic, exitMode), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun imagePayload( + tile: BleProtocol.ImageTileOptions, + sessionId: Int, + payload: ByteArray, + label: String, + leftArm: Boolean, + ): OutboundMessage { + return imagePayload("sound", tile, sessionId, payload, label, leftArm) + } + + fun imagePayload( + kind: String, + tile: BleProtocol.ImageTileOptions, + sessionId: Int, + payload: ByteArray, + label: String, + leftArm: Boolean, + ): OutboundMessage { + return customMessage(kind, payload, label, -1, leftArm) + } + + fun customMessage( + kind: String, + payload: ByteArray?, + label: String, + tileIndex: Int, + leftArm: Boolean, + ): OutboundMessage { + if (((payload == null) || (payload.size > CfwTransport.MAX_MESSAGE))) { + throw IllegalArgumentException("CFW message exceeds uint16 length") + } + var streamId: Int = magicPool.allocate() + return OutboundMessage( + kind, + label, + CfwTransport.SID, + 0, + streamId, + payload, + CfwMessageWindow.ACK_TIMEOUT_MS, + tileIndex, + leftArm, + ) + } + + fun cfwCleanup( + tile: BleProtocol.ImageTileOptions, + sessionId: Int, + leftArm: Boolean, + ): OutboundMessage { + var payload: ByteArray = byteArrayOf((BleProtocol.CFW_IMAGE_MODE_CLEANUP).toByte()) + return imagePayload("cfw-cleanup", tile, sessionId, payload, "CFW cleanup", leftArm) + } + + fun enableOrDisableMic(enable: Boolean): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "audio-control", + (if (enable) "G2 mic enable" else "G2 mic disable"), + BleProtocol.SID_EVENHUB, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildAudioControl(magic, enable), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun enableOrDisableImu(enable: Boolean, reportFrq: Int): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "imu-control", + (if (enable) "IMU enable" else "IMU disable"), + BleProtocol.SID_EVENHUB, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildImuControl(magic, enable, reportFrq), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun setBrightness(autoAdjust: Boolean, brightnessLevel: Int): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "brightness-control", + (if (autoAdjust) "brightness auto" else ("brightness level=" + brightnessLevel)), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildSetBrightness(magic, autoAdjust, brightnessLevel), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun setWearDetection(enabled: Boolean): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "wear-detection-control", + ("wear detection " + (if (enabled) "enable" else "disable")), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildSetWearDetection(magic, enabled), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun createLayout(): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "create-layout", + "create-layout", + BleProtocol.SID_EVENHUB, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildCreateInputPage(magic), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun startupTextProbe(): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "startup-text-probe", + "startup text probe", + BleProtocol.SID_EVENHUB, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildDashboardTextUpgrade(magic), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun heartbeat(): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "heartbeat", + "heartbeat", + BleProtocol.SID_EVENHUB, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildHeartbeat(magic), + HEARTBEAT_TIMEOUT_MS, + -1, + false, + ) + } + + fun batteryQuery(): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "battery", + "battery", + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildSettingsQuery(magic), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + + fun faceclawWakeControl(operation: Int, nonce: Int, leftArm: Boolean): OutboundMessage { + return OutboundMessage( + "wake-lease-control", + (((("wake lease op=" + operation) + " nonce=") + nonce) + + (if (leftArm) " L" else " R")), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + 0, + BleProtocol.buildFaceclawWakeControl(operation, nonce), + ACK_TIMEOUT_MS, + -1, + leftArm, + ) + } + + fun faceclawFramebufferControl(operation: Int, leftArm: Boolean): OutboundMessage { + return OutboundMessage( + "framebuffer-lease-control", + (("framebuffer lease op=" + operation) + (if (leftArm) " L" else " R")), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + 0, + BleProtocol.buildFaceclawWakeControl(operation, 0), + ACK_TIMEOUT_MS, + -1, + leftArm, + ) + } + + /** CFW mic_control write (settings field 103) targeted at one temple. */ + fun faceclawMicControl(record: ByteArray, label: String, leftArm: Boolean): OutboundMessage { + return OutboundMessage( + "mic-control", + (("mic control " + label) + (if (leftArm) " L" else " R")), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + 0, + BleProtocol.buildFaceclawMicControl(record), + ACK_TIMEOUT_MS, + -1, + leftArm, + ) + } + + fun faceclawWearQuery(leftArm: Boolean): OutboundMessage { + return OutboundMessage( + "wear-query-control", + ("wear state query" + (if (leftArm) " L" else " R")), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + 0, + BleProtocol.buildFaceclawWakeControl(BleProtocol.FACECLAW_WEAR_OP_QUERY, 0), + ACK_TIMEOUT_MS, + -1, + leftArm, + ) + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageReceiver.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageReceiver.kt new file mode 100644 index 00000000..6761dfa8 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageReceiver.kt @@ -0,0 +1,127 @@ +package com.faceclaw.app + +/** Split/coalesced notifications and protobuf fragments, isolated per BLE link and sequence. */ +class MessageReceiver { + class Message( + val sid: Int, + val flag: Int, + val payload: ByteArray, + val command: Int, + val magic: Int, + val packet: ByteArray? = null, + ) + + private class Partial(val total: Int, var seen: Long) { + val chunks = mutableListOf() + var size = 0 + } + + private val lock = protocolPlatform().createLock() + private val streams = mutableMapOf() + private val messages = mutableMapOf() + + fun clear() = lock.withLock { + streams.clear() + messages.clear() + } + + fun receive(link: String, data: ByteArray, now: Long): List = lock.withLock { + messages.entries.removeAll { now - it.value.seen > 5000 } + val stream = (streams[link] ?: ByteArray(0)) + data + val output = mutableListOf() + var offset = 0 + while (stream.size - offset >= 8) { + fun u8(index: Int) = stream[offset + index].toInt() and 255 + if (u8(0) != 0xaa || u8(1) !in intArrayOf(0x21, 0x12)) { + offset++ + continue + } + val length = u8(3) + if (stream.size - offset < length + 8) break + val count = u8(4) + val index = u8(5) + val sid = u8(6) + val flag = u8(7) + val key = "$link:${u8(2)}:$sid:$flag" + val packet = stream.copyOfRange(offset, offset + length + 8) + val chunk = packet.copyOfRange(8, packet.size) + offset += length + 8 + if (sid == CfwTransport.SID) { + output.add(Message(sid, flag, chunk, -1, -1, packet)) + continue + } + if (count == 0 || index == 0 || index > count) { + messages.remove(key) + continue + } + if (index == 1) messages[key] = Partial(count, now) + val partial = messages[key] + if (partial == null || partial.total != count || partial.chunks.size + 1 != index) { + messages.remove(key) + continue + } + partial.chunks.add(chunk) + partial.size += chunk.size + partial.seen = now + if (partial.size > 65536) { + messages.remove(key) + continue + } + if (index != count) continue + messages.remove(key) + val bytes = ByteSink(partial.size) + partial.chunks.forEach { bytes.write(it) } + val complete = bytes.toByteArray() + if (complete.size < 2) continue + val payload = complete.copyOf(complete.size - 2) + val checksum = + (complete[complete.size - 2].toInt() and 255) or + ((complete.last().toInt() and 255) shl 8) + if (CfwTransport.crc(payload) != checksum || !validProtobuf(payload)) continue + output.add( + Message( + sid, + flag, + payload, + BleProtocol.readVarintFieldValue(payload, 1, -1), + BleProtocol.readVarintFieldValue(payload, 2, -1), + ) + ) + } + streams[link] = stream.copyOfRange(offset, stream.size) + output + } + + private fun validProtobuf(data: ByteArray): Boolean { + var offset = 0 + fun varint(): Int? { + var value = 0 + for (i in 0..4) { + if (offset == data.size) return null + val byte = data[offset++].toInt() and 255 + if (i == 4 && byte > 15) return null + value = value or ((byte and 127) shl (i * 7)) + if (byte and 128 == 0) return value + } + return null + } + while (offset < data.size) { + val key = varint() ?: return false + if (key ushr 3 == 0) return false + if (key and 7 == 0) { + varint() ?: return false + continue + } + val length = + when (key and 7) { + 1 -> 8 + 5 -> 4 + 2 -> varint() ?: return false + else -> return false + } + if (length < 0 || length > data.size - offset) return false + offset += length + } + return true + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/OutboundMessage.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/OutboundMessage.kt new file mode 100644 index 00000000..43ee91db --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/OutboundMessage.kt @@ -0,0 +1,68 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField + +/** Mutable attempt state, owned by the communicator and guarded by its lock. */ +class OutboundMessage( + @JvmField val kind: String?, + @JvmField val label: String?, + @JvmField val sid: Int, + @JvmField val flag: Int, + @JvmField var magic: Int, + message: ByteArray?, + @JvmField val ackTimeoutMs: Int, + @JvmField val tileIndex: Int, + @JvmField val isLeftArmMessage: Boolean, +) { + @JvmField val message: ByteArray = message?.copyOf() ?: ByteArray(0) + @JvmField + val cfwChecksum: Int = if (sid == CfwTransport.SID) CfwTransport.crc(this.message) else 0 + @JvmField var cfwAckLenses = 0 + @JvmField var cfwRetries = 0 + @JvmField var cfwRetryPending = false + @JvmField var imageUpdateId = 0 + @JvmField var imageMessageNumber = 0 + @JvmField var imageMessageCount = 0 + @JvmField var sentAtMs = 0L + @JvmField var writeStartedAtMs = 0L + @JvmField var ackDeadlineAtMs = 0L + @JvmField var ackPayload = ByteArray(0) + @JvmField var onSent: MessageCallback? = null + @JvmField var onAck: MessageCallback? = null + @JvmField var onTimeout: MessageCallback? = null + + /** NACK names this attempt's sequence, even without a decoded size. */ + fun acceptCfwAck(ack: CfwTransport.Ack?): Boolean { + if ( + ack == null || + sid != CfwTransport.SID || + magic != ack.streamId || + ack.messageId != 0 || + (ack.lens != 1 && ack.lens != 2) + ) + return false + if (ack.nack) { + cfwRetryPending = true + return false + } + if (message.size != ack.size || cfwChecksum != ack.checksum) return false + cfwAckLenses = cfwAckLenses or ack.lens + return !cfwRetryPending && cfwAckLenses == CfwTransport.BOTH + } + + /** Replace an attempt atomically under the communicator lock. */ + fun prepareCfwReplay(streamId: Int) { + magic = streamId + cfwRetries++ + cfwAckLenses = 0 + cfwRetryPending = false + ackDeadlineAtMs = 0 + ackPayload = ByteArray(0) + } + + fun setImageUpdatePosition(updateId: Int, messageNumber: Int, messageCount: Int) { + imageUpdateId = updateId + imageMessageNumber = messageNumber + imageMessageCount = messageCount + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ProtocolPlatform.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ProtocolPlatform.kt new file mode 100644 index 00000000..ab7691f0 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ProtocolPlatform.kt @@ -0,0 +1,46 @@ +package com.faceclaw.app + +/** OS services used by the protocol state machines; supplied by their owner. */ +interface ProtocolPlatform { + /** Monotonic milliseconds since boot, including time spent asleep. */ + fun elapsedRealtimeMs(): Long + + fun createLock(): ProtocolLock + + fun createDeflater(): ProtocolDeflater +} + +/** Must be reentrant: transport encoding can reset its history under the lock. */ +interface ProtocolLock { + fun lock() + + fun unlock() +} + +/** Persistent zlib stream. Each write completes a SYNC_FLUSH, not a new stream. */ +interface ProtocolDeflater { + fun reset() + + fun syncFlush(message: ByteArray): ByteArray + + fun close() +} + +internal inline fun ProtocolLock.withLock(action: () -> T): T { + lock() + try { + return action() + } finally { + unlock() + } +} + +enum class GattWriteMode { + WITH_RESPONSE, + WITHOUT_RESPONSE, +} + +/** Keeps message callbacks independent of java.lang.Runnable. */ +fun interface MessageCallback { + fun run() +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TextureCacheState.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TextureCacheState.kt new file mode 100644 index 00000000..623ff165 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TextureCacheState.kt @@ -0,0 +1,191 @@ +package com.faceclaw.app + +/** + * Phone-side model of the CFW's flat 256 KiB texture cache (zlib_glue mode 18). The firmware holds + * only bytes; every structural decision — where font tables and glyph images live, what is + * resident, when to start over — is made here. Layout per font: a 96-entry uint32 offset table + * (chars 32..127) whose entries are written lazily as each glyph is uploaded; glyph images are the + * GlyphAtlas cached bytes ([w][h][4bpp RLE]). + * + * Allocation is a bump pointer with reset-on-full: the realistic working set (a few fonts' tables + * plus ~hundreds of glyphs at tens of bytes each) is far below 256 KiB, so eviction sophistication + * buys nothing. After a reset the firmware cache still holds stale bytes; that is safe because a + * draw only ever references (font table entry, glyph image) pairs uploaded in the current + * generation — stale table entries are never named in a mode-20 string. + * + * Upload entries accumulate as complete [offset u32][len u16][data] records and drain into one or + * more standalone mode-18 payloads, each independently valid, enqueued ahead of the image message + * that references them (the transport is FIFO, so no ack round-trip is needed before use). + * + * Not thread-safe by itself: the communicator calls it under its own lock. + */ +class TextureCacheState { + companion object { + const val CACHE_SIZE: Int = 262144 + + private val FONT_TABLE_BYTES: Int = (96 * 4) + } + + private var allocEnd: Int = 0 + + private val fontTableOffsets: MutableMap = HashMap() + + private val glyphOffsets: MutableMap = HashMap() + + private val imageOffsets: MutableMap = HashMap() + + private val pendingEntries: MutableList = ArrayList() + + private var pendingBytes: Int = 0 + + private var generation: Int = 0 + + /** + * Forget everything: the on-glasses cache is gone (session teardown, lease loss) or no longer + * trustworthy (upload timeout), or we ran out of space. Glyphs and images re-upload lazily on + * next use. + */ + fun reset(): Unit { + allocEnd = 0 + fontTableOffsets.clear() + glyphOffsets.clear() + imageOffsets.clear() + pendingEntries.clear() + pendingBytes = 0 + generation++ + } + + /** Bumped by every reset; lets a planner detect a mid-plan reset. */ + fun generation(): Int { + return generation + } + + /** + * Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a sudden drop in the frame + * log means a reset-on-full re-upload cycle — the signal that the 256 KiB budget is being + * outgrown). + */ + fun usedBytes(): Int { + return allocEnd + } + + /** + * The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit. + */ + fun fontTableOffset(fontId: Int): Int { + var existing: Int? = fontTableOffsets.get(fontId) + return (if ((existing == null)) -1 else existing) + } + + /** + * Ensure a glyph image (and its font table slot) is resident, queuing the mode-18 upload + * entries for anything newly placed. Returns the glyph's image offset, or -1 when the cache is + * full (caller resets and retries) or the encoding is outside the mode-20 table (32..127). + */ + fun ensureGlyph(fontId: Int, encoding: Int, glyph: GlyphAtlas.Glyph?): Int { + if ((((glyph == null) || (encoding < 32)) || (encoding > 127))) { + return -1 + } + var key: Long = (((fontId).toLong() shl 32) or (encoding.toLong() and 0xffffffffL)) + var existing: Int? = glyphOffsets.get(key) + if ((existing != null)) { + return existing + } + var table: Int? = fontTableOffsets.get(fontId) + var need: Int = (glyph.cachedBytes.size + (if ((table == null)) FONT_TABLE_BYTES else 0)) + if (((allocEnd + need) > CACHE_SIZE)) { + return -1 + } + if ((table == null)) { + table = allocEnd + allocEnd += FONT_TABLE_BYTES + fontTableOffsets.put(fontId, table) + } + var offset: Int = allocEnd + allocEnd += glyph.cachedBytes.size + glyphOffsets.put(key, offset) + addEntry(offset, glyph.cachedBytes) + addEntry( + (table + (4 * (encoding - 32))), + byteArrayOf( + (offset).toByte(), + ((offset shr 8)).toByte(), + ((offset shr 16)).toByte(), + ((offset shr 24)).toByte(), + ), + ) + return offset + } + + /** + * Ensure an ImageAtlas entry is resident (mode-19 draws reference it by raw cache offset; no + * table involved). Returns the image offset, or -1 when the cache is full (caller resets and + * retries). + */ + fun ensureImage(imageId: Int, image: ImageAtlas.Entry?): Int { + if ((image == null)) { + return -1 + } + var existing: Int? = imageOffsets.get(imageId) + if ((existing != null)) { + return existing + } + if (((allocEnd + image.cachedBytes.size) > CACHE_SIZE)) { + return -1 + } + var offset: Int = allocEnd + allocEnd += image.cachedBytes.size + imageOffsets.put(imageId, offset) + addEntry(offset, image.cachedBytes) + return offset + } + + private fun addEntry(offset: Int, data: ByteArray): Unit { + var entry: ByteArray = ByteArray((6 + data.size)) + entry[0] = ((offset and 0xff)).toByte() + entry[1] = (((offset shr 8) and 0xff)).toByte() + entry[2] = (((offset shr 16) and 0xff)).toByte() + entry[3] = (((offset shr 24) and 0xff)).toByte() + entry[4] = ((data.size and 0xff)).toByte() + entry[5] = (((data.size shr 8) and 0xff)).toByte() + data.copyInto(entry, 6, 0, 0 + data.size) + pendingEntries.add(entry) + pendingBytes += entry.size + } + + fun hasPendingUploads(): Boolean { + return !pendingEntries.isEmpty() + } + + /** Total queued upload bytes (entry framing included), for logging. */ + fun pendingUploadBytes(): Int { + return pendingBytes + } + + /** + * Drain the queued entries into complete mode-18 payloads ([18][entries]), each at most + * maxPayloadBytes so a payload fits one BLE image message without fragmentation. A single entry + * larger than the cap still gets its own payload (the transport can fragment it). + */ + fun drainUploadPayloads(maxPayloadBytes: Int): MutableList { + var payloads: MutableList = ArrayList() + var current: ByteSink? = null + for (entry in pendingEntries) { + if (((current != null) && ((current.size() + entry.size) > maxPayloadBytes))) { + payloads.add(current.toByteArray()) + current = null + } + if ((current == null)) { + current = ByteSink(minOf(maxPayloadBytes, (4 + entry.size))) + current.write(18) + } + current.write(entry, 0, entry.size) + } + if ((current != null)) { + payloads.add(current.toByteArray()) + } + pendingEntries.clear() + pendingBytes = 0 + return payloads + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt new file mode 100644 index 00000000..6effc438 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt @@ -0,0 +1,987 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * Plans a screen update that ships text and icons as on-glasses cached draws instead of pixels (CFW + * modes 18/19/20; see g2flash/patches/zlib_glue.c and texture_cache.c). + * + * Approach: dirty rects come from comparing the fully composited old and new frames, exactly as the + * plain incremental path does. For each deferred draw (glyph or image) whose pixels intersect a + * sent rect, check that every pixel the on-glasses draw would write lands correct — i.e. the new + * composite's 4bpp value at each in-panel nonzero-source pixel equals what the draw produces (this + * one check subsumes plane occlusion, surface occlusion/clipping, and draw-on-draw overlap). A draw + * that passes and is (or can be made) resident in the texture cache is replayed on-glasses: its + * written pixels are punched to 0 in the delta rect content — which is what makes text and icons + * nearly free to compress — and re-emitted as a mode-20 string or mode-19 image sub-message in the + * same atomic mode-8 batch, so the shadow after apply equals the full composite exactly. Everything + * else stays baked. + * + * Fallback shape: when nothing qualifies, plan() returns null and the caller uses the plain + * single-bbox/multi-rect/full-frame paths unchanged. + */ +class TexturePlanner { + companion object { + /** Cap on mode-20 string sub-messages (mode-8 count is a u8). */ + private const val MAX_GLYPH_RUNS: Int = 180 + + /** + * Soft cap on cached-glyph selection per update, before run grouping. Sized above a + * full-screen 6x12 terminal repaint (~4,240 cells) so dense screens draw everything; the + * run budget is the real bound. + */ + private const val MAX_SELECTED_GLYPHS: Int = 4600 + + /** Cap on mode-19 image sub-messages per update. */ + private const val MAX_IMAGE_DRAWS: Int = 60 + + /** Cap on mode-15 builtin-font string sub-messages per update. */ + private const val MAX_FWTEXT_RUNS: Int = 80 + + /** Mode-18 payload size that fits one BLE image message comfortably. */ + private const val UPLOAD_PAYLOAD_MAX: Int = 3600 + + /** Max x-adjust control bytes between two glyphs before starting a new run. */ + private const val MAX_ADJUST_BYTES: Int = 4 + + /** Options: identity LUT (top 15) + transparent, for mode-19 image draws. */ + private const val IMAGE_DRAW_OPTIONS: Int = 0x1f + + /** + * Plan a cached-draw update. previous is the delta base (the frame the shadow currently + * holds), or null/mismatched for a full-frame keyframe. Returns null when the plain paths + * should run instead (no replayable draws, or identical frames). + */ + @JvmStatic + fun plan( + previous: ByteArray?, + next: ByteArray?, + width: Int, + height: Int, + draws: Array?, + cache: TextureCacheState, + fidStart: Int, + allowMultiRect: Boolean, + maxRects: Int, + ): Result? { + if ( + (((((next == null) || (draws == null)) || (draws.size == 0)) || (width <= 0)) || + (height <= 0)) + ) { + return null + } + var stride: Int = ((width + 1) shr 1) + if ((next.size != (stride * height))) { + return null + } + var planStartedAtMs: Long = protocolPlatform().elapsedRealtimeMs() + var fullFrame: Boolean = ((previous == null) || (previous.size != next.size)) + var rects: MutableList + if (!fullFrame) { + var box: IntArray? = + BleImageOptimizer.computeChangedBox(previous, next, width, height) + if ((box == null)) { + return null + } + if (((box[2] >= width) && (box[3] >= height))) { + fullFrame = true + rects = mutableListOf(intArrayOf(0, 0, width, height)) + } else { + var split: MutableList? = + (if (allowMultiRect) + BleImageOptimizer.computeChangedRects( + previous, + next, + width, + height, + maxRects, + ) + else null) + rects = (if ((split != null)) split else mutableListOf(box)) + } + } else { + rects = mutableListOf(intArrayOf(0, 0, width, height)) + } + var matchStartedAtMs: Long = protocolPlatform().elapsedRealtimeMs() + var selected: MutableList = ArrayList() + var bakedCandidates: Int = 0 + var selectedImages: Int = 0 + var fwSubs: MutableList = ArrayList() + var fwPunches: MutableList = ArrayList() + var fwGlyphCount: Int = 0 + var fwBaked: Int = 0 + for (draw in draws) { + if ((draw.kind != SurfaceCompositor.ScreenDraw.KIND_FWTEXT)) { + continue + } + fwBaked += planFwRun(draw, rects, next, stride, width, height, fwSubs, fwPunches) + } + fwGlyphCount = fwPunches.size + bakedCandidates += fwBaked + for (draw in draws) { + if ((draw.kind == SurfaceCompositor.ScreenDraw.KIND_GLYPH)) { + var atlas: GlyphAtlas.Glyph? = GlyphAtlas.get(draw.fontId, draw.encoding) + if ((atlas == null)) { + continue + } + var gx: Int = (draw.x + atlas.bbxX) + var inkTop: Int = (draw.y + atlas.inkTop) + if (!intersectsAny(rects, gx, inkTop, atlas.width, atlas.inkHeight)) { + continue + } + if ( + ((((((draw.encoding < 32) || (draw.encoding > 127)) || (gx < 0)) || + (draw.y < 0)) || (gx > 0xffff)) || (draw.y > 0xffff)) + ) { + bakedCandidates++ + continue + } + var top: Int = BmpUtil.nibbleForGray(draw.value) + if ( + !glyphMatchesComposite(next, stride, width, height, atlas, gx, draw.y, top) + ) { + bakedCandidates++ + continue + } + if ((selected.size >= MAX_SELECTED_GLYPHS)) { + bakedCandidates++ + continue + } + selected.add(Selected(draw, atlas, null, gx, top)) + } else { + var atlas: ImageAtlas.Entry? = ImageAtlas.get(draw.imageId) + if ((atlas == null)) { + continue + } + if (!intersectsAny(rects, draw.x, draw.y, atlas.width, atlas.height)) { + continue + } + if ( + (((((draw.x < 0) || (draw.y < 0)) || (draw.x > 0xffff)) || + (draw.y > 0xffff)) || (selectedImages >= MAX_IMAGE_DRAWS)) + ) { + bakedCandidates++ + continue + } + if ( + !imageMatchesComposite(next, stride, width, height, atlas, draw.x, draw.y) + ) { + bakedCandidates++ + continue + } + selected.add(Selected(draw, null, atlas, draw.x, 0)) + selectedImages++ + } + } + if ((selected.isEmpty() && fwSubs.isEmpty())) { + return null + } + var cacheStartedAtMs: Long = protocolPlatform().elapsedRealtimeMs() + var drawable: MutableList = + (if (selected.isEmpty()) mutableListOf() else ensureResident(cache, selected)) + bakedCandidates += (selected.size - drawable.size) + var glyphDrawable: MutableList = ArrayList() + var imageDrawable: MutableList = ArrayList() + for (sel in drawable) { + if ((sel.glyphAtlas != null)) { + glyphDrawable.add(sel) + } else { + imageDrawable.add(sel) + } + } + var runs: MutableList = ArrayList() + var drawnGlyphs: MutableList = buildRuns(cache, glyphDrawable, runs) + bakedCandidates += (glyphDrawable.size - drawnGlyphs.size) + var drawn: MutableList = ArrayList(drawnGlyphs) + drawn.addAll(imageDrawable) + if ( + ((drawn.isEmpty() && fwSubs.isEmpty()) || + ((((rects.size + runs.size) + imageDrawable.size) + fwSubs.size) > 255)) + ) { + if (!selected.isEmpty()) { + cache.reset() + } + return null + } + var punchStartedAtMs: Long = protocolPlatform().elapsedRealtimeMs() + var punched: ByteArray = next.copyOf() + for (sel in drawn) { + punch(punched, stride, width, height, sel) + } + for (fw in fwPunches) { + punchFw(punched, stride, width, height, fw) + } + var encodeStartedAtMs: Long = protocolPlatform().elapsedRealtimeMs() + var subs: MutableList = ArrayList() + var fid: Int = fidStart + if (fullFrame) { + subs.add(BleImageOptimizer.maybeCompress(punched, width, height)) + } else { + for (r in rects) { + subs.add( + BleImageOptimizer.encodeMode3Rect( + punched, + stride, + r[0], + r[1], + r[2], + r[3], + fid, + ) + ) + fid = (if ((fid >= 0xfffe)) 1 else (fid + 1)) + } + } + for (sel in imageDrawable) { + subs.add(encodeImageDraw(cache, sel)) + } + subs.addAll(runs) + subs.addAll(fwSubs) + var payload: ByteArray = assembleMode8(subs) + var uploadBytes: Int = cache.pendingUploadBytes() + var uploads: MutableList = cache.drainUploadPayloads(UPLOAD_PAYLOAD_MAX) + var result: Result = + Result( + payload, + uploads, + fid, + rects.size, + drawnGlyphs.size, + runs.size, + imageDrawable.size, + fwGlyphCount, + fwSubs.size, + bakedCandidates, + uploadBytes, + fullFrame, + ) + var doneAtMs: Long = protocolPlatform().elapsedRealtimeMs() + result.rectsMs = ((matchStartedAtMs - planStartedAtMs)).toInt() + result.matchMs = ((cacheStartedAtMs - matchStartedAtMs)).toInt() + result.cacheMs = ((punchStartedAtMs - cacheStartedAtMs)).toInt() + result.punchMs = ((encodeStartedAtMs - punchStartedAtMs)).toInt() + result.encodeMs = ((doneAtMs - encodeStartedAtMs)).toInt() + return result + } + + /** + * Plan one firmware-font run (mode 15): classify every member, and when any correct ink + * member intersects a sent rect, emit sub-messages over the maximal correct stretches. A + * stretch never spans an incorrect member — the sub-message boundary restarts the + * firmware's advance walk at an explicit pen x, so occluded or unknown glyphs simply stay + * baked without disturbing their neighbors' positions. Emitted ink members are queued for + * punching. Returns how many ink members stay baked. + */ + private fun planFwRun( + draw: SurfaceCompositor.ScreenDraw, + rects: MutableList, + next: ByteArray, + stride: Int, + width: Int, + height: Int, + outSubs: MutableList, + outPunches: MutableList, + ): Int { + var n: Int = draw.fwCps!!.size + if ((((n == 0) || (draw.y < 0)) || (draw.y > 0xffff))) { + return countInk(draw) + } + var top: Int = BmpUtil.nibbleForGray(draw.value) + var ok: BooleanArray = BooleanArray(n) + var entries: Array = arrayOfNulls(n) + var relevant: Boolean = false + var baked: Int = 0 + run { + var i: Int = 0 + while ((i < n)) { + if (!draw.fwInk!![i]) { + ok[i] = true + i++ + continue + } + var entry: FwGlyphAtlas.Entry? = FwGlyphAtlas.get(draw.fwCps!![i]) + if ((entry == null)) { + baked++ + i++ + continue + } + var gx: Int = ((draw.x + draw.fwDx!![i]) + entry.ofsX) + var gy: Int = (draw.y + entry.inkTop) + if (!fwGlyphMatchesComposite(next, stride, width, height, entry, gx, gy, top)) { + baked++ + i++ + continue + } + ok[i] = true + entries[i] = entry + if (intersectsAny(rects, gx, gy, entry.boxW, entry.boxH)) { + relevant = true + } + i++ + } + } + if (!relevant) { + return 0 + } + var i: Int = 0 + while (((i < n) && (outSubs.size < MAX_FWTEXT_RUNS))) { + while (((i < n) && !(ok[i] && draw.fwInk!![i]))) { + i++ + } + if ((i >= n)) { + break + } + var startX: Int = (draw.x + draw.fwDx!![i]) + if (((startX < 0) || (startX > 0xffff))) { + baked++ + i++ + continue + } + var j: Int = i + var lastInk: Int = -1 + var bytesToLastInk: Int = 0 + var bytesSoFar: Int = 0 + while (((j < n) && ok[j])) { + var encodedLength: Int = utf8Length(draw.fwCps!![j]) + if (((bytesSoFar + encodedLength) > 255)) { + break + } + bytesSoFar += encodedLength + if (draw.fwInk!![j]) { + lastInk = j + bytesToLastInk = bytesSoFar + } + j++ + } + if ((lastInk < i)) { + baked++ + i++ + continue + } + var sub: ByteArray = ByteArray((7 + bytesToLastInk)) + sub[0] = 15 + sub[1] = ((startX and 0xff)).toByte() + sub[2] = (((startX shr 8) and 0xff)).toByte() + sub[3] = ((draw.y and 0xff)).toByte() + sub[4] = (((draw.y shr 8) and 0xff)).toByte() + sub[5] = ((top or 0x10)).toByte() + sub[6] = (bytesToLastInk).toByte() + var pos: Int = 7 + run { + var k: Int = i + while ((k <= lastInk)) { + var encoded: ByteArray = codePointUtf8(draw.fwCps!![k]) + encoded.copyInto(sub, pos, 0, 0 + encoded.size) + pos += encoded.size + k++ + } + } + outSubs.add(sub) + run { + var k: Int = i + while ((k <= lastInk)) { + if (draw.fwInk!![k]) { + outPunches.add( + FwPunch( + entries[k]!!, + ((draw.x + draw.fwDx!![k]) + entries[k]!!.ofsX), + (draw.y + entries[k]!!.inkTop), + ) + ) + } + k++ + } + } + i = (lastInk + 1) + } + return baked + } + + private fun utf8Length(cp: Int): Int { + if ((cp < 0x80)) { + return 1 + } + if ((cp < 0x800)) { + return 2 + } + if ((cp < 0x10000)) { + return 3 + } + return 4 + } + + private fun countInk(draw: SurfaceCompositor.ScreenDraw): Int { + var count: Int = 0 + for (ink in draw.fwInk!!) { + if (ink) { + count++ + } + } + return count + } + + /** + * Whether every in-panel nonzero pixel of the builtin-font glyph lands correct: the + * composite's 4bpp value must equal the firmware's LUT output src*top/15 (integer division + * — a general LUT check, since these glyphs are anti-aliased). + */ + private fun fwGlyphMatchesComposite( + packed: ByteArray, + stride: Int, + width: Int, + height: Int, + entry: FwGlyphAtlas.Entry, + gx: Int, + gy: Int, + top: Int, + ): Boolean { + run { + var row: Int = 0 + while ((row < entry.boxH)) { + var y: Int = (gy + row) + if (((y < 0) || (y >= height))) { + row++ + continue + } + run { + var col: Int = 0 + while ((col < entry.boxW)) { + var source: Int = entry.nibbleAt(col, row) + if ((source == 0)) { + col++ + continue + } + var x: Int = (gx + col) + if (((x < 0) || (x >= width))) { + col++ + continue + } + if ((nibbleAt(packed, stride, x, y) != ((source * top) / 15))) { + return false + } + col++ + } + } + row++ + } + } + return true + } + + private fun punchFw( + packed: ByteArray, + stride: Int, + width: Int, + height: Int, + fw: FwPunch, + ): Unit { + run { + var row: Int = 0 + while ((row < fw.entry.boxH)) { + var y: Int = (fw.gy + row) + if (((y < 0) || (y >= height))) { + row++ + continue + } + run { + var col: Int = 0 + while ((col < fw.entry.boxW)) { + if ((fw.entry.nibbleAt(col, row) == 0)) { + col++ + continue + } + punchPixel(packed, stride, width, (fw.gx + col), y) + col++ + } + } + row++ + } + } + } + + private fun intersectsAny( + rects: MutableList, + x: Int, + y: Int, + w: Int, + h: Int, + ): Boolean { + if (((w <= 0) || (h <= 0))) { + return false + } + for (r in rects) { + if ( + ((((x < (r[0] + r[2])) && ((x + w) > r[0])) && (y < (r[1] + r[3]))) && + ((y + h) > r[1])) + ) { + return true + } + } + return false + } + + /** + * Whether every in-panel ink pixel of the glyph lands correct in the packed new frame: the + * composite's 4bpp value must equal the firmware's LUT output source*top/15 (integer). For + * 1bpp glyphs (source 15) this is exactly `== top`; AA glyphs get the general check. Ink + * outside the panel is clipped by the firmware and irrelevant. + */ + private fun glyphMatchesComposite( + packed: ByteArray, + stride: Int, + width: Int, + height: Int, + atlas: GlyphAtlas.Glyph, + gx: Int, + lineY: Int, + top: Int, + ): Boolean { + run { + var row: Int = atlas.inkTop + while ((row < (atlas.inkTop + atlas.inkHeight))) { + var y: Int = (lineY + row) + if (((y < 0) || (y >= height))) { + row++ + continue + } + run { + var col: Int = 0 + while ((col < atlas.width)) { + var source: Int = atlas.nibbleAt(col, row) + if ((source == 0)) { + col++ + continue + } + var x: Int = (gx + col) + if (((x < 0) || (x >= width))) { + col++ + continue + } + if ((nibbleAt(packed, stride, x, y) != ((source * top) / 15))) { + return false + } + col++ + } + } + row++ + } + } + return true + } + + /** + * Whether every in-panel nonzero pixel of the cached image equals the packed new frame — + * the mode-19 transparent draw writes exactly those. + */ + private fun imageMatchesComposite( + packed: ByteArray, + stride: Int, + width: Int, + height: Int, + atlas: ImageAtlas.Entry, + left: Int, + top: Int, + ): Boolean { + run { + var row: Int = 0 + while ((row < atlas.height)) { + var y: Int = (top + row) + if (((y < 0) || (y >= height))) { + row++ + continue + } + run { + var col: Int = 0 + while ((col < atlas.width)) { + var source: Int = atlas.nibbleAt(col, row) + if ((source == 0)) { + col++ + continue + } + var x: Int = (left + col) + if (((x < 0) || (x >= width))) { + col++ + continue + } + if ((nibbleAt(packed, stride, x, y) != source)) { + return false + } + col++ + } + } + row++ + } + } + return true + } + + private fun nibbleAt(packed: ByteArray, stride: Int, x: Int, y: Int): Int { + var b: Int = (packed[((y * stride) + (x shr 1))] and 0xff) + return (if (((x and 1) != 0)) (b and 0x0f) else (b shr 4)) + } + + private fun punch( + packed: ByteArray, + stride: Int, + width: Int, + height: Int, + sel: Selected, + ): Unit { + if ((sel.glyphAtlas != null)) { + var atlas: GlyphAtlas.Glyph = sel.glyphAtlas + run { + var row: Int = atlas.inkTop + while ((row < (atlas.inkTop + atlas.inkHeight))) { + var y: Int = (sel.draw.y + row) + if (((y < 0) || (y >= height))) { + row++ + continue + } + run { + var col: Int = 0 + while ((col < atlas.width)) { + if (!atlas.inkAt(col, row)) { + col++ + continue + } + punchPixel(packed, stride, width, (sel.gx + col), y) + col++ + } + } + row++ + } + } + } else { + var atlas: ImageAtlas.Entry = sel.imageAtlas!! + run { + var row: Int = 0 + while ((row < atlas.height)) { + var y: Int = (sel.draw.y + row) + if (((y < 0) || (y >= height))) { + row++ + continue + } + run { + var col: Int = 0 + while ((col < atlas.width)) { + if ((atlas.nibbleAt(col, row) == 0)) { + col++ + continue + } + punchPixel(packed, stride, width, (sel.gx + col), y) + col++ + } + } + row++ + } + } + } + } + + private fun punchPixel(packed: ByteArray, stride: Int, width: Int, x: Int, y: Int): Unit { + if (((x < 0) || (x >= width))) { + return + } + var index: Int = ((y * stride) + (x shr 1)) + if (((x and 1) != 0)) { + packed[index] = ((packed[index] and 0xf0)).toByte() + } else { + packed[index] = ((packed[index] and 0x0f)).toByte() + } + } + + /** + * Ensure residency for all of `selected`, resetting the cache once if the allocator fills + * mid-plan (a reset invalidates offsets handed out earlier in the same pass, so the whole + * pass reruns). Returns the draws that are resident afterwards. + */ + private fun ensureResident( + cache: TextureCacheState, + selected: MutableList, + ): MutableList { + run { + var attempt: Int = 0 + while ((attempt < 2)) { + var resident: MutableList = ArrayList(selected.size) + var full: Boolean = false + for (sel in selected) { + var offset: Int = + (if ((sel.glyphAtlas != null)) + cache.ensureGlyph( + sel.draw.fontId, + sel.draw.encoding, + sel.glyphAtlas, + ) + else cache.ensureImage(sel.draw.imageId, sel.imageAtlas)) + if ((offset >= 0)) { + resident.add(sel) + } else { + full = true + } + } + if ((!full || (attempt == 1))) { + return resident + } + cache.reset() + attempt++ + } + } + return mutableListOf() + } + + /** Mode-19 cached-image draw: [19][offset u32][x u16][y u16][options u8]. */ + @JvmStatic + private fun encodeImageDraw(cache: TextureCacheState, sel: Selected): ByteArray { + var offset: Int = cache.ensureImage(sel.draw.imageId, sel.imageAtlas) + var sub: ByteArray = ByteArray(10) + sub[0] = 19 + sub[1] = ((offset and 0xff)).toByte() + sub[2] = (((offset shr 8) and 0xff)).toByte() + sub[3] = (((offset shr 16) and 0xff)).toByte() + sub[4] = (((offset shr 24) and 0xff)).toByte() + sub[5] = ((sel.draw.x and 0xff)).toByte() + sub[6] = (((sel.draw.x shr 8) and 0xff)).toByte() + sub[7] = ((sel.draw.y and 0xff)).toByte() + sub[8] = (((sel.draw.y shr 8) and 0xff)).toByte() + sub[9] = (IMAGE_DRAW_OPTIONS).toByte() + return sub + } + + /** + * Group selected glyphs into mode-20 string sub-messages: [20][fontTable u32][x u16][y + * u16][options u8][strlen u8][string] One run per (font, line y, top color) span; within a + * run, control bytes 1..31 adjust x by -10..+20 to hit each glyph's exact position, and + * each glyph advances x by its cached width. Order across runs is free: every drawn glyph's + * ink equals the composite, so overlaps write equal values. Returns the glyphs actually + * emitted (run budget can drop stragglers). + */ + @JvmStatic + private fun buildRuns( + cache: TextureCacheState, + drawable: MutableList, + outRuns: MutableList, + ): MutableList { + var sorted: MutableList = ArrayList(drawable) + sorted.sortWith( + compareBy { it.draw.fontId } + .thenBy { it.draw.y } + .thenBy { it.top } + .thenBy { it.gx } + ) + var drawn: MutableList = ArrayList(sorted.size) + var i: Int = 0 + while (((i < sorted.size) && (outRuns.size < MAX_GLYPH_RUNS))) { + var first: Selected = sorted.get(i) + var fontTable: Int = cache.fontTableOffset(first.draw.fontId) + if ((fontTable < 0)) { + i++ + continue + } + var string: ByteSink = ByteSink() + var runGlyphs: MutableList = ArrayList() + var cursor: Int = first.gx + var j: Int = i + while ((j < sorted.size)) { + var sel: Selected = sorted.get(j) + if ( + (((sel.draw.fontId != first.draw.fontId) || (sel.draw.y != first.draw.y)) || + (sel.top != first.top)) + ) { + break + } + var delta: Int = (sel.gx - cursor) + var adjustBytes: Int = adjustByteCount(delta) + if (((adjustBytes > MAX_ADJUST_BYTES) && (j > i))) { + break + } + if ((((string.size() + adjustBytes) + 1) > 255)) { + break + } + emitAdjust(string, delta) + string.write(sel.draw.encoding) + runGlyphs.add(sel) + cursor = (sel.gx + sel.glyphAtlas!!.width) + j++ + } + if (!runGlyphs.isEmpty()) { + var stringBytes: ByteArray = string.toByteArray() + var run: ByteArray = ByteArray((11 + stringBytes.size)) + run[0] = 20 + run[1] = ((fontTable and 0xff)).toByte() + run[2] = (((fontTable shr 8) and 0xff)).toByte() + run[3] = (((fontTable shr 16) and 0xff)).toByte() + run[4] = (((fontTable shr 24) and 0xff)).toByte() + run[5] = ((first.gx and 0xff)).toByte() + run[6] = (((first.gx shr 8) and 0xff)).toByte() + run[7] = ((first.draw.y and 0xff)).toByte() + run[8] = (((first.draw.y shr 8) and 0xff)).toByte() + run[9] = ((first.top or 0x10)).toByte() + run[10] = (stringBytes.size).toByte() + stringBytes.copyInto(run, 11, 0, 0 + stringBytes.size) + outRuns.add(run) + drawn.addAll(runGlyphs) + i = j + } else { + i++ + } + } + return drawn + } + + /** How many 1..31 control bytes are needed to move the cursor by delta. */ + private fun adjustByteCount(delta: Int): Int { + if ((delta == 0)) { + return 0 + } + return (if ((delta > 0)) ((delta + 19) / 20) else ((-delta + 9) / 10)) + } + + private fun emitAdjust(out: ByteSink, delta: Int): Unit { + var delta = delta + while ((delta != 0)) { + var step: Int = (if ((delta > 0)) minOf(delta, 20) else maxOf(delta, -10)) + out.write((step + 11)) + delta -= step + } + } + + /** [8][count]([len u16][sub])* — one atomic present for deltas + draws. */ + private fun assembleMode8(subs: MutableList): ByteArray { + var total: Int = 2 + for (sub in subs) { + total += (2 + sub.size) + } + var out: ByteArray = ByteArray(total) + out[0] = 8 + out[1] = (subs.size).toByte() + var pos: Int = 2 + for (sub in subs) { + out[pos] = ((sub.size and 0xff)).toByte() + out[(pos + 1)] = (((sub.size shr 8) and 0xff)).toByte() + pos += 2 + sub.copyInto(out, pos, 0, 0 + sub.size) + pos += sub.size + } + return out + } + } + + constructor() {} + + class Result { + /** Complete image payload: a mode-8 batch of rect deltas + cached draws. */ + @JvmField val payload: ByteArray + + /** Mode-18 upload payloads to enqueue BEFORE the image message. */ + @JvmField val uploads: MutableList + + /** Next mode-3 frame id (deltas consumed some). */ + @JvmField val nextFid: Int + + @JvmField val rectCount: Int + + @JvmField val drawnGlyphs: Int + + @JvmField val runCount: Int + + @JvmField val drawnImages: Int + + @JvmField val fwGlyphs: Int + + @JvmField val fwRuns: Int + + @JvmField val bakedCandidates: Int + + @JvmField val uploadBytes: Int + + @JvmField val fullFrame: Boolean + + /** + * Where the planning time went, in ms. This runs on the send thread, directly in the + * input-to-display path, so the split is reported in the frame log: rects = finding the + * changed region, match = checking each draw against the composite, cache = + * residency/uploads plus run building, punch = clearing replayed draws out of a copy of the + * frame, encode = compressing the leftover pixels. + */ + @JvmField var rectsMs: Int = 0 + + @JvmField var matchMs: Int = 0 + + @JvmField var cacheMs: Int = 0 + + @JvmField var punchMs: Int = 0 + + @JvmField var encodeMs: Int = 0 + + constructor( + payload: ByteArray, + uploads: MutableList, + nextFid: Int, + rectCount: Int, + drawnGlyphs: Int, + runCount: Int, + drawnImages: Int, + fwGlyphs: Int, + fwRuns: Int, + bakedCandidates: Int, + uploadBytes: Int, + fullFrame: Boolean, + ) { + this.payload = payload + this.uploads = uploads + this.nextFid = nextFid + this.rectCount = rectCount + this.drawnGlyphs = drawnGlyphs + this.runCount = runCount + this.drawnImages = drawnImages + this.fwGlyphs = fwGlyphs + this.fwRuns = fwRuns + this.bakedCandidates = bakedCandidates + this.uploadBytes = uploadBytes + this.fullFrame = fullFrame + } + } + + /** One draw selected for on-glasses replay. */ + private class Selected { + @JvmField val draw: SurfaceCompositor.ScreenDraw + + @JvmField val glyphAtlas: GlyphAtlas.Glyph? + + @JvmField val imageAtlas: ImageAtlas.Entry? + + @JvmField val gx: Int + + @JvmField val top: Int + + constructor( + draw: SurfaceCompositor.ScreenDraw, + glyphAtlas: GlyphAtlas.Glyph?, + imageAtlas: ImageAtlas.Entry?, + gx: Int, + top: Int, + ) { + this.draw = draw + this.glyphAtlas = glyphAtlas + this.imageAtlas = imageAtlas + this.gx = gx + this.top = top + } + } + + /** One punch task for an emitted firmware-font glyph. */ + private class FwPunch { + @JvmField val entry: FwGlyphAtlas.Entry + + @JvmField val gx: Int + + @JvmField val gy: Int + + constructor(entry: FwGlyphAtlas.Entry, gx: Int, gy: Int) { + this.entry = entry + this.gx = gx + this.gy = gy + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/FwGlyphAtlas.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/FwGlyphAtlas.kt new file mode 100644 index 00000000..ce972e08 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/FwGlyphAtlas.kt @@ -0,0 +1,89 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * Process-wide registry of the glasses' builtin 20 px font's glyph rasters, fed from the TS side + * (EvenHubFont's extracted firmware font data). Unlike GlyphAtlas/ImageAtlas entries, these are + * never uploaded: the firmware already owns the font, and mode 15 draws it directly. The planner + * needs the rasters purely phone-side — for the "would this draw land correctly" check against the + * composite and for punching drawn ink out of delta rects. + * + * Placement model matches the firmware's mode-15 math: a glyph draws at (penX + ofsX, lineY + + * inkTop) where inkTop already folds in the root font's line height and baseline. The TS side only + * registers glyphs whose on-glasses rendering it predicts exactly (native-table glyphs; baseline- + * shifted fallback symbols stay baked). + */ +class FwGlyphAtlas { + companion object { + private val lock = protocolPlatform().createLock() + + private val entries: MutableMap = HashMap() + + /** + * Register glyph rasters. Little-endian buffer of records: [cp u32][ofsX s16][inkTop + * s16][boxW u8][boxH u8] [boxH x ceil(boxW/2) nibble bytes] Re-registration of a codepoint + * is ignored (the builtin font is fixed). + */ + @JvmStatic + fun register(buffer: ByteReader?): Unit { + if ((buffer == null)) { + return + } + val cursor: ByteReader = buffer + lock.withLock { + while ((cursor.remaining() >= 10)) { + var cp: Int = cursor.getInt() + var ofsX: Int = cursor.getShort().toInt() + var inkTop: Int = cursor.getShort().toInt() + var boxW: Int = (cursor.get() and 0xff) + var boxH: Int = (cursor.get() and 0xff) + var nibbles: ByteArray = ByteArray((((boxW + 1) shr 1) * boxH)) + cursor.get(nibbles) + if ((((boxW > 0) && (boxH > 0)) && !entries.containsKey(cp))) { + entries.put(cp, Entry(ofsX, inkTop, boxW, boxH, nibbles)) + } + } + } + } + + @JvmStatic + fun get(cp: Int): Entry? { + lock.withLock { + return entries.get(cp) + } + } + } + + constructor() {} + + class Entry { + /** Draw x = pen x + ofsX. */ + @JvmField val ofsX: Int + + /** First raster row relative to the run's line-top y. */ + @JvmField val inkTop: Int + + @JvmField val boxW: Int + + @JvmField val boxH: Int + + /** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */ + @JvmField val nibbles: ByteArray + + constructor(ofsX: Int, inkTop: Int, boxW: Int, boxH: Int, nibbles: ByteArray) { + this.ofsX = ofsX + this.inkTop = inkTop + this.boxW = boxW + this.boxH = boxH + this.nibbles = nibbles + } + + /** The 4bpp source value at (col, row); the draw skips 0 (transparent). */ + fun nibbleAt(col: Int, row: Int): Int { + var b: Int = (nibbles[((row * ((boxW + 1) shr 1)) + (col shr 1))] and 0xff) + return (if (((col and 1) != 0)) (b and 0x0f) else (b shr 4)) + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GlyphAtlas.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GlyphAtlas.kt new file mode 100644 index 00000000..97450875 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GlyphAtlas.kt @@ -0,0 +1,325 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * Process-wide registry of glyph rasters, fed from the TS side so glyph identity survives to the + * BLE encoder (see notes/texture-cache-display-list- design.md). A frame's glyph list references + * entries here by (fontId, encoding); the texture-cache planner uses the raster three ways: + * + * - as the ink mask for the "would this draw land correctly" check against the composited frame, + * - to punch the ink pixels out of the baked delta rect it replaces, + * - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded via mode 12 and drawn via + * mode 14 with a top-color LUT. 1bpp (BDF) glyphs store ink at 15 so the LUT maps them to exactly + * the requested level; AA (TTF) glyphs store true coverage nibbles the LUT scales. + * + * Fonts are identified by a stable string key (the embedded font name), NOT a per-JS-context + * counter: worker threads register independently and must agree on ids. Glyph rasters are immutable + * once registered; re-registration of the same (fontId, encoding) is ignored. + * + * Cell model: a cached glyph image is bbxWidth x cellHeight (the font's line height), with the ink + * rows at inkTop — so every glyph of a line is drawn at the same y (the line top), which is what + * lets one mode-14 string carry a whole run. The horizontal bearing (bbxX) is applied phone-side + * when computing the draw x. Vertical padding is nearly free: it RLE-encodes to a couple of tokens. + */ +class GlyphAtlas { + companion object { + private val lock = protocolPlatform().createLock() + + private val fontIds: MutableMap = HashMap() + + private val glyphs: MutableMap = HashMap() + + private var nextFontId: Int = 1 + + /** Stable id for a font key; assigns one on first use. */ + @JvmStatic + fun fontId(key: String): Int { + lock.withLock { + var id: Int? = fontIds.get(key) + if ((id == null)) { + id = nextFontId++ + fontIds.put(key, id) + } + return id + } + } + + @JvmStatic + fun get(fontId: Int, encoding: Int): Glyph? { + lock.withLock { + return glyphs.get(key(fontId, encoding)) + } + } + + private fun key(fontId: Int, encoding: Int): Long { + return (((fontId).toLong() shl 32) or (encoding.toLong() and 0xffffffffL)) + } + + /** + * Register a batch of glyph rasters. Little-endian buffer, a sequence of font groups: + * [keyLen u8][key utf8][cellHeight u8][count u16] each followed by count glyph records: + * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] [inkHeight x rows u32] (bit + * (ceil(width/8)*8 - 1 - col) = ink) Records for an already-registered (font, encoding) are + * skipped (glyph rasters are immutable for a given font key). Malformed buffers throw: they + * indicate a phone-side marshalling bug, never device state. + */ + @JvmStatic + fun register(buffer: ByteReader?): Unit { + if ((buffer == null)) { + return + } + val cursor: ByteReader = buffer + lock.withLock { + while ((cursor.remaining() > 0)) { + var keyLen: Int = (cursor.get() and 0xff) + var keyBytes: ByteArray = ByteArray(keyLen) + cursor.get(keyBytes) + var fontKey: String = keyBytes.decodeToString() + var cellHeight: Int = (cursor.get() and 0xff) + var count: Int = (cursor.getShort().toInt() and 0xffff) + var idBoxed: Int? = fontIds.get(fontKey) + var id: Int = 0 + if ((idBoxed == null)) { + id = nextFontId++ + fontIds.put(fontKey, id) + } else { + id = idBoxed + } + run { + var g: Int = 0 + while ((g < count)) { + var encoding: Int = cursor.getInt() + val bbxX: Int = cursor.get().toInt() + var inkTop: Int = (cursor.get() and 0xff) + var width: Int = (cursor.get() and 0xff) + var inkHeight: Int = (cursor.get() and 0xff) + val rows: IntArray = IntArray(inkHeight) + run { + var r: Int = 0 + while ((r < inkHeight)) { + rows[r] = cursor.getInt() + r++ + } + } + var k: Long = key(id, encoding) + if ( + (((!glyphs.containsKey(k) && (width > 0)) && (cellHeight > 0)) && + ((inkTop + inkHeight) <= cellHeight)) + ) { + glyphs.put( + k, + Glyph(width, cellHeight, inkTop, inkHeight, bbxX, rows, null), + ) + } + g++ + } + } + } + } + } + + /** + * Register a batch of antialiased (4bpp) glyph rasters, e.g. TTF renders. Same font-group + * framing as register(), but each glyph record carries packed coverage instead of 1bpp + * rows: [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] + * [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel] Nibble values are the + * final 4bpp source levels (the draw-time LUT scales them by the top color). + * Already-registered (font, encoding) records are skipped, as in register(). + */ + @JvmStatic + fun registerAa(buffer: ByteReader?): Unit { + if ((buffer == null)) { + return + } + val cursor: ByteReader = buffer + lock.withLock { + while ((cursor.remaining() > 0)) { + var keyLen: Int = (cursor.get() and 0xff) + var keyBytes: ByteArray = ByteArray(keyLen) + cursor.get(keyBytes) + var fontKey: String = keyBytes.decodeToString() + var cellHeight: Int = (cursor.get() and 0xff) + var count: Int = (cursor.getShort().toInt() and 0xffff) + var idBoxed: Int? = fontIds.get(fontKey) + var id: Int = 0 + if ((idBoxed == null)) { + id = nextFontId++ + fontIds.put(fontKey, id) + } else { + id = idBoxed + } + run { + var g: Int = 0 + while ((g < count)) { + var encoding: Int = cursor.getInt() + val bbxX: Int = cursor.get().toInt() + var inkTop: Int = (cursor.get() and 0xff) + var width: Int = (cursor.get() and 0xff) + var inkHeight: Int = (cursor.get() and 0xff) + var stride: Int = ((width + 1) shr 1) + var coverage: ByteArray = ByteArray((width * inkHeight)) + run { + var r: Int = 0 + while ((r < inkHeight)) { + run { + var b: Int = 0 + while ((b < stride)) { + var packed: Int = (cursor.get() and 0xff) + var col: Int = (b * 2) + coverage[((r * width) + col)] = + ((packed shr 4)).toByte() + if (((col + 1) < width)) { + coverage[(((r * width) + col) + 1)] = + ((packed and 0x0f)).toByte() + } + b++ + } + } + r++ + } + } + var k: Long = key(id, encoding) + if ( + (((!glyphs.containsKey(k) && (width > 0)) && (cellHeight > 0)) && + ((inkTop + inkHeight) <= cellHeight)) + ) { + glyphs.put( + k, + Glyph( + width, + cellHeight, + inkTop, + inkHeight, + bbxX, + null, + coverage, + ), + ) + } + g++ + } + } + } + } + } + } + + constructor() {} + + class Glyph { + /** Cached image width in pixels (the glyph's tight bbox width). */ + @JvmField val width: Int + + /** Cached image height in pixels (the font's line height). */ + @JvmField val cellHeight: Int + + /** First row of ink within the cell. */ + @JvmField val inkTop: Int + + /** Number of ink rows. */ + @JvmField val inkHeight: Int + + /** Horizontal bearing: draw x = pen x + bbxX. */ + @JvmField val bbxX: Int + + /** 1bpp ink rows (inkHeight entries), bit (rowBitWidth-1-col) = ink; null for AA glyphs. */ + @JvmField val rows: IntArray? + + @JvmField val rowBitWidth: Int + + /** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */ + @JvmField val coverage: ByteArray? + + /** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */ + @JvmField val cachedBytes: ByteArray + + constructor( + width: Int, + cellHeight: Int, + inkTop: Int, + inkHeight: Int, + bbxX: Int, + rows: IntArray?, + coverage: ByteArray?, + ) { + this.width = width + this.cellHeight = cellHeight + this.inkTop = inkTop + this.inkHeight = inkHeight + this.bbxX = bbxX + this.rows = rows + this.coverage = coverage + this.rowBitWidth = (((width + 7) shr 3) shl 3) + this.cachedBytes = encodeCachedImage() + } + + /** + * The cell pixel's 4bpp source value at (col, row), row cell-relative. 1bpp glyphs are + * stored at 15 so the draw-time LUT (source*top/15) maps them to exactly the requested top + * color; AA glyphs carry their true coverage level. + */ + fun nibbleAt(col: Int, row: Int): Int { + if (((col < 0) || (col >= width))) { + return 0 + } + var inkRow: Int = (row - inkTop) + if (((inkRow < 0) || (inkRow >= inkHeight))) { + return 0 + } + if ((coverage != null)) { + return coverage[((inkRow * width) + col)].toInt() + } + return (if ((((rows!![inkRow] ushr ((rowBitWidth - 1) - col)) and 1) != 0)) 15 else 0) + } + + /** Whether the cell pixel at (col, row) has any ink (row is cell-relative). */ + fun inkAt(col: Int, row: Int): Boolean { + return (nibbleAt(col, row) != 0) + } + + /** + * The firmware cached-image encoding: [width][height][RLE tokens] covering exactly + * width*cellHeight pixels (no row padding), each pixel's stored color being its nibbleAt + * value. + */ + private fun encodeCachedImage(): ByteArray { + var total: Int = (width * cellHeight) + var out: ByteArray = ByteArray((2 + total)) + out[0] = (width).toByte() + out[1] = (cellHeight).toByte() + var o: Int = 2 + var i: Int = 0 + while ((i < total)) { + var color: Int = nibbleAt((i % width), (i / width)) + var j: Int = (i + 1) + while (((j < total) && (nibbleAt((j % width), (j / width)) == color))) { + j++ + } + var run: Int = (j - i) + while ((run > 0)) { + var c: Int = minOf(run, 0xffff) + if ((c <= 15)) { + out[o++] = (((c shl 4) or color)).toByte() + } else { + if ((c <= 255)) { + out[o++] = (color).toByte() + out[o++] = (c).toByte() + } else { + out[o++] = (color).toByte() + out[o++] = 0 + out[o++] = ((c and 0xff)).toByte() + out[o++] = ((c shr 8)).toByte() + } + } + run -= c + } + i = j + } + var trimmed: ByteArray = ByteArray(o) + out.copyInto(trimmed, 0, 0, 0 + o) + return trimmed + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/ImageAtlas.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/ImageAtlas.kt new file mode 100644 index 00000000..01af3217 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/ImageAtlas.kt @@ -0,0 +1,134 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * Process-wide registry of icon/image rasters for the texture-cache pipeline, the image counterpart + * of GlyphAtlas. Entries are content-addressed: the TS side keys each image by a hash of its + * dimensions and pixels, so the same icon registered from any thread or rendered at any time + * dedupes to one entry, and an icon whose content changes is simply a new entry. + * + * Unlike glyphs (1-bit ink recolored at draw time), images keep their exact 4bpp values: the cached + * bytes quantize the registered 8bpp pixels with the same GRAY_TO_NIBBLE table the composite is + * packed with, and the planner draws them via mode 13 with an identity LUT (top color 15) and the + * transparent bit — the draw writes exactly the nonzero-nibble pixels, which is what the + * eligibility check and hole punching are defined over. + */ +class ImageAtlas { + companion object { + private val lock = protocolPlatform().createLock() + + private val ids: MutableMap = HashMap() + + private val entries: MutableList = ArrayList() + + /** + * Register an image (8bpp grayscale pixels, width*height bytes, row-major) under a content + * key, returning its id — or the existing id when the key is already registered (the pixels + * are then ignored: same key means same content). Ids are positive and stable for the + * process lifetime. + */ + @JvmStatic + fun ensure(key: String?, width: Int, height: Int, pixels8bpp: ByteReader?): Int { + if ( + (((((((key == null) || (width <= 0)) || (width > 255)) || (height <= 0)) || + (height > 255)) || (pixels8bpp == null)) || + (pixels8bpp.remaining() < (width * height))) + ) { + throw IllegalArgumentException( + ((("bad image registration " + width) + "x") + height) + ) + } + lock.withLock { + var existing: Int? = ids.get(key) + if ((existing != null)) { + return existing + } + var nibbles: ByteArray = ByteArray((width * height)) + run { + var i: Int = 0 + while ((i < nibbles.size)) { + nibbles[i] = (BmpUtil.nibbleForGray((pixels8bpp.get(i) and 0xff))).toByte() + i++ + } + } + entries.add(Entry(width, height, nibbles)) + var id: Int = entries.size + ids.put(key, id) + return id + } + } + + @JvmStatic + fun get(id: Int): Entry? { + lock.withLock { + return (if (((id >= 1) && (id <= entries.size))) entries.get((id - 1)) else null) + } + } + } + + constructor() {} + + class Entry { + @JvmField val width: Int + + @JvmField val height: Int + + /** One 4bpp value per pixel, row-major. */ + @JvmField val nibbles: ByteArray + + /** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */ + @JvmField val cachedBytes: ByteArray + + constructor(width: Int, height: Int, nibbles: ByteArray) { + this.width = width + this.height = height + this.nibbles = nibbles + this.cachedBytes = encodeCachedImage() + } + + /** The 4bpp value the mode-13 draw would write at (col, row); 0 = skipped. */ + fun nibbleAt(col: Int, row: Int): Int { + return (nibbles[((row * width) + col)] and 0xff) + } + + private fun encodeCachedImage(): ByteArray { + var total: Int = (width * height) + var out: ByteArray = ByteArray((2 + total)) + out[0] = (width).toByte() + out[1] = (height).toByte() + var o: Int = 2 + var i: Int = 0 + while ((i < total)) { + var color: Int = (nibbles[i] and 0xff) + var j: Int = (i + 1) + while (((j < total) && ((nibbles[j] and 0xff) == color))) { + j++ + } + var run: Int = (j - i) + while ((run > 0)) { + var c: Int = minOf(run, 0xffff) + if ((c <= 15)) { + out[o++] = (((c shl 4) or color)).toByte() + } else { + if ((c <= 255)) { + out[o++] = (color).toByte() + out[o++] = (c).toByte() + } else { + out[o++] = (color).toByte() + out[o++] = 0 + out[o++] = ((c and 0xff)).toByte() + out[o++] = ((c shr 8)).toByte() + } + } + run -= c + } + i = j + } + var trimmed: ByteArray = ByteArray(o) + out.copyInto(trimmed, 0, 0, 0 + o) + return trimmed + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/LvglFontFile.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/LvglFontFile.kt new file mode 100644 index 00000000..cffb1817 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/LvglFontFile.kt @@ -0,0 +1,250 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * Reader for the serialized LVGL font image in the G2's CJK flash partition. Faceclaw bundles that + * font because it is Source Han Sans SC Light under the SIL OFL. Proprietary fonts embedded in the + * main firmware never pass through this class and are instead extracted on the user's phone during + * CFW install. + */ +class LvglFontFile { + companion object { + private val lock = protocolPlatform().createLock() + + private fun load(path: String?): FontData? = lock.withLock { + if (path.isNullOrEmpty()) return null + val cached = fontCache.remove(path) + if (cached != null) { + fontCache[path] = cached + return cached + } + try { + val font = FontData(readFileData(path)) + fontCache[path] = font + while (fontCache.size > FONT_CACHE_SIZE) fontCache.remove(fontCache.keys.first()) + font + } catch (_: Exception) { + null + } + } + + private const val TAG: String = "LvglFontFile" + + private const val MAPPED_BASE: Int = -2146435072 + + private const val FONT_CACHE_SIZE: Int = 2 + + private val fontCache: MutableMap = LinkedHashMap() + + /** + * Returns [lineHeight, baseline] or an empty array when the font is invalid. The baseline + * is measured down from the top of the line. + */ + @JvmStatic + fun getMetrics(path: String): ByteArray { + var font: FontData? = load(path) + if ((font == null)) { + return ByteArray(0) + } + return byteArrayOf((font.lineHeight).toByte(), (font.baseline).toByte()) + } + + /** + * Returns one packed glyph: u16 boxW, u16 boxH, i16 ofsX, i16 ofsY, then aligned 4bpp + * bitmap bytes. Returns an empty array when the code point is not mapped. + */ + @JvmStatic + fun getGlyph(path: String, codePoint: Int): ByteArray { + var font: FontData? = load(path) + if ((font == null)) { + return ByteArray(0) + } + var glyphId: Int? = font.codePointToGlyph.get(codePoint) + if (((glyphId == null) || (glyphId <= 0))) { + return ByteArray(0) + } + try { + var descriptor: Int = (font.glyphDescriptors + (glyphId * 16)) + var bitmapIndex: Int = font.u32(descriptor) + var boxWidth: Int = font.u16((descriptor + 8)) + var boxHeight: Int = font.u16((descriptor + 10)) + var offsetX: Int = font.i16((descriptor + 12)) + var offsetY: Int = font.i16((descriptor + 14)) + var stride: Int = ((boxWidth / 2) + 1) + var bitmapLength: Int = + (if (((boxWidth > 0) && (boxHeight > 0))) (stride * boxHeight) else 0) + font.check((font.glyphBitmaps + bitmapIndex), bitmapLength) + var result: ByteArray = ByteArray((8 + bitmapLength)) + putU16(result, 0, boxWidth) + putU16(result, 2, boxHeight) + putU16(result, 4, offsetX) + putU16(result, 6, offsetY) + font.bytes.copyInto( + result, + 8, + (font.glyphBitmaps + bitmapIndex), + (font.glyphBitmaps + bitmapIndex) + bitmapLength, + ) + return result + } catch (e: RuntimeException) { + + return ByteArray(0) + } + } + + private fun putU16(destination: ByteArray, offset: Int, value: Int): Unit { + destination[offset] = ((value and 0xff)).toByte() + destination[(offset + 1)] = (((value ushr 8) and 0xff)).toByte() + } + } + + constructor() {} + + private class FontData { + @JvmField val bytes: ByteArray + + @JvmField val lineHeight: Int + + @JvmField val baseline: Int + + @JvmField val glyphBitmaps: Int + + @JvmField val glyphDescriptors: Int + + @JvmField val codePointToGlyph: MutableMap + + constructor(bytes: ByteArray) { + this.bytes = bytes + if ( + (((((bytes.size < 0x40) || (bytes[0].toInt() != 90)) || (bytes[1].toInt() != 90)) || + (bytes[2].toInt() != 90)) || (bytes[3].toInt() != 90)) + ) { + throw IllegalArgumentException("missing ZZZZ font header") + } + lineHeight = u16(0x38) + var baseLine: Int = u16(0x3a) + baseline = (lineHeight - baseLine) + var descriptor: Int = pointerOffset(u32(0x34)) + glyphBitmaps = pointerOffset(u32(descriptor)) + glyphDescriptors = pointerOffset(u32((descriptor + 4))) + var cmaps: Int = pointerOffset(u32((descriptor + 8))) + var packed: Int = u16((descriptor + 18)) + var cmapCount: Int = (packed and 0x1ff) + var bitsPerPixel: Int = ((packed ushr 9) and 0xf) + var bitmapFormat: Int = ((packed ushr 14) and 0x3) + if ( + (((((lineHeight <= 0) || (baseline < 0)) || (cmapCount <= 0)) || + (bitsPerPixel != 4)) || (bitmapFormat != 3)) + ) { + throw IllegalArgumentException("unsupported LVGL font metadata") + } + codePointToGlyph = HashMap() + run { + var index: Int = 0 + while ((index < cmapCount)) { + readCmap((cmaps + (index * 20))) + index++ + } + } + } + + fun readCmap(address: Int): Unit { + var rangeStart: Int = u32(address) + var rangeLength: Int = u16((address + 4)) + var glyphIdStart: Int = u16((address + 6)) + var unicodeListPointer: Int = u32((address + 8)) + var glyphIdOffsetsPointer: Int = u32((address + 12)) + var listLength: Int = u16((address + 16)) + var type: Int = u8((address + 18)) + if ((type == 2)) { + run { + var relative: Int = 0 + while ((relative < rangeLength)) { + codePointToGlyph.put((rangeStart + relative), (glyphIdStart + relative)) + relative++ + } + } + return + } + if ((type == 0)) { + var offsets: Int = pointerOffset(glyphIdOffsetsPointer) + run { + var relative: Int = 0 + while ((relative < rangeLength)) { + var offset: Int = u8((offsets + relative)) + if (((relative != 0) && (offset == 0))) { + relative++ + continue + } + codePointToGlyph.put((rangeStart + relative), (glyphIdStart + offset)) + relative++ + } + } + return + } + if (((type == 3) || (type == 1))) { + var unicodeList: Int = pointerOffset(unicodeListPointer) + var offsets: Int = + (if ((glyphIdOffsetsPointer == 0)) -1 else pointerOffset(glyphIdOffsetsPointer)) + run { + var index: Int = 0 + while ((index < listLength)) { + var relative: Int = u16((unicodeList + (index * 2))) + var offset: Int = index + if ((offsets >= 0)) { + offset = + (if ((type == 1)) u16((offsets + (index * 2))) + else u8((offsets + index))) + if (((index != 0) && (offset == 0))) { + index++ + continue + } + } + codePointToGlyph.put((rangeStart + relative), (glyphIdStart + offset)) + index++ + } + } + return + } + throw IllegalArgumentException(("unknown cmap type " + type)) + } + + fun u8(offset: Int): Int { + check(offset, 1) + return (bytes[offset] and 0xff) + } + + fun u16(offset: Int): Int { + check(offset, 2) + return ((bytes[offset].toInt() and 255) or ((bytes[offset + 1].toInt() and 255) shl 8)) + } + + fun i16(offset: Int): Int { + check(offset, 2) + return u16(offset).toShort().toInt() + } + + fun u32(offset: Int): Int { + check(offset, 4) + return u16(offset) or (u16(offset + 2) shl 16) + } + + fun pointerOffset(pointer: Int): Int { + var unsignedPointer: Long = (pointer.toLong() and 0xffffffffL) + var offset: Long = (unsignedPointer - (MAPPED_BASE.toLong() and 0xffffffffL)) + if (((offset < 0) || (offset > Int.MAX_VALUE))) { + throw IllegalArgumentException("font pointer is outside the mapped partition") + } + check((offset).toInt(), 1) + return (offset).toInt() + } + + fun check(offset: Int, length: Int): Unit { + if ((((offset < 0) || (length < 0)) || (((offset).toLong() + length) > bytes.size))) { + throw IllegalArgumentException("font range is out of bounds") + } + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedGifScreenRecorder.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedGifScreenRecorder.kt new file mode 100644 index 00000000..c47d8d84 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedGifScreenRecorder.kt @@ -0,0 +1,270 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.math.roundToLong + +/** + * Accumulates composited screen frames and saves them as a looping animated GIF with a 16-entry + * grayscale palette. Stored frames are deflate-compressed (a typical UI frame shrinks from 166KB of + * indices to a few KB) so long recordings stay affordable in memory; consecutive identical frames + * are deduplicated, with the elapsed time folded into the next frame's delay. + */ +class SharedGifScreenRecorder { + private val lock = protocolPlatform().createLock() + + fun encode(): ByteArray = lock.withLock { + if (frames.isEmpty()) return ByteArray(0) + val out = ByteSink() + writeGif(out) + out.toByteArray() + } + + companion object { + /** Backstop against unbounded growth if a recording is left running. */ + private const val MAX_FRAMES: Int = 3600 + + /** Displayed duration of the final frame, which has no successor. */ + private const val LAST_FRAME_DELAY_CS: Int = 100 + + /** Below this, GIF renderers historically clamp or ignore the delay. */ + private const val MIN_FRAME_DELAY_CS: Int = 2 + + /** GIF LZW compression of a 4-bit index stream (minimum code size 4). */ + private fun writeLzwImageData(out: ByteSink, indices: ByteArray): Unit { + val minCodeSize: Int = 4 + out.write(minCodeSize) + var writer: SubBlockBitWriter = SubBlockBitWriter(out) + val clearCode: Int = (1 shl minCodeSize) + val eoiCode: Int = (clearCode + 1) + var codeSize: Int = (minCodeSize + 1) + var nextCode: Int = (eoiCode + 1) + var table: HashMap = HashMap() + writer.writeCode(clearCode, codeSize) + var prefix: Int = (indices[0] and 0xff) + run { + var i: Int = 1 + while ((i < indices.size)) { + var k: Int = (indices[i] and 0xff) + var key: Int = ((prefix shl 8) or k) + var code: Int? = table.get(key) + if ((code != null)) { + prefix = code + i++ + continue + } + writer.writeCode(prefix, codeSize) + if ((nextCode == 0x1000)) { + writer.writeCode(clearCode, codeSize) + table.clear() + codeSize = (minCodeSize + 1) + nextCode = (eoiCode + 1) + } else { + if ((nextCode >= (1 shl codeSize))) { + codeSize++ + } + table.put(key, nextCode++) + } + prefix = k + i++ + } + } + writer.writeCode(prefix, codeSize) + writer.writeCode(eoiCode, codeSize) + writer.finish() + } + + private fun writeShortLE(out: ByteSink, value: Int): Unit { + out.write((value and 0xff)) + out.write(((value ushr 8) and 0xff)) + } + } + + private class Frame { + @JvmField val deflatedIndices: ByteArray + + @JvmField val timestampMs: Long + + constructor(deflatedIndices: ByteArray, timestampMs: Long) { + this.deflatedIndices = deflatedIndices + this.timestampMs = timestampMs + } + } + + private val frames: MutableList = ArrayList() + + /** Undeflated copy of the newest frame, kept for duplicate detection. */ + private var lastIndices: ByteArray? = null + + private var width: Int = 0 + + private var height: Int = 0 + + private var overflowed: Boolean = false + + /** + * Append the current screen. gray is 8bpp; only the top nibble (the display's native depth, and + * the palette index) is kept. + */ + fun addFrame(gray: ByteArray?, width: Int, height: Int, timestampMs: Long): Unit { + lock.withLock { + if ( + ((((gray == null) || (width <= 0)) || (height <= 0)) || + (gray.size < (width * height))) + ) { + return + } + if (frames.isEmpty()) { + this.width = width + this.height = height + } else { + if (((width != this.width) || (height != this.height))) { + return + } + } + var indices: ByteArray = ByteArray((width * height)) + run { + var i: Int = 0 + while ((i < indices.size)) { + indices[i] = (((gray[i] and 0xff) ushr 4)).toByte() + i++ + } + } + if (((lastIndices != null) && indices.contentEquals(lastIndices))) { + return + } + if ((frames.size >= MAX_FRAMES)) { + overflowed = true + return + } + frames.add(Frame(deflateData(indices), timestampMs)) + lastIndices = indices + } + } + + /** True if frames were dropped because the recording hit MAX_FRAMES. */ + fun isOverflowed(): Boolean { + lock.withLock { + return overflowed + } + } + + private fun writeGif(out: ByteSink): Unit { + out.write('G') + out.write('I') + out.write('F') + out.write('8') + out.write('9') + out.write('a') + writeShortLE(out, width) + writeShortLE(out, height) + out.write(((0x80 or 0x70) or 0x03)) + out.write(0) + out.write(0) + run { + var i: Int = 0 + while ((i < 16)) { + var v: Int = (i * 17) + out.write(v) + out.write(v) + out.write(v) + i++ + } + } + out.write(0x21) + out.write(0xff) + out.write(11) + out.write("NETSCAPE2.0".encodeToByteArray()) + out.write(3) + out.write(1) + writeShortLE(out, 0) + out.write(0) + run { + var i: Int = 0 + while ((i < frames.size)) { + val frame: Frame = frames.get(i) + var delayCs: Int = LAST_FRAME_DELAY_CS + if (((i + 1) < frames.size)) { + var deltaMs: Long = (frames.get((i + 1)).timestampMs - frame.timestampMs) + delayCs = + (maxOf( + MIN_FRAME_DELAY_CS.toLong(), + minOf(0xffffL, (deltaMs / 10.0).roundToLong()), + )) + .toInt() + } + out.write(0x21) + out.write(0xf9) + out.write(4) + out.write(0x04) + writeShortLE(out, delayCs) + out.write(0) + out.write(0) + out.write(0x2c) + writeShortLE(out, 0) + writeShortLE(out, 0) + writeShortLE(out, width) + writeShortLE(out, height) + out.write(0) + writeLzwImageData(out, inflateData(frame.deflatedIndices, (width * height))) + i++ + } + } + out.write(0x3b) + } + + /** + * Packs LZW codes LSB-first into bytes and the bytes into the 255-byte data sub-blocks GIF + * image data is carried in. + */ + private class SubBlockBitWriter { + private val out: ByteSink + + private val block: ByteArray = ByteArray(255) + + private var blockLength: Int = 0 + + private var bitBuffer: Int = 0 + + private var bitCount: Int = 0 + + constructor(out: ByteSink) { + this.out = out + } + + fun writeCode(code: Int, codeSize: Int): Unit { + bitBuffer = (bitBuffer or (code shl bitCount)) + bitCount += codeSize + while ((bitCount >= 8)) { + writeByte((bitBuffer and 0xff)) + bitBuffer = (bitBuffer ushr 8) + bitCount -= 8 + } + } + + fun finish(): Unit { + if ((bitCount > 0)) { + writeByte((bitBuffer and 0xff)) + bitBuffer = 0 + bitCount = 0 + } + flushBlock() + out.write(0) + } + + private fun writeByte(b: Int): Unit { + block[blockLength++] = (b).toByte() + if ((blockLength == 255)) { + flushBlock() + } + } + + private fun flushBlock(): Unit { + if ((blockLength == 0)) { + return + } + out.write(blockLength) + out.write(block, 0, blockLength) + blockLength = 0 + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedScreenshots.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedScreenshots.kt new file mode 100644 index 00000000..b0043485 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SharedScreenshots.kt @@ -0,0 +1,55 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmStatic + +/** Screen export codecs; filesystem destinations belong to platform adapters. */ +class SharedScreenshots private constructor() { + companion object { + @JvmStatic + fun encode4BitGrayPng(gray: ByteArray, width: Int, height: Int): ByteArray { + require(width > 0 && height > 0 && gray.size >= width * height) + val rowBytes = (width + 1) / 2 + val raw = ByteArray(height * (1 + rowBytes)) + var offset = 0 + for (y in 0 until height) { + raw[offset++] = 0 // PNG filter: None + for (x in 0 until width step 2) { + val hi = (gray[y * width + x].toInt() and 255) ushr 4 + val lo = + if (x + 1 < width) (gray[y * width + x + 1].toInt() and 255) ushr 4 else 0 + raw[offset++] = ((hi shl 4) or lo).toByte() + } + } + val out = ByteSink() + out.write(byteArrayOf(0x89.toByte(), 80, 78, 71, 13, 10, 26, 10)) + val header = ByteSink() + writeInt(header, width) + writeInt(header, height) + header.write(byteArrayOf(4, 0, 0, 0, 0)) + writeChunk(out, "IHDR", header.toByteArray()) + writeChunk(out, "IDAT", deflateData(raw)) + writeChunk(out, "IEND", ByteArray(0)) + return out.toByteArray() + } + + private fun writeChunk(out: ByteSink, type: String, data: ByteArray) { + writeInt(out, data.size) + val name = type.encodeToByteArray() + out.write(name) + out.write(data) + var crc = -1 + for (bytes in arrayOf(name, data)) for (byte in bytes) { + crc = crc xor (byte.toInt() and 255) + repeat(8) { crc = (crc ushr 1) xor (if (crc and 1 != 0) 0xedb88320.toInt() else 0) } + } + writeInt(out, crc.inv()) + } + + private fun writeInt(out: ByteSink, value: Int) { + out.write(value ushr 24) + out.write(value ushr 16) + out.write(value ushr 8) + out.write(value) + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SurfaceCompositor.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SurfaceCompositor.kt new file mode 100644 index 00000000..78c59e86 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SurfaceCompositor.kt @@ -0,0 +1,702 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * Retained-surface compositor: the phone-side model of what is on the glasses screen. + * + * Each surface is an 8bpp grayscale buffer retained at its last-submitted contents, positioned on + * the screen with a z-order and a transparency mode. Sources on the TS side (today the dashboard; + * later the shell and per-app worker threads) submit updates to their surface, and the compositor + * recombines the retained surfaces into a full-screen frame that feeds the existing + * dedupe/compress/transmit pipeline. Because every surface is retained, a full screen frame can be + * regenerated at any time (frame drops, reconnects, previews, screenshots) without asking sources + * to repaint. + * + * Surface format contract (mirrored by the TS side): + * - Pixels are 8bpp grayscale, row-major, one byte per pixel. The wire pipeline quantizes to 4bpp + * downstream, so the low nibble is not visible. + * - TRANSPARENCY_COLOR_KEY surfaces treat pixel value 0 as fully transparent and value 1 as black. + * Quantization collapses 1 into the same 4bpp level as 0, so reserving 0 costs no visible shade; + * painters of color-key surfaces must clamp intentional black to 1. + * - An update may cover any rect of its surface; the rest is retained. The update buffer holds + * exactly rectWidth*rectHeight bytes, row-major. + * - Surfaces composite in ascending z-order onto a black (0) background. + * - Surface geometry changes take effect when the next frame composites; they do not trigger a + * recomposite by themselves. + * + * Thread safety: all methods are safe to call from any thread. Apply and composite happen + * atomically under an internal lock, and each composite carries a monotonic sequence number so + * callers can detect when a composite was superseded by a concurrent one before being acted on. + */ +class SurfaceCompositor { + companion object { + const val TRANSPARENCY_OPAQUE: Int = 0 + + const val TRANSPARENCY_COLOR_KEY: Int = 1 + + private val NO_DRAWS: Array = emptyArray() + + private fun dimValue(value: Int, dim: Int): Int { + return maxOf(1, (((value * dim) + 128) shr 8)) + } + + private fun parseDraws(draws: ByteReader?): Array { + if (((draws == null) || (draws.remaining() < 1))) { + return NO_DRAWS + } + val cursor: ByteReader = draws + var out: MutableList = ArrayList() + while ((cursor.remaining() >= 1)) { + var kind: Int = (cursor.get() and 0xff) + if (((kind == ScreenDraw.KIND_GLYPH) && (cursor.remaining() >= 11))) { + var fontId: Int = (cursor.getShort().toInt() and 0xffff) + var encoding: Int = cursor.getInt() + var penX: Int = cursor.getShort().toInt() + var lineY: Int = cursor.getShort().toInt() + var value: Int = (cursor.get() and 0xff) + out.add(ScreenDraw.glyph(fontId, encoding, penX, lineY, value)) + } else { + if (((kind == ScreenDraw.KIND_IMAGE) && (cursor.remaining() >= 8))) { + var imageId: Int = cursor.getInt() + var x: Int = cursor.getShort().toInt() + var y: Int = cursor.getShort().toInt() + out.add(ScreenDraw.image(imageId, x, y)) + } else { + if (((kind == ScreenDraw.KIND_FWTEXT) && (cursor.remaining() >= 6))) { + var x: Int = cursor.getShort().toInt() + var y: Int = cursor.getShort().toInt() + var value: Int = (cursor.get() and 0xff) + var count: Int = (cursor.get() and 0xff) + if ((cursor.remaining() < (count * 7))) { + break + } + var cps: IntArray = IntArray(count) + var dx: IntArray = IntArray(count) + var ink: BooleanArray = BooleanArray(count) + run { + var i: Int = 0 + while ((i < count)) { + cps[i] = cursor.getInt() + dx[i] = cursor.getShort().toInt() + ink[i] = (cursor.get().toInt() != 0) + i++ + } + } + out.add(ScreenDraw.fwText(x, y, value, cps, dx, ink)) + } else { + break + } + } + } + } + return out.toTypedArray() + } + } + + /** + * One deferred draw (a text glyph or an icon image) within a frame, in screen coordinates. The + * draw's pixels are already baked into the composited gray buffer (the TS side bakes before + * submitting); this record preserves the draw's identity so the texture-cache planner can + * replay it as an on-glasses cached draw instead of image bytes. + * + * Glyphs: x/y are the pen position and line top; the raster (and its bearing/cell placement) + * comes from GlyphAtlas under (fontId, encoding). Images: x/y are the blit's top-left; the + * raster comes from ImageAtlas under imageId. + */ + class ScreenDraw { + companion object { + const val KIND_GLYPH: Int = 0 + + const val KIND_IMAGE: Int = 1 + + /** A firmware-builtin-font text run (CFW mode 15). */ + const val KIND_FWTEXT: Int = 2 + + @JvmStatic + fun glyph(fontId: Int, encoding: Int, penX: Int, lineY: Int, value: Int): ScreenDraw { + return ScreenDraw( + KIND_GLYPH, + fontId, + encoding, + 0, + penX, + lineY, + value, + null, + null, + null, + ) + } + + @JvmStatic + fun image(imageId: Int, x: Int, y: Int): ScreenDraw { + return ScreenDraw(KIND_IMAGE, 0, 0, imageId, x, y, 0, null, null, null) + } + + @JvmStatic + fun fwText( + x: Int, + y: Int, + value: Int, + cps: IntArray, + dx: IntArray, + ink: BooleanArray, + ): ScreenDraw { + return ScreenDraw(KIND_FWTEXT, 0, 0, 0, x, y, value, cps, dx, ink) + } + } + + @JvmField val kind: Int + + /** Glyph draws only. */ + @JvmField val fontId: Int + + /** Glyph draws only. */ + @JvmField val encoding: Int + + /** Image draws only. */ + @JvmField val imageId: Int + + @JvmField val x: Int + + @JvmField val y: Int + + /** Glyph and fw-text draws: 8-bit brightness. */ + @JvmField val value: Int + + /** Fw-text runs only: member codepoints, in text order. */ + @JvmField val fwCps: IntArray? + + /** Fw-text runs only: member pen offsets relative to x. */ + @JvmField val fwDx: IntArray? + + /** Fw-text runs only: whether each member has visible pixels. */ + @JvmField val fwInk: BooleanArray? + + constructor( + kind: Int, + fontId: Int, + encoding: Int, + imageId: Int, + x: Int, + y: Int, + value: Int, + fwCps: IntArray?, + fwDx: IntArray?, + fwInk: BooleanArray?, + ) { + this.kind = kind + this.fontId = fontId + this.encoding = encoding + this.imageId = imageId + this.x = x + this.y = y + this.value = value + this.fwCps = fwCps + this.fwDx = fwDx + this.fwInk = fwInk + } + } + + /** One composited full-screen frame plus the metadata the pipeline needs. */ + class Composite { + /** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */ + @JvmField val gray: ByteArray + + @JvmField val width: Int + + @JvmField val height: Int + + /** + * Stable identifier of screen content, combining every surface's geometry and content + * fingerprint; equal fingerprints mean equal composited pixels. + */ + @JvmField val fingerprint: String + + /** Monotonic: a Composite with a higher seq contains strictly newer state. */ + @JvmField val seq: Long + + /** + * Screen-space deferred draws of every visible surface, in composite order (surface z + * ascending, then each surface's draw order). Their pixels are already baked into gray. + */ + @JvmField val draws: Array? + + constructor( + gray: ByteArray, + width: Int, + height: Int, + fingerprint: String, + seq: Long, + draws: Array?, + ) { + this.gray = gray + this.width = width + this.height = height + this.fingerprint = fingerprint + this.seq = seq + this.draws = (if ((draws == null)) NO_DRAWS else draws) + } + } + + private class Surface { + @JvmField val id: String + + @JvmField var x: Int = 0 + + @JvmField var y: Int = 0 + + @JvmField var width: Int = 0 + + @JvmField var height: Int = 0 + + @JvmField var zOrder: Int = 0 + + @JvmField var transparency: Int = 0 + + @JvmField var visible: Boolean = true + + @JvmField var pixels: ByteArray = ByteArray(0) + + @JvmField var fingerprint: String = "" + + /** Surface-local deferred draws of the retained content (already baked into pixels). */ + @JvmField var draws: Array = NO_DRAWS + + constructor(id: String) { + this.id = id + } + } + + private val lock = protocolPlatform().createLock() + + private var screenWidth: Int = 0 + + private var screenHeight: Int = 0 + + private var blanked: Boolean = false + + /** See setUnderlayDim: surfaces with zOrder below this are dimmed by underlayDim/256. */ + private var underlayDimBelowZOrder: Int = Int.MIN_VALUE + + private var underlayDim: Int = 256 + + private val surfaces: MutableMap = HashMap() + + private var nextCompositeSeq: Long = 1 + + /** Set the output frame size. Must be called before any surface work. */ + fun configureScreen(width: Int, height: Int): Unit { + if (((width <= 0) || (height <= 0))) { + throw IllegalArgumentException(((("bad screen size " + width) + "x") + height)) + } + lock.withLock { + this.screenWidth = width + this.screenHeight = height + } + } + + /** + * Create a surface or update an existing one's geometry. A resize discards the surface's + * retained pixels (reset to 0). + */ + fun configureSurface( + id: String?, + x: Int, + y: Int, + width: Int, + height: Int, + zOrder: Int, + transparency: Int, + ): Unit { + if (((id == null) || id.isEmpty())) { + throw IllegalArgumentException("surface id must be non-empty") + } + if (((width <= 0) || (height <= 0))) { + throw IllegalArgumentException( + ((((("bad surface size " + width) + "x") + height) + " for ") + id) + ) + } + if (((transparency != TRANSPARENCY_OPAQUE) && (transparency != TRANSPARENCY_COLOR_KEY))) { + throw IllegalArgumentException( + ((("bad transparency mode " + transparency) + " for ") + id) + ) + } + lock.withLock { + requireScreenConfiguredLocked() + var surface: Surface? = surfaces.get(id) + if ((surface == null)) { + surface = Surface(id) + surfaces.put(id, surface) + } + if ( + (((surface.pixels == null) || (surface.width != width)) || + (surface.height != height)) + ) { + surface.pixels = ByteArray((width * height)) + surface.fingerprint = "" + surface.draws = NO_DRAWS + } + surface.x = x + surface.y = y + surface.width = width + surface.height = height + surface.zOrder = zOrder + surface.transparency = transparency + } + } + + fun removeSurface(id: String): Unit { + lock.withLock { + surfaces.remove(id) + } + } + + /** + * Dim every surface whose zOrder is below belowZOrder to factor256/256 of its brightness (256 = + * no dimming): how a shell overlay that dims what it covers (the TS Layer.dimUnderneath) + * reaches the window surfaces beneath the shell surface, which the shell's own layer stack + * cannot paint. Visible pixels stay at least 1 (the color-key black); glyph and firmware-text + * draws keep their cached-draw form with a dimmed value; image draws leave the composite's draw + * list (their pixels, already baked into the surface, dim as raster). Takes effect when the + * next frame composites. + */ + fun setUnderlayDim(belowZOrder: Int, factor256: Int): Unit { + lock.withLock { + underlayDimBelowZOrder = belowZOrder + underlayDim = maxOf(0, minOf(256, factor256)) + } + } + + /** The dim factor (256 = none) that applies to a surface. */ + private fun dimForLocked(surface: Surface): Int { + return (if ((surface.zOrder < underlayDimBelowZOrder)) underlayDim else 256) + } + + /** + * Hidden surfaces keep their retained pixels and accept updates, but are excluded from the + * composite and its fingerprint (used for background windows). Takes effect when the next frame + * composites. + */ + fun setSurfaceVisible(id: String, visible: Boolean): Unit { + lock.withLock { + var surface: Surface? = surfaces.get(id) + if ((surface == null)) { + throw IllegalArgumentException(("unknown surface " + id)) + } + surface.visible = visible + } + } + + /** + * While blanked (screen off), composites are all-zero regardless of surface content; retained + * state is untouched, so unblanking restores the screen without asking sources to repaint. + */ + fun setBlanked(blanked: Boolean): Unit { + lock.withLock { + this.blanked = blanked + } + } + + /** + * Apply an update to one surface and composite the whole screen, as one atomic step. The update + * covers the rect (rectX, rectY, rectWidth, rectHeight) in surface-local coordinates; pixels + * must hold exactly rectWidth*rectHeight bytes. contentFingerprint identifies the surface's + * full content after this update. + */ + fun applyAndComposite( + surfaceId: String, + pixels: ByteReader, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String, + ): Composite { + return applyAndComposite( + surfaceId, + pixels, + rectX, + rectY, + rectWidth, + rectHeight, + contentFingerprint, + null, + ) + } + + /** + * As above, with the frame's deferred draws: a little-endian buffer of tagged records + * [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph) [1][imageId u32][x + * s16][y s16] (image) in surface-local coordinates and draw order. The list describes the + * surface's FULL retained content and replaces the previous list, so it is only meaningful for + * full-surface updates (which is what every submitter sends); pass null to clear. Draw pixels + * must already be baked into pixels. + */ + fun applyAndComposite( + surfaceId: String, + pixels: ByteReader?, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String?, + glyphs: ByteReader?, + ): Composite = + updateSurface( + surfaceId, + pixels, + rectX, + rectY, + rectWidth, + rectHeight, + contentFingerprint, + glyphs, + true, + )!! + + /** Retain pixels without recompositing; iOS coalesces updates before its frame callback. */ + fun submitSurface( + surfaceId: String, + pixels: ByteReader, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + fingerprint: String, + ) { + updateSurface( + surfaceId, + pixels, + rectX, + rectY, + rectWidth, + rectHeight, + fingerprint, + null, + false, + ) + } + + private fun updateSurface( + surfaceId: String, + pixels: ByteReader?, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String?, + glyphs: ByteReader?, + composeAfter: Boolean, + ): Composite? { + var parsed: Array = parseDraws(glyphs) + lock.withLock { + requireScreenConfiguredLocked() + var surface: Surface? = surfaces.get(surfaceId) + if ((surface == null)) { + throw IllegalArgumentException(("unknown surface " + surfaceId)) + } + if ( + ((((((rectX < 0) || (rectY < 0)) || (rectWidth <= 0)) || (rectHeight <= 0)) || + ((rectX + rectWidth) > surface.width)) || + ((rectY + rectHeight) > surface.height)) + ) { + throw IllegalArgumentException( + (((((((((((((("update rect " + rectWidth) + "x") + rectHeight) + "+") + rectX) + + "+") + rectY) + " outside surface ") + surfaceId) + " (") + surface.width) + + "x") + surface.height) + ")") + ) + } + var expectedBytes: Int = (rectWidth * rectHeight) + if (((pixels == null) || (pixels.remaining() != expectedBytes))) { + throw IllegalArgumentException( + ((((("update buffer for " + surfaceId) + " has ") + + (if ((pixels == null)) 0 else pixels.remaining())) + " bytes, expected ") + + expectedBytes) + ) + } + run { + var row: Int = 0 + while ((row < rectHeight)) { + var dstOffset: Int = (((rectY + row) * surface.width) + rectX) + pixels.get(surface.pixels, dstOffset, rectWidth) + row++ + } + } + surface.fingerprint = (if ((contentFingerprint == null)) "" else contentFingerprint) + surface.draws = parsed + return if (composeAfter) compositeLocked() else null + } + } + + /** Composite the current retained state without applying an update. */ + fun composite(): Composite { + lock.withLock { + requireScreenConfiguredLocked() + return compositeLocked() + } + } + + /** + * Current composited pixels for the phone-side preview / screenshot, or null before the screen + * is configured. Does not consume a sequence number (it is never stored as the desired frame). + */ + fun previewComposite(): Composite? { + lock.withLock { + if (((screenWidth <= 0) || (screenHeight <= 0))) { + return null + } + var gray: ByteArray = buildGrayLocked() + return Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS) + } + } + + private fun buildGrayLocked(): ByteArray { + var gray: ByteArray = ByteArray((screenWidth * screenHeight)) + if (blanked) { + return gray + } + var ordered: MutableList = ArrayList(surfaces.values) + ordered.sortWith(compareBy { it.zOrder }.thenBy { it.id }) + for (surface in ordered) { + if (surface.visible) { + blendLocked(gray, surface) + } + } + return gray + } + + private fun compositeLocked(): Composite { + var gray: ByteArray = ByteArray((screenWidth * screenHeight)) + if (blanked) { + return Composite( + gray, + screenWidth, + screenHeight, + ((("blanked:" + screenWidth) + "x") + screenHeight), + nextCompositeSeq++, + NO_DRAWS, + ) + } + var ordered: MutableList = ArrayList(surfaces.values) + ordered.sortWith(compareBy { it.zOrder }.thenBy { it.id }) + var fingerprint: StringBuilder = StringBuilder() + fingerprint.append(screenWidth).append('x').append(screenHeight) + var draws: MutableList = ArrayList() + for (surface in ordered) { + if (!surface.visible) { + continue + } + blendLocked(gray, surface) + var dim: Int = dimForLocked(surface) + for (draw in surface.draws) { + if (((dim < 256) && (draw.kind == ScreenDraw.KIND_IMAGE))) { + continue + } + var value: Int = (if ((dim < 256)) dimValue(draw.value, dim) else draw.value) + draws.add( + ScreenDraw( + draw.kind, + draw.fontId, + draw.encoding, + draw.imageId, + (draw.x + surface.x), + (draw.y + surface.y), + value, + draw.fwCps, + draw.fwDx, + draw.fwInk, + ) + ) + } + fingerprint + .append('|') + .append(surface.id) + .append('@') + .append(surface.x) + .append(',') + .append(surface.y) + .append('+') + .append(surface.width) + .append('x') + .append(surface.height) + .append('#') + .append(surface.zOrder) + .append(':') + .append(surface.transparency) + .append(':') + .append(surface.fingerprint) + if ((dim < 256)) { + fingerprint.append(":dim").append(dim) + } + } + return Composite( + gray, + screenWidth, + screenHeight, + fingerprint.toString(), + nextCompositeSeq++, + draws.toTypedArray(), + ) + } + + private fun blendLocked(gray: ByteArray, surface: Surface): Unit { + var srcX: Int = maxOf(0, -surface.x) + var srcY: Int = maxOf(0, -surface.y) + var dstX: Int = maxOf(0, surface.x) + var dstY: Int = maxOf(0, surface.y) + var copyWidth: Int = minOf((surface.width - srcX), (screenWidth - dstX)) + var copyHeight: Int = minOf((surface.height - srcY), (screenHeight - dstY)) + if (((copyWidth <= 0) || (copyHeight <= 0))) { + return + } + var dim: Int = dimForLocked(surface) + run { + var row: Int = 0 + while ((row < copyHeight)) { + var srcOffset: Int = (((srcY + row) * surface.width) + srcX) + var dstOffset: Int = (((dstY + row) * screenWidth) + dstX) + if ((dim < 256)) { + run { + var col: Int = 0 + while ((col < copyWidth)) { + var value: Int = (surface.pixels[(srcOffset + col)] and 0xff) + if ((value.toInt() != 0)) { + gray[(dstOffset + col)] = (dimValue(value, dim)).toByte() + } else { + if ((surface.transparency == TRANSPARENCY_OPAQUE)) { + gray[(dstOffset + col)] = 0 + } + } + col++ + } + } + } else { + if ((surface.transparency == TRANSPARENCY_OPAQUE)) { + surface.pixels.copyInto(gray, dstOffset, srcOffset, srcOffset + copyWidth) + } else { + run { + var col: Int = 0 + while ((col < copyWidth)) { + var value: Byte = surface.pixels[(srcOffset + col)] + if ((value.toInt() != 0)) { + gray[(dstOffset + col)] = value + } + col++ + } + } + } + } + row++ + } + } + } + + private fun requireScreenConfiguredLocked(): Unit { + if (((screenWidth <= 0) || (screenHeight <= 0))) { + throw IllegalStateException("configureScreen must be called first") + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BinaryEncoding.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BinaryEncoding.kt new file mode 100644 index 00000000..9a7c1f77 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BinaryEncoding.kt @@ -0,0 +1,45 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmStatic + +/** Binary encodings shared by firmware downloads and audio recordings. */ +object BinaryEncoding { + @JvmStatic + fun bytesToHex(bytes: ByteArray?): String { + if (bytes == null) return "" + val digits = "0123456789abcdef" + return buildString(bytes.size * 2) { + for (byte in bytes) { + val value = byte.toInt() and 255 + append(digits[value ushr 4]) + append(digits[value and 15]) + } + } + } + + @JvmStatic + fun wavHeader(pcmBytes: Int, sampleRate: Int, channels: Int): ByteArray { + val out = ByteSink(44) + fun u16(value: Int) { + out.write(value) + out.write(value ushr 8) + } + fun u32(value: Int) { + u16(value) + u16(value ushr 16) + } + out.write("RIFF".encodeToByteArray()) + u32(36 + pcmBytes) + out.write("WAVEfmt ".encodeToByteArray()) + u32(16) + u16(1) + u16(channels) + u32(sampleRate) + u32(sampleRate * channels * 2) + u16(channels * 2) + u16(16) + out.write("data".encodeToByteArray()) + u32(pcmBytes) + return out.toByteArray() + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BmpUtil.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BmpUtil.kt new file mode 100644 index 00000000..b68046cf --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/BmpUtil.kt @@ -0,0 +1,150 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmStatic + +class BmpUtil { + companion object { + @JvmStatic + fun copyTileBmp(bmp: ByteArray?): ByteArray { + if (((bmp == null) || (bmp.size == 0))) { + return ByteArray(0) + } + return bmp.copyOf(bmp.size) + } + + private val GRAY_TO_NIBBLE = + ByteArray(256) { v -> if (v == 0) 0 else minOf(15, (v + 8) shr 4).toByte() } + + /** + * The 4bpp level an 8-bit gray value packs to. Public so the texture-cache planner computes + * a glyph draw's top color with the exact quantization the composited frame was packed + * with. + */ + @JvmStatic + fun nibbleForGray(gray: Int): Int { + return (GRAY_TO_NIBBLE[(gray and 0xff)] and 0xff) + } + + /** + * Pack a full-resolution 8bpp grayscale buffer (row-major, top-to-bottom, one byte per + * pixel) into the headerless 4bpp wire format used by CFW load_image_z modes 3/6/8: + * top-down rows, stride ceil(width/2), high nibble = left pixel. This is the canonical + * in-memory frame format; BMP framing (needed only when an uncompressed full frame goes on + * the wire) is added on demand by build4bppBmpFromPacked. + */ + @JvmStatic + fun pack4bppFromGray8(gray8: ByteArray?, width: Int, height: Int): ByteArray { + if (((width <= 0) || (height <= 0))) { + return ByteArray(0) + } + var stride: Int = ((width + 1) shr 1) + var out: ByteArray = ByteArray((stride * height)) + if (((gray8 == null) || (gray8.size < (width * height)))) { + return out + } + var pairs: Int = (width shr 1) + var src: Int = 0 + var dst: Int = 0 + run { + var y: Int = 0 + while ((y < height)) { + run { + var i: Int = 0 + while ((i < pairs)) { + out[dst++] = + (((GRAY_TO_NIBBLE[(gray8[src] and 0xff)] shl 4) or + (GRAY_TO_NIBBLE[(gray8[(src + 1)] and 0xff)] and 0xff))) + .toByte() + src += 2 + i++ + } + } + if (((width and 1) != 0)) { + out[dst++] = ((GRAY_TO_NIBBLE[(gray8[src++] and 0xff)] shl 4)).toByte() + } + y++ + } + } + return out + } + + /** + * Wrap a packed 4bpp frame in BMP framing (BITMAPINFOHEADER + 16-entry gray palette, + * bottom-up rows padded to a 4-byte stride). Only the full-frame fallback used when mode-6 + * compression does not shrink the frame still speaks BMP on the wire; everything else uses + * the headerless packed format directly. + */ + @JvmStatic + fun build4bppBmpFromPacked(packed: ByteArray?, width: Int, height: Int): ByteArray { + var packedStride: Int = ((width + 1) shr 1) + if ( + ((((width <= 0) || (height <= 0)) || (packed == null)) || + (packed.size < (packedStride * height))) + ) { + return ByteArray(0) + } + var rowStride: Int = ((packedStride + 3) and 3.inv()) + var pixelDataSize: Int = (rowStride * height) + var fileHeaderSize: Int = 14 + var dibHeaderSize: Int = 40 + var paletteSize: Int = (16 * 4) + var pixelOffset: Int = ((fileHeaderSize + dibHeaderSize) + paletteSize) + var fileSize: Int = (pixelOffset + pixelDataSize) + var buf: ByteArray = ByteArray(fileSize) + buf[0] = 0x42 + buf[1] = 0x4 + putUint32Le(buf, 2, fileSize) + putUint32Le(buf, 10, pixelOffset) + putUint32Le(buf, 14, dibHeaderSize) + putInt32Le(buf, 18, width) + putInt32Le(buf, 22, height) + putUint16Le(buf, 26, 1) + putUint16Le(buf, 28, 4) + putUint32Le(buf, 30, 0) + putUint32Le(buf, 34, pixelDataSize) + putUint32Le(buf, 46, 16) + run { + var i: Int = 0 + while ((i < 16)) { + var v: Int = (i * 17) + var base: Int = ((fileHeaderSize + dibHeaderSize) + (i * 4)) + buf[base] = (v).toByte() + buf[(base + 1)] = (v).toByte() + buf[(base + 2)] = (v).toByte() + buf[(base + 3)] = 0 + i++ + } + } + run { + var bmpRow: Int = 0 + while ((bmpRow < height)) { + var srcY: Int = ((height - 1) - bmpRow) + packed.copyInto( + buf, + (pixelOffset + (bmpRow * rowStride)), + (srcY * packedStride), + (srcY * packedStride) + packedStride, + ) + bmpRow++ + } + } + return buf + } + + private fun putUint16Le(buf: ByteArray, offset: Int, value: Int): Unit { + buf[offset] = ((value and 0xff)).toByte() + buf[(offset + 1)] = (((value ushr 8) and 0xff)).toByte() + } + + private fun putUint32Le(buf: ByteArray, offset: Int, value: Int): Unit { + buf[offset] = ((value and 0xff)).toByte() + buf[(offset + 1)] = (((value ushr 8) and 0xff)).toByte() + buf[(offset + 2)] = (((value ushr 16) and 0xff)).toByte() + buf[(offset + 3)] = (((value ushr 24) and 0xff)).toByte() + } + + private fun putInt32Le(buf: ByteArray, offset: Int, value: Int): Unit { + putUint32Le(buf, offset, value) + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteReader.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteReader.kt new file mode 100644 index 00000000..cfb69420 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteReader.kt @@ -0,0 +1,43 @@ +package com.faceclaw.app + +/** Sequential little-endian input. Platform adapters can read native buffers without a copy. */ +abstract class ByteReader { + abstract fun remaining(): Int + + abstract fun get(): Byte + + abstract fun get(index: Int): Byte + + open fun get(bytes: ByteArray): ByteReader = get(bytes, 0, bytes.size) + + open fun get(bytes: ByteArray, offset: Int, length: Int): ByteReader { + require( + length >= 0 && offset >= 0 && offset <= bytes.size - length && remaining() >= length + ) + for (i in offset until offset + length) bytes[i] = get() + return this + } + + fun getShort(): Short = ((get().toInt() and 255) or ((get().toInt() and 255) shl 8)).toShort() + + fun getInt(): Int = (getShort().toInt() and 65535) or (getShort().toInt() shl 16) +} + +class ArrayByteReader(private val bytes: ByteArray) : ByteReader() { + private var position = 0 + + override fun remaining(): Int = bytes.size - position + + override fun get(): Byte = bytes[position++] + + override fun get(index: Int): Byte = bytes[index] + + override fun get(bytes: ByteArray, offset: Int, length: Int): ByteReader { + require( + length >= 0 && offset >= 0 && offset <= bytes.size - length && remaining() >= length + ) + this.bytes.copyInto(bytes, offset, position, position + length) + position += length + return this + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteSink.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteSink.kt new file mode 100644 index 00000000..3362e26d --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/ByteSink.kt @@ -0,0 +1,44 @@ +package com.faceclaw.app + +/** Growable byte buffer for wire encoders; no streams or platform objects escape. */ +internal class ByteSink(capacity: Int = 32) { + private var bytes = ByteArray(capacity.coerceAtLeast(1)) + private var count = 0 + + fun size(): Int = count + + fun write(value: Char) = write(value.code) + + fun write(value: ByteArray) = write(value, 0, value.size) + + fun write(value: Int) { + reserve(1) + bytes[count++] = value.toByte() + } + + fun write(value: ByteArray, offset: Int, length: Int) { + require(offset >= 0 && length >= 0 && offset <= value.size - length) + reserve(length) + value.copyInto(bytes, count, offset, offset + length) + count += length + } + + fun toByteArray(): ByteArray = bytes.copyOf(count) + + private fun reserve(extra: Int) { + if (count + extra > bytes.size) bytes = bytes.copyOf(maxOf(count + extra, bytes.size * 2)) + } +} + +internal infix fun Byte.and(mask: Int): Int = toInt() and mask + +internal infix fun Byte.shl(bits: Int): Int = toInt() shl bits + +internal fun codePointUtf8(codePoint: Int): ByteArray { + require(codePoint in 0..0x10ffff) + if (codePoint <= 0xffff) return codePoint.toChar().toString().encodeToByteArray() + val value = codePoint - 0x10000 + return charArrayOf((0xd800 + (value ushr 10)).toChar(), (0xdc00 + (value and 1023)).toChar()) + .concatToString() + .encodeToByteArray() +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/CollectionUtils.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/CollectionUtils.kt new file mode 100644 index 00000000..45012c10 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/util/CollectionUtils.kt @@ -0,0 +1,31 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmStatic + +class CollectionUtils { + companion object { + @JvmStatic + fun singletonList(value: ByteArray): MutableList { + var list: MutableList = ArrayList(1) + list.add(value) + return list + } + + @JvmStatic + fun listOf(a: ByteArray, b: ByteArray): MutableList { + var list: MutableList = ArrayList(2) + list.add(a) + list.add(b) + return list + } + + @JvmStatic + fun listOf(a: ByteArray, b: ByteArray, c: ByteArray): MutableList { + var list: MutableList = ArrayList(3) + list.add(a) + list.add(b) + list.add(c) + return list + } + } +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/shared/KotlinBridge.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/shared/KotlinBridge.kt new file mode 100644 index 00000000..b43e81ab --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/shared/KotlinBridge.kt @@ -0,0 +1,35 @@ +package com.faceclaw.shared + +/** NativeScript implements this Kotlin-owned contract in TypeScript. */ +interface KotlinBridgeListener { + fun transform(message: String): String +} + +/** + * Portable Kotlin at the same commonMain source path used by ios-port. Calls and callbacks are + * synchronous on the caller's thread. The listener is used only during roundTrip and is never + * retained. + */ +class KotlinBridge { + fun platform(): String = PlatformInfo.name + + fun threadName(): String = PlatformInfo.threadName() + + fun isMainThread(): Boolean = PlatformInfo.isMainThread() + + fun greet(name: String): String = "Hello, $name, from Kotlin" + + fun roundTrip(message: String, listener: KotlinBridgeListener): String { + val reply = listener.transform("Kotlin received: $message") + // Consume the TypeScript return value, then return it across the bridge. + return "Kotlin received callback: $reply" + } +} + +internal expect object PlatformInfo { + val name: String + + fun threadName(): String + + fun isMainThread(): Boolean +} diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt new file mode 100644 index 00000000..e461a9b6 --- /dev/null +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/IosNativeApi.kt @@ -0,0 +1,264 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +package com.faceclaw.app + +import kotlinx.cinterop.* +import platform.Foundation.NSData +import platform.Foundation.create +import platform.posix.memcpy + +/** NSData keeps large binary crossings out of per-element JavaScript bridge loops. */ +internal fun NSData.byteArray(): ByteArray { + require(length <= Int.MAX_VALUE.toULong()) + val result = ByteArray(length.toInt()) + if (result.isNotEmpty()) result.usePinned { memcpy(it.addressOf(0), bytes, length) } + return result +} + +internal fun ByteArray.data(): NSData = + if (isEmpty()) NSData() + else + usePinned { + NSData.create(bytes = it.addressOf(0), length = size.toULong()) + } + +class IosByteReader(private val data: NSData) : ByteReader() { + private var position = 0 + + override fun remaining(): Int = data.length.toInt() - position + + override fun get(): Byte = get(position++) + + override fun get(index: Int): Byte { + require(index >= 0 && index < data.length.toInt()) + return data.bytes!!.reinterpret()[index] + } + + override fun get(bytes: ByteArray, offset: Int, length: Int): ByteReader { + require( + offset >= 0 && length >= 0 && offset <= bytes.size - length && remaining() >= length + ) + if (length > 0) + bytes.usePinned { + memcpy( + it.addressOf(offset), + data.bytes!!.reinterpret() + position, + length.toULong(), + ) + } + position += length + return this + } +} + +class IosCfwTransport { + private val transport = CfwTransport(IosProtocolPlatform) + + fun reset() = transport.reset() + + fun close() = transport.close() + + fun encode(data: NSData, streamId: Int, lenses: Int, maxWrite: Int): List = + transport.encode(data.byteArray(), streamId, lenses, maxWrite + 3).map { it.data() } +} + +class IosProtocolMessage(message: MessageReceiver.Message) { + val sid = message.sid + val flag = message.flag + val payload = message.payload.data() + val command = message.command + val magic = message.magic + val packet = message.packet?.data() +} + +class IosMessageReceiver { + private val receiver = MessageReceiver() + + fun clear() = receiver.clear() + + fun receive(link: String, data: NSData, now: Long): List = + receiver.receive(link, data.byteArray(), now).map { IosProtocolMessage(it) } +} + +/** Stable binary facade over the shared wire algorithms and font parser. */ +class IosProtocol { + fun authentication(magic: Int): NSData = BleProtocol.buildAuthenticationRequest(magic).data() + + fun prelude(): NSData = BleProtocol.PRELUDE_F5872_PAYLOAD.data() + + fun heartbeat(magic: Int): NSData = BleProtocol.buildHeartbeat(magic).data() + + fun audioControl(magic: Int, enabled: Boolean): NSData = + BleProtocol.buildAudioControl(magic, enabled).data() + + fun settingsQuery(magic: Int): NSData = BleProtocol.buildSettingsQuery(magic).data() + + fun shutdown(magic: Int): NSData = BleProtocol.buildShutdown(magic, 1).data() + + fun framebufferLease(acquire: Boolean): NSData = + BleProtocol.buildFaceclawWakeControl( + if (acquire) BleProtocol.FACECLAW_FB_OP_ACQUIRE + else BleProtocol.FACECLAW_FB_OP_RELEASE, + 0, + ) + .data() + + fun createLayout(magic: Int): NSData = BleProtocol.buildCreateInputPage(magic).data() + + fun integer(field: Int, value: Int): NSData = BleProtocol.encodeVarintField(field, value).data() + + fun bytes(field: Int, data: NSData): NSData = + BleProtocol.encodeBytesField(field, data.byteArray()).data() + + fun crc(data: NSData): Int = CfwTransport.crc(data.byteArray()) + + fun frame(data: NSData, sid: Int, flag: Int, sequence: Int, maxWrite: Int): List = + BleProtocol.framePb(data.byteArray(), sid, flag, sequence, maxWrite).map { it.data() } + + fun acks(data: NSData): List? = + CfwTransport.parseAcks(data.byteArray())?.toList() + + fun readInteger(data: NSData, field: Int, fallback: Int): Int = + BleProtocol.readVarintFieldValue(data.byteArray(), field, fallback) + + fun readBytes(data: NSData, field: Int): NSData? = + BleProtocol.readFieldBytes(data.byteArray(), field)?.data() + + fun readString(data: NSData, field: Int): String = + BleProtocol.readStringFieldValue(data.byteArray(), field) + + fun glassesInput(data: NSData, sid: Int, flag: Int): G2Event? = + if (sid == BleProtocol.SID_EVENHUB && flag in intArrayOf(1, 6)) + G2Event.decodePayload(sid, data.byteArray()) + else null + + fun ringInput(data: NSData): G2Event? = FaceclawRingEventDecoder.decode(data.byteArray())?.event + + fun pack(data: NSData, width: Int, height: Int): NSData { + require(width > 0 && height > 0 && data.length == width.toULong() * height.toULong()) + return BmpUtil.pack4bppFromGray8(data.byteArray(), width, height).data() + } + + fun rle(data: NSData): NSData = BleImageOptimizer.rleEncode(data.byteArray()).data() + + fun boundingBox( + previous: NSData?, + next: NSData, + width: Int, + height: Int, + frameId: Int, + ): NSData? = + BleImageOptimizer.buildIncrementalImagePayload( + previous?.byteArray(), + next.byteArray(), + width, + height, + frameId, + ) + ?.payload + ?.data() + + fun fullFrameBands(data: NSData, width: Int, height: Int, firstId: Int): List { + require( + width in 1..640 && + width % 4 == 0 && + height in 1..480 && + height % 2 == 0 && + data.length == (width * height / 2).toULong() + ) + val packed = data.byteArray() + var id = firstId + return (0 until height step 64).map { top -> + val result = + BleImageOptimizer.encodeMode3Rect( + packed, + width / 2, + 0, + top, + width, + minOf(64, height - top), + id, + ) + .data() + id = if (id >= 0xfffe) 1 else id + 1 + result + } + } + + fun fontMetrics(path: String): NSData = LvglFontFile.getMetrics(path).data() + + fun fontGlyph(path: String, codePoint: Int): NSData = + LvglFontFile.getGlyph(path, codePoint).data() + + fun png(data: NSData, width: Int, height: Int): NSData = + SharedScreenshots.encode4BitGrayPng(data.byteArray(), width, height).data() +} + +/** iOS retains/coalesces surface updates in the same compositor Android uses. */ +class IosSurfaceCompositor(width: Int, height: Int) { + private val compositor = SurfaceCompositor().also { it.configureScreen(width, height) } + private val sizes = mutableMapOf>() + private var sequence = 0L + + fun configure( + id: String, + x: Int, + y: Int, + width: Int, + height: Int, + zOrder: Int, + transparent: Boolean, + ) { + compositor.configureSurface(id, x, y, width, height, zOrder, if (transparent) 1 else 0) + sizes[id] = width to height + } + + fun remove(id: String) { + sizes.remove(id) + compositor.removeSurface(id) + } + + fun visible(id: String, visible: Boolean) = compositor.setSurfaceVisible(id, visible) + + fun dim(below: Int, factor: Double) { + require(factor.isFinite()) + compositor.setUnderlayDim(below, (factor.coerceIn(0.0, 1.0) * 256).toInt()) + } + + fun blank(blanked: Boolean) = compositor.setBlanked(blanked) + + fun submit(id: String, data: NSData, x: Int, y: Int, width: Int, height: Int) { + val (sw, sh) = sizes[id] ?: error("Unknown surface: $id") + require(width > 0 && height > 0 && data.length == width.toULong() * height.toULong()) + val left = maxOf(0, x) + val right = minOf(sw, x + width) + val top = maxOf(0, y) + val bottom = minOf(sh, y + height) + if (right <= left || bottom <= top) return + val reader = + if (left == x && top == y && right - left == width && bottom - top == height) + IosByteReader(data) + else { + val source = data.byteArray() + val clipped = ByteArray((right - left) * (bottom - top)) + for (row in top until bottom) source.copyInto( + clipped, + (row - top) * (right - left), + (row - y) * width + left - x, + (row - y) * width + right - x, + ) + ArrayByteReader(clipped) + } + compositor.submitSurface( + id, + reader, + left, + top, + right - left, + bottom - top, + (++sequence).toString(), + ) + } + + fun composite(): NSData = compositor.composite().gray.data() +} diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformServices.ios.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformServices.ios.kt new file mode 100644 index 00000000..91525178 --- /dev/null +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/PlatformServices.ios.kt @@ -0,0 +1,72 @@ +package com.faceclaw.app + +import kotlinx.cinterop.* + +internal actual fun protocolPlatform(): ProtocolPlatform = IosProtocolPlatform + +@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) +internal actual fun readFileData(path: String): ByteArray { + val file = platform.posix.fopen(path, "rb") ?: error("Cannot read $path") + try { + check(platform.posix.fseek(file, 0, platform.posix.SEEK_END) == 0) + val size = platform.posix.ftell(file) + require(size in 0..Int.MAX_VALUE.toLong()) + platform.posix.rewind(file) + val bytes = ByteArray(size.toInt()) + if (bytes.isNotEmpty()) + bytes.usePinned { + check( + platform.posix.fread(it.addressOf(0), 1u, bytes.size.toULong(), file) == + bytes.size.toULong() + ) + } + return bytes + } finally { + platform.posix.fclose(file) + } +} + +@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) +internal actual fun deflateData(bytes: ByteArray): ByteArray = + kotlinx.cinterop.memScoped { + val capacity = platform.zlib.compressBound(bytes.size.toULong()) + val result = ByteArray(capacity.toInt()) + val length = alloc() + length.value = capacity + bytes.usePinned { input -> + result.usePinned { output -> + check( + platform.zlib.compress2( + output.addressOf(0).reinterpret(), + length.ptr, + if (bytes.isEmpty()) null else input.addressOf(0).reinterpret(), + bytes.size.toULong(), + platform.zlib.Z_DEFAULT_COMPRESSION, + ) == platform.zlib.Z_OK + ) + } + } + result.copyOf(length.value.toInt()) + } + +@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) +internal actual fun inflateData(bytes: ByteArray, size: Int): ByteArray = + kotlinx.cinterop.memScoped { + val result = ByteArray(size) + val length = alloc() + length.value = size.toULong() + bytes.usePinned { input -> + result.usePinned { output -> + check( + platform.zlib.uncompress( + output.addressOf(0).reinterpret(), + length.ptr, + input.addressOf(0).reinterpret(), + bytes.size.toULong(), + ) == platform.zlib.Z_OK + ) + } + } + check(length.value.toInt() == size) { "recorded frame truncated" } + result + } diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/g2protocol/IosProtocolPlatform.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/g2protocol/IosProtocolPlatform.kt new file mode 100644 index 00000000..3b24fcca --- /dev/null +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/app/g2protocol/IosProtocolPlatform.kt @@ -0,0 +1,129 @@ +@file:OptIn( + kotlinx.cinterop.ExperimentalForeignApi::class, + kotlin.experimental.ExperimentalNativeApi::class, +) + +package com.faceclaw.app + +import kotlin.native.ref.createCleaner +import kotlinx.cinterop.* +import platform.CoreBluetooth.CBCharacteristicWriteType +import platform.CoreBluetooth.CBCharacteristicWriteWithResponse +import platform.CoreBluetooth.CBCharacteristicWriteWithoutResponse +import platform.Foundation.NSRecursiveLock +import platform.posix.CLOCK_MONOTONIC_RAW +import platform.posix.clock_gettime_nsec_np +import platform.posix.memset +import platform.zlib.* + +object IosProtocolPlatform : ProtocolPlatform { + // Unlike wall time, this cannot jump; unlike uptime, it includes sleep. + override fun elapsedRealtimeMs(): Long = + (clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW.convert()) / 1_000_000uL).toLong() + + override fun createLock(): ProtocolLock = + object : ProtocolLock { + private val lock = NSRecursiveLock() + + override fun lock() = lock.lock() + + override fun unlock() = lock.unlock() + } + + override fun createDeflater(): ProtocolDeflater = IosDeflater() + + fun writeType(mode: GattWriteMode): CBCharacteristicWriteType = + when (mode) { + GattWriteMode.WITH_RESPONSE -> CBCharacteristicWriteWithResponse + GattWriteMode.WITHOUT_RESPONSE -> CBCharacteristicWriteWithoutResponse + } +} + +/** The cleaner holds only the allocation state, never the owning deflater. */ +private class ZlibState { + private val lock = NSRecursiveLock() + var stream: CPointer? = nativeHeap.alloc().ptr + private set + + init { + memset(stream, 0, sizeOf().convert()) + val status = deflateInit(stream, Z_DEFAULT_COMPRESSION) + if (status != Z_OK) { + nativeHeap.free(stream!!) + stream = null + error("zlib initialization failed: $status") + } + } + + fun use(action: (CPointer) -> T): T { + lock.lock() + try { + return action(checkNotNull(stream) { "zlib stream is closed" }) + } finally { + lock.unlock() + } + } + + fun close() { + lock.lock() + try { + val value = stream ?: return + deflateEnd(value) + nativeHeap.free(value) + stream = null + } finally { + lock.unlock() + } + } +} + +private class IosDeflater : ProtocolDeflater { + private val state = ZlibState() + private val cleaner = createCleaner(state) { it.close() } + + override fun reset() = state.use { check(deflateReset(it) == Z_OK) } + + override fun syncFlush(message: ByteArray): ByteArray = state.use { stream -> + val chunks = mutableListOf() + var size = 0 + val buffer = ByteArray(4096) + message.usePinned { input -> + stream.pointed.next_in = + if (message.isEmpty()) null else input.addressOf(0).reinterpret() + stream.pointed.avail_in = message.size.toUInt() + try { + buffer.usePinned { output -> + do { + stream.pointed.next_out = output.addressOf(0).reinterpret() + stream.pointed.avail_out = buffer.size.toUInt() + val status = deflate(stream, Z_SYNC_FLUSH) + // A repeated empty SYNC_FLUSH is a valid no-op. + check(status == Z_OK || status == Z_BUF_ERROR) { + "zlib deflate failed: $status" + } + val count = buffer.size - stream.pointed.avail_out.toInt() + if (count > 0) { + chunks.add(buffer.copyOf(count)) + size += count + } + } while (stream.pointed.avail_out == 0u) + } + } finally { + // Do not leave pointers to unpinned Kotlin arrays in native memory. + stream.pointed.next_in = null + stream.pointed.next_out = null + stream.pointed.avail_in = 0u + stream.pointed.avail_out = 0u + } + } + ByteArray(size).also { result -> + var offset = 0 + for (chunk in chunks) { + chunk.copyInto(result, offset) + offset += chunk.size + } + } + } + + override fun close() = state.close() +} diff --git a/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/shared/PlatformInfo.ios.kt b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/shared/PlatformInfo.ios.kt new file mode 100644 index 00000000..890b5d90 --- /dev/null +++ b/native/kotlin/shared/src/iosMain/kotlin/com/faceclaw/shared/PlatformInfo.ios.kt @@ -0,0 +1,13 @@ +package com.faceclaw.shared + +import platform.Foundation.NSThread + +internal actual object PlatformInfo { + actual val name: String = "iOS" + + actual fun threadName(): String = + NSThread.currentThread.name?.takeIf { it.isNotEmpty() } + ?: if (NSThread.isMainThread) "main" else "background" + + actual fun isMainThread(): Boolean = NSThread.isMainThread +} diff --git a/package.json b/package.json index a9779a9a..d0d81931 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "native:android": "node scripts/kotlin-build.cjs android", "native:ios": "node scripts/kotlin-build.cjs ios", - "test:kotlin": "cd native/kotlin && ./gradlew :shared:testAndroidHostTest :shared:iosSimulatorArm64Test", + "test:kotlin": "bash scripts/test-kotlin.sh", "build:ios": "scripts/ios.sh build ios --for-device", "run:ios": "scripts/ios.sh run ios", "prepare:ios": "scripts/ios.sh prepare ios", @@ -15,7 +15,8 @@ "run": "./build_and_run.sh", "release": "scripts/release.sh", "clean": "rm -rf platforms", - "test": "tsc -p tests/tsconfig.json && node --test tests/*.test.cjs" + "test": "tsc -p tests/tsconfig.json && node --test tests/*.test.cjs", + "test:kotlin:ios": "bash scripts/test-kotlin.sh iosSimulatorArm64Test compileKotlinIosArm64" }, "dependencies": { "@evenrealities/pretext": "^0.1.4", diff --git a/scripts/kotlin-build.cjs b/scripts/kotlin-build.cjs new file mode 100644 index 00000000..1d4d07d3 --- /dev/null +++ b/scripts/kotlin-build.cjs @@ -0,0 +1,73 @@ +const { spawn } = require('node:child_process'); +const { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync } = require('node:fs'); +const path = require('node:path'); + +const root = path.resolve(__dirname, '..'); +const kotlin = path.join(root, 'native/kotlin'); + +function sameContents(source, destination) { + if (!existsSync(destination)) return false; + if (statSync(source).isDirectory()) { + if (!statSync(destination).isDirectory()) return false; + const entries = readdirSync(source).sort(); + const previous = readdirSync(destination).sort(); + return entries.length === previous.length && entries.every((entry, index) => + entry === previous[index] && sameContents(path.join(source, entry), path.join(destination, entry))); + } + return statSync(destination).isFile() && readFileSync(source).equals(readFileSync(destination)); +} + +function checkAndroidRuntime(projectDir = root) { + const generated = path.join(projectDir, 'platforms/android/build-tools/android-metadata-generator.jar'); + const installed = path.join(projectDir, 'node_modules/@nativescript/android/framework/build-tools/android-metadata-generator.jar'); + if (existsSync(generated) && !sameContents(installed, generated)) { + throw new Error('Generated Android runtime is stale. Run "ns platform clean android" once after npm ci, then rebuild.'); + } +} + +async function buildKotlin(platform, configuration = 'debug') { + if (!['android', 'ios'].includes(platform)) throw new Error(`Unsupported Kotlin platform: ${platform}`); + if (!['debug', 'release'].includes(configuration)) throw new Error(`Unsupported Kotlin configuration: ${configuration}`); + const variant = configuration === 'release' ? 'Release' : 'Debug'; + const task = platform === 'android' ? ':shared:assembleAndroidMain' : `:shared:assembleFaceclawKit${variant}XCFramework`; + const wrapper = path.join(root, 'wear', process.platform === 'win32' ? 'gradlew.bat' : 'gradlew'); + console.log(`[kotlin] Building ${platform} (${configuration})`); + await new Promise((resolve, reject) => { + const child = spawn(wrapper, [task, '--console=plain'], { cwd: kotlin, stdio: 'inherit' }); + child.on('error', reject); + child.on('exit', (code, signal) => { + if (code === 0) resolve(); + else reject(new Error(`Kotlin build failed (${signal || code})`)); + }); + }); + // Stage only after a successful build. npm links this directory into node_modules. + const destination = path.join(kotlin, 'plugin/platforms', platform); + mkdirSync(destination, { recursive: true }); + if (platform === 'android') { + // NativeScript's resource copy can retain deleted sources. Remove the + // generated copy from the pre-KMP bridge so it cannot duplicate the AAR. + rmSync(path.join(root, 'platforms/android/app/src/main/java/com/faceclaw/shared/KotlinBridge.kt'), { force: true }); + const output = path.join(kotlin, 'shared/build/outputs/aar'); + const files = readdirSync(output).filter(name => name.endsWith('.aar')); + if (files.length !== 1) throw new Error(`Expected one Kotlin AAR, found: ${files.join(', ')}`); + const source = path.join(output, files[0]); + const aar = path.join(destination, 'faceclaw-shared.aar'); + if (!sameContents(source, aar)) cpSync(source, aar); + } else { + const framework = path.join(destination, 'FaceclawKit.xcframework'); + const source = path.join(kotlin, `shared/build/XCFrameworks/${configuration}/FaceclawKit.xcframework`); + // Preserve mtimes on no-op builds: NativeScript uses them to detect changes. + if (!sameContents(source, framework)) { + rmSync(framework, { recursive: true, force: true }); + cpSync(source, framework, { recursive: true }); + } + } +} + +module.exports = { buildKotlin, checkAndroidRuntime }; +if (require.main === module) { + buildKotlin(process.argv[2], process.argv[3]).catch(error => { + console.error(error); + process.exitCode = 1; + }); +} diff --git a/scripts/test-kotlin.sh b/scripts/test-kotlin.sh new file mode 100644 index 00000000..ee4bcd4e --- /dev/null +++ b/scripts/test-kotlin.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/.." +source scripts/before_build.sh +if [ "$#" -eq 0 ]; then + set -- testAndroidHostTest +fi +exec ./wear/gradlew -p tests/kotlin "$@" diff --git a/tests/README.md b/tests/README.md index a9fe157f..98f99845 100644 --- a/tests/README.md +++ b/tests/README.md @@ -15,15 +15,13 @@ That compiles the pure modules with the project's TypeScript into dependencies are needed — the tests are plain `.test.cjs` files that use `node:test` and `node:assert`. -Compass diagnostics have focused UI and wire-format checks: +The shared Kotlin suite runs separately from the Node tests: - node --test tests/compass-debug.test.cjs tests/compass-protocol.test.cjs + npm run test:kotlin + npm run test:kotlin:ios -The protocol check also compiles the production Java parser, so it needs a -JDK (`JAVA_HOME`, or `javac` and `java` on `PATH`). Its golden packet is shared -with the firmware encoder test in `g2flash/tests/compass_diagnostics_test.c`. - -R1 battery field-106 decoding (valid zero, charging, unavailable/disconnected, -legacy firmware, malformed messages) uses the same C/Java golden packet: - - node --test tests/ring-battery-protocol.test.cjs +It includes the migrated compass, R1 battery, gesture and texture Java fixtures, +which now call the Kotlin implementation from Android host tests. Common tests +also run as native iOS simulator binaries. See [the Kotlin README](../native/kotlin/README.md) +for SDK requirements, source layout and coverage. The compass and battery +fixtures retain the golden packets shared with firmware tests. diff --git a/tests/cfw-transport.test.cjs b/tests/cfw-transport.test.cjs index 6e9e3ad4..8811c45d 100644 --- a/tests/cfw-transport.test.cjs +++ b/tests/cfw-transport.test.cjs @@ -9,28 +9,22 @@ const { crc16, concat, MessageReceiver } = require('../.test-build/app/g2/ble-pr const hex = value => Buffer.from(value).toString('hex'); test('iOS transport matches Android packets including persistent compression, reset, raw fallback and ATT limits', () => { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-cfw-java-')); - const java = name => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name; const random = new Uint8Array(65535); let seed = 42; for (let i = 0; i < random.length; i++) { seed ^= seed << 13; seed ^= seed >>> 17; seed ^= seed << 5; random[i] = seed & 255; } const cases = [[100, 3, 185, new Uint8Array([6, 0, 0, 255, 255])], [101, 3, 185, new Uint8Array([6, 0, 0, 255, 255])], [102, 1, 20, new Uint8Array(500).fill(42)], 'reset', [255, 3, 514, random], [103, 3, 240, new Uint8Array([11])]]; - try { - execFileSync(java('javac'), ['-d', dir, - path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java'), - path.join(__dirname, 'fixtures/CfwTransportVectors.java')]); - const expected = execFileSync(java('java'), ['-cp', dir, 'com.faceclaw.app.CfwTransportVectors', - ...cases.map(c => c === 'reset' ? c : `${c[0]}:${c[1]}:${c[2]}:${hex(c[3])}`)], - { maxBuffer: 1024 * 1024 }).toString().trim().split('\n'); + // Captured from the original Java encoder before migration; production now uses Kotlin. + const expected = require('./fixtures/cfw-java-golden.json'); + const hash = value => require('node:crypto').createHash('sha256').update(value).digest('hex'); const transport = new CfwTransport(); let index = 0; for (const c of cases) { if (c === 'reset') { transport.reset(); continue; } const [id, lenses, maxWrite, payload] = c, packets = transport.encode(payload, id, lenses, maxWrite); assert.ok(packets.every(p => p.length <= maxWrite)); - assert.equal(packets.map(hex).join(' '), expected[index++]); + assert.equal(hash(packets.map(hex).join(' ')), expected[index++]); } - } finally { fs.rmSync(dir, { recursive: true, force: true }); } + }); function ackPacket(records, lens = 1, nack = false) { diff --git a/tests/compass-protocol.test.cjs b/tests/compass-protocol.test.cjs deleted file mode 100644 index feee8260..00000000 --- a/tests/compass-protocol.test.cjs +++ /dev/null @@ -1,19 +0,0 @@ -const test = require('node:test'); -const fs = require('node:fs'); -const os = require('node:os'); -const path = require('node:path'); -const { execFileSync } = require('node:child_process'); - -test('compass diagnostics decode with legacy and malformed-extension fallback', () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-compass-java-')); - const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name; - try { - execFileSync(javaBin('javac'), ['-d', directory, - path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java'), - path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java'), - path.join(__dirname, 'fixtures/CompassProtocolTest.java')], { stdio: 'pipe' }); - execFileSync(javaBin('java'), ['-ea', '-cp', directory, 'com.faceclaw.app.CompassProtocolTest'], { stdio: 'pipe' }); - } finally { - fs.rmSync(directory, { recursive: true, force: true }); - } -}); diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs index 55edd8ff..2f947829 100644 --- a/tests/compass-texture-cache.test.cjs +++ b/tests/compass-texture-cache.test.cjs @@ -151,7 +151,7 @@ test('the background registers once and uses a nine-byte image reference on late const registered = []; const { prepareFrameDraws } = load('app/graphics/glyph-wire.ts', {}, '', { global: { isAndroid: true }, - com: { faceclaw: { app: { ImageAtlas: { ensure: (...args) => { + com: { faceclaw: { app: { AndroidByteReader: class { constructor(buffer) { this.buffer = buffer; } }, ImageAtlas: { ensure: (...args) => { registered.push(args); return 1; } } } } }, diff --git a/tests/faceclaw-gesture-event.test.cjs b/tests/faceclaw-gesture-event.test.cjs deleted file mode 100644 index 1071c1f7..00000000 --- a/tests/faceclaw-gesture-event.test.cjs +++ /dev/null @@ -1,19 +0,0 @@ -const test = require('node:test'); -const fs = require('node:fs'); -const os = require('node:os'); -const path = require('node:path'); -const { execFileSync } = require('node:child_process'); - -test('CFW idle-gesture events decode to sys-event types and sources; wake and junk do not', () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-gesture-java-')); - const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name; - try { - execFileSync(javaBin('javac'), ['-d', directory, - path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java'), - path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java'), - path.join(__dirname, 'fixtures/FaceclawGestureEventTest.java')], { stdio: 'pipe' }); - execFileSync(javaBin('java'), ['-ea', '-cp', directory, 'com.faceclaw.app.FaceclawGestureEventTest'], { stdio: 'pipe' }); - } finally { - fs.rmSync(directory, { recursive: true, force: true }); - } -}); diff --git a/tests/fixtures/CfwTransportVectors.java b/tests/fixtures/CfwTransportVectors.java deleted file mode 100644 index bbbf00f0..00000000 --- a/tests/fixtures/CfwTransportVectors.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.faceclaw.app; - -/** Production Android encoder oracle for the iOS transport parity test. */ -public final class CfwTransportVectors { - public static void main(String[] args) { - CfwTransport transport = new CfwTransport(); - for (String line : args) { - String[] fields = line.split(":", -1); - if (fields[0].equals("reset")) { transport.reset(); continue; } - int id = Integer.parseInt(fields[0]), lenses = Integer.parseInt(fields[1]); - int maxWrite = Integer.parseInt(fields[2]); - byte[] payload = java.util.HexFormat.of().parseHex(fields[3]); - StringBuilder result = new StringBuilder(); - for (byte[] packet : transport.encode(payload, id, lenses, maxWrite + 3)) - result.append(java.util.HexFormat.of().formatHex(packet)).append(" "); - System.out.println(result.toString().trim()); - } - } -} diff --git a/tests/fixtures/cfw-java-golden.json b/tests/fixtures/cfw-java-golden.json new file mode 100644 index 00000000..8eb619d3 --- /dev/null +++ b/tests/fixtures/cfw-java-golden.json @@ -0,0 +1,7 @@ +[ + "d73e76bae77dd09f6af2f9483b8d24b7ebb79dca266ace5f062dea936c181219", + "60afd64152c40d6a0b4a70437d3636a36b41f690fe7fa43a44c1fff1accb0845", + "8f4ce275c048001ef30da8b9d93e63c15d3fb49bc643d3f5fd6908b0adaf2292", + "50a45b6816d4f0669d09958ef2dd32ab3d162b6959b6252a940d3ec1180bd80e", + "5e53d5665b33cd46cb868f8063cda22ee6cce4584e5e28123803e41642f5563f" +] diff --git a/tests/kotlin-build.test.cjs b/tests/kotlin-build.test.cjs new file mode 100644 index 00000000..07d4f8cf --- /dev/null +++ b/tests/kotlin-build.test.cjs @@ -0,0 +1,43 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { checkAndroidRuntime } = require('../scripts/kotlin-build.cjs'); + +test('Android preparation rejects a stale metadata generator but allows fresh and matching platforms', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-kotlin-build-')); + try { + const installed = path.join(root, 'node_modules/@nativescript/android/framework/build-tools/android-metadata-generator.jar'); + const generated = path.join(root, 'platforms/android/build-tools/android-metadata-generator.jar'); + fs.mkdirSync(path.dirname(installed), { recursive: true }); + fs.writeFileSync(installed, 'current metadata generator'); + assert.doesNotThrow(() => checkAndroidRuntime(root)); + fs.mkdirSync(path.dirname(generated), { recursive: true }); + fs.writeFileSync(generated, 'old metadata generator'); + assert.throws(() => checkAndroidRuntime(root), /ns platform clean android/); + fs.copyFileSync(installed, generated); + assert.doesNotThrow(() => checkAndroidRuntime(root)); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); + +test('every remaining Java source has an explicit Android boundary and every migration has one replacement', () => { + const root = path.resolve(__dirname, '..'); + const java = path.join(root, 'App_Resources/Android/src/main/java'); + const list = directory => fs.readdirSync(directory, { withFileTypes: true }).flatMap(entry => { + const file = path.join(directory, entry.name); + return entry.isDirectory() ? list(file) : file.endsWith('.java') ? [path.relative(java, file)] : []; + }); + const boundaries = require('../native/kotlin/android-java-boundaries.json'); + const retained = boundaries.flatMap(group => { assert.ok(group.reason.length > 20); return group.files; }); + assert.deepEqual(list(java).sort(), retained.sort()); + assert.equal(new Set(retained).size, retained.length); + const migrated = require('../native/kotlin/migrated-java-sources.json'); + assert.equal(new Set(migrated.map(entry => entry.java)).size, migrated.length); + for (const entry of migrated) { + assert.equal(fs.existsSync(path.join(java, entry.java)), false, entry.java); + assert.ok(fs.existsSync(path.join(root, 'native/kotlin', entry.kotlin)), entry.kotlin); + } +}); diff --git a/tests/kotlin/build.gradle.kts b/tests/kotlin/build.gradle.kts new file mode 100644 index 00000000..0c97093c --- /dev/null +++ b/tests/kotlin/build.gradle.kts @@ -0,0 +1,40 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget + +plugins { + kotlin("multiplatform") version "2.4.20" + id("com.android.kotlin.multiplatform.library") version "8.13.2" +} + +// Compile the production sources without building NativeScript or touching BLE. +kotlin { + android { + namespace = "com.faceclaw.protocoltests" + compileSdk = 35 + minSdk = 24 + withJava() + compilerOptions { jvmTarget.set(JvmTarget.JVM_17) } + withHostTestBuilder {}.configure {} + } + iosArm64() + iosSimulatorArm64() + sourceSets { + commonMain { kotlin.srcDir("../../native/kotlin/shared/src/commonMain/kotlin") } + androidMain { kotlin.srcDir("../../native/kotlin/shared/src/androidMain/kotlin") } + iosMain { kotlin.srcDir("../../native/kotlin/shared/src/iosMain/kotlin") } + commonTest.dependencies { implementation(kotlin("test")) } + } +} + +// The font is already shipped in the app; avoid copying megabytes into test fixtures. +val generateFixturePaths = tasks.register("generateFixturePaths") { + val output = layout.buildDirectory.file("generated/fixtures/com/faceclaw/app/FixturePaths.kt") + outputs.dir(layout.buildDirectory.dir("generated/fixtures")) + doLast { + val font = file("../../app/fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin").canonicalPath + output.get().asFile.apply { + parentFile.mkdirs() + writeText("package com.faceclaw.app\ninternal const val TEST_FONT_PATH = \"${font.replace("\\", "\\\\").replace("\"", "\\\"")}\"\n") + } + } +} +kotlin.sourceSets.commonTest { kotlin.srcDir(generateFixturePaths) } diff --git a/tests/kotlin/settings.gradle.kts b/tests/kotlin/settings.gradle.kts new file mode 100644 index 00000000..6c535290 --- /dev/null +++ b/tests/kotlin/settings.gradle.kts @@ -0,0 +1,3 @@ +pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } } +dependencyResolutionManagement { repositories { google(); mavenCentral() } } +rootProject.name = "FaceclawProtocolTests" diff --git a/tests/fixtures/CompassProtocolTest.java b/tests/kotlin/src/androidHostTest/java/com/faceclaw/app/CompassProtocolTest.java similarity index 100% rename from tests/fixtures/CompassProtocolTest.java rename to tests/kotlin/src/androidHostTest/java/com/faceclaw/app/CompassProtocolTest.java diff --git a/tests/fixtures/FaceclawGestureEventTest.java b/tests/kotlin/src/androidHostTest/java/com/faceclaw/app/FaceclawGestureEventTest.java similarity index 100% rename from tests/fixtures/FaceclawGestureEventTest.java rename to tests/kotlin/src/androidHostTest/java/com/faceclaw/app/FaceclawGestureEventTest.java diff --git a/tests/fixtures/RingBatteryProtocolTest.java b/tests/kotlin/src/androidHostTest/java/com/faceclaw/app/RingBatteryProtocolTest.java similarity index 100% rename from tests/fixtures/RingBatteryProtocolTest.java rename to tests/kotlin/src/androidHostTest/java/com/faceclaw/app/RingBatteryProtocolTest.java diff --git a/tests/fixtures/TextureCacheProtocolTest.java b/tests/kotlin/src/androidHostTest/java/com/faceclaw/app/TextureCacheProtocolTest.java similarity index 100% rename from tests/fixtures/TextureCacheProtocolTest.java rename to tests/kotlin/src/androidHostTest/java/com/faceclaw/app/TextureCacheProtocolTest.java diff --git a/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/AndroidProtocolTest.kt b/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/AndroidProtocolTest.kt new file mode 100644 index 00000000..2a48d43b --- /dev/null +++ b/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/AndroidProtocolTest.kt @@ -0,0 +1,117 @@ +package com.faceclaw.app + +import java.io.ByteArrayOutputStream +import java.util.Collections +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit +import java.util.zip.Inflater +import kotlin.test.* + +// Host tests have no Android clock; all other services use the production adapter. +internal actual fun testPlatform(): ProtocolPlatform = + object : ProtocolPlatform by AndroidProtocolPlatform { + override fun elapsedRealtimeMs() = System.nanoTime() / 1_000_000 + } + +internal actual fun inflateRecords(records: List): List { + val inflater = Inflater() + try { + return records.map { record -> + if (record[0].toInt() and CfwTransport.RESET_CONTEXT != 0) inflater.reset() + val body = record.copyOfRange(5, record.size) + if (record[0].toInt() and CfwTransport.COMPRESSED == 0) body + else { + inflater.setInput(body) + val out = ByteArrayOutputStream() + val buffer = ByteArray(4096) + do { + val count = inflater.inflate(buffer) + out.write(buffer, 0, count) + } while (!inflater.needsInput()) + out.toByteArray() + } + } + } finally { + inflater.end() + } +} + +class AndroidProtocolTest { + @Test + fun javaGoldenCorpus() { + // SHA-256 of packets emitted by the pre-migration Java implementation. + val raw = java.security.MessageDigest.getInstance("SHA-256") + val compressed = java.security.MessageDigest.getInstance("SHA-256") + val transport = CfwTransport(testPlatform()) + var state = 123456789 + try { + repeat(120) { i -> + val size = intArrayOf(0, 1, 4, 247, 248, 252, 4095, 4096, 8192, 65535)[i % 10] + val data = + ByteArray(size) { + state = state xor (state shl 13) + state = state xor (state ushr 17) + state = state xor (state shl 5) + state.toByte() + } + if (i % 3 == 0) data.fill((i % 7).toByte()) + val id = (i * 37) and 255 + val lenses = 1 + (i / 7) % 3 + val mtu = intArrayOf(23, 247, 512, 517)[i % 4] + if (i % 13 == 0) transport.reset() + CfwTransport.frame(data, id, lenses, mtu).forEach { raw.update(it) } + transport.encode(data, id, lenses, mtu).forEach { compressed.update(it) } + } + assertEquals( + "d0efdb99ac71763dd29b70fbd0021b80e1cfe514dfc8a60e2a8424892b16c9f5", + raw.digest().toHexString(), + ) + assertEquals( + "eb5ce0cc6e41b8e84f0d9455f98f3f666b20be0643c3756c5f7aa9d9a66085e2", + compressed.digest().toHexString(), + ) + } finally { + transport.close() + } + } + + @Test + fun writeModeMapping() { + assertEquals( + android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT, + AndroidProtocolPlatform.writeType(GattWriteMode.WITH_RESPONSE), + ) + assertEquals( + android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE, + AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), + ) + } + + @Test + fun concurrentAllocationNeverDuplicatesAnInUseId() { + val pool = BleMagicPool(testPlatform()) + val inUse = Collections.synchronizedSet(mutableSetOf()) + val executor = Executors.newFixedThreadPool(8) + try { + val jobs = + List(8) { + executor.submit { + repeat(2000) { + val id = pool.allocate() + assertTrue(inUse.add(id)) + Thread.yield() + assertTrue(inUse.remove(id)) + pool.release(1, id, null, null) + } + } + } + jobs.forEach { it.get(30, TimeUnit.SECONDS) } + assertEquals(156, List(156) { pool.allocate() }.toSet().size) + } finally { + executor.shutdownNow() + } + } +} + +internal actual fun digest(bytes: ByteArray): String = + BinaryEncoding.bytesToHex(java.security.MessageDigest.getInstance("SHA-256").digest(bytes)) diff --git a/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/JavaCompatibilityTest.kt b/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/JavaCompatibilityTest.kt new file mode 100644 index 00000000..1edb969a --- /dev/null +++ b/tests/kotlin/src/androidHostTest/kotlin/com/faceclaw/app/JavaCompatibilityTest.kt @@ -0,0 +1,27 @@ +package com.faceclaw.app + +import kotlin.test.Test + +/** Existing wire fixtures also verify the Java API retained by the Kotlin port. */ +class JavaCompatibilityTest { + @Test fun compass() = CompassProtocolTest.main(emptyArray()) + + @Test fun ringBattery() = RingBatteryProtocolTest.main(emptyArray()) + + @Test fun gestures() = FaceclawGestureEventTest.main(emptyArray()) + + @Test fun textureCache() = TextureCacheProtocolTest.main(emptyArray()) +} + +class AndroidByteReaderTest { + @Test fun repeatedBridgeCrossingsHaveIndependentCursors() { + val buffer = java.nio.ByteBuffer.wrap(byteArrayOf(10, 20, 30)) + val first = AndroidByteReader(buffer) + kotlin.test.assertEquals(10, first.get().toInt()) + kotlin.test.assertEquals(0, buffer.position()) + val second = AndroidByteReader(buffer) + kotlin.test.assertEquals(3, second.remaining()) + kotlin.test.assertEquals(10, second.get().toInt()) + kotlin.test.assertEquals(20, first.get().toInt()) + } +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/AudioRecordingTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/AudioRecordingTest.kt new file mode 100644 index 00000000..b3316cd9 --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/AudioRecordingTest.kt @@ -0,0 +1,44 @@ +package com.faceclaw.app + +import kotlin.test.* + +internal expect fun digest(bytes: ByteArray): String + +class AudioRecordingTest { + @Test + fun javaNoiseSuppressionGoldenAcrossChunksAndReset() { + val suppressor = FaceclawNoiseSuppressor(16000) + val output = ByteSink() + for (round in 0 until 80) { + val input = ByteArray(if (round % 7 == 0) 514 else 640) + for (i in 0 until input.size / 2) { + val value = ((round * 317 + i * 773) % 48000) - 24000 + input[i * 2] = value.toByte() + input[i * 2 + 1] = (value shr 8).toByte() + } + if (round == 41) suppressor.reset() + output.write(assertNotNull(suppressor.process(input))) + } + // SHA-256 from the original Java DSP, including warmup and irregular chunks. + assertEquals( + "4a7e86b70d61d683b17c7051dbd6666f5ea62427fdd7fdb1d4c9a1772a1eb8ef", + digest(output.toByteArray()), + ) + } + + @Test + fun javaGifGoldenIncludingDedupAndLzwCodeGrowth() { + val recorder = SharedGifScreenRecorder() + assertTrue(recorder.encode().isEmpty()) + for (round in 0 until 7) { + val pixels = ByteArray(33 * 17) { ((it * 73 + round * 117) % 256).toByte() } + recorder.addFrame(pixels, 33, 17, round * 133L) + recorder.addFrame(pixels, 33, 17, round * 133L + 10) + } + assertFalse(recorder.isOverflowed()) + assertEquals( + "8c5dcb98fa03653972f7f7d5ddaaacff323dd633578905bc252ebe21f9d82780", + digest(recorder.encode()), + ) + } +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FontTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FontTest.kt new file mode 100644 index 00000000..922261b2 --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FontTest.kt @@ -0,0 +1,22 @@ +package com.faceclaw.app + +import kotlin.test.* + +class FontTest { + @Test + fun bundledFontMatchesJavaMetricsAndGlyphs() { + assertContentEquals(byteArrayOf(25, 19), LvglFontFile.getMetrics(TEST_FONT_PATH)) + val vectors = + mapOf( + 0x25a6 to "b08ecb603fd059b0a00ba3088f18b79185c421e73049797cd3cb0828e1e5cd04", + 0x25c6 to "c176d5dcdb5f7e3a7220455528efde3760672c40cee39b422fc3f5ad20df66dd", + 0x4e2d to "9bde2c8f1ffcf7afc598f4d0350c9ff262484816aa0c9b46d31f0c3c09613003", + 0xff21 to "0074f96299a75b86912282416e47e0823790e091352aec852eff49b89b2e719d", + ) + vectors.forEach { (codePoint, expected) -> + assertEquals(expected, digest(LvglFontFile.getGlyph(TEST_FONT_PATH, codePoint))) + } + assertTrue(LvglFontFile.getGlyph(TEST_FONT_PATH, 0x10ffff).isEmpty()) + assertTrue(LvglFontFile.getMetrics("/nonexistent/faceclaw-font").isEmpty()) + } +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ProtocolTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ProtocolTest.kt new file mode 100644 index 00000000..a2c006e6 --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/ProtocolTest.kt @@ -0,0 +1,283 @@ +package com.faceclaw.app + +import kotlin.random.Random +import kotlin.test.* + +internal expect fun testPlatform(): ProtocolPlatform + +internal expect fun inflateRecords(records: List): List + +internal fun record(packets: List): ByteArray = + packets.flatMap { it.slice(9 until it.size - 2) }.toByteArray() + +private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray() + +private fun u16(bytes: ByteArray, offset: Int) = + (bytes[offset].toInt() and 255) or ((bytes[offset + 1].toInt() and 255) shl 8) + +class ProtocolTest { + @Test + fun javaGoldenPacketsAndCrc() { + assertEquals(0x29b1, CfwTransport.crc("123456789".encodeToByteArray())) + assertEquals(0xffff, CfwTransport.crc(byteArrayOf())) + // Captured from the Java implementation before migration, including sequence wrap. + val expected = + listOf( + "aa21ff0c0101f000830b0f0078c331323334862b", + "aa21000c0101f000033536373839616263648c7b", + "aa2101050101f000436566de70", + ) + val actual = CfwTransport.frame("123456789abcdef".encodeToByteArray(), 255, 3, 23) + assertEquals(expected.size, actual.size) + expected.zip(actual).forEach { (a, b) -> assertContentEquals(hex(a), b) } + } + + @Test + fun framingBoundariesAndValidation() { + for (size in listOf(0, 1, 247, 252, 253, 65535)) { + val data = ByteArray(size) { it.toByte() } + for (mtu in listOf(23, 247, 512, 517)) { + val packets = CfwTransport.frame(data, 255, 2, mtu) + packets.forEachIndexed { i, packet -> + assertTrue(packet.size <= mtu - 3) + assertEquals((255 + i) and 255, packet[2].toInt() and 255) + assertEquals(packet.size - 8, packet[3].toInt() and 255) + assertEquals( + 2 or (if (i == 0) 128 else 0) or (if (i == packets.lastIndex) 64 else 0), + packet[8].toInt() and 255, + ) + assertEquals( + CfwTransport.crc(packet.copyOfRange(8, packet.size - 2)), + u16(packet, packet.size - 2), + ) + } + val stream = record(packets) + assertEquals(size, u16(stream, 1)) + assertEquals(CfwTransport.crc(data), u16(stream, 3)) + assertContentEquals(data, stream.copyOfRange(5, stream.size)) + } + } + for (invalid in listOf(null, ByteArray(65536))) assertFailsWith< + IllegalArgumentException + > { + CfwTransport.frame(invalid, 0, 3, 23) + } + for (stream in listOf(-1, 256)) assertFailsWith { + CfwTransport.frame(byteArrayOf(), stream, 3, 23) + } + for (lens in listOf(0, 4)) assertFailsWith { + CfwTransport.frame(byteArrayOf(), 0, lens, 23) + } + for (mtu in listOf(22, 518)) assertFailsWith { + CfwTransport.frame(byteArrayOf(), 0, 3, mtu) + } + } + + @Test + fun streamingCompressionRoundTripsAndResets() { + val transport = CfwTransport(testPlatform()) + try { + val text = "shared history 👓 ".repeat(300).encodeToByteArray() + val inputs = listOf(text, text, byteArrayOf(), byteArrayOf(), text, text) + val records = inputs.mapIndexed { i, data -> + if (i == 5) transport.reset() + record(transport.encode(data, i, if (i < 4) 3 else 1, 247)) + } + assertTrue(records[1].size < records[0].size) + assertTrue(records[0][0].toInt() and CfwTransport.RESET_CONTEXT != 0) + assertEquals(0, records[1][0].toInt() and CfwTransport.RESET_CONTEXT) + assertTrue(records[4][0].toInt() and CfwTransport.RESET_CONTEXT != 0) + assertTrue(records[5][0].toInt() and CfwTransport.RESET_CONTEXT != 0) + inputs.zip(inflateRecords(records)).forEach { (a, b) -> assertContentEquals(a, b) } + transport.close() + transport.close() + val reopened = record(transport.encode(text, 10, 1, 512)) + assertTrue(reopened[0].toInt() and CfwTransport.RESET_CONTEXT != 0) + assertContentEquals(text, inflateRecords(listOf(reopened)).single()) + } finally { + transport.close() + } + } + + @Test + fun incompressibleMaximumFallsBackWithoutPoisoningHistory() { + val transport = CfwTransport(testPlatform()) + try { + val data = Random(42).nextBytes(65535) + val raw = record(transport.encode(data, 255, 3, 517)) + assertEquals(3 or CfwTransport.RESET_CONTEXT, raw[0].toInt()) + val next = record(transport.encode(data.copyOf(1000), 0, 3, 517)) + assertTrue(next[0].toInt() and CfwTransport.RESET_CONTEXT != 0) + val decoded = inflateRecords(listOf(raw, next)) + assertContentEquals(data, decoded[0]) + assertContentEquals(data.copyOf(1000), decoded[1]) + } finally { + transport.close() + } + } + + @Test + fun ackBatchesAndMalformedPackets() { + for (count in 1..4) { + val packet = ackPacket(count) + val acks = assertNotNull(CfwTransport.parseAcks(packet)) + assertEquals(count, acks.size) + acks.forEachIndexed { i, ack -> + assertFalse(ack.nack) + assertEquals(255 - i, ack.streamId) + assertEquals(0x1234 + i, ack.messageId) + assertEquals(2, ack.lens) + assertEquals(0x4567 + i, ack.size) + assertEquals(0xabcd + i, ack.checksum) + } + assertEquals(255, assertNotNull(CfwTransport.parseAck(packet)).streamId) + // Every bit of the payload (including ACK trailers and CRC) is protected. + for (i in 8 until packet.size) { + val corrupt = packet.copyOf() + corrupt[i] = (corrupt[i].toInt() xor 1).toByte() + assertNull(CfwTransport.parseAcks(corrupt)) + } + } + assertTrue(assertNotNull(CfwTransport.parseAck(ackPacket(1, 3))).nack) + assertNull(CfwTransport.parseAcks(ackPacket(2, 3))) + assertNull(CfwTransport.parseAcks(ackPacket(5))) + assertNull(CfwTransport.parseAcks(ackPacket(1, 2))) + for (index in listOf(0, 1, 3, 4, 5, 6, 7)) { + val corrupt = ackPacket(1) + corrupt[index] = (corrupt[index].toInt() xor 1).toByte() + assertNull(CfwTransport.parseAcks(corrupt)) + } + val wrongLens = ackPacket(1) + wrongLens[12] = 3 + sealAck(wrongLens) + assertNull(CfwTransport.parseAcks(wrongLens)) + for (size in 0..18) assertNull(CfwTransport.parseAcks(ByteArray(size))) + assertNull(CfwTransport.parseAcks(null)) + } + + @Test + fun ackMatchingAndReplay() { + val input = byteArrayOf(1, 2, 3) + val message = message(input = input) + input[0] = 99 + assertContentEquals(byteArrayOf(1, 2, 3), message.message) + fun ack( + lens: Int, + stream: Int = 100, + nack: Boolean = false, + size: Int = 3, + checksum: Int = message.cfwChecksum, + id: Int = 0, + ) = CfwTransport.Ack(nack, stream, id, lens, size, checksum) + assertFalse(message.acceptCfwAck(null)) + assertFalse(message.acceptCfwAck(ack(1, stream = 99))) + assertFalse(message.acceptCfwAck(ack(1, size = 4))) + assertFalse(message.acceptCfwAck(ack(1, checksum = 0))) + assertFalse(message.acceptCfwAck(ack(3))) + assertFalse(message.acceptCfwAck(ack(1, id = 1))) + assertEquals(0, message.cfwAckLenses) + assertFalse(message.acceptCfwAck(ack(1))) + assertFalse(message.acceptCfwAck(ack(1))) + assertTrue(message.acceptCfwAck(ack(2))) + assertFalse(message.acceptCfwAck(ack(1, nack = true, size = 0, checksum = 0))) + assertFalse(message.acceptCfwAck(ack(2))) + message.ackDeadlineAtMs = 500 + message.ackPayload = byteArrayOf(1) + message.prepareCfwReplay(101) + assertEquals(1, message.cfwRetries) + assertFalse(message.cfwRetryPending) + assertEquals(0, message.cfwAckLenses) + assertEquals(0L, message.ackDeadlineAtMs) + assertTrue(message.ackPayload.isEmpty()) + assertFalse(message.acceptCfwAck(ack(1))) + assertFalse(message.acceptCfwAck(ack(1, stream = 101))) + assertTrue(message.acceptCfwAck(ack(2, stream = 101))) + var callbacks = 0 + message.onAck = MessageCallback { callbacks++ } + message.onAck!!.run() + assertEquals(1, callbacks) + } + + @Test + fun orderedCompletionAndWholeWindowRecovery() { + val head = message() + val tail = message() + val stock = message(sid = 1) + val window = listOf(stock, head, tail) + tail.cfwAckLenses = 3 + assertNull(CfwMessageWindow.acknowledgedHead(window)) + assertTrue(CfwMessageWindow.replayWindow(window, 100).isEmpty()) + head.ackDeadlineAtMs = 100 + assertTrue(CfwMessageWindow.replayWindow(window, 99).isEmpty()) + assertEquals(listOf(head, tail), CfwMessageWindow.replayWindow(window, 100)) + head.cfwAckLenses = 3 + assertSame(head, CfwMessageWindow.acknowledgedHead(window)) + assertTrue(CfwMessageWindow.replayWindow(window, 100).isEmpty()) + tail.cfwRetryPending = true + assertEquals(listOf(head, tail), CfwMessageWindow.replayWindow(window, 100)) + head.cfwRetryPending = true + assertNull(CfwMessageWindow.acknowledgedHead(window)) + } + + @Test + fun magicPoolExhaustionLruAndReleaseRecords() { + var now = 123L + val platform = + object : ProtocolPlatform by testPlatform() { + override fun elapsedRealtimeMs() = now + } + val pool = BleMagicPool(platform) + assertEquals((100..255).toList(), List(156) { pool.allocate() }) + assertFailsWith { pool.allocate() } + pool.release(1, 101, "first", "ack") + pool.release(1, 100, null, null) + now = 456 + pool.release(2, 101, "other", "timeout") // duplicate release must not allocate twice + assertEquals(101, pool.allocate()) + assertEquals(100, pool.allocate()) + assertFailsWith { pool.allocate() } + val first = assertNotNull(pool.getReleaseRecord(1, 101)) + assertEquals(123L, first.releasedAtMs) + assertEquals("first", first.label) + assertEquals(456L, assertNotNull(pool.getReleaseRecord(2, 101)).releasedAtMs) + assertEquals("", assertNotNull(pool.getReleaseRecord(1, 100)).reason) + for (invalid in listOf(-1, 99, 256)) { + pool.release(1, invalid, "bad", "bad") + assertNull(pool.getReleaseRecord(1, invalid)) + } + } +} + +private fun message(sid: Int = CfwTransport.SID, input: ByteArray = byteArrayOf(1, 2, 3)) = + OutboundMessage("test", "test", sid, 0, 100, input, 500, 0, false) + +private fun ackPacket(count: Int, kind: Int = 1): ByteArray { + val packet = ByteArray(19 + (count - 1) * 7) + packet[0] = 0xaa.toByte() + packet[1] = 0x12 + packet[3] = (packet.size - 8).toByte() + packet[4] = 1 + packet[5] = 1 + packet[6] = 0xf0.toByte() + packet[8] = kind.toByte() + packet[12] = 2 + fun put16(offset: Int, value: Int) { + packet[offset] = value.toByte() + packet[offset + 1] = (value ushr 8).toByte() + } + for (i in 0 until count) { + val offset = if (i == 0) 9 else 17 + (i - 1) * 7 + packet[offset] = (255 - i).toByte() + put16(offset + 1, 0x1234 + i) + put16(offset + (if (i == 0) 4 else 3), 0x4567 + i) + put16(offset + (if (i == 0) 6 else 5), 0xabcd + i) + } + sealAck(packet) + return packet +} + +private fun sealAck(packet: ByteArray) { + val crc = CfwTransport.crc(packet.copyOfRange(8, packet.size - 2)) + packet[packet.size - 2] = crc.toByte() + packet[packet.size - 1] = (crc ushr 8).toByte() +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/SharedGraphicsTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/SharedGraphicsTest.kt new file mode 100644 index 00000000..d2ef7b4a --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/SharedGraphicsTest.kt @@ -0,0 +1,136 @@ +package com.faceclaw.app + +import kotlin.test.* + +class SharedGraphicsTest { + private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray() + + @Test + fun androidWireVectorsAndGrayQuantization() { + val vectors = + listOf( + BleProtocol.buildAuthenticationRequest(101) to "080410651a0408011004", + BleProtocol.PRELUDE_F5872_PAYLOAD to "0802109c01220a1a081206120408001000", + BleProtocol.buildCreateInputPage(102) to + "080010661a2308011a1c0800100018c00420a0024801520964617368626f617264580162012028904e", + BleProtocol.buildHeartbeat(103) to "080c106772020800", + BleProtocol.buildSettingsQuery(104) to "0802106822020801", + BleProtocol.buildShutdown(105, 1) to "080910695a020801", + BleProtocol.buildFaceclawWakeControl(5, 0) to "08011000aa0606464301050000", + ) + vectors.forEach { (actual, expected) -> assertContentEquals(hex(expected), actual) } + val packed = + BmpUtil.pack4bppFromGray8( + byteArrayOf(0, 1, 7, 8, 15, 16, 23, 24, 127, -128, -9, -1), + 3, + 4, + ) + assertContentEquals(hex("0000111012808ff0"), packed) + assertContentEquals(hex("40311011121810182f10"), BleImageOptimizer.rleEncode(packed)) + assertContentEquals( + hex("aa21400c01018000080410651a04080110041024"), + BleProtocol.framePb(vectors.first().first, 128, 0, 64).single(), + ) + } + + @Test + fun splitStreamsExpiryAndCorruption() { + val payload = + BleProtocol.encodeVarintField(1, 3) + + BleProtocol.encodeVarintField(2, 110) + + BleProtocol.encodeBytesField(8, ByteArray(3900) { 55 }) + val frames = BleProtocol.framePb(payload, 224, 32, 17, 185) + assertTrue(frames.all { it.size <= 185 }) + val stream = frames.reduce { a, b -> a + b } + val receiver = MessageReceiver() + assertTrue(receiver.receive("L", stream.copyOf(7), 0).isEmpty()) + val message = receiver.receive("L", stream.copyOfRange(7, stream.size), 1).single() + assertContentEquals(payload, message.payload) + assertEquals(3, message.command) + assertEquals(110, message.magic) + stream[stream.lastIndex] = (stream.last().toInt() xor 1).toByte() + assertTrue(receiver.receive("L", stream, 2).isEmpty()) + receiver.receive("L", frames.first(), 10) + assertTrue(receiver.receive("R", frames.drop(1).reduce { a, b -> a + b }, 11).isEmpty()) + assertTrue(receiver.receive("L", frames.drop(1).reduce { a, b -> a + b }, 5011).isEmpty()) + assertFailsWith { BleProtocol.framePb(payload, 1, 0, 1, 10) } + assertFailsWith { BleProtocol.framePb(ByteArray(60000), 1, 0, 1) } + val invalid = BleProtocol.framePb(byteArrayOf(0x1a, 0x7f), 224, 0, 2).single() + assertTrue(receiver.receive("L", invalid, 6000).isEmpty()) + } + + @Test + fun eventDecodeAcceptsValidatedPayloadWithoutStrippingAgain() { + val event = BleProtocol.encodeVarintField(1, 12) + BleProtocol.encodeVarintField(2, 2) + val payload = BleProtocol.encodeBytesField(13, BleProtocol.encodeBytesField(3, event)) + val received = + MessageReceiver() + .receive("L", BleProtocol.framePb(payload, 224, 1, 1).single(), 0) + .single() + val decoded = assertNotNull(G2Event.decodePayload(received.sid, received.payload)) + assertEquals("sys-event", decoded.kind) + assertEquals(12, decoded.eventType) + assertEquals(2, decoded.eventSource) + } + + @Test + fun retainedCompositionClippingDimmingAndBlanking() { + val c = SurfaceCompositor() + c.configureScreen(3, 2) + c.configureSurface("app", 0, 0, 3, 2, 0, 0) + c.submitSurface("app", ArrayByteReader(ByteArray(6) { 200.toByte() }), 0, 0, 3, 2, "1") + c.configureSurface("shell", 0, 0, 3, 2, 1, 1) + c.submitSurface("shell", ArrayByteReader(byteArrayOf(0, 1, -1, 0, 0, 0)), 0, 0, 3, 2, "2") + c.setUnderlayDim(1, 128) + assertContentEquals(byteArrayOf(100, 1, -1, 100, 100, 100), c.composite().gray) + c.setSurfaceVisible("shell", false) + assertContentEquals(ByteArray(6) { 100 }, c.composite().gray) + c.configureSurface("opaque", 1, 0, 1, 2, 2, 0) + c.submitSurface("opaque", ArrayByteReader(byteArrayOf(0, 0)), 0, 0, 1, 2, "3") + assertContentEquals(byteArrayOf(100, 0, 100, 100, 0, 100), c.composite().gray) + c.setBlanked(true) + assertContentEquals(ByteArray(6), c.composite().gray) + c.setBlanked(false) + c.removeSurface("opaque") + c.configureSurface("app", -1, 0, 3, 2, 0, 0) + c.setUnderlayDim(0, 256) + assertContentEquals(byteArrayOf(-56, -56, 0, -56, -56, 0), c.composite().gray) + } + + @Test + fun pngChunksAndPixels() { + val png = SharedScreenshots.encode4BitGrayPng(byteArrayOf(0, 16, -1, 32, 48, 64), 3, 2) + assertContentEquals(hex("89504e470d0a1a0a"), png.copyOf(8)) + fun intAt(offset: Int): Int = + (0..3).fold(0) { n, i -> (n shl 8) or (png[offset + i].toInt() and 255) } + var offset = 8 + val types = mutableListOf() + var compressed = ByteArray(0) + while (offset < png.size) { + val size = intAt(offset) + val type = png.copyOfRange(offset + 4, offset + 8).decodeToString() + types.add(type) + if (type == "IHDR") { + assertEquals(3, intAt(offset + 8)) + assertEquals(2, intAt(offset + 12)) + assertEquals(4, png[offset + 16].toInt()) + assertEquals(0, png[offset + 17].toInt()) + } + if (type == "IDAT") compressed += png.copyOfRange(offset + 8, offset + 8 + size) + offset += size + 12 + } + assertEquals(listOf("IHDR", "IDAT", "IEND"), types) + assertContentEquals(hex("0001f0002340"), inflateData(compressed, 6)) + } + + @Test + fun wavHeaderAndHex() { + assertEquals("007f80ff", BinaryEncoding.bytesToHex(byteArrayOf(0, 127, -128, -1))) + assertContentEquals( + hex( + "524946462804000057415645666d74201000000001000100803e0000007d0000020010006461746104040000" + ), + BinaryEncoding.wavHeader(1028, 16000, 1), + ) + } +} diff --git a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt new file mode 100644 index 00000000..2d0f0191 --- /dev/null +++ b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosNativeApiTest.kt @@ -0,0 +1,38 @@ +package com.faceclaw.app + +import kotlin.test.* + +class IosNativeApiTest { + @Test + fun binaryFacadeRoundTripsAndSlices() { + val bytes = byteArrayOf(0, -1, 2, -128) + assertContentEquals(bytes, bytes.data().byteArray()) + assertTrue(byteArrayOf().data().byteArray().isEmpty()) + val reader = IosByteReader(bytes.data()) + assertEquals(0, reader.get().toInt()) + val target = ByteArray(5) + reader.get(target, 1, 3) + assertContentEquals(byteArrayOf(0, -1, 2, -128, 0), target) + assertEquals(0, reader.remaining()) + val protocol = IosProtocol() + val auth = protocol.authentication(101) + val frames = protocol.frame(auth, 128, 0, 64, 185) + val result = IosMessageReceiver().receive("L", frames.single(), 0).single() + assertEquals(101, result.magic) + assertContentEquals(auth.byteArray(), result.payload.byteArray()) + } + + @Test + fun nativeCompositorClipsPartialUpdatesBeforeCoalescing() { + val compositor = IosSurfaceCompositor(3, 2) + compositor.configure("app", 0, 0, 3, 2, 0, false) + compositor.submit("app", byteArrayOf(10, 20, 30, 40, 50, 60).data(), -1, 0, 3, 2) + assertContentEquals(byteArrayOf(20, 30, 0, 50, 60, 0), compositor.composite().byteArray()) + compositor.submit("app", byteArrayOf(99).data(), 2, 1, 1, 1) + assertContentEquals(byteArrayOf(20, 30, 0, 50, 60, 99), compositor.composite().byteArray()) + compositor.blank(true) + assertContentEquals(ByteArray(6), compositor.composite().byteArray()) + compositor.blank(false) + assertEquals(99, compositor.composite().byteArray().last().toInt()) + } +} diff --git a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosProtocolTest.kt b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosProtocolTest.kt new file mode 100644 index 00000000..0afc74d4 --- /dev/null +++ b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosProtocolTest.kt @@ -0,0 +1,81 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +package com.faceclaw.app + +import kotlin.test.* +import kotlinx.cinterop.* +import platform.CoreBluetooth.* +import platform.posix.memset +import platform.zlib.* + +internal actual fun testPlatform(): ProtocolPlatform = IosProtocolPlatform + +internal actual fun inflateRecords(records: List): List = memScoped { + val stream = alloc() + memset(stream.ptr, 0, sizeOf().convert()) + check(inflateInit(stream.ptr) == Z_OK) + try { + records.map { record -> + if (record[0].toInt() and CfwTransport.RESET_CONTEXT != 0) + check(inflateReset(stream.ptr) == Z_OK) + val body = record.copyOfRange(5, record.size) + if (record[0].toInt() and CfwTransport.COMPRESSED == 0) body + else { + val output = mutableListOf() + val buffer = ByteArray(4096) + body.usePinned { input -> + stream.next_in = input.addressOf(0).reinterpret() + stream.avail_in = body.size.toUInt() + buffer.usePinned { destination -> + do { + stream.next_out = destination.addressOf(0).reinterpret() + stream.avail_out = buffer.size.toUInt() + val status = inflate(stream.ptr, Z_SYNC_FLUSH) + check(status == Z_OK || status == Z_BUF_ERROR) + output.addAll(buffer.take(buffer.size - stream.avail_out.toInt())) + } while (stream.avail_in > 0u || stream.avail_out == 0u) + } + } + stream.next_in = null + stream.next_out = null + output.toByteArray() + } + } + } finally { + inflateEnd(stream.ptr) + } +} + +class IosProtocolTest { + @Test + fun platformClockAndWriteModes() { + val before = IosProtocolPlatform.elapsedRealtimeMs() + assertTrue(before > 0) + assertTrue(IosProtocolPlatform.elapsedRealtimeMs() >= before) + assertEquals( + CBCharacteristicWriteWithResponse, + IosProtocolPlatform.writeType(GattWriteMode.WITH_RESPONSE), + ) + assertEquals( + CBCharacteristicWriteWithoutResponse, + IosProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), + ) + } +} + +internal actual fun digest(bytes: ByteArray): String { + val result = ByteArray(32) + result.usePinned { output -> + if (bytes.isEmpty()) + platform.CoreCrypto.CC_SHA256(null, 0u, output.addressOf(0).reinterpret()) + else + bytes.usePinned { input -> + platform.CoreCrypto.CC_SHA256( + input.addressOf(0), + bytes.size.toUInt(), + output.addressOf(0).reinterpret(), + ) + } + } + return BinaryEncoding.bytesToHex(result) +} diff --git a/tests/ring-battery-protocol.test.cjs b/tests/ring-battery-protocol.test.cjs deleted file mode 100644 index eebaea03..00000000 --- a/tests/ring-battery-protocol.test.cjs +++ /dev/null @@ -1,19 +0,0 @@ -const test = require('node:test'); -const fs = require('node:fs'); -const os = require('node:os'); -const path = require('node:path'); -const { execFileSync } = require('node:child_process'); - -test('ring battery decodes valid, unavailable, legacy, and malformed reports', () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-ring-java-')); - const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name; - try { - execFileSync(javaBin('javac'), ['-d', directory, - path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java'), - path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java'), - path.join(__dirname, 'fixtures/RingBatteryProtocolTest.java')], { stdio: 'pipe' }); - execFileSync(javaBin('java'), ['-ea', '-cp', directory, 'com.faceclaw.app.RingBatteryProtocolTest'], { stdio: 'pipe' }); - } finally { - fs.rmSync(directory, { recursive: true, force: true }); - } -}); diff --git a/tests/texture-cache-protocol.test.cjs b/tests/texture-cache-protocol.test.cjs deleted file mode 100644 index dc397251..00000000 --- a/tests/texture-cache-protocol.test.cjs +++ /dev/null @@ -1,23 +0,0 @@ -const test = require('node:test'); -const fs = require('node:fs'); -const os = require('node:os'); -const path = require('node:path'); -const { execFileSync } = require('node:child_process'); - -test('256 KiB cache uploads and planner draws preserve wide image and glyph offsets', () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-texture-java-')); - const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name; - const root = path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app'); - try { - const clock = path.join(directory, 'SystemClock.java'); - const log = path.join(directory, 'Log.java'); - fs.writeFileSync(clock, 'package android.os; public class SystemClock { public static long elapsedRealtime() { return 0; } }'); - fs.writeFileSync(log, 'package android.util; public class Log { public static int i(String tag, String msg) { return 0; } }'); - const sources = ['GlyphAtlas.java', 'ImageAtlas.java', 'FwGlyphAtlas.java', 'SurfaceCompositor.java', - 'util/BmpUtil.java', 'util/CollectionUtils.java', 'g2protocol/BleProtocol.java', - 'g2protocol/BleImageOptimizer.java', 'g2protocol/TextureCacheState.java', 'g2protocol/TexturePlanner.java']; - execFileSync(javaBin('javac'), ['-d', directory, ...sources.map(file => path.join(root, file)), - clock, log, path.join(__dirname, 'fixtures/TextureCacheProtocolTest.java')], { stdio: 'pipe' }); - execFileSync(javaBin('java'), ['-ea', '-cp', directory, 'com.faceclaw.app.TextureCacheProtocolTest'], { stdio: 'pipe' }); - } finally { fs.rmSync(directory, { recursive: true, force: true }); } -});