@mentra/bluetooth-sdk 3.5.0-dev.1260 → 3.5.0-dev.1262
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
|
@@ -701,23 +701,42 @@ enum BleIncidentLogRelayKind {
|
|
|
701
701
|
case logcat
|
|
702
702
|
}
|
|
703
703
|
|
|
704
|
+
/// BES bytes kept on the phone until the MTK log for the same report has been uploaded.
|
|
705
|
+
private final class HeldBesLog {
|
|
706
|
+
let relay: BleIncidentLogRelayEntry
|
|
707
|
+
let fileName: String
|
|
708
|
+
let data: Data
|
|
709
|
+
let savedAt: TimeInterval
|
|
710
|
+
var uploadStarted = false
|
|
711
|
+
var flush: DispatchWorkItem?
|
|
712
|
+
|
|
713
|
+
init(relay: BleIncidentLogRelayEntry, fileName: String, data: Data, savedAt: TimeInterval) {
|
|
714
|
+
self.relay = relay
|
|
715
|
+
self.fileName = fileName
|
|
716
|
+
self.data = data
|
|
717
|
+
self.savedAt = savedAt
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
704
721
|
final class BleIncidentLogRelayEntry {
|
|
705
722
|
let fileBaseKey: String
|
|
706
723
|
let incidentId: String
|
|
707
724
|
let apiBaseUrl: String
|
|
708
725
|
let kind: BleIncidentLogRelayKind
|
|
709
726
|
let reservedAt: TimeInterval
|
|
727
|
+
let tokenOwner: String?
|
|
710
728
|
var session: MentraLiveFileTransferSession?
|
|
711
729
|
|
|
712
730
|
init(
|
|
713
731
|
fileBaseKey: String, incidentId: String, apiBaseUrl: String, kind: BleIncidentLogRelayKind,
|
|
714
|
-
reservedAt: TimeInterval
|
|
732
|
+
reservedAt: TimeInterval, tokenOwner: String?
|
|
715
733
|
) {
|
|
716
734
|
self.fileBaseKey = fileBaseKey
|
|
717
735
|
self.incidentId = incidentId
|
|
718
736
|
self.apiBaseUrl = apiBaseUrl
|
|
719
737
|
self.kind = kind
|
|
720
738
|
self.reservedAt = reservedAt
|
|
739
|
+
self.tokenOwner = tokenOwner
|
|
721
740
|
}
|
|
722
741
|
}
|
|
723
742
|
|
|
@@ -1654,6 +1673,21 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1654
1673
|
private var activeFileTransfers = [String: MentraLiveFileTransferSession]()
|
|
1655
1674
|
private var blePhotoTransfers = [String: BlePhotoTransfer]()
|
|
1656
1675
|
private(set) var bleIncidentLogRelays = [String: BleIncidentLogRelayEntry]()
|
|
1676
|
+
/// BES bytes kept on the phone until the MTK log for the same report has been uploaded.
|
|
1677
|
+
private var heldBesLogs = [String: HeldBesLog]()
|
|
1678
|
+
/// The BES file arrives first. Hold it this long so the MTK log can be uploaded before it.
|
|
1679
|
+
var besHoldSeconds: TimeInterval = 30
|
|
1680
|
+
/// A link lost mid-transfer leaves the MTK file incomplete with no terminal event, so stop
|
|
1681
|
+
/// waiting after the glasses' own 120 s limit on that transfer.
|
|
1682
|
+
var besMtkWaitMaxSeconds: TimeInterval = 150
|
|
1683
|
+
/// The glasses delete a BES incident once they receive transfer_complete, so the phone owns
|
|
1684
|
+
/// the only copy afterward. Retry the Cloud POST on this cadence until it is confirmed,
|
|
1685
|
+
/// surviving a connectivity outage or process death.
|
|
1686
|
+
var besRetrySeconds: TimeInterval = 60
|
|
1687
|
+
/// Files with a resume attempt or retry pending, so a reconnect doesn't start a second one.
|
|
1688
|
+
private var resumingBesFiles = Set<String>()
|
|
1689
|
+
/// Durable store for held BES logs whose Cloud upload has not yet been confirmed.
|
|
1690
|
+
private let besHeldDirName = "MentraLive_HeldBes"
|
|
1657
1691
|
// Longer than the glasses' bounded relay of one report (BES logs, two file transfers, the
|
|
1658
1692
|
// phone POST), so an unreleased name cannot be reused while its report is in flight.
|
|
1659
1693
|
static let incidentRelayOwnershipSeconds: TimeInterval = 600
|
|
@@ -4517,9 +4551,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
4517
4551
|
guard !coreToken.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
|
4518
4552
|
throw BluetoothSdkError(code: "core_token_unavailable", message: "Cannot request incident logs without the current Core access token.")
|
|
4519
4553
|
}
|
|
4554
|
+
// Logs only ever go to the signed-in Core: there is no default to fall back to.
|
|
4520
4555
|
var base = (apiBaseUrl ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
|
|
4521
|
-
|
|
4522
|
-
|
|
4556
|
+
guard !base.isEmpty else {
|
|
4557
|
+
throw BluetoothSdkError(code: "core_url_unavailable", message: "Cannot request incident logs without the signed-in Core's URL.")
|
|
4523
4558
|
}
|
|
4524
4559
|
while base.hasSuffix("/") {
|
|
4525
4560
|
base = String(base.dropLast())
|
|
@@ -4527,11 +4562,14 @@ class MentraLive: NSObject, SGCManager {
|
|
|
4527
4562
|
let bKey = MentraLive.incidentBleFileBase(incidentId: incidentId, prefix: "B")
|
|
4528
4563
|
let lKey = MentraLive.incidentBleFileBase(incidentId: incidentId, prefix: "L")
|
|
4529
4564
|
let now = incidentRelayClock()
|
|
4565
|
+
let tokenOwner = MentraLive.coreTokenOwner(coreToken)
|
|
4530
4566
|
let bRelay = BleIncidentLogRelayEntry(
|
|
4531
|
-
fileBaseKey: bKey, incidentId: incidentId, apiBaseUrl: base, kind: .firmware, reservedAt: now
|
|
4567
|
+
fileBaseKey: bKey, incidentId: incidentId, apiBaseUrl: base, kind: .firmware, reservedAt: now,
|
|
4568
|
+
tokenOwner: tokenOwner
|
|
4532
4569
|
)
|
|
4533
4570
|
let lRelay = BleIncidentLogRelayEntry(
|
|
4534
|
-
fileBaseKey: lKey, incidentId: incidentId, apiBaseUrl: base, kind: .logcat, reservedAt: now
|
|
4571
|
+
fileBaseKey: lKey, incidentId: incidentId, apiBaseUrl: base, kind: .logcat, reservedAt: now,
|
|
4572
|
+
tokenOwner: tokenOwner
|
|
4535
4573
|
)
|
|
4536
4574
|
try reserveIncidentRelays([bRelay, lRelay], now: now)
|
|
4537
4575
|
|
|
@@ -4541,7 +4579,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
4541
4579
|
]
|
|
4542
4580
|
// MainActor queue insertion preserves auth-before-upload order, including all fragments.
|
|
4543
4581
|
guard sendJson(auth, wakeUp: true, requireAck: true, bridgeLogging: true),
|
|
4544
|
-
sendJson(["type": "upload_incident_logs", "incidentId": incidentId
|
|
4582
|
+
sendJson(["type": "upload_incident_logs", "incidentId": incidentId],
|
|
4545
4583
|
wakeUp: true, requireAck: true, bridgeLogging: true)
|
|
4546
4584
|
else {
|
|
4547
4585
|
releaseIncidentRelay(bRelay)
|
|
@@ -4816,6 +4854,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
4816
4854
|
sendCoreTokenToAsgClient()
|
|
4817
4855
|
sendStoredUserEmailToAsgClient()
|
|
4818
4856
|
|
|
4857
|
+
// Finish any BES log upload that was acknowledged to the glasses but not yet confirmed
|
|
4858
|
+
// by Cloud before a previous process ended.
|
|
4859
|
+
resumePersistedBesUploads()
|
|
4860
|
+
|
|
4819
4861
|
// Send user settings to glasses
|
|
4820
4862
|
sendUserSettings()
|
|
4821
4863
|
|
|
@@ -5534,20 +5576,257 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5534
5576
|
func uploadBleIncidentLogRelay(
|
|
5535
5577
|
relay: BleIncidentLogRelayEntry, fileName: String, data: Data
|
|
5536
5578
|
) {
|
|
5537
|
-
|
|
5538
|
-
|
|
5579
|
+
// A different report now owns this name, so saving or confirming here would mark
|
|
5580
|
+
// its file done. Drop the stale completion before it touches Cloud or the glasses.
|
|
5581
|
+
if let owner = bleIncidentLogRelays[relay.fileBaseKey], owner.incidentId != relay.incidentId {
|
|
5582
|
+
Bridge.log("LIVE: Dropping stale incident relay callback for \(relay.fileBaseKey)")
|
|
5583
|
+
return
|
|
5584
|
+
}
|
|
5585
|
+
if relay.kind == .firmware {
|
|
5586
|
+
saveBesUntilMtkArrives(relay: relay, fileName: fileName, data: data)
|
|
5587
|
+
return
|
|
5588
|
+
}
|
|
5589
|
+
postIncidentLog(relay: relay, fileName: fileName, data: data, attemptsLeft: 0) { success, _ in
|
|
5590
|
+
self.finishMtkUpload(relay: relay, fileName: fileName, success: success)
|
|
5591
|
+
}
|
|
5592
|
+
}
|
|
5593
|
+
|
|
5594
|
+
/// The glasses send the BES file first, then wait for transfer_complete before the MTK file.
|
|
5595
|
+
/// Keep the BES bytes on the phone and accept the transfer immediately so the MTK log can
|
|
5596
|
+
/// arrive and be uploaded first. The BES upload follows that POST.
|
|
5597
|
+
private func saveBesUntilMtkArrives(
|
|
5598
|
+
relay: BleIncidentLogRelayEntry, fileName: String, data: Data
|
|
5599
|
+
) {
|
|
5600
|
+
let held = HeldBesLog(
|
|
5601
|
+
relay: relay, fileName: fileName, data: data, savedAt: incidentRelayClock()
|
|
5602
|
+
)
|
|
5603
|
+
// The glasses drop their copy once they see this confirmation, so persist the bytes
|
|
5604
|
+
// before acknowledging ownership. A process death or destroy() during the hold/upload
|
|
5605
|
+
// then resumes from disk instead of stranding an already-acknowledged incident. With no
|
|
5606
|
+
// durable copy, the glasses keep theirs and retry.
|
|
5607
|
+
guard persistHeldBes(held) else {
|
|
5608
|
+
relay.session = nil
|
|
5539
5609
|
sendTransferCompleteConfirmation(fileName: fileName, success: false)
|
|
5540
|
-
|
|
5541
|
-
|
|
5610
|
+
return
|
|
5611
|
+
}
|
|
5612
|
+
heldBesLogs[relay.incidentId]?.flush?.cancel()
|
|
5613
|
+
let flush = DispatchWorkItem { [weak self] in
|
|
5614
|
+
self?.flushHeldBesIfMtkMissing(relay.incidentId)
|
|
5615
|
+
}
|
|
5616
|
+
held.flush = flush
|
|
5617
|
+
heldBesLogs[relay.incidentId] = held
|
|
5618
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + besHoldSeconds, execute: flush)
|
|
5619
|
+
sendTransferCompleteConfirmation(fileName: fileName, success: true)
|
|
5620
|
+
Bridge.log("LIVE: Saved BES log until the MTK log is uploaded: \(relay.incidentId)")
|
|
5621
|
+
}
|
|
5622
|
+
|
|
5623
|
+
private func mtkTransferInProgress(_ incidentId: String) -> Bool {
|
|
5624
|
+
for relay in bleIncidentLogRelays.values {
|
|
5625
|
+
if relay.incidentId != incidentId || relay.kind != .logcat { continue }
|
|
5626
|
+
guard let session = relay.session else { return false }
|
|
5627
|
+
return !session.isComplete
|
|
5628
|
+
}
|
|
5629
|
+
return false
|
|
5630
|
+
}
|
|
5631
|
+
|
|
5632
|
+
private func flushHeldBesIfMtkMissing(_ incidentId: String) {
|
|
5633
|
+
guard let held = heldBesLogs[incidentId], !held.uploadStarted else { return }
|
|
5634
|
+
if incidentRelayClock() - held.savedAt < besMtkWaitMaxSeconds, mtkTransferInProgress(incidentId) {
|
|
5635
|
+
let again = DispatchWorkItem { [weak self] in
|
|
5636
|
+
self?.flushHeldBesIfMtkMissing(incidentId)
|
|
5542
5637
|
}
|
|
5638
|
+
held.flush = again
|
|
5639
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + besHoldSeconds, execute: again)
|
|
5543
5640
|
return
|
|
5544
5641
|
}
|
|
5642
|
+
startHeldBesUpload(incidentId)
|
|
5643
|
+
}
|
|
5545
5644
|
|
|
5546
|
-
|
|
5547
|
-
|
|
5645
|
+
private func finishMtkUpload(
|
|
5646
|
+
relay: BleIncidentLogRelayEntry, fileName: String, success: Bool
|
|
5647
|
+
) {
|
|
5648
|
+
if !success {
|
|
5649
|
+
// Keep the saved BES bytes. The glasses retry the MTK file; it is uploaded first.
|
|
5548
5650
|
if let existing = bleIncidentLogRelays[relay.fileBaseKey] {
|
|
5549
5651
|
existing.session = nil
|
|
5550
5652
|
}
|
|
5653
|
+
sendTransferCompleteConfirmation(fileName: fileName, success: false)
|
|
5654
|
+
return
|
|
5655
|
+
}
|
|
5656
|
+
sendTransferCompleteConfirmation(fileName: fileName, success: true)
|
|
5657
|
+
// Release only after the glasses' confirmation is queued, so a report that reuses
|
|
5658
|
+
// this name is ordered behind the owner's completion.
|
|
5659
|
+
releaseIncidentRelay(relay)
|
|
5660
|
+
startHeldBesUpload(relay.incidentId)
|
|
5661
|
+
}
|
|
5662
|
+
|
|
5663
|
+
private func startHeldBesUpload(_ incidentId: String) {
|
|
5664
|
+
guard let held = heldBesLogs[incidentId], !held.uploadStarted else { return }
|
|
5665
|
+
held.uploadStarted = true
|
|
5666
|
+
held.flush?.cancel()
|
|
5667
|
+
held.flush = nil
|
|
5668
|
+
// transfer_complete already told the glasses this file arrived, so they will not resend
|
|
5669
|
+
// it. Retry a failed Cloud POST on the phone.
|
|
5670
|
+
postIncidentLog(
|
|
5671
|
+
relay: held.relay, fileName: held.fileName, data: held.data, attemptsLeft: 1
|
|
5672
|
+
) { success, status in
|
|
5673
|
+
let rejected = MentraLive.isPermanentIncidentRejection(status)
|
|
5674
|
+
if rejected {
|
|
5675
|
+
Bridge.log("LIVE: Cloud refused the BES log for \(incidentId): HTTP \(status ?? 0)")
|
|
5676
|
+
}
|
|
5677
|
+
if success || rejected {
|
|
5678
|
+
if self.heldBesLogs[incidentId] === held {
|
|
5679
|
+
self.heldBesLogs.removeValue(forKey: incidentId)
|
|
5680
|
+
}
|
|
5681
|
+
self.releaseIncidentRelay(held.relay)
|
|
5682
|
+
self.deletePersistedBes(incidentId)
|
|
5683
|
+
} else {
|
|
5684
|
+
Bridge.log("LIVE: BES log upload failed after the MTK log for \(incidentId)")
|
|
5685
|
+
// The flush timer is gone and the MTK relay is released, so nothing else will
|
|
5686
|
+
// retry this acknowledged artifact. Keep the durable copy and schedule another
|
|
5687
|
+
// attempt until Cloud confirms it.
|
|
5688
|
+
held.uploadStarted = false
|
|
5689
|
+
let retry = DispatchWorkItem { [weak self] in
|
|
5690
|
+
self?.startHeldBesUpload(incidentId)
|
|
5691
|
+
}
|
|
5692
|
+
held.flush = retry
|
|
5693
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + self.besRetrySeconds, execute: retry)
|
|
5694
|
+
}
|
|
5695
|
+
}
|
|
5696
|
+
}
|
|
5697
|
+
|
|
5698
|
+
private func besHeldDir() -> URL? {
|
|
5699
|
+
guard let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
|
|
5700
|
+
else { return nil }
|
|
5701
|
+
let dir = base.appendingPathComponent(besHeldDirName, isDirectory: true)
|
|
5702
|
+
if !FileManager.default.fileExists(atPath: dir.path) {
|
|
5703
|
+
do {
|
|
5704
|
+
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
5705
|
+
} catch {
|
|
5706
|
+
Bridge.log("LIVE: Failed to create held BES directory: \(error)")
|
|
5707
|
+
return nil
|
|
5708
|
+
}
|
|
5709
|
+
}
|
|
5710
|
+
return dir
|
|
5711
|
+
}
|
|
5712
|
+
|
|
5713
|
+
private func besHeldFileURL(_ incidentId: String) -> URL? {
|
|
5714
|
+
let safe = incidentId.map { $0.isLetter || $0.isNumber || $0 == "." || $0 == "-" || $0 == "_" ? $0 : "_" }
|
|
5715
|
+
return besHeldDir()?.appendingPathComponent(String(safe) + ".json")
|
|
5716
|
+
}
|
|
5717
|
+
|
|
5718
|
+
/// Durably store a held BES log so its Cloud POST can survive process death or destroy().
|
|
5719
|
+
private func persistHeldBes(_ held: HeldBesLog) -> Bool {
|
|
5720
|
+
guard let url = besHeldFileURL(held.relay.incidentId) else { return false }
|
|
5721
|
+
let record: [String: Any] = [
|
|
5722
|
+
"incidentId": held.relay.incidentId,
|
|
5723
|
+
"fileBaseKey": held.relay.fileBaseKey,
|
|
5724
|
+
"apiBaseUrl": held.relay.apiBaseUrl,
|
|
5725
|
+
"tokenOwner": held.relay.tokenOwner ?? NSNull(),
|
|
5726
|
+
"fileName": held.fileName,
|
|
5727
|
+
"bytesBase64": held.data.base64EncodedString(),
|
|
5728
|
+
]
|
|
5729
|
+
do {
|
|
5730
|
+
let data = try JSONSerialization.data(withJSONObject: record)
|
|
5731
|
+
// Write atomically so a reader never observes a partially written record.
|
|
5732
|
+
try data.write(to: url, options: .atomic)
|
|
5733
|
+
return true
|
|
5734
|
+
} catch {
|
|
5735
|
+
Bridge.log("LIVE: Failed to persist held BES log for \(held.relay.incidentId): \(error)")
|
|
5736
|
+
return false
|
|
5737
|
+
}
|
|
5738
|
+
}
|
|
5739
|
+
|
|
5740
|
+
private func deletePersistedBes(_ incidentId: String) {
|
|
5741
|
+
guard let url = besHeldFileURL(incidentId) else { return }
|
|
5742
|
+
try? FileManager.default.removeItem(at: url)
|
|
5743
|
+
}
|
|
5744
|
+
|
|
5745
|
+
/// Re-POST any BES logs that were acknowledged to the glasses but whose Cloud upload never
|
|
5746
|
+
/// confirmed before the previous process ended. The glasses no longer hold these, so the
|
|
5747
|
+
/// phone is the only source; keep retrying until Cloud accepts each one.
|
|
5748
|
+
func resumePersistedBesUploads() {
|
|
5749
|
+
guard !isKilled, let dir = besHeldDir() else { return }
|
|
5750
|
+
let files = (try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)) ?? []
|
|
5751
|
+
let tracked = Set(heldBesLogs.values.compactMap { besHeldFileURL($0.relay.incidentId)?.lastPathComponent })
|
|
5752
|
+
for file in files where file.pathExtension == "json" && !tracked.contains(file.lastPathComponent) {
|
|
5753
|
+
guard resumingBesFiles.insert(file.lastPathComponent).inserted else { continue }
|
|
5754
|
+
resumePersistedBesFile(file)
|
|
5755
|
+
}
|
|
5756
|
+
}
|
|
5757
|
+
|
|
5758
|
+
private func resumePersistedBesFile(_ file: URL) {
|
|
5759
|
+
guard !isKilled else {
|
|
5760
|
+
resumingBesFiles.remove(file.lastPathComponent)
|
|
5761
|
+
return
|
|
5762
|
+
}
|
|
5763
|
+
guard
|
|
5764
|
+
let data = try? Data(contentsOf: file),
|
|
5765
|
+
let record = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],
|
|
5766
|
+
let incidentId = record["incidentId"] as? String, !incidentId.isEmpty,
|
|
5767
|
+
let apiBaseUrl = record["apiBaseUrl"] as? String, !apiBaseUrl.isEmpty,
|
|
5768
|
+
let base64 = record["bytesBase64"] as? String,
|
|
5769
|
+
let bytes = Data(base64Encoded: base64)
|
|
5770
|
+
else {
|
|
5771
|
+
Bridge.log("LIVE: Dropping unreadable persisted BES log: \(file.lastPathComponent)")
|
|
5772
|
+
try? FileManager.default.removeItem(at: file)
|
|
5773
|
+
resumingBesFiles.remove(file.lastPathComponent)
|
|
5774
|
+
return
|
|
5775
|
+
}
|
|
5776
|
+
let fileName = record["fileName"] as? String ?? file.lastPathComponent
|
|
5777
|
+
// A resume is a pure phone→Cloud POST; use an empty relay key so it can never be
|
|
5778
|
+
// mistaken for (or dropped by) a live incident relay's ownership guard.
|
|
5779
|
+
let relay = BleIncidentLogRelayEntry(
|
|
5780
|
+
fileBaseKey: "", incidentId: incidentId, apiBaseUrl: apiBaseUrl, kind: .firmware,
|
|
5781
|
+
reservedAt: incidentRelayClock(), tokenOwner: record["tokenOwner"] as? String
|
|
5782
|
+
)
|
|
5783
|
+
postIncidentLog(relay: relay, fileName: fileName, data: bytes, attemptsLeft: 1) { [weak self] success, status in
|
|
5784
|
+
guard let self else { return }
|
|
5785
|
+
if success || MentraLive.isPermanentIncidentRejection(status) {
|
|
5786
|
+
Bridge.log("LIVE: Resumed BES log upload finished for \(incidentId): HTTP \(status ?? 0)")
|
|
5787
|
+
try? FileManager.default.removeItem(at: file)
|
|
5788
|
+
self.resumingBesFiles.remove(file.lastPathComponent)
|
|
5789
|
+
} else if !self.isKilled {
|
|
5790
|
+
Bridge.log("LIVE: Resumed BES log upload failed for \(incidentId)")
|
|
5791
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + self.besRetrySeconds) { [weak self] in
|
|
5792
|
+
self?.resumePersistedBesFile(file)
|
|
5793
|
+
}
|
|
5794
|
+
}
|
|
5795
|
+
}
|
|
5796
|
+
}
|
|
5797
|
+
|
|
5798
|
+
/// Who a Core access token speaks for, or nil if it isn't a readable token.
|
|
5799
|
+
static func coreTokenOwner(_ token: String) -> String? {
|
|
5800
|
+
let parts = token.split(separator: ".")
|
|
5801
|
+
guard parts.count >= 2 else { return nil }
|
|
5802
|
+
var payload = parts[1].replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
|
|
5803
|
+
payload += String(repeating: "=", count: (4 - payload.count % 4) % 4)
|
|
5804
|
+
guard let data = Data(base64Encoded: payload),
|
|
5805
|
+
let claims = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
|
|
5806
|
+
else { return nil }
|
|
5807
|
+
return "\(claims["iss"] as? String ?? "")|\(claims["sub"] as? String ?? "")"
|
|
5808
|
+
}
|
|
5809
|
+
|
|
5810
|
+
/// Cloud will refuse these bytes every time: missing report, invalid, conflicting or oversized body.
|
|
5811
|
+
static func isPermanentIncidentRejection(_ status: Int?) -> Bool {
|
|
5812
|
+
guard let status else { return false }
|
|
5813
|
+
return [400, 404, 409, 410, 413, 422].contains(status)
|
|
5814
|
+
}
|
|
5815
|
+
|
|
5816
|
+
private func postIncidentLog(
|
|
5817
|
+
relay: BleIncidentLogRelayEntry,
|
|
5818
|
+
fileName: String,
|
|
5819
|
+
data: Data,
|
|
5820
|
+
attemptsLeft: Int,
|
|
5821
|
+
completion: @escaping (Bool, Int?) -> Void
|
|
5822
|
+
) {
|
|
5823
|
+
let token = DeviceStore.shared.get("bluetooth", "core_token") as? String ?? ""
|
|
5824
|
+
// After a sign-out or Core switch, the current token must not reach the report's Core.
|
|
5825
|
+
guard !token.isEmpty,
|
|
5826
|
+
MentraLive.coreTokenOwner(token) == relay.tokenOwner,
|
|
5827
|
+
var components = URLComponents(string: relay.apiBaseUrl)
|
|
5828
|
+
else {
|
|
5829
|
+
DispatchQueue.main.async { completion(false, nil) }
|
|
5551
5830
|
return
|
|
5552
5831
|
}
|
|
5553
5832
|
let basePath = components.path.hasSuffix("/")
|
|
@@ -5555,10 +5834,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5555
5834
|
: components.path
|
|
5556
5835
|
components.path = basePath + "/api/client/reports/\(relay.incidentId)/artifacts"
|
|
5557
5836
|
guard let url = components.url else {
|
|
5558
|
-
|
|
5559
|
-
if let existing = bleIncidentLogRelays[relay.fileBaseKey] {
|
|
5560
|
-
existing.session = nil
|
|
5561
|
-
}
|
|
5837
|
+
DispatchQueue.main.async { completion(false, nil) }
|
|
5562
5838
|
return
|
|
5563
5839
|
}
|
|
5564
5840
|
|
|
@@ -5582,23 +5858,25 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5582
5858
|
statusCode = nil
|
|
5583
5859
|
}
|
|
5584
5860
|
DispatchQueue.main.async {
|
|
5585
|
-
|
|
5586
|
-
|
|
5587
|
-
|
|
5861
|
+
if let owner = self.bleIncidentLogRelays[relay.fileBaseKey],
|
|
5862
|
+
owner.incidentId != relay.incidentId
|
|
5863
|
+
{
|
|
5588
5864
|
Bridge.log("LIVE: Dropping stale incident relay callback for \(relay.fileBaseKey)")
|
|
5589
5865
|
return
|
|
5590
5866
|
}
|
|
5591
5867
|
if ok {
|
|
5592
5868
|
Bridge.log("LIVE: ✅ Incident log BLE relay uploaded (\(relay.kind))")
|
|
5593
|
-
|
|
5594
|
-
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5599
|
-
|
|
5600
|
-
|
|
5601
|
-
|
|
5869
|
+
completion(true, statusCode)
|
|
5870
|
+
} else if attemptsLeft > 0, !MentraLive.isPermanentIncidentRejection(statusCode) {
|
|
5871
|
+
self.postIncidentLog(
|
|
5872
|
+
relay: relay, fileName: fileName, data: data, attemptsLeft: attemptsLeft - 1,
|
|
5873
|
+
completion: completion
|
|
5874
|
+
)
|
|
5875
|
+
} else {
|
|
5876
|
+
Bridge.log(
|
|
5877
|
+
"LIVE: ❌ Incident log BLE relay upload failed (\(relay.kind)) status=\(statusCode.map(String.init) ?? "none")"
|
|
5878
|
+
)
|
|
5879
|
+
completion(false, statusCode)
|
|
5602
5880
|
}
|
|
5603
5881
|
}
|
|
5604
5882
|
}.resume()
|
|
@@ -7069,6 +7347,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
7069
7347
|
)
|
|
7070
7348
|
|
|
7071
7349
|
isKilled = true
|
|
7350
|
+
for held in heldBesLogs.values {
|
|
7351
|
+
held.flush?.cancel()
|
|
7352
|
+
}
|
|
7353
|
+
heldBesLogs.removeAll()
|
|
7072
7354
|
// The pairing state outlives the manager; only its timer belongs to this instance.
|
|
7073
7355
|
cancelPairingPauseTimer()
|
|
7074
7356
|
unpairFlushWorkItem?.cancel()
|