@mentra/bluetooth-sdk 3.4.0-dev.998 → 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.
- package/MIGRATION.md +16 -7
- package/README.md +40 -13
- package/android/build.gradle +60 -51
- package/android/gradle.properties +3 -0
- package/android/settings.gradle +47 -3
- package/android/src/main/assets/nimo/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +77 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +41 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +114 -122
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +2 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +31 -10
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +57 -196
- package/android/src/main/java/com/mentra/bluetoothsdk/OtaManifest.kt +1 -16
- package/android/src/main/java/com/mentra/bluetoothsdk/debug/BleTraceLogger.kt +10 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/Ipv4Prefix.kt +51 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/NetworkFacts.kt +81 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkError.kt +108 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadiness.kt +103 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpec.kt +44 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkState.kt +117 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedSoftApNetwork.kt +598 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/SoftApTrace.kt +143 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/otaserver/MentraOtaServerModule.kt +0 -7
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/ClassicAudioConnectionTracker.kt +15 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +274 -56
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +5 -5
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +210 -325
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +0 -35
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +2 -10
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdater.kt +213 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocol.kt +112 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/streaming/StreamModels.kt +17 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +2 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/StreamRequestCaptureAudioTest.kt +23 -4
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/Ipv4PrefixTest.kt +59 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/NetworkFactsTest.kt +59 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedLocalNetworkPermissionTest.kt +35 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkErrorTest.kt +85 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkLifecycleTest.kt +164 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadinessTest.kt +141 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpecTest.kt +56 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/SoftApTraceTest.kt +97 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/UnavailableRetryTest.kt +47 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/ClassicAudioConnectionTrackerTest.kt +26 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +141 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +3 -3
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveRgbLedResponseTest.kt +76 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareTargetTest.kt +15 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdaterTest.kt +76 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocolTest.kt +77 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoReadinessDriverTest.kt +31 -1
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
- package/build/BluetoothSdk.types.d.ts +103 -5
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +42 -8
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js +1 -0
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/_private/MentraLocalNetworkModule.d.ts +1 -1
- package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
- package/build/_private/MentraLocalNetworkModule.js.map +1 -1
- package/build/generated/changelogCatalog.d.ts +2 -2
- package/build/generated/changelogCatalog.js +2 -2
- package/build/generated/changelogCatalog.js.map +1 -1
- package/build/generated/releaseMetadata.js +5 -5
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -1
- package/build/index.js.map +1 -1
- package/build/ota-server/MentraOtaServerModule.d.ts +0 -2
- package/build/ota-server/MentraOtaServerModule.d.ts.map +1 -1
- package/build/ota-server/MentraOtaServerModule.js.map +1 -1
- package/build/ota-server/MentraOtaServerModule.web.d.ts +0 -1
- package/build/ota-server/MentraOtaServerModule.web.d.ts.map +1 -1
- package/build/ota-server/MentraOtaServerModule.web.js +0 -3
- package/build/ota-server/MentraOtaServerModule.web.js.map +1 -1
- package/build/ota-transport/index.d.ts +3 -3
- package/build/ota-transport/index.d.ts.map +1 -1
- package/build/ota-transport/index.js +4 -4
- package/build/ota-transport/index.js.map +1 -1
- package/build/react/useGlassesConnection.d.ts +1 -1
- package/build/react/useGlassesConnection.d.ts.map +1 -1
- package/build/react/useGlassesConnection.js +3 -3
- package/build/react/useGlassesConnection.js.map +1 -1
- package/build/react/useMentraBluetooth.d.ts +1 -1
- package/build/react/useMentraBluetooth.d.ts.map +1 -1
- package/build/react/useMentraBluetooth.js +1 -1
- package/build/react/useMentraBluetooth.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +73 -22
- package/ios/LocalIPv4.swift +0 -61
- package/ios/MentraBluetoothSDK.podspec +7 -1
- package/ios/MentraOtaServerModule.swift +0 -16
- package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
- package/ios/Resources/NimoFirmware/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
- package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
- package/ios/Source/BesCrashReportWaiters.swift +64 -0
- package/ios/Source/BluetoothSdkDefaults.swift +4 -4
- package/ios/Source/Bridge.swift +14 -0
- package/ios/Source/DeviceManager.swift +43 -22
- package/ios/Source/GeneratedChangelogCatalog.swift +2 -2
- package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
- package/ios/Source/MentraBluetoothSDK.swift +35 -16
- package/ios/Source/OtaManifest.swift +1 -17
- package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
- package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
- package/ios/Source/audio/SpeechActivity.swift +277 -0
- package/ios/Source/events/BluetoothEvents.swift +41 -0
- package/ios/Source/internal/BleTraceLogger.swift +14 -1
- package/ios/Source/internal/BluetoothAvailability.swift +15 -5
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +5 -0
- package/ios/Source/sgcs/MentraLive.swift +195 -66
- package/ios/Source/sgcs/Nimo.swift +249 -276
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +9 -38
- package/ios/Source/sgcs/NimoCanvasSession.swift +0 -1
- package/ios/Source/sgcs/NimoFirmwareUpdater.swift +260 -0
- package/ios/Source/sgcs/NimoOtaProtocol.swift +159 -0
- package/ios/Source/sgcs/SGCManager.swift +6 -0
- package/ios/Source/streaming/StreamModels.swift +15 -6
- package/ios/Source/stt/STTTools.swift +13 -31
- package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
- package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
- package/ios/Source/stt/WakeWordDetector.swift +145 -0
- package/ios/Source/utils/BoundedWait.swift +44 -0
- package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
- package/ios/Source/utils/SttModelTraits.swift +62 -0
- package/ios/Tests/BesCrashReportTests.swift +142 -0
- package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
- package/ios/Tests/DeviceManagerReconnectTests.swift +0 -71
- package/ios/Tests/GlassesAudioSetupPolicyTests.swift +9 -3
- package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
- package/ios/Tests/MentraLiveAudioSetupTests.swift +6 -12
- package/ios/Tests/MentraLiveCommandQueueTests.swift +35 -0
- package/ios/Tests/MentraLiveSessionReadinessTests.swift +179 -10
- package/ios/Tests/NimoCanvasTransportTests.swift +0 -89
- package/ios/Tests/NimoOtaProtocolTests.swift +78 -0
- package/ios/Tests/PcmStreamTests.swift +299 -0
- package/ios/Tests/SpeechActivityTests.swift +238 -0
- package/ios/Tests/StreamRequestTests.swift +18 -0
- package/ios/Tests/SttModelTraitsTests.swift +62 -0
- package/package.json +2 -2
- package/plugin/build/withAndroid.js +9 -11
- package/plugin/build/withKotlinVersion.d.ts +2 -0
- package/plugin/build/withKotlinVersion.js +46 -0
- package/src/BluetoothSdk.types.ts +107 -5
- package/src/_private/BluetoothSdkModule.ts +45 -8
- package/src/_private/MentraLocalNetworkModule.ts +5 -1
- package/src/generated/changelogCatalog.ts +2 -2
- package/src/generated/releaseMetadata.ts +5 -5
- package/src/index.ts +13 -1
- package/src/ota-server/MentraOtaServerModule.ts +0 -2
- package/src/ota-server/MentraOtaServerModule.web.ts +0 -4
- package/src/ota-transport/index.ts +4 -5
- package/src/react/useGlassesConnection.ts +4 -4
- package/src/react/useMentraBluetooth.ts +2 -2
- package/android/silero/LICENSE.md +0 -17
- package/android/silero/build.gradle +0 -50
- package/android/silero/proguard-rules.pro +0 -24
- package/android/silero/src/androidTest/assets/hello.wav +0 -0
- package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
- package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
- package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
- package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -274
- package/android/src/main/java/com/mentra/bluetoothsdk/ScopedNetworkReadiness.kt +0 -17
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
- package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
- package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -16
- package/android/src/test/java/com/mentra/bluetoothsdk/ScopedNetworkReadinessTest.kt +0 -39
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -851
- package/ios/Source/sgcs/NimoMacResourceTransport.swift +0 -217
- package/ios/Source/sgcs/NimoResourceFixture.swift +0 -384
- package/ios/Source/sgcs/NimoResourceSession.swift +0 -406
- package/ios/Tests/NimoResourceSessionTests.swift +0 -476
- /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
- /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
|
@@ -7,49 +7,40 @@ final class NimoCanvasCoordinator {
|
|
|
7
7
|
private final class Flight {
|
|
8
8
|
let key: Int
|
|
9
9
|
var finalStarted = false
|
|
10
|
-
var finalStartedElapsedMs: UInt64?
|
|
11
10
|
var complete = false
|
|
12
|
-
var earlyAck:
|
|
11
|
+
var earlyAck: Data?
|
|
13
12
|
init(key: Int) {
|
|
14
13
|
self.key = key
|
|
15
14
|
}
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
private let continuousNow: (() -> UInt64)?
|
|
19
17
|
private let schedule: NimoScheduler
|
|
20
18
|
private let writeCapacity: () -> Int
|
|
21
19
|
private let enqueue: ([Data], @escaping () -> Void, @escaping () -> Void) -> Bool
|
|
22
20
|
private let reconnect: (String) -> Void
|
|
23
21
|
private let rejected: (Int) -> Void
|
|
24
|
-
private let result: (Int, Int) -> Void
|
|
25
|
-
private let observedResult: (Int, Int, UInt64?) -> Void
|
|
26
22
|
private let session = NimoCanvasSession()
|
|
27
23
|
private var flight: Flight?
|
|
28
|
-
private var releaseDraining = false
|
|
29
24
|
private var cancelRetry: (() -> Void)?
|
|
30
25
|
private var cancelDeadline: (() -> Void)?
|
|
31
26
|
|
|
32
27
|
init(schedule: @escaping NimoScheduler, writeCapacity: @escaping () -> Int,
|
|
33
28
|
enqueue: @escaping ([Data], @escaping () -> Void, @escaping () -> Void) -> Bool,
|
|
34
|
-
reconnect: @escaping (String) -> Void, rejected: @escaping (Int) -> Void
|
|
29
|
+
reconnect: @escaping (String) -> Void, rejected: @escaping (Int) -> Void)
|
|
35
30
|
{
|
|
36
|
-
self.continuousNow = continuousNow
|
|
37
31
|
self.schedule = schedule; self.writeCapacity = writeCapacity; self.enqueue = enqueue
|
|
38
|
-
self.reconnect = reconnect; self.rejected = rejected
|
|
32
|
+
self.reconnect = reconnect; self.rejected = rejected
|
|
39
33
|
}
|
|
40
34
|
|
|
41
35
|
func offer(_ bytes: Data, scope: String, force: Bool = false) {
|
|
42
|
-
guard !releaseDraining else { return }
|
|
43
36
|
run(session.offer(bytes, scope: scope, force: force))
|
|
44
37
|
}
|
|
45
38
|
|
|
46
39
|
func activate() {
|
|
47
|
-
guard !releaseDraining else { return }
|
|
48
40
|
run(session.activate())
|
|
49
41
|
}
|
|
50
42
|
|
|
51
43
|
func readiness(_ value: Bool, confirmed: Bool = false) {
|
|
52
|
-
guard !releaseDraining else { return }
|
|
53
44
|
if !value { cancelRetry?(); cancelRetry = nil }
|
|
54
45
|
run(session.readiness(value, confirmed: confirmed))
|
|
55
46
|
}
|
|
@@ -59,20 +50,7 @@ final class NimoCanvasCoordinator {
|
|
|
59
50
|
run(session.exit())
|
|
60
51
|
}
|
|
61
52
|
|
|
62
|
-
/// Fence new scenes/retries while retaining only the already-issued command
|
|
63
|
-
/// and its exact ACK, followed by the final Exit through the same queue.
|
|
64
|
-
func drainForRelease() {
|
|
65
|
-
releaseDraining = true
|
|
66
|
-
cancelRetry?(); cancelRetry = nil
|
|
67
|
-
run(session.exit())
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
func acceptsReleaseResponse(key: Int, payload: Data) -> Bool {
|
|
71
|
-
releaseDraining && flight?.key == key && session.acceptsResponse(key: key, payload: payload)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
53
|
func nativeApp(_ appId: Int, entered: Bool) {
|
|
75
|
-
guard !releaseDraining else { return }
|
|
76
54
|
run(session.nativeApp(appId, entered: entered))
|
|
77
55
|
}
|
|
78
56
|
|
|
@@ -83,24 +61,17 @@ final class NimoCanvasCoordinator {
|
|
|
83
61
|
session.disconnected()
|
|
84
62
|
}
|
|
85
63
|
|
|
86
|
-
func response(key: Int, payload: Data
|
|
64
|
+
func response(key: Int, payload: Data) {
|
|
87
65
|
guard let current = flight, session.acceptsResponse(key: key, payload: payload) else { return }
|
|
88
|
-
if let receivedElapsedMs, let started = current.finalStartedElapsedMs, receivedElapsedMs < started { return }
|
|
89
66
|
if !current.complete {
|
|
90
|
-
if current.finalStarted, current.earlyAck == nil { current.earlyAck =
|
|
67
|
+
if current.finalStarted, current.earlyAck == nil { current.earlyAck = payload }
|
|
91
68
|
return
|
|
92
69
|
}
|
|
93
70
|
cancelDeadline?(); cancelDeadline = nil
|
|
94
71
|
flight = nil
|
|
95
|
-
|
|
96
|
-
result(key, Int(payload.first ?? 255))
|
|
97
|
-
observedResult(key, Int(payload.first ?? 255), receivedElapsedMs)
|
|
98
|
-
run(actions)
|
|
72
|
+
run(session.response(key: key, payload: payload))
|
|
99
73
|
}
|
|
100
74
|
|
|
101
|
-
var pendingCount: Int { (flight == nil ? 0 : 1) + (cancelRetry == nil ? 0 : 1) }
|
|
102
|
-
var mayHaveEntered: Bool { session.mayHaveEntered }
|
|
103
|
-
|
|
104
75
|
private func run(_ actions: [NimoCanvasSession.Action]) {
|
|
105
76
|
for action in actions {
|
|
106
77
|
switch action {
|
|
@@ -116,11 +87,11 @@ final class NimoCanvasCoordinator {
|
|
|
116
87
|
do {
|
|
117
88
|
let frames = try NimoCanvasCodec.frames(key: key, content: frame, writeCapacity: writeCapacity())
|
|
118
89
|
let queued = enqueue(frames, { [weak self] in
|
|
119
|
-
if
|
|
90
|
+
if self?.flight === current { current.finalStarted = true }
|
|
120
91
|
}, { [weak self] in
|
|
121
92
|
guard let self, self.flight === current else { return }
|
|
122
93
|
current.complete = true
|
|
123
|
-
if let ack = current.earlyAck { self.response(key: key, payload: ack
|
|
94
|
+
if let ack = current.earlyAck { self.response(key: key, payload: ack) }
|
|
124
95
|
})
|
|
125
96
|
if !queued, flight === current { disconnected(); reconnect("Canvas transport unavailable") }
|
|
126
97
|
} catch {
|
|
@@ -129,7 +100,7 @@ final class NimoCanvasCoordinator {
|
|
|
129
100
|
case let .reconnect(reason): disconnected(); reconnect(reason)
|
|
130
101
|
case let .rejected(status):
|
|
131
102
|
rejected(status)
|
|
132
|
-
if status == 7
|
|
103
|
+
if status == 7 {
|
|
133
104
|
cancelRetry?()
|
|
134
105
|
cancelRetry = schedule(1) { [weak self] in
|
|
135
106
|
guard let self else { return }
|
|
@@ -32,7 +32,6 @@ final class NimoCanvasSession {
|
|
|
32
32
|
private var sentRevision: UInt64 = 0
|
|
33
33
|
private var ticket: UInt64 = 0
|
|
34
34
|
private var flight: Flight?
|
|
35
|
-
var mayHaveEntered: Bool { active || flight != nil }
|
|
36
35
|
|
|
37
36
|
func offer(_ frame: Data, scope: String, force: Bool = false) -> [Action] {
|
|
38
37
|
if self.scope != scope || force { forceRevision &+= 1 }
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import CryptoKit
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
/// The one NIMO firmware the app supports: the vendor's 537cf1 dynamic-v1 build.
|
|
5
|
+
enum NimoFirmwareTarget {
|
|
6
|
+
static let packedVersion = "0.1.1.1"
|
|
7
|
+
static let buildTag = "537cf1"
|
|
8
|
+
static let sha256 = "5e23c574e3dfd8c27172f4f9a33475e6efd0d33e7230bfcce8f4fe8ca59fe433"
|
|
9
|
+
static let resourceBundle = "MentraNimoFirmware"
|
|
10
|
+
static let resourceName = "nimo-firmware-0.1.1.1-537cf1"
|
|
11
|
+
static let updatingText = "Updating your NIMO glasses.\n\nKeep the Mentra app open."
|
|
12
|
+
|
|
13
|
+
/// A missing detail string (older firmware) is judged on the packed version alone.
|
|
14
|
+
static func isCurrent(packedVersion: String, detail: String) -> Bool {
|
|
15
|
+
packedVersion == self.packedVersion && (detail.isEmpty || detail.contains("-\(buildTag)-"))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static func hash(_ data: Data) -> String {
|
|
19
|
+
SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// The bundled image, only if it is exactly the expected file.
|
|
23
|
+
static func load() -> Data? {
|
|
24
|
+
let candidates = [Bundle(for: Nimo.self), Bundle.main]
|
|
25
|
+
for bundle in candidates {
|
|
26
|
+
let resources = bundle.url(forResource: resourceBundle, withExtension: "bundle").flatMap(Bundle.init(url:)) ?? bundle
|
|
27
|
+
guard let url = resources.url(forResource: resourceName, withExtension: "bin"),
|
|
28
|
+
let data = try? Data(contentsOf: url) else { continue }
|
|
29
|
+
if hash(data) == sha256 { return data }
|
|
30
|
+
Bridge.log("NIMO: bundled firmware hash mismatch")
|
|
31
|
+
return nil
|
|
32
|
+
}
|
|
33
|
+
Bridge.log("NIMO: bundled firmware unavailable")
|
|
34
|
+
return nil
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// Single-attempt NIMO OTA over the 2001/2002 channel. Main-thread confined; `schedule` posts there.
|
|
39
|
+
/// `finished` gets nil once the reboot command was sent, otherwise the failure reason.
|
|
40
|
+
/// No failure path sends reset; the next connection decides again from the reported version.
|
|
41
|
+
final class NimoFirmwareUpdater {
|
|
42
|
+
private struct Failure: Error, LocalizedError {
|
|
43
|
+
let message: String
|
|
44
|
+
var errorDescription: String? { message }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private final class Pending {
|
|
48
|
+
let command: UInt8
|
|
49
|
+
let sequence: UInt8
|
|
50
|
+
let callback: (Result<Data, Error>) -> Void
|
|
51
|
+
init(_ command: UInt8, _ sequence: UInt8, _ callback: @escaping (Result<Data, Error>) -> Void) {
|
|
52
|
+
self.command = command; self.sequence = sequence; self.callback = callback
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private let firmware: Data
|
|
57
|
+
private let capacity: Int
|
|
58
|
+
private let schedule: NimoScheduler
|
|
59
|
+
private let write: (Data, @escaping (Bool) -> Void) -> Void
|
|
60
|
+
private let progress: (Int) -> Void
|
|
61
|
+
private let finished: (String?) -> Void
|
|
62
|
+
private let decoder = NimoOtaProtocol.Decoder()
|
|
63
|
+
private var sequence: UInt8 = 0
|
|
64
|
+
private var pending: Pending?
|
|
65
|
+
private var cancelTimer: (() -> Void)?
|
|
66
|
+
private var cancelDelay: (() -> Void)?
|
|
67
|
+
private var done = false
|
|
68
|
+
private var crc = false
|
|
69
|
+
private var covered: [Bool]
|
|
70
|
+
private var coveredCount = 0
|
|
71
|
+
private var lastPercent = -1
|
|
72
|
+
private var stalls = 0
|
|
73
|
+
private var transferStarted = Date()
|
|
74
|
+
private var syncPending = 0
|
|
75
|
+
private var syncFailures = 0
|
|
76
|
+
|
|
77
|
+
init(firmware: Data, writeCapacity: Int, schedule: @escaping NimoScheduler,
|
|
78
|
+
write: @escaping (Data, @escaping (Bool) -> Void) -> Void,
|
|
79
|
+
progress: @escaping (Int) -> Void, finished: @escaping (String?) -> Void)
|
|
80
|
+
{
|
|
81
|
+
self.firmware = Data(firmware)
|
|
82
|
+
capacity = min(512, writeCapacity)
|
|
83
|
+
self.schedule = schedule
|
|
84
|
+
self.write = write
|
|
85
|
+
self.progress = progress
|
|
86
|
+
self.finished = finished
|
|
87
|
+
covered = Array(repeating: false, count: firmware.count)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
func start() {
|
|
91
|
+
exchange(NimoOtaProtocol.info, Data(repeating: 0xFF, count: 4)) { [self] info in
|
|
92
|
+
try checkDeviceInfo(info)
|
|
93
|
+
exchange(NimoOtaProtocol.fileOffsetCommand) { [self] body in
|
|
94
|
+
let header = try NimoOtaProtocol.firmwareSlice(firmware, NimoOtaProtocol.fileOffset(body))
|
|
95
|
+
exchange(NimoOtaProtocol.canUpdate, header) { [self] result in
|
|
96
|
+
// 0 = can update, 3 = same image already staged; anything else is a refusal.
|
|
97
|
+
guard result == Data([0]) || result == Data([3]) else {
|
|
98
|
+
throw Failure(message: "Glasses refused the firmware (battery or prerequisites)")
|
|
99
|
+
}
|
|
100
|
+
exchange(NimoOtaProtocol.enter) { [self] entryBody in
|
|
101
|
+
let entry = try NimoOtaProtocol.enterResult(entryBody)
|
|
102
|
+
crc = entry.crc
|
|
103
|
+
transferStarted = Date()
|
|
104
|
+
try sendBlock(entry.slice)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
func receive(_ data: Data) {
|
|
112
|
+
guard !done else { return }
|
|
113
|
+
do {
|
|
114
|
+
for reply in try decoder.feed(data) {
|
|
115
|
+
guard let expected = pending, expected.command == reply.command, expected.sequence == reply.sequence else { continue }
|
|
116
|
+
pending = nil
|
|
117
|
+
cancelTimer?(); cancelTimer = nil
|
|
118
|
+
if reply.status == 0 { expected.callback(.success(reply.body)) } else {
|
|
119
|
+
expected.callback(.failure(Failure(message: "OTA command \(reply.command) returned status \(reply.status)")))
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} catch { fail(error) }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/// The connection went away; stop without reporting.
|
|
126
|
+
func cancel() {
|
|
127
|
+
done = true
|
|
128
|
+
clearTimers()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private func checkDeviceInfo(_ body: Data) throws {
|
|
132
|
+
let fields = try NimoOtaProtocol.deviceInfo(body)
|
|
133
|
+
guard fields[3] == Data([1]), fields[5] == Data([1]), fields[2]?.count == 2 else {
|
|
134
|
+
throw Failure(message: "Both arms must be connected and ready to update")
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private func sendBlock(_ slice: NimoOtaProtocol.Slice) throws {
|
|
139
|
+
guard Date().timeIntervalSince(transferStarted) <= 20 * 60 else { throw Failure(message: "Transfer exceeded 20 minutes") }
|
|
140
|
+
let parts = try NimoOtaProtocol.blockParts(firmware, slice: slice, crc: crc, writeCapacity: capacity)
|
|
141
|
+
sendPart(parts, 0) { [self] body in
|
|
142
|
+
let next = try NimoOtaProtocol.blockResult(body)
|
|
143
|
+
var added = 0
|
|
144
|
+
for index in Int(slice.offset) ..< Int(slice.offset) + slice.length where !covered[index] {
|
|
145
|
+
covered[index] = true
|
|
146
|
+
added += 1
|
|
147
|
+
}
|
|
148
|
+
coveredCount += added
|
|
149
|
+
stalls = added == 0 ? stalls + 1 : 0
|
|
150
|
+
guard stalls < 5 else { throw Failure(message: "Five repeated blocks without progress") }
|
|
151
|
+
let percent = coveredCount * 100 / firmware.count
|
|
152
|
+
if percent != lastPercent { lastPercent = percent; progress(percent) }
|
|
153
|
+
if next.slice.offset == 0, next.slice.length == 0 {
|
|
154
|
+
exchange(NimoOtaProtocol.validate, timeout: 30) { [self] validation in
|
|
155
|
+
guard validation == Data([0]) else { throw Failure(message: "Glasses rejected the transferred image") }
|
|
156
|
+
pollSync()
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
_ = try NimoOtaProtocol.firmwareSlice(firmware, next.slice)
|
|
160
|
+
delay(TimeInterval(next.delayMs) / 1000) { [self] in try sendBlock(next.slice) }
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private func sendPart(_ parts: [Data], _ index: Int, completion: @escaping (Data) throws -> Void) {
|
|
166
|
+
if index == parts.count - 1 { exchange(NimoOtaProtocol.block, parts[index], timeout: 30, completion); return }
|
|
167
|
+
do {
|
|
168
|
+
let packet = try nextPacket(NimoOtaProtocol.block, parts[index])
|
|
169
|
+
write(packet) { [weak self] ok in
|
|
170
|
+
guard let self, !self.done else { return }
|
|
171
|
+
if !ok { self.fail(Failure(message: "OTA write failed")); return }
|
|
172
|
+
self.delay(0.005) { [self] in self.sendPart(parts, index + 1, completion: completion) }
|
|
173
|
+
}
|
|
174
|
+
} catch { fail(error) }
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// Wait for the right arm to copy the image to the left arm, then reboot both.
|
|
178
|
+
private func pollSync() {
|
|
179
|
+
exchangeResult(NimoOtaProtocol.sync, timeout: 8) { [self] result in
|
|
180
|
+
let body = try? result.get()
|
|
181
|
+
if body == Data([0]) {
|
|
182
|
+
do {
|
|
183
|
+
let packet = try nextPacket(NimoOtaProtocol.reset, Data([0]))
|
|
184
|
+
write(packet) { _ in }
|
|
185
|
+
done = true
|
|
186
|
+
clearTimers()
|
|
187
|
+
finished(nil)
|
|
188
|
+
} catch { fail(error) }
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
if body == Data([1]) { syncPending += 1 } else { syncFailures += 1 }
|
|
192
|
+
if syncPending >= 180 || syncFailures >= 8 {
|
|
193
|
+
fail(Failure(message: "Arms did not finish synchronizing; no reboot sent"))
|
|
194
|
+
} else {
|
|
195
|
+
delay(1) { [self] in pollSync() }
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private func exchange(_ command: UInt8, _ params: Data = Data(), timeout: TimeInterval = 15,
|
|
201
|
+
_ completion: @escaping (Data) throws -> Void)
|
|
202
|
+
{
|
|
203
|
+
exchangeResult(command, params, timeout: timeout) { [weak self] result in
|
|
204
|
+
do { try completion(result.get()) } catch { self?.fail(error) }
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private func exchangeResult(_ command: UInt8, _ params: Data = Data(), timeout: TimeInterval,
|
|
209
|
+
_ completion: @escaping (Result<Data, Error>) -> Void)
|
|
210
|
+
{
|
|
211
|
+
guard !done else { return }
|
|
212
|
+
do {
|
|
213
|
+
let packet = try nextPacket(command, params)
|
|
214
|
+
let entry = Pending(command, sequence, completion)
|
|
215
|
+
pending = entry
|
|
216
|
+
cancelTimer = schedule(timeout) { [weak self] in
|
|
217
|
+
guard let self, self.pending === entry, !self.done else { return }
|
|
218
|
+
self.pending = nil; self.cancelTimer = nil
|
|
219
|
+
completion(.failure(Failure(message: "OTA command \(command) timed out")))
|
|
220
|
+
}
|
|
221
|
+
write(packet) { [weak self] ok in
|
|
222
|
+
guard let self, !ok, self.pending === entry, !self.done else { return }
|
|
223
|
+
self.pending = nil
|
|
224
|
+
self.cancelTimer?(); self.cancelTimer = nil
|
|
225
|
+
completion(.failure(Failure(message: "OTA write failed")))
|
|
226
|
+
}
|
|
227
|
+
} catch { completion(.failure(error)) }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private func nextPacket(_ command: UInt8, _ params: Data = Data()) throws -> Data {
|
|
231
|
+
sequence &+= 1
|
|
232
|
+
let packet = try NimoOtaProtocol.request(command, sequence: sequence, params: params)
|
|
233
|
+
guard packet.count <= capacity else { throw Failure(message: "OTA request exceeds the negotiated write capacity") }
|
|
234
|
+
return packet
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private func delay(_ seconds: TimeInterval, _ callback: @escaping () throws -> Void) {
|
|
238
|
+
cancelDelay?()
|
|
239
|
+
cancelDelay = schedule(seconds) { [weak self] in
|
|
240
|
+
guard let self else { return }
|
|
241
|
+
self.cancelDelay = nil
|
|
242
|
+
guard !self.done else { return }
|
|
243
|
+
do { try callback() } catch { self.fail(error) }
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private func clearTimers() {
|
|
248
|
+
cancelTimer?(); cancelTimer = nil
|
|
249
|
+
cancelDelay?(); cancelDelay = nil
|
|
250
|
+
pending = nil
|
|
251
|
+
decoder.reset()
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
private func fail(_ error: Error) {
|
|
255
|
+
guard !done else { return }
|
|
256
|
+
done = true
|
|
257
|
+
clearTimers()
|
|
258
|
+
finished(error.localizedDescription)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// NIMO's OTA channel (7033/2001/2002), distinct from the normal 0xBF command channel.
|
|
4
|
+
enum NimoOtaProtocol {
|
|
5
|
+
static let info: UInt8 = 0x02
|
|
6
|
+
static let reset: UInt8 = 0x03
|
|
7
|
+
static let fileOffsetCommand: UInt8 = 0xE1
|
|
8
|
+
static let canUpdate: UInt8 = 0xE2
|
|
9
|
+
static let enter: UInt8 = 0xE3
|
|
10
|
+
static let block: UInt8 = 0xE5
|
|
11
|
+
static let validate: UInt8 = 0xE6
|
|
12
|
+
static let sync: UInt8 = 0xE8
|
|
13
|
+
private static let header: [UInt8] = [0x70, 0x07, 0x6E]
|
|
14
|
+
|
|
15
|
+
struct ProtocolError: Error, LocalizedError {
|
|
16
|
+
let message: String
|
|
17
|
+
var errorDescription: String? {
|
|
18
|
+
message
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
struct Response {
|
|
23
|
+
let command: UInt8
|
|
24
|
+
let sequence: UInt8
|
|
25
|
+
let status: UInt8
|
|
26
|
+
let body: Data
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
struct Slice: Equatable {
|
|
30
|
+
let offset: UInt64
|
|
31
|
+
let length: Int
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
struct EnterResult {
|
|
35
|
+
let slice: Slice
|
|
36
|
+
let crc: Bool
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
struct BlockResult {
|
|
40
|
+
let slice: Slice
|
|
41
|
+
let delayMs: Int
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static func request(_ command: UInt8, sequence: UInt8, params: Data = Data()) throws -> Data {
|
|
45
|
+
try require(params.count <= 65534, "Invalid OTA request")
|
|
46
|
+
let length = params.count + 1
|
|
47
|
+
return Data(header + [0xC0, command, UInt8(length >> 8), UInt8(truncatingIfNeeded: length), sequence]) + params + Data([0x33])
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// Bounded incremental decoder. Malformed input invalidates the exchange; it is never silently skipped.
|
|
51
|
+
final class Decoder {
|
|
52
|
+
private var buffer: [UInt8] = []
|
|
53
|
+
func reset() {
|
|
54
|
+
buffer.removeAll(keepingCapacity: true)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
func feed(_ bytes: Data) throws -> [Response] {
|
|
58
|
+
var result: [Response] = []
|
|
59
|
+
do {
|
|
60
|
+
for byte in bytes {
|
|
61
|
+
buffer.append(byte)
|
|
62
|
+
if buffer.count <= 3 { try require(byte == header[buffer.count - 1], "Invalid OTA header") }
|
|
63
|
+
if buffer.count == 4 { try require(buffer[3] == 0, "Invalid OTA direction") }
|
|
64
|
+
if buffer.count >= 7 {
|
|
65
|
+
let length = u16(buffer, 5)
|
|
66
|
+
try require((2 ... 4096).contains(length), "Invalid OTA response length")
|
|
67
|
+
if buffer.count == length + 8 {
|
|
68
|
+
try require(byte == 0x33, "Invalid OTA footer")
|
|
69
|
+
result.append(Response(command: buffer[4], sequence: buffer[8], status: buffer[7], body: Data(buffer[9 ..< buffer.count - 1])))
|
|
70
|
+
reset()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} catch { reset(); throw error }
|
|
75
|
+
return result
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static func deviceInfo(_ body: Data) throws -> [UInt8: Data] {
|
|
80
|
+
let bytes = [UInt8](body)
|
|
81
|
+
var fields: [UInt8: Data] = [:]
|
|
82
|
+
var index = 0
|
|
83
|
+
while index < bytes.count {
|
|
84
|
+
let length = Int(bytes[index])
|
|
85
|
+
try require(length >= 1 && length <= bytes.count - index - 1, "Truncated OTA device info")
|
|
86
|
+
let type = bytes[index + 1]
|
|
87
|
+
try require(fields[type] == nil, "Duplicate OTA device info")
|
|
88
|
+
fields[type] = Data(bytes[index + 2 ..< index + 1 + length])
|
|
89
|
+
index += 1 + length
|
|
90
|
+
}
|
|
91
|
+
return fields
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static func fileOffset(_ body: Data) throws -> Slice {
|
|
95
|
+
let bytes = [UInt8](body)
|
|
96
|
+
try require(bytes.count == 6, "Invalid OTA file offset")
|
|
97
|
+
return Slice(offset: u32(bytes, 0), length: u16(bytes, 4))
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static func enterResult(_ body: Data) throws -> EnterResult {
|
|
101
|
+
let bytes = [UInt8](body)
|
|
102
|
+
try require(bytes.count == 8 && bytes[0] == 0 && bytes[7] <= 1, "OTA upgrade entry refused")
|
|
103
|
+
return EnterResult(slice: Slice(offset: u32(bytes, 1), length: u16(bytes, 5)), crc: bytes[7] == 1)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static func blockResult(_ body: Data) throws -> BlockResult {
|
|
107
|
+
let bytes = [UInt8](body)
|
|
108
|
+
try require(bytes.count == 9 && bytes[0] == 0, "OTA block refused")
|
|
109
|
+
let delay = u16(bytes, 7)
|
|
110
|
+
try require(delay <= 30000, "Invalid OTA block delay")
|
|
111
|
+
return BlockResult(slice: Slice(offset: u32(bytes, 1), length: u16(bytes, 5)), delayMs: delay)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static func firmwareSlice(_ firmware: Data, _ slice: Slice) throws -> Data {
|
|
115
|
+
try require(slice.offset <= UInt64(firmware.count) && slice.length > 0 &&
|
|
116
|
+
UInt64(slice.length) <= UInt64(firmware.count) - slice.offset, "OTA slice exceeds firmware")
|
|
117
|
+
let start = firmware.startIndex + Int(slice.offset)
|
|
118
|
+
return firmware.subdata(in: start ..< start + slice.length)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// Each chunk gets its own CRC and sequence; only the final chunk gets a block response.
|
|
122
|
+
static func blockParts(_ firmware: Data, slice: Slice, crc: Bool, writeCapacity: Int) throws -> [Data] {
|
|
123
|
+
let data = try firmwareSlice(firmware, slice)
|
|
124
|
+
try require((20 ... 512).contains(writeCapacity), "Unsupported OTA write capacity")
|
|
125
|
+
let chunkSize = min(496, writeCapacity - 16) - (crc ? 4 : 0)
|
|
126
|
+
try require(chunkSize > 0, "OTA write capacity too small for CRC")
|
|
127
|
+
return stride(from: 0, to: data.count, by: chunkSize).map { start in
|
|
128
|
+
let piece = data.subdata(in: start ..< min(start + chunkSize, data.count))
|
|
129
|
+
return u32Bytes(slice.offset + UInt64(start)) + piece + (crc ? u32Bytes(UInt64(crc32(piece))) : Data())
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static func crc32(_ data: Data) -> UInt32 {
|
|
134
|
+
var value: UInt32 = 0xFFFF_FFFF
|
|
135
|
+
for byte in data {
|
|
136
|
+
value ^= UInt32(byte)
|
|
137
|
+
for _ in 0 ..< 8 {
|
|
138
|
+
value = (value >> 1) ^ (value & 1 == 1 ? 0xEDB8_8320 : 0)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return value ^ 0xFFFF_FFFF
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private static func require(_ valid: Bool, _ message: String) throws {
|
|
145
|
+
if !valid { throw ProtocolError(message: message) }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private static func u16(_ bytes: [UInt8], _ offset: Int) -> Int {
|
|
149
|
+
Int(bytes[offset]) << 8 | Int(bytes[offset + 1])
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private static func u32(_ bytes: [UInt8], _ offset: Int) -> UInt64 {
|
|
153
|
+
(0 ..< 4).reduce(UInt64(0)) { ($0 << 8) | UInt64(bytes[offset + $1]) }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private static func u32Bytes(_ value: UInt64) -> Data {
|
|
157
|
+
Data([24, 16, 8, 0].map { UInt8(truncatingIfNeeded: value >> $0) })
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -221,6 +221,8 @@ protocol SGCManager {
|
|
|
221
221
|
// MARK: - Incident Reporting
|
|
222
222
|
|
|
223
223
|
func sendIncidentId(_ incidentId: String, apiBaseUrl: String?) throws
|
|
224
|
+
/// Tell the glasses Cloud recorded `reportId` for a BES crash incident (Mentra Live only).
|
|
225
|
+
func sendBesCrashReported(incidentKey: String, reportId: String) throws
|
|
224
226
|
|
|
225
227
|
// MARK: - Gallery
|
|
226
228
|
|
|
@@ -287,6 +289,10 @@ extension SGCManager {
|
|
|
287
289
|
|
|
288
290
|
func replayStreamControlReady() {}
|
|
289
291
|
|
|
292
|
+
func sendBesCrashReported(incidentKey _: String, reportId _: String) throws {
|
|
293
|
+
throw BluetoothSdkError(code: "unsupported_device", message: "BES crash reporting is not supported on \(type).")
|
|
294
|
+
}
|
|
295
|
+
|
|
290
296
|
@discardableResult func requestSavedWifiNetworks(requestId _: String, sid _: String) -> Bool {
|
|
291
297
|
false
|
|
292
298
|
}
|
|
@@ -97,10 +97,10 @@ public struct StreamAudioConfig {
|
|
|
97
97
|
public struct StreamIceConfig {
|
|
98
98
|
/// STUN server the glasses use while gathering candidates.
|
|
99
99
|
///
|
|
100
|
-
/// `nil` and empty are different answers
|
|
101
|
-
///
|
|
102
|
-
///
|
|
103
|
-
///
|
|
100
|
+
/// `nil` and empty are different answers. `nil` sends the default Cloudflare STUN server; an
|
|
101
|
+
/// empty string is an explicit request for host-only gathering, which is what a WHIP server on
|
|
102
|
+
/// the glasses' own hotspot needs, since a reflexive candidate there is meaningless and
|
|
103
|
+
/// unreachable.
|
|
104
104
|
public let stun: String?
|
|
105
105
|
|
|
106
106
|
public init(stun: String? = nil) {
|
|
@@ -110,7 +110,7 @@ public struct StreamIceConfig {
|
|
|
110
110
|
var dictionary: [String: Any] {
|
|
111
111
|
var values: [String: Any] = [:]
|
|
112
112
|
// Not filtered on emptiness, unlike the other optional string fields here: "" is the
|
|
113
|
-
// host-only signal, so dropping it would silently
|
|
113
|
+
// host-only signal, so dropping it would silently send the default STUN server.
|
|
114
114
|
if let stun { values["stun"] = stun }
|
|
115
115
|
return values
|
|
116
116
|
}
|
|
@@ -386,6 +386,15 @@ public struct StreamRequest {
|
|
|
386
386
|
)
|
|
387
387
|
}
|
|
388
388
|
|
|
389
|
+
/// The glasses read an http(s) URL as WHIP, the only protocol that uses ICE, and have no STUN
|
|
390
|
+
/// server of their own.
|
|
391
|
+
private var iceToSend: StreamIceConfig? {
|
|
392
|
+
if ice?.stun == nil, streamUrl.hasPrefix("https://") || streamUrl.hasPrefix("http://") {
|
|
393
|
+
return StreamIceConfig(stun: "stun:stun.cloudflare.com:3478")
|
|
394
|
+
}
|
|
395
|
+
return ice
|
|
396
|
+
}
|
|
397
|
+
|
|
389
398
|
public var values: [String: Any] {
|
|
390
399
|
var values: [String: Any] = [:]
|
|
391
400
|
values["type"] = "start_stream"
|
|
@@ -404,7 +413,7 @@ public struct StreamRequest {
|
|
|
404
413
|
if !captureAudio {
|
|
405
414
|
values["captureAudio"] = false
|
|
406
415
|
}
|
|
407
|
-
if let iceValues =
|
|
416
|
+
if let iceValues = iceToSend?.dictionary, !iceValues.isEmpty {
|
|
408
417
|
values["ice"] = iceValues
|
|
409
418
|
}
|
|
410
419
|
if let traceId, !traceId.isEmpty {
|