@mentra/glasses-media 3.4.0-dev.998 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/android/build.gradle +1 -0
- package/android/src/main/java/com/mentra/glassesmedia/GlassesMediaRelayModule.kt +12 -10
- package/android/src/main/java/com/mentra/glassesmedia/network/InternetHold.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/network/ScopedNetworkChangeDetector.kt +3 -1
- package/android/src/main/java/com/mentra/glassesmedia/network/ScopedNetworkObserver.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/publisher/PhoneWhipPublisher.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/CloudflareWhepSource.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/DecodedFrameTap.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/DecodedTrackRelay.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/GlassesMediaSource.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/LocalWhipIngestSource.kt +4 -4
- package/android/src/main/java/com/mentra/glassesmedia/source/SelectedIcePair.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/SoftApIceDiagnosis.kt +3 -2
- package/android/src/main/java/com/mentra/glassesmedia/source/SoftApSdpGuard.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/SyntheticI420Source.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/source/VideoSourceArm.kt +2 -0
- package/android/src/main/java/com/mentra/glassesmedia/source/WhipIngestServer.kt +3 -3
- package/android/src/main/java/com/mentra/glassesmedia/telemetry/AvSyncProbe.kt +1 -1
- package/android/src/main/java/com/mentra/glassesmedia/trace/MediaLog.kt +35 -0
- package/index.d.ts +6 -0
- package/index.js +21 -0
- package/ios/CoreKit/Sources/GlassesMediaCore/HotspotJoinFailure.swift +4 -0
- package/ios/CoreKit/Sources/GlassesMediaCore/HotspotReapplyPolicy.swift +26 -0
- package/ios/CoreKit/Sources/GlassesMediaCore/LocalNetworkAccessRequest.swift +33 -7
- package/ios/CoreKit/Sources/GlassesMediaCore/MacHotspotAssociation.swift +146 -18
- package/ios/GlassesHotspotNetwork.swift +90 -33
- package/ios/GlassesMediaRelayModule.swift +19 -2
- package/ios/LocalWhipIngestSource.swift +2 -2
- package/ios/MediaLog.swift +13 -0
- package/ios/WhepVideoSource.swift +8 -8
- package/package.json +4 -1
- package/android/src/main/java/com/mentra/glassesmedia/network/Ipv4Prefix.kt +0 -51
- package/android/src/main/java/com/mentra/glassesmedia/network/NetworkFacts.kt +0 -81
- package/android/src/main/java/com/mentra/glassesmedia/network/ScopedNetworkError.kt +0 -109
- package/android/src/main/java/com/mentra/glassesmedia/network/ScopedNetworkReadiness.kt +0 -103
- package/android/src/main/java/com/mentra/glassesmedia/network/ScopedNetworkRequestSpec.kt +0 -53
- package/android/src/main/java/com/mentra/glassesmedia/network/ScopedNetworkState.kt +0 -127
- package/android/src/main/java/com/mentra/glassesmedia/network/ScopedSoftApNetwork.kt +0 -573
- package/android/src/main/java/com/mentra/glassesmedia/trace/SoftApTrace.kt +0 -143
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
package com.mentra.glassesmedia.network
|
|
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
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
package com.mentra.glassesmedia.network
|
|
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
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
package com.mentra.glassesmedia.network
|
|
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
|
-
}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
package com.mentra.glassesmedia.network
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Decides when a joined scoped network is actually ready for ICE to gather on.
|
|
5
|
-
*
|
|
6
|
-
* `onAvailable` is not that moment. Android's own guidance is to wait for `onLinkPropertiesChanged`
|
|
7
|
-
* and `onCapabilitiesChanged` rather than reading properties synchronously inside `onAvailable`, and
|
|
8
|
-
* on device the consequence was concrete: the hotspot address can land in the kernel's interface
|
|
9
|
-
* table *after* `onAvailable` returns, and libwebrtc gathers from that table. Starting ingest on
|
|
10
|
-
* `onAvailable` therefore raced the address into existence, and losing that race is indistinguishable
|
|
11
|
-
* from the wiring bugs it was mistaken for.
|
|
12
|
-
*
|
|
13
|
-
* Readiness needs agreement from three places, so all three are recorded separately and the missing
|
|
14
|
-
* one is named. The kernel table is the odd one out: it has no callback, so the shell polls it and
|
|
15
|
-
* feeds the answer in through [onInterfaceTable].
|
|
16
|
-
*
|
|
17
|
-
* Pure and mutable-but-self-contained, so the awkward orderings are unit testable without a device.
|
|
18
|
-
*/
|
|
19
|
-
class ScopedNetworkReadiness {
|
|
20
|
-
|
|
21
|
-
/** What each source has reported so far. Null means "not yet", not "absent". */
|
|
22
|
-
data class Observations(
|
|
23
|
-
val available: Boolean = false,
|
|
24
|
-
val capabilities: Boolean = false,
|
|
25
|
-
val linkInterface: String? = null,
|
|
26
|
-
val linkAddress: String? = null,
|
|
27
|
-
val tableOwner: String? = null,
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
sealed interface Verdict {
|
|
31
|
-
/**
|
|
32
|
-
* [owner] is the interface the kernel says holds [address]. It can differ from
|
|
33
|
-
* [interfaceName], which is what `LinkProperties` claimed — a disagreement worth logging,
|
|
34
|
-
* not worth blocking on, since ICE only ever uses the address.
|
|
35
|
-
*/
|
|
36
|
-
data class Ready(
|
|
37
|
-
val address: String,
|
|
38
|
-
val interfaceName: String?,
|
|
39
|
-
val owner: String,
|
|
40
|
-
) : Verdict
|
|
41
|
-
|
|
42
|
-
/** [missing] names the one source still to report, for the timeout message. */
|
|
43
|
-
data class Waiting(val missing: String) : Verdict
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
@Volatile
|
|
47
|
-
var observations = Observations()
|
|
48
|
-
private set
|
|
49
|
-
|
|
50
|
-
private val lock = Any()
|
|
51
|
-
|
|
52
|
-
fun onAvailable() = update { it.copy(available = true) }
|
|
53
|
-
|
|
54
|
-
fun onCapabilities() = update { it.copy(capabilities = true) }
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Taken verbatim: `LinkProperties` carries the network's whole current state, so a later
|
|
58
|
-
* callback without an address means the address is gone, not that the old one still holds.
|
|
59
|
-
*/
|
|
60
|
-
fun onLinkProperties(interfaceName: String?, address: String?) =
|
|
61
|
-
update { it.copy(linkInterface = interfaceName, linkAddress = address) }
|
|
62
|
-
|
|
63
|
-
/** Seed from the synchronous read in `onAvailable`; never clears what a callback reported. */
|
|
64
|
-
fun seedLinkProperties(interfaceName: String?, address: String?) =
|
|
65
|
-
update {
|
|
66
|
-
if (address == null) it else it.copy(linkInterface = interfaceName, linkAddress = address)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
fun onInterfaceTable(owner: String?) = update { it.copy(tableOwner = owner) }
|
|
70
|
-
|
|
71
|
-
fun reset() = update { Observations() }
|
|
72
|
-
|
|
73
|
-
fun verdict(): Verdict = verdictOf(observations)
|
|
74
|
-
|
|
75
|
-
/** True once everything with a callback has reported, so only the table poll is left. */
|
|
76
|
-
fun callbacksSatisfied(): Boolean = callbacksSatisfiedBy(observations)
|
|
77
|
-
|
|
78
|
-
private inline fun update(transform: (Observations) -> Observations) {
|
|
79
|
-
synchronized(lock) { observations = transform(observations) }
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
companion object {
|
|
83
|
-
fun callbacksSatisfiedBy(observations: Observations): Boolean =
|
|
84
|
-
observations.available && observations.capabilities && observations.linkAddress != null
|
|
85
|
-
|
|
86
|
-
/** Ordered so the named blocker is the earliest unmet one, which is the useful one. */
|
|
87
|
-
fun verdictOf(observations: Observations): Verdict =
|
|
88
|
-
when {
|
|
89
|
-
!observations.available -> Verdict.Waiting("onAvailable")
|
|
90
|
-
!observations.capabilities -> Verdict.Waiting("onCapabilitiesChanged")
|
|
91
|
-
observations.linkAddress == null ->
|
|
92
|
-
Verdict.Waiting("an IPv4 address from onLinkPropertiesChanged")
|
|
93
|
-
observations.tableOwner == null ->
|
|
94
|
-
Verdict.Waiting("the kernel interface table to carry ${observations.linkAddress}")
|
|
95
|
-
else ->
|
|
96
|
-
Verdict.Ready(
|
|
97
|
-
observations.linkAddress,
|
|
98
|
-
observations.linkInterface,
|
|
99
|
-
observations.tableOwner,
|
|
100
|
-
)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
package com.mentra.glassesmedia.network
|
|
2
|
-
|
|
3
|
-
import android.net.NetworkCapabilities
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Declarative description of the SoftAP network request.
|
|
7
|
-
*
|
|
8
|
-
* `NetworkRequest.Builder` cannot be constructed in a plain JVM test, so the shape of the request
|
|
9
|
-
* lives here as data and [ScopedSoftApNetwork] translates it into the real builder. That makes the
|
|
10
|
-
* one property this architecture depends on — that the request does **not** ask for
|
|
11
|
-
* `NET_CAPABILITY_INTERNET` — assertable without a device.
|
|
12
|
-
*
|
|
13
|
-
* Dropping the internet capability is what keeps the phone dual-homed: Android will not promote the
|
|
14
|
-
* glasses hotspot to the default network, so ACS traffic stays on cellular while media crosses the
|
|
15
|
-
* local link. The other half of that guarantee is the MTK firmware patch that stops the AP
|
|
16
|
-
* advertising a default route.
|
|
17
|
-
*/
|
|
18
|
-
data class ScopedNetworkRequestSpec(
|
|
19
|
-
val ssid: String,
|
|
20
|
-
val passphrase: String,
|
|
21
|
-
val transportTypes: Set<Int>,
|
|
22
|
-
val removedCapabilities: Set<Int>,
|
|
23
|
-
val timeoutMs: Int,
|
|
24
|
-
) {
|
|
25
|
-
/** True when the request cannot be satisfied by, or promoted to, an internet-bearing network. */
|
|
26
|
-
val avoidsInternetCapability: Boolean
|
|
27
|
-
get() = removedCapabilities.contains(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
28
|
-
|
|
29
|
-
val hasPassphrase: Boolean
|
|
30
|
-
get() = passphrase.isNotEmpty()
|
|
31
|
-
|
|
32
|
-
companion object {
|
|
33
|
-
/**
|
|
34
|
-
* Android caps `requestNetwork` timeouts well above this; 30 s matches the value the
|
|
35
|
-
* Bluetooth SDK already uses for the same hotspot and is long enough for a cold AP start.
|
|
36
|
-
*/
|
|
37
|
-
const val DEFAULT_TIMEOUT_MS = 30_000
|
|
38
|
-
|
|
39
|
-
/** The canonical SoftAP request. Every caller should go through this. */
|
|
40
|
-
fun forSoftAp(
|
|
41
|
-
ssid: String,
|
|
42
|
-
passphrase: String,
|
|
43
|
-
timeoutMs: Int = DEFAULT_TIMEOUT_MS,
|
|
44
|
-
): ScopedNetworkRequestSpec =
|
|
45
|
-
ScopedNetworkRequestSpec(
|
|
46
|
-
ssid = ssid,
|
|
47
|
-
passphrase = passphrase,
|
|
48
|
-
transportTypes = setOf(NetworkCapabilities.TRANSPORT_WIFI),
|
|
49
|
-
removedCapabilities = setOf(NetworkCapabilities.NET_CAPABILITY_INTERNET),
|
|
50
|
-
timeoutMs = timeoutMs,
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
package com.mentra.glassesmedia.network
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Pure lifecycle state machine for a scoped SoftAP network join.
|
|
5
|
-
*
|
|
6
|
-
* Split from the Android shell so the awkward parts — duplicate callbacks, callbacks arriving after
|
|
7
|
-
* a cancel, a timeout racing a late `onAvailable` — are unit testable without a device. The shell
|
|
8
|
-
* ([ScopedSoftApNetwork]) owns `ConnectivityManager` and forwards raw callbacks in here.
|
|
9
|
-
*
|
|
10
|
-
* `ConnectivityManager` gives no ordering or at-most-once guarantee: `onAvailable` can fire more
|
|
11
|
-
* than once, `onCapabilitiesChanged` fires repeatedly, and callbacks from a request that was already
|
|
12
|
-
* unregistered can still land. Every transition below is therefore idempotent and generation-guarded.
|
|
13
|
-
*/
|
|
14
|
-
class ScopedNetworkState {
|
|
15
|
-
|
|
16
|
-
enum class Phase {
|
|
17
|
-
/** No request outstanding. */
|
|
18
|
-
IDLE,
|
|
19
|
-
|
|
20
|
-
/** `requestNetwork` issued, waiting for a usable network. */
|
|
21
|
-
REQUESTING,
|
|
22
|
-
|
|
23
|
-
/** Network joined and usable. */
|
|
24
|
-
AVAILABLE,
|
|
25
|
-
|
|
26
|
-
/** Terminal for this generation: released, lost, timed out, or failed. */
|
|
27
|
-
LOST,
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Why a join ended without producing a usable network. */
|
|
31
|
-
enum class Failure {
|
|
32
|
-
NONE,
|
|
33
|
-
TIMEOUT,
|
|
34
|
-
UNAVAILABLE,
|
|
35
|
-
LOST,
|
|
36
|
-
PERMISSION_DENIED,
|
|
37
|
-
REQUEST_FAILED,
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
var phase: Phase = Phase.IDLE
|
|
41
|
-
private set
|
|
42
|
-
|
|
43
|
-
var failure: Failure = Failure.NONE
|
|
44
|
-
private set
|
|
45
|
-
|
|
46
|
-
/** Incremented on every new request so late callbacks from an old one can be rejected. */
|
|
47
|
-
var generation: Int = 0
|
|
48
|
-
private set
|
|
49
|
-
|
|
50
|
-
val isActive: Boolean
|
|
51
|
-
get() = phase == Phase.REQUESTING || phase == Phase.AVAILABLE
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Begin a new join. Any in-flight generation is abandoned, so its callbacks become stale.
|
|
55
|
-
*
|
|
56
|
-
* @return the generation for this request; pass it back to every callback
|
|
57
|
-
*/
|
|
58
|
-
fun startRequest(): Int {
|
|
59
|
-
generation += 1
|
|
60
|
-
phase = Phase.REQUESTING
|
|
61
|
-
failure = Failure.NONE
|
|
62
|
-
return generation
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** True when a callback belongs to the current, still-active generation. */
|
|
66
|
-
fun accepts(callbackGeneration: Int): Boolean = callbackGeneration == generation && isActive
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* A usable network arrived.
|
|
70
|
-
*
|
|
71
|
-
* @return true if this transitioned the machine; false when stale or already available
|
|
72
|
-
*/
|
|
73
|
-
fun onAvailable(callbackGeneration: Int): Boolean {
|
|
74
|
-
if (!accepts(callbackGeneration)) return false
|
|
75
|
-
if (phase == Phase.AVAILABLE) return false // duplicate onAvailable
|
|
76
|
-
phase = Phase.AVAILABLE
|
|
77
|
-
return true
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** The request timed out before a usable network arrived. Ignored once available. */
|
|
81
|
-
fun onTimeout(callbackGeneration: Int): Boolean {
|
|
82
|
-
if (callbackGeneration != generation) return false
|
|
83
|
-
if (phase != Phase.REQUESTING) return false
|
|
84
|
-
phase = Phase.LOST
|
|
85
|
-
failure = Failure.TIMEOUT
|
|
86
|
-
return true
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** The framework reported the network cannot be provided. */
|
|
90
|
-
fun onUnavailable(callbackGeneration: Int): Boolean {
|
|
91
|
-
if (!accepts(callbackGeneration)) return false
|
|
92
|
-
phase = Phase.LOST
|
|
93
|
-
failure = Failure.UNAVAILABLE
|
|
94
|
-
return true
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** The joined network went away. This is the one the call path must react to. */
|
|
98
|
-
fun onLost(callbackGeneration: Int): Boolean {
|
|
99
|
-
if (!accepts(callbackGeneration)) return false
|
|
100
|
-
phase = Phase.LOST
|
|
101
|
-
failure = Failure.LOST
|
|
102
|
-
return true
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** `requestNetwork` threw, or the local-network permission was denied. */
|
|
106
|
-
fun onRequestFailed(callbackGeneration: Int, permissionDenied: Boolean): Boolean {
|
|
107
|
-
if (callbackGeneration != generation) return false
|
|
108
|
-
if (!isActive) return false
|
|
109
|
-
phase = Phase.LOST
|
|
110
|
-
failure = if (permissionDenied) Failure.PERMISSION_DENIED else Failure.REQUEST_FAILED
|
|
111
|
-
return true
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Caller-initiated teardown. Not a failure. */
|
|
115
|
-
fun release(): Boolean {
|
|
116
|
-
if (!isActive) return false
|
|
117
|
-
phase = Phase.LOST
|
|
118
|
-
failure = Failure.NONE
|
|
119
|
-
return true
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** Return to IDLE so the same instance can be reused for the next call. */
|
|
123
|
-
fun reset() {
|
|
124
|
-
phase = Phase.IDLE
|
|
125
|
-
failure = Failure.NONE
|
|
126
|
-
}
|
|
127
|
-
}
|