@mentra/bluetooth-sdk 3.3.0-dev.943 → 3.4.0-dev.1003
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 +61 -0
- package/README.md +78 -0
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +274 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +13 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +8 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +79 -8
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceStore.kt +21 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +1 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +22 -4
- package/android/src/main/java/com/mentra/bluetoothsdk/TouchGesture.kt +19 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/connection/ConnectionModels.kt +5 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicy.kt +64 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +832 -134
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +227 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisement.kt +19 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicy.kt +94 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachine.kt +411 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateStore.kt +124 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +337 -39
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +8 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasEncoder.kt +8 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +1 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +50 -0
- package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +16 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/DeviceManagerOwnerLossTest.kt +146 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusAudioSetupTest.kt +51 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusClassicAudioTest.kt +2 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/TouchGestureCommandTest.kt +112 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/WifiAdbPeerIntentTest.kt +19 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicyTest.kt +65 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveAudioSetupReadinessTest.kt +231 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +368 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisementParserTest.kt +20 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicyTest.kt +136 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachineTest.kt +346 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +564 -1
- package/build/BluetoothSdk.types.d.ts +110 -2
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +6 -2
- 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 +3 -0
- package/build/generated/changelogCatalog.d.ts.map +1 -1
- package/build/generated/changelogCatalog.js +4 -0
- 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 +4 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +24 -4
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/Bridge.swift +11 -3
- package/ios/Source/DeviceManager.swift +103 -7
- package/ios/Source/DeviceStore.swift +27 -0
- package/ios/Source/GeneratedChangelogCatalog.swift +1 -0
- package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
- package/ios/Source/MentraBluetoothSDK.swift +41 -7
- package/ios/Source/TouchGesture.swift +13 -0
- package/ios/Source/errors/BluetoothSdkError.swift +5 -1
- package/ios/Source/events/BluetoothEvents.swift +4 -0
- package/ios/Source/internal/BleTraceLogger.swift +9 -6
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +181 -0
- package/ios/Source/sgcs/MentraLive.swift +1196 -173
- package/ios/Source/sgcs/MentraLivePairingAdvertisement.swift +260 -1
- package/ios/Source/sgcs/MentraLivePairingStateMachine.swift +385 -0
- package/ios/Source/sgcs/MentraLivePairingStateStore.swift +121 -0
- package/ios/Source/sgcs/Nimo.swift +278 -38
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +77 -18
- package/ios/Source/sgcs/NimoCanvasSession.swift +1 -0
- package/ios/Source/sgcs/NimoMacResourceTransport.swift +217 -0
- package/ios/Source/sgcs/NimoResourceFixture.swift +384 -0
- package/ios/Source/sgcs/NimoResourceSession.swift +406 -0
- package/ios/Source/sgcs/SGCManager.swift +7 -0
- package/ios/Source/sgcs/Simulated.swift +2 -0
- package/ios/Source/status/DeviceStatus.swift +35 -0
- package/ios/Source/types/DeviceModels.swift +8 -1
- package/ios/Source/utils/AudioSessionMonitor.swift +158 -5
- package/ios/Tests/BluetoothAudioRouteTests.swift +82 -0
- package/ios/Tests/DeviceManagerReconnectTests.swift +75 -2
- package/ios/Tests/GlassesAudioSetupPolicyTests.swift +191 -0
- package/ios/Tests/MentraLiveAudioSetupTests.swift +215 -0
- package/ios/Tests/MentraLiveConnectionOptionsTests.swift +55 -0
- package/ios/Tests/MentraLivePairingAdvertisementTests.swift +327 -0
- package/ios/Tests/MentraLivePairingStateMachineTests.swift +402 -0
- package/ios/Tests/MentraLiveSessionReadinessTests.swift +328 -26
- package/ios/Tests/NimoCanvasTransportTests.swift +147 -0
- package/ios/Tests/NimoResourceSessionTests.swift +476 -0
- package/ios/Tests/TouchGestureCommandTests.swift +87 -0
- package/ios/Tests/WifiAdbDispatchTests.swift +118 -0
- package/package.json +3 -2
- package/src/BluetoothSdk.types.ts +115 -1
- package/src/_private/BluetoothSdkModule.ts +7 -1
- package/src/generated/changelogCatalog.ts +4 -0
- package/src/generated/releaseMetadata.ts +6 -6
- package/src/index.ts +12 -0
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Darwin
|
|
3
|
+
|
|
4
|
+
/// All mutation is serialized on Main by the native fixture/SDK boundary.
|
|
5
|
+
/// Epoch clocks are deliberately absent: host freshness has a separate authority.
|
|
6
|
+
enum NimoResourceClock {
|
|
7
|
+
static func now() -> UInt64 {
|
|
8
|
+
var info = mach_timebase_info_data_t()
|
|
9
|
+
mach_timebase_info(&info)
|
|
10
|
+
let ticks = mach_continuous_time()
|
|
11
|
+
let divisor = UInt64(info.denom) * 1_000_000
|
|
12
|
+
return (ticks / divisor) * UInt64(info.numer) + (ticks % divisor) * UInt64(info.numer) / divisor
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/// Shared product state belongs to the current manager, independently of the
|
|
17
|
+
/// retained original object's cleanup and closure evidence.
|
|
18
|
+
enum NimoResourceManagerState {
|
|
19
|
+
static func allowsReplacement(fixtureEnabled: Bool, originalClosed: Bool) -> Bool {
|
|
20
|
+
!fixtureEnabled || originalClosed
|
|
21
|
+
}
|
|
22
|
+
static func publish(owner: AnyObject, current: AnyObject?, _ update: () -> Void) {
|
|
23
|
+
guard current === owner else { return }
|
|
24
|
+
update()
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Queue tickets freeze dispatch scope before DeviceManager's asynchronous work.
|
|
29
|
+
/// All entries fence initial arm; only original NIMO entries belong to its drain.
|
|
30
|
+
final class NimoResourceManagerEntries {
|
|
31
|
+
final class Ticket {
|
|
32
|
+
fileprivate weak var owner: NimoResourceManagerEntries?
|
|
33
|
+
fileprivate let nimo: Bool
|
|
34
|
+
fileprivate var ended = false
|
|
35
|
+
fileprivate init(owner: NimoResourceManagerEntries, nimo: Bool) { self.owner = owner; self.nimo = nimo }
|
|
36
|
+
}
|
|
37
|
+
private(set) var all = 0
|
|
38
|
+
private(set) var originalNimo = 0
|
|
39
|
+
func begin(_ wearable: String) -> Ticket {
|
|
40
|
+
let nimo = wearable.contains("NIMO")
|
|
41
|
+
all += 1
|
|
42
|
+
if nimo { originalNimo += 1 }
|
|
43
|
+
return Ticket(owner: self, nimo: nimo)
|
|
44
|
+
}
|
|
45
|
+
func end(_ ticket: Ticket) {
|
|
46
|
+
guard ticket.owner === self, !ticket.ended else { return }
|
|
47
|
+
ticket.ended = true; all -= 1
|
|
48
|
+
if ticket.nimo { originalNimo -= 1 }
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
struct NimoResourceOriginal: Codable, Equatable {
|
|
53
|
+
let instanceId: String
|
|
54
|
+
let peripheralUuid: String
|
|
55
|
+
let sessionGeneration: UInt64
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
struct NimoResourceProcess: Codable, Equatable {
|
|
59
|
+
let pid: Int32
|
|
60
|
+
let instanceId: String
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
struct NimoResourcePair: Codable, Equatable {
|
|
64
|
+
let name: String
|
|
65
|
+
let peripheralUuid: String
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// Accepted selection remains immutable even after connected-service discovery.
|
|
69
|
+
struct NimoResourceSelection: Codable, Equatable {
|
|
70
|
+
static let connectedService = "connected-service-exact-name-v1"
|
|
71
|
+
let name: String
|
|
72
|
+
let peripheralUuid: String?
|
|
73
|
+
let peripheralSelection: String?
|
|
74
|
+
init(name: String, peripheralUuid: String? = nil, peripheralSelection: String? = nil) { self.name = name; self.peripheralUuid = peripheralUuid; self.peripheralSelection = peripheralSelection }
|
|
75
|
+
init(pair: NimoResourcePair) { name = pair.name; peripheralUuid = pair.peripheralUuid; peripheralSelection = nil }
|
|
76
|
+
var dictionary: [String: Any] {
|
|
77
|
+
if let peripheralUuid { return ["name": name, "peripheralUuid": peripheralUuid] }
|
|
78
|
+
return ["name": name, "peripheralSelection": peripheralSelection ?? ""]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// Pure candidate ledger. Selection is committed only once, after the bounded
|
|
83
|
+
/// observer has stopped; a later callback cannot replace its physical identity.
|
|
84
|
+
final class NimoResourceDiscovery {
|
|
85
|
+
static let serviceUuid = "00007033-0000-1000-8000-00805f9b34fb"
|
|
86
|
+
let name: String
|
|
87
|
+
let startedElapsedMs: UInt64
|
|
88
|
+
private(set) var settledElapsedMs: UInt64?
|
|
89
|
+
private(set) var selectedPeripheralUuid: String?
|
|
90
|
+
private(set) var candidates: [String: String] = [:]
|
|
91
|
+
private var currentlyConnected: Set<String> = []
|
|
92
|
+
private(set) var errors: [String] = []
|
|
93
|
+
private(set) var cancelled = false
|
|
94
|
+
init(name: String, startedElapsedMs: UInt64) { self.name = name; self.startedElapsedMs = startedElapsedMs }
|
|
95
|
+
func observe(uuid: String, name: String?) {
|
|
96
|
+
guard settledElapsedMs == nil, !cancelled, name == self.name else { return }
|
|
97
|
+
guard UUID(uuidString: uuid)?.uuidString.lowercased() == uuid else { recordError("invalid-discovery-uuid"); return }
|
|
98
|
+
if candidates.count >= 16, candidates[uuid] == nil { recordError("discovery-candidate-budget"); return }
|
|
99
|
+
candidates[uuid] = name
|
|
100
|
+
currentlyConnected.insert(uuid)
|
|
101
|
+
if candidates.count > 1 { recordError("ambiguous-connected-service-peer") }
|
|
102
|
+
}
|
|
103
|
+
func observeConnectedSnapshot(_ peers: [(String, String?)]) {
|
|
104
|
+
guard settledElapsedMs == nil, !cancelled else { return }
|
|
105
|
+
currentlyConnected.removeAll()
|
|
106
|
+
for (uuid, name) in peers { observe(uuid: uuid, name: name) }
|
|
107
|
+
}
|
|
108
|
+
func recordError(_ error: String) { guard settledElapsedMs == nil else { return }; if errors.count < 16, !errors.contains(error) { errors.append(error) } }
|
|
109
|
+
func cancel() { guard settledElapsedMs == nil else { return }; cancelled = true }
|
|
110
|
+
func finish(at: UInt64, callbacksPending: Int) {
|
|
111
|
+
guard settledElapsedMs == nil, callbacksPending == 0, at >= startedElapsedMs else { return }
|
|
112
|
+
if !cancelled, errors.isEmpty, candidates.count == 1, currentlyConnected.count == 1 { selectedPeripheralUuid = candidates.keys.first }
|
|
113
|
+
else if !cancelled, currentlyConnected.isEmpty { recordError("no-connected-service-peer") }
|
|
114
|
+
settledElapsedMs = at
|
|
115
|
+
}
|
|
116
|
+
func dictionary(callbacksPending: Int) -> [String: Any] {
|
|
117
|
+
["kind": NimoResourceSelection.connectedService, "selectedPeripheralUuid": nullable(selectedPeripheralUuid),
|
|
118
|
+
"candidates": candidates.keys.sorted().map { ["peripheralUuid": $0, "name": candidates[$0]!, "source": "connected-service", "serviceUuid": Self.serviceUuid] },
|
|
119
|
+
"startedElapsedMs": startedElapsedMs, "settledElapsedMs": nullable(settledElapsedMs),
|
|
120
|
+
"settled": settledElapsedMs != nil && callbacksPending == 0, "callbacksPending": callbacksPending, "errors": errors]
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
struct NimoResourceRequest: Codable, Equatable {
|
|
125
|
+
enum Operation: String, Codable { case discover, arm, status, release, cancel }
|
|
126
|
+
let version: Int
|
|
127
|
+
let operation: Operation
|
|
128
|
+
let requestNonce: String
|
|
129
|
+
let captureNonce: String
|
|
130
|
+
let planSha256: String
|
|
131
|
+
let grantSha256: String
|
|
132
|
+
let expectedProcess: NimoResourceProcess
|
|
133
|
+
let expectedPair: NimoResourceSelection
|
|
134
|
+
let original: NimoResourceOriginal?
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
struct NimoResourceProducers {
|
|
138
|
+
var writerActive = 0, writerQueued = 0, encoderWorker = 0, encoderDelivery = 0
|
|
139
|
+
var postedCallbacks = 0, canvasCommands = 0, pendingAcks = 0, audioWorkers = 0
|
|
140
|
+
var scan = 0, timers = 0, reconnectTask = 0
|
|
141
|
+
var dictionary: [String: Int] {
|
|
142
|
+
["writerActive": writerActive, "writerQueued": writerQueued, "encoderWorker": encoderWorker,
|
|
143
|
+
"encoderDelivery": encoderDelivery, "postedCallbacks": postedCallbacks, "canvasCommands": canvasCommands,
|
|
144
|
+
"pendingAcks": pendingAcks, "audioWorkers": audioWorkers, "scan": scan, "timers": timers, "reconnectTask": reconnectTask]
|
|
145
|
+
}
|
|
146
|
+
var settled: Bool { dictionary.values.allSatisfy { $0 == 0 } }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/// Retains the ORIGINAL object identities even after cancellation or timeout.
|
|
150
|
+
/// A later object, generation, peripheral or release nonce cannot be adopted.
|
|
151
|
+
final class NimoResourceSession {
|
|
152
|
+
let captureNonce: String
|
|
153
|
+
let pair: NimoResourcePair
|
|
154
|
+
let armedElapsedMs: UInt64
|
|
155
|
+
private(set) var admittedElapsedMs: UInt64?
|
|
156
|
+
private(set) var instanceId: String?
|
|
157
|
+
private(set) var original: NimoResourceOriginal?
|
|
158
|
+
private(set) var cancelled = false
|
|
159
|
+
private(set) var connected = false
|
|
160
|
+
private var everConnected = false
|
|
161
|
+
private(set) var connectionFailedElapsedMs: UInt64?
|
|
162
|
+
private(set) var startedElapsedMs: UInt64?
|
|
163
|
+
private(set) var twsConnected = false
|
|
164
|
+
private(set) var twsObservedElapsedMs: UInt64?
|
|
165
|
+
private(set) var handshakeTwsElapsedMs: UInt64?
|
|
166
|
+
private(set) var timeCommandId: String?
|
|
167
|
+
private(set) var timeWriteStartedElapsedMs: UInt64?
|
|
168
|
+
private(set) var timeWriteCompletedElapsedMs: UInt64?
|
|
169
|
+
private(set) var timeAckElapsedMs: UInt64?
|
|
170
|
+
private(set) var timeAckStatusCode: Int?
|
|
171
|
+
private(set) var releaseNonce: String?
|
|
172
|
+
private(set) var releaseStartedElapsedMs: UInt64?
|
|
173
|
+
private(set) var micStopRequired = false
|
|
174
|
+
private(set) var displayExitRequired = false
|
|
175
|
+
private(set) var micStopStartedElapsedMs: UInt64?
|
|
176
|
+
private(set) var micStopWrittenElapsedMs: UInt64?
|
|
177
|
+
private(set) var micStopMarkerElapsedMs: UInt64?
|
|
178
|
+
private(set) var exitStatusCode: Int?
|
|
179
|
+
private(set) var exitAckElapsedMs: UInt64?
|
|
180
|
+
private(set) var disconnectRequestedElapsedMs: UInt64?
|
|
181
|
+
private(set) var disconnectCallbackElapsedMs: UInt64?
|
|
182
|
+
private(set) var disconnectError: String?
|
|
183
|
+
private(set) var noTransport = false
|
|
184
|
+
private(set) var errors: [String] = []
|
|
185
|
+
private var owner: AnyObject?
|
|
186
|
+
private var peripheral: AnyObject?
|
|
187
|
+
private let now: () -> UInt64
|
|
188
|
+
|
|
189
|
+
init(captureNonce: String, pair: NimoResourcePair, now: @escaping () -> UInt64 = NimoResourceClock.now, armedElapsedMs: UInt64? = nil) {
|
|
190
|
+
self.captureNonce = captureNonce; self.pair = pair; self.now = now
|
|
191
|
+
self.armedElapsedMs = armedElapsedMs ?? now()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
func isOwner(_ candidate: AnyObject) -> Bool { owner === candidate }
|
|
195
|
+
var hasOwner: Bool { owner != nil }
|
|
196
|
+
func recordError(_ value: String) { let bounded = String(value.prefix(160)); if errors.count < 16, !errors.contains(bounded) { errors.append(bounded) } }
|
|
197
|
+
|
|
198
|
+
/// Ordinary manager disposal may follow an already accepted original close.
|
|
199
|
+
/// It must not rewrite that physical history or touch a replacement device.
|
|
200
|
+
/// An active or contradicted capture retains the original diagnostic error.
|
|
201
|
+
func shouldRunOrdinaryDisconnect(_ candidate: AnyObject, originalClosed: Bool) -> Bool {
|
|
202
|
+
guard isOwner(candidate) else { return false }
|
|
203
|
+
if originalClosed && settled(producers: NimoResourceProducers()) { return false }
|
|
204
|
+
recordError("ordinary-disconnect-during-capture")
|
|
205
|
+
if releaseNonce != nil { disconnectRequested(candidate) }
|
|
206
|
+
return true
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/// Called before scanning/connecting, not from a status response.
|
|
210
|
+
func admit(_ candidate: AnyObject, instanceId: String, name: String?, persist: (UInt64) -> Bool = { _ in true }) -> Bool {
|
|
211
|
+
guard !cancelled, releaseNonce == nil, original == nil, name == nil || name == pair.name,
|
|
212
|
+
owner == nil || owner === candidate else { return false }
|
|
213
|
+
if owner == nil {
|
|
214
|
+
let proposed = now()
|
|
215
|
+
owner = candidate; self.instanceId = instanceId
|
|
216
|
+
guard persist(proposed) else { cancelled = true; recordError("admission-persist-failed"); return false }
|
|
217
|
+
admittedElapsedMs = proposed
|
|
218
|
+
}
|
|
219
|
+
return true
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
func allowsEntry(_ candidate: AnyObject) -> Bool {
|
|
223
|
+
owner === candidate && !cancelled && releaseNonce == nil && original == nil
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// The plan pins a CoreBluetooth UUID; it is never constructed from a MAC.
|
|
227
|
+
func beginTransport(_ candidate: AnyObject, peripheral: AnyObject, uuid: String, persist: (UInt64) -> Bool = { _ in true }) -> Bool {
|
|
228
|
+
guard allowsEntry(candidate), uuid.lowercased() == pair.peripheralUuid.lowercased(), let instanceId else { return false }
|
|
229
|
+
let proposed = now()
|
|
230
|
+
guard persist(proposed) else { cancelled = true; recordError("transport-persist-failed"); return false }
|
|
231
|
+
self.peripheral = peripheral
|
|
232
|
+
original = NimoResourceOriginal(instanceId: instanceId, peripheralUuid: uuid.lowercased(), sessionGeneration: 1)
|
|
233
|
+
startedElapsedMs = proposed
|
|
234
|
+
return true
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
func matches(_ candidate: AnyObject, _ peer: AnyObject) -> Bool { owner === candidate && peripheral === peer }
|
|
238
|
+
func mayReceive(_ candidate: AnyObject, _ peer: AnyObject) -> Bool { matches(candidate, peer) && disconnectCallbackElapsedMs == nil && connectionFailedElapsedMs == nil }
|
|
239
|
+
func transportConnected(_ candidate: AnyObject, _ peer: AnyObject) {
|
|
240
|
+
guard matches(candidate, peer) else { return }
|
|
241
|
+
everConnected = true; connected = true
|
|
242
|
+
if connectionFailedElapsedMs != nil { recordError("connect-after-failed-attempt"); return }
|
|
243
|
+
}
|
|
244
|
+
/// The original failed-attempt callback proves this never-connected transport ended.
|
|
245
|
+
func connectionFailed(_ candidate: AnyObject, _ peer: AnyObject, receivedElapsedMs: UInt64) {
|
|
246
|
+
guard matches(candidate, peer), !everConnected, let startedElapsedMs, receivedElapsedMs >= startedElapsedMs else { return }
|
|
247
|
+
if connectionFailedElapsedMs == nil { connectionFailedElapsedMs = receivedElapsedMs }
|
|
248
|
+
connected = false
|
|
249
|
+
}
|
|
250
|
+
func observeTws(_ candidate: AnyObject, connected: Bool, receivedElapsedMs: UInt64? = nil) {
|
|
251
|
+
guard owner === candidate, releaseNonce == nil, self.connected else { return }
|
|
252
|
+
let received = receivedElapsedMs ?? now()
|
|
253
|
+
guard let startedElapsedMs, received >= startedElapsedMs else { return }
|
|
254
|
+
twsConnected = connected; twsObservedElapsedMs = received
|
|
255
|
+
}
|
|
256
|
+
/// A setup timeout after the release fence is an abandoned handshake, not
|
|
257
|
+
/// a new failure or authority to disconnect/retry the original transport.
|
|
258
|
+
func recordHandshakeFailure(_ candidate: AnyObject) -> Bool {
|
|
259
|
+
guard owner === candidate, releaseNonce == nil else { return false }
|
|
260
|
+
recordError("handshake-failed")
|
|
261
|
+
return true
|
|
262
|
+
}
|
|
263
|
+
func beginTimeCommand(_ candidate: AnyObject) -> String? {
|
|
264
|
+
guard owner === candidate, releaseNonce == nil, connected, twsConnected, let twsObservedElapsedMs else { return nil }
|
|
265
|
+
if handshakeTwsElapsedMs == nil { handshakeTwsElapsedMs = twsObservedElapsedMs }
|
|
266
|
+
let id = UUID().uuidString.lowercased(); timeCommandId = id
|
|
267
|
+
timeWriteStartedElapsedMs = nil; timeWriteCompletedElapsedMs = nil; timeAckElapsedMs = nil; timeAckStatusCode = nil
|
|
268
|
+
return id
|
|
269
|
+
}
|
|
270
|
+
func timeWriteStarted(_ candidate: AnyObject, command: String) {
|
|
271
|
+
guard owner === candidate, timeCommandId == command, releaseNonce == nil, timeWriteStartedElapsedMs == nil else { return }
|
|
272
|
+
timeWriteStartedElapsedMs = now()
|
|
273
|
+
}
|
|
274
|
+
func timeWriteCompleted(_ candidate: AnyObject, command: String) {
|
|
275
|
+
guard owner === candidate, timeCommandId == command, releaseNonce == nil, timeWriteStartedElapsedMs != nil, timeWriteCompletedElapsedMs == nil else { return }
|
|
276
|
+
timeWriteCompletedElapsedMs = now()
|
|
277
|
+
}
|
|
278
|
+
func timeAck(_ candidate: AnyObject, command: String, status: Int, receivedElapsedMs: UInt64? = nil) {
|
|
279
|
+
guard owner === candidate, timeCommandId == command, releaseNonce == nil, timeWriteStartedElapsedMs != nil, timeAckElapsedMs == nil else { return }
|
|
280
|
+
let received = receivedElapsedMs ?? now()
|
|
281
|
+
guard let started = timeWriteStartedElapsedMs, received >= started else { return }
|
|
282
|
+
timeAckElapsedMs = received; timeAckStatusCode = status
|
|
283
|
+
}
|
|
284
|
+
var ready: Bool {
|
|
285
|
+
guard !cancelled, releaseNonce == nil, connected, twsConnected,
|
|
286
|
+
let start = startedElapsedMs, let tws = handshakeTwsElapsedMs,
|
|
287
|
+
let written = timeWriteStartedElapsedMs, let completed = timeWriteCompletedElapsedMs,
|
|
288
|
+
let ack = timeAckElapsedMs, timeAckStatusCode == 0 else { return false }
|
|
289
|
+
return start <= tws && tws <= written && written <= completed && written <= ack
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Preserve the genuine release-admission boundary for delivered compiler proof.
|
|
293
|
+
@inline(never)
|
|
294
|
+
func beginRelease(nonce: String, cancel: Bool, mic: Bool, canvas: Bool) -> Bool {
|
|
295
|
+
if let releaseNonce { return releaseNonce == nonce }
|
|
296
|
+
releaseNonce = nonce; releaseStartedElapsedMs = now(); cancelled = true
|
|
297
|
+
micStopRequired = mic; displayExitRequired = canvas
|
|
298
|
+
if cancel { cancelled = true }
|
|
299
|
+
return true
|
|
300
|
+
}
|
|
301
|
+
func micStopStarted(_ candidate: AnyObject) {
|
|
302
|
+
guard owner === candidate, releaseNonce != nil, micStopRequired else { return }; micStopStartedElapsedMs = now()
|
|
303
|
+
}
|
|
304
|
+
func micStopWritten(_ candidate: AnyObject) {
|
|
305
|
+
guard owner === candidate, micStopStartedElapsedMs != nil, micStopWrittenElapsedMs == nil else { return }; micStopWrittenElapsedMs = now()
|
|
306
|
+
}
|
|
307
|
+
func micStopMarker(_ candidate: AnyObject, receivedElapsedMs: UInt64? = nil) {
|
|
308
|
+
guard owner === candidate, let started = micStopStartedElapsedMs, micStopMarkerElapsedMs == nil else { return }
|
|
309
|
+
let received = receivedElapsedMs ?? now()
|
|
310
|
+
guard received >= started else { return }; micStopMarkerElapsedMs = received
|
|
311
|
+
}
|
|
312
|
+
func exitAcknowledged(_ candidate: AnyObject, status: Int, receivedElapsedMs: UInt64? = nil) {
|
|
313
|
+
guard owner === candidate, releaseNonce != nil, displayExitRequired else { return }
|
|
314
|
+
let received = receivedElapsedMs ?? now()
|
|
315
|
+
guard let started = releaseStartedElapsedMs, received >= started else { return }
|
|
316
|
+
exitStatusCode = status; exitAckElapsedMs = received
|
|
317
|
+
}
|
|
318
|
+
var commandsSettled: Bool {
|
|
319
|
+
(!micStopRequired || micStopStartedElapsedMs != nil && micStopWrittenElapsedMs != nil && micStopMarkerElapsedMs != nil) &&
|
|
320
|
+
(!displayExitRequired || exitStatusCode == 0 && exitAckElapsedMs != nil)
|
|
321
|
+
}
|
|
322
|
+
func disconnectRequested(_ candidate: AnyObject) {
|
|
323
|
+
guard owner === candidate, releaseNonce != nil, disconnectRequestedElapsedMs == nil else { return }
|
|
324
|
+
disconnectRequestedElapsedMs = now()
|
|
325
|
+
}
|
|
326
|
+
/// A nil app reference or cancelPeripheralConnection return is NOT closure.
|
|
327
|
+
func disconnected(_ candidate: AnyObject, _ peer: AnyObject, error: String?, receivedElapsedMs: UInt64? = nil) {
|
|
328
|
+
guard matches(candidate, peer) else { return }
|
|
329
|
+
connected = false
|
|
330
|
+
guard disconnectRequestedElapsedMs != nil else { recordError("unexpected-disconnect"); return }
|
|
331
|
+
let received = receivedElapsedMs ?? now()
|
|
332
|
+
guard let requested = disconnectRequestedElapsedMs, received >= requested else { recordError("pre-request-disconnect"); return }
|
|
333
|
+
if disconnectCallbackElapsedMs == nil { disconnectCallbackElapsedMs = received; disconnectError = error }
|
|
334
|
+
}
|
|
335
|
+
func proveNoTransport() {
|
|
336
|
+
guard releaseNonce != nil, original == nil, peripheral == nil else { return }; noTransport = true
|
|
337
|
+
}
|
|
338
|
+
func settled(producers: NimoResourceProducers) -> Bool {
|
|
339
|
+
releaseNonce != nil && commandsSettled && (noTransport || connectionFailedElapsedMs != nil || disconnectCallbackElapsedMs != nil && disconnectError == nil) && producers.settled && errors.isEmpty
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
func sessionDictionary() -> [String: Any]? {
|
|
343
|
+
guard let instanceId, admittedElapsedMs != nil else { return nil }
|
|
344
|
+
let original = original ?? NimoResourceOriginal(instanceId: instanceId, peripheralUuid: pair.peripheralUuid.lowercased(), sessionGeneration: 0)
|
|
345
|
+
return ["instanceId": original.instanceId, "peripheralUuid": original.peripheralUuid,
|
|
346
|
+
"sessionGeneration": original.sessionGeneration, "startedElapsedMs": nullable(startedElapsedMs),
|
|
347
|
+
"connected": connected, "initializationPending": !ready && releaseNonce == nil,
|
|
348
|
+
"twsConnected": twsConnected, "twsObservedElapsedMs": nullable(twsObservedElapsedMs),
|
|
349
|
+
"handshakeTwsElapsedMs": nullable(handshakeTwsElapsedMs), "timeCommandId": nullable(timeCommandId),
|
|
350
|
+
"timeWriteStartedElapsedMs": nullable(timeWriteStartedElapsedMs), "timeWriteCompletedElapsedMs": nullable(timeWriteCompletedElapsedMs),
|
|
351
|
+
"timeAckElapsedMs": nullable(timeAckElapsedMs), "timeAckStatusCode": nullable(timeAckStatusCode)]
|
|
352
|
+
}
|
|
353
|
+
func releaseDictionary(producers: NimoResourceProducers) -> [String: Any] {
|
|
354
|
+
["nonce": nullable(releaseNonce), "startedElapsedMs": nullable(releaseStartedElapsedMs),
|
|
355
|
+
"micStopRequired": micStopRequired, "displayExitRequired": displayExitRequired,
|
|
356
|
+
"micStopStartedElapsedMs": nullable(micStopStartedElapsedMs), "micStopWrittenElapsedMs": nullable(micStopWrittenElapsedMs),
|
|
357
|
+
"micStopMarkerElapsedMs": nullable(micStopMarkerElapsedMs), "exitStatusCode": nullable(exitStatusCode), "exitAckElapsedMs": nullable(exitAckElapsedMs),
|
|
358
|
+
"disconnectRequestedElapsedMs": nullable(disconnectRequestedElapsedMs), "disconnectCallbackElapsedMs": nullable(disconnectCallbackElapsedMs),
|
|
359
|
+
"disconnectError": nullable(disconnectError), "connectionFailedElapsedMs": nullable(connectionFailedElapsedMs), "noTransport": noTransport, "settled": settled(producers: producers)]
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
func nullable<T>(_ value: T?) -> Any { value.map { $0 as Any } ?? NSNull() }
|
|
364
|
+
|
|
365
|
+
/// Cross-queue roster; queued Main callbacks cannot disappear on logical reset.
|
|
366
|
+
final class NimoResourceCallbackCounter: @unchecked Sendable {
|
|
367
|
+
private let lock = NSLock()
|
|
368
|
+
private var count = 0
|
|
369
|
+
func add(_ amount: Int) { lock.lock(); count += amount; lock.unlock() }
|
|
370
|
+
var value: Int { lock.lock(); defer { lock.unlock() }; return count }
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
extension NimoResourceRequest {
|
|
374
|
+
static func decode(_ bytes: Data) throws -> NimoResourceRequest {
|
|
375
|
+
// JSONDecoder ignores unknown keys; reject them before typed decoding.
|
|
376
|
+
guard bytes.count <= 16_384, let value = try JSONSerialization.jsonObject(with: bytes) as? [String: Any],
|
|
377
|
+
Set(value.keys) == Set(["version","operation","requestNonce","captureNonce","planSha256","grantSha256","expectedProcess","expectedPair","original"]),
|
|
378
|
+
let process = value["expectedProcess"] as? [String: Any], Set(process.keys) == Set(["pid","instanceId"]),
|
|
379
|
+
let pair = value["expectedPair"] as? [String: Any],
|
|
380
|
+
Set(pair.keys) == Set(["name","peripheralUuid"]) || Set(pair.keys) == Set(["name","peripheralSelection"]) else { throw failure("invalid-request-schema") }
|
|
381
|
+
if let original = value["original"] as? [String: Any], Set(original.keys) != Set(["instanceId","peripheralUuid","sessionGeneration"]) { throw failure("invalid-original-schema") }
|
|
382
|
+
let result = try JSONDecoder().decode(NimoResourceRequest.self, from: bytes)
|
|
383
|
+
let ids = [result.requestNonce, result.captureNonce, result.expectedProcess.instanceId] + (result.expectedPair.peripheralUuid.map { [$0] } ?? []) +
|
|
384
|
+
(result.original.map { [$0.instanceId, $0.peripheralUuid] } ?? [])
|
|
385
|
+
for id in ids {
|
|
386
|
+
guard UUID(uuidString: id)?.uuidString.lowercased() == id else { throw failure("invalid-uuid") }
|
|
387
|
+
}
|
|
388
|
+
if result.operation == .discover {
|
|
389
|
+
guard result.expectedPair.peripheralSelection == NimoResourceSelection.connectedService,
|
|
390
|
+
result.requestNonce == result.captureNonce, result.original == nil else { throw failure("invalid-discovery-identity") }
|
|
391
|
+
}
|
|
392
|
+
if result.operation == .arm {
|
|
393
|
+
guard result.original == nil,
|
|
394
|
+
(result.expectedPair.peripheralUuid != nil ? result.requestNonce == result.captureNonce : result.requestNonce != result.captureNonce) else { throw failure("invalid-arm-identity") }
|
|
395
|
+
}
|
|
396
|
+
if let original = result.original {
|
|
397
|
+
guard original.sessionGeneration <= 1, (result.expectedPair.peripheralUuid == nil || original.peripheralUuid == result.expectedPair.peripheralUuid) else { throw failure("invalid-original-identity") }
|
|
398
|
+
}
|
|
399
|
+
guard (result.expectedPair.peripheralUuid != nil && result.expectedPair.peripheralSelection == nil || result.expectedPair.peripheralUuid == nil && result.expectedPair.peripheralSelection == NimoResourceSelection.connectedService),
|
|
400
|
+
result.expectedProcess.pid > 1, result.version == 1, !result.expectedPair.name.isEmpty, result.expectedPair.name.utf8.count <= 128,
|
|
401
|
+
result.planSha256.range(of: "^[0-9a-f]{64}$", options: .regularExpression) != nil,
|
|
402
|
+
result.grantSha256.range(of: "^[0-9a-f]{64}$", options: .regularExpression) != nil else { throw failure("invalid-request-identity") }
|
|
403
|
+
return result
|
|
404
|
+
}
|
|
405
|
+
private static func failure(_ code: String) -> Error { NSError(domain: "NimoResourceFixture", code: 1, userInfo: [NSLocalizedDescriptionKey: code]) }
|
|
406
|
+
}
|
|
@@ -50,6 +50,9 @@ protocol SGCManager {
|
|
|
50
50
|
|
|
51
51
|
var type: String { get set }
|
|
52
52
|
var hasMic: Bool { get }
|
|
53
|
+
/// The driver publishes `GlassesStatus.audioSetup` itself. Otherwise the status layer
|
|
54
|
+
/// reports `not_applicable` while connected and `idle` otherwise.
|
|
55
|
+
var publishesAudioSetup: Bool { get }
|
|
53
56
|
|
|
54
57
|
// MARK: - Audio Control
|
|
55
58
|
|
|
@@ -278,6 +281,10 @@ extension SGCManager {
|
|
|
278
281
|
false
|
|
279
282
|
}
|
|
280
283
|
|
|
284
|
+
var publishesAudioSetup: Bool {
|
|
285
|
+
false
|
|
286
|
+
}
|
|
287
|
+
|
|
281
288
|
func replayStreamControlReady() {}
|
|
282
289
|
|
|
283
290
|
@discardableResult func requestSavedWifiNetworks(requestId _: String, sid _: String) -> Bool {
|
|
@@ -18,6 +18,8 @@ class Simulated: SGCManager {
|
|
|
18
18
|
BluetoothSdkDefaults.voiceActivityDetectionEnabled
|
|
19
19
|
)
|
|
20
20
|
DeviceStore.shared.apply("glasses", "bluetoothClassicConnected", false)
|
|
21
|
+
DeviceStore.shared.apply("glasses", "bluetoothClassicProfilesConnected", false)
|
|
22
|
+
DeviceStore.shared.apply("glasses", "bluetoothAudioRouted", false)
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
// MARK: - Device Information
|
|
@@ -124,6 +124,7 @@ struct GlassesStatus: CustomStringConvertible {
|
|
|
124
124
|
"signalStrength": -1,
|
|
125
125
|
"signalStrengthUpdatedAt": 0,
|
|
126
126
|
"g2MissingArm": NSNull(),
|
|
127
|
+
"audioSetup": GlassesAudioSetupState.idle.toMap(),
|
|
127
128
|
]))
|
|
128
129
|
}
|
|
129
130
|
|
|
@@ -151,6 +152,14 @@ struct GlassesStatus: CustomStringConvertible {
|
|
|
151
152
|
boolValue(values, "bluetoothClassicConnected") ?? false
|
|
152
153
|
}
|
|
153
154
|
|
|
155
|
+
var bluetoothClassicProfilesConnected: Bool {
|
|
156
|
+
boolValue(values, "bluetoothClassicProfilesConnected") ?? false
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
var bluetoothAudioRouted: Bool {
|
|
160
|
+
boolValue(values, "bluetoothAudioRouted") ?? false
|
|
161
|
+
}
|
|
162
|
+
|
|
154
163
|
var signalStrength: Int {
|
|
155
164
|
intValue(values["signalStrength"]) ?? -1
|
|
156
165
|
}
|
|
@@ -302,6 +311,16 @@ struct GlassesStatus: CustomStringConvertible {
|
|
|
302
311
|
intValue(values["ringSignalStrength"]) ?? -1
|
|
303
312
|
}
|
|
304
313
|
|
|
314
|
+
/// Mentra Live `MentraLivePairingState` (`{state, windowId?, endsAtMs?}`); nil for other glasses.
|
|
315
|
+
var pairingState: [String: Any]? {
|
|
316
|
+
values["pairingState"] as? [String: Any]
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/// `GlassesAudioSetupState` (`{state, action?}`). Every model publishes it.
|
|
320
|
+
var audioSetup: [String: Any]? {
|
|
321
|
+
values["audioSetup"] as? [String: Any]
|
|
322
|
+
}
|
|
323
|
+
|
|
305
324
|
var description: String {
|
|
306
325
|
values.description
|
|
307
326
|
}
|
|
@@ -688,6 +707,14 @@ struct GlassesStatusUpdate: CustomStringConvertible {
|
|
|
688
707
|
optionalBoolValue(values, "bluetoothClassicConnected")
|
|
689
708
|
}
|
|
690
709
|
|
|
710
|
+
var bluetoothClassicProfilesConnected: Bool? {
|
|
711
|
+
optionalBoolValue(values, "bluetoothClassicProfilesConnected")
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
var bluetoothAudioRouted: Bool? {
|
|
715
|
+
optionalBoolValue(values, "bluetoothAudioRouted")
|
|
716
|
+
}
|
|
717
|
+
|
|
691
718
|
var signalStrength: Int? {
|
|
692
719
|
optionalIntValue(values, "signalStrength")
|
|
693
720
|
}
|
|
@@ -848,6 +875,14 @@ struct GlassesStatusUpdate: CustomStringConvertible {
|
|
|
848
875
|
optionalIntValue(values, "ringSignalStrength")
|
|
849
876
|
}
|
|
850
877
|
|
|
878
|
+
var pairingState: [String: Any]? {
|
|
879
|
+
values["pairingState"] as? [String: Any]
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
var audioSetup: [String: Any]? {
|
|
883
|
+
values["audioSetup"] as? [String: Any]
|
|
884
|
+
}
|
|
885
|
+
|
|
851
886
|
var description: String {
|
|
852
887
|
values.description
|
|
853
888
|
}
|
|
@@ -172,14 +172,21 @@ public struct ConnectOptions {
|
|
|
172
172
|
/// Mentra Live on iOS only. Whether CoreBluetooth should require ANCS
|
|
173
173
|
/// authorization while establishing the connection.
|
|
174
174
|
public let requiresAncs: Bool
|
|
175
|
+
/// Mentra Live on iOS only. Ask CoreBluetooth to bring up the Classic
|
|
176
|
+
/// profiles (A2DP/HFP) after the BLE connection. On by default: iOS derives
|
|
177
|
+
/// the Classic key at the BLE bond but never starts Classic, and the glasses
|
|
178
|
+
/// commit `no_classic`, so pairing waits forever for an audio route without it.
|
|
179
|
+
public let enableTransportBridging: Bool
|
|
175
180
|
|
|
176
181
|
public init(
|
|
177
182
|
saveAsDefault: Bool = true,
|
|
178
183
|
cancelExistingConnectionAttempt: Bool = true,
|
|
179
|
-
requiresAncs: Bool = true
|
|
184
|
+
requiresAncs: Bool = true,
|
|
185
|
+
enableTransportBridging: Bool = true
|
|
180
186
|
) {
|
|
181
187
|
self.saveAsDefault = saveAsDefault
|
|
182
188
|
self.cancelExistingConnectionAttempt = cancelExistingConnectionAttempt
|
|
183
189
|
self.requiresAncs = requiresAncs
|
|
190
|
+
self.enableTransportBridging = enableTransportBridging
|
|
184
191
|
}
|
|
185
192
|
}
|