Skip to content

Commit

Permalink
Rename waitDeviceSessionToOpen to waitDeviceSessionWithFtpToOpen and …
Browse files Browse the repository at this point in the history
…update it so that it waits for FTP client to be ready polarofficial#496
  • Loading branch information
korzonkiee committed Dec 6, 2024
1 parent beb14ba commit faf64e2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
val unzippedFwPackage = PolarFirmwareUpdateUtils.unzipFirmwarePackage(firmwareBytes.bytes())
doFactoryReset(identifier, true)
.andThen(Completable.timer(30, TimeUnit.SECONDS))
.andThen(waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L))
.andThen(waitDeviceSessionWithFtpToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L))
.andThen(Completable.timer(5, TimeUnit.SECONDS))
.andThen(
writeFirmwareToDevice(identifier, unzippedFwPackage)
Expand All @@ -1960,7 +1960,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
BleLogger.d(TAG, "Starting finalization of firmware update")
Completable.timer(rebootTriggeredWaitTimeSeconds, TimeUnit.SECONDS)
.andThen(
waitDeviceSessionToOpen(identifier, rebootMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 120L)
waitDeviceSessionWithFtpToOpen(identifier, rebootMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 120L)
.andThen(
Completable.fromCallable {
BleLogger.d(TAG, "Restoring backup to device after version ${firmwareUpdateResponse.version}")
Expand All @@ -1976,7 +1976,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}
.concatMap { status ->
if (status is FirmwareUpdateStatus.FinalizingFwUpdate) {
waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 60L)
waitDeviceSessionWithFtpToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 60L)
.andThen(Flowable.just(FirmwareUpdateStatus.FwUpdateCompletedSuccessfully(firmwareUpdateResponse.version)))
} else {
Flowable.just(status)
Expand Down Expand Up @@ -2343,12 +2343,12 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}, BackpressureStrategy.BUFFER)
}

private fun waitDeviceSessionToOpen(
private fun waitDeviceSessionWithFtpToOpen(
deviceId: String,
timeoutSeconds: Long,
waitForDeviceDownSeconds: Long = 0L
): Completable {
BleLogger.d(TAG, "waitDeviceSessionToOpen(), seconds: $timeoutSeconds, waitForDeviceDownSeconds: $waitForDeviceDownSeconds")
BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): seconds $timeoutSeconds, waitForDeviceDownSeconds $waitForDeviceDownSeconds")
val pollIntervalSeconds = 5L

return Observable.timer(waitForDeviceDownSeconds, TimeUnit.SECONDS)
Expand All @@ -2359,15 +2359,20 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
.takeUntil(Observable.timer(timeoutSeconds, TimeUnit.SECONDS))
.timeout(timeoutSeconds, TimeUnit.SECONDS)
.subscribe({
if (deviceSessionState == DeviceSessionState.SESSION_OPEN) {
BleLogger.d(TAG, "Session opened, deviceId: $deviceId")
val isSessionOpen = deviceSessionState == DeviceSessionState.SESSION_OPEN
val isFtpClientReady = isFtpClientReady(deviceId)

BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): isSessionOpen $isSessionOpen, isFtpClientReady $isFtpClientReady")

if (isSessionOpen && isFtpClientReady) {
BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): completed")
disposable?.dispose()
emitter.onComplete()
} else {
BleLogger.d(TAG, "Waiting for device session to open, deviceId: $deviceId, current state: $deviceSessionState")
BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): current state $deviceSessionState")
}
}, { error ->
BleLogger.e(TAG, "Timeout reached while waiting for device session to open, deviceId: $deviceId")
BleLogger.e(TAG, "waitDeviceSessionWithFtpToOpen(): error $error")
emitter.onError(error)
})
emitter.setCancellable {
Expand All @@ -2376,6 +2381,22 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
})
}

private fun isFtpClientReady(identifier: String): Boolean {
try {
val session = sessionPsFtpClientReady(identifier)

val client = session.fetchClient(BlePsFtpUtils.RFC77_PFTP_SERVICE) as BlePsFtpClient?
if (client != null) {
return true
}

return false
}
catch (error: Throwable) {
return false
}
}

override fun deleteStoredDeviceData(identifier: String, dataType: PolarStoredDataType, until: LocalDate?, maxFilesToDelete: Int?): Completable {
BleLogger.d(TAG, "Deleting stored data from device $identifier of type ${dataType.type}, until: $until, maxFilesToDelete: $maxFilesToDelete")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@ extension PolarBleApiImpl: PolarBleApi {
BleLogger.trace("Firmware update is in finalizing stage")

BleLogger.trace("Device rebooting")
self.waitDeviceSessionToOpen(deviceId: identifier, timeoutSeconds: Int(rebootMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
self.waitDeviceSessionWithFtpToOpen(deviceId: identifier, timeoutSeconds: Int(rebootMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
.do(onSubscribe: {
BleLogger.trace("Waiting for device session to open after reboot with timeout: \(rebootMaxWaitTimeSeconds) seconds")
})
Expand All @@ -2369,7 +2369,7 @@ extension PolarBleApiImpl: PolarBleApi {
}
.flatMap { _ in
BleLogger.trace("Waiting for device session to open after factory reset with timeout: \(factoryResetMaxWaitTimeSeconds) seconds")
return self.waitDeviceSessionToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
return self.waitDeviceSessionWithFtpToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
.do(onSubscribe: {
BleLogger.trace("Waiting for device session to open post factory reset")
})
Expand Down Expand Up @@ -3190,7 +3190,7 @@ extension PolarBleApiImpl: PolarBleApi {
let factoryResetMaxWaitTimeSeconds: TimeInterval = 6 * 60
BleLogger.trace("Write FW to device")
return doFactoryReset(deviceId, preservePairingInformation: true)
.andThen(waitDeviceSessionToOpen(deviceId: deviceId, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10))
.andThen(waitDeviceSessionWithFtpToOpen(deviceId: deviceId, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10))
.andThen(Observable.create { observer in
do {
let session = try self.sessionFtpClientReady(deviceId)
Expand Down Expand Up @@ -3236,8 +3236,8 @@ extension PolarBleApiImpl: PolarBleApi {
}


private func waitDeviceSessionToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable {
BleLogger.trace("Wait for device session to open, timeoutSeconds: \(timeoutSeconds), waitForDeviceDownSeconds: \(waitForDeviceDownSeconds)")
private func waitDeviceSessionWithFtpToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable {
BleLogger.trace("waitDeviceSessionWithFtpToOpen(): timeoutSeconds \(timeoutSeconds), waitForDeviceDownSeconds \(waitForDeviceDownSeconds)")
let pollIntervalSeconds = 5

return Completable.create { emitter in
Expand All @@ -3249,22 +3249,39 @@ extension PolarBleApiImpl: PolarBleApi {
}
.timeout(RxTimeInterval.seconds(timeoutSeconds), scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
if self.deviceSessionState == BleDeviceSession.DeviceSessionState.sessionOpen {
BleLogger.trace("Session opened, deviceId: \(deviceId)")
let isSessionOpen = self.deviceSessionState == BleDeviceSession.DeviceSessionState.sessionOpen
let isFtpClientReady = self.isFtpClientReady(deviceId)

BleLogger.trace("waitDeviceSessionWithFtpToOpen(): isSessionOpen \(isSessionOpen), isFTPReady \(isFtpClientReady)")

if isSessionOpen && isFtpClientReady {
BleLogger.trace("waitDeviceSessionWithFtpToOpen(): completed")
disposable?.dispose()
emitter(.completed)
} else {
BleLogger.trace("Waiting for device session to open, deviceId: \(deviceId), current state: \(String(describing: self.deviceSessionState))")
BleLogger.trace("waitDeviceSessionWithFtpToOpen(): current state \(String(describing: self.deviceSessionState))")
}
}, onError: { error in
BleLogger.trace("Timeout reached while waiting for device session to open, deviceId: \(deviceId)")
BleLogger.error("waitDeviceSessionWithFtpToOpen(): error \(error)")
emitter(.error(error))
})
return Disposables.create {
disposable?.dispose()
}
}
}

private func isFtpClientReady(_ identifier: String) -> Bool {
do {
let session = try self.sessionFtpClientReady(identifier)
guard let client = session.fetchGattClient(BlePsFtpClient.PSFTP_SERVICE) as? BlePsFtpClient else {
return false
}
return true
} catch _ {
return false
}
}


private func processAccData(
Expand Down

0 comments on commit faf64e2

Please sign in to comment.