mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 04:05:20 +00:00
36 lines
1.1 KiB
Swift
36 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
enum BLEScanDutyPlan: Equatable {
|
|
case continuous
|
|
case dutyCycle(onDuration: TimeInterval, offDuration: TimeInterval)
|
|
}
|
|
|
|
enum BLEScanDutyPolicy {
|
|
static func plan(
|
|
dutyEnabled: Bool,
|
|
appIsActive: Bool,
|
|
connectedCount: Int,
|
|
hasRecentTraffic: Bool,
|
|
highDegreeThreshold: Int = TransportConfig.bleHighDegreeThreshold
|
|
) -> BLEScanDutyPlan {
|
|
let forceContinuousScan = connectedCount <= 2 || hasRecentTraffic
|
|
let shouldDutyCycle = dutyEnabled && appIsActive && connectedCount > 0 && !forceContinuousScan
|
|
|
|
guard shouldDutyCycle else {
|
|
return .continuous
|
|
}
|
|
|
|
if connectedCount >= highDegreeThreshold {
|
|
return .dutyCycle(
|
|
onDuration: TransportConfig.bleDutyOnDurationDense,
|
|
offDuration: TransportConfig.bleDutyOffDurationDense
|
|
)
|
|
}
|
|
|
|
return .dutyCycle(
|
|
onDuration: TransportConfig.bleDutyOnDuration,
|
|
offDuration: TransportConfig.bleDutyOffDuration
|
|
)
|
|
}
|
|
}
|