@mentra/bluetooth-sdk 3.5.0-dev.1271 → 3.5.0-dev.1276
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/GeneratedReleaseMetadata.kt +5 -5
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +57 -196
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/Ipv4Prefix.kt +51 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/NetworkFacts.kt +81 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkError.kt +109 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadiness.kt +103 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpec.kt +53 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkState.kt +127 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedSoftApNetwork.kt +604 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/SoftApTrace.kt +143 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/Ipv4PrefixTest.kt +59 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/NetworkFactsTest.kt +59 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedLocalNetworkPermissionTest.kt +35 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkErrorTest.kt +96 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkLifecycleTest.kt +187 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadinessTest.kt +141 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpecTest.kt +69 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/SoftApTraceTest.kt +97 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/UnavailableRetryTest.kt +47 -0
- package/build/_private/MentraLocalNetworkModule.d.ts +1 -1
- package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
- package/build/_private/MentraLocalNetworkModule.js.map +1 -1
- package/build/generated/releaseMetadata.js +5 -5
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/ota-transport/index.d.ts +3 -2
- package/build/ota-transport/index.d.ts.map +1 -1
- package/build/ota-transport/index.js +4 -3
- package/build/ota-transport/index.js.map +1 -1
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
- package/package.json +2 -2
- package/src/_private/MentraLocalNetworkModule.ts +5 -1
- package/src/generated/releaseMetadata.ts +5 -5
- package/src/ota-transport/index.ts +4 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/ScopedNetworkReadiness.kt +0 -17
- package/android/src/test/java/com/mentra/bluetoothsdk/ScopedNetworkReadinessTest.kt +0 -39
|
@@ -3,9 +3,9 @@ package com.mentra.bluetoothsdk
|
|
|
3
3
|
/** Generated by release CI. Do not edit in a release checkout. */
|
|
4
4
|
internal object GeneratedReleaseMetadata {
|
|
5
5
|
const val FAMILY_BASE_VERSION: String = "3.5.0"
|
|
6
|
-
const val RELEASE_IDENTITY: String = "3.5.0-dev.
|
|
7
|
-
const val RELEASE_SET_ID: String = "mentra-3.5.0-dev.
|
|
8
|
-
const val SOURCE_COMMIT: String = "
|
|
9
|
-
const val OTA_MANIFEST_URL: String = "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.
|
|
10
|
-
const val OTA_MANIFEST_SHA256: String = "
|
|
6
|
+
const val RELEASE_IDENTITY: String = "3.5.0-dev.1276"
|
|
7
|
+
const val RELEASE_SET_ID: String = "mentra-3.5.0-dev.1276"
|
|
8
|
+
const val SOURCE_COMMIT: String = "2951837f72b37f35559f0ffa29eeb1d8c7ccedf4"
|
|
9
|
+
const val OTA_MANIFEST_URL: String = "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.1276.json"
|
|
10
|
+
const val OTA_MANIFEST_SHA256: String = "ffa521f8e633cd79e50d1df7466c4268ab2ea1f34428f293092395f85fecf5d0"
|
|
11
11
|
}
|
|
@@ -3,11 +3,9 @@ package com.mentra.bluetoothsdk
|
|
|
3
3
|
import android.net.ConnectivityManager
|
|
4
4
|
import android.net.Network
|
|
5
5
|
import android.net.NetworkCapabilities
|
|
6
|
-
import android.net.NetworkRequest
|
|
7
|
-
import android.net.wifi.WifiNetworkSpecifier
|
|
8
|
-
import android.os.Handler
|
|
9
|
-
import android.os.Looper
|
|
10
6
|
import android.util.Base64
|
|
7
|
+
import com.mentra.bluetoothsdk.hotspot.ScopedNetworkError
|
|
8
|
+
import com.mentra.bluetoothsdk.hotspot.ScopedSoftApNetwork
|
|
11
9
|
import com.mentra.bluetoothsdk.utils.NativeLog as Log
|
|
12
10
|
import expo.modules.kotlin.Promise
|
|
13
11
|
import expo.modules.kotlin.modules.Module
|
|
@@ -15,27 +13,27 @@ import expo.modules.kotlin.modules.ModuleDefinition
|
|
|
15
13
|
import java.io.File
|
|
16
14
|
import java.io.FileOutputStream
|
|
17
15
|
import java.net.HttpURLConnection
|
|
18
|
-
import java.net.Inet4Address
|
|
19
16
|
import java.net.URL
|
|
20
17
|
import java.util.concurrent.ConcurrentHashMap
|
|
21
18
|
import java.util.concurrent.Executors
|
|
22
19
|
|
|
23
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* MentraOS-internal transport for glasses-local traffic without process network binding. Gallery
|
|
22
|
+
* sync and hotspot OTA join through [ScopedSoftApNetwork], the same join Mentra Call and the
|
|
23
|
+
* managed relay use, and send their HTTP over the `Network` it returns.
|
|
24
|
+
*/
|
|
24
25
|
class MentraLocalNetworkModule : Module() {
|
|
25
26
|
private companion object {
|
|
26
27
|
const val TAG = "MentraLocalNetwork"
|
|
27
|
-
const val NETWORK_READINESS_TIMEOUT_MS = 5_000L
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
private val lock = Any()
|
|
31
31
|
private val executor = Executors.newCachedThreadPool()
|
|
32
|
-
|
|
32
|
+
// Joins block, so they run one at a time off the JS thread. Disconnect does not queue
|
|
33
|
+
// behind them: releasing the network ends a join in progress at once.
|
|
34
|
+
private val joins = Executors.newSingleThreadExecutor()
|
|
33
35
|
private val activeConnections = ConcurrentHashMap<String, HttpURLConnection>()
|
|
34
|
-
private
|
|
35
|
-
private var localNetwork: Network? = null
|
|
36
|
-
private var networkCallback: ConnectivityManager.NetworkCallback? = null
|
|
37
|
-
private var pendingConnectPromise: Promise? = null
|
|
38
|
-
private var pendingReadinessTimeout: Runnable? = null
|
|
36
|
+
private var scoped: ScopedSoftApNetwork? = null
|
|
39
37
|
|
|
40
38
|
override fun definition() = ModuleDefinition {
|
|
41
39
|
Name("MentraLocalNetwork")
|
|
@@ -49,7 +47,7 @@ class MentraLocalNetworkModule : Module() {
|
|
|
49
47
|
sampledAtMs,
|
|
50
48
|
manager.boundNetworkForProcess,
|
|
51
49
|
manager.activeNetwork,
|
|
52
|
-
synchronized(lock) {
|
|
50
|
+
synchronized(lock) { scoped }?.network(),
|
|
53
51
|
) { network ->
|
|
54
52
|
manager.getNetworkCapabilities(network)?.let { capabilities ->
|
|
55
53
|
mapOf(
|
|
@@ -65,8 +63,8 @@ class MentraLocalNetworkModule : Module() {
|
|
|
65
63
|
}
|
|
66
64
|
}
|
|
67
65
|
|
|
68
|
-
AsyncFunction("connect") { ssid: String, password: String, promise: Promise ->
|
|
69
|
-
connect(ssid, password, promise)
|
|
66
|
+
AsyncFunction("connect") { ssid: String, password: String, incoming: Boolean, promise: Promise ->
|
|
67
|
+
connect(ssid, password, incoming, promise)
|
|
70
68
|
}
|
|
71
69
|
|
|
72
70
|
AsyncFunction("request") {
|
|
@@ -106,6 +104,7 @@ class MentraLocalNetworkModule : Module() {
|
|
|
106
104
|
|
|
107
105
|
OnDestroy {
|
|
108
106
|
disconnect()
|
|
107
|
+
joins.shutdown()
|
|
109
108
|
}
|
|
110
109
|
}
|
|
111
110
|
|
|
@@ -114,113 +113,51 @@ class MentraLocalNetworkModule : Module() {
|
|
|
114
113
|
"ConnectivityManager is unavailable"
|
|
115
114
|
}
|
|
116
115
|
|
|
117
|
-
private fun connect(ssid: String, password: String, promise: Promise) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
.
|
|
129
|
-
|
|
130
|
-
.build()
|
|
116
|
+
private fun connect(ssid: String, password: String, incoming: Boolean, promise: Promise) {
|
|
117
|
+
joins.execute {
|
|
118
|
+
disconnect()
|
|
119
|
+
val context = appContext.reactContext?.applicationContext
|
|
120
|
+
if (context == null) {
|
|
121
|
+
promise.reject("ERR_LOCAL_WIFI_REQUEST", "No React context", null)
|
|
122
|
+
return@execute
|
|
123
|
+
}
|
|
124
|
+
val network = ScopedSoftApNetwork(context)
|
|
125
|
+
synchronized(lock) { scoped = network }
|
|
126
|
+
val listener =
|
|
127
|
+
object : ScopedSoftApNetwork.Listener {
|
|
128
|
+
override fun onAvailable(network: Network, localIpv4: String) = Unit
|
|
131
129
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (networkCallback !== this) {
|
|
137
|
-
Log.d(TAG, "Ignoring onAvailable from a stale network request")
|
|
138
|
-
return
|
|
139
|
-
}
|
|
140
|
-
networkReadiness.onAvailable(network)
|
|
130
|
+
override fun onLost(error: ScopedNetworkError) {
|
|
131
|
+
if (synchronized(lock) { scoped !== network }) return
|
|
132
|
+
cancelAll()
|
|
133
|
+
sendEvent("networkLost", mapOf("ssid" to ssid))
|
|
141
134
|
}
|
|
142
|
-
Log.i(
|
|
143
|
-
TAG,
|
|
144
|
-
"Captured glasses WiFi network=$network; waiting for foreground capability " +
|
|
145
|
-
"processBoundNetwork=${manager.boundNetworkForProcess}",
|
|
146
|
-
)
|
|
147
|
-
scheduleReadinessTimeout(this, ssid)
|
|
148
135
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
network,
|
|
156
|
-
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND),
|
|
157
|
-
)
|
|
158
|
-
) {
|
|
159
|
-
return
|
|
160
|
-
}
|
|
161
|
-
localNetwork = network
|
|
162
|
-
connectPromise = pendingConnectPromise
|
|
163
|
-
pendingConnectPromise = null
|
|
164
|
-
cancelReadinessTimeoutLocked()
|
|
165
|
-
}
|
|
166
|
-
Log.i(
|
|
167
|
-
TAG,
|
|
168
|
-
"Glasses WiFi network=$network is foreground and ready " +
|
|
169
|
-
"processBoundNetwork=${manager.boundNetworkForProcess}",
|
|
170
|
-
)
|
|
171
|
-
val localAddress =
|
|
172
|
-
manager.getLinkProperties(network)
|
|
173
|
-
?.linkAddresses
|
|
174
|
-
?.firstOrNull { it.address is Inet4Address }
|
|
175
|
-
?.address
|
|
176
|
-
?.hostAddress
|
|
177
|
-
connectPromise?.resolve(
|
|
178
|
-
buildMap<String, Any> {
|
|
179
|
-
put("connected", true)
|
|
180
|
-
put("ssid", ssid)
|
|
181
|
-
if (!localAddress.isNullOrBlank()) put("localAddress", localAddress)
|
|
182
|
-
},
|
|
183
|
-
)
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
override fun onUnavailable() {
|
|
187
|
-
synchronized(lock) {
|
|
188
|
-
if (networkCallback !== this) {
|
|
189
|
-
Log.d(TAG, "Ignoring onUnavailable from a stale network request")
|
|
190
|
-
return
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
rejectPendingConnect("ERR_LOCAL_WIFI_UNAVAILABLE", "Could not connect to $ssid")
|
|
194
|
-
clearNetworkCallback(this)
|
|
136
|
+
try {
|
|
137
|
+
network.join(ssid, password, listener, incoming)
|
|
138
|
+
if (synchronized(lock) { scoped !== network }) {
|
|
139
|
+
network.release()
|
|
140
|
+
promise.reject("ERR_LOCAL_WIFI_CANCELLED", "Local WiFi connection cancelled", null)
|
|
141
|
+
return@execute
|
|
195
142
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
143
|
+
Log.i(TAG, "Glasses WiFi network=${network.network()} is ready")
|
|
144
|
+
promise.resolve(
|
|
145
|
+
buildMap<String, Any> {
|
|
146
|
+
put("connected", true)
|
|
147
|
+
put("ssid", ssid)
|
|
148
|
+
network.localIpv4()?.let { put("localAddress", it) }
|
|
149
|
+
},
|
|
150
|
+
)
|
|
151
|
+
} catch (error: Exception) {
|
|
152
|
+
val cancelled = synchronized(lock) { (scoped !== network).also { if (!it) scoped = null } }
|
|
153
|
+
if (cancelled) {
|
|
154
|
+
promise.reject("ERR_LOCAL_WIFI_CANCELLED", "Local WiFi connection cancelled", error)
|
|
155
|
+
} else {
|
|
156
|
+
network.release()
|
|
157
|
+
val code = (error as? ScopedNetworkError)?.code ?: "ERR_LOCAL_WIFI_REQUEST"
|
|
158
|
+
promise.reject(code, error.message ?: "Could not connect to $ssid", error)
|
|
212
159
|
}
|
|
213
160
|
}
|
|
214
|
-
|
|
215
|
-
synchronized(lock) {
|
|
216
|
-
pendingConnectPromise = promise
|
|
217
|
-
networkCallback = callback
|
|
218
|
-
}
|
|
219
|
-
try {
|
|
220
|
-
manager.requestNetwork(request, callback, 30_000)
|
|
221
|
-
} catch (error: Exception) {
|
|
222
|
-
clearNetworkCallback(callback)
|
|
223
|
-
rejectPendingConnect("ERR_LOCAL_WIFI_REQUEST", error.message ?: "Local WiFi request failed", error)
|
|
224
161
|
}
|
|
225
162
|
}
|
|
226
163
|
|
|
@@ -334,7 +271,7 @@ class MentraLocalNetworkModule : Module() {
|
|
|
334
271
|
}
|
|
335
272
|
|
|
336
273
|
private fun openConnection(requestId: String, url: String): HttpURLConnection {
|
|
337
|
-
val network = synchronized(lock) {
|
|
274
|
+
val network = synchronized(lock) { scoped }?.network()
|
|
338
275
|
?: throw IllegalStateException("Local WiFi is not connected")
|
|
339
276
|
val connection = network.openConnection(URL(url)) as HttpURLConnection
|
|
340
277
|
activeConnections[requestId] = connection
|
|
@@ -354,85 +291,9 @@ class MentraLocalNetworkModule : Module() {
|
|
|
354
291
|
activeConnections.keys.toList().forEach(::cancel)
|
|
355
292
|
}
|
|
356
293
|
|
|
357
|
-
private fun scheduleReadinessTimeout(
|
|
358
|
-
callback: ConnectivityManager.NetworkCallback,
|
|
359
|
-
ssid: String,
|
|
360
|
-
) {
|
|
361
|
-
val timeout =
|
|
362
|
-
Runnable {
|
|
363
|
-
val connectPromise = synchronized(lock) {
|
|
364
|
-
if (networkCallback !== callback || pendingConnectPromise == null) return@Runnable
|
|
365
|
-
pendingReadinessTimeout = null
|
|
366
|
-
val promise = pendingConnectPromise
|
|
367
|
-
pendingConnectPromise = null
|
|
368
|
-
promise
|
|
369
|
-
}
|
|
370
|
-
connectPromise?.reject(
|
|
371
|
-
"ERR_LOCAL_WIFI_NOT_READY",
|
|
372
|
-
"Connected to $ssid but the network did not become ready",
|
|
373
|
-
null,
|
|
374
|
-
)
|
|
375
|
-
clearNetworkCallback(callback)
|
|
376
|
-
}
|
|
377
|
-
synchronized(lock) {
|
|
378
|
-
if (networkCallback !== callback || pendingConnectPromise == null) return
|
|
379
|
-
cancelReadinessTimeoutLocked()
|
|
380
|
-
pendingReadinessTimeout = timeout
|
|
381
|
-
}
|
|
382
|
-
handler.postDelayed(timeout, NETWORK_READINESS_TIMEOUT_MS)
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
private fun cancelReadinessTimeoutLocked() {
|
|
386
|
-
pendingReadinessTimeout?.let(handler::removeCallbacks)
|
|
387
|
-
pendingReadinessTimeout = null
|
|
388
|
-
}
|
|
389
|
-
|
|
390
294
|
private fun disconnect() {
|
|
391
295
|
cancelAll()
|
|
392
|
-
val
|
|
393
|
-
|
|
394
|
-
synchronized(lock) {
|
|
395
|
-
callback = networkCallback
|
|
396
|
-
networkCallback = null
|
|
397
|
-
localNetwork = null
|
|
398
|
-
networkReadiness.clear()
|
|
399
|
-
cancelReadinessTimeoutLocked()
|
|
400
|
-
}
|
|
401
|
-
if (callback != null && manager != null) {
|
|
402
|
-
try {
|
|
403
|
-
manager.unregisterNetworkCallback(callback)
|
|
404
|
-
} catch (_: IllegalArgumentException) {
|
|
405
|
-
// Already unregistered by the platform.
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
rejectPendingConnect("ERR_LOCAL_WIFI_CANCELLED", "Local WiFi connection cancelled")
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
private fun clearNetworkCallback(callback: ConnectivityManager.NetworkCallback) {
|
|
412
|
-
val manager = appContext.reactContext?.getSystemService(ConnectivityManager::class.java)
|
|
413
|
-
synchronized(lock) {
|
|
414
|
-
if (networkCallback == callback) {
|
|
415
|
-
networkCallback = null
|
|
416
|
-
localNetwork = null
|
|
417
|
-
networkReadiness.clear()
|
|
418
|
-
cancelReadinessTimeoutLocked()
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
if (manager != null) {
|
|
422
|
-
try {
|
|
423
|
-
manager.unregisterNetworkCallback(callback)
|
|
424
|
-
} catch (_: IllegalArgumentException) {
|
|
425
|
-
// Callback was not registered or has already been removed.
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
private fun rejectPendingConnect(code: String, message: String, cause: Throwable? = null) {
|
|
431
|
-
val promise: Promise?
|
|
432
|
-
synchronized(lock) {
|
|
433
|
-
promise = pendingConnectPromise
|
|
434
|
-
pendingConnectPromise = null
|
|
435
|
-
}
|
|
436
|
-
promise?.reject(code, message, cause)
|
|
296
|
+
val network = synchronized(lock) { scoped.also { scoped = null } }
|
|
297
|
+
network?.release()
|
|
437
298
|
}
|
|
438
299
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.hotspot
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An IPv4 subnet, taken from the phone's own address on a joined network.
|
|
5
|
+
*
|
|
6
|
+
* SoftAP correctness is an interface question, not an address-range question: the media path is
|
|
7
|
+
* right when the selected candidate sits on the network we joined, and wrong otherwise. Matching
|
|
8
|
+
* "looks RFC1918" or "starts with 192.168.43" answers a different question and stops being true the
|
|
9
|
+
* moment an OEM picks another hotspot subnet, so every SoftAP check compares against the prefix
|
|
10
|
+
* `LinkProperties` actually reported.
|
|
11
|
+
*/
|
|
12
|
+
data class Ipv4Prefix(val address: String, val prefixLength: Int) {
|
|
13
|
+
|
|
14
|
+
private val network: Int? = packIpv4(address)?.let { it and maskFor(prefixLength) }
|
|
15
|
+
|
|
16
|
+
/** True when [candidate] is a dotted-quad IPv4 address inside this prefix. */
|
|
17
|
+
fun contains(candidate: String?): Boolean {
|
|
18
|
+
val base = network ?: return false
|
|
19
|
+
val value = packIpv4(candidate) ?: return false
|
|
20
|
+
return (value and maskFor(prefixLength)) == base
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
override fun toString(): String = "$address/$prefixLength"
|
|
24
|
+
|
|
25
|
+
companion object {
|
|
26
|
+
/**
|
|
27
|
+
* Dotted-quad to a packed int, or null when [token] is not one. Deliberately strict: a
|
|
28
|
+
* token like `192.168.43` or `candidate:1` must not be read as an address, because the
|
|
29
|
+
* callers scan whitespace-split SDP lines where most tokens are not addresses at all.
|
|
30
|
+
*/
|
|
31
|
+
fun packIpv4(token: String?): Int? {
|
|
32
|
+
val text = token ?: return null
|
|
33
|
+
val octets = text.split('.')
|
|
34
|
+
if (octets.size != 4) return null
|
|
35
|
+
var packed = 0
|
|
36
|
+
for (octet in octets) {
|
|
37
|
+
if (octet.isEmpty() || octet.length > 3) return null
|
|
38
|
+
val value = octet.toIntOrNull() ?: return null
|
|
39
|
+
if (value < 0 || value > 255) return null
|
|
40
|
+
packed = (packed shl 8) or value
|
|
41
|
+
}
|
|
42
|
+
return packed
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A /0 masks nothing; Kotlin's `shl 32` is a no-op, so that case is spelled out. */
|
|
46
|
+
private fun maskFor(prefixLength: Int): Int {
|
|
47
|
+
val bits = prefixLength.coerceIn(0, 32)
|
|
48
|
+
return if (bits == 0) 0 else (-1 shl (32 - bits))
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.hotspot
|
|
2
|
+
|
|
3
|
+
import java.net.Inet4Address
|
|
4
|
+
import java.net.NetworkInterface
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The kernel's interface table, as libwebrtc's `getifaddrs` sees it.
|
|
8
|
+
*
|
|
9
|
+
* Every SoftAP ICE failure so far has come from our view of the network disagreeing with
|
|
10
|
+
* libwebrtc's, and the two views come from different sources. We publish
|
|
11
|
+
* [ConnectivityManager.getLinkProperties][android.net.ConnectivityManager.getLinkProperties] for
|
|
12
|
+
* the scoped `Network`; libwebrtc enumerates `getifaddrs` and only asks our monitor for each
|
|
13
|
+
* *interface name*'s adapter type. When those disagree — an address on a name we never typed, or a
|
|
14
|
+
* scoped address absent from the table entirely — no mask and no inventory can help, because ICE
|
|
15
|
+
* gathers per address, not per `Network`.
|
|
16
|
+
*
|
|
17
|
+
* `java.net.NetworkInterface` is getifaddrs-backed, so this is the closest thing to libwebrtc's
|
|
18
|
+
* own input that we can read from Kotlin. Pure enough to unit test, which matters: it is the
|
|
19
|
+
* evidence the next diagnosis rests on.
|
|
20
|
+
*/
|
|
21
|
+
object NetworkFacts {
|
|
22
|
+
|
|
23
|
+
/** One interface, with the flags libwebrtc's enumeration also keys off. */
|
|
24
|
+
data class Iface(
|
|
25
|
+
val name: String,
|
|
26
|
+
val index: Int,
|
|
27
|
+
val up: Boolean,
|
|
28
|
+
val loopback: Boolean,
|
|
29
|
+
val virtual: Boolean,
|
|
30
|
+
val pointToPoint: Boolean,
|
|
31
|
+
val mtu: Int,
|
|
32
|
+
/** IPv4 only, as `address/prefixLength` — the form libwebrtc turns into a Network key. */
|
|
33
|
+
val ipv4: List<String>,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
/** Read the table. Never throws: a diagnostic that crashes the call is worse than no data. */
|
|
37
|
+
fun snapshot(): List<Iface> =
|
|
38
|
+
runCatching {
|
|
39
|
+
NetworkInterface.getNetworkInterfaces()?.toList().orEmpty().map { candidate ->
|
|
40
|
+
Iface(
|
|
41
|
+
name = candidate.name ?: "?",
|
|
42
|
+
index = runCatching { candidate.index }.getOrDefault(-1),
|
|
43
|
+
up = runCatching { candidate.isUp }.getOrDefault(false),
|
|
44
|
+
loopback = runCatching { candidate.isLoopback }.getOrDefault(false),
|
|
45
|
+
virtual = runCatching { candidate.isVirtual }.getOrDefault(false),
|
|
46
|
+
pointToPoint = runCatching { candidate.isPointToPoint }.getOrDefault(false),
|
|
47
|
+
mtu = runCatching { candidate.mtu }.getOrDefault(-1),
|
|
48
|
+
ipv4 =
|
|
49
|
+
runCatching {
|
|
50
|
+
candidate.interfaceAddresses
|
|
51
|
+
.filter { it.address is Inet4Address }
|
|
52
|
+
.map { "${it.address.hostAddress}/${it.networkPrefixLength}" }
|
|
53
|
+
}.getOrDefault(emptyList()),
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
}.getOrElse { emptyList() }
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Only interfaces that can carry a host candidate: up, non-loopback, with an IPv4 address.
|
|
60
|
+
* Anything here is something ICE may gather on.
|
|
61
|
+
*/
|
|
62
|
+
fun gatherable(table: List<Iface> = snapshot()): List<Iface> =
|
|
63
|
+
table.filter { it.up && !it.loopback && it.ipv4.isNotEmpty() }
|
|
64
|
+
|
|
65
|
+
/** The interface the kernel says owns [address], or null when the table has never seen it. */
|
|
66
|
+
fun ownerOf(address: String, table: List<Iface> = snapshot()): String? =
|
|
67
|
+
table.firstOrNull { iface -> iface.ipv4.any { it.substringBefore('/') == address } }?.name
|
|
68
|
+
|
|
69
|
+
/** One log line per interface, flags abbreviated so a full table fits in a logcat entry. */
|
|
70
|
+
fun render(table: List<Iface>): String =
|
|
71
|
+
table.joinToString(" ") { iface ->
|
|
72
|
+
val flags =
|
|
73
|
+
buildString {
|
|
74
|
+
if (iface.up) append("U")
|
|
75
|
+
if (iface.loopback) append("L")
|
|
76
|
+
if (iface.virtual) append("V")
|
|
77
|
+
if (iface.pointToPoint) append("P")
|
|
78
|
+
}
|
|
79
|
+
"${iface.name}#${iface.index}[$flags mtu=${iface.mtu}](${iface.ipv4.joinToString(",")})"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.hotspot
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Typed failures for a scoped SoftAP join.
|
|
5
|
+
*
|
|
6
|
+
* A denied local-network permission must surface as a distinct, immediate error rather than a
|
|
7
|
+
* 30-second hang that looks like a flaky hotspot. `ACCESS_LOCAL_NETWORK` sits in the nearby-devices
|
|
8
|
+
* group, so it is often already granted during development and easy to miss until a user without it
|
|
9
|
+
* hits a silent timeout.
|
|
10
|
+
*/
|
|
11
|
+
sealed class ScopedNetworkError(val code: String, message: String) : Exception(message) {
|
|
12
|
+
|
|
13
|
+
/** `ACCESS_LOCAL_NETWORK` (or the nearby-devices prerequisite) was not granted. */
|
|
14
|
+
class PermissionDenied(val permission: String) :
|
|
15
|
+
ScopedNetworkError(
|
|
16
|
+
CODE_PERMISSION_DENIED,
|
|
17
|
+
"Local network permission not granted: $permission",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
/** No usable network arrived before the request timeout. */
|
|
21
|
+
class Timeout(val ssid: String, val timeoutMs: Int) :
|
|
22
|
+
ScopedNetworkError(CODE_TIMEOUT, "Timed out joining $ssid after ${timeoutMs}ms")
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The framework reported it cannot provide this network at all.
|
|
26
|
+
*
|
|
27
|
+
* WifiNetworkSpecifier does not keep scanning for the request timeout. If the glasses AP is
|
|
28
|
+
* not in the current scan — or the user dismissed the system join sheet — this arrives in
|
|
29
|
+
* well under a second.
|
|
30
|
+
*/
|
|
31
|
+
class Unavailable(val ssid: String) :
|
|
32
|
+
ScopedNetworkError(
|
|
33
|
+
CODE_UNAVAILABLE,
|
|
34
|
+
"Could not join $ssid (SSID not in scan, Wi-Fi off, or the system join prompt was dismissed)",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
/** The network was joined and then went away mid-call. */
|
|
38
|
+
class Lost(val ssid: String) :
|
|
39
|
+
ScopedNetworkError(CODE_LOST, "Lost the connection to $ssid")
|
|
40
|
+
|
|
41
|
+
/** `requestNetwork` itself threw. */
|
|
42
|
+
class RequestFailed(val reason: String) :
|
|
43
|
+
ScopedNetworkError(CODE_REQUEST_FAILED, "Scoped network request failed: $reason")
|
|
44
|
+
|
|
45
|
+
/** Joined, but no IPv4 address was assigned, so nothing can bind to the link. */
|
|
46
|
+
class NoLocalAddress(val ssid: String) :
|
|
47
|
+
ScopedNetworkError(CODE_NO_LOCAL_ADDRESS, "Joined $ssid but no IPv4 address was assigned")
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Joined, but the network never became usable for ICE: a callback never reported, or the
|
|
51
|
+
* address never appeared in the kernel's interface table.
|
|
52
|
+
*
|
|
53
|
+
* Failing here is deliberate. Proceeding produces an ICE answer with no hotspot candidate,
|
|
54
|
+
* which is the same symptom as several unrelated faults and cost real time to attribute.
|
|
55
|
+
*/
|
|
56
|
+
class NotReady(val ssid: String, val missing: String) :
|
|
57
|
+
ScopedNetworkError(
|
|
58
|
+
CODE_NOT_READY,
|
|
59
|
+
"Joined $ssid but the network never became usable: still waiting for $missing",
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
/** Phone Wi-Fi radio is off. WifiNetworkSpecifier then fails as Unavailable in under a second. */
|
|
63
|
+
class WifiDisabled :
|
|
64
|
+
ScopedNetworkError(CODE_WIFI_DISABLED, "Phone Wi-Fi is off; turn it on before joining the glasses hotspot")
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A VPN owns this app's UID. netd then refuses to bind our sockets to the hotspot network
|
|
68
|
+
* (`EPERM`), and the glasses' TCP handshake to the WHIP listener never completes because the
|
|
69
|
+
* reply path is captured by the tunnel. Verified on-device: a shell-UID listener on the same
|
|
70
|
+
* address accepts the glasses' connection while an app-UID listener times out. The scoped join
|
|
71
|
+
* itself succeeds, so without this check the failure only shows up ten seconds into the camera
|
|
72
|
+
* step as an opaque connect timeout.
|
|
73
|
+
*/
|
|
74
|
+
class VpnCapturesApp :
|
|
75
|
+
ScopedNetworkError(
|
|
76
|
+
CODE_VPN_ACTIVE,
|
|
77
|
+
"A VPN is routing this app's traffic, so the glasses cannot reach the phone. " +
|
|
78
|
+
"Exclude Mentra from the VPN (split tunneling) or turn the VPN off, then retry",
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
companion object {
|
|
82
|
+
const val CODE_PERMISSION_DENIED = "SOFTAP_PERMISSION_DENIED"
|
|
83
|
+
const val CODE_TIMEOUT = "SOFTAP_JOIN_TIMEOUT"
|
|
84
|
+
const val CODE_UNAVAILABLE = "SOFTAP_UNAVAILABLE"
|
|
85
|
+
const val CODE_LOST = "SOFTAP_NETWORK_LOST"
|
|
86
|
+
const val CODE_REQUEST_FAILED = "SOFTAP_REQUEST_FAILED"
|
|
87
|
+
const val CODE_NO_LOCAL_ADDRESS = "SOFTAP_NO_LOCAL_ADDRESS"
|
|
88
|
+
const val CODE_NOT_READY = "SOFTAP_NETWORK_NOT_READY"
|
|
89
|
+
const val CODE_WIFI_DISABLED = "SOFTAP_WIFI_DISABLED"
|
|
90
|
+
const val CODE_VPN_ACTIVE = "SOFTAP_VPN_ACTIVE"
|
|
91
|
+
|
|
92
|
+
/** Map a terminal state-machine failure onto the typed error the call path reports. */
|
|
93
|
+
fun from(failure: ScopedNetworkState.Failure, ssid: String, timeoutMs: Int): ScopedNetworkError? =
|
|
94
|
+
when (failure) {
|
|
95
|
+
ScopedNetworkState.Failure.NONE -> null
|
|
96
|
+
ScopedNetworkState.Failure.TIMEOUT -> Timeout(ssid, timeoutMs)
|
|
97
|
+
ScopedNetworkState.Failure.UNAVAILABLE -> Unavailable(ssid)
|
|
98
|
+
ScopedNetworkState.Failure.LOST -> Lost(ssid)
|
|
99
|
+
ScopedNetworkState.Failure.PERMISSION_DENIED -> PermissionDenied(LOCAL_NETWORK_PERMISSION)
|
|
100
|
+
ScopedNetworkState.Failure.REQUEST_FAILED -> RequestFailed("requestNetwork threw")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Enforced on Android 17+ for apps targeting SDK 37+. Hosts targeting older SDKs
|
|
105
|
+
* retain implicit access through INTERNET and must not request this permission.
|
|
106
|
+
*/
|
|
107
|
+
const val LOCAL_NETWORK_PERMISSION = "android.permission.ACCESS_LOCAL_NETWORK"
|
|
108
|
+
}
|
|
109
|
+
}
|