Skip to content

Commit 6455800

Browse files
authored
Merge pull request #38 from kmatzen/fix/packet-reconstructor-buffer
Fix packet reconstructor buffer selection for concurrent multipart responses
2 parents 1c0c5ff + 1b26e7a commit 6455800

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

Facett/BLEPacketReconstructor.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,22 @@ class BLEPacketReconstructor {
184184
"peripheral_id": peripheralId
185185
])
186186

187-
// Find the buffer for this peripheral (we don't know the query ID yet)
188187
let bufferKeys = continuationBuffer.keys.filter { $0.hasPrefix(peripheralId) }
189188

190189
if bufferKeys.isEmpty {
191-
// This might be the first packet of a multipart response
192190
return handleFirstPacketOfMultipart(data: data, peripheralId: peripheralId)
193191
}
194192

195-
// Track sequence counters for multiple concurrent operations
196-
guard let bufferKey = bufferKeys.first else {
193+
// When multiple buffers exist, pick the most recently active one.
194+
// Continuation packets don't carry a query ID, so we use recency
195+
// as the best heuristic. The GoPro BLE protocol expects one
196+
// multipart response per characteristic at a time.
197+
let bufferKey: String
198+
if bufferKeys.count == 1 {
199+
bufferKey = bufferKeys[0]
200+
} else if let mostRecent = bufferKeys.max(by: { (lastPacketTime[$0] ?? .distantPast) < (lastPacketTime[$1] ?? .distantPast) }) {
201+
bufferKey = mostRecent
202+
} else {
197203
ErrorHandler.bleError("No buffer key found for continuation packet", context: ["peripheral_id": peripheralId])
198204
return nil
199205
}

0 commit comments

Comments
 (0)