@mentra/bluetooth-sdk 3.4.0-dev.1208 → 3.4.0-dev.1209
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/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +2 -16
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +2 -9
- 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/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
- package/build/_private/BluetoothSdkModule.d.ts +0 -3
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/generated/releaseMetadata.js +5 -5
- package/build/generated/releaseMetadata.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +0 -6
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/DeviceManager.swift +1 -10
- package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
- package/ios/Source/MentraBluetoothSDK.swift +2 -15
- 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/Tests/DeviceManagerReconnectTests.swift +0 -71
- package/ios/Tests/NimoCanvasTransportTests.swift +0 -89
- package/package.json +2 -2
- package/src/_private/BluetoothSdkModule.ts +0 -3
- package/src/generated/releaseMetadata.ts +5 -5
- 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/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
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
#if os(macOS) || os(iOS)
|
|
3
|
-
import CryptoKit
|
|
4
|
-
import Darwin
|
|
5
|
-
|
|
6
|
-
struct NimoMacProcessBirth: Codable, Equatable {
|
|
7
|
-
let seconds: UInt64
|
|
8
|
-
let microseconds: UInt64
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
struct NimoMacFixtureConfiguration: Codable {
|
|
12
|
-
let version: Int
|
|
13
|
-
let ownerPid: Int32
|
|
14
|
-
let ownerAuditToken: String
|
|
15
|
-
let ownerBirth: NimoMacProcessBirth
|
|
16
|
-
let planSha256: String
|
|
17
|
-
let grantSha256: String
|
|
18
|
-
let bootstrapNonce: String
|
|
19
|
-
let appExecutableSha256: String
|
|
20
|
-
let appBundleId: String
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/// Private, one-owner fixture IPC. Only typed lifecycle operations reach Main.
|
|
24
|
-
/// No socket path, packet, host address or connection command is caller supplied.
|
|
25
|
-
final class NimoMacResourceTransport {
|
|
26
|
-
let configuration: NimoMacFixtureConfiguration
|
|
27
|
-
private let process: NimoResourceProcess
|
|
28
|
-
private let directory: String
|
|
29
|
-
private let directoryFd: Int32
|
|
30
|
-
private let directoryDevice: UInt64
|
|
31
|
-
private let directoryInode: UInt64
|
|
32
|
-
private let socketFd: Int32
|
|
33
|
-
private let socketPath: String
|
|
34
|
-
private let processAuditToken: String
|
|
35
|
-
private let executableSha256: String
|
|
36
|
-
private let queue = DispatchQueue(label: "NimoResourceFixtureSocket")
|
|
37
|
-
|
|
38
|
-
init(process: NimoResourceProcess) throws {
|
|
39
|
-
#if os(iOS)
|
|
40
|
-
guard ProcessInfo.processInfo.isiOSAppOnMac else { throw transportError("fixture-requires-ios-on-mac") }
|
|
41
|
-
#endif
|
|
42
|
-
guard let text = ProcessInfo.processInfo.environment["MENTRA_NIMO_RESOURCE_BOOTSTRAP"], text.utf8.count <= 4096,
|
|
43
|
-
let bytes = text.data(using: .utf8), let object = try JSONSerialization.jsonObject(with: bytes) as? [String: Any],
|
|
44
|
-
Set(object.keys) == Set(["version","ownerPid","ownerAuditToken","ownerBirth","planSha256","grantSha256","bootstrapNonce","appExecutableSha256","appBundleId"]),
|
|
45
|
-
let birth = object["ownerBirth"] as? [String: Any], Set(birth.keys) == Set(["seconds","microseconds"]) else { throw transportError("invalid-bootstrap") }
|
|
46
|
-
configuration = try JSONDecoder().decode(NimoMacFixtureConfiguration.self, from: bytes)
|
|
47
|
-
guard configuration.version == 1, configuration.ownerPid > 1,
|
|
48
|
-
configuration.ownerPid != process.pid,
|
|
49
|
-
UUID(uuidString: configuration.bootstrapNonce)?.uuidString.lowercased() == configuration.bootstrapNonce,
|
|
50
|
-
[configuration.planSha256, configuration.grantSha256, configuration.appExecutableSha256, configuration.ownerAuditToken].allSatisfy({ $0.range(of: "^[0-9a-f]{64}$", options: .regularExpression) != nil }),
|
|
51
|
-
Bundle.main.bundleIdentifier == configuration.appBundleId,
|
|
52
|
-
let executable = Bundle.main.executableURL else { throw transportError("bootstrap-identity-mismatch") }
|
|
53
|
-
let executableBytes = try Data(contentsOf: executable, options: [.mappedIfSafe])
|
|
54
|
-
guard executableBytes.count <= 256 * 1024 * 1024 else { throw transportError("executable-budget") }
|
|
55
|
-
executableSha256 = SHA256.hash(data: executableBytes).map { String(format: "%02x", $0) }.joined()
|
|
56
|
-
guard executableSha256 == configuration.appExecutableSha256 else { throw transportError("executable-mismatch") }
|
|
57
|
-
self.process = process; processAuditToken = try nimoOwnAuditToken()
|
|
58
|
-
directory = NSHomeDirectory() + "/tmp/nr-" + configuration.bootstrapNonce.replacingOccurrences(of: "-", with: "").prefix(16)
|
|
59
|
-
// Never reuse another process's directory, socket or receipts.
|
|
60
|
-
guard mkdir(directory, 0o700) == 0 else { throw transportError("fixture-directory-exists-or-unavailable") }
|
|
61
|
-
directoryFd = open(directory, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)
|
|
62
|
-
guard directoryFd >= 0 else { throw transportError("fixture-directory-open") }
|
|
63
|
-
var info = stat()
|
|
64
|
-
guard fstat(directoryFd, &info) == 0, info.st_uid == geteuid(), info.st_mode & 0o777 == 0o700 else { throw transportError("fixture-directory-permissions") }
|
|
65
|
-
directoryDevice = UInt64(info.st_dev); directoryInode = UInt64(info.st_ino)
|
|
66
|
-
socketPath = directory + "/s"
|
|
67
|
-
socketFd = socket(AF_UNIX, SOCK_STREAM, 0)
|
|
68
|
-
guard socketFd >= 0 else { throw transportError("fixture-socket-open") }
|
|
69
|
-
var address = sockaddr_un(); address.sun_family = sa_family_t(AF_UNIX)
|
|
70
|
-
let pathBytes = Array(socketPath.utf8) + [0]
|
|
71
|
-
guard pathBytes.count <= MemoryLayout.size(ofValue: address.sun_path) else { throw transportError("fixture-socket-path-budget") }
|
|
72
|
-
withUnsafeMutableBytes(of: &address.sun_path) { $0.copyBytes(from: pathBytes) }
|
|
73
|
-
let length = socklen_t(MemoryLayout<sockaddr_un>.size)
|
|
74
|
-
address.sun_len = UInt8(length)
|
|
75
|
-
let result = withUnsafePointer(to: &address) { ptr in ptr.withMemoryRebound(to: sockaddr.self, capacity: 1) { Darwin.bind(socketFd, $0, length) } }
|
|
76
|
-
guard result == 0, chmod(socketPath, 0o600) == 0, listen(socketFd, 4) == 0 else { throw transportError("fixture-socket-bind") }
|
|
77
|
-
let flags = fcntl(socketFd, F_GETFD); _ = fcntl(socketFd, F_SETFD, flags | FD_CLOEXEC)
|
|
78
|
-
let schedulerQoS: DispatchQoS = .unspecified
|
|
79
|
-
let schedulerFlags = DispatchWorkItemFlags(rawValue: 0)
|
|
80
|
-
enqueueServe(qos: schedulerQoS, flags: schedulerFlags)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// A narrow scheduling boundary for this transport's original accept loop.
|
|
84
|
-
@inline(never)
|
|
85
|
-
private func enqueueServe(qos: DispatchQoS, flags: DispatchWorkItemFlags) {
|
|
86
|
-
queue.async(group: nil, qos: qos, flags: flags) { [weak self] in self?.serve() }
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private func sameDirectory() -> Bool {
|
|
90
|
-
var byPath = stat(), byFd = stat()
|
|
91
|
-
return lstat(directory, &byPath) == 0 && fstat(directoryFd, &byFd) == 0 &&
|
|
92
|
-
byPath.st_mode & S_IFMT == S_IFDIR && byPath.st_dev == byFd.st_dev && byPath.st_ino == byFd.st_ino &&
|
|
93
|
-
UInt64(byFd.st_dev) == directoryDevice && UInt64(byFd.st_ino) == directoryInode && byFd.st_uid == geteuid() && byFd.st_mode & 0o777 == 0o700
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
func persist(_ bytes: Data, nonce: String, kind: String) throws {
|
|
97
|
-
guard sameDirectory(), UUID(uuidString: nonce)?.uuidString.lowercased() == nonce,
|
|
98
|
-
["intent","receipt","admission","transport"].contains(kind), bytes.count <= 32_768 else { throw transportError("journal-identity-mismatch") }
|
|
99
|
-
let final = "\(nonce).\(kind).json", temp = "\(nonce).\(kind).pending"
|
|
100
|
-
let fd = openat(directoryFd, temp, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0o600)
|
|
101
|
-
guard fd >= 0 else { throw transportError("journal-intent-exists-or-unavailable") }
|
|
102
|
-
defer { close(fd); unlinkat(directoryFd, temp, 0) }
|
|
103
|
-
try bytes.withUnsafeBytes { raw in
|
|
104
|
-
var offset = 0
|
|
105
|
-
while offset < raw.count {
|
|
106
|
-
let count = Darwin.write(fd, raw.baseAddress!.advanced(by: offset), raw.count - offset)
|
|
107
|
-
guard count > 0 else { throw transportError("journal-write-failed") }; offset += count
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
guard fsync(fd) == 0, sameDirectory(), linkat(directoryFd, temp, directoryFd, final, 0) == 0,
|
|
111
|
-
fsync(directoryFd) == 0 else { throw transportError("journal-atomic-publish-failed") }
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private func peerAllowed(_ fd: Int32) -> Bool {
|
|
115
|
-
var peerPid: Int32 = 0, length = socklen_t(MemoryLayout<Int32>.size)
|
|
116
|
-
var uid: uid_t = 0, gid: gid_t = 0
|
|
117
|
-
var token = audit_token_t(), tokenLength = socklen_t(MemoryLayout<audit_token_t>.size)
|
|
118
|
-
return getsockopt(fd, SOL_LOCAL, LOCAL_PEERPID, &peerPid, &length) == 0 &&
|
|
119
|
-
length == MemoryLayout<Int32>.size && peerPid == configuration.ownerPid && getpeereid(fd, &uid, &gid) == 0 && uid == geteuid() &&
|
|
120
|
-
getsockopt(fd, SOL_LOCAL, LOCAL_PEERTOKEN, &token, &tokenLength) == 0 &&
|
|
121
|
-
tokenLength == MemoryLayout<audit_token_t>.size && nimoAuditTokenHex(token) == configuration.ownerAuditToken && sameDirectory()
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private func serve() {
|
|
125
|
-
while true {
|
|
126
|
-
let fd = accept(socketFd, nil, nil)
|
|
127
|
-
if fd < 0 { if errno == EINTR { continue }; return }
|
|
128
|
-
defer { close(fd) }
|
|
129
|
-
guard peerAllowed(fd) else { continue }
|
|
130
|
-
var timeout = timeval(tv_sec: 5, tv_usec: 0)
|
|
131
|
-
_ = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, socklen_t(MemoryLayout<timeval>.size))
|
|
132
|
-
_ = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, socklen_t(MemoryLayout<timeval>.size))
|
|
133
|
-
var noSignal: Int32 = 1
|
|
134
|
-
_ = setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &noSignal, socklen_t(MemoryLayout<Int32>.size))
|
|
135
|
-
var request = Data(), complete = false
|
|
136
|
-
let readDeadline = NimoResourceClock.now() + 5_000
|
|
137
|
-
while request.count <= 16_384, NimoResourceClock.now() < readDeadline {
|
|
138
|
-
var buffer = [UInt8](repeating: 0, count: 1024)
|
|
139
|
-
let count = read(fd, &buffer, buffer.count)
|
|
140
|
-
if count <= 0 { break }
|
|
141
|
-
request.append(contentsOf: buffer.prefix(count))
|
|
142
|
-
if request.contains(10) { complete = true; break }
|
|
143
|
-
}
|
|
144
|
-
guard complete, request.count <= 16_384, request.last == 10, request.dropLast().contains(10) == false,
|
|
145
|
-
peerAllowed(fd) else { continue }
|
|
146
|
-
do {
|
|
147
|
-
guard let envelope = try JSONSerialization.jsonObject(with: request) as? [String: Any],
|
|
148
|
-
envelope["bootstrapNonce"] as? String == configuration.bootstrapNonce else { throw transportError("bootstrap-capability-mismatch") }
|
|
149
|
-
let response: Data
|
|
150
|
-
if Set(envelope.keys) == Set(["bootstrapNonce","hello"]), envelope["hello"] as? Bool == true {
|
|
151
|
-
response = try JSONSerialization.data(withJSONObject: hello(), options: [.sortedKeys])
|
|
152
|
-
} else {
|
|
153
|
-
guard Set(envelope.keys) == Set(["bootstrapNonce","request"]), let value = envelope["request"] as? [String: Any] else { throw transportError("invalid-envelope") }
|
|
154
|
-
let canonical = try JSONSerialization.data(withJSONObject: value, options: [.sortedKeys])
|
|
155
|
-
let semaphore = DispatchSemaphore(value: 0)
|
|
156
|
-
// Connection execution is serial; parent timeout does not cancel
|
|
157
|
-
// native cleanup or renew its original operation authority.
|
|
158
|
-
final class Box: @unchecked Sendable { var result: Result<Data, Error>? }
|
|
159
|
-
let box = Box()
|
|
160
|
-
Task { @MainActor in
|
|
161
|
-
do { box.result = .success(try await NimoResourceFixture.shared.execute(canonical)) }
|
|
162
|
-
catch { box.result = .failure(error) }
|
|
163
|
-
semaphore.signal()
|
|
164
|
-
}
|
|
165
|
-
semaphore.wait()
|
|
166
|
-
guard peerAllowed(fd), let result = box.result else { continue }
|
|
167
|
-
let receipt = try result.get()
|
|
168
|
-
response = try JSONSerialization.data(withJSONObject: ["receipt": try JSONSerialization.jsonObject(with: receipt)], options: [.sortedKeys])
|
|
169
|
-
}
|
|
170
|
-
send(response + Data([10]), fd)
|
|
171
|
-
} catch {
|
|
172
|
-
let code = String(error.localizedDescription.prefix(160))
|
|
173
|
-
if let bytes = try? JSONSerialization.data(withJSONObject: ["error": code], options: [.sortedKeys]) { send(bytes + Data([10]), fd) }
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
private func send(_ data: Data, _ fd: Int32) {
|
|
178
|
-
data.withUnsafeBytes { bytes in
|
|
179
|
-
var offset = 0
|
|
180
|
-
while offset < bytes.count {
|
|
181
|
-
let count = Darwin.write(fd, bytes.baseAddress!.advanced(by: offset), bytes.count - offset)
|
|
182
|
-
if count <= 0 { return }; offset += count
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
private func hello() -> [String: Any] {
|
|
187
|
-
["protocol": "nimo-macos-resource-v1", "process": ["pid": process.pid, "instanceId": process.instanceId],
|
|
188
|
-
"processAuditToken": processAuditToken, "ownerAuditToken": configuration.ownerAuditToken,
|
|
189
|
-
"ownerPid": configuration.ownerPid, "ownerBirth": ["seconds": configuration.ownerBirth.seconds, "microseconds": configuration.ownerBirth.microseconds],
|
|
190
|
-
"executableSha256": executableSha256, "bundleIdentifier": configuration.appBundleId,
|
|
191
|
-
"directory": ["device": directoryDevice, "inode": directoryInode], "socketPath": socketPath]
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
// Audit tokens are opaque kernel identities: never interpret their word layout.
|
|
195
|
-
private func nimoAuditTokenHex(_ token: audit_token_t) -> String {
|
|
196
|
-
withUnsafeBytes(of: token) { $0.map { String(format: "%02x", $0) }.joined() }
|
|
197
|
-
}
|
|
198
|
-
private func nimoOwnAuditToken() throws -> String {
|
|
199
|
-
var token = audit_token_t()
|
|
200
|
-
var count = mach_msg_type_number_t(MemoryLayout<audit_token_t>.size / MemoryLayout<integer_t>.size)
|
|
201
|
-
let result = withUnsafeMutablePointer(to: &token) { pointer in
|
|
202
|
-
pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
|
|
203
|
-
task_info(mach_task_self_, task_flavor_t(TASK_AUDIT_TOKEN), $0, &count)
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
guard result == KERN_SUCCESS, count == MemoryLayout<audit_token_t>.size / MemoryLayout<integer_t>.size else { throw transportError("process-audit-token-unavailable") }
|
|
207
|
-
return nimoAuditTokenHex(token)
|
|
208
|
-
}
|
|
209
|
-
private func transportError(_ code: String) -> Error { NSError(domain: "NimoResourceTransport", code: 1, userInfo: [NSLocalizedDescriptionKey: code]) }
|
|
210
|
-
#else
|
|
211
|
-
struct NimoMacFixtureConfiguration { let planSha256 = "", grantSha256 = "" }
|
|
212
|
-
final class NimoMacResourceTransport {
|
|
213
|
-
let configuration = NimoMacFixtureConfiguration()
|
|
214
|
-
init(process: NimoResourceProcess) throws { throw NSError(domain: "NimoResourceFixture", code: 1) }
|
|
215
|
-
func persist(_ bytes: Data, nonce: String, kind: String) throws { throw NSError(domain: "NimoResourceFixture", code: 1) }
|
|
216
|
-
}
|
|
217
|
-
#endif
|
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
import CoreBluetooth
|
|
3
|
-
|
|
4
|
-
/// One process-local authority shared by the typed Expo bridge and the Mac
|
|
5
|
-
/// fixture socket. Neither accepts packets, connection addresses or file paths.
|
|
6
|
-
@MainActor
|
|
7
|
-
public enum NimoResourceFixtureBridge {
|
|
8
|
-
public static func execute(_ json: String) async throws -> String {
|
|
9
|
-
let bytes = Data(json.utf8)
|
|
10
|
-
return String(decoding: try await NimoResourceFixture.shared.execute(bytes), as: UTF8.self)
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
@MainActor
|
|
15
|
-
final class NimoResourceFixture {
|
|
16
|
-
static let shared = NimoResourceFixture()
|
|
17
|
-
let process = NimoResourceProcess(pid: ProcessInfo.processInfo.processIdentifier, instanceId: UUID().uuidString.lowercased())
|
|
18
|
-
private(set) var session: NimoResourceSession?
|
|
19
|
-
private(set) var owner: Nimo?
|
|
20
|
-
private var acceptedPair: NimoResourceSelection?
|
|
21
|
-
private var captureNonce: String?
|
|
22
|
-
private var armedElapsedMs: UInt64?
|
|
23
|
-
private var discovery: NimoResourceConnectedDiscovery?
|
|
24
|
-
private var discoveryRequestNonce: String?
|
|
25
|
-
private var discoveryReleaseNonce: String?
|
|
26
|
-
private var discoveryReleaseStartedElapsedMs: UInt64?
|
|
27
|
-
private var discoveryCancelled = false
|
|
28
|
-
private var completed: [String: (Data, Data)] = [:]
|
|
29
|
-
private var pending: [String: (Data, Task<Data, Error>)] = [:]
|
|
30
|
-
private var transport: NimoMacResourceTransport?
|
|
31
|
-
private var startupError: Error?
|
|
32
|
-
private var originalClosed = false
|
|
33
|
-
private let managerEntries = NimoResourceManagerEntries()
|
|
34
|
-
var pendingManagerEntries: Int { managerEntries.all }
|
|
35
|
-
var pendingOriginalManagerEntries: Int { managerEntries.originalNimo }
|
|
36
|
-
func beginManagerEntry(_ wearable: String) -> NimoResourceManagerEntries.Ticket { managerEntries.begin(wearable) }
|
|
37
|
-
func endManagerEntry(_ ticket: NimoResourceManagerEntries.Ticket) { managerEntries.end(ticket) }
|
|
38
|
-
#if os(macOS)
|
|
39
|
-
private let requested = ProcessInfo.processInfo.environment["MENTRA_NIMO_RESOURCE_BOOTSTRAP"] != nil
|
|
40
|
-
#elseif os(iOS)
|
|
41
|
-
private let requested = ProcessInfo.processInfo.isiOSAppOnMac && ProcessInfo.processInfo.environment["MENTRA_NIMO_RESOURCE_BOOTSTRAP"] != nil
|
|
42
|
-
#else
|
|
43
|
-
private let requested = false
|
|
44
|
-
#endif
|
|
45
|
-
#if SWIFT_PACKAGE
|
|
46
|
-
private var packageEnabled = false
|
|
47
|
-
#endif
|
|
48
|
-
var enabled: Bool {
|
|
49
|
-
#if SWIFT_PACKAGE
|
|
50
|
-
return requested || packageEnabled
|
|
51
|
-
#else
|
|
52
|
-
return requested
|
|
53
|
-
#endif
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
func start() {
|
|
57
|
-
guard enabled, transport == nil, startupError == nil else { return }
|
|
58
|
-
do { transport = try NimoMacResourceTransport(process: process) }
|
|
59
|
-
catch { startupError = error }
|
|
60
|
-
}
|
|
61
|
-
func allowsManager(_ wearable: String, name: String? = nil) -> Bool {
|
|
62
|
-
guard enabled else { return true }
|
|
63
|
-
// A different manager can clean up the captured Nimo object. Permit it
|
|
64
|
-
// only after the original resource has actually completed teardown.
|
|
65
|
-
guard wearable.contains("NIMO") else { return NimoResourceManagerState.allowsReplacement(fixtureEnabled: enabled, originalClosed: originalClosed && (session == nil || session?.errors.isEmpty == true && session?.connected == false)) }
|
|
66
|
-
guard let session, !session.cancelled, session.releaseNonce == nil, session.original == nil,
|
|
67
|
-
name == nil || name == session.pair.name else { return false }
|
|
68
|
-
return true
|
|
69
|
-
}
|
|
70
|
-
func hasVerifiedOriginalClosure(_ candidate: Nimo) -> Bool {
|
|
71
|
-
originalClosed && owner === candidate && session?.isOwner(candidate) == true
|
|
72
|
-
&& session?.errors.isEmpty == true && session?.connected == false
|
|
73
|
-
}
|
|
74
|
-
func admit(_ candidate: Nimo, name: String?) -> Bool {
|
|
75
|
-
guard enabled else { return true }
|
|
76
|
-
guard (DeviceManager.shared.sgc as AnyObject?) === candidate, let session, let transport else { return false }
|
|
77
|
-
let admitted = session.admit(candidate, instanceId: candidate.resourceInstanceId, name: name) { proposed in
|
|
78
|
-
self.persistAdmission(session, instanceId: candidate.resourceInstanceId, elapsed: proposed, transportStart: false, transport: transport)
|
|
79
|
-
}
|
|
80
|
-
if session.isOwner(candidate), owner == nil { owner = candidate }
|
|
81
|
-
return admitted && owner === candidate
|
|
82
|
-
}
|
|
83
|
-
private func persistAdmission(_ session: NimoResourceSession, instanceId: String, elapsed: UInt64, transportStart: Bool, transport: NimoMacResourceTransport) -> Bool {
|
|
84
|
-
let value: [String: Any] = ["protocol": "nimo-macos-resource-v1", "captureNonce": session.captureNonce,
|
|
85
|
-
"process": ["pid": process.pid, "instanceId": process.instanceId],
|
|
86
|
-
"planSha256": transport.configuration.planSha256, "grantSha256": transport.configuration.grantSha256,
|
|
87
|
-
"instanceId": instanceId, "peripheralUuid": session.pair.peripheralUuid, "sessionGeneration": transportStart ? 1 : 0,
|
|
88
|
-
"armedElapsedMs": session.armedElapsedMs, "admittedElapsedMs": transportStart ? nullable(session.admittedElapsedMs) : elapsed,
|
|
89
|
-
"startedElapsedMs": transportStart ? elapsed : NSNull() as Any]
|
|
90
|
-
do {
|
|
91
|
-
let bytes = try JSONSerialization.data(withJSONObject: value, options: [.sortedKeys])
|
|
92
|
-
try transport.persist(bytes, nonce: session.captureNonce, kind: transportStart ? "transport" : "admission")
|
|
93
|
-
return true
|
|
94
|
-
} catch { return false }
|
|
95
|
-
}
|
|
96
|
-
func preserveScan(_ name: String) -> Bool {
|
|
97
|
-
guard enabled, let owner, let session, session.isOwner(owner),
|
|
98
|
-
session.original == nil, !session.cancelled, name == session.pair.name else { return false }
|
|
99
|
-
return (DeviceManager.shared.sgc as AnyObject?) === owner
|
|
100
|
-
}
|
|
101
|
-
func beginTransport(_ candidate: Nimo, peripheral: AnyObject, uuid: String) -> Bool {
|
|
102
|
-
guard enabled else { return true }
|
|
103
|
-
guard (DeviceManager.shared.sgc as AnyObject?) === candidate, let session, let transport else { return false }
|
|
104
|
-
return session.beginTransport(candidate, peripheral: peripheral, uuid: uuid) { proposed in
|
|
105
|
-
self.persistAdmission(session, instanceId: candidate.resourceInstanceId, elapsed: proposed, transportStart: true, transport: transport)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
func allowsProducer(_ candidate: Nimo) -> Bool {
|
|
109
|
-
guard enabled else { return true }
|
|
110
|
-
return session?.isOwner(candidate) == true && session?.releaseNonce == nil && session?.cancelled == false && (DeviceManager.shared.sgc as AnyObject?) === candidate
|
|
111
|
-
}
|
|
112
|
-
func captured(_ candidate: Nimo) -> NimoResourceSession? {
|
|
113
|
-
guard enabled, session?.isOwner(candidate) == true else { return nil }; return session
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
func execute(_ bytes: Data) async throws -> Data {
|
|
117
|
-
start()
|
|
118
|
-
guard bytes.count <= 16_384, enabled, let transport, startupError == nil else { throw failure("fixture-unavailable") }
|
|
119
|
-
let request = try decode(bytes)
|
|
120
|
-
guard request.version == 1, request.expectedProcess == process,
|
|
121
|
-
request.planSha256 == transport.configuration.planSha256,
|
|
122
|
-
request.grantSha256 == transport.configuration.grantSha256 else { throw failure("fixture-authority-mismatch") }
|
|
123
|
-
if let (original, receipt) = completed[request.requestNonce] {
|
|
124
|
-
guard original == bytes else { throw failure("request-nonce-reused") }; return receipt
|
|
125
|
-
}
|
|
126
|
-
if let (original, task) = pending[request.requestNonce] {
|
|
127
|
-
guard original == bytes else { throw failure("request-nonce-reused") }; return try await task.value
|
|
128
|
-
}
|
|
129
|
-
if request.operation != .arm && request.operation != .discover {
|
|
130
|
-
guard captureNonce == request.captureNonce, acceptedPair == request.expectedPair else { throw failure("capture-mismatch") }
|
|
131
|
-
let session = self.session
|
|
132
|
-
let observed = session?.original ?? session?.instanceId.map { NimoResourceOriginal(instanceId: $0, peripheralUuid: session!.pair.peripheralUuid, sessionGeneration: 0) }
|
|
133
|
-
if let expected = request.original { guard expected == observed else { throw failure("original-session-mismatch") } }
|
|
134
|
-
if request.operation == .release, session?.hasOwner == true, request.original == nil { throw failure("release-requires-original") }
|
|
135
|
-
if request.operation == .release, let release = session?.releaseNonce ?? discoveryReleaseNonce, release != request.requestNonce { throw failure("original-release-nonce-required") }
|
|
136
|
-
}
|
|
137
|
-
let recovery = request.operation == .release || request.operation == .cancel || session?.releaseNonce != nil || discoveryReleaseNonce != nil
|
|
138
|
-
guard completed.count + pending.count < (recovery ? 256 : 192) else { throw failure("request-budget-exhausted") }
|
|
139
|
-
// Intent is persisted before ANY capture or release mutation. No caller
|
|
140
|
-
// path is used; replay compares the exact original request bytes.
|
|
141
|
-
try transport.persist(bytes, nonce: request.requestNonce, kind: "intent")
|
|
142
|
-
let received = NimoResourceClock.now()
|
|
143
|
-
let task = Task { @MainActor in
|
|
144
|
-
let result = try await self.perform(request, received: received)
|
|
145
|
-
let encoded = try JSONSerialization.data(withJSONObject: result, options: [.sortedKeys])
|
|
146
|
-
guard encoded.count <= 32_768 else { throw self.failure("receipt-budget-exhausted") }
|
|
147
|
-
try transport.persist(encoded, nonce: request.requestNonce, kind: "receipt")
|
|
148
|
-
return encoded
|
|
149
|
-
}
|
|
150
|
-
pending[request.requestNonce] = (bytes, task)
|
|
151
|
-
do {
|
|
152
|
-
let receipt = try await task.value
|
|
153
|
-
completed[request.requestNonce] = (bytes, receipt); pending.removeValue(forKey: request.requestNonce)
|
|
154
|
-
return receipt
|
|
155
|
-
} catch {
|
|
156
|
-
// Keep the original failed intent fenced; do not reexecute effects.
|
|
157
|
-
throw error
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private func perform(_ request: NimoResourceRequest, received: UInt64) async throws -> [String: Any] {
|
|
162
|
-
if request.operation == .discover {
|
|
163
|
-
guard request.expectedPair.peripheralSelection == NimoResourceSelection.connectedService,
|
|
164
|
-
captureNonce == nil, request.original == nil, DeviceManager.shared.sgc == nil, pendingManagerEntries == 0 else { throw failure("discover-requires-unowned-manager") }
|
|
165
|
-
acceptedPair = request.expectedPair; captureNonce = request.captureNonce; armedElapsedMs = NimoResourceClock.now()
|
|
166
|
-
let observer = NimoResourceConnectedDiscovery(name: request.expectedPair.name, startedElapsedMs: armedElapsedMs!)
|
|
167
|
-
discovery = observer; discoveryRequestNonce = request.requestNonce
|
|
168
|
-
await observer.discoverConnected()
|
|
169
|
-
}
|
|
170
|
-
if request.operation == .arm {
|
|
171
|
-
guard session == nil, request.original == nil, DeviceManager.shared.sgc == nil, pendingManagerEntries == 0 else { throw failure("arm-requires-unowned-manager") }
|
|
172
|
-
let uuid: String
|
|
173
|
-
if let observed = request.expectedPair.peripheralUuid {
|
|
174
|
-
guard captureNonce == nil else { throw failure("capture-already-exists") }
|
|
175
|
-
acceptedPair = request.expectedPair; captureNonce = request.captureNonce
|
|
176
|
-
uuid = observed
|
|
177
|
-
} else {
|
|
178
|
-
guard captureNonce == request.captureNonce, acceptedPair == request.expectedPair,
|
|
179
|
-
!discoveryCancelled, discoveryReleaseNonce == nil,
|
|
180
|
-
let discoveryRequestNonce, completed[discoveryRequestNonce] != nil,
|
|
181
|
-
let discovery, discovery.ledger.settledElapsedMs != nil, discovery.callbacksPending == 0,
|
|
182
|
-
discovery.ledger.errors.isEmpty, let selected = discovery.ledger.selectedPeripheralUuid else { throw failure("discovery-not-admissible") }
|
|
183
|
-
uuid = selected
|
|
184
|
-
}
|
|
185
|
-
if armedElapsedMs == nil { armedElapsedMs = NimoResourceClock.now() }
|
|
186
|
-
session = NimoResourceSession(captureNonce: request.captureNonce, pair: NimoResourcePair(name: request.expectedPair.name, peripheralUuid: uuid), armedElapsedMs: armedElapsedMs!)
|
|
187
|
-
}
|
|
188
|
-
guard captureNonce == request.captureNonce, acceptedPair == request.expectedPair else { throw failure("capture-mismatch") }
|
|
189
|
-
if session == nil {
|
|
190
|
-
guard request.original == nil else { throw failure("original-session-mismatch") }
|
|
191
|
-
if request.operation == .release || request.operation == .cancel {
|
|
192
|
-
if let original = discoveryReleaseNonce, request.operation == .release, original != request.requestNonce { throw failure("original-release-nonce-required") }
|
|
193
|
-
if discoveryReleaseNonce == nil {
|
|
194
|
-
discoveryReleaseNonce = request.requestNonce; discoveryReleaseStartedElapsedMs = NimoResourceClock.now()
|
|
195
|
-
discoveryCancelled = request.operation == .cancel
|
|
196
|
-
await discovery?.stopDiscovery(cancel: true)
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return discoveryOnlyReceipt(request, received: received)
|
|
200
|
-
}
|
|
201
|
-
guard let session, session.captureNonce == request.captureNonce, acceptedPair == request.expectedPair else { throw failure("capture-mismatch") }
|
|
202
|
-
if let expected = request.original {
|
|
203
|
-
let observed = session.original ?? session.instanceId.map { NimoResourceOriginal(instanceId: $0, peripheralUuid: session.pair.peripheralUuid, sessionGeneration: 0) }
|
|
204
|
-
guard expected == observed else { throw failure("original-session-mismatch") }
|
|
205
|
-
} else if request.operation == .release, session.hasOwner { throw failure("release-requires-original") }
|
|
206
|
-
if request.operation == .release || request.operation == .cancel {
|
|
207
|
-
if session.releaseNonce == nil {
|
|
208
|
-
let mic = owner?.resourceMicStopRequired ?? false, canvas = owner?.resourceCanvasRequired ?? false
|
|
209
|
-
guard session.beginRelease(nonce: request.requestNonce, cancel: request.operation == .cancel, mic: mic, canvas: canvas) else { throw failure("release-mismatch") }
|
|
210
|
-
if let owner { await owner.releaseResource(session) }
|
|
211
|
-
else {
|
|
212
|
-
let deadline = NimoResourceClock.now() + 1_000
|
|
213
|
-
while pendingOriginalManagerEntries != 0, NimoResourceClock.now() < deadline { try? await Task.sleep(nanoseconds: 10_000_000) }
|
|
214
|
-
if pendingOriginalManagerEntries != 0 { session.recordError("pending-manager-entry-not-settled") }
|
|
215
|
-
session.proveNoTransport()
|
|
216
|
-
}
|
|
217
|
-
} else if request.operation == .release, session.releaseNonce != request.requestNonce {
|
|
218
|
-
throw failure("original-release-nonce-required")
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
var producers = NimoResourceProducers()
|
|
222
|
-
producers.postedCallbacks = pendingOriginalManagerEntries
|
|
223
|
-
if let owner { producers = await owner.resourceProducers() }
|
|
224
|
-
originalClosed = session.settled(producers: producers)
|
|
225
|
-
let manager = DeviceManager.shared.sgc
|
|
226
|
-
let managerId = (manager as? Nimo)?.resourceInstanceId
|
|
227
|
-
let current: [String: Any] = ["type": nullable(manager?.type), "instanceId": nullable(managerId)]
|
|
228
|
-
var result: [String: Any] = ["version": 1, "protocol": "nimo-macos-resource-v1", "operation": request.operation.rawValue,
|
|
229
|
-
"requestNonce": request.requestNonce, "captureNonce": request.captureNonce,
|
|
230
|
-
"planSha256": request.planSha256, "grantSha256": request.grantSha256,
|
|
231
|
-
"process": ["pid": process.pid, "instanceId": process.instanceId],
|
|
232
|
-
"clock": ["kind": "darwin-continuous-time-v1", "receivedElapsedMs": received, "observedElapsedMs": NimoResourceClock.now()],
|
|
233
|
-
"capture": ["armedElapsedMs": session.armedElapsedMs, "admittedElapsedMs": nullable(session.admittedElapsedMs), "cancelled": session.cancelled,
|
|
234
|
-
"expectedPair": request.expectedPair.dictionary],
|
|
235
|
-
"currentManager": current, "session": nullable(session.sessionDictionary()),
|
|
236
|
-
"release": session.releaseDictionary(producers: producers), "producers": producers.dictionary, "errors": session.errors]
|
|
237
|
-
if let discovery { result["discovery"] = discovery.ledger.dictionary(callbacksPending: discovery.callbacksPending) }
|
|
238
|
-
return result
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
private func discoveryOnlyReceipt(_ request: NimoResourceRequest, received: UInt64) -> [String: Any] {
|
|
242
|
-
let settled = discovery?.ledger.settledElapsedMs != nil && discovery?.callbacksPending == 0
|
|
243
|
-
let manager = DeviceManager.shared.sgc
|
|
244
|
-
let managerSafe = manager == nil || originalClosed && !(manager?.type.contains("NIMO") ?? false)
|
|
245
|
-
let closed = discoveryReleaseNonce != nil && settled && pendingOriginalManagerEntries == 0 && managerSafe
|
|
246
|
-
originalClosed = closed
|
|
247
|
-
var producers = NimoResourceProducers()
|
|
248
|
-
producers.postedCallbacks = pendingOriginalManagerEntries + (discovery?.callbacksPending ?? 0)
|
|
249
|
-
producers.timers = settled ? 0 : 1
|
|
250
|
-
let release: [String: Any] = ["nonce": nullable(discoveryReleaseNonce), "startedElapsedMs": nullable(discoveryReleaseStartedElapsedMs),
|
|
251
|
-
"micStopRequired": false, "displayExitRequired": false, "micStopStartedElapsedMs": NSNull(), "micStopWrittenElapsedMs": NSNull(), "micStopMarkerElapsedMs": NSNull(),
|
|
252
|
-
"exitStatusCode": NSNull(), "exitAckElapsedMs": NSNull(), "disconnectRequestedElapsedMs": NSNull(), "disconnectCallbackElapsedMs": NSNull(), "disconnectError": NSNull(),
|
|
253
|
-
"connectionFailedElapsedMs": NSNull(), "noTransport": closed, "settled": closed]
|
|
254
|
-
return ["version": 1, "protocol": "nimo-macos-resource-v1", "operation": request.operation.rawValue,
|
|
255
|
-
"requestNonce": request.requestNonce, "captureNonce": request.captureNonce, "planSha256": request.planSha256, "grantSha256": request.grantSha256,
|
|
256
|
-
"process": ["pid": process.pid, "instanceId": process.instanceId],
|
|
257
|
-
"clock": ["kind": "darwin-continuous-time-v1", "receivedElapsedMs": received, "observedElapsedMs": NimoResourceClock.now()],
|
|
258
|
-
"capture": ["armedElapsedMs": armedElapsedMs!, "admittedElapsedMs": NSNull(), "cancelled": discoveryCancelled, "expectedPair": request.expectedPair.dictionary],
|
|
259
|
-
"currentManager": ["type": nullable(DeviceManager.shared.sgc?.type), "instanceId": nullable((DeviceManager.shared.sgc as? Nimo)?.resourceInstanceId)],
|
|
260
|
-
"session": NSNull(), "release": release, "producers": producers.dictionary, "errors": [],
|
|
261
|
-
"discovery": discovery!.ledger.dictionary(callbacksPending: discovery!.callbacksPending)]
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
#if SWIFT_PACKAGE
|
|
265
|
-
/// Test-only original discovery seed; no CoreBluetooth object is created.
|
|
266
|
-
/// Tests still execute the real perform/status/cancel/arm authority branches.
|
|
267
|
-
func packageSeedDiscovery(name: String, capture: String, discoveryOperation: String, candidates: [(String, String?)], persisted: Bool) {
|
|
268
|
-
packageEnabled = true
|
|
269
|
-
acceptedPair = NimoResourceSelection(name: name, peripheralSelection: NimoResourceSelection.connectedService)
|
|
270
|
-
captureNonce = capture; armedElapsedMs = NimoResourceClock.now()
|
|
271
|
-
let observer = NimoResourceConnectedDiscovery(name: name, startedElapsedMs: armedElapsedMs!)
|
|
272
|
-
discovery = observer; discoveryRequestNonce = discoveryOperation
|
|
273
|
-
for (uuid, candidateName) in candidates { observer.ledger.observe(uuid: uuid, name: candidateName) }
|
|
274
|
-
observer.ledger.finish(at: NimoResourceClock.now(), callbacksPending: 0)
|
|
275
|
-
if persisted { completed[discoveryOperation] = (Data(), Data()) }
|
|
276
|
-
}
|
|
277
|
-
func packagePerform(_ bytes: Data) async throws -> [String: Any] {
|
|
278
|
-
try await perform(try decode(bytes), received: NimoResourceClock.now())
|
|
279
|
-
}
|
|
280
|
-
#endif
|
|
281
|
-
|
|
282
|
-
private func decode(_ bytes: Data) throws -> NimoResourceRequest { try NimoResourceRequest.decode(bytes) }
|
|
283
|
-
private func failure(_ code: String) -> Error { NSError(domain: "NimoResourceFixture", code: 1, userInfo: [NSLocalizedDescriptionKey: code]) }
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
/// Retrieval only: no scan, connection, peripheral delegate, service discovery or
|
|
288
|
-
/// writes. The serial state-delegate queue drains before candidate commitment.
|
|
289
|
-
@MainActor
|
|
290
|
-
final class NimoResourceConnectedDiscovery: NSObject, CBCentralManagerDelegate {
|
|
291
|
-
let ledger: NimoResourceDiscovery
|
|
292
|
-
private let queue = DispatchQueue(label: "com.mentra.nimo.resource.connected-discovery")
|
|
293
|
-
private var central: CBCentralManager?
|
|
294
|
-
private var stopped = false
|
|
295
|
-
private var running = false
|
|
296
|
-
private var now: () -> UInt64 = NimoResourceClock.now
|
|
297
|
-
private var pause: () async throws -> Void = { try await Task.sleep(nanoseconds: 50_000_000) }
|
|
298
|
-
#if SWIFT_PACKAGE
|
|
299
|
-
private var packageSample: (() -> (CBManagerState, [(String, String?)]))?
|
|
300
|
-
func packageConfigureObserver(now: @escaping () -> UInt64, pause: @escaping () async throws -> Void,
|
|
301
|
-
sample: @escaping () -> (CBManagerState, [(String, String?)])) {
|
|
302
|
-
precondition(!running && central == nil)
|
|
303
|
-
self.now = now; self.pause = pause; packageSample = sample
|
|
304
|
-
}
|
|
305
|
-
#endif
|
|
306
|
-
nonisolated private let callbacks = NimoResourceCallbackCounter()
|
|
307
|
-
var callbacksPending: Int { callbacks.value }
|
|
308
|
-
init(name: String, startedElapsedMs: UInt64) {
|
|
309
|
-
ledger = NimoResourceDiscovery(name: name, startedElapsedMs: startedElapsedMs); super.init()
|
|
310
|
-
}
|
|
311
|
-
nonisolated func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
312
|
-
callbacks.add(1)
|
|
313
|
-
defer { callbacks.add(-1) }
|
|
314
|
-
// No deferred work. State is sampled by the owned bounded Main loop.
|
|
315
|
-
}
|
|
316
|
-
@inline(never)
|
|
317
|
-
private func retrieveConnected(_ central: CBCentralManager) -> [CBPeripheral] {
|
|
318
|
-
central.retrieveConnectedPeripherals(withServices: [CBUUID(string: "00007033-0000-1000-8000-00805F9B34FB")])
|
|
319
|
-
}
|
|
320
|
-
@inline(never)
|
|
321
|
-
func discoverConnected() async {
|
|
322
|
-
guard !running, !stopped else { return }
|
|
323
|
-
running = true
|
|
324
|
-
#if SWIFT_PACKAGE
|
|
325
|
-
if packageSample == nil {
|
|
326
|
-
central = CBCentralManager(delegate: self, queue: queue, options: [CBCentralManagerOptionShowPowerAlertKey: false])
|
|
327
|
-
}
|
|
328
|
-
#else
|
|
329
|
-
central = CBCentralManager(delegate: self, queue: queue, options: [CBCentralManagerOptionShowPowerAlertKey: false])
|
|
330
|
-
#endif
|
|
331
|
-
let deadline = ledger.startedElapsedMs + 5_000
|
|
332
|
-
while !stopped {
|
|
333
|
-
// A post-sleep final powered-on retrieval replaces earlier facts.
|
|
334
|
-
let finalSample = now() >= deadline
|
|
335
|
-
var state = central?.state ?? .unknown
|
|
336
|
-
#if SWIFT_PACKAGE
|
|
337
|
-
let simulated = packageSample?()
|
|
338
|
-
if let simulated { state = simulated.0 }
|
|
339
|
-
#endif
|
|
340
|
-
switch state {
|
|
341
|
-
case .poweredOn:
|
|
342
|
-
#if SWIFT_PACKAGE
|
|
343
|
-
if let simulated { ledger.observeConnectedSnapshot(simulated.1) }
|
|
344
|
-
else if let central { ledger.observeConnectedSnapshot(retrieveConnected(central).map { ($0.identifier.uuidString.lowercased(), $0.name) }) }
|
|
345
|
-
else { ledger.observeConnectedSnapshot([]) }
|
|
346
|
-
#else
|
|
347
|
-
if let central { ledger.observeConnectedSnapshot(retrieveConnected(central).map { ($0.identifier.uuidString.lowercased(), $0.name) }) }
|
|
348
|
-
else { ledger.observeConnectedSnapshot([]) }
|
|
349
|
-
#endif
|
|
350
|
-
case .unsupported, .unauthorized:
|
|
351
|
-
ledger.observeConnectedSnapshot([])
|
|
352
|
-
ledger.recordError("connected-service-observer-unavailable"); finishDiscovery(); return
|
|
353
|
-
default:
|
|
354
|
-
// Radio loss invalidates the current snapshot, not history.
|
|
355
|
-
ledger.observeConnectedSnapshot([])
|
|
356
|
-
}
|
|
357
|
-
if finalSample { break }
|
|
358
|
-
do { try await pause() }
|
|
359
|
-
catch { ledger.recordError("discovery-task-cancelled"); ledger.cancel(); finishDiscovery(); return }
|
|
360
|
-
}
|
|
361
|
-
finishDiscovery()
|
|
362
|
-
}
|
|
363
|
-
@inline(never)
|
|
364
|
-
func stopDiscovery(cancel: Bool) async {
|
|
365
|
-
if cancel { ledger.cancel() }
|
|
366
|
-
stopped = true
|
|
367
|
-
let deadline = NimoResourceClock.now() + 1_000
|
|
368
|
-
while running, NimoResourceClock.now() < deadline {
|
|
369
|
-
do { try await Task.sleep(nanoseconds: 1_000_000) } catch { await Task.yield() }
|
|
370
|
-
}
|
|
371
|
-
guard !running else { ledger.recordError("discovery-loop-not-settled"); return }
|
|
372
|
-
finishDiscovery()
|
|
373
|
-
}
|
|
374
|
-
@inline(never)
|
|
375
|
-
private func finishDiscovery() {
|
|
376
|
-
stopped = true
|
|
377
|
-
central?.delegate = nil
|
|
378
|
-
// Callback contains no Main work; the queue barrier cannot deadlock.
|
|
379
|
-
queue.sync {}
|
|
380
|
-
central = nil
|
|
381
|
-
ledger.finish(at: now(), callbacksPending: callbacksPending)
|
|
382
|
-
running = false
|
|
383
|
-
}
|
|
384
|
-
}
|