Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename waitDeviceSessionToOpen to waitDeviceSessionWithPftpToOpen and update it so that it waits for PFTP client to be ready #496 #516

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS

doFactoryReset(identifier, true)
.andThen(Completable.timer(30, TimeUnit.SECONDS))
.andThen(waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L))
.andThen(waitDeviceSessionWithPftpToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L))
.andThen(Completable.timer(5, TimeUnit.SECONDS))
.andThen(
Flowable.fromIterable(firmwareFiles)
Expand All @@ -2015,7 +2015,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, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 120L)
waitDeviceSessionWithPftpToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 120L)
.andThen(
Completable.fromCallable {
BleLogger.d(TAG, "Restoring backup to device after version ${firmwareUpdateResponse.version}")
Expand All @@ -2031,7 +2031,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)
waitDeviceSessionWithPftpToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 60L)
.andThen(Flowable.just(FirmwareUpdateStatus.FwUpdateCompletedSuccessfully(firmwareUpdateResponse.version)))
} else {
Flowable.just(status)
Expand Down Expand Up @@ -2403,12 +2403,12 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}, BackpressureStrategy.BUFFER)
}

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

return Observable.timer(waitForDeviceDownSeconds, TimeUnit.SECONDS)
Expand All @@ -2419,15 +2419,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 isPftpClientReady = isPftpClientReady(deviceId)

BleLogger.d(TAG, "waitDeviceSessionWithPftpToOpen(): isSessionOpen $isSessionOpen, isPftpClientReady $isPftpClientReady")

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

private fun isPftpClientReady(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?): Completable {

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

BleLogger.trace("Device rebooting")
self.waitDeviceSessionToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
self.waitDeviceSessionWithPftpToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
.do(onSubscribe: {
BleLogger.trace("Waiting for device session to open after reboot with timeout: \(rebootMaxWaitTimeSeconds) seconds")
})
Expand All @@ -2350,7 +2350,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.waitDeviceSessionWithPftpToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
.do(onSubscribe: {
BleLogger.trace("Waiting for device session to open post factory reset")
})
Expand Down Expand Up @@ -3060,7 +3060,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(waitDeviceSessionWithPftpToOpen(deviceId: deviceId, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10))
.andThen(Observable.create { observer in
do {
let session = try self.sessionFtpClientReady(deviceId)
Expand Down Expand Up @@ -3109,9 +3109,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 waitDeviceSessionWithPftpToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable {
BleLogger.trace("waitDeviceSessionWithPftpToOpen(): timeoutSeconds \(timeoutSeconds), waitForDeviceDownSeconds \(waitForDeviceDownSeconds)")
let pollIntervalSeconds = 5

return Completable.create { emitter in
Expand All @@ -3123,22 +3122,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 isPftpClientReady = self.isPftpClientReady(deviceId)

BleLogger.trace("waitDeviceSessionWithPftpToOpen(): isSessionOpen \(isSessionOpen), isFTPReady \(isPftpClientReady)")

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

private func isPftpClientReady(_ 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