@mentra/bluetooth-sdk 3.5.0-dev.1260 → 3.5.0-dev.1261
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/sgcs/MentraLive.kt +318 -18
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleUploadService.java +13 -6
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +291 -14
- package/build/generated/releaseMetadata.js +5 -5
- package/build/generated/releaseMetadata.js.map +1 -1
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
- package/ios/Source/sgcs/MentraLive.swift +310 -28
- package/ios/Tests/IncidentLogDispatchTests.swift +257 -5
- package/package.json +2 -2
- package/src/generated/releaseMetadata.ts +5 -5
|
@@ -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.1261"
|
|
7
|
+
const val RELEASE_SET_ID: String = "mentra-3.5.0-dev.1261"
|
|
8
|
+
const val SOURCE_COMMIT: String = "c8f6349da74f3a0ae62c979a59aafbf62c644f7c"
|
|
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.1261.json"
|
|
10
|
+
const val OTA_MANIFEST_SHA256: String = "fc13b964185dce4340c5e6f2481c3894c7b53ee1d57d6119dbcaad64e8ff0496"
|
|
11
11
|
}
|
|
@@ -160,6 +160,18 @@ class MentraLive : SGCManager() {
|
|
|
160
160
|
// Longer than the glasses' bounded relay of one report (BES logs, two file transfers,
|
|
161
161
|
// the phone POST), so an unreleased name cannot be reused while its report is in flight.
|
|
162
162
|
internal const val INCIDENT_RELAY_OWNERSHIP_MS = 10 * 60_000L
|
|
163
|
+
// The BES file arrives first and is older than the MTK ring. Hold it this long so the
|
|
164
|
+
// MTK log can be uploaded before it; if the MTK file never arrives, upload it anyway.
|
|
165
|
+
private const val BES_HOLD_MS = 30_000L
|
|
166
|
+
// A link lost mid-transfer leaves the MTK file incomplete with no terminal event, so stop
|
|
167
|
+
// waiting after the glasses' own 120 s limit on that transfer.
|
|
168
|
+
private const val BES_MTK_WAIT_MAX_MS = 150_000L
|
|
169
|
+
// The glasses delete a BES incident once they receive transfer_complete, so the phone
|
|
170
|
+
// owns the only copy afterward. Keep that copy on disk and retry the Cloud POST on this
|
|
171
|
+
// cadence until it is confirmed, surviving a connectivity outage or process death.
|
|
172
|
+
private const val BES_RETRY_MS = 60_000L
|
|
173
|
+
// Durable store for held BES logs whose Cloud upload has not yet been confirmed.
|
|
174
|
+
private const val BES_HELD_DIR = "MentraLive_HeldBes"
|
|
163
175
|
private const val GATT_DISCONNECT_TIMEOUT_MS = 2_000L
|
|
164
176
|
private const val MTU_CALLBACK_TIMEOUT_MS = 3_000L
|
|
165
177
|
private val gattTeardownBarrier = MentraLiveGattTeardownBarrier()
|
|
@@ -801,6 +813,9 @@ class MentraLive : SGCManager() {
|
|
|
801
813
|
|
|
802
814
|
/** Expected incident log relay files from glasses (B… firmware, L… logcat). */
|
|
803
815
|
private val bleIncidentLogRelays = ConcurrentHashMap<String, BleIncidentLogRelay>()
|
|
816
|
+
private val heldBesLogs = ConcurrentHashMap<String, HeldBesLog>()
|
|
817
|
+
// Files with a resume attempt or retry pending, so a reconnect doesn't start a second one.
|
|
818
|
+
private val resumingBesFiles = ConcurrentHashMap.newKeySet<String>()
|
|
804
819
|
|
|
805
820
|
// File packet reassembly buffer for handling fragmented BLE notifications
|
|
806
821
|
// Android BLE stack delivers notifications in MTU-sized chunks (253 bytes with default MTU)
|
|
@@ -849,12 +864,24 @@ class MentraLive : SGCManager() {
|
|
|
849
864
|
val fileBaseKey: String,
|
|
850
865
|
val incidentId: String,
|
|
851
866
|
val apiBaseUrl: String,
|
|
852
|
-
val kind: BleIncidentLogKind
|
|
867
|
+
val kind: BleIncidentLogKind,
|
|
868
|
+
val tokenOwner: String?
|
|
853
869
|
) {
|
|
854
870
|
val reservedAtMs: Long = SystemClock.elapsedRealtime()
|
|
855
871
|
var session: FileTransferSession? = null
|
|
856
872
|
}
|
|
857
873
|
|
|
874
|
+
/** BES bytes kept on the phone until the MTK log for the same report has been uploaded. */
|
|
875
|
+
private class HeldBesLog(
|
|
876
|
+
val relay: BleIncidentLogRelay,
|
|
877
|
+
val fileName: String,
|
|
878
|
+
val bytes: ByteArray,
|
|
879
|
+
) {
|
|
880
|
+
val savedAtMs: Long = SystemClock.elapsedRealtime()
|
|
881
|
+
@Volatile var uploadStarted: Boolean = false
|
|
882
|
+
var flush: Runnable? = null
|
|
883
|
+
}
|
|
884
|
+
|
|
858
885
|
// Inner class to track incoming file transfers
|
|
859
886
|
internal class FileTransferSession(var fileName: String, var fileSize: Int, flags: Int = 0) {
|
|
860
887
|
private val dynamicPayload = (flags and 0x0002) != 0
|
|
@@ -5132,6 +5159,14 @@ class MentraLive : SGCManager() {
|
|
|
5132
5159
|
Bridge.log("LIVE: ⚠️ glasses_ready: sendCoreTokenToAsgClient threw: " + t)
|
|
5133
5160
|
}
|
|
5134
5161
|
|
|
5162
|
+
// Finish any BES log upload that was acknowledged to the glasses but not yet
|
|
5163
|
+
// confirmed by Cloud before a previous process ended.
|
|
5164
|
+
try {
|
|
5165
|
+
resumePersistedBesUploads()
|
|
5166
|
+
} catch (t: Throwable) {
|
|
5167
|
+
Bridge.log("LIVE: ⚠️ glasses_ready: resumePersistedBesUploads threw: " + t)
|
|
5168
|
+
}
|
|
5169
|
+
|
|
5135
5170
|
// Send stored user email for crash reporting
|
|
5136
5171
|
try {
|
|
5137
5172
|
sendStoredUserEmailToAsgClient()
|
|
@@ -6588,16 +6623,18 @@ class MentraLive : SGCManager() {
|
|
|
6588
6623
|
if (coreToken.isNullOrBlank()) {
|
|
6589
6624
|
throw BluetoothSdkException("core_token_unavailable", "Cannot request incident logs without the current Core access token.")
|
|
6590
6625
|
}
|
|
6626
|
+
// Logs only ever go to the signed-in Core: there is no default to fall back to.
|
|
6627
|
+
val base = apiBaseUrl?.trim().orEmpty()
|
|
6628
|
+
if (base.isEmpty()) {
|
|
6629
|
+
throw BluetoothSdkException("core_url_unavailable", "Cannot request incident logs without the signed-in Core's URL.")
|
|
6630
|
+
}
|
|
6591
6631
|
val sessionGeneration = bleSessionGeneration.get()
|
|
6592
6632
|
try {
|
|
6593
|
-
var base = if (apiBaseUrl != null) apiBaseUrl.trim() else ""
|
|
6594
|
-
if (base.isEmpty()) {
|
|
6595
|
-
base = "https://api.mentra.glass"
|
|
6596
|
-
}
|
|
6597
6633
|
val bKey = IncidentLogBleRelayNaming.bleFileBaseName(incidentId, 'B')
|
|
6598
6634
|
val lKey = IncidentLogBleRelayNaming.bleFileBaseName(incidentId, 'L')
|
|
6599
|
-
val
|
|
6600
|
-
val
|
|
6635
|
+
val tokenOwner = coreTokenOwner(coreToken)
|
|
6636
|
+
val bRelay = BleIncidentLogRelay(bKey, incidentId, base, BleIncidentLogKind.FIRMWARE, tokenOwner)
|
|
6637
|
+
val lRelay = BleIncidentLogRelay(lKey, incidentId, base, BleIncidentLogKind.LOGCAT, tokenOwner)
|
|
6601
6638
|
reserveIncidentRelays(bRelay, lRelay)
|
|
6602
6639
|
|
|
6603
6640
|
val auth = JSONObject()
|
|
@@ -6607,7 +6644,6 @@ class MentraLive : SGCManager() {
|
|
|
6607
6644
|
val json = JSONObject()
|
|
6608
6645
|
json.put("type", "upload_incident_logs")
|
|
6609
6646
|
json.put("incidentId", incidentId)
|
|
6610
|
-
json.put("apiBaseUrl", base)
|
|
6611
6647
|
// Wake ASG with its current token before the upload command enters the same FIFO.
|
|
6612
6648
|
if (!sendJson(auth, true, true, sessionGeneration) ||
|
|
6613
6649
|
sessionGeneration != bleSessionGeneration.get() ||
|
|
@@ -8777,6 +8813,7 @@ class MentraLive : SGCManager() {
|
|
|
8777
8813
|
|
|
8778
8814
|
// Mark as killed to prevent reconnection attempts
|
|
8779
8815
|
isKilled = true
|
|
8816
|
+
clearHeldBesLogs()
|
|
8780
8817
|
classicAudioServiceDiscovery.cancel()
|
|
8781
8818
|
explicitConnectByName = false
|
|
8782
8819
|
// The pairing state itself lives on in MentraLivePairingStateStore.
|
|
@@ -11528,14 +11565,139 @@ class MentraLive : SGCManager() {
|
|
|
11528
11565
|
relay: BleIncidentLogRelay,
|
|
11529
11566
|
fileName: String,
|
|
11530
11567
|
jsonUtf8: ByteArray
|
|
11568
|
+
) {
|
|
11569
|
+
handler.post {
|
|
11570
|
+
// A different report now owns this name, so saving or confirming here would mark
|
|
11571
|
+
// ITS file done. Drop the stale completion before it touches Cloud or the glasses.
|
|
11572
|
+
val owner = bleIncidentLogRelays[relay.fileBaseKey]
|
|
11573
|
+
if (owner != null && owner.incidentId != relay.incidentId) {
|
|
11574
|
+
Log.w(TAG, "Dropping stale incident relay callback for " + relay.fileBaseKey)
|
|
11575
|
+
return@post
|
|
11576
|
+
}
|
|
11577
|
+
if (relay.kind == BleIncidentLogKind.FIRMWARE) {
|
|
11578
|
+
saveBesUntilMtkArrives(relay, fileName, jsonUtf8)
|
|
11579
|
+
} else {
|
|
11580
|
+
postIncidentLog(relay, fileName, jsonUtf8, 0) { success, _ ->
|
|
11581
|
+
finishMtkUpload(relay, fileName, success)
|
|
11582
|
+
}
|
|
11583
|
+
}
|
|
11584
|
+
}
|
|
11585
|
+
}
|
|
11586
|
+
|
|
11587
|
+
/**
|
|
11588
|
+
* The glasses send the BES file first, then wait for transfer_complete before the MTK file.
|
|
11589
|
+
* Keep the BES bytes on the phone and accept the transfer immediately so the MTK log can
|
|
11590
|
+
* arrive and be uploaded first. The BES upload follows that POST.
|
|
11591
|
+
*/
|
|
11592
|
+
private fun saveBesUntilMtkArrives(
|
|
11593
|
+
relay: BleIncidentLogRelay,
|
|
11594
|
+
fileName: String,
|
|
11595
|
+
jsonUtf8: ByteArray,
|
|
11596
|
+
) {
|
|
11597
|
+
val held = HeldBesLog(relay, fileName, jsonUtf8)
|
|
11598
|
+
// The glasses drop their copy once they see this confirmation, so persist the bytes
|
|
11599
|
+
// before acknowledging ownership. A process death or destroy() during the hold/upload
|
|
11600
|
+
// then resumes from disk instead of stranding an already-acknowledged incident. With no
|
|
11601
|
+
// durable copy, the glasses keep theirs and retry.
|
|
11602
|
+
if (!persistHeldBes(held)) {
|
|
11603
|
+
relay.session = null
|
|
11604
|
+
sendTransferCompleteConfirmation(fileName, false)
|
|
11605
|
+
return
|
|
11606
|
+
}
|
|
11607
|
+
val previous = heldBesLogs[relay.incidentId]
|
|
11608
|
+
previous?.flush?.let { handler.removeCallbacks(it) }
|
|
11609
|
+
val flush = Runnable { flushHeldBesIfMtkMissing(relay.incidentId) }
|
|
11610
|
+
held.flush = flush
|
|
11611
|
+
heldBesLogs[relay.incidentId] = held
|
|
11612
|
+
handler.postDelayed(flush, BES_HOLD_MS)
|
|
11613
|
+
sendTransferCompleteConfirmation(fileName, true)
|
|
11614
|
+
Bridge.log("LIVE: Saved BES log until the MTK log is uploaded: " + relay.incidentId)
|
|
11615
|
+
}
|
|
11616
|
+
|
|
11617
|
+
private fun mtkTransferInProgress(incidentId: String): Boolean {
|
|
11618
|
+
for (relay in bleIncidentLogRelays.values) {
|
|
11619
|
+
if (relay.incidentId != incidentId || relay.kind != BleIncidentLogKind.LOGCAT) continue
|
|
11620
|
+
val session = relay.session ?: return false
|
|
11621
|
+
return !session.isComplete
|
|
11622
|
+
}
|
|
11623
|
+
return false
|
|
11624
|
+
}
|
|
11625
|
+
|
|
11626
|
+
private fun flushHeldBesIfMtkMissing(incidentId: String) {
|
|
11627
|
+
val held = heldBesLogs[incidentId] ?: return
|
|
11628
|
+
if (held.uploadStarted) return
|
|
11629
|
+
val waited = SystemClock.elapsedRealtime() - held.savedAtMs
|
|
11630
|
+
if (waited < BES_MTK_WAIT_MAX_MS && mtkTransferInProgress(incidentId)) {
|
|
11631
|
+
val again = Runnable { flushHeldBesIfMtkMissing(incidentId) }
|
|
11632
|
+
held.flush = again
|
|
11633
|
+
handler.postDelayed(again, BES_HOLD_MS)
|
|
11634
|
+
return
|
|
11635
|
+
}
|
|
11636
|
+
startHeldBesUpload(incidentId)
|
|
11637
|
+
}
|
|
11638
|
+
|
|
11639
|
+
private fun finishMtkUpload(relay: BleIncidentLogRelay, fileName: String, success: Boolean) {
|
|
11640
|
+
if (!success) {
|
|
11641
|
+
// Keep the saved BES bytes. The glasses retry the MTK file; it is uploaded first.
|
|
11642
|
+
relay.session = null
|
|
11643
|
+
sendTransferCompleteConfirmation(fileName, false)
|
|
11644
|
+
return
|
|
11645
|
+
}
|
|
11646
|
+
sendTransferCompleteConfirmation(fileName, true)
|
|
11647
|
+
// Release only after the glasses' confirmation is queued, so a report that reuses
|
|
11648
|
+
// this name is ordered behind the owner's completion.
|
|
11649
|
+
bleIncidentLogRelays.remove(relay.fileBaseKey, relay)
|
|
11650
|
+
startHeldBesUpload(relay.incidentId)
|
|
11651
|
+
}
|
|
11652
|
+
|
|
11653
|
+
private fun startHeldBesUpload(incidentId: String) {
|
|
11654
|
+
val held = heldBesLogs[incidentId] ?: return
|
|
11655
|
+
if (held.uploadStarted) return
|
|
11656
|
+
held.uploadStarted = true
|
|
11657
|
+
held.flush?.let { handler.removeCallbacks(it) }
|
|
11658
|
+
held.flush = null
|
|
11659
|
+
// transfer_complete already told the glasses this file arrived, so they will not resend
|
|
11660
|
+
// it. Retry a failed Cloud POST on the phone.
|
|
11661
|
+
postIncidentLog(held.relay, held.fileName, held.bytes, 1) { success, status ->
|
|
11662
|
+
val rejected = IncidentLogBleUploadService.isPermanentRejection(status)
|
|
11663
|
+
if (rejected) {
|
|
11664
|
+
Log.e(TAG, "Cloud refused the BES log for " + incidentId + ": HTTP " + status)
|
|
11665
|
+
}
|
|
11666
|
+
if (success || rejected) {
|
|
11667
|
+
heldBesLogs.remove(incidentId, held)
|
|
11668
|
+
bleIncidentLogRelays.remove(held.relay.fileBaseKey, held.relay)
|
|
11669
|
+
deletePersistedBes(incidentId)
|
|
11670
|
+
} else {
|
|
11671
|
+
Log.e(TAG, "BES log upload failed after the MTK log for " + incidentId)
|
|
11672
|
+
// The flush timer is gone and the MTK relay is released, so nothing else will
|
|
11673
|
+
// retry this acknowledged artifact. Keep the durable copy and schedule another
|
|
11674
|
+
// attempt until Cloud confirms it.
|
|
11675
|
+
held.uploadStarted = false
|
|
11676
|
+
val retry = Runnable { startHeldBesUpload(incidentId) }
|
|
11677
|
+
held.flush = retry
|
|
11678
|
+
handler.postDelayed(retry, BES_RETRY_MS)
|
|
11679
|
+
}
|
|
11680
|
+
}
|
|
11681
|
+
}
|
|
11682
|
+
|
|
11683
|
+
private fun postIncidentLog(
|
|
11684
|
+
relay: BleIncidentLogRelay,
|
|
11685
|
+
fileName: String,
|
|
11686
|
+
jsonUtf8: ByteArray,
|
|
11687
|
+
attemptsLeft: Int,
|
|
11688
|
+
onDone: (Boolean, Int) -> Unit,
|
|
11531
11689
|
) {
|
|
11532
11690
|
val token = getCoreToken()
|
|
11691
|
+
// After a sign-out or Core switch, the current token must not reach the report's Core.
|
|
11692
|
+
if (coreTokenOwner(token) != relay.tokenOwner) {
|
|
11693
|
+
handler.post { onDone(false, 0) }
|
|
11694
|
+
return
|
|
11695
|
+
}
|
|
11533
11696
|
IncidentLogBleUploadService.upload(relay.apiBaseUrl, relay.incidentId, token, jsonUtf8) {
|
|
11534
11697
|
success,
|
|
11698
|
+
status,
|
|
11535
11699
|
message ->
|
|
11536
|
-
|
|
11537
|
-
// A different report now owns this name (it was admitted after our owner finished),
|
|
11538
|
-
// so a completion here would mark ITS file done. Drop the stale callback.
|
|
11700
|
+
handler.post {
|
|
11539
11701
|
val owner = bleIncidentLogRelays[relay.fileBaseKey]
|
|
11540
11702
|
if (owner != null && owner.incidentId != relay.incidentId) {
|
|
11541
11703
|
Log.w(TAG, "Dropping stale incident relay callback for " + relay.fileBaseKey)
|
|
@@ -11548,6 +11710,9 @@ class MentraLive : SGCManager() {
|
|
|
11548
11710
|
"): " +
|
|
11549
11711
|
relay.incidentId
|
|
11550
11712
|
)
|
|
11713
|
+
onDone(true, status)
|
|
11714
|
+
} else if (attemptsLeft > 0 && !IncidentLogBleUploadService.isPermanentRejection(status)) {
|
|
11715
|
+
postIncidentLog(relay, fileName, jsonUtf8, attemptsLeft - 1, onDone)
|
|
11551
11716
|
} else {
|
|
11552
11717
|
Log.e(
|
|
11553
11718
|
TAG,
|
|
@@ -11556,14 +11721,149 @@ class MentraLive : SGCManager() {
|
|
|
11556
11721
|
"): " +
|
|
11557
11722
|
message
|
|
11558
11723
|
)
|
|
11559
|
-
|
|
11560
|
-
relay.session = null
|
|
11724
|
+
onDone(false, status)
|
|
11561
11725
|
}
|
|
11562
|
-
|
|
11563
|
-
|
|
11564
|
-
|
|
11565
|
-
|
|
11566
|
-
|
|
11726
|
+
}
|
|
11727
|
+
}
|
|
11728
|
+
}
|
|
11729
|
+
|
|
11730
|
+
/** Who a Core access token speaks for, or null if it isn't a readable token. */
|
|
11731
|
+
private fun coreTokenOwner(token: String?): String? {
|
|
11732
|
+
return try {
|
|
11733
|
+
val payload = token?.split(".")?.getOrNull(1) ?: return null
|
|
11734
|
+
val decoded = android.util.Base64.decode(
|
|
11735
|
+
payload, android.util.Base64.URL_SAFE or android.util.Base64.NO_WRAP)
|
|
11736
|
+
val claims = JSONObject(String(decoded, StandardCharsets.UTF_8))
|
|
11737
|
+
claims.optString("iss") + "|" + claims.optString("sub")
|
|
11738
|
+
} catch (e: Exception) {
|
|
11739
|
+
null
|
|
11740
|
+
}
|
|
11741
|
+
}
|
|
11742
|
+
|
|
11743
|
+
private fun clearHeldBesLogs() {
|
|
11744
|
+
// Only drop the in-memory timers and cache; the on-disk copies survive so a later
|
|
11745
|
+
// startup can finish uploading an acknowledged incident. Confirmed uploads already
|
|
11746
|
+
// deleted their files.
|
|
11747
|
+
for (held in heldBesLogs.values) {
|
|
11748
|
+
held.flush?.let { handler.removeCallbacks(it) }
|
|
11749
|
+
}
|
|
11750
|
+
heldBesLogs.clear()
|
|
11751
|
+
}
|
|
11752
|
+
|
|
11753
|
+
private fun besHeldDir(): File? {
|
|
11754
|
+
val ctx = context ?: return null
|
|
11755
|
+
val base = ctx.getExternalFilesDir(null) ?: return null
|
|
11756
|
+
val dir = File(base, BES_HELD_DIR)
|
|
11757
|
+
if (!dir.exists() && !dir.mkdirs()) return null
|
|
11758
|
+
return dir
|
|
11759
|
+
}
|
|
11760
|
+
|
|
11761
|
+
private fun besHeldFileName(incidentId: String): String =
|
|
11762
|
+
incidentId.replace(Regex("[^A-Za-z0-9._-]"), "_") + ".json"
|
|
11763
|
+
|
|
11764
|
+
/** Durably store a held BES log so its Cloud POST can survive process death or destroy(). */
|
|
11765
|
+
private fun persistHeldBes(held: HeldBesLog): Boolean {
|
|
11766
|
+
try {
|
|
11767
|
+
val dir = besHeldDir() ?: return false
|
|
11768
|
+
val record = JSONObject()
|
|
11769
|
+
record.put("incidentId", held.relay.incidentId)
|
|
11770
|
+
record.put("fileBaseKey", held.relay.fileBaseKey)
|
|
11771
|
+
record.put("apiBaseUrl", held.relay.apiBaseUrl)
|
|
11772
|
+
record.put("tokenOwner", held.relay.tokenOwner ?: JSONObject.NULL)
|
|
11773
|
+
record.put("fileName", held.fileName)
|
|
11774
|
+
record.put(
|
|
11775
|
+
"bytesBase64",
|
|
11776
|
+
android.util.Base64.encodeToString(held.bytes, android.util.Base64.NO_WRAP),
|
|
11777
|
+
)
|
|
11778
|
+
val tmp = File(dir, besHeldFileName(held.relay.incidentId) + ".tmp")
|
|
11779
|
+
FileOutputStream(tmp).use {
|
|
11780
|
+
it.write(record.toString().toByteArray(StandardCharsets.UTF_8))
|
|
11781
|
+
it.fd.sync()
|
|
11782
|
+
}
|
|
11783
|
+
// Rename so a reader never observes a partially written record.
|
|
11784
|
+
if (!tmp.renameTo(File(dir, besHeldFileName(held.relay.incidentId)))) {
|
|
11785
|
+
tmp.delete()
|
|
11786
|
+
return false
|
|
11787
|
+
}
|
|
11788
|
+
return true
|
|
11789
|
+
} catch (e: Exception) {
|
|
11790
|
+
Log.e(TAG, "Failed to persist held BES log for " + held.relay.incidentId, e)
|
|
11791
|
+
return false
|
|
11792
|
+
}
|
|
11793
|
+
}
|
|
11794
|
+
|
|
11795
|
+
private fun deletePersistedBes(incidentId: String) {
|
|
11796
|
+
try {
|
|
11797
|
+
val dir = besHeldDir() ?: return
|
|
11798
|
+
File(dir, besHeldFileName(incidentId)).delete()
|
|
11799
|
+
} catch (e: Exception) {
|
|
11800
|
+
Log.e(TAG, "Failed to delete persisted BES log for " + incidentId, e)
|
|
11801
|
+
}
|
|
11802
|
+
}
|
|
11803
|
+
|
|
11804
|
+
/**
|
|
11805
|
+
* Re-POST any BES logs that were acknowledged to the glasses but whose Cloud upload never
|
|
11806
|
+
* confirmed before the previous process ended. The glasses no longer hold these, so the
|
|
11807
|
+
* phone is the only source; keep retrying until Cloud accepts each one.
|
|
11808
|
+
*/
|
|
11809
|
+
private fun resumePersistedBesUploads() {
|
|
11810
|
+
if (isKilled) return
|
|
11811
|
+
val dir = besHeldDir() ?: return
|
|
11812
|
+
val files = dir.listFiles { file -> file.name.endsWith(".json") } ?: return
|
|
11813
|
+
for (file in files) {
|
|
11814
|
+
// An entry still tracked in memory is being handled by its live flush/upload path.
|
|
11815
|
+
if (heldBesLogs.values.any { besHeldFileName(it.relay.incidentId) == file.name }) continue
|
|
11816
|
+
if (!resumingBesFiles.add(file.name)) continue
|
|
11817
|
+
resumePersistedBesFile(file)
|
|
11818
|
+
}
|
|
11819
|
+
}
|
|
11820
|
+
|
|
11821
|
+
private fun resumePersistedBesFile(file: File) {
|
|
11822
|
+
if (isKilled) {
|
|
11823
|
+
resumingBesFiles.remove(file.name)
|
|
11824
|
+
return
|
|
11825
|
+
}
|
|
11826
|
+
val record =
|
|
11827
|
+
try {
|
|
11828
|
+
JSONObject(String(file.readBytes(), StandardCharsets.UTF_8))
|
|
11829
|
+
} catch (e: Exception) {
|
|
11830
|
+
Log.e(TAG, "Dropping unreadable persisted BES log: " + file.name, e)
|
|
11831
|
+
file.delete()
|
|
11832
|
+
resumingBesFiles.remove(file.name)
|
|
11833
|
+
return
|
|
11834
|
+
}
|
|
11835
|
+
val incidentId = record.optString("incidentId")
|
|
11836
|
+
val apiBaseUrl = record.optString("apiBaseUrl")
|
|
11837
|
+
val bytes =
|
|
11838
|
+
try {
|
|
11839
|
+
android.util.Base64.decode(record.optString("bytesBase64"), android.util.Base64.NO_WRAP)
|
|
11840
|
+
} catch (e: IllegalArgumentException) {
|
|
11841
|
+
Log.e(TAG, "Dropping persisted BES log with bad payload: " + file.name, e)
|
|
11842
|
+
file.delete()
|
|
11843
|
+
resumingBesFiles.remove(file.name)
|
|
11844
|
+
return
|
|
11845
|
+
}
|
|
11846
|
+
if (incidentId.isEmpty() || apiBaseUrl.isEmpty()) {
|
|
11847
|
+
file.delete()
|
|
11848
|
+
resumingBesFiles.remove(file.name)
|
|
11849
|
+
return
|
|
11850
|
+
}
|
|
11851
|
+
val token = getCoreToken()
|
|
11852
|
+
val tokenOwner = if (record.isNull("tokenOwner")) null else record.optString("tokenOwner")
|
|
11853
|
+
// A different account or Core is signed in: wait for the one this report belongs to.
|
|
11854
|
+
if (coreTokenOwner(token) != tokenOwner) {
|
|
11855
|
+
handler.postDelayed({ resumePersistedBesFile(file) }, BES_RETRY_MS)
|
|
11856
|
+
return
|
|
11857
|
+
}
|
|
11858
|
+
IncidentLogBleUploadService.upload(apiBaseUrl, incidentId, token, bytes) { success, status, message ->
|
|
11859
|
+
handler.post {
|
|
11860
|
+
if (success || IncidentLogBleUploadService.isPermanentRejection(status)) {
|
|
11861
|
+
Bridge.log("LIVE: Resumed BES log upload finished for " + incidentId + ": HTTP " + status)
|
|
11862
|
+
file.delete()
|
|
11863
|
+
resumingBesFiles.remove(file.name)
|
|
11864
|
+
} else if (!isKilled) {
|
|
11865
|
+
Log.e(TAG, "Resumed BES log upload failed for " + incidentId + ": " + message)
|
|
11866
|
+
handler.postDelayed({ resumePersistedBesFile(file) }, BES_RETRY_MS)
|
|
11567
11867
|
}
|
|
11568
11868
|
}
|
|
11569
11869
|
}
|
package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleUploadService.java
CHANGED
|
@@ -24,7 +24,14 @@ public final class IncidentLogBleUploadService {
|
|
|
24
24
|
private IncidentLogBleUploadService() {}
|
|
25
25
|
|
|
26
26
|
public interface Callback {
|
|
27
|
-
|
|
27
|
+
/** @param httpStatus Cloud's status code, or 0 when no response arrived. */
|
|
28
|
+
void onDone(boolean success, int httpStatus, String message);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Cloud will refuse these bytes every time: missing report, invalid, conflicting or oversized body. */
|
|
32
|
+
public static boolean isPermanentRejection(int httpStatus) {
|
|
33
|
+
return httpStatus == 400 || httpStatus == 404 || httpStatus == 409 || httpStatus == 410
|
|
34
|
+
|| httpStatus == 413 || httpStatus == 422;
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
public static void upload(String apiBaseUrl, String incidentId, String authToken,
|
|
@@ -32,12 +39,12 @@ public final class IncidentLogBleUploadService {
|
|
|
32
39
|
new Thread(() -> {
|
|
33
40
|
try {
|
|
34
41
|
if (authToken == null || authToken.isEmpty()) {
|
|
35
|
-
callback.onDone(false, "no auth token");
|
|
42
|
+
callback.onDone(false, 0, "no auth token");
|
|
36
43
|
return;
|
|
37
44
|
}
|
|
38
45
|
HttpUrl baseHttpUrl = HttpUrl.parse(apiBaseUrl != null ? apiBaseUrl.trim() : "");
|
|
39
46
|
if (baseHttpUrl == null) {
|
|
40
|
-
callback.onDone(false, "empty or malformed api base url");
|
|
47
|
+
callback.onDone(false, 0, "empty or malformed api base url");
|
|
41
48
|
return;
|
|
42
49
|
}
|
|
43
50
|
HttpUrl url = baseHttpUrl.newBuilder()
|
|
@@ -61,16 +68,16 @@ public final class IncidentLogBleUploadService {
|
|
|
61
68
|
try (Response response = client.newCall(request).execute()) {
|
|
62
69
|
if (response.isSuccessful()) {
|
|
63
70
|
NativeLog.i(TAG, "Incident BLE relay upload OK: " + url);
|
|
64
|
-
callback.onDone(true, null);
|
|
71
|
+
callback.onDone(true, response.code(), null);
|
|
65
72
|
} else {
|
|
66
73
|
String msg = "HTTP " + response.code();
|
|
67
74
|
NativeLog.e(TAG, "Incident BLE relay upload failed: " + msg + " " + url);
|
|
68
|
-
callback.onDone(false, msg);
|
|
75
|
+
callback.onDone(false, response.code(), msg);
|
|
69
76
|
}
|
|
70
77
|
}
|
|
71
78
|
} catch (IOException e) {
|
|
72
79
|
NativeLog.e(TAG, "Incident BLE relay upload IO error", e);
|
|
73
|
-
callback.onDone(false, e.getMessage());
|
|
80
|
+
callback.onDone(false, 0, e.getMessage());
|
|
74
81
|
}
|
|
75
82
|
}).start();
|
|
76
83
|
}
|