@mentra/bluetooth-sdk 3.3.0-dev.944 → 3.4.0-beta.1237

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 (174) hide show
  1. package/MIGRATION.md +61 -0
  2. package/README.md +111 -11
  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/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
  7. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -2
  8. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +89 -6
  9. package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +35 -1
  10. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +159 -110
  11. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceStore.kt +21 -0
  12. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +2 -1
  13. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
  14. package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +35 -2
  15. package/android/src/main/java/com/mentra/bluetoothsdk/TouchGesture.kt +19 -0
  16. package/android/src/main/java/com/mentra/bluetoothsdk/connection/ConnectionModels.kt +5 -0
  17. package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
  18. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicy.kt +64 -0
  19. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +1002 -157
  20. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +227 -0
  21. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisement.kt +19 -0
  22. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicy.kt +94 -0
  23. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachine.kt +411 -0
  24. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateStore.kt +124 -0
  25. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +54 -59
  26. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +5 -32
  27. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasEncoder.kt +8 -1
  28. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +3 -10
  29. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
  30. package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +50 -0
  31. package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
  32. package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
  33. package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
  34. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
  35. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
  36. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
  37. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
  38. package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
  39. package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +2 -2
  40. package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
  41. package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -0
  42. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
  43. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
  44. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
  45. package/android/src/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
  46. package/android/src/test/java/com/mentra/bluetoothsdk/DeviceManagerOwnerLossTest.kt +146 -0
  47. package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusAudioSetupTest.kt +51 -0
  48. package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusClassicAudioTest.kt +2 -0
  49. package/android/src/test/java/com/mentra/bluetoothsdk/TouchGestureCommandTest.kt +112 -0
  50. package/android/src/test/java/com/mentra/bluetoothsdk/WifiAdbPeerIntentTest.kt +19 -0
  51. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicyTest.kt +65 -0
  52. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveAudioSetupReadinessTest.kt +231 -0
  53. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +141 -0
  54. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
  55. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +368 -0
  56. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisementParserTest.kt +20 -0
  57. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicyTest.kt +136 -0
  58. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachineTest.kt +346 -0
  59. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveRgbLedResponseTest.kt +76 -0
  60. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
  61. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
  62. package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -0
  63. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
  64. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
  65. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
  66. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
  67. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
  68. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
  69. package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
  70. package/build/BluetoothSdk.types.d.ts +207 -2
  71. package/build/BluetoothSdk.types.d.ts.map +1 -1
  72. package/build/BluetoothSdk.types.js.map +1 -1
  73. package/build/_private/BluetoothSdkModule.d.ts +45 -6
  74. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  75. package/build/_private/BluetoothSdkModule.js +2 -0
  76. package/build/_private/BluetoothSdkModule.js.map +1 -1
  77. package/build/generated/changelogCatalog.d.ts +4 -1
  78. package/build/generated/changelogCatalog.d.ts.map +1 -1
  79. package/build/generated/changelogCatalog.js +5 -1
  80. package/build/generated/changelogCatalog.js.map +1 -1
  81. package/build/generated/releaseMetadata.js +6 -6
  82. package/build/generated/releaseMetadata.js.map +1 -1
  83. package/build/index.d.ts +1 -1
  84. package/build/index.d.ts.map +1 -1
  85. package/build/index.js +10 -0
  86. package/build/index.js.map +1 -1
  87. package/ios/BluetoothSdkModule.swift +84 -12
  88. package/ios/MentraBluetoothSDK.podspec +4 -0
  89. package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
  90. package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
  91. package/ios/Source/BesCrashReportWaiters.swift +64 -0
  92. package/ios/Source/BluetoothSdkDefaults.swift +2 -2
  93. package/ios/Source/Bridge.swift +11 -3
  94. package/ios/Source/DeviceManager.swift +135 -18
  95. package/ios/Source/DeviceStore.swift +27 -0
  96. package/ios/Source/GeneratedChangelogCatalog.swift +2 -1
  97. package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
  98. package/ios/Source/MentraBluetoothSDK.swift +51 -5
  99. package/ios/Source/TouchGesture.swift +13 -0
  100. package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
  101. package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
  102. package/ios/Source/audio/SpeechActivity.swift +277 -0
  103. package/ios/Source/errors/BluetoothSdkError.swift +5 -1
  104. package/ios/Source/events/BluetoothEvents.swift +45 -0
  105. package/ios/Source/internal/BleTraceLogger.swift +9 -6
  106. package/ios/Source/internal/BluetoothAvailability.swift +15 -5
  107. package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +186 -0
  108. package/ios/Source/sgcs/MentraLive.swift +1366 -212
  109. package/ios/Source/sgcs/MentraLivePairingAdvertisement.swift +260 -1
  110. package/ios/Source/sgcs/MentraLivePairingStateMachine.swift +385 -0
  111. package/ios/Source/sgcs/MentraLivePairingStateStore.swift +121 -0
  112. package/ios/Source/sgcs/Nimo.swift +10 -3
  113. package/ios/Source/sgcs/NimoCanvasCoordinator.swift +39 -9
  114. package/ios/Source/sgcs/SGCManager.swift +13 -0
  115. package/ios/Source/sgcs/Simulated.swift +2 -0
  116. package/ios/Source/status/DeviceStatus.swift +35 -0
  117. package/ios/Source/stt/STTTools.swift +13 -31
  118. package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
  119. package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
  120. package/ios/Source/stt/WakeWordDetector.swift +145 -0
  121. package/ios/Source/types/DeviceModels.swift +8 -1
  122. package/ios/Source/utils/AudioSessionMonitor.swift +158 -5
  123. package/ios/Source/utils/BoundedWait.swift +44 -0
  124. package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
  125. package/ios/Source/utils/SttModelTraits.swift +62 -0
  126. package/ios/Tests/BesCrashReportTests.swift +142 -0
  127. package/ios/Tests/BluetoothAudioRouteTests.swift +82 -0
  128. package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
  129. package/ios/Tests/DeviceManagerReconnectTests.swift +4 -2
  130. package/ios/Tests/GlassesAudioSetupPolicyTests.swift +197 -0
  131. package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
  132. package/ios/Tests/MentraLiveAudioSetupTests.swift +209 -0
  133. package/ios/Tests/MentraLiveCommandQueueTests.swift +35 -0
  134. package/ios/Tests/MentraLiveConnectionOptionsTests.swift +55 -0
  135. package/ios/Tests/MentraLivePairingAdvertisementTests.swift +327 -0
  136. package/ios/Tests/MentraLivePairingStateMachineTests.swift +402 -0
  137. package/ios/Tests/MentraLiveSessionReadinessTests.swift +497 -26
  138. package/ios/Tests/NimoCanvasTransportTests.swift +58 -0
  139. package/ios/Tests/PcmStreamTests.swift +299 -0
  140. package/ios/Tests/SpeechActivityTests.swift +238 -0
  141. package/ios/Tests/SttModelTraitsTests.swift +62 -0
  142. package/ios/Tests/TouchGestureCommandTests.swift +87 -0
  143. package/ios/Tests/WifiAdbDispatchTests.swift +118 -0
  144. package/package.json +3 -2
  145. package/plugin/build/withAndroid.js +9 -11
  146. package/plugin/build/withKotlinVersion.d.ts +2 -0
  147. package/plugin/build/withKotlinVersion.js +46 -0
  148. package/src/BluetoothSdk.types.ts +216 -1
  149. package/src/_private/BluetoothSdkModule.ts +49 -5
  150. package/src/generated/changelogCatalog.ts +5 -1
  151. package/src/generated/releaseMetadata.ts +6 -6
  152. package/src/index.ts +24 -0
  153. package/android/silero/LICENSE.md +0 -17
  154. package/android/silero/build.gradle +0 -50
  155. package/android/silero/proguard-rules.pro +0 -24
  156. package/android/silero/src/androidTest/assets/hello.wav +0 -0
  157. package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
  158. package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
  159. package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
  160. package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
  161. package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
  162. package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
  163. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
  164. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
  165. package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
  166. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
  167. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
  168. package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
  169. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
  170. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
  171. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
  172. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -288
  173. /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
  174. /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
@@ -0,0 +1,346 @@
1
+ package com.mentra.bluetoothsdk.sgcs
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertNull
6
+ import org.junit.Assert.assertTrue
7
+ import org.junit.Test
8
+
9
+ class MentraLivePairingStateMachineTest {
10
+ private val t0 = 1_700_000_000_000L
11
+ private val device = "MENTRA_LIVE_BLE_ABCD"
12
+
13
+ private fun enter(
14
+ machine: MentraLivePairingStateMachine,
15
+ windowId: Int? = 7,
16
+ windowMs: Long? = 120_000L,
17
+ ownerRetained: Boolean = true,
18
+ explicit: Boolean = false,
19
+ now: Long = t0,
20
+ ) = machine.onEnteringPairingMode(windowMs, windowId, ownerRetained, explicit, device, now)
21
+
22
+ private fun pausedMachine(windowId: Int = 7): MentraLivePairingStateMachine =
23
+ MentraLivePairingStateMachine().also { enter(it, windowId = windowId) }
24
+
25
+ private fun reject(machine: MentraLivePairingStateMachine, now: Long, explicit: Boolean = false) =
26
+ machine.onAuthRejection(secureFirmware = true, explicitAttempt = explicit, nowMs = now)
27
+
28
+ @Test
29
+ fun v4NoticePausesForTheWindowPlusMarginWithoutA300sClamp() {
30
+ val machine = MentraLivePairingStateMachine()
31
+ val transition = enter(machine)
32
+ assertEquals(MentraLivePairingEffect.PAUSE, transition.effect)
33
+ assertEquals(MentraLivePairingState.PairingMode(7, t0 + 150_000L), machine.state)
34
+ assertFalse(machine.allowsAutomaticReconnect)
35
+ assertEquals(150_000L, machine.remainingPauseMs(t0))
36
+
37
+ val long = MentraLivePairingStateMachine()
38
+ enter(long, windowMs = 900_000L)
39
+ assertEquals(MentraLivePairingState.PairingMode(7, t0 + 930_000L), long.state)
40
+ }
41
+
42
+ @Test
43
+ fun publicStateMapsMatchTheTypeScriptContract() {
44
+ assertEquals(mapOf("state" to "normal"), MentraLivePairingState.Normal.toMap())
45
+ assertEquals(mapOf("state" to "pairing_rejected"), MentraLivePairingState.Rejected.toMap())
46
+ assertEquals(
47
+ mapOf("state" to "pairing_mode", "windowId" to 7, "endsAtMs" to t0),
48
+ MentraLivePairingState.PairingMode(7, t0).toMap(),
49
+ )
50
+ }
51
+
52
+ @Test
53
+ fun aDuplicateNoticeForTheSameWindowIsIgnored() {
54
+ val machine = pausedMachine()
55
+ val again = enter(machine, now = t0 + 5_000L)
56
+ assertEquals(MentraLivePairingEffect.NONE, again.effect)
57
+ assertEquals(MentraLivePairingState.PairingMode(7, t0 + 150_000L), machine.state)
58
+ }
59
+
60
+ @Test
61
+ fun anExplicitAttemptIsNeverPausedByTheNotice() {
62
+ val machine = MentraLivePairingStateMachine()
63
+ assertEquals(MentraLivePairingEffect.NONE, enter(machine, explicit = true).effect)
64
+ assertEquals(MentraLivePairingState.Normal, machine.state)
65
+ }
66
+
67
+ @Test
68
+ fun flagOffFirmwareForgetsThisPhoneOnEntry() {
69
+ val machine = MentraLivePairingStateMachine()
70
+ val transition = enter(machine, windowId = null, ownerRetained = false)
71
+ assertEquals(MentraLivePairingEffect.REJECT, transition.effect)
72
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
73
+
74
+ val pairing = MentraLivePairingStateMachine()
75
+ enter(pairing, windowId = null, ownerRetained = false, explicit = true)
76
+ assertEquals(MentraLivePairingState.Normal, pairing.state)
77
+ }
78
+
79
+ @Test
80
+ fun pairingModeEndedResumesEarlyForTheMatchingWindowOnly() {
81
+ val machine = pausedMachine()
82
+ assertEquals(MentraLivePairingEffect.NONE, machine.onPairingModeEnded("timeout", 8).effect)
83
+ assertTrue(machine.isPaused)
84
+ val ended = machine.onPairingModeEnded("paired", 7)
85
+ assertEquals(MentraLivePairingEffect.RESUME, ended.effect)
86
+ assertEquals(MentraLivePairingState.Normal, machine.state)
87
+ // The connected phone was admitted, so there is nothing to verify.
88
+ assertNull(machine.endedWindowId)
89
+ }
90
+
91
+ @Test
92
+ fun aClearedAdvertisementFlagResumesEarlyAfterTheGrace() {
93
+ val machine = pausedMachine()
94
+ // An advertisement from before the window opened can still be in flight.
95
+ val early = machine.onAdvertisement(false, false, 6, t0 + 1_000L)
96
+ assertEquals(MentraLivePairingEffect.NONE, early.effect)
97
+ assertTrue(machine.isPaused)
98
+ assertEquals(MentraLivePairingEffect.NONE, machine.onAdvertisement(true, false, 7, t0 + 20_000L).effect)
99
+ val cleared = machine.onAdvertisement(false, false, 7, t0 + 60_000L)
100
+ assertEquals(MentraLivePairingEffect.RESUME, cleared.effect)
101
+ assertEquals(MentraLivePairingState.Normal, machine.state)
102
+ assertEquals(7, machine.endedWindowId)
103
+ }
104
+
105
+ @Test
106
+ fun theDeadlineResumes() {
107
+ val machine = pausedMachine()
108
+ assertEquals(MentraLivePairingEffect.NONE, machine.onTick(t0 + 149_999L).effect)
109
+ assertEquals(MentraLivePairingEffect.RESUME, machine.onTick(t0 + 150_000L).effect)
110
+ assertTrue(machine.allowsAutomaticReconnect)
111
+ assertTrue(machine.awaitingTakeoverCheck)
112
+ }
113
+
114
+ @Test
115
+ fun ownerChangedForTheEndedWindowRejectsOnTheFirstFailedVerification() {
116
+ val machine = pausedMachine()
117
+ machine.onTick(t0 + 150_000L)
118
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 151_000L)
119
+ assertTrue(machine.verificationArmed)
120
+ assertFalse(machine.awaitingTakeoverCheck)
121
+ val verdict = reject(machine, t0 + 152_000L)
122
+ assertEquals(MentraLivePairingEffect.REJECT, verdict.effect)
123
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
124
+ }
125
+
126
+ @Test
127
+ fun ownerChangedCarriedOnTheFlagClearingAdvertisementArmsTheVerification() {
128
+ val machine = pausedMachine()
129
+ val cleared = machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 60_000L)
130
+ assertEquals(MentraLivePairingEffect.RESUME, cleared.effect)
131
+ assertTrue(machine.verificationArmed)
132
+ }
133
+
134
+ @Test
135
+ fun aSpoofedOwnerChangedBitFollowedByASuccessfulReconnectDoesNotStrandThePhone() {
136
+ val machine = pausedMachine()
137
+ machine.onTick(t0 + 150_000L)
138
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 151_000L)
139
+ assertTrue(machine.verificationArmed)
140
+ machine.onSessionReady(explicitAttempt = false)
141
+ assertEquals(MentraLivePairingState.Normal, machine.state)
142
+ assertFalse(machine.verificationArmed)
143
+ // The same bit again arms nothing: exactly one verification per window.
144
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 200_000L)
145
+ assertFalse(machine.verificationArmed)
146
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0 + 201_000L).effect)
147
+ assertEquals(MentraLivePairingState.Normal, machine.state)
148
+ }
149
+
150
+ @Test
151
+ fun ownerChangedForAnotherWindowOrStillInPairingModeArmsNothing() {
152
+ val other = pausedMachine()
153
+ other.onTick(t0 + 150_000L)
154
+ other.onAdvertisement(false, ownerChanged = true, generation = 9, nowMs = t0 + 151_000L)
155
+ assertFalse(other.verificationArmed)
156
+
157
+ val stillPairing = pausedMachine()
158
+ stillPairing.onAdvertisement(true, ownerChanged = true, generation = 7, nowMs = t0 + 60_000L)
159
+ assertFalse(stillPairing.verificationArmed)
160
+
161
+ // Without a window this phone paused for, the bit is not this phone's business.
162
+ val idle = MentraLivePairingStateMachine()
163
+ idle.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0)
164
+ assertFalse(idle.verificationArmed)
165
+ }
166
+
167
+ @Test
168
+ fun aVerificationThatFailsWithoutAnAuthVerdictFallsBackToTheStreak() {
169
+ val machine = pausedMachine()
170
+ machine.onTick(t0 + 150_000L)
171
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 151_000L)
172
+ machine.onConnectAttemptFailed()
173
+ assertFalse(machine.verificationArmed)
174
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0 + 160_000L).effect)
175
+ assertEquals(MentraLivePairingState.Normal, machine.state)
176
+ }
177
+
178
+ @Test
179
+ fun withoutTheBitThreeRejectionsSpanningTheMinimumReject() {
180
+ val machine = MentraLivePairingStateMachine()
181
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0).effect)
182
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0 + 20_000L).effect)
183
+ val third = reject(machine, t0 + MentraLiveOwnerLossPolicy.MIN_REJECTION_SPAN_MS)
184
+ assertEquals(MentraLivePairingEffect.REJECT, third.effect)
185
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
186
+ }
187
+
188
+ @Test
189
+ fun rejectionsDuringThePauseOrWhileThePairingAdvertIsFreshNeverCount() {
190
+ val machine = pausedMachine()
191
+ // The intentional disconnect and every 0x14/auth refusal inside the window.
192
+ var now = t0
193
+ repeat(6) {
194
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, now).effect)
195
+ now += 20_000L
196
+ }
197
+ assertTrue(machine.isPaused)
198
+
199
+ val advertising = MentraLivePairingStateMachine()
200
+ repeat(6) { i ->
201
+ val at = t0 + i * 20_000L
202
+ advertising.onAdvertisement(true, false, 3, at)
203
+ assertEquals(MentraLivePairingEffect.NONE, reject(advertising, at + 1_000L).effect)
204
+ }
205
+ assertEquals(MentraLivePairingState.Normal, advertising.state)
206
+ // The streak starts fresh once pairing mode is no longer advertised.
207
+ advertising.onAdvertisement(false, false, 3, t0 + 200_000L)
208
+ assertEquals(MentraLivePairingEffect.NONE, reject(advertising, t0 + 200_500L).effect)
209
+ assertEquals(MentraLivePairingState.Normal, advertising.state)
210
+ }
211
+
212
+ @Test
213
+ fun legacyFirmwareAndExplicitAttemptsNeverCount() {
214
+ val machine = MentraLivePairingStateMachine()
215
+ var now = t0
216
+ repeat(6) {
217
+ machine.onAuthRejection(secureFirmware = false, explicitAttempt = false, nowMs = now)
218
+ reject(machine, now + 1, explicit = true)
219
+ now += 20_000L
220
+ }
221
+ assertEquals(MentraLivePairingState.Normal, machine.state)
222
+ }
223
+
224
+ @Test
225
+ fun onlyAnExplicitPairOrAForgetClearsARejection() {
226
+ val machine = MentraLivePairingStateMachine()
227
+ enter(machine, windowId = null, ownerRetained = false)
228
+ machine.onSessionReady(explicitAttempt = false)
229
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
230
+ machine.onPairingModeEnded("timeout", 1)
231
+ machine.onTick(t0 + 1_000_000L)
232
+ machine.onAdvertisement(false, false, 1, t0 + 1_000_000L)
233
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
234
+ val paired = machine.onSessionReady(explicitAttempt = true)
235
+ assertTrue(paired.stateChanged)
236
+ assertEquals(MentraLivePairingState.Normal, machine.state)
237
+
238
+ val forgotten = MentraLivePairingStateMachine()
239
+ enter(forgotten, windowId = null, ownerRetained = false)
240
+ forgotten.onForget()
241
+ assertEquals(MentraLivePairingState.Normal, forgotten.state)
242
+ assertEquals("", forgotten.deviceId)
243
+ }
244
+
245
+ @Test
246
+ fun aPauseSurvivesARestartWithTheSameWindowId() {
247
+ val machine = pausedMachine(windowId = 42)
248
+ val persisted = machine.persisted()
249
+ val restored = MentraLivePairingStateMachine()
250
+ val transition = restored.restore(persisted, device, t0 + 30_000L)
251
+ assertEquals(MentraLivePairingEffect.PAUSE, transition.effect)
252
+ assertEquals(MentraLivePairingState.PairingMode(42, t0 + 150_000L), restored.state)
253
+ assertEquals(120_000L, restored.remainingPauseMs(t0 + 30_000L))
254
+ }
255
+
256
+ @Test
257
+ fun aPauseThatEndedWhileTheAppWasAwayStillVerifiesTheTakeover() {
258
+ val persisted = pausedMachine(windowId = 42).persisted()
259
+ val restored = MentraLivePairingStateMachine()
260
+ val transition = restored.restore(persisted, device, t0 + 400_000L)
261
+ assertEquals(MentraLivePairingEffect.NONE, transition.effect)
262
+ assertEquals(MentraLivePairingState.Normal, restored.state)
263
+ assertEquals(42, restored.endedWindowId)
264
+ restored.onAdvertisement(false, ownerChanged = true, generation = 42, nowMs = t0 + 401_000L)
265
+ assertTrue(restored.verificationArmed)
266
+ }
267
+
268
+ @Test
269
+ fun anArmedVerificationIsReArmedAfterARestart() {
270
+ val machine = pausedMachine(windowId = 42)
271
+ machine.onTick(t0 + 150_000L)
272
+ machine.onAdvertisement(false, ownerChanged = true, generation = 42, nowMs = t0 + 151_000L)
273
+ assertTrue(machine.verificationArmed)
274
+ val restored = MentraLivePairingStateMachine()
275
+ restored.restore(machine.persisted(), device, t0 + 160_000L)
276
+ assertFalse(restored.verificationArmed)
277
+ restored.onAdvertisement(false, ownerChanged = true, generation = 42, nowMs = t0 + 161_000L)
278
+ assertTrue(restored.verificationArmed)
279
+ }
280
+
281
+ @Test
282
+ fun anImplausibleRestoredDeadlineDoesNotPause() {
283
+ val persisted = pausedMachine().persisted().toMutableMap()
284
+ persisted[MentraLivePairingStateMachine.KEY_ENDS_AT_MS] = t0 + 10_000_000L
285
+ val restored = MentraLivePairingStateMachine()
286
+ restored.restore(persisted, device, t0)
287
+ assertEquals(MentraLivePairingState.Normal, restored.state)
288
+ }
289
+
290
+ @Test
291
+ fun aRejectionSurvivesARestart() {
292
+ val machine = MentraLivePairingStateMachine()
293
+ enter(machine, windowId = null, ownerRetained = false)
294
+ val restored = MentraLivePairingStateMachine()
295
+ val transition = restored.restore(machine.persisted(), device, t0 + 86_400_000L)
296
+ assertEquals(MentraLivePairingEffect.REJECT, transition.effect)
297
+ assertEquals(MentraLivePairingState.Rejected, restored.state)
298
+ }
299
+
300
+ @Test
301
+ fun restoreIgnoresUnknownKeysAndMalformedValues() {
302
+ val values =
303
+ mapOf(
304
+ MentraLivePairingStateMachine.KEY_DEVICE_ID to device,
305
+ MentraLivePairingStateMachine.KEY_STATE to "pairing_mode",
306
+ MentraLivePairingStateMachine.KEY_WINDOW_ID to "not a number",
307
+ MentraLivePairingStateMachine.KEY_ENDS_AT_MS to t0 + 60_000L,
308
+ "pairing_v9_future_key" to "whatever",
309
+ "unrelated" to 12,
310
+ )
311
+ val restored = MentraLivePairingStateMachine()
312
+ restored.restore(values, device, t0)
313
+ assertEquals(MentraLivePairingState.Normal, restored.state)
314
+
315
+ val future = MentraLivePairingStateMachine()
316
+ future.restore(mapOf(MentraLivePairingStateMachine.KEY_STATE to "pairing_v9_state"), device, t0)
317
+ assertEquals(MentraLivePairingState.Normal, future.state)
318
+ }
319
+
320
+ @Test
321
+ fun aRecordForOtherGlassesDoesNotApply() {
322
+ val persisted = pausedMachine().persisted()
323
+ val restored = MentraLivePairingStateMachine()
324
+ restored.restore(persisted, "MENTRA_LIVE_BLE_OTHER", t0 + 1_000L)
325
+ assertEquals(MentraLivePairingState.Normal, restored.state)
326
+ assertEquals("MENTRA_LIVE_BLE_OTHER", restored.deviceId)
327
+
328
+ val bound = MentraLivePairingStateMachine()
329
+ enter(bound, windowId = null, ownerRetained = false)
330
+ assertTrue(bound.bindDevice("MENTRA_LIVE_BLE_OTHER").stateChanged)
331
+ assertEquals(MentraLivePairingState.Normal, bound.state)
332
+ }
333
+
334
+ @Test
335
+ fun theLegacyStandDownPhaseMigratesToRejected() {
336
+ val restored = MentraLivePairingStateMachine()
337
+ val transition =
338
+ restored.restore(
339
+ mapOf(MentraLivePairingStateMachine.LEGACY_REJECTION_PHASE_KEY to "waiting"),
340
+ device,
341
+ t0,
342
+ )
343
+ assertEquals(MentraLivePairingEffect.REJECT, transition.effect)
344
+ assertEquals(MentraLivePairingState.Rejected, restored.state)
345
+ }
346
+ }
@@ -0,0 +1,76 @@
1
+ package com.mentra.bluetoothsdk.sgcs
2
+
3
+ import android.os.Looper
4
+ import androidx.test.core.app.ApplicationProvider
5
+ import com.mentra.bluetoothsdk.Bridge
6
+ import java.time.Duration
7
+ import org.json.JSONObject
8
+ import org.junit.Assert.assertEquals
9
+ import org.junit.Assert.assertTrue
10
+ import org.junit.Test
11
+ import org.junit.runner.RunWith
12
+ import org.robolectric.RobolectricTestRunner
13
+ import org.robolectric.Shadows.shadowOf
14
+ import org.robolectric.annotation.Config
15
+ import org.robolectric.annotation.LooperMode
16
+
17
+ /**
18
+ * The glasses answer every RGB LED command with `rgb_led_control_response` echoing the command's
19
+ * requestId. MentraLive must forward that reply so the pending SDK request settles instead of
20
+ * timing out.
21
+ */
22
+ @RunWith(RobolectricTestRunner::class)
23
+ @Config(sdk = [33], shadows = [G1PairingTest.ShadowLc3Cpp::class], instrumentedPackages = ["com.mentra.lc3Lib"])
24
+ @LooperMode(LooperMode.Mode.PAUSED)
25
+ class MentraLiveRgbLedResponseTest {
26
+ @Test fun `success reply settles the request it echoes`() {
27
+ val replies = forwardedReplies(
28
+ JSONObject().put("type", "rgb_led_control_response").put("requestId", "led-1")
29
+ .put("state", "success").put("success", true),
30
+ )
31
+
32
+ assertEquals(1, replies.size)
33
+ assertEquals("led-1", replies[0]["requestId"])
34
+ assertEquals("success", replies[0]["state"])
35
+ }
36
+
37
+ @Test fun `error reply carries the glasses error code`() {
38
+ val replies = forwardedReplies(
39
+ JSONObject().put("type", "rgb_led_control_response").put("requestId", "led-2")
40
+ .put("state", "error").put("success", false)
41
+ .put("errorCode", "device_not_supported").put("error", "RGB LED not supported"),
42
+ )
43
+
44
+ assertEquals(1, replies.size)
45
+ assertEquals("error", replies[0]["state"])
46
+ assertEquals("device_not_supported", replies[0]["errorCode"])
47
+ }
48
+
49
+ @Test fun `reply without a requestId is not forwarded`() {
50
+ val replies = forwardedReplies(
51
+ JSONObject().put("type", "rgb_led_control_response").put("state", "success"),
52
+ )
53
+
54
+ assertTrue(replies.isEmpty())
55
+ }
56
+
57
+ private fun forwardedReplies(reply: JSONObject): List<Map<String, Any?>> {
58
+ Bridge.initialize(ApplicationProvider.getApplicationContext())
59
+ val live = MentraLive()
60
+ val replies = mutableListOf<Map<String, Any?>>()
61
+ val sink = Bridge.addEventSink { type, body ->
62
+ if (type == "rgb_led_control_response") replies.add(body.toMap())
63
+ }
64
+ try {
65
+ MentraLive::class.java.getDeclaredMethod("processJsonMessage", JSONObject::class.java)
66
+ .apply { isAccessible = true }
67
+ .invoke(live, reply)
68
+ shadowOf(Looper.getMainLooper()).idle()
69
+ } finally {
70
+ Bridge.removeEventSink(sink)
71
+ live.destroy()
72
+ shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(2))
73
+ }
74
+ return replies
75
+ }
76
+ }
@@ -31,12 +31,11 @@ class NimoCanvasCoordinatorTest {
31
31
  val writes = mutableListOf<ByteArray>()
32
32
  val failures = mutableListOf<String>()
33
33
  val rejections = mutableListOf<Int>()
34
- var holdPreemptions = 0
35
34
  var rejectWrites = false
36
35
  val queue = NimoGattQueue<Any>(clock, { _, bytes -> writes += bytes; !rejectWrites }, { failures += it })
37
36
  val canvas = NimoCanvasCoordinator(clock, { 20 },
38
37
  { frames, started, completed -> queue.enqueue(connection, characteristic, frames, started, completed) },
39
- { queue.reset(); failures += it }, { rejections += it }, { holdPreemptions++ })
38
+ { queue.reset(); failures += it }, { rejections += it })
40
39
  init { queue.connected(connection) }
41
40
  fun written(success: Boolean = true) {
42
41
  queue.written(connection, characteristic, success)
@@ -244,60 +243,6 @@ class NimoCanvasCoordinatorTest {
244
243
  assertTrue(f.failures.isEmpty())
245
244
  }
246
245
 
247
- @Test fun diagnosticHoldWaitsForCurrentBusinessAckAndThenResumesLatestScene() {
248
- val f = Fixture()
249
- val initial = byteArrayOf(0, 0, 1)
250
- val latest = byteArrayOf(2, 0, 1)
251
- f.launch(initial)
252
- var ready = 0
253
- f.canvas.hold { ready++ }
254
- f.canvas.offer(byteArrayOf(1, 0, 1), "one:1")
255
- f.canvas.offer(latest, "one:1")
256
-
257
- assertEquals(0, ready)
258
- f.finishUpdate(initial)
259
- assertEquals(1, ready)
260
- val beforeRelease = f.writes.size
261
-
262
- f.canvas.releaseHold()
263
-
264
- assertEquals(beforeRelease + 1, f.writes.size)
265
- assertArrayEquals(NimoCanvasCodec.frames(4, latest, 20).first(), f.writes.last())
266
- }
267
-
268
- @Test fun scopeTakeoverPreemptsHoldWithoutFlushingHeldScene() {
269
- val f = Fixture()
270
- val initial = byteArrayOf(0, 0, 1)
271
- val held = byteArrayOf(1, 0, 1)
272
- val takeover = byteArrayOf(2, 0, 1)
273
- f.launch(initial); f.finishUpdate(initial)
274
- var ready = 0
275
- f.canvas.hold { ready++ }
276
- f.canvas.offer(held, "one:1")
277
- val beforeTakeover = f.writes.size
278
-
279
- f.canvas.offer(takeover, "two:1")
280
-
281
- assertEquals(1, ready)
282
- assertEquals(1, f.holdPreemptions)
283
- assertEquals(beforeTakeover + 1, f.writes.size)
284
- assertArrayEquals(NimoCanvasCodec.frames(4, takeover, 20).first(), f.writes.last())
285
- }
286
-
287
- @Test fun firstScopePreemptsHoldAcquiredBeforeAnyCanvasScene() {
288
- val f = Fixture()
289
- var ready = 0
290
- f.canvas.readiness(true)
291
- f.canvas.hold { ready++ }
292
- assertEquals(1, ready)
293
-
294
- f.canvas.offer(byteArrayOf(0, 0, 1), "first:1")
295
-
296
- assertEquals(1, f.holdPreemptions)
297
- assertEquals(1, f.writes.size)
298
- assertArrayEquals(NimoCanvasCodec.frames(1, writeCapacity = 20).single(), f.writes.single())
299
- }
300
-
301
246
  @Test fun reentrantCompletionCannotReleaseNewConnectionWrite() {
302
247
  val f = Fixture(); val old = f.connection
303
248
  f.queue.enqueue(old, f.characteristic, listOf(byteArrayOf(1)), completed = {
@@ -314,26 +314,4 @@ class NimoCanvasSessionTest {
314
314
  assertThrows(IllegalArgumentException::class.java) { session.offer(ByteArray(12281), "one:1") }
315
315
  }
316
316
 
317
- @Test fun diagnosticHoldRetainsOnlyLatestSceneUntilReleased() {
318
- val session = NimoCanvasSession()
319
- launched(session); success(session, 4)
320
-
321
- assertTrue(session.hold(true).isEmpty())
322
- assertTrue(session.offer(frameB, "two:1").isEmpty())
323
- assertTrue(session.offer(frameC, "two:2", force = true).isEmpty())
324
-
325
- val update = send(session.hold(false))
326
- assertEquals(4, update.key)
327
- assertArrayEquals(frameC, update.frame)
328
- }
329
-
330
- @Test fun preemptedHoldDoesNotFlushStaleSceneBeforeExit() {
331
- val session = NimoCanvasSession()
332
- launched(session); success(session, 4)
333
- session.hold(true)
334
- session.offer(frameB, "two:1")
335
-
336
- assertTrue(session.hold(false, resume = false).isEmpty())
337
- assertEquals(3, send(session.exit()).key)
338
- }
339
317
  }
@@ -0,0 +1,23 @@
1
+ package com.mentra.bluetoothsdk.utils
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertTrue
6
+ import org.junit.Test
7
+
8
+ class IncidentLogBleRelayNamingTest {
9
+ @Test fun `relay names built for a report are recognized`() {
10
+ val firmware = IncidentLogBleRelayNaming.bleFileBaseName("rep_01M4HBVT88DFSN8TYNZ1WTV4PB", 'B')
11
+ val logcat = IncidentLogBleRelayNaming.bleFileBaseName("rep_01M4HBVT88DFSN8TYNZ1WTV4PB", 'L')
12
+ assertEquals("Brep_01m4hbvt88d", firmware)
13
+ assertTrue(IncidentLogBleRelayNaming.isRelayFileName(firmware))
14
+ assertTrue(IncidentLogBleRelayNaming.isRelayFileName(logcat))
15
+ assertTrue(IncidentLogBleRelayNaming.isRelayFileName(IncidentLogBleRelayNaming.bleFileBaseName("short", 'B')))
16
+ }
17
+
18
+ @Test fun `photo and other transfers are not relays`() {
19
+ for (name in listOf("I000123456", "I000123456.jpg", "ble_1234567890", "Brep_01m4hbvt88d.json", "Xrep_01m4hbvt88d", "", null)) {
20
+ assertFalse(name.toString(), IncidentLogBleRelayNaming.isRelayFileName(name))
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,81 @@
1
+ package com.mentra.bluetoothsdk.stt
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertNull
5
+ import org.junit.Test
6
+
7
+ class BoundedAudioQueueTest {
8
+ private sealed class Item {
9
+ data class Audio(val n: Int) : Item()
10
+ data class Control(val name: String) : Item()
11
+ }
12
+
13
+ private fun queue(capacity: Int) = BoundedAudioQueue<Item>(capacity) { it is Item.Audio }
14
+
15
+ @Test
16
+ fun dropsOnlyTheOldestAudioWhenFull() {
17
+ val q = queue(2)
18
+ q.offer(Item.Audio(1))
19
+ q.offer(Item.Control("finish"))
20
+ q.offer(Item.Audio(2))
21
+ val dropped = q.offer(Item.Audio(3))
22
+
23
+ assertEquals(Item.Audio(1), dropped)
24
+ assertEquals(1L, q.droppedAudio)
25
+ assertEquals(Item.Control("finish"), q.poll(0))
26
+ assertEquals(Item.Audio(2), q.poll(0))
27
+ assertEquals(Item.Audio(3), q.poll(0))
28
+ assertNull(q.poll(0))
29
+ }
30
+
31
+ @Test
32
+ fun neverDropsControlItemsEvenWhenTheWorkerFallsBehind() {
33
+ val q = queue(1)
34
+ repeat(50) { i ->
35
+ q.offer(Item.Control("c$i"))
36
+ q.offer(Item.Audio(i))
37
+ }
38
+ val drained = q.drain()
39
+ assertEquals(50, drained.count { it is Item.Control })
40
+ // Only the newest audio frame fits.
41
+ assertEquals(listOf(Item.Audio(49)), drained.filterIsInstance<Item.Audio>())
42
+ }
43
+
44
+ @Test
45
+ fun keepsControlItemsInOrderAfterEarlierAudio() {
46
+ val q = queue(10)
47
+ q.offer(Item.Audio(1))
48
+ q.offer(Item.Audio(2))
49
+ q.offer(Item.Control("finish"))
50
+ q.offer(Item.Audio(3))
51
+
52
+ assertEquals(
53
+ listOf(Item.Audio(1), Item.Audio(2), Item.Control("finish"), Item.Audio(3)),
54
+ List(4) { q.poll(0) },
55
+ )
56
+ }
57
+
58
+ @Test
59
+ fun clearAudioKeepsControlItems() {
60
+ val q = queue(10)
61
+ q.offer(Item.Audio(1))
62
+ q.offer(Item.Control("reset"))
63
+ q.offer(Item.Audio(2))
64
+
65
+ assertEquals(2, q.clearAudio())
66
+ assertEquals(Item.Control("reset"), q.poll(0))
67
+ assertNull(q.poll(0))
68
+ // Capacity is available again after clearing.
69
+ repeat(10) { q.offer(Item.Audio(it)) }
70
+ assertEquals(0L, q.droppedAudio)
71
+ }
72
+
73
+ @Test
74
+ fun pollTimesOutWhenEmpty() {
75
+ val q = queue(1)
76
+ val started = System.nanoTime()
77
+ assertNull(q.poll(30))
78
+ val elapsedMs = (System.nanoTime() - started) / 1_000_000
79
+ assert(elapsedMs >= 25) { "poll returned after ${elapsedMs}ms" }
80
+ }
81
+ }