Skip to content

Commit

Permalink
⚡ (LekaUpdater): Maximum packet size related to negotiated MTU charac…
Browse files Browse the repository at this point in the history
…teristic
  • Loading branch information
YannLocatelli committed May 29, 2024
1 parent 3e94fa2 commit b7b3ae6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ private class StateSendingFile: GKState, StateEventProcessor {
// MARK: Lifecycle

override init() {
let dataSize = globalFirmwareManager.data.count

self.expectedCompletePackets = Int(floor(Double(dataSize / self.maximumPacketSize)))
self.expectedRemainingBytes = Int(dataSize % self.maximumPacketSize)
self.firmwareDataSize = globalFirmwareManager.data.count

self.currentPacket = 0

Expand All @@ -242,7 +239,20 @@ private class StateSendingFile: GKState, StateEventProcessor {
}

override func didEnter(from _: GKState?) {
self.sendFile()
Robot.shared.connectedPeripheral?.peripheral.readValue(forCharacteristic: BLESpecs.Monitoring.Characteristics.negotiatedMTU, inService: BLESpecs.Monitoring.service)

Check warning on line 242 in Apps/LekaUpdater/Sources/Libs/UpdateProcess/Version/UpdateProcessV150.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Line should be 150 characters or less; currently it has 172 characters (line_length)
.receive(on: DispatchQueue.main)
.sink(
receiveCompletion: { _ in
// nothing to do
},
receiveValue: { data in
if let data {
self.maximumPacketSize = Int(data.map { UInt16($0) }.reduce(0) { $0 << 8 + $1 }) - self.l2capOverhead
self.sendFile()
}
}
)
.store(in: &self.cancellables)
}

override func willExit(to _: GKState) {
Expand All @@ -265,11 +275,6 @@ private class StateSendingFile: GKState, StateEventProcessor {

private var cancellables: Set<AnyCancellable> = []

private let maximumPacketSize: Int = 182 // 185 (iOS max MTU) - 3 (header size)

private var currentPacket: Int = 0
private var expectedCompletePackets: Int
private var expectedRemainingBytes: Int
private lazy var characteristic: CharacteristicModelWriteOnly? = CharacteristicModelWriteOnly(
characteristicUUID: BLESpecs.FileExchange.Characteristics.fileReceptionBuffer,
serviceUUID: BLESpecs.FileExchange.service,
Expand All @@ -279,6 +284,20 @@ private class StateSendingFile: GKState, StateEventProcessor {
}
)

private var firmwareDataSize: Int = 0
private let l2capOverhead: Int = 3
private var maximumPacketSize: Int = 182 // 185 (iOS max MTU) - 3 (header size)

private var currentPacket: Int = 0

private var expectedCompletePackets: Int {
Int(floor(Double(self.firmwareDataSize / self.maximumPacketSize)))
}

private var expectedRemainingBytes: Int {
self.firmwareDataSize % self.maximumPacketSize
}

private var expectedPackets: Int {
self.expectedRemainingBytes == 0 ? self.expectedCompletePackets : self.expectedCompletePackets + 1
}
Expand All @@ -291,10 +310,7 @@ private class StateSendingFile: GKState, StateEventProcessor {
globalFirmwareManager.$data
.receive(on: DispatchQueue.main)
.sink { data in
let dataSize = data.count

self.expectedCompletePackets = Int(floor(Double(dataSize / self.maximumPacketSize)))
self.expectedRemainingBytes = Int(dataSize % self.maximumPacketSize)
self.firmwareDataSize = data.count
}
.store(in: &self.cancellables)
}
Expand Down
1 change: 1 addition & 0 deletions Modules/BLEKit/Sources/BLESpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum BLESpecs {
public static let screensaverEnable = CBUUID(string: "0x8369")
public static let softReboot = CBUUID(string: "0x8382")
public static let hardReboot = CBUUID(string: "0x7282")
public static let negotiatedMTU = CBUUID(data: Data("NEGOTIATED_MTU".utf8) + Data([0, 0]))
}

public static let service = CBUUID(string: "0x7779")
Expand Down

0 comments on commit b7b3ae6

Please sign in to comment.