From 5ddc048ea13f693ac206e29d3926cb4e1a0947c7 Mon Sep 17 00:00:00 2001 From: Maciej Korzeniewski Date: Fri, 6 Dec 2024 11:02:03 +0200 Subject: [PATCH 1/3] Rename waitDeviceSessionToOpen to waitDeviceSessionWithFtpToOpen and update it so that it waits for FTP client to be ready #496 --- .../java/com/polar/sdk/impl/BDBleApiImpl.kt | 39 ++++++++++++++----- .../sdk/impl/PolarBleApiImpl.swift | 36 ++++++++++++----- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt index 579b63b2..4a3439b6 100644 --- a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt +++ b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt @@ -1991,7 +1991,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set 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) @@ -2403,12 +2403,12 @@ class BDBleApiImpl private constructor(context: Context, features: Set - 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 { @@ -2436,6 +2441,22 @@ class BDBleApiImpl private constructor(context: Context, features: Set 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 @@ -3123,15 +3122,20 @@ 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 { @@ -3139,6 +3143,18 @@ extension PolarBleApiImpl: PolarBleApi { } } } + + 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( From 10fd25d11bf65e23c72d0f91436ff47e798c9c07 Mon Sep 17 00:00:00 2001 From: Maciej Korzeniewski Date: Fri, 6 Dec 2024 11:39:39 +0200 Subject: [PATCH 2/3] Rename waitDeviceSessionWithFtpToOpen to waitDeviceSessionWithPftpToOpen --- .../java/com/polar/sdk/impl/BDBleApiImpl.kt | 18 +++++++++--------- .../PolarBleSdk/sdk/impl/PolarBleApiImpl.swift | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt index 4a3439b6..e919a408 100644 --- a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt +++ b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt @@ -1991,7 +1991,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set if (status is FirmwareUpdateStatus.FinalizingFwUpdate) { - waitDeviceSessionWithFtpToOpen(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) @@ -2403,12 +2403,12 @@ class BDBleApiImpl private constructor(context: Context, features: Set - BleLogger.e(TAG, "waitDeviceSessionWithFtpToOpen(): error $error") + BleLogger.e(TAG, "waitDeviceSessionWithPftpToOpen(): error $error") emitter.onError(error) }) emitter.setCancellable { diff --git a/sources/iOS/ios-communications/Sources/PolarBleSdk/sdk/impl/PolarBleApiImpl.swift b/sources/iOS/ios-communications/Sources/PolarBleSdk/sdk/impl/PolarBleApiImpl.swift index bc3d4d20..17e94f71 100644 --- a/sources/iOS/ios-communications/Sources/PolarBleSdk/sdk/impl/PolarBleApiImpl.swift +++ b/sources/iOS/ios-communications/Sources/PolarBleSdk/sdk/impl/PolarBleApiImpl.swift @@ -2335,7 +2335,7 @@ extension PolarBleApiImpl: PolarBleApi { BleLogger.trace("Firmware update is in finalizing stage") BleLogger.trace("Device rebooting") - self.waitDeviceSessionWithFtpToOpen(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") }) @@ -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.waitDeviceSessionWithFtpToOpen(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") }) @@ -3060,7 +3060,7 @@ extension PolarBleApiImpl: PolarBleApi { let factoryResetMaxWaitTimeSeconds: TimeInterval = 6 * 60 BleLogger.trace("Write FW to device") return doFactoryReset(deviceId, preservePairingInformation: true) - .andThen(waitDeviceSessionWithFtpToOpen(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) @@ -3109,8 +3109,8 @@ extension PolarBleApiImpl: PolarBleApi { }) } - private func waitDeviceSessionWithFtpToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable { - BleLogger.trace("waitDeviceSessionWithFtpToOpen(): 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 @@ -3125,17 +3125,17 @@ extension PolarBleApiImpl: PolarBleApi { let isSessionOpen = self.deviceSessionState == BleDeviceSession.DeviceSessionState.sessionOpen let isFtpClientReady = self.isFtpClientReady(deviceId) - BleLogger.trace("waitDeviceSessionWithFtpToOpen(): isSessionOpen \(isSessionOpen), isFTPReady \(isFtpClientReady)") + BleLogger.trace("waitDeviceSessionWithPftpToOpen(): isSessionOpen \(isSessionOpen), isFTPReady \(isFtpClientReady)") if isSessionOpen && isFtpClientReady { - BleLogger.trace("waitDeviceSessionWithFtpToOpen(): completed") + BleLogger.trace("waitDeviceSessionWithPftpToOpen(): completed") disposable?.dispose() emitter(.completed) } else { - BleLogger.trace("waitDeviceSessionWithFtpToOpen(): current state \(String(describing: self.deviceSessionState))") + BleLogger.trace("waitDeviceSessionWithPftpToOpen(): current state \(String(describing: self.deviceSessionState))") } }, onError: { error in - BleLogger.error("waitDeviceSessionWithFtpToOpen(): error \(error)") + BleLogger.error("waitDeviceSessionWithPftpToOpen(): error \(error)") emitter(.error(error)) }) return Disposables.create { From d238bcd5fcbc23a5a14124edd44952ed5867f3d6 Mon Sep 17 00:00:00 2001 From: Maciej Korzeniewski Date: Fri, 6 Dec 2024 14:02:02 +0200 Subject: [PATCH 3/3] Rename isFtpClientReady to isPftpClientReady --- .../src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt | 8 ++++---- .../Sources/PolarBleSdk/sdk/impl/PolarBleApiImpl.swift | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt index e919a408..9c123cb9 100644 --- a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt +++ b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt @@ -2420,11 +2420,11 @@ class BDBleApiImpl private constructor(context: Context, features: Set Bool { + private func isPftpClientReady(_ identifier: String) -> Bool { do { let session = try self.sessionFtpClientReady(identifier) guard let client = session.fetchGattClient(BlePsFtpClient.PSFTP_SERVICE) as? BlePsFtpClient else {