@mentra/glasses-media 3.5.0-dev.1382 → 3.5.0-dev.1386

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.
@@ -9,11 +9,17 @@ final class MacHotspotAssociation {
9
9
  /// glasses still hold the Mac's previous association (-3912). A later attempt in the same window succeeds.
10
10
  static let associationAttempts = 3
11
11
  static let associationRetryDelay: TimeInterval = 2
12
+ /// After leaving, macOS auto-join normally returns to the previous network within a few seconds.
13
+ static let autoJoinWait: TimeInterval = 15
14
+ /// Auto-join after a Wi-Fi power cycle: 2 s on lab air-1.
15
+ static let powerCycleWait: TimeInterval = 30
12
16
 
13
17
  private let condition = NSCondition()
14
18
  private var cancelled = false
15
19
  private var interface: MacWifiInterface?
16
20
  private var joinedSSID: String?
21
+ /// The Wi-Fi network the Mac was on before this join, which every leave returns it to.
22
+ private var previousSSID: String?
17
23
  private let deadline: TimeInterval
18
24
  private let now: () -> TimeInterval
19
25
  private let pause: ((TimeInterval) -> Void)?
@@ -38,6 +44,8 @@ final class MacHotspotAssociation {
38
44
  try checkCurrent()
39
45
  let interface = try makeInterface()
40
46
  self.interface = interface
47
+ let before = try readSSID(interface)
48
+ if before != ssid { previousSSID = before }
41
49
  for attempt in 1 ... Self.associationAttempts {
42
50
  guard try readSSID(interface) != ssid else { break }
43
51
  let network = try discoverNetwork(ssid: ssid, interface: interface)
@@ -73,17 +81,71 @@ final class MacHotspotAssociation {
73
81
 
74
82
  /// Run on the same worker as association, after its blocking call returns.
75
83
  /// A connection the user replaced must never be disconnected by our cleanup.
76
- func leave() throws {
77
- if let joinedSSID {
78
- if let interface, try readSSID(interface) == joinedSSID {
84
+ ///
85
+ /// Once an association was requested, whether it succeeded, failed or timed out, the
86
+ /// Mac may have left its own network for it and macOS has saved the hotspot as a known
87
+ /// network. Leaving forgets the hotspot and gets the Mac back on the network it was on
88
+ /// before (lab air-1, 2026-11-11: a failed join left it with no network for 5 hours).
89
+ @discardableResult
90
+ func leave() throws -> MacHotspotLeaveReport {
91
+ var report = MacHotspotLeaveReport()
92
+ if let joinedSSID, let interface {
93
+ if try readSSID(interface) == joinedSSID {
79
94
  interface.disassociate()
80
95
  guard try readSSID(interface) != joinedSSID else {
81
96
  throw LocalMediaError("Mac Wi-Fi has not released the glasses hotspot")
82
97
  }
98
+ report.disassociated = true
83
99
  }
100
+ do { try interface.forget(ssid: joinedSSID); report.forgotHotspot = true }
101
+ catch { report.failures.append("forget: \(error.localizedDescription)") }
102
+ report.restore = restorePrevious(interface, report: &report)
84
103
  }
85
104
  interface = nil
86
105
  joinedSSID = nil
106
+ previousSSID = nil
107
+ return report
108
+ }
109
+
110
+ /// Never takes the Mac off a network it is on: only an interface left with no network is
111
+ /// power cycled. After a failed association air-1's Wi-Fi answered every auto-join scan with
112
+ /// EBUSY for hours; a power cycle starts auto-join over. An app cannot rejoin the previous
113
+ /// network itself: without the passphrase, CoreWLAN answers -3900 (tmpErr).
114
+ private func restorePrevious(_ interface: MacWifiInterface, report: inout MacHotspotLeaveReport) -> MacHotspotRestore {
115
+ guard let previous = previousSSID else { return .noPreviousNetwork }
116
+ if let outcome = settle(interface, previous: previous, within: Self.autoJoinWait, success: .autoJoined) { return outcome }
117
+ do {
118
+ try interface.setPower(false)
119
+ sleep(1)
120
+ try interface.setPower(true)
121
+ } catch {
122
+ report.failures.append("power cycle: \(error.localizedDescription)")
123
+ return .notRestored
124
+ }
125
+ return settle(interface, previous: previous, within: Self.powerCycleWait, success: .powerCycled) ?? .notRestored
126
+ }
127
+
128
+ /// Polls once a second until the Mac is on the previous network (success) or on
129
+ /// another one (left alone); nil while it is still on none when the time runs out.
130
+ private func settle(_ interface: MacWifiInterface, previous: String, within seconds: TimeInterval,
131
+ success: MacHotspotRestore) -> MacHotspotRestore?
132
+ {
133
+ var waited: TimeInterval = 0
134
+ while true {
135
+ // An unreadable SSID (Location revoked) counts as no network: recovery never disconnects.
136
+ switch try? readSSID(interface) {
137
+ case previous?: return success
138
+ case .some: return .otherNetwork
139
+ case nil: break
140
+ }
141
+ guard waited < seconds else { return nil }
142
+ sleep(1); waited += 1
143
+ }
144
+ }
145
+
146
+ /// Cleanup pacing: unlike wait(_:), runs after cancel() and must not be cut short by it.
147
+ private func sleep(_ seconds: TimeInterval) {
148
+ if let pause { pause(seconds) } else { Thread.sleep(forTimeInterval: seconds) }
87
149
  }
88
150
 
89
151
  func currentSSID() throws -> String? {
@@ -145,6 +207,33 @@ protocol MacWifiInterface: AnyObject {
145
207
  func network(ssid: String) throws -> NSObject?
146
208
  func associate(network: NSObject, password: String) throws
147
209
  func disassociate()
210
+ /// Removes the network from the Mac's known networks, so auto-join never picks it.
211
+ func forget(ssid: String) throws
212
+ func setPower(_ on: Bool) throws
213
+ }
214
+
215
+ /// What leaving did to the Mac's Wi-Fi, for the hotspot log. Never holds an SSID.
216
+ struct MacHotspotLeaveReport: Equatable {
217
+ var disassociated = false
218
+ var forgotHotspot = false
219
+ var restore: MacHotspotRestore = .notNeeded
220
+ var failures: [String] = []
221
+
222
+ var summary: String {
223
+ "disassociated=\(disassociated) forgot_hotspot=\(forgotHotspot) previous_network=\(restore.rawValue)" +
224
+ (failures.isEmpty ? "" : " failures=[\(failures.joined(separator: "; "))]")
225
+ }
226
+ }
227
+
228
+ enum MacHotspotRestore: String, Equatable {
229
+ /// No association was requested, so the Mac never left its network.
230
+ case notNeeded = "not_needed"
231
+ case noPreviousNetwork = "none_before"
232
+ case autoJoined = "auto_joined"
233
+ case powerCycled = "power_cycled"
234
+ /// The Mac is on a network other than the previous one, which recovery leaves alone.
235
+ case otherNetwork = "other_network"
236
+ case notRestored = "not_restored"
148
237
  }
149
238
 
150
239
  enum MacWifiInvocation {
@@ -152,6 +241,8 @@ enum MacWifiInvocation {
152
241
  // pointer storage would over-release errors when the native pool drains.
153
242
  typealias Scan = @convention(c) (AnyObject, Selector, NSData, AutoreleasingUnsafeMutablePointer<NSError?>) -> Unmanaged<NSSet>?
154
243
  typealias Associate = @convention(c) (AnyObject, Selector, AnyObject, NSString, AutoreleasingUnsafeMutablePointer<NSError?>) -> Bool
244
+ typealias Forget = @convention(c) (AnyObject, Selector, NSData, UInt64, AutoreleasingUnsafeMutablePointer<NSError?>) -> Bool
245
+ typealias SetPower = @convention(c) (AnyObject, Selector, Bool, AutoreleasingUnsafeMutablePointer<NSError?>) -> Bool
155
246
  }
156
247
 
157
248
  /// CoreWLAN is a public macOS framework. Foundation loads it only in the Mac
@@ -160,6 +251,7 @@ enum MacWifiInvocation {
160
251
  private final class PublicCoreWlanInterface: MacWifiInterface {
161
252
  private let bundle: Bundle
162
253
  private let interface: NSObject
254
+ private let client: NSObject
163
255
  private let authorizationFacts: MacHotspotAuthorizationFacts
164
256
  private let authorization: MacHotspotAuthorization?
165
257
  private typealias Disassociate = @convention(c) (AnyObject, Selector) -> Void
@@ -184,6 +276,7 @@ private final class PublicCoreWlanInterface: MacWifiInterface {
184
276
  }
185
277
  }
186
278
  self.interface = interface
279
+ self.client = client
187
280
  self.bundle = bundle
188
281
  }
189
282
 
@@ -237,6 +330,24 @@ private final class PublicCoreWlanInterface: MacWifiInterface {
237
330
  guard invoke(interface, selector, network, password as NSString, &error) else { throw Self.failure("association", error) }
238
331
  }
239
332
 
333
+ /// associateToNetwork saves the network as a known network, and CoreWLAN has no public way to
334
+ /// remove one without an administrator's authorization. CWWiFiClient's forget call needs none.
335
+ func forget(ssid: String) throws {
336
+ let selector = NSSelectorFromString("forgetWiFiProfileWithSSID:event:error:")
337
+ guard client.responds(to: selector) else { throw LocalMediaError("Mac Wi-Fi cannot forget networks") }
338
+ let invoke = unsafeBitCast(client.method(for: selector), to: MacWifiInvocation.Forget.self)
339
+ var error: NSError?
340
+ guard invoke(client, selector, NSData(data: Data(ssid.utf8)), 0, &error) else { throw Self.failure("forget", error) }
341
+ }
342
+
343
+ func setPower(_ on: Bool) throws {
344
+ let selector = NSSelectorFromString("setPower:error:")
345
+ guard interface.responds(to: selector) else { throw LocalMediaError("Mac Wi-Fi cannot change power") }
346
+ let invoke = unsafeBitCast(interface.method(for: selector), to: MacWifiInvocation.SetPower.self)
347
+ var error: NSError?
348
+ guard invoke(interface, selector, on, &error) else { throw Self.failure("power", error) }
349
+ }
350
+
240
351
  func disassociate() {
241
352
  let selector = NSSelectorFromString("disassociate")
242
353
  let invoke = unsafeBitCast(interface.method(for: selector), to: Disassociate.self)
@@ -535,12 +535,14 @@ public final class GlassesHotspotNetwork {
535
535
  if let association = macAssociation {
536
536
  association.cancel()
537
537
  // This queue is also the association queue: cleanup cannot overtake
538
- // a blocking scan/join, and process-wide ownership remains held.
538
+ // a blocking scan/join, and process-wide ownership remains held, including
539
+ // while leave() forgets the hotspot and returns the Mac to its previous Wi-Fi.
539
540
  macWorker.async {
540
541
  let result = Result { try association.leave() }
541
542
  self.queue.async {
542
543
  switch result {
543
- case .success:
544
+ case let .success(report):
545
+ if report.restore != .notNeeded { MediaLog.log("HOTSPOT_LEAVE mac_wifi \(report.summary)") }
544
546
  self.macAssociation = nil
545
547
  self.completeLeave()
546
548
  case let .failure(error):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/glasses-media",
3
- "version": "3.5.0-dev.1382",
3
+ "version": "3.5.0-dev.1386",
4
4
  "description": "Shared native glasses media receivers and local network transport",
5
5
  "license": "MIT",
6
6
  "author": "Mentra Community",
@@ -16,7 +16,7 @@
16
16
  "access": "public"
17
17
  },
18
18
  "dependencies": {
19
- "@mentra/bluetooth-sdk": "3.5.0-dev.1382"
19
+ "@mentra/bluetooth-sdk": "3.5.0-dev.1386"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "expo-modules-core": "~55.0.25"