@mentra/bluetooth-sdk 3.4.0-dev.999 → 3.4.1-beta.1409

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/MIGRATION.md +16 -7
  2. package/README.md +41 -14
  3. package/android/build.gradle +60 -51
  4. package/android/gradle.properties +3 -0
  5. package/android/settings.gradle +47 -3
  6. package/android/src/main/assets/nimo/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
  7. package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
  8. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -1
  9. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +77 -6
  10. package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +41 -0
  11. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +131 -122
  12. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +3 -2
  13. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
  14. package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +31 -10
  15. package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +57 -196
  16. package/android/src/main/java/com/mentra/bluetoothsdk/OtaManifest.kt +1 -16
  17. package/android/src/main/java/com/mentra/bluetoothsdk/debug/BleTraceLogger.kt +10 -1
  18. package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
  19. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/Ipv4Prefix.kt +51 -0
  20. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/NetworkFacts.kt +81 -0
  21. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkError.kt +108 -0
  22. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadiness.kt +103 -0
  23. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpec.kt +44 -0
  24. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkState.kt +117 -0
  25. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedSoftApNetwork.kt +598 -0
  26. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/SoftApTrace.kt +143 -0
  27. package/android/src/main/java/com/mentra/bluetoothsdk/otaserver/MentraOtaServerModule.kt +0 -7
  28. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/ClassicAudioConnectionTracker.kt +15 -0
  29. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +308 -60
  30. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +14 -5
  31. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +210 -325
  32. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +0 -35
  33. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +2 -10
  34. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdater.kt +213 -0
  35. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocol.kt +112 -0
  36. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
  37. package/android/src/main/java/com/mentra/bluetoothsdk/streaming/StreamModels.kt +17 -6
  38. package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
  39. package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
  40. package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
  41. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
  42. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
  43. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
  44. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
  45. package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
  46. package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +2 -2
  47. package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
  48. package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -0
  49. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
  50. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
  51. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
  52. package/android/src/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
  53. package/android/src/test/java/com/mentra/bluetoothsdk/StreamRequestCaptureAudioTest.kt +23 -4
  54. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/Ipv4PrefixTest.kt +59 -0
  55. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/NetworkFactsTest.kt +59 -0
  56. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedLocalNetworkPermissionTest.kt +35 -0
  57. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkErrorTest.kt +85 -0
  58. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkLifecycleTest.kt +164 -0
  59. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadinessTest.kt +141 -0
  60. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpecTest.kt +56 -0
  61. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/SoftApTraceTest.kt +97 -0
  62. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/UnavailableRetryTest.kt +47 -0
  63. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/ClassicAudioConnectionTrackerTest.kt +26 -0
  64. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +141 -0
  65. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
  66. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +9 -3
  67. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveRgbLedResponseTest.kt +76 -0
  68. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
  69. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
  70. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareTargetTest.kt +15 -0
  71. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdaterTest.kt +76 -0
  72. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocolTest.kt +77 -0
  73. package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -0
  74. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoReadinessDriverTest.kt +31 -1
  75. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
  76. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
  77. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
  78. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
  79. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
  80. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
  81. package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
  82. package/build/BluetoothSdk.types.d.ts +103 -5
  83. package/build/BluetoothSdk.types.d.ts.map +1 -1
  84. package/build/BluetoothSdk.types.js.map +1 -1
  85. package/build/_private/BluetoothSdkModule.d.ts +42 -8
  86. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  87. package/build/_private/BluetoothSdkModule.js +1 -0
  88. package/build/_private/BluetoothSdkModule.js.map +1 -1
  89. package/build/_private/MentraLocalNetworkModule.d.ts +1 -1
  90. package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
  91. package/build/_private/MentraLocalNetworkModule.js.map +1 -1
  92. package/build/generated/changelogCatalog.d.ts +5 -2
  93. package/build/generated/changelogCatalog.d.ts.map +1 -1
  94. package/build/generated/changelogCatalog.js +6 -2
  95. package/build/generated/changelogCatalog.js.map +1 -1
  96. package/build/generated/releaseMetadata.js +6 -6
  97. package/build/generated/releaseMetadata.js.map +1 -1
  98. package/build/index.d.ts +1 -1
  99. package/build/index.d.ts.map +1 -1
  100. package/build/index.js +7 -1
  101. package/build/index.js.map +1 -1
  102. package/build/ota-server/MentraOtaServerModule.d.ts +0 -2
  103. package/build/ota-server/MentraOtaServerModule.d.ts.map +1 -1
  104. package/build/ota-server/MentraOtaServerModule.js.map +1 -1
  105. package/build/ota-server/MentraOtaServerModule.web.d.ts +0 -1
  106. package/build/ota-server/MentraOtaServerModule.web.d.ts.map +1 -1
  107. package/build/ota-server/MentraOtaServerModule.web.js +0 -3
  108. package/build/ota-server/MentraOtaServerModule.web.js.map +1 -1
  109. package/build/ota-transport/index.d.ts +3 -3
  110. package/build/ota-transport/index.d.ts.map +1 -1
  111. package/build/ota-transport/index.js +4 -4
  112. package/build/ota-transport/index.js.map +1 -1
  113. package/build/react/useGlassesConnection.d.ts +1 -1
  114. package/build/react/useGlassesConnection.d.ts.map +1 -1
  115. package/build/react/useGlassesConnection.js +3 -3
  116. package/build/react/useGlassesConnection.js.map +1 -1
  117. package/build/react/useMentraBluetooth.d.ts +1 -1
  118. package/build/react/useMentraBluetooth.d.ts.map +1 -1
  119. package/build/react/useMentraBluetooth.js +1 -1
  120. package/build/react/useMentraBluetooth.js.map +1 -1
  121. package/ios/BluetoothSdkModule.swift +73 -22
  122. package/ios/LocalIPv4.swift +0 -61
  123. package/ios/MentraBluetoothSDK.podspec +7 -1
  124. package/ios/MentraOtaServerModule.swift +0 -16
  125. package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
  126. package/ios/Resources/NimoFirmware/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
  127. package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
  128. package/ios/Source/BesCrashReportWaiters.swift +64 -0
  129. package/ios/Source/BluetoothSdkDefaults.swift +4 -4
  130. package/ios/Source/Bridge.swift +14 -0
  131. package/ios/Source/DeviceManager.swift +52 -22
  132. package/ios/Source/GeneratedChangelogCatalog.swift +3 -2
  133. package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
  134. package/ios/Source/MentraBluetoothSDK.swift +35 -16
  135. package/ios/Source/OtaManifest.swift +1 -17
  136. package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
  137. package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
  138. package/ios/Source/audio/SpeechActivity.swift +277 -0
  139. package/ios/Source/events/BluetoothEvents.swift +41 -0
  140. package/ios/Source/internal/BleTraceLogger.swift +14 -1
  141. package/ios/Source/internal/BluetoothAvailability.swift +15 -5
  142. package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +5 -0
  143. package/ios/Source/sgcs/MentraLive.swift +207 -66
  144. package/ios/Source/sgcs/Nimo.swift +249 -276
  145. package/ios/Source/sgcs/NimoCanvasCoordinator.swift +9 -38
  146. package/ios/Source/sgcs/NimoCanvasSession.swift +0 -1
  147. package/ios/Source/sgcs/NimoFirmwareUpdater.swift +260 -0
  148. package/ios/Source/sgcs/NimoOtaProtocol.swift +159 -0
  149. package/ios/Source/sgcs/SGCManager.swift +6 -0
  150. package/ios/Source/streaming/StreamModels.swift +15 -6
  151. package/ios/Source/stt/STTTools.swift +13 -31
  152. package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
  153. package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
  154. package/ios/Source/stt/WakeWordDetector.swift +145 -0
  155. package/ios/Source/utils/BoundedWait.swift +44 -0
  156. package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
  157. package/ios/Source/utils/SttModelTraits.swift +62 -0
  158. package/ios/Tests/BesCrashReportTests.swift +142 -0
  159. package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
  160. package/ios/Tests/DeviceManagerReconnectTests.swift +0 -71
  161. package/ios/Tests/GlassesAudioSetupPolicyTests.swift +9 -3
  162. package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
  163. package/ios/Tests/MentraLiveAudioSetupTests.swift +6 -12
  164. package/ios/Tests/MentraLiveCommandQueueTests.swift +35 -0
  165. package/ios/Tests/MentraLiveSessionReadinessTests.swift +179 -10
  166. package/ios/Tests/NimoCanvasTransportTests.swift +0 -89
  167. package/ios/Tests/NimoOtaProtocolTests.swift +78 -0
  168. package/ios/Tests/PcmStreamTests.swift +299 -0
  169. package/ios/Tests/SpeechActivityTests.swift +238 -0
  170. package/ios/Tests/StreamRequestTests.swift +18 -0
  171. package/ios/Tests/SttModelTraitsTests.swift +62 -0
  172. package/package.json +2 -2
  173. package/plugin/build/withAndroid.js +9 -11
  174. package/plugin/build/withKotlinVersion.d.ts +2 -0
  175. package/plugin/build/withKotlinVersion.js +46 -0
  176. package/src/BluetoothSdk.types.ts +107 -5
  177. package/src/_private/BluetoothSdkModule.ts +45 -8
  178. package/src/_private/MentraLocalNetworkModule.ts +5 -1
  179. package/src/generated/changelogCatalog.ts +6 -2
  180. package/src/generated/releaseMetadata.ts +6 -6
  181. package/src/index.ts +13 -1
  182. package/src/ota-server/MentraOtaServerModule.ts +0 -2
  183. package/src/ota-server/MentraOtaServerModule.web.ts +0 -4
  184. package/src/ota-transport/index.ts +4 -5
  185. package/src/react/useGlassesConnection.ts +4 -4
  186. package/src/react/useMentraBluetooth.ts +2 -2
  187. package/android/silero/LICENSE.md +0 -17
  188. package/android/silero/build.gradle +0 -50
  189. package/android/silero/proguard-rules.pro +0 -24
  190. package/android/silero/src/androidTest/assets/hello.wav +0 -0
  191. package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
  192. package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
  193. package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
  194. package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
  195. package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
  196. package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
  197. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
  198. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
  199. package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
  200. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
  201. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
  202. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -274
  203. package/android/src/main/java/com/mentra/bluetoothsdk/ScopedNetworkReadiness.kt +0 -17
  204. package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
  205. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
  206. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -16
  207. package/android/src/test/java/com/mentra/bluetoothsdk/ScopedNetworkReadinessTest.kt +0 -39
  208. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
  209. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
  210. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -851
  211. package/ios/Source/sgcs/NimoMacResourceTransport.swift +0 -217
  212. package/ios/Source/sgcs/NimoResourceFixture.swift +0 -384
  213. package/ios/Source/sgcs/NimoResourceSession.swift +0 -406
  214. package/ios/Tests/NimoResourceSessionTests.swift +0 -476
  215. /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
  216. /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
@@ -1,287 +0,0 @@
1
- package com.mentra.bluetoothsdk.sgcs
2
-
3
- import android.content.Context
4
- import android.os.Handler
5
- import android.os.Looper
6
- import androidx.test.core.app.ApplicationProvider
7
- import java.io.File
8
- import java.util.concurrent.TimeUnit
9
- import org.json.JSONObject
10
- import org.junit.After
11
- import org.junit.Assert.*
12
- import org.junit.Before
13
- import org.junit.Test
14
- import org.junit.runner.RunWith
15
- import org.robolectric.RobolectricTestRunner
16
- import org.robolectric.Shadows
17
- import org.robolectric.annotation.Config
18
- import org.robolectric.annotation.LooperMode
19
-
20
- @RunWith(RobolectricTestRunner::class)
21
- @Config(sdk = [33])
22
- @LooperMode(LooperMode.Mode.PAUSED)
23
- class NimoDiagnosticsTest {
24
- private data class Write(val packet: ByteArray, val completed: () -> Unit)
25
- private val writes = mutableListOf<Write>()
26
- private var heldReady: (() -> Unit)? = null
27
- private val holdReleases = mutableListOf<Boolean>()
28
- private lateinit var diagnostics: NimoDiagnostics
29
- private lateinit var root: File
30
- private lateinit var previousDirectories: Set<String>
31
-
32
- @Before fun setup() {
33
- val context = ApplicationProvider.getApplicationContext<Context>()
34
- root = File(context.filesDir, "nimo-framebuffer")
35
- previousDirectories = root.listFiles()?.map { it.name }?.toSet() ?: emptySet()
36
- diagnostics = NimoDiagnostics(
37
- context,
38
- Handler(Looper.getMainLooper()),
39
- { frame, complete -> writes.add(Write(frame.copyOf(), complete)); true },
40
- { ready -> heldReady = ready },
41
- { resume -> holdReleases += resume },
42
- )
43
- diagnostics.connected(512)
44
- }
45
-
46
- @After fun dispose() { diagnostics.close() }
47
-
48
- @Test fun probePreservesNotReadyAndAcceptsResponsesBeforeWriteCallbacks() {
49
- start("probe")
50
- driveToPageGate()
51
- assertTrue(diagnostics.onPacket(fbpReply(writes[3].packet, ByteArray(240))))
52
- val result = receipt()
53
- assertTrue(result.getBoolean("probe_ok"))
54
- assertFalse(result.getBoolean("ok"))
55
- assertTrue(result.isNull("error"))
56
- assertEquals(0, result.getJSONObject("readiness").getInt("ready"))
57
- assertEquals(1, result.getJSONObject("readiness").getInt("tws"))
58
- assertEquals(NimoFramebufferProtocol.EXPECTED_VERSION, result.getString("runtime_version"))
59
- assertEquals(8, result.getInt("wire_events"))
60
- assertEquals(8, File(captureDirectory(), "wire.jsonl").readLines().size)
61
- writes.toList().forEach { it.completed() }
62
- idle(4_000)
63
- assertEquals(4, writes.size)
64
- assertTrue(receipt().getBoolean("probe_ok"))
65
- assertTrue(writes.all { it.packet[8] == 2.toByte() && (it.packet[9].toInt() and 255) in listOf(0x0B, 0x19, 0x15) })
66
- }
67
-
68
- @Test fun duplicateTriggerDoesNotStartAnotherRead() {
69
- start("capture")
70
- start("probe")
71
- assertEquals(1, writes.size)
72
- assertEquals(1, newDirectories().size)
73
- }
74
-
75
- @Test fun disconnectCancelsPendingTimeoutAndIgnoresLateWriteCompletion() {
76
- start("capture")
77
- writes.single().completed()
78
- diagnostics.disconnected()
79
- val error = receipt().getString("error")
80
- assertTrue(error.contains("disconnected"))
81
- diagnostics.connected(512)
82
- writes.single().completed()
83
- idle(4_000)
84
- assertEquals(error, receipt().getString("error"))
85
- assertEquals(1, writes.size)
86
- assertFalse(diagnostics.onPacket(versionReply()))
87
- assertFalse(receipt().getBoolean("ok"))
88
- }
89
-
90
- @Test fun timeoutStartsAtWriteCompletionAndDoesNotRetry() {
91
- start("probe")
92
- idle(5_000)
93
- assertFalse(File(captureDirectory(), "session.json").exists())
94
- writes.single().completed()
95
- idle(2_900)
96
- assertFalse(File(captureDirectory(), "session.json").exists())
97
- idle(200)
98
- assertTrue(receipt().getString("error").contains("response timeout"))
99
- assertEquals(1, writes.size)
100
- }
101
-
102
- @Test fun captureRejectsCrcBeforeRequestingBulkPages() {
103
- start("capture")
104
- driveToPageGate()
105
- val corrupted = fbpReply(writes[3].packet, ByteArray(240)).also { it[4] = (it[4].toInt() xor 1).toByte() }
106
- assertTrue(diagnostics.onPacket(corrupted))
107
- assertFalse(receipt().getBoolean("ok"))
108
- assertFalse(receipt().getBoolean("probe_ok"))
109
- assertTrue(receipt().getString("error").contains("CRC mismatch"))
110
- assertEquals(4, writes.size)
111
- assertEquals(0, receipt().getJSONArray("banks").length())
112
- }
113
-
114
- @Test fun totalDeadlineBoundsAWriteThatNeverCompletes() {
115
- start("capture")
116
- idle(600_001)
117
- assertTrue(receipt().getString("error").contains("10-minute budget"))
118
- writes.single().completed()
119
- idle(4_000)
120
- assertEquals(1, writes.size)
121
- }
122
-
123
- @Test fun heldSampleBudgetIncludesCanvasDrainAndStaleReadyCannotStartCapture() {
124
- start("held-sample")
125
- assertTrue(writes.isEmpty())
126
- assertNotNull(heldReady)
127
-
128
- idle(15_001)
129
-
130
- assertEquals(listOf(true), holdReleases)
131
- assertTrue(receipt().getString("error").contains("15-second budget"))
132
- heldReady?.invoke()
133
- assertTrue(writes.isEmpty())
134
- assertEquals(listOf(true), holdReleases)
135
- }
136
-
137
- @Test fun disconnectBeforeHeldCaptureIsReadyReleasesWithoutResumingCanvas() {
138
- start("held-capture")
139
-
140
- diagnostics.disconnected()
141
-
142
- assertEquals(listOf(false), holdReleases)
143
- assertTrue(receipt().getString("error").contains("disconnected"))
144
- heldReady?.invoke()
145
- assertTrue(writes.isEmpty())
146
- assertEquals(listOf(false), holdReleases)
147
- }
148
-
149
- @Test fun duplicateHeldCaptureDoesNotReplaceHoldOrDeadline() {
150
- start("held-sample")
151
- val firstReady = heldReady
152
-
153
- start("held-capture")
154
-
155
- assertSame(firstReady, heldReady)
156
- assertEquals(1, newDirectories().size)
157
- idle(15_001)
158
- assertEquals(listOf(true), holdReleases)
159
- }
160
-
161
- @Test fun insufficientCapacityFailsWithoutSendingDiagnosticCommands() {
162
- diagnostics.connected(20)
163
- start("capture")
164
- assertEquals(0, writes.size)
165
- assertTrue(receipt().getString("error").contains("Write capacity"))
166
- }
167
-
168
- @Test fun samplePinsPreparedBankAndNeverClaimsStaticPairVerification() {
169
- assertSample(1)
170
- }
171
-
172
- @Test fun sampleAlsoPinsPreparedBankZero() {
173
- assertSample(0)
174
- }
175
-
176
- @Test fun heldSampleStartsOnlyAfterDrainAndReleasesOnSuccess() {
177
- assertSample(1, "held-sample")
178
- assertEquals(listOf(true), holdReleases)
179
- }
180
-
181
- private fun assertSample(bank: Int, mode: String = "sample") {
182
- val state = byteArrayOf((1 - bank).toByte(), bank.toByte(), 0, 0)
183
- start(mode)
184
- if (mode == "held-sample") {
185
- assertTrue(writes.isEmpty())
186
- requireNotNull(heldReady).invoke()
187
- }
188
- driveToPageGate()
189
- diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240), state))
190
- // The gate always probes bank0/page0; the sample follows its prepared selector.
191
- repeat(315) { page ->
192
- val request = writes.last().packet
193
- assertEquals(bank, request[21].toInt())
194
- assertEquals(page, (request[22].toInt() and 255) or ((request[23].toInt() and 255) shl 8))
195
- diagnostics.onPacket(fbpReply(request, ByteArray(240) { page.toByte() }, state))
196
- }
197
- val result = receipt()
198
- assertTrue(result.getBoolean("sample_ok"))
199
- assertFalse(result.getBoolean("ok"))
200
- assertFalse(result.getBoolean("atomic"))
201
- assertFalse(result.getBoolean("optical"))
202
- assertEquals("non_atomic_single_pass", result.getString("evidence_kind"))
203
- assertEquals(0, result.getInt("static_pairs_verified"))
204
- assertEquals(1, result.getJSONArray("banks").length())
205
- assertEquals(319, writes.size)
206
- assertEquals(638, result.getInt("wire_events"))
207
- assertFalse(result.getBoolean("pixel_stability_verified"))
208
- assertEquals(mode, result.getString("mode"))
209
- assertEquals(75_600, File(captureDirectory(), "bank$bank-pass1.bin").length().toInt())
210
- }
211
-
212
- @Test fun sampleRejectsSelectorChangeBetweenGateAndFirstPageWithoutRetrying() {
213
- start("sample")
214
- driveToPageGate()
215
- diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240)))
216
- diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240), byteArrayOf(1, 0, 0, 0)))
217
- val result = receipt()
218
- assertFalse(result.getBoolean("sample_ok"))
219
- assertFalse(result.getBoolean("ok"))
220
- assertTrue(result.getString("error").contains("Sample metadata differs from page gate"))
221
- assertEquals(5, writes.size)
222
- assertEquals(0, result.getJSONArray("banks").length())
223
- }
224
-
225
- @Test fun sampleTimeoutCannotProduceSuccessOrStartASecondPass() {
226
- start("sample")
227
- driveToPageGate()
228
- diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240)))
229
- writes.last().completed()
230
- idle(3_001)
231
- assertFalse(receipt().getBoolean("sample_ok"))
232
- assertFalse(receipt().getBoolean("ok"))
233
- assertEquals(5, writes.size)
234
- }
235
-
236
- @Test fun fullCaptureStillRequiresFourCompleteReadsAndTwoStaticPairs() {
237
- start("capture")
238
- driveToPageGate()
239
- diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240)))
240
- repeat(4) { pass ->
241
- repeat(315) { page ->
242
- val request = writes.last().packet
243
- assertEquals(pass / 2, request[21].toInt())
244
- diagnostics.onPacket(fbpReply(request, ByteArray(240) { page.toByte() }))
245
- }
246
- if (pass < 3) assertFalse(File(captureDirectory(), "session.json").exists())
247
- }
248
- assertTrue(receipt().getBoolean("ok"))
249
- assertEquals(2, receipt().getInt("static_pairs_verified"))
250
- assertEquals(4, receipt().getJSONArray("banks").length())
251
- assertEquals(1264, writes.size)
252
- }
253
-
254
- private fun driveToPageGate() {
255
- assertEquals(1, writes.size)
256
- assertFalse(diagnostics.onPacket(versionReply()))
257
- assertEquals(2, writes.size)
258
- assertFalse(diagnostics.onPacket(frame(0x19, byteArrayOf(0, 0, 1, 0))))
259
- assertEquals(3, writes.size)
260
- assertTrue(diagnostics.onPacket(fbpReply(writes[2].packet, "537cf1p1".toByteArray())))
261
- assertEquals(4, writes.size)
262
- }
263
-
264
- // Exercise the production coordinator without pretending Robolectric proves Android's
265
- // signature-permission enforcement for the ADB receiver. That boundary needs a device test.
266
- private fun start(mode: String) {
267
- NimoDiagnostics::class.java.getDeclaredMethod("start", String::class.java).apply { isAccessible = true }
268
- .invoke(diagnostics, mode)
269
- }
270
- private fun idle(milliseconds: Long) = Shadows.shadowOf(Looper.getMainLooper()).idleFor(milliseconds, TimeUnit.MILLISECONDS)
271
- private fun newDirectories() = root.listFiles()?.filter { it.name !in previousDirectories } ?: emptyList()
272
- private fun captureDirectory() = newDirectories().single()
273
- private fun receipt() = JSONObject(File(captureDirectory(), "session.json").readText())
274
- private fun versionReply() = frame(0x0B, byteArrayOf(0) + NimoFramebufferProtocol.EXPECTED_VERSION.toByteArray())
275
-
276
- private fun fbpReply(request: ByteArray, data: ByteArray, state: ByteArray = byteArrayOf(0, 1, 0, 0)): ByteArray {
277
- val payload = byteArrayOf(0) + "FBP1".toByteArray() + request.copyOfRange(16, 24) + state + state +
278
- byteArrayOf(data.size.toByte(), (data.size shr 8).toByte()) + data
279
- return frame(0x15, payload)
280
- }
281
- private fun frame(key: Int, payload: ByteArray): ByteArray {
282
- val body = byteArrayOf(2, key.toByte(), payload.size.toByte(), (payload.size shr 8).toByte()) + payload
283
- val crc = NimoCanvasCodec.crc16(body)
284
- return byteArrayOf(0xBF.toByte(), 0, body.size.toByte(), (body.size shr 8).toByte(),
285
- crc.toByte(), (crc shr 8).toByte(), 0, 0) + body
286
- }
287
- }
@@ -1,127 +0,0 @@
1
- package com.mentra.bluetoothsdk.sgcs
2
-
3
- import org.junit.Assert.*
4
- import org.junit.Test
5
-
6
- class NimoFramebufferProtocolTest {
7
- @Test fun hexPreservesEveryByteAndEmptyEvidence() {
8
- val bytes = ByteArray(256) { it.toByte() }
9
- assertEquals(bytes.joinToString("") { "%02x".format(it.toInt() and 255) }, NimoFramebufferProtocol.hex(bytes))
10
- assertEquals("", NimoFramebufferProtocol.hex(byteArrayOf()))
11
- }
12
- private val pingRequest = NimoFramebufferProtocol.Request(0x01020304L, 0, 0, 0)
13
- // Fixed vectors produced independently by the existing Python FBP1 codec.
14
- private val ping = hex("bf0023008234000002151f0000464250310403020100000000000100000001000008003533376366317031")
15
- private val page = hex("bf000b0175e500000215070100464250310403020101013a010001000000010000f000") +
16
- ByteArray(240) { it.toByte() }
17
-
18
- @Test fun requestAndReplyMatchIndependentGoldenVectors() {
19
- assertArrayEquals(hex("bf001000227a000002150c00464250310403020100000000"),
20
- NimoFramebufferProtocol.requestFrame(pingRequest))
21
- val reply = NimoFramebufferProtocol.reply(ping, pingRequest)
22
- assertArrayEquals("537cf1p1".toByteArray(), reply.data)
23
- assertEquals(NimoFramebufferProtocol.State(0, 1, 0, 0), reply.before)
24
- val expectedPage = NimoFramebufferProtocol.Request(0x01020304L, 1, 1, 314)
25
- assertArrayEquals(ByteArray(240) { it.toByte() }, NimoFramebufferProtocol.reply(page, expectedPage).data)
26
- }
27
-
28
- @Test fun rejectsCrcLengthsDirectionIndexAndCommand() {
29
- for (offset in listOf(0, 1, 2, 4, 6, 8, 9, 10)) {
30
- val invalid = ping.copyOf().also { it[offset] = (it[offset].toInt() xor 1).toByte() }
31
- rejects { NimoFramebufferProtocol.reply(invalid, pingRequest) }
32
- }
33
- rejects { NimoFramebufferProtocol.reply(ping + 0, pingRequest) }
34
- rejects { NimoFramebufferProtocol.reply(ping.copyOf(ping.size - 1), pingRequest) }
35
- }
36
-
37
- @Test fun rejectsWrongTagMagicNonceOperationBankAndPage() {
38
- for (offset in listOf(13, 17, 21, 22, 23, 33, 35)) {
39
- val invalid = changeAndRecrc(ping, offset, ping[offset].toInt() xor 1)
40
- rejects { NimoFramebufferProtocol.reply(invalid, pingRequest) }
41
- }
42
- rejects { NimoFramebufferProtocol.reply(ping, pingRequest.copy(nonce = 9)) }
43
- rejects { NimoFramebufferProtocol.reply(ping, pingRequest.copy(op = 1)) }
44
- }
45
-
46
- @Test fun rejectsStatusErrorsEvenWithValidCorrelation() {
47
- for (status in listOf(1, 6, 7, 255)) {
48
- val payload = ping.copyOfRange(12, 35).also { it[0] = status.toByte(); it[21] = 0; it[22] = 0 }
49
- rejects { NimoFramebufferProtocol.reply(frame(0x15, payload), pingRequest) }
50
- }
51
- rejects { NimoFramebufferProtocol.reply(changeAndRecrc(ping, 12, 6), pingRequest) }
52
- }
53
-
54
- @Test fun rejectsMetadataChangedWithinAndAcrossPagesAndOutOfOrder() {
55
- val request = NimoFramebufferProtocol.Request(0x01020304L, 1, 1, 0)
56
- val first = NimoFramebufferProtocol.reply(pageAt(0), request)
57
- val read = NimoFramebufferProtocol.BankRead(request.nonce, request.bank)
58
- read.add(first)
59
- rejects { read.add(first) }
60
- rejects { NimoFramebufferProtocol.BankRead(request.nonce, request.bank).add(first.copy(page = 1)) }
61
- rejects { NimoFramebufferProtocol.reply(changeAndRecrc(pageAt(0), 29, 1), request) }
62
- val changed = first.copy(page = 1, before = first.before.copy(dirty = 1), after = first.after.copy(dirty = 1))
63
- val changedRead = NimoFramebufferProtocol.BankRead(request.nonce, request.bank)
64
- changedRead.add(first)
65
- rejects { changedRead.add(changed) }
66
- }
67
-
68
- @Test fun bankRequiresEveryPageAndIndependentEqualSecondPass() {
69
- val first = completeBank(10)
70
- val second = completeBank(11)
71
- NimoFramebufferProtocol.verifyPair(first, second)
72
- assertEquals(75600, first.bytes().size)
73
- rejects { NimoFramebufferProtocol.verifyPair(first, first) }
74
- rejects { NimoFramebufferProtocol.verifyPair(first, completeBank(12, changedLastPixel = true)) }
75
- rejects { NimoFramebufferProtocol.BankRead(20, 1).bytes() }
76
- }
77
-
78
- @Test fun requestLimitsPreventArbitraryCommandsOrAddresses() {
79
- for (request in listOf(pingRequest.copy(nonce = -1), pingRequest.copy(nonce = 0x100000000L),
80
- pingRequest.copy(op = 2), pingRequest.copy(bank = 2), pingRequest.copy(page = 1),
81
- pingRequest.copy(op = 1, page = 315))) {
82
- rejects { NimoFramebufferProtocol.requestFrame(request) }
83
- }
84
- rejects { NimoFramebufferProtocol.queryFrame(0x0A) }
85
- }
86
-
87
- @Test fun basicResponsesRequireExactVersionAndReadinessShape() {
88
- val version = byteArrayOf(0) + NimoFramebufferProtocol.EXPECTED_VERSION.toByteArray()
89
- assertEquals(NimoFramebufferProtocol.EXPECTED_VERSION,
90
- NimoFramebufferProtocol.version(frame(0x0B, version)))
91
- rejects { NimoFramebufferProtocol.version(frame(0x0B, version + 0)) }
92
- rejects { NimoFramebufferProtocol.version(frame(0x0B, version.copyOf().also { it[0] = 7 })) }
93
- assertEquals(listOf(0, 0, 1, 0), NimoFramebufferProtocol.readiness(frame(0x19, byteArrayOf(0, 0, 1, 0))))
94
- rejects { NimoFramebufferProtocol.readiness(frame(0x19, byteArrayOf(0, 1, 1))) }
95
- }
96
-
97
- private fun completeBank(nonce: Long, changedLastPixel: Boolean = false): NimoFramebufferProtocol.BankRead {
98
- val read = NimoFramebufferProtocol.BankRead(nonce, 1)
99
- repeat(315) { index ->
100
- val original = NimoFramebufferProtocol.reply(pageAt(index),
101
- NimoFramebufferProtocol.Request(0x01020304L, 1, 1, index))
102
- val data = original.data.copyOf()
103
- if (changedLastPixel && index == 314) data[239] = 0
104
- read.add(original.copy(nonce = nonce, data = data))
105
- }
106
- return read
107
- }
108
-
109
- private fun pageAt(index: Int): ByteArray {
110
- val result = page.copyOf()
111
- result[23] = index.toByte(); result[24] = (index shr 8).toByte()
112
- return recrc(result)
113
- }
114
- private fun changeAndRecrc(value: ByteArray, index: Int, byte: Int) =
115
- recrc(value.copyOf().also { it[index] = byte.toByte() })
116
- private fun recrc(value: ByteArray): ByteArray {
117
- val crc = NimoCanvasCodec.crc16(value.copyOfRange(8, value.size))
118
- value[4] = crc.toByte(); value[5] = (crc shr 8).toByte()
119
- return value
120
- }
121
- private fun frame(key: Int, payload: ByteArray): ByteArray {
122
- val body = byteArrayOf(2, key.toByte(), payload.size.toByte(), (payload.size shr 8).toByte()) + payload
123
- return recrc(byteArrayOf(0xBF.toByte(), 0, body.size.toByte(), (body.size shr 8).toByte(), 0, 0, 0, 0) + body)
124
- }
125
- private fun rejects(block: () -> Unit) { assertThrows(IllegalArgumentException::class.java, block) }
126
- private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
127
- }