@mentra/bluetooth-sdk 3.4.0-dev.999 → 3.4.0

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 (215) hide show
  1. package/MIGRATION.md +16 -7
  2. package/README.md +40 -13
  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 +114 -122
  12. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +2 -2
  13. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
  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 +274 -56
  30. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +5 -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 +3 -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 +2 -2
  93. package/build/generated/changelogCatalog.js +2 -2
  94. package/build/generated/changelogCatalog.js.map +1 -1
  95. package/build/generated/releaseMetadata.js +5 -5
  96. package/build/generated/releaseMetadata.js.map +1 -1
  97. package/build/index.d.ts +1 -1
  98. package/build/index.d.ts.map +1 -1
  99. package/build/index.js +7 -1
  100. package/build/index.js.map +1 -1
  101. package/build/ota-server/MentraOtaServerModule.d.ts +0 -2
  102. package/build/ota-server/MentraOtaServerModule.d.ts.map +1 -1
  103. package/build/ota-server/MentraOtaServerModule.js.map +1 -1
  104. package/build/ota-server/MentraOtaServerModule.web.d.ts +0 -1
  105. package/build/ota-server/MentraOtaServerModule.web.d.ts.map +1 -1
  106. package/build/ota-server/MentraOtaServerModule.web.js +0 -3
  107. package/build/ota-server/MentraOtaServerModule.web.js.map +1 -1
  108. package/build/ota-transport/index.d.ts +3 -3
  109. package/build/ota-transport/index.d.ts.map +1 -1
  110. package/build/ota-transport/index.js +4 -4
  111. package/build/ota-transport/index.js.map +1 -1
  112. package/build/react/useGlassesConnection.d.ts +1 -1
  113. package/build/react/useGlassesConnection.d.ts.map +1 -1
  114. package/build/react/useGlassesConnection.js +3 -3
  115. package/build/react/useGlassesConnection.js.map +1 -1
  116. package/build/react/useMentraBluetooth.d.ts +1 -1
  117. package/build/react/useMentraBluetooth.d.ts.map +1 -1
  118. package/build/react/useMentraBluetooth.js +1 -1
  119. package/build/react/useMentraBluetooth.js.map +1 -1
  120. package/ios/BluetoothSdkModule.swift +73 -22
  121. package/ios/LocalIPv4.swift +0 -61
  122. package/ios/MentraBluetoothSDK.podspec +7 -1
  123. package/ios/MentraOtaServerModule.swift +0 -16
  124. package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
  125. package/ios/Resources/NimoFirmware/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
  126. package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
  127. package/ios/Source/BesCrashReportWaiters.swift +64 -0
  128. package/ios/Source/BluetoothSdkDefaults.swift +4 -4
  129. package/ios/Source/Bridge.swift +14 -0
  130. package/ios/Source/DeviceManager.swift +43 -22
  131. package/ios/Source/GeneratedChangelogCatalog.swift +2 -2
  132. package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
  133. package/ios/Source/MentraBluetoothSDK.swift +35 -16
  134. package/ios/Source/OtaManifest.swift +1 -17
  135. package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
  136. package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
  137. package/ios/Source/audio/SpeechActivity.swift +277 -0
  138. package/ios/Source/events/BluetoothEvents.swift +41 -0
  139. package/ios/Source/internal/BleTraceLogger.swift +14 -1
  140. package/ios/Source/internal/BluetoothAvailability.swift +15 -5
  141. package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +5 -0
  142. package/ios/Source/sgcs/MentraLive.swift +195 -66
  143. package/ios/Source/sgcs/Nimo.swift +249 -276
  144. package/ios/Source/sgcs/NimoCanvasCoordinator.swift +9 -38
  145. package/ios/Source/sgcs/NimoCanvasSession.swift +0 -1
  146. package/ios/Source/sgcs/NimoFirmwareUpdater.swift +260 -0
  147. package/ios/Source/sgcs/NimoOtaProtocol.swift +159 -0
  148. package/ios/Source/sgcs/SGCManager.swift +6 -0
  149. package/ios/Source/streaming/StreamModels.swift +15 -6
  150. package/ios/Source/stt/STTTools.swift +13 -31
  151. package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
  152. package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
  153. package/ios/Source/stt/WakeWordDetector.swift +145 -0
  154. package/ios/Source/utils/BoundedWait.swift +44 -0
  155. package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
  156. package/ios/Source/utils/SttModelTraits.swift +62 -0
  157. package/ios/Tests/BesCrashReportTests.swift +142 -0
  158. package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
  159. package/ios/Tests/DeviceManagerReconnectTests.swift +0 -71
  160. package/ios/Tests/GlassesAudioSetupPolicyTests.swift +9 -3
  161. package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
  162. package/ios/Tests/MentraLiveAudioSetupTests.swift +6 -12
  163. package/ios/Tests/MentraLiveCommandQueueTests.swift +35 -0
  164. package/ios/Tests/MentraLiveSessionReadinessTests.swift +179 -10
  165. package/ios/Tests/NimoCanvasTransportTests.swift +0 -89
  166. package/ios/Tests/NimoOtaProtocolTests.swift +78 -0
  167. package/ios/Tests/PcmStreamTests.swift +299 -0
  168. package/ios/Tests/SpeechActivityTests.swift +238 -0
  169. package/ios/Tests/StreamRequestTests.swift +18 -0
  170. package/ios/Tests/SttModelTraitsTests.swift +62 -0
  171. package/package.json +2 -2
  172. package/plugin/build/withAndroid.js +9 -11
  173. package/plugin/build/withKotlinVersion.d.ts +2 -0
  174. package/plugin/build/withKotlinVersion.js +46 -0
  175. package/src/BluetoothSdk.types.ts +107 -5
  176. package/src/_private/BluetoothSdkModule.ts +45 -8
  177. package/src/_private/MentraLocalNetworkModule.ts +5 -1
  178. package/src/generated/changelogCatalog.ts +2 -2
  179. package/src/generated/releaseMetadata.ts +5 -5
  180. package/src/index.ts +13 -1
  181. package/src/ota-server/MentraOtaServerModule.ts +0 -2
  182. package/src/ota-server/MentraOtaServerModule.web.ts +0 -4
  183. package/src/ota-transport/index.ts +4 -5
  184. package/src/react/useGlassesConnection.ts +4 -4
  185. package/src/react/useMentraBluetooth.ts +2 -2
  186. package/android/silero/LICENSE.md +0 -17
  187. package/android/silero/build.gradle +0 -50
  188. package/android/silero/proguard-rules.pro +0 -24
  189. package/android/silero/src/androidTest/assets/hello.wav +0 -0
  190. package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
  191. package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
  192. package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
  193. package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
  194. package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
  195. package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
  196. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
  197. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
  198. package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
  199. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
  200. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
  201. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -274
  202. package/android/src/main/java/com/mentra/bluetoothsdk/ScopedNetworkReadiness.kt +0 -17
  203. package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
  204. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
  205. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -16
  206. package/android/src/test/java/com/mentra/bluetoothsdk/ScopedNetworkReadinessTest.kt +0 -39
  207. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
  208. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
  209. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -851
  210. package/ios/Source/sgcs/NimoMacResourceTransport.swift +0 -217
  211. package/ios/Source/sgcs/NimoResourceFixture.swift +0 -384
  212. package/ios/Source/sgcs/NimoResourceSession.swift +0 -406
  213. package/ios/Tests/NimoResourceSessionTests.swift +0 -476
  214. /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
  215. /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
@@ -0,0 +1,103 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ /**
4
+ * Decides when a joined scoped network is actually ready for ICE to gather on.
5
+ *
6
+ * `onAvailable` is not that moment. Android's own guidance is to wait for `onLinkPropertiesChanged`
7
+ * and `onCapabilitiesChanged` rather than reading properties synchronously inside `onAvailable`, and
8
+ * on device the consequence was concrete: the hotspot address can land in the kernel's interface
9
+ * table *after* `onAvailable` returns, and libwebrtc gathers from that table. Starting ingest on
10
+ * `onAvailable` therefore raced the address into existence, and losing that race is indistinguishable
11
+ * from the wiring bugs it was mistaken for.
12
+ *
13
+ * Readiness needs agreement from three places, so all three are recorded separately and the missing
14
+ * one is named. The kernel table is the odd one out: it has no callback, so the shell polls it and
15
+ * feeds the answer in through [onInterfaceTable].
16
+ *
17
+ * Pure and mutable-but-self-contained, so the awkward orderings are unit testable without a device.
18
+ */
19
+ class ScopedNetworkReadiness {
20
+
21
+ /** What each source has reported so far. Null means "not yet", not "absent". */
22
+ data class Observations(
23
+ val available: Boolean = false,
24
+ val capabilities: Boolean = false,
25
+ val linkInterface: String? = null,
26
+ val linkAddress: String? = null,
27
+ val tableOwner: String? = null,
28
+ )
29
+
30
+ sealed interface Verdict {
31
+ /**
32
+ * [owner] is the interface the kernel says holds [address]. It can differ from
33
+ * [interfaceName], which is what `LinkProperties` claimed — a disagreement worth logging,
34
+ * not worth blocking on, since ICE only ever uses the address.
35
+ */
36
+ data class Ready(
37
+ val address: String,
38
+ val interfaceName: String?,
39
+ val owner: String,
40
+ ) : Verdict
41
+
42
+ /** [missing] names the one source still to report, for the timeout message. */
43
+ data class Waiting(val missing: String) : Verdict
44
+ }
45
+
46
+ @Volatile
47
+ var observations = Observations()
48
+ private set
49
+
50
+ private val lock = Any()
51
+
52
+ fun onAvailable() = update { it.copy(available = true) }
53
+
54
+ fun onCapabilities() = update { it.copy(capabilities = true) }
55
+
56
+ /**
57
+ * Taken verbatim: `LinkProperties` carries the network's whole current state, so a later
58
+ * callback without an address means the address is gone, not that the old one still holds.
59
+ */
60
+ fun onLinkProperties(interfaceName: String?, address: String?) =
61
+ update { it.copy(linkInterface = interfaceName, linkAddress = address) }
62
+
63
+ /** Seed from the synchronous read in `onAvailable`; never clears what a callback reported. */
64
+ fun seedLinkProperties(interfaceName: String?, address: String?) =
65
+ update {
66
+ if (address == null) it else it.copy(linkInterface = interfaceName, linkAddress = address)
67
+ }
68
+
69
+ fun onInterfaceTable(owner: String?) = update { it.copy(tableOwner = owner) }
70
+
71
+ fun reset() = update { Observations() }
72
+
73
+ fun verdict(): Verdict = verdictOf(observations)
74
+
75
+ /** True once everything with a callback has reported, so only the table poll is left. */
76
+ fun callbacksSatisfied(): Boolean = callbacksSatisfiedBy(observations)
77
+
78
+ private inline fun update(transform: (Observations) -> Observations) {
79
+ synchronized(lock) { observations = transform(observations) }
80
+ }
81
+
82
+ companion object {
83
+ fun callbacksSatisfiedBy(observations: Observations): Boolean =
84
+ observations.available && observations.capabilities && observations.linkAddress != null
85
+
86
+ /** Ordered so the named blocker is the earliest unmet one, which is the useful one. */
87
+ fun verdictOf(observations: Observations): Verdict =
88
+ when {
89
+ !observations.available -> Verdict.Waiting("onAvailable")
90
+ !observations.capabilities -> Verdict.Waiting("onCapabilitiesChanged")
91
+ observations.linkAddress == null ->
92
+ Verdict.Waiting("an IPv4 address from onLinkPropertiesChanged")
93
+ observations.tableOwner == null ->
94
+ Verdict.Waiting("the kernel interface table to carry ${observations.linkAddress}")
95
+ else ->
96
+ Verdict.Ready(
97
+ observations.linkAddress,
98
+ observations.linkInterface,
99
+ observations.tableOwner,
100
+ )
101
+ }
102
+ }
103
+ }
@@ -0,0 +1,44 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import android.net.NetworkCapabilities
4
+
5
+ /**
6
+ * Declarative description of the SoftAP network request.
7
+ *
8
+ * `NetworkRequest.Builder` cannot be constructed in a plain JVM test, so the shape of the request
9
+ * lives here as data and [ScopedSoftApNetwork] translates it into the real builder. That makes the
10
+ * one property this architecture depends on — that the request does **not** ask for
11
+ * `NET_CAPABILITY_INTERNET` — assertable without a device.
12
+ *
13
+ * Dropping the internet capability is what keeps the phone dual-homed: Android will not promote the
14
+ * glasses hotspot to the default network, so ACS traffic stays on cellular while media crosses the
15
+ * local link. The other half of that guarantee is the MTK firmware patch that stops the AP
16
+ * advertising a default route.
17
+ */
18
+ data class ScopedNetworkRequestSpec(
19
+ val ssid: String,
20
+ val passphrase: String,
21
+ val transportTypes: Set<Int>,
22
+ val removedCapabilities: Set<Int>,
23
+ ) {
24
+ /** True when the request cannot be satisfied by, or promoted to, an internet-bearing network. */
25
+ val avoidsInternetCapability: Boolean
26
+ get() = removedCapabilities.contains(NetworkCapabilities.NET_CAPABILITY_INTERNET)
27
+
28
+ val hasPassphrase: Boolean
29
+ get() = passphrase.isNotEmpty()
30
+
31
+ companion object {
32
+ /** The canonical SoftAP request. Every caller should go through this. */
33
+ fun forSoftAp(
34
+ ssid: String,
35
+ passphrase: String,
36
+ ): ScopedNetworkRequestSpec =
37
+ ScopedNetworkRequestSpec(
38
+ ssid = ssid,
39
+ passphrase = passphrase,
40
+ transportTypes = setOf(NetworkCapabilities.TRANSPORT_WIFI),
41
+ removedCapabilities = setOf(NetworkCapabilities.NET_CAPABILITY_INTERNET),
42
+ )
43
+ }
44
+ }
@@ -0,0 +1,117 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ /**
4
+ * Pure lifecycle state machine for a scoped SoftAP network join.
5
+ *
6
+ * Split from the Android shell so the awkward parts — duplicate callbacks, callbacks arriving after
7
+ * a cancel racing a late `onAvailable` — are unit testable without a device. The shell
8
+ * ([ScopedSoftApNetwork]) owns `ConnectivityManager` and forwards raw callbacks in here.
9
+ *
10
+ * `ConnectivityManager` gives no ordering or at-most-once guarantee: `onAvailable` can fire more
11
+ * than once, `onCapabilitiesChanged` fires repeatedly, and callbacks from a request that was already
12
+ * unregistered can still land. Every transition below is therefore idempotent and generation-guarded.
13
+ */
14
+ class ScopedNetworkState {
15
+
16
+ enum class Phase {
17
+ /** No request outstanding. */
18
+ IDLE,
19
+
20
+ /** `requestNetwork` issued, waiting for a usable network. */
21
+ REQUESTING,
22
+
23
+ /** Network joined and usable. */
24
+ AVAILABLE,
25
+
26
+ /** Terminal for this generation: released, lost, or failed. */
27
+ LOST,
28
+ }
29
+
30
+ /** Why a join ended without producing a usable network. */
31
+ enum class Failure {
32
+ NONE,
33
+ UNAVAILABLE,
34
+ LOST,
35
+ PERMISSION_DENIED,
36
+ REQUEST_FAILED,
37
+ }
38
+
39
+ var phase: Phase = Phase.IDLE
40
+ private set
41
+
42
+ var failure: Failure = Failure.NONE
43
+ private set
44
+
45
+ /** Incremented on every new request so late callbacks from an old one can be rejected. */
46
+ var generation: Int = 0
47
+ private set
48
+
49
+ val isActive: Boolean
50
+ get() = phase == Phase.REQUESTING || phase == Phase.AVAILABLE
51
+
52
+ /**
53
+ * Begin a new join. Any in-flight generation is abandoned, so its callbacks become stale.
54
+ *
55
+ * @return the generation for this request; pass it back to every callback
56
+ */
57
+ fun startRequest(): Int {
58
+ generation += 1
59
+ phase = Phase.REQUESTING
60
+ failure = Failure.NONE
61
+ return generation
62
+ }
63
+
64
+ /** True when a callback belongs to the current, still-active generation. */
65
+ fun accepts(callbackGeneration: Int): Boolean = callbackGeneration == generation && isActive
66
+
67
+ /**
68
+ * A usable network arrived.
69
+ *
70
+ * @return true if this transitioned the machine; false when stale or already available
71
+ */
72
+ fun onAvailable(callbackGeneration: Int): Boolean {
73
+ if (!accepts(callbackGeneration)) return false
74
+ if (phase == Phase.AVAILABLE) return false // duplicate onAvailable
75
+ phase = Phase.AVAILABLE
76
+ return true
77
+ }
78
+
79
+ /** The framework reported the network cannot be provided. */
80
+ fun onUnavailable(callbackGeneration: Int): Boolean {
81
+ if (!accepts(callbackGeneration)) return false
82
+ phase = Phase.LOST
83
+ failure = Failure.UNAVAILABLE
84
+ return true
85
+ }
86
+
87
+ /** The joined network went away. This is the one the call path must react to. */
88
+ fun onLost(callbackGeneration: Int): Boolean {
89
+ if (!accepts(callbackGeneration)) return false
90
+ phase = Phase.LOST
91
+ failure = Failure.LOST
92
+ return true
93
+ }
94
+
95
+ /** `requestNetwork` threw, or the local-network permission was denied. */
96
+ fun onRequestFailed(callbackGeneration: Int, permissionDenied: Boolean): Boolean {
97
+ if (callbackGeneration != generation) return false
98
+ if (!isActive) return false
99
+ phase = Phase.LOST
100
+ failure = if (permissionDenied) Failure.PERMISSION_DENIED else Failure.REQUEST_FAILED
101
+ return true
102
+ }
103
+
104
+ /** Caller-initiated teardown. Not a failure. */
105
+ fun release(): Boolean {
106
+ if (!isActive) return false
107
+ phase = Phase.LOST
108
+ failure = Failure.NONE
109
+ return true
110
+ }
111
+
112
+ /** Return to IDLE so the same instance can be reused for the next call. */
113
+ fun reset() {
114
+ phase = Phase.IDLE
115
+ failure = Failure.NONE
116
+ }
117
+ }