@mentra/bluetooth-sdk 3.4.0-dev.1207 → 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,274 +0,0 @@
|
|
|
1
|
-
package com.mentra.bluetoothsdk.sgcs
|
|
2
|
-
|
|
3
|
-
import android.content.BroadcastReceiver
|
|
4
|
-
import android.content.Context
|
|
5
|
-
import android.content.Intent
|
|
6
|
-
import android.content.IntentFilter
|
|
7
|
-
import android.os.Build
|
|
8
|
-
import android.os.Handler
|
|
9
|
-
import android.os.Looper
|
|
10
|
-
import android.os.Process
|
|
11
|
-
import android.os.SystemClock
|
|
12
|
-
import com.mentra.bluetoothsdk.BuildConfig
|
|
13
|
-
import com.mentra.bluetoothsdk.DeviceManager
|
|
14
|
-
import org.json.JSONObject
|
|
15
|
-
import java.io.File
|
|
16
|
-
import java.lang.ref.WeakReference
|
|
17
|
-
import java.util.UUID
|
|
18
|
-
|
|
19
|
-
/** Fixed, DUMP-protected debug commands. Expected address is comparison-only, never a connection target.
|
|
20
|
-
* No packet, device-command, caller path, reset, bond or radio inputs are accepted. */
|
|
21
|
-
internal object NimoResourceDiagnostics {
|
|
22
|
-
const val CLOCK_PROTOCOL = "nimo-monotonic-receipt-v1"
|
|
23
|
-
const val ACTION = "com.mentra.bluetoothsdk.NIMO_RESOURCE_LIFECYCLE"
|
|
24
|
-
private val main = Handler(Looper.getMainLooper())
|
|
25
|
-
private val processInstanceId = UUID.randomUUID().toString()
|
|
26
|
-
private data class Observed(val instance: WeakReference<Nimo>, var tombstone: JSONObject)
|
|
27
|
-
private val sessions = linkedMapOf<String, Observed>()
|
|
28
|
-
private var registered = false
|
|
29
|
-
private var application: Context? = null
|
|
30
|
-
private val requests = linkedSetOf<String>()
|
|
31
|
-
private data class Capture(val nonce: String, val name: String, val address: String,
|
|
32
|
-
var instance: Nimo? = null, var cancelled: Boolean = false, var connectStarted: Boolean = false,
|
|
33
|
-
var transportStarted: Boolean = false, val armedElapsedMs: Long,
|
|
34
|
-
var admittedElapsedMs: Long? = null)
|
|
35
|
-
// One diagnostic attempt per app process. Cancellation is a process-lifetime fence.
|
|
36
|
-
@Volatile private var capture: Capture? = null
|
|
37
|
-
internal fun preArmCaptureRequired(buildType: String = BuildConfig.BUILD_TYPE): Boolean = buildType == "nimoDiagnostic"
|
|
38
|
-
internal fun allowUnarmedWearable(buildType: String, wearable: String): Boolean =
|
|
39
|
-
!preArmCaptureRequired(buildType) || !wearable.contains("NIMO", ignoreCase = true)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
/** Debug entries share the main admission queue even before arm, so a call that
|
|
43
|
-
* starts just before arm cannot resume effects past it. Release builds are no-op.
|
|
44
|
-
* Queued calls re-enter current capture guards before any connection effect. */
|
|
45
|
-
fun dispatchCapturedEntry(entry: () -> Unit): Boolean {
|
|
46
|
-
if (!preArmCaptureRequired() && capture == null) return false
|
|
47
|
-
if (Looper.myLooper() == main.looper) return false
|
|
48
|
-
val task = java.util.concurrent.FutureTask<Unit> { entry() }
|
|
49
|
-
main.post(task)
|
|
50
|
-
try {
|
|
51
|
-
task.get()
|
|
52
|
-
} catch (error: java.util.concurrent.ExecutionException) {
|
|
53
|
-
throw (error.cause ?: error)
|
|
54
|
-
} catch (error: InterruptedException) {
|
|
55
|
-
// A queued entry must not run later after its original caller abandoned it.
|
|
56
|
-
task.cancel(false)
|
|
57
|
-
Thread.currentThread().interrupt()
|
|
58
|
-
throw error
|
|
59
|
-
}
|
|
60
|
-
return true
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
@Synchronized
|
|
64
|
-
fun allowManagerConnect(wearable: String, name: String? = null): Boolean {
|
|
65
|
-
val owned = capture ?: return allowUnarmedWearable(BuildConfig.BUILD_TYPE, wearable)
|
|
66
|
-
if (Looper.myLooper() != main.looper) return false
|
|
67
|
-
if (!wearable.contains("NIMO", ignoreCase = true)) return true
|
|
68
|
-
val current = DeviceManager.getInstance().sgc
|
|
69
|
-
return !owned.cancelled && (name == null || !owned.connectStarted && name == owned.name) &&
|
|
70
|
-
(current == null || current is Nimo && (owned.instance === current ||
|
|
71
|
-
owned.instance == null && current.resourceCaptureEligible()))
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Scan-to-connect is one captured attempt: never destroy or transfer its instance. */
|
|
75
|
-
@Synchronized
|
|
76
|
-
fun preserveCapturedScan(wearable: String, name: String): Boolean {
|
|
77
|
-
val owned = capture ?: return false
|
|
78
|
-
if (Looper.myLooper() != main.looper || !wearable.contains("NIMO", ignoreCase = true) ||
|
|
79
|
-
owned.cancelled || owned.connectStarted || name != owned.name) return false
|
|
80
|
-
val original = owned.instance ?: return false
|
|
81
|
-
return DeviceManager.getInstance().sgc === original &&
|
|
82
|
-
original.resourceSnapshot().getLong("sessionGeneration") == 0L
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
@Synchronized
|
|
86
|
-
fun admit(instance: Nimo, name: String?, connect: Boolean): Boolean {
|
|
87
|
-
val owned = capture ?: return !preArmCaptureRequired()
|
|
88
|
-
if (Looper.myLooper() != main.looper) return false
|
|
89
|
-
if (!allowManagerConnect("NIMO", name) || DeviceManager.getInstance().sgc !== instance ||
|
|
90
|
-
owned.instance != null && owned.instance !== instance || owned.connectStarted) return false
|
|
91
|
-
if (owned.instance == null) {
|
|
92
|
-
if (!instance.resourceCaptureEligible()) return false
|
|
93
|
-
val candidateAdmittedElapsedMs = SystemClock.elapsedRealtime()
|
|
94
|
-
owned.instance = instance
|
|
95
|
-
// Immutable admission survives a setup timeout before any GATT session exists.
|
|
96
|
-
val app = application ?: run { owned.cancelled = true; return false }
|
|
97
|
-
val root = File(app.filesDir, "nimo-lifecycle")
|
|
98
|
-
val target = File(root, "${owned.nonce}.admission.json")
|
|
99
|
-
if (target.exists()) { owned.cancelled = true; return false }
|
|
100
|
-
val pending = File(root, "${owned.nonce}.admission.tmp")
|
|
101
|
-
try {
|
|
102
|
-
pending.outputStream().use { it.write(captureSnapshot(owned, candidateAdmittedElapsedMs).toString().toByteArray(Charsets.UTF_8)); it.flush() }
|
|
103
|
-
check(pending.renameTo(target))
|
|
104
|
-
// Durable admission is authoritative; no live clock may claim it
|
|
105
|
-
// before the original immutable receipt is safely committed.
|
|
106
|
-
instance.resourceCaptureAdmission(owned.nonce, owned.armedElapsedMs, candidateAdmittedElapsedMs)
|
|
107
|
-
owned.admittedElapsedMs = candidateAdmittedElapsedMs
|
|
108
|
-
} catch (_: Exception) {
|
|
109
|
-
owned.cancelled = true
|
|
110
|
-
return false
|
|
111
|
-
} finally { pending.delete() }
|
|
112
|
-
}
|
|
113
|
-
if (connect) owned.connectStarted = true
|
|
114
|
-
return true
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
@Synchronized
|
|
118
|
-
fun allowPeer(instance: Nimo, address: String, bonded: Boolean): Boolean {
|
|
119
|
-
val owned = capture ?: return !preArmCaptureRequired()
|
|
120
|
-
if (Looper.myLooper() != main.looper) return false
|
|
121
|
-
return !owned.cancelled && owned.instance === instance && address == owned.address && bonded &&
|
|
122
|
-
DeviceManager.getInstance().sgc === instance
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
@Synchronized
|
|
126
|
-
fun beginTransport(instance: Nimo, address: String, bonded: Boolean): Boolean {
|
|
127
|
-
val owned = capture ?: return !preArmCaptureRequired()
|
|
128
|
-
if (Looper.myLooper() != main.looper) return false
|
|
129
|
-
if (!allowPeer(instance, address, bonded) || owned.transportStarted) return false
|
|
130
|
-
owned.transportStarted = true
|
|
131
|
-
return true
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
@Synchronized
|
|
135
|
-
private fun captureSnapshot(owned: Capture, admittedElapsedMs: Long? = owned.admittedElapsedMs): JSONObject = JSONObject()
|
|
136
|
-
.put("clockProtocol", CLOCK_PROTOCOL)
|
|
137
|
-
.put("clock", JSONObject().put("kind", "android-elapsed-realtime-v1").put("armedElapsedMs", owned.armedElapsedMs)
|
|
138
|
-
.put("admittedElapsedMs", admittedElapsedMs ?: JSONObject.NULL).put("observedElapsedMs", SystemClock.elapsedRealtime()))
|
|
139
|
-
.put("version", "nimo-preconnect-capture-v1").put("nonce", owned.nonce)
|
|
140
|
-
.put("captureRequiredBeforeArm", preArmCaptureRequired())
|
|
141
|
-
.put("expectedPair", JSONObject().put("name", owned.name).put("bluetoothAddress", owned.address))
|
|
142
|
-
.put("instanceId", owned.instance?.resourceInstanceId ?: JSONObject.NULL)
|
|
143
|
-
.put("sessionGeneration", owned.instance?.resourceSnapshot()?.getLong("sessionGeneration") ?: JSONObject.NULL)
|
|
144
|
-
.put("observedPair", if (owned.transportStarted) owned.instance?.resourceSnapshot()?.getJSONObject("pair") ?: JSONObject.NULL else JSONObject.NULL)
|
|
145
|
-
.put("cancelled", owned.cancelled).put("connectStarted", owned.connectStarted)
|
|
146
|
-
.put("transportStarted", owned.transportStarted)
|
|
147
|
-
.put("appPid", Process.myPid()).put("processInstanceId", processInstanceId)
|
|
148
|
-
.put("observedAtEpochMs", System.currentTimeMillis())
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
fun start(context: Context) {
|
|
152
|
-
if (Looper.myLooper() != main.looper) { main.post { start(context) }; return }
|
|
153
|
-
if (registered) return
|
|
154
|
-
val app = context.applicationContext
|
|
155
|
-
val filter = IntentFilter(ACTION)
|
|
156
|
-
if (Build.VERSION.SDK_INT >= 33) app.registerReceiver(receiver, filter, "android.permission.DUMP", main, Context.RECEIVER_EXPORTED)
|
|
157
|
-
else { @Suppress("DEPRECATION") app.registerReceiver(receiver, filter, "android.permission.DUMP", main) }
|
|
158
|
-
application = app
|
|
159
|
-
registered = true
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
fun track(instance: Nimo) {
|
|
163
|
-
check(Looper.myLooper() == main.looper)
|
|
164
|
-
val snapshot = instance.resourceSnapshot()
|
|
165
|
-
sessions["${instance.resourceInstanceId}:${snapshot.getLong("sessionGeneration")}"] = Observed(WeakReference(instance), snapshot)
|
|
166
|
-
while (sessions.size > 8) sessions.remove(sessions.keys.first())
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
private val receiver = object : BroadcastReceiver() {
|
|
170
|
-
override fun onReceive(context: Context?, intent: Intent?) {
|
|
171
|
-
if (intent?.action != ACTION) return
|
|
172
|
-
try { execute(intent) } catch (_: Exception) { /* No receipt means unknown; never crash the product app. */ }
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/** Separate from BroadcastReceiver for source tests; no exported unrestricted app API. */
|
|
177
|
-
@Synchronized
|
|
178
|
-
internal fun execute(intent: Intent) {
|
|
179
|
-
check(Looper.myLooper() == main.looper)
|
|
180
|
-
val requestReceivedElapsedMs = SystemClock.elapsedRealtime()
|
|
181
|
-
val nonce = intent.getStringExtra("nonce") ?: return
|
|
182
|
-
if (!uuid(nonce) || nonce in requests) return
|
|
183
|
-
val app = application ?: return
|
|
184
|
-
val root = File(app.filesDir, "nimo-lifecycle")
|
|
185
|
-
if ((!root.isDirectory && !root.mkdir()) || File(root, "$nonce.json").exists()) return
|
|
186
|
-
requests.add(nonce)
|
|
187
|
-
while (requests.size > 128) requests.remove(requests.first())
|
|
188
|
-
val mode = intent.getStringExtra("mode") ?: "status"
|
|
189
|
-
val address = intent.getStringExtra("expectedAddress") ?: ""
|
|
190
|
-
val instanceId = intent.getStringExtra("instanceId")
|
|
191
|
-
val generation = intent.getLongExtra("sessionGeneration", -1)
|
|
192
|
-
val request = JSONObject().put("nonce", nonce).put("mode", mode).put("expectedAddress", address)
|
|
193
|
-
.put("instanceId", instanceId ?: JSONObject.NULL).put("sessionGeneration", if (generation >= 0) generation else JSONObject.NULL)
|
|
194
|
-
fun save(session: JSONObject?, error: String? = null): Boolean {
|
|
195
|
-
val current = DeviceManager.getInstance().sgc
|
|
196
|
-
val receipt = JSONObject().put("clockProtocol", CLOCK_PROTOCOL)
|
|
197
|
-
.put("clock", JSONObject().put("kind", "android-elapsed-realtime-v1").put("requestReceivedElapsedMs", requestReceivedElapsedMs).put("observedElapsedMs", SystemClock.elapsedRealtime()))
|
|
198
|
-
.put("schemaVersion", 1).put("request", request).put("observedAtEpochMs", System.currentTimeMillis())
|
|
199
|
-
.put("appPid", Process.myPid()).put("processInstanceId", processInstanceId)
|
|
200
|
-
.put("currentManager", JSONObject().put("type", current?.type ?: JSONObject.NULL)
|
|
201
|
-
.put("instanceId", (current as? Nimo)?.resourceInstanceId ?: JSONObject.NULL))
|
|
202
|
-
.put("session", session ?: JSONObject.NULL).put("error", error ?: JSONObject.NULL)
|
|
203
|
-
val pending = File(root, "$nonce.tmp")
|
|
204
|
-
try {
|
|
205
|
-
pending.outputStream().use { it.write(receipt.toString().toByteArray(Charsets.UTF_8)); it.flush() }
|
|
206
|
-
check(pending.renameTo(File(root, "$nonce.json")))
|
|
207
|
-
return true
|
|
208
|
-
} catch (_: Exception) { return false /* Absent receipt is unknown. */ } finally { pending.delete() }
|
|
209
|
-
}
|
|
210
|
-
if (mode in listOf("arm-capture", "capture-status", "cancel-capture")) {
|
|
211
|
-
val expectedName = intent.getStringExtra("expectedName")
|
|
212
|
-
val captureNonce = intent.getStringExtra("captureNonce")
|
|
213
|
-
request.remove("instanceId"); request.remove("sessionGeneration")
|
|
214
|
-
request.put("expectedName", expectedName ?: JSONObject.NULL).put("captureNonce", captureNonce ?: JSONObject.NULL)
|
|
215
|
-
if (!Regex("^[0-9A-F]{2}(:[0-9A-F]{2}){5}$").matches(address) ||
|
|
216
|
-
intent.extras?.keySet()?.any { it !in setOf("nonce", "mode", "expectedAddress", "expectedName", "captureNonce") } == true ||
|
|
217
|
-
expectedName == null || !Regex("^Nimo-[A-Za-z0-9]{1,32}$").matches(expectedName) ||
|
|
218
|
-
captureNonce == null || !uuid(captureNonce)) { save(null, "invalid-fixed-capture-request"); return }
|
|
219
|
-
if (mode == "arm-capture") {
|
|
220
|
-
val manager = DeviceManager.getInstance().sgc
|
|
221
|
-
if (capture != null || captureNonce != nonce || manager != null) {
|
|
222
|
-
save(null, "capture-arm-refused-existing-owner"); return
|
|
223
|
-
}
|
|
224
|
-
capture = Capture(captureNonce, expectedName, address, armedElapsedMs = requestReceivedElapsedMs)
|
|
225
|
-
}
|
|
226
|
-
val owned = capture
|
|
227
|
-
if (owned == null || owned.nonce != captureNonce || owned.name != expectedName || owned.address != address) {
|
|
228
|
-
save(null, "capture-not-original"); return
|
|
229
|
-
}
|
|
230
|
-
fun captureReceipt(session: JSONObject? = null, error: String? = null): Boolean {
|
|
231
|
-
return save(JSONObject().put("capture", captureSnapshot(owned)).put("originalSession", session ?: JSONObject.NULL), error)
|
|
232
|
-
}
|
|
233
|
-
if (mode != "cancel-capture") {
|
|
234
|
-
if (!captureReceipt(owned.instance?.resourceSnapshot()) && mode == "arm-capture") owned.cancelled = true
|
|
235
|
-
return
|
|
236
|
-
}
|
|
237
|
-
owned.cancelled = true // fence before draining or emitting a no-admission receipt
|
|
238
|
-
val original = owned.instance
|
|
239
|
-
if (original == null) { captureReceipt(); return }
|
|
240
|
-
if (DeviceManager.getInstance().sgc !== original) { captureReceipt(original.resourceSnapshot(), "capture-cancel-refused-replacement"); return }
|
|
241
|
-
original.releaseCapturedResource({ DeviceManager.getInstance().sgc === original }, { result -> captureReceipt(result) }, nonce, requestReceivedElapsedMs)
|
|
242
|
-
return
|
|
243
|
-
}
|
|
244
|
-
if (mode !in listOf("status", "release") || !Regex("^[0-9A-F]{2}(:[0-9A-F]{2}){5}$").matches(address) ||
|
|
245
|
-
instanceId != null && !uuid(instanceId) || intent.extras?.keySet()?.any { it !in setOf("nonce", "mode", "expectedAddress", "instanceId", "sessionGeneration") } == true) {
|
|
246
|
-
save(null, "invalid-fixed-request"); return
|
|
247
|
-
}
|
|
248
|
-
val current = DeviceManager.getInstance().sgc as? Nimo
|
|
249
|
-
current?.let { track(it) }
|
|
250
|
-
val selected = if (instanceId != null) sessions["$instanceId:$generation"] else sessions.values.lastOrNull {
|
|
251
|
-
it.tombstone.getJSONObject("pair").optString("bluetoothAddress") == address
|
|
252
|
-
}
|
|
253
|
-
val candidate = selected?.instance?.get()
|
|
254
|
-
val live = candidate?.resourceSnapshot()
|
|
255
|
-
val instance = candidate?.takeIf { live?.optLong("sessionGeneration") == selected?.tombstone?.optLong("sessionGeneration") }
|
|
256
|
-
val snapshot = if (instance != null) live else selected?.tombstone
|
|
257
|
-
if (snapshot == null || snapshot.getJSONObject("pair").optString("bluetoothAddress") != address ||
|
|
258
|
-
generation >= 0 && snapshot.optLong("sessionGeneration", -1) != generation) {
|
|
259
|
-
save(null, "target-not-observed-or-generation-mismatch"); return
|
|
260
|
-
}
|
|
261
|
-
if (mode == "status") {
|
|
262
|
-
save(JSONObject(snapshot.toString()).put("sessionObservationCurrent", instance != null).put("source", if (instance != null) "live-original-instance" else "retained-tombstone")); return
|
|
263
|
-
}
|
|
264
|
-
if (instanceId == null || generation < 1 || instance == null || instance !== current) {
|
|
265
|
-
save(snapshot, "release-refused-replaced-or-unowned-target"); return
|
|
266
|
-
}
|
|
267
|
-
instance.releaseResource(generation, { DeviceManager.getInstance().sgc === instance }, { result ->
|
|
268
|
-
track(instance)
|
|
269
|
-
if (result.has("error")) save(instance.resourceSnapshot(), result.getString("error")) else save(result)
|
|
270
|
-
}, nonce, requestReceivedElapsedMs)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
private fun uuid(value: String): Boolean = try { UUID.fromString(value).toString() == value } catch (_: IllegalArgumentException) { false }
|
|
274
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
package com.mentra.bluetoothsdk.sgcs
|
|
2
|
-
|
|
3
|
-
import android.content.Context
|
|
4
|
-
import android.os.Handler
|
|
5
|
-
|
|
6
|
-
/** No diagnostic receiver, commands, or screen capture in release builds. */
|
|
7
|
-
@Suppress("UNUSED_PARAMETER")
|
|
8
|
-
internal class NimoDiagnostics(
|
|
9
|
-
context: Context,
|
|
10
|
-
handler: Handler,
|
|
11
|
-
send: (ByteArray, () -> Unit) -> Boolean,
|
|
12
|
-
requestCanvasHold: ((() -> Unit) -> Unit) = { _ -> },
|
|
13
|
-
releaseCanvasHold: (resume: Boolean) -> Unit = { _ -> },
|
|
14
|
-
) {
|
|
15
|
-
fun connected(maxWriteBytes: Int) {}
|
|
16
|
-
fun disconnected() {}
|
|
17
|
-
fun close() {}
|
|
18
|
-
fun onPacket(packet: ByteArray): Boolean = false
|
|
19
|
-
fun cancelHeldCapture(reason: String, resumeCanvas: Boolean = false) {}
|
|
20
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
package com.mentra.bluetoothsdk.sgcs
|
|
2
|
-
|
|
3
|
-
import android.content.Context
|
|
4
|
-
|
|
5
|
-
/** Release builds register no receiver and expose no lifecycle control surface. */
|
|
6
|
-
@Suppress("UNUSED_PARAMETER")
|
|
7
|
-
internal object NimoResourceDiagnostics {
|
|
8
|
-
fun start(context: Context) {}
|
|
9
|
-
fun track(instance: Nimo) {}
|
|
10
|
-
fun dispatchCapturedEntry(entry: () -> Unit) = false
|
|
11
|
-
fun allowManagerConnect(wearable: String, name: String? = null) = true
|
|
12
|
-
fun preserveCapturedScan(wearable: String, name: String) = false
|
|
13
|
-
fun admit(instance: Nimo, name: String?, connect: Boolean) = true
|
|
14
|
-
fun allowPeer(instance: Nimo, address: String, bonded: Boolean) = true
|
|
15
|
-
fun beginTransport(instance: Nimo, address: String, bonded: Boolean) = true
|
|
16
|
-
}
|
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
package com.mentra.bluetoothsdk.sgcs
|
|
2
|
-
|
|
3
|
-
import android.content.Context
|
|
4
|
-
import android.os.Handler
|
|
5
|
-
import android.os.Looper
|
|
6
|
-
import androidx.test.core.app.ApplicationProvider
|
|
7
|
-
import java.io.File
|
|
8
|
-
import java.util.concurrent.TimeUnit
|
|
9
|
-
import org.json.JSONObject
|
|
10
|
-
import org.junit.After
|
|
11
|
-
import org.junit.Assert.*
|
|
12
|
-
import org.junit.Before
|
|
13
|
-
import org.junit.Test
|
|
14
|
-
import org.junit.runner.RunWith
|
|
15
|
-
import org.robolectric.RobolectricTestRunner
|
|
16
|
-
import org.robolectric.Shadows
|
|
17
|
-
import org.robolectric.annotation.Config
|
|
18
|
-
import org.robolectric.annotation.LooperMode
|
|
19
|
-
|
|
20
|
-
@RunWith(RobolectricTestRunner::class)
|
|
21
|
-
@Config(sdk = [33])
|
|
22
|
-
@LooperMode(LooperMode.Mode.PAUSED)
|
|
23
|
-
class NimoDiagnosticsTest {
|
|
24
|
-
private data class Write(val packet: ByteArray, val completed: () -> Unit)
|
|
25
|
-
private val writes = mutableListOf<Write>()
|
|
26
|
-
private var heldReady: (() -> Unit)? = null
|
|
27
|
-
private val holdReleases = mutableListOf<Boolean>()
|
|
28
|
-
private lateinit var diagnostics: NimoDiagnostics
|
|
29
|
-
private lateinit var root: File
|
|
30
|
-
private lateinit var previousDirectories: Set<String>
|
|
31
|
-
|
|
32
|
-
@Before fun setup() {
|
|
33
|
-
val context = ApplicationProvider.getApplicationContext<Context>()
|
|
34
|
-
root = File(context.filesDir, "nimo-framebuffer")
|
|
35
|
-
previousDirectories = root.listFiles()?.map { it.name }?.toSet() ?: emptySet()
|
|
36
|
-
diagnostics = NimoDiagnostics(
|
|
37
|
-
context,
|
|
38
|
-
Handler(Looper.getMainLooper()),
|
|
39
|
-
{ frame, complete -> writes.add(Write(frame.copyOf(), complete)); true },
|
|
40
|
-
{ ready -> heldReady = ready },
|
|
41
|
-
{ resume -> holdReleases += resume },
|
|
42
|
-
)
|
|
43
|
-
diagnostics.connected(512)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
@After fun dispose() { diagnostics.close() }
|
|
47
|
-
|
|
48
|
-
@Test fun probePreservesNotReadyAndAcceptsResponsesBeforeWriteCallbacks() {
|
|
49
|
-
start("probe")
|
|
50
|
-
driveToPageGate()
|
|
51
|
-
assertTrue(diagnostics.onPacket(fbpReply(writes[3].packet, ByteArray(240))))
|
|
52
|
-
val result = receipt()
|
|
53
|
-
assertTrue(result.getBoolean("probe_ok"))
|
|
54
|
-
assertFalse(result.getBoolean("ok"))
|
|
55
|
-
assertTrue(result.isNull("error"))
|
|
56
|
-
assertEquals(0, result.getJSONObject("readiness").getInt("ready"))
|
|
57
|
-
assertEquals(1, result.getJSONObject("readiness").getInt("tws"))
|
|
58
|
-
assertEquals(NimoFramebufferProtocol.EXPECTED_VERSION, result.getString("runtime_version"))
|
|
59
|
-
assertEquals(8, result.getInt("wire_events"))
|
|
60
|
-
assertEquals(8, File(captureDirectory(), "wire.jsonl").readLines().size)
|
|
61
|
-
writes.toList().forEach { it.completed() }
|
|
62
|
-
idle(4_000)
|
|
63
|
-
assertEquals(4, writes.size)
|
|
64
|
-
assertTrue(receipt().getBoolean("probe_ok"))
|
|
65
|
-
assertTrue(writes.all { it.packet[8] == 2.toByte() && (it.packet[9].toInt() and 255) in listOf(0x0B, 0x19, 0x15) })
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
@Test fun duplicateTriggerDoesNotStartAnotherRead() {
|
|
69
|
-
start("capture")
|
|
70
|
-
start("probe")
|
|
71
|
-
assertEquals(1, writes.size)
|
|
72
|
-
assertEquals(1, newDirectories().size)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
@Test fun disconnectCancelsPendingTimeoutAndIgnoresLateWriteCompletion() {
|
|
76
|
-
start("capture")
|
|
77
|
-
writes.single().completed()
|
|
78
|
-
diagnostics.disconnected()
|
|
79
|
-
val error = receipt().getString("error")
|
|
80
|
-
assertTrue(error.contains("disconnected"))
|
|
81
|
-
diagnostics.connected(512)
|
|
82
|
-
writes.single().completed()
|
|
83
|
-
idle(4_000)
|
|
84
|
-
assertEquals(error, receipt().getString("error"))
|
|
85
|
-
assertEquals(1, writes.size)
|
|
86
|
-
assertFalse(diagnostics.onPacket(versionReply()))
|
|
87
|
-
assertFalse(receipt().getBoolean("ok"))
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
@Test fun timeoutStartsAtWriteCompletionAndDoesNotRetry() {
|
|
91
|
-
start("probe")
|
|
92
|
-
idle(5_000)
|
|
93
|
-
assertFalse(File(captureDirectory(), "session.json").exists())
|
|
94
|
-
writes.single().completed()
|
|
95
|
-
idle(2_900)
|
|
96
|
-
assertFalse(File(captureDirectory(), "session.json").exists())
|
|
97
|
-
idle(200)
|
|
98
|
-
assertTrue(receipt().getString("error").contains("response timeout"))
|
|
99
|
-
assertEquals(1, writes.size)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
@Test fun captureRejectsCrcBeforeRequestingBulkPages() {
|
|
103
|
-
start("capture")
|
|
104
|
-
driveToPageGate()
|
|
105
|
-
val corrupted = fbpReply(writes[3].packet, ByteArray(240)).also { it[4] = (it[4].toInt() xor 1).toByte() }
|
|
106
|
-
assertTrue(diagnostics.onPacket(corrupted))
|
|
107
|
-
assertFalse(receipt().getBoolean("ok"))
|
|
108
|
-
assertFalse(receipt().getBoolean("probe_ok"))
|
|
109
|
-
assertTrue(receipt().getString("error").contains("CRC mismatch"))
|
|
110
|
-
assertEquals(4, writes.size)
|
|
111
|
-
assertEquals(0, receipt().getJSONArray("banks").length())
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
@Test fun totalDeadlineBoundsAWriteThatNeverCompletes() {
|
|
115
|
-
start("capture")
|
|
116
|
-
idle(600_001)
|
|
117
|
-
assertTrue(receipt().getString("error").contains("10-minute budget"))
|
|
118
|
-
writes.single().completed()
|
|
119
|
-
idle(4_000)
|
|
120
|
-
assertEquals(1, writes.size)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
@Test fun heldSampleBudgetIncludesCanvasDrainAndStaleReadyCannotStartCapture() {
|
|
124
|
-
start("held-sample")
|
|
125
|
-
assertTrue(writes.isEmpty())
|
|
126
|
-
assertNotNull(heldReady)
|
|
127
|
-
|
|
128
|
-
idle(15_001)
|
|
129
|
-
|
|
130
|
-
assertEquals(listOf(true), holdReleases)
|
|
131
|
-
assertTrue(receipt().getString("error").contains("15-second budget"))
|
|
132
|
-
heldReady?.invoke()
|
|
133
|
-
assertTrue(writes.isEmpty())
|
|
134
|
-
assertEquals(listOf(true), holdReleases)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
@Test fun disconnectBeforeHeldCaptureIsReadyReleasesWithoutResumingCanvas() {
|
|
138
|
-
start("held-capture")
|
|
139
|
-
|
|
140
|
-
diagnostics.disconnected()
|
|
141
|
-
|
|
142
|
-
assertEquals(listOf(false), holdReleases)
|
|
143
|
-
assertTrue(receipt().getString("error").contains("disconnected"))
|
|
144
|
-
heldReady?.invoke()
|
|
145
|
-
assertTrue(writes.isEmpty())
|
|
146
|
-
assertEquals(listOf(false), holdReleases)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
@Test fun duplicateHeldCaptureDoesNotReplaceHoldOrDeadline() {
|
|
150
|
-
start("held-sample")
|
|
151
|
-
val firstReady = heldReady
|
|
152
|
-
|
|
153
|
-
start("held-capture")
|
|
154
|
-
|
|
155
|
-
assertSame(firstReady, heldReady)
|
|
156
|
-
assertEquals(1, newDirectories().size)
|
|
157
|
-
idle(15_001)
|
|
158
|
-
assertEquals(listOf(true), holdReleases)
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
@Test fun insufficientCapacityFailsWithoutSendingDiagnosticCommands() {
|
|
162
|
-
diagnostics.connected(20)
|
|
163
|
-
start("capture")
|
|
164
|
-
assertEquals(0, writes.size)
|
|
165
|
-
assertTrue(receipt().getString("error").contains("Write capacity"))
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
@Test fun samplePinsPreparedBankAndNeverClaimsStaticPairVerification() {
|
|
169
|
-
assertSample(1)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
@Test fun sampleAlsoPinsPreparedBankZero() {
|
|
173
|
-
assertSample(0)
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
@Test fun heldSampleStartsOnlyAfterDrainAndReleasesOnSuccess() {
|
|
177
|
-
assertSample(1, "held-sample")
|
|
178
|
-
assertEquals(listOf(true), holdReleases)
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private fun assertSample(bank: Int, mode: String = "sample") {
|
|
182
|
-
val state = byteArrayOf((1 - bank).toByte(), bank.toByte(), 0, 0)
|
|
183
|
-
start(mode)
|
|
184
|
-
if (mode == "held-sample") {
|
|
185
|
-
assertTrue(writes.isEmpty())
|
|
186
|
-
requireNotNull(heldReady).invoke()
|
|
187
|
-
}
|
|
188
|
-
driveToPageGate()
|
|
189
|
-
diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240), state))
|
|
190
|
-
// The gate always probes bank0/page0; the sample follows its prepared selector.
|
|
191
|
-
repeat(315) { page ->
|
|
192
|
-
val request = writes.last().packet
|
|
193
|
-
assertEquals(bank, request[21].toInt())
|
|
194
|
-
assertEquals(page, (request[22].toInt() and 255) or ((request[23].toInt() and 255) shl 8))
|
|
195
|
-
diagnostics.onPacket(fbpReply(request, ByteArray(240) { page.toByte() }, state))
|
|
196
|
-
}
|
|
197
|
-
val result = receipt()
|
|
198
|
-
assertTrue(result.getBoolean("sample_ok"))
|
|
199
|
-
assertFalse(result.getBoolean("ok"))
|
|
200
|
-
assertFalse(result.getBoolean("atomic"))
|
|
201
|
-
assertFalse(result.getBoolean("optical"))
|
|
202
|
-
assertEquals("non_atomic_single_pass", result.getString("evidence_kind"))
|
|
203
|
-
assertEquals(0, result.getInt("static_pairs_verified"))
|
|
204
|
-
assertEquals(1, result.getJSONArray("banks").length())
|
|
205
|
-
assertEquals(319, writes.size)
|
|
206
|
-
assertEquals(638, result.getInt("wire_events"))
|
|
207
|
-
assertFalse(result.getBoolean("pixel_stability_verified"))
|
|
208
|
-
assertEquals(mode, result.getString("mode"))
|
|
209
|
-
assertEquals(75_600, File(captureDirectory(), "bank$bank-pass1.bin").length().toInt())
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
@Test fun sampleRejectsSelectorChangeBetweenGateAndFirstPageWithoutRetrying() {
|
|
213
|
-
start("sample")
|
|
214
|
-
driveToPageGate()
|
|
215
|
-
diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240)))
|
|
216
|
-
diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240), byteArrayOf(1, 0, 0, 0)))
|
|
217
|
-
val result = receipt()
|
|
218
|
-
assertFalse(result.getBoolean("sample_ok"))
|
|
219
|
-
assertFalse(result.getBoolean("ok"))
|
|
220
|
-
assertTrue(result.getString("error").contains("Sample metadata differs from page gate"))
|
|
221
|
-
assertEquals(5, writes.size)
|
|
222
|
-
assertEquals(0, result.getJSONArray("banks").length())
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
@Test fun sampleTimeoutCannotProduceSuccessOrStartASecondPass() {
|
|
226
|
-
start("sample")
|
|
227
|
-
driveToPageGate()
|
|
228
|
-
diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240)))
|
|
229
|
-
writes.last().completed()
|
|
230
|
-
idle(3_001)
|
|
231
|
-
assertFalse(receipt().getBoolean("sample_ok"))
|
|
232
|
-
assertFalse(receipt().getBoolean("ok"))
|
|
233
|
-
assertEquals(5, writes.size)
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
@Test fun fullCaptureStillRequiresFourCompleteReadsAndTwoStaticPairs() {
|
|
237
|
-
start("capture")
|
|
238
|
-
driveToPageGate()
|
|
239
|
-
diagnostics.onPacket(fbpReply(writes.last().packet, ByteArray(240)))
|
|
240
|
-
repeat(4) { pass ->
|
|
241
|
-
repeat(315) { page ->
|
|
242
|
-
val request = writes.last().packet
|
|
243
|
-
assertEquals(pass / 2, request[21].toInt())
|
|
244
|
-
diagnostics.onPacket(fbpReply(request, ByteArray(240) { page.toByte() }))
|
|
245
|
-
}
|
|
246
|
-
if (pass < 3) assertFalse(File(captureDirectory(), "session.json").exists())
|
|
247
|
-
}
|
|
248
|
-
assertTrue(receipt().getBoolean("ok"))
|
|
249
|
-
assertEquals(2, receipt().getInt("static_pairs_verified"))
|
|
250
|
-
assertEquals(4, receipt().getJSONArray("banks").length())
|
|
251
|
-
assertEquals(1264, writes.size)
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
private fun driveToPageGate() {
|
|
255
|
-
assertEquals(1, writes.size)
|
|
256
|
-
assertFalse(diagnostics.onPacket(versionReply()))
|
|
257
|
-
assertEquals(2, writes.size)
|
|
258
|
-
assertFalse(diagnostics.onPacket(frame(0x19, byteArrayOf(0, 0, 1, 0))))
|
|
259
|
-
assertEquals(3, writes.size)
|
|
260
|
-
assertTrue(diagnostics.onPacket(fbpReply(writes[2].packet, "537cf1p1".toByteArray())))
|
|
261
|
-
assertEquals(4, writes.size)
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Exercise the production coordinator without pretending Robolectric proves Android's
|
|
265
|
-
// signature-permission enforcement for the ADB receiver. That boundary needs a device test.
|
|
266
|
-
private fun start(mode: String) {
|
|
267
|
-
NimoDiagnostics::class.java.getDeclaredMethod("start", String::class.java).apply { isAccessible = true }
|
|
268
|
-
.invoke(diagnostics, mode)
|
|
269
|
-
}
|
|
270
|
-
private fun idle(milliseconds: Long) = Shadows.shadowOf(Looper.getMainLooper()).idleFor(milliseconds, TimeUnit.MILLISECONDS)
|
|
271
|
-
private fun newDirectories() = root.listFiles()?.filter { it.name !in previousDirectories } ?: emptyList()
|
|
272
|
-
private fun captureDirectory() = newDirectories().single()
|
|
273
|
-
private fun receipt() = JSONObject(File(captureDirectory(), "session.json").readText())
|
|
274
|
-
private fun versionReply() = frame(0x0B, byteArrayOf(0) + NimoFramebufferProtocol.EXPECTED_VERSION.toByteArray())
|
|
275
|
-
|
|
276
|
-
private fun fbpReply(request: ByteArray, data: ByteArray, state: ByteArray = byteArrayOf(0, 1, 0, 0)): ByteArray {
|
|
277
|
-
val payload = byteArrayOf(0) + "FBP1".toByteArray() + request.copyOfRange(16, 24) + state + state +
|
|
278
|
-
byteArrayOf(data.size.toByte(), (data.size shr 8).toByte()) + data
|
|
279
|
-
return frame(0x15, payload)
|
|
280
|
-
}
|
|
281
|
-
private fun frame(key: Int, payload: ByteArray): ByteArray {
|
|
282
|
-
val body = byteArrayOf(2, key.toByte(), payload.size.toByte(), (payload.size shr 8).toByte()) + payload
|
|
283
|
-
val crc = NimoCanvasCodec.crc16(body)
|
|
284
|
-
return byteArrayOf(0xBF.toByte(), 0, body.size.toByte(), (body.size shr 8).toByte(),
|
|
285
|
-
crc.toByte(), (crc shr 8).toByte(), 0, 0) + body
|
|
286
|
-
}
|
|
287
|
-
}
|