@mentra/bluetooth-sdk 3.4.0-dev.999 → 3.5.0-dev.1238
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/README.md +33 -11
- package/android/build.gradle +60 -51
- package/android/gradle.properties +3 -0
- package/android/settings.gradle +47 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +76 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +27 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +98 -120
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +2 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +24 -9
- package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +170 -23
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +13 -316
- 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/SGCManager.kt +6 -0
- 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/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/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/utils/IncidentLogBleRelayNamingTest.kt +23 -0
- 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 +98 -1
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +42 -7
- 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/generated/changelogCatalog.d.ts +4 -1
- package/build/generated/changelogCatalog.d.ts.map +1 -1
- package/build/generated/changelogCatalog.js +5 -1
- package/build/generated/changelogCatalog.js.map +1 -1
- package/build/generated/releaseMetadata.js +6 -6
- 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 +6 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +66 -14
- package/ios/MentraBluetoothSDK.podspec +4 -0
- package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
- package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
- package/ios/Source/BesCrashReportWaiters.swift +64 -0
- package/ios/Source/BluetoothSdkDefaults.swift +2 -2
- package/ios/Source/DeviceManager.swift +43 -22
- package/ios/Source/GeneratedChangelogCatalog.swift +2 -1
- package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
- package/ios/Source/MentraBluetoothSDK.swift +27 -15
- 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/BluetoothAvailability.swift +15 -5
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +5 -0
- package/ios/Source/sgcs/MentraLive.swift +173 -42
- package/ios/Source/sgcs/Nimo.swift +39 -272
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +9 -38
- package/ios/Source/sgcs/NimoCanvasSession.swift +0 -1
- package/ios/Source/sgcs/SGCManager.swift +6 -0
- 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/PcmStreamTests.swift +299 -0
- package/ios/Tests/SpeechActivityTests.swift +238 -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 +102 -1
- package/src/_private/BluetoothSdkModule.ts +45 -7
- package/src/generated/changelogCatalog.ts +5 -1
- package/src/generated/releaseMetadata.ts +6 -6
- package/src/index.ts +12 -0
- 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/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/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
|
@@ -536,7 +536,6 @@ private class NimoOpusDecoder {
|
|
|
536
536
|
|
|
537
537
|
actor NimoReconnectionManager {
|
|
538
538
|
private var task: Task<Void, Never>?
|
|
539
|
-
private(set) var liveTasks = 0
|
|
540
539
|
private let intervalSeconds: UInt64
|
|
541
540
|
|
|
542
541
|
init(intervalSeconds: UInt64 = 30) {
|
|
@@ -545,9 +544,7 @@ actor NimoReconnectionManager {
|
|
|
545
544
|
|
|
546
545
|
func start(_ onAttempt: @escaping () async -> Bool) {
|
|
547
546
|
stop()
|
|
548
|
-
liveTasks += 1
|
|
549
547
|
task = Task {
|
|
550
|
-
defer { liveTasks -= 1 }
|
|
551
548
|
var attempts = 0
|
|
552
549
|
while !Task.isCancelled {
|
|
553
550
|
try? await Task.sleep(nanoseconds: intervalSeconds * 1_000_000_000)
|
|
@@ -562,13 +559,6 @@ actor NimoReconnectionManager {
|
|
|
562
559
|
}
|
|
563
560
|
}
|
|
564
561
|
|
|
565
|
-
func stopAndWait() async {
|
|
566
|
-
let original = task
|
|
567
|
-
original?.cancel()
|
|
568
|
-
await original?.value
|
|
569
|
-
if liveTasks == 0 { task = nil }
|
|
570
|
-
}
|
|
571
|
-
|
|
572
562
|
func stop() {
|
|
573
563
|
task?.cancel()
|
|
574
564
|
task = nil
|
|
@@ -578,34 +568,6 @@ actor NimoReconnectionManager {
|
|
|
578
568
|
// MARK: - Nimo Class
|
|
579
569
|
|
|
580
570
|
class Nimo: NSObject, SGCManager {
|
|
581
|
-
let resourceInstanceId = UUID().uuidString.lowercased()
|
|
582
|
-
private(set) var resourceMicEnabled = false
|
|
583
|
-
private var resourceReleaseTask: Task<Void, Never>?
|
|
584
|
-
private var resourceNotificationElapsedMs: UInt64?
|
|
585
|
-
private var resourceTimeCommand: String?
|
|
586
|
-
private var resourceReconnectTasks = 0
|
|
587
|
-
nonisolated private let resourceCallbackCounter = NimoResourceCallbackCounter()
|
|
588
|
-
var resourceCanvasRequired: Bool { canvas.mayHaveEntered }
|
|
589
|
-
var resourceMicStopRequired: Bool { resourceMicEnabled || resourceSession?.connected == true }
|
|
590
|
-
#if SWIFT_PACKAGE
|
|
591
|
-
// Package-only, radio-free fixture for exercising actual private wiring.
|
|
592
|
-
private var packageTestResourceSession: NimoResourceSession?
|
|
593
|
-
#endif
|
|
594
|
-
private var resourceSession: NimoResourceSession? {
|
|
595
|
-
#if SWIFT_PACKAGE
|
|
596
|
-
if let packageTestResourceSession { return packageTestResourceSession }
|
|
597
|
-
#endif
|
|
598
|
-
return NimoResourceFixture.shared.captured(self)
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
nonisolated private func postCallback(_ action: @escaping @MainActor () -> Void) {
|
|
602
|
-
resourceCallbackCounter.add(1)
|
|
603
|
-
DispatchQueue.main.async { [self] in
|
|
604
|
-
action()
|
|
605
|
-
resourceCallbackCounter.add(-1)
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
|
|
609
571
|
var type: String = DeviceTypes.NIMO
|
|
610
572
|
let hasMic = true
|
|
611
573
|
let showBrightnessConfirmation = false
|
|
@@ -718,11 +680,7 @@ class Nimo: NSObject, SGCManager {
|
|
|
718
680
|
enqueue: { [weak self] frames, started, completed in
|
|
719
681
|
self?.enqueueFrames(frames, finalStarted: started, completed: completed) ?? false
|
|
720
682
|
}, reconnect: { [weak self] in self?.abortTransport($0) },
|
|
721
|
-
rejected: { Bridge.log("NIMO: canvas rejected status=\($0); not a render ACK") }
|
|
722
|
-
observedResult: { [weak self] key, status, received in
|
|
723
|
-
guard let self else { return }
|
|
724
|
-
if key == 3 { self.resourceSession?.exitAcknowledged(self, status: status, receivedElapsedMs: received) }
|
|
725
|
-
}, continuousNow: NimoResourceClock.now
|
|
683
|
+
rejected: { Bridge.log("NIMO: canvas rejected status=\($0); not a render ACK") }
|
|
726
684
|
)
|
|
727
685
|
private var batteryPollTimer: Timer?
|
|
728
686
|
|
|
@@ -742,7 +700,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
742
700
|
// MARK: - SGCManager: Connection Management
|
|
743
701
|
|
|
744
702
|
func findCompatibleDevices() {
|
|
745
|
-
guard NimoResourceFixture.shared.admit(self, name: nil) else { return }
|
|
746
703
|
Bridge.log("NIMO: findCompatibleDevices()")
|
|
747
704
|
DEVICE_SEARCH_ID = "NOT_SET"
|
|
748
705
|
isDisconnecting = false
|
|
@@ -752,7 +709,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
752
709
|
}
|
|
753
710
|
|
|
754
711
|
func connectById(_ id: String) {
|
|
755
|
-
guard NimoResourceFixture.shared.admit(self, name: id) else { return }
|
|
756
712
|
Bridge.log("NIMO: connectById(\(id))")
|
|
757
713
|
DEVICE_SEARCH_ID = id
|
|
758
714
|
scanRequested = true
|
|
@@ -769,10 +725,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
769
725
|
|
|
770
726
|
func disconnect() {
|
|
771
727
|
Bridge.log("NIMO: disconnect()")
|
|
772
|
-
if let resourceSession,
|
|
773
|
-
!resourceSession.shouldRunOrdinaryDisconnect(self, originalClosed: NimoResourceFixture.shared.hasVerifiedOriginalClosure(self)) {
|
|
774
|
-
return
|
|
775
|
-
}
|
|
776
728
|
isDisconnecting = true
|
|
777
729
|
cancelPairingTimeout()
|
|
778
730
|
cancelTwsTimeout()
|
|
@@ -797,7 +749,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
797
749
|
|
|
798
750
|
/// Explicit Unpair only; passive forget/cleanup must never reset the glasses.
|
|
799
751
|
func resetForUnpair(onResult: @escaping (Bool) -> Void) {
|
|
800
|
-
if let resourceSession { resourceSession.recordError("factory-reset-refused-during-capture"); onResult(false); return }
|
|
801
752
|
guard peripheral != nil, txChar != nil, handshakeState == .ready else {
|
|
802
753
|
Bridge.log("NIMO: unpair while offline; remote reset unavailable")
|
|
803
754
|
onResult(false)
|
|
@@ -818,7 +769,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
818
769
|
}
|
|
819
770
|
|
|
820
771
|
func forget() {
|
|
821
|
-
if let resourceSession { resourceSession.recordError("forget-refused-during-capture"); return }
|
|
822
772
|
Bridge.log("NIMO: forget()")
|
|
823
773
|
disconnect()
|
|
824
774
|
lastDeviceName = nil
|
|
@@ -851,13 +801,11 @@ class Nimo: NSObject, SGCManager {
|
|
|
851
801
|
// MARK: - SGCManager: Audio Control
|
|
852
802
|
|
|
853
803
|
func setMicEnabled(_ enabled: Bool) {
|
|
854
|
-
guard NimoResourceFixture.shared.allowsProducer(self) else { return }
|
|
855
804
|
Bridge.log("NIMO: setMicEnabled(\(enabled))")
|
|
856
805
|
guard let micChar else {
|
|
857
806
|
Bridge.log("NIMO: mic characteristic not available")
|
|
858
807
|
return
|
|
859
808
|
}
|
|
860
|
-
resourceMicEnabled = enabled
|
|
861
809
|
DeviceStore.shared.apply("glasses", "micEnabled", enabled)
|
|
862
810
|
if enabled {
|
|
863
811
|
if opusDecoder == nil {
|
|
@@ -964,7 +912,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
964
912
|
}
|
|
965
913
|
|
|
966
914
|
private func encodeCanvas(scope: String, force: Bool = false, encode: @escaping () throws -> Data) {
|
|
967
|
-
guard NimoResourceFixture.shared.allowsProducer(self) else { return }
|
|
968
915
|
canvasEncoder.submit(encode: encode) { [weak self] bytes in
|
|
969
916
|
self?.canvas.offer(bytes, scope: scope, force: force)
|
|
970
917
|
}
|
|
@@ -1163,7 +1110,7 @@ class Nimo: NSObject, SGCManager {
|
|
|
1163
1110
|
|
|
1164
1111
|
@discardableResult
|
|
1165
1112
|
private func startScan() -> Bool {
|
|
1166
|
-
guard scanRequested, !isDisconnecting
|
|
1113
|
+
guard scanRequested, !isDisconnecting else { return false }
|
|
1167
1114
|
Bridge.log("NIMO: startScan()")
|
|
1168
1115
|
if centralManager == nil {
|
|
1169
1116
|
centralManager = CBCentralManager(
|
|
@@ -1195,7 +1142,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
1195
1142
|
guard DEVICE_SEARCH_ID != "NOT_SET", name == DEVICE_SEARCH_ID,
|
|
1196
1143
|
self.peripheral == nil else { return }
|
|
1197
1144
|
Bridge.log("NIMO: Connecting to \(name)")
|
|
1198
|
-
guard NimoResourceFixture.shared.beginTransport(self, peripheral: peripheral, uuid: peripheral.identifier.uuidString) else { return }
|
|
1199
1145
|
stopScan()
|
|
1200
1146
|
lastDeviceName = name
|
|
1201
1147
|
lastDeviceUUID = peripheral.identifier.uuidString
|
|
@@ -1215,7 +1161,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
1215
1161
|
else { return false }
|
|
1216
1162
|
|
|
1217
1163
|
Bridge.log("NIMO: connectByUUID - \(known.name ?? uuidString)")
|
|
1218
|
-
guard NimoResourceFixture.shared.beginTransport(self, peripheral: known, uuid: known.identifier.uuidString) else { return false }
|
|
1219
1164
|
peripheral = known
|
|
1220
1165
|
known.delegate = self
|
|
1221
1166
|
centralManager?.connect(known, options: nil)
|
|
@@ -1243,7 +1188,7 @@ class Nimo: NSObject, SGCManager {
|
|
|
1243
1188
|
// MARK: - Write Queue
|
|
1244
1189
|
|
|
1245
1190
|
private func enqueueWrite(_ characteristic: CBCharacteristic, _ bytes: Data) {
|
|
1246
|
-
guard
|
|
1191
|
+
guard let peripheral else { return }
|
|
1247
1192
|
writes.enqueue(peripheral, characteristic: characteristic, frames: [bytes])
|
|
1248
1193
|
}
|
|
1249
1194
|
|
|
@@ -1256,17 +1201,12 @@ class Nimo: NSObject, SGCManager {
|
|
|
1256
1201
|
completed: @escaping () -> Void) -> Bool
|
|
1257
1202
|
{
|
|
1258
1203
|
guard let peripheral, let txChar else { return false }
|
|
1259
|
-
if let resourceSession, resourceSession.releaseNonce != nil, frames.first?.count ?? 0 > 9, frames.first?[9] != 3 { return false }
|
|
1260
1204
|
return writes.enqueue(peripheral, characteristic: txChar, frames: frames,
|
|
1261
1205
|
finalStarted: finalStarted, completed: completed)
|
|
1262
1206
|
}
|
|
1263
1207
|
|
|
1264
1208
|
private func abortTransport(_ reason: String) {
|
|
1265
1209
|
Bridge.log("NIMO: retiring transport: \(reason)")
|
|
1266
|
-
if let resourceSession {
|
|
1267
|
-
resourceSession.recordError("transport-retired: \(reason)")
|
|
1268
|
-
if resourceSession.releaseNonce != nil { resourceSession.disconnectRequested(self) }
|
|
1269
|
-
}
|
|
1270
1210
|
resetSessionState()
|
|
1271
1211
|
if let peripheral { centralManager?.cancelPeripheralConnection(peripheral) }
|
|
1272
1212
|
}
|
|
@@ -1274,29 +1214,21 @@ class Nimo: NSObject, SGCManager {
|
|
|
1274
1214
|
// MARK: - Pending ACKs
|
|
1275
1215
|
|
|
1276
1216
|
private func sendAwaitingAck(
|
|
1277
|
-
cmd: Int, key: Int, payload: Data,
|
|
1217
|
+
cmd: Int, key: Int, payload: Data, onResult: @escaping (Bool) -> Void
|
|
1278
1218
|
) {
|
|
1279
|
-
guard NimoResourceFixture.shared.allowsProducer(self) else { onResult(false); return }
|
|
1280
1219
|
let ackKey = (cmd << 8) | key
|
|
1281
1220
|
// Only one in-flight ack per (cmd,key); fail any previous waiter.
|
|
1282
1221
|
if let previous = pendingAcks.removeValue(forKey: ackKey) {
|
|
1283
1222
|
previous.timeoutItem.cancel()
|
|
1284
1223
|
previous.onResult(false)
|
|
1285
1224
|
}
|
|
1286
|
-
let timeoutItem =
|
|
1225
|
+
let timeoutItem = DispatchWorkItem { [weak self] in
|
|
1226
|
+
guard let self else { return }
|
|
1227
|
+
self.pendingAcks.removeValue(forKey: ackKey)?.onResult(false)
|
|
1228
|
+
}
|
|
1287
1229
|
pendingAcks[ackKey] = PendingAck(onResult: onResult, timeoutItem: timeoutItem)
|
|
1288
1230
|
DispatchQueue.main.asyncAfter(deadline: .now() + Const.ackTimeoutSeconds, execute: timeoutItem)
|
|
1289
|
-
|
|
1290
|
-
writes.enqueue(peripheral, characteristic: txChar,
|
|
1291
|
-
frames: [NimoFrameCodec.encodeFrame(cmd: cmd, key: key, payload: payload, needsAck: true)],
|
|
1292
|
-
finalStarted: writeStarted, completed: writeCompleted)
|
|
1293
|
-
}
|
|
1294
|
-
|
|
1295
|
-
private func pendingAckTimeoutAction(_ ackKey: Int) -> () -> Void {
|
|
1296
|
-
{ [weak self] in self?.pendingAcks.removeValue(forKey: ackKey)?.onResult(false) }
|
|
1297
|
-
}
|
|
1298
|
-
private func pendingAckTimeout(_ ackKey: Int) -> DispatchWorkItem {
|
|
1299
|
-
DispatchWorkItem(block: pendingAckTimeoutAction(ackKey))
|
|
1231
|
+
sendFrame(NimoFrameCodec.encodeFrame(cmd: cmd, key: key, payload: payload, needsAck: true))
|
|
1300
1232
|
}
|
|
1301
1233
|
|
|
1302
1234
|
private func resolvePendingAck(cmd: Int, key: Int, success: Bool) {
|
|
@@ -1354,34 +1286,23 @@ class Nimo: NSObject, SGCManager {
|
|
|
1354
1286
|
}
|
|
1355
1287
|
|
|
1356
1288
|
private func attemptSetTime() {
|
|
1357
|
-
guard
|
|
1289
|
+
guard handshakeState == .awaitingTimeAck, let expected = peripheral else { return }
|
|
1358
1290
|
setTimeAttempts += 1
|
|
1359
|
-
let command = resourceSession?.beginTimeCommand(self)
|
|
1360
|
-
resourceTimeCommand = command
|
|
1361
1291
|
sendAwaitingAck(cmd: NimoProtocol.CMD_SET_PARAMETER, key: NimoProtocol.SET_TIME,
|
|
1362
|
-
payload: NimoFrameCodec.encodeDeviceTime()
|
|
1363
|
-
writeStarted: { [weak self] in guard let self, let command else { return }; self.resourceSession?.timeWriteStarted(self, command: command) },
|
|
1364
|
-
writeCompleted: { [weak self] in guard let self, let command else { return }; self.resourceSession?.timeWriteCompleted(self, command: command)
|
|
1365
|
-
if self.resourceSession?.ready == true, self.handshakeState == .awaitingTimeAck { self.finishHandshake() }
|
|
1366
|
-
})
|
|
1292
|
+
payload: NimoFrameCodec.encodeDeviceTime())
|
|
1367
1293
|
{ [weak self] ok in
|
|
1368
|
-
self
|
|
1294
|
+
guard let self, self.peripheral === expected, self.handshakeState == .awaitingTimeAck else { return }
|
|
1295
|
+
if ok { self.finishHandshake() }
|
|
1296
|
+
else if self.setTimeAttempts < 3 {
|
|
1297
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
|
1298
|
+
guard let self, self.peripheral === expected else { return }
|
|
1299
|
+
self.attemptSetTime()
|
|
1300
|
+
}
|
|
1301
|
+
} else { self.handshakeFailed() }
|
|
1369
1302
|
}
|
|
1370
1303
|
}
|
|
1371
1304
|
|
|
1372
|
-
private func handleTimeSyncResult(_ ok: Bool, expected: CBPeripheral?) {
|
|
1373
|
-
guard peripheral === expected, handshakeState == .awaitingTimeAck else { return }
|
|
1374
|
-
if ok { if resourceSession == nil || resourceSession?.ready == true { finishHandshake() } }
|
|
1375
|
-
else if resourceSession == nil, setTimeAttempts < 3 {
|
|
1376
|
-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
|
1377
|
-
guard let self, self.peripheral === expected else { return }
|
|
1378
|
-
self.attemptSetTime()
|
|
1379
|
-
}
|
|
1380
|
-
} else { handshakeFailed() }
|
|
1381
|
-
}
|
|
1382
|
-
|
|
1383
1305
|
private func finishHandshake() {
|
|
1384
|
-
guard NimoResourceFixture.shared.allowsProducer(self) else { return }
|
|
1385
1306
|
Bridge.log("NIMO: handshake complete — fully connected")
|
|
1386
1307
|
handshakeState = .ready
|
|
1387
1308
|
sendFrame(
|
|
@@ -1411,7 +1332,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
1411
1332
|
}
|
|
1412
1333
|
|
|
1413
1334
|
private func handshakeFailed() {
|
|
1414
|
-
if let resourceSession, !resourceSession.recordHandshakeFailure(self) { return }
|
|
1415
1335
|
handshakeState = .idle
|
|
1416
1336
|
if let peripheral {
|
|
1417
1337
|
centralManager?.cancelPeripheralConnection(peripheral)
|
|
@@ -1433,10 +1353,7 @@ class Nimo: NSObject, SGCManager {
|
|
|
1433
1353
|
}
|
|
1434
1354
|
|
|
1435
1355
|
private func startReconnectionTimer() {
|
|
1436
|
-
|
|
1437
|
-
resourceReconnectTasks += 1
|
|
1438
|
-
Task { [self] in
|
|
1439
|
-
defer { resourceReconnectTasks -= 1 }
|
|
1356
|
+
Task {
|
|
1440
1357
|
await reconnectionManager.start { [weak self] in
|
|
1441
1358
|
guard let self else { return false }
|
|
1442
1359
|
if await MainActor.run(body: {
|
|
@@ -1480,14 +1397,13 @@ class Nimo: NSObject, SGCManager {
|
|
|
1480
1397
|
private func handleRxPacket(_ packet: Data) {
|
|
1481
1398
|
if packet.count >= 10, packet[8] == 7, [1, 3, 4].contains(packet[9]) {
|
|
1482
1399
|
if let response = NimoCanvasCodec.response(packet) {
|
|
1483
|
-
|
|
1484
|
-
guard productAllowed || (resourceSession?.releaseNonce != nil && canvas.acceptsReleaseResponse(key: response.key, payload: response.payload)) else { return }
|
|
1400
|
+
guard (DeviceManager.shared.sgc as? Nimo) === self else { return }
|
|
1485
1401
|
if response.key == 1 { Bridge.log("NIMO: canvas Launch response status=\(response.payload.first.map(Int.init) ?? -1)") }
|
|
1486
|
-
canvas.response(key: response.key, payload: response.payload
|
|
1402
|
+
canvas.response(key: response.key, payload: response.payload)
|
|
1487
1403
|
}
|
|
1488
1404
|
return
|
|
1489
1405
|
}
|
|
1490
|
-
guard (DeviceManager.shared.sgc as? Nimo) === self
|
|
1406
|
+
guard (DeviceManager.shared.sgc as? Nimo) === self else { return }
|
|
1491
1407
|
receiveAssembler.cleanup()
|
|
1492
1408
|
for frame in receiveAssembler.ingest(packet) {
|
|
1493
1409
|
guard let decoded = NimoFrameCodec.decode(frame),
|
|
@@ -1565,7 +1481,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
1565
1481
|
}
|
|
1566
1482
|
|
|
1567
1483
|
private func onTwsState(_ connected: Bool) {
|
|
1568
|
-
resourceSession?.observeTws(self, connected: connected, receivedElapsedMs: resourceNotificationElapsedMs)
|
|
1569
1484
|
twsConnected = connected
|
|
1570
1485
|
if connected, handshakeState == .awaitingTws {
|
|
1571
1486
|
proceedToTimeSync()
|
|
@@ -1612,13 +1527,6 @@ class Nimo: NSObject, SGCManager {
|
|
|
1612
1527
|
if cmd == NimoProtocol.CMD_SET_PARAMETER, key == NimoProtocol.SET_SYSTEM_LANGUAGE {
|
|
1613
1528
|
Bridge.log("NIMO: English menu setting acknowledged=\(statusCode == 0) status=\(statusCode)")
|
|
1614
1529
|
}
|
|
1615
|
-
if cmd == NimoProtocol.CMD_SET_PARAMETER, key == NimoProtocol.SET_TIME, let resourceSession {
|
|
1616
|
-
guard let started = resourceSession.timeWriteStartedElapsedMs,
|
|
1617
|
-
let received = resourceNotificationElapsedMs, received >= started else { return }
|
|
1618
|
-
}
|
|
1619
|
-
if cmd == NimoProtocol.CMD_SET_PARAMETER, key == NimoProtocol.SET_TIME, let command = resourceTimeCommand {
|
|
1620
|
-
resourceSession?.timeAck(self, command: command, status: statusCode, receivedElapsedMs: resourceNotificationElapsedMs)
|
|
1621
|
-
}
|
|
1622
1530
|
resolvePendingAck(cmd: cmd, key: key, success: statusCode == 0)
|
|
1623
1531
|
|
|
1624
1532
|
guard cmd == NimoProtocol.CMD_GET_PARAMETER, statusCode == 0 else { return }
|
|
@@ -1684,9 +1592,7 @@ class Nimo: NSObject, SGCManager {
|
|
|
1684
1592
|
// MARK: - Mic Audio
|
|
1685
1593
|
|
|
1686
1594
|
private func handleMicPacket(_ data: Data) {
|
|
1687
|
-
|
|
1688
|
-
guard (DeviceManager.shared.sgc as? Nimo) === self, NimoResourceFixture.shared.allowsProducer(self) else { return }
|
|
1689
|
-
if resourceSession?.releaseNonce != nil { return }
|
|
1595
|
+
guard (DeviceManager.shared.sgc as? Nimo) === self else { return }
|
|
1690
1596
|
guard let packet = NimoAudioParser.parse(data) else { return }
|
|
1691
1597
|
let now = Date()
|
|
1692
1598
|
switch packet.type {
|
|
@@ -1713,134 +1619,14 @@ class Nimo: NSObject, SGCManager {
|
|
|
1713
1619
|
}
|
|
1714
1620
|
}
|
|
1715
1621
|
}
|
|
1716
|
-
|
|
1717
|
-
func resourceProducers() async -> NimoResourceProducers {
|
|
1718
|
-
var result = NimoResourceProducers()
|
|
1719
|
-
result.writerActive = writes.activeCount; result.writerQueued = writes.queuedCount
|
|
1720
|
-
result.encoderWorker = canvasEncoder.workerCount; result.encoderDelivery = canvasEncoder.deliveryCount
|
|
1721
|
-
result.postedCallbacks = resourceCallbackCounter.value + NimoResourceFixture.shared.pendingOriginalManagerEntries
|
|
1722
|
-
result.canvasCommands = canvas.pendingCount; result.pendingAcks = pendingAcks.count
|
|
1723
|
-
// Opus decode, LC3 conversion and Bridge emission are synchronous on
|
|
1724
|
-
// Main on macOS; their entire extent is inside the posted CB callback.
|
|
1725
|
-
result.audioWorkers = 0
|
|
1726
|
-
result.scan = scanRequested ? 1 : 0
|
|
1727
|
-
result.timers = (batteryPollTimer == nil ? 0 : 1) + (twsTimeoutItem == nil ? 0 : 1) + (pairingTimeoutItem == nil ? 0 : 1)
|
|
1728
|
-
result.reconnectTask = await reconnectionManager.liveTasks + resourceReconnectTasks
|
|
1729
|
-
return result
|
|
1730
|
-
}
|
|
1731
|
-
|
|
1732
|
-
func releaseResource(_ original: NimoResourceSession) async {
|
|
1733
|
-
if let resourceReleaseTask { await resourceReleaseTask.value; return }
|
|
1734
|
-
guard original.isOwner(self) else { return }
|
|
1735
|
-
// Retire setup waiters synchronously. Their ACK/timeout can already be
|
|
1736
|
-
// queued when the capture fence is installed; shutdown is not a failed
|
|
1737
|
-
// handshake. The real transport writes and Canvas drain remain intact.
|
|
1738
|
-
handshakeState = .idle
|
|
1739
|
-
cancelTwsTimeout()
|
|
1740
|
-
resolvePendingAck(cmd: NimoProtocol.CMD_SET_PARAMETER, key: NimoProtocol.SET_TIME, success: false)
|
|
1741
|
-
// Original Canvas ACKs may arrive while reconnect tasks are settling.
|
|
1742
|
-
canvas.drainForRelease()
|
|
1743
|
-
let task = Task { @MainActor [self] in
|
|
1744
|
-
isDisconnecting = true
|
|
1745
|
-
stopScan(); stopTimers(); cancelPairingTimeout(); cancelTwsTimeout()
|
|
1746
|
-
canvasEncoder.invalidate()
|
|
1747
|
-
await reconnectionManager.stopAndWait()
|
|
1748
|
-
// Existing pending commands retain their real callbacks/deadlines.
|
|
1749
|
-
// Exit is queued through the same canvas/write state machine.
|
|
1750
|
-
if original.micStopRequired {
|
|
1751
|
-
if let peripheral, let micChar {
|
|
1752
|
-
writes.enqueue(peripheral, characteristic: micChar, frames: [Data([0x52, 0, 0, 0])],
|
|
1753
|
-
finalStarted: { [weak self] in guard let self else { return }; original.micStopStarted(self) },
|
|
1754
|
-
completed: { [weak self] in guard let self else { return }; original.micStopWritten(self) })
|
|
1755
|
-
} else { original.recordError("mic-stop-transport-unavailable") }
|
|
1756
|
-
}
|
|
1757
|
-
let deadline = NimoResourceClock.now() + 15_000
|
|
1758
|
-
while NimoResourceClock.now() < deadline {
|
|
1759
|
-
let producers = await resourceProducers()
|
|
1760
|
-
if original.commandsSettled, producers.writerActive == 0, producers.writerQueued == 0,
|
|
1761
|
-
producers.encoderWorker == 0, producers.encoderDelivery == 0, producers.pendingAcks == 0,
|
|
1762
|
-
producers.canvasCommands == 0, producers.reconnectTask == 0, producers.postedCallbacks == 0,
|
|
1763
|
-
producers.scan == 0, producers.timers == 0, producers.audioWorkers == 0 { break }
|
|
1764
|
-
try? await Task.sleep(nanoseconds: 10_000_000)
|
|
1765
|
-
}
|
|
1766
|
-
if !original.commandsSettled { original.recordError("release-command-evidence-incomplete") }
|
|
1767
|
-
if let peripheral {
|
|
1768
|
-
original.disconnectRequested(self)
|
|
1769
|
-
centralManager?.cancelPeripheralConnection(peripheral)
|
|
1770
|
-
// Retain this exact peripheral and delegate until original closure.
|
|
1771
|
-
while original.disconnectCallbackElapsedMs == nil, original.connectionFailedElapsedMs == nil, NimoResourceClock.now() < deadline + 5_000 {
|
|
1772
|
-
try? await Task.sleep(nanoseconds: 10_000_000)
|
|
1773
|
-
}
|
|
1774
|
-
if original.disconnectCallbackElapsedMs == nil, original.connectionFailedElapsedMs == nil { original.recordError("original-disconnect-callback-missing") }
|
|
1775
|
-
} else if original.connectionFailedElapsedMs == nil { original.proveNoTransport() }
|
|
1776
|
-
if original.disconnectCallbackElapsedMs != nil || original.connectionFailedElapsedMs != nil || original.noTransport {
|
|
1777
|
-
resetSessionState()
|
|
1778
|
-
peripheral = nil; txChar = nil; rxChar = nil; micChar = nil; opusDecoder = nil
|
|
1779
|
-
resourceMicEnabled = false
|
|
1780
|
-
NimoResourceManagerState.publish(owner: self, current: DeviceManager.shared.sgc as? Nimo) {
|
|
1781
|
-
DeviceStore.shared.apply("glasses", "micEnabled", false)
|
|
1782
|
-
DeviceStore.shared.apply("glasses", "connected", false)
|
|
1783
|
-
DeviceStore.shared.apply("glasses", "fullyBooted", false)
|
|
1784
|
-
}
|
|
1785
|
-
}
|
|
1786
|
-
}
|
|
1787
|
-
resourceReleaseTask = task
|
|
1788
|
-
await task.value
|
|
1789
|
-
}
|
|
1790
|
-
|
|
1791
|
-
}
|
|
1792
|
-
|
|
1793
|
-
#if SWIFT_PACKAGE
|
|
1794
|
-
// SDK-package-only, radio-free regression fixture; no app or command API.
|
|
1795
|
-
struct NimoPackageHandshakeObservation {
|
|
1796
|
-
let errors: [String]
|
|
1797
|
-
let pendingAcks: Int
|
|
1798
|
-
let idle: Bool
|
|
1799
|
-
let ready: Bool
|
|
1800
|
-
let sameWriteEvidence: Bool
|
|
1801
|
-
let ackStillAbsent: Bool
|
|
1802
|
-
let noRadio: Bool
|
|
1803
|
-
let physicalClosure: Bool
|
|
1804
1622
|
}
|
|
1805
|
-
extension Nimo {
|
|
1806
|
-
@MainActor
|
|
1807
|
-
func packageExercisePendingTimeAck(cancel: Bool, timeoutBeforeEntry: Bool, activeTimeout: Bool = false) async -> NimoPackageHandshakeObservation {
|
|
1808
|
-
let id = "00000000-0000-4000-8000-000000000001"
|
|
1809
|
-
var tick: UInt64 = 100
|
|
1810
|
-
let session = NimoResourceSession(captureNonce: id, pair: NimoResourcePair(name: "NIMO test", peripheralUuid: id), now: { tick += 1; return tick })
|
|
1811
|
-
let peer = NSObject()
|
|
1812
|
-
_ = session.admit(self, instanceId: resourceInstanceId, name: "NIMO test")
|
|
1813
|
-
_ = session.beginTransport(self, peripheral: peer, uuid: id)
|
|
1814
|
-
session.transportConnected(self, peer); session.observeTws(self, connected: true)
|
|
1815
|
-
let command = session.beginTimeCommand(self)!
|
|
1816
|
-
session.timeWriteStarted(self, command: command); session.timeWriteCompleted(self, command: command)
|
|
1817
|
-
let started = session.timeWriteStartedElapsedMs, completed = session.timeWriteCompletedElapsedMs
|
|
1818
|
-
packageTestResourceSession = session; resourceTimeCommand = command; handshakeState = .awaitingTimeAck
|
|
1819
|
-
let key = (NimoProtocol.CMD_SET_PARAMETER << 8) | NimoProtocol.SET_TIME
|
|
1820
|
-
let alreadyQueuedTimeout = pendingAckTimeoutAction(key)
|
|
1821
|
-
pendingAcks[key] = PendingAck(onResult: { [weak self] ok in self?.handleTimeSyncResult(ok, expected: nil) }, timeoutItem: DispatchWorkItem(block: alreadyQueuedTimeout))
|
|
1822
|
-
if activeTimeout { alreadyQueuedTimeout() }
|
|
1823
|
-
_ = session.beginRelease(nonce: id, cancel: cancel, mic: false, canvas: false)
|
|
1824
|
-
if timeoutBeforeEntry { alreadyQueuedTimeout() }
|
|
1825
|
-
await releaseResource(session)
|
|
1826
|
-
alreadyQueuedTimeout() // Closure already dequeued before cancellation.
|
|
1827
|
-
handleTimeSyncResult(true, expected: nil)
|
|
1828
|
-
session.timeAck(self, command: command, status: 0)
|
|
1829
|
-
return NimoPackageHandshakeObservation(errors: session.errors, pendingAcks: pendingAcks.count,
|
|
1830
|
-
idle: handshakeState == .idle, ready: session.ready,
|
|
1831
|
-
sameWriteEvidence: started == session.timeWriteStartedElapsedMs && completed == session.timeWriteCompletedElapsedMs,
|
|
1832
|
-
ackStillAbsent: session.timeAckElapsedMs == nil, noRadio: centralManager == nil && peripheral == nil && !scanRequested,
|
|
1833
|
-
physicalClosure: session.settled(producers: NimoResourceProducers()))
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
#endif
|
|
1837
1623
|
|
|
1838
1624
|
// MARK: - CBCentralManagerDelegate
|
|
1839
1625
|
|
|
1840
1626
|
extension Nimo: CBCentralManagerDelegate {
|
|
1841
1627
|
nonisolated func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
1842
1628
|
let state = central.state
|
|
1843
|
-
|
|
1629
|
+
DispatchQueue.main.async { [weak self] in
|
|
1844
1630
|
guard let self else { return }
|
|
1845
1631
|
Bridge.log("NIMO: Bluetooth state: \(state.rawValue)")
|
|
1846
1632
|
if state == .poweredOn {
|
|
@@ -1857,7 +1643,7 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1857
1643
|
) {
|
|
1858
1644
|
let advertisedName = advertisementData[CBAdvertisementDataLocalNameKey] as? String
|
|
1859
1645
|
let rssiValue = rssi.intValue
|
|
1860
|
-
|
|
1646
|
+
DispatchQueue.main.async { [weak self] in
|
|
1861
1647
|
self?.discovery.receive(peripheral, advertisedName: advertisedName, rssi: rssiValue)
|
|
1862
1648
|
}
|
|
1863
1649
|
}
|
|
@@ -1867,27 +1653,17 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1867
1653
|
_: CBCentralManager, connectionEventDidOccur event: CBConnectionEvent, for peripheral: CBPeripheral
|
|
1868
1654
|
) {
|
|
1869
1655
|
guard event == .peerConnected else { return }
|
|
1870
|
-
|
|
1656
|
+
DispatchQueue.main.async { [weak self] in
|
|
1871
1657
|
self?.discovery.receive(peripheral)
|
|
1872
1658
|
}
|
|
1873
1659
|
}
|
|
1874
1660
|
#endif
|
|
1875
1661
|
|
|
1876
1662
|
nonisolated func centralManager(_: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
|
1877
|
-
|
|
1663
|
+
DispatchQueue.main.async { [weak self] in
|
|
1878
1664
|
guard let self else { return }
|
|
1879
1665
|
Bridge.log("NIMO: Connected to \(peripheral.name ?? "unknown")")
|
|
1880
|
-
if let original = self.resourceSession, original.matches(self, peripheral), original.connectionFailedElapsedMs != nil {
|
|
1881
|
-
original.transportConnected(self, peripheral)
|
|
1882
|
-
self.centralManager?.cancelPeripheralConnection(peripheral)
|
|
1883
|
-
return
|
|
1884
|
-
}
|
|
1885
1666
|
guard self.peripheral === peripheral else { return }
|
|
1886
|
-
self.resourceSession?.transportConnected(self, peripheral)
|
|
1887
|
-
guard NimoResourceFixture.shared.allowsProducer(self) else {
|
|
1888
|
-
if self.resourceSession?.releaseNonce != nil { self.resourceSession?.disconnectRequested(self) }
|
|
1889
|
-
self.centralManager?.cancelPeripheralConnection(peripheral); return
|
|
1890
|
-
}
|
|
1891
1667
|
self.writes.connected(peripheral)
|
|
1892
1668
|
self.lastDeviceUUID = peripheral.identifier.uuidString
|
|
1893
1669
|
peripheral.discoverServices([NimoBLE.SERVICE_UUID])
|
|
@@ -1897,14 +1673,12 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1897
1673
|
nonisolated func centralManager(
|
|
1898
1674
|
_: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?
|
|
1899
1675
|
) {
|
|
1900
|
-
|
|
1901
|
-
self.postCallback { [weak self] in
|
|
1676
|
+
DispatchQueue.main.async { [weak self] in
|
|
1902
1677
|
guard let self else { return }
|
|
1903
1678
|
Bridge.log(
|
|
1904
1679
|
"NIMO: Failed to connect \(peripheral.name ?? "unknown"): \(error?.localizedDescription ?? "unknown")"
|
|
1905
1680
|
)
|
|
1906
1681
|
guard self.peripheral === peripheral else { return }
|
|
1907
|
-
self.resourceSession?.connectionFailed(self, peripheral, receivedElapsedMs: receivedElapsedMs)
|
|
1908
1682
|
self.writes.connectionClosed(peripheral)
|
|
1909
1683
|
self.peripheral = nil
|
|
1910
1684
|
self.startReconnectionTimer()
|
|
@@ -1914,13 +1688,9 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1914
1688
|
nonisolated func centralManager(
|
|
1915
1689
|
_: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?
|
|
1916
1690
|
) {
|
|
1917
|
-
|
|
1918
|
-
self.postCallback { [weak self] in
|
|
1691
|
+
DispatchQueue.main.async { [weak self] in
|
|
1919
1692
|
guard let self else { return }
|
|
1920
1693
|
Bridge.log("NIMO: Disconnected: \(error?.localizedDescription ?? "clean")")
|
|
1921
|
-
if let original = self.resourceSession, original.matches(self, peripheral) {
|
|
1922
|
-
original.disconnected(self, peripheral, error: error?.localizedDescription, receivedElapsedMs: receivedElapsedMs)
|
|
1923
|
-
}
|
|
1924
1694
|
self.writes.connectionClosed(peripheral)
|
|
1925
1695
|
if self.isDisconnecting || self.peripheral !== peripheral { return }
|
|
1926
1696
|
|
|
@@ -1930,7 +1700,7 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1930
1700
|
self.micChar = nil
|
|
1931
1701
|
self.resetSessionState()
|
|
1932
1702
|
|
|
1933
|
-
|
|
1703
|
+
if (DeviceManager.shared.sgc as? Nimo) === self {
|
|
1934
1704
|
DeviceStore.shared.apply("glasses", "connected", false)
|
|
1935
1705
|
DeviceStore.shared.apply("glasses", "fullyBooted", false)
|
|
1936
1706
|
DeviceStore.shared.apply("glasses", "connectionState", ConnTypes.DISCONNECTED)
|
|
@@ -1944,8 +1714,8 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1944
1714
|
|
|
1945
1715
|
extension Nimo: CBPeripheralDelegate {
|
|
1946
1716
|
nonisolated func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
|
1947
|
-
|
|
1948
|
-
guard let self, self.peripheral === peripheral
|
|
1717
|
+
DispatchQueue.main.async { [weak self] in
|
|
1718
|
+
guard let self, self.peripheral === peripheral else { return }
|
|
1949
1719
|
guard error == nil, let service = peripheral.services?.first(where: { $0.uuid == NimoBLE.SERVICE_UUID }) else {
|
|
1950
1720
|
self.abortTransport("NIMO service discovery failed")
|
|
1951
1721
|
return
|
|
@@ -1961,9 +1731,9 @@ extension Nimo: CBPeripheralDelegate {
|
|
|
1961
1731
|
let characteristics = service.characteristics
|
|
1962
1732
|
else { return }
|
|
1963
1733
|
|
|
1964
|
-
|
|
1734
|
+
DispatchQueue.main.async { [weak self] in
|
|
1965
1735
|
guard let self else { return }
|
|
1966
|
-
guard self.peripheral === peripheral
|
|
1736
|
+
guard self.peripheral === peripheral else { return }
|
|
1967
1737
|
guard error == nil else { self.abortTransport("NIMO characteristic discovery failed"); return }
|
|
1968
1738
|
for char in characteristics {
|
|
1969
1739
|
switch char.uuid {
|
|
@@ -1993,9 +1763,9 @@ extension Nimo: CBPeripheralDelegate {
|
|
|
1993
1763
|
_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic,
|
|
1994
1764
|
error: Error?
|
|
1995
1765
|
) {
|
|
1996
|
-
|
|
1766
|
+
DispatchQueue.main.async { [weak self] in
|
|
1997
1767
|
guard let self else { return }
|
|
1998
|
-
guard self.peripheral === peripheral
|
|
1768
|
+
guard self.peripheral === peripheral else { return }
|
|
1999
1769
|
if let error {
|
|
2000
1770
|
self.abortTransport("Notification subscription failed")
|
|
2001
1771
|
Bridge.log("NIMO: notify enable failed: \(error.localizedDescription)")
|
|
@@ -2014,14 +1784,11 @@ extension Nimo: CBPeripheralDelegate {
|
|
|
2014
1784
|
nonisolated func peripheral(
|
|
2015
1785
|
_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?
|
|
2016
1786
|
) {
|
|
2017
|
-
let receivedElapsedMs = NimoResourceClock.now()
|
|
2018
1787
|
guard let data = characteristic.value, error == nil else { return }
|
|
2019
1788
|
let uuid = characteristic.uuid
|
|
2020
|
-
|
|
1789
|
+
DispatchQueue.main.async { [weak self] in
|
|
2021
1790
|
guard let self else { return }
|
|
2022
1791
|
guard self.peripheral === peripheral else { return }
|
|
2023
|
-
self.resourceNotificationElapsedMs = receivedElapsedMs
|
|
2024
|
-
defer { self.resourceNotificationElapsedMs = nil }
|
|
2025
1792
|
if uuid == NimoBLE.CHAR_RX, characteristic === self.rxChar {
|
|
2026
1793
|
self.handleRxPacket(data)
|
|
2027
1794
|
} else if uuid == NimoBLE.CHAR_MIC, characteristic === self.micChar {
|
|
@@ -2033,7 +1800,7 @@ extension Nimo: CBPeripheralDelegate {
|
|
|
2033
1800
|
nonisolated func peripheral(
|
|
2034
1801
|
_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?
|
|
2035
1802
|
) {
|
|
2036
|
-
|
|
1803
|
+
DispatchQueue.main.async { [weak self] in
|
|
2037
1804
|
self?.writes.written(peripheral, characteristic: characteristic, success: error == nil)
|
|
2038
1805
|
}
|
|
2039
1806
|
}
|