@@ -17,6 +17,43 @@ package polling
1717
1818import "time"
1919
20+ // SleepRecoveryConfig configures automatic recovery after host sleep/wake
21+ type SleepRecoveryConfig struct {
22+ // Enabled enables sleep detection and recovery attempts
23+ Enabled bool
24+
25+ // TimeDiscontinuityThreshold is the minimum elapsed time beyond the expected
26+ // poll interval that indicates a sleep occurred. Default: 2 seconds
27+ TimeDiscontinuityThreshold time.Duration
28+
29+ // MaxRecoveryAttempts is the number of recovery attempts before
30+ // treating as a fatal error. Default: 3
31+ MaxRecoveryAttempts int
32+
33+ // RecoveryBackoff is the delay between recovery attempts
34+ RecoveryBackoff time.Duration
35+ }
36+
37+ // DefaultSleepRecoveryConfig returns sensible defaults for sleep recovery
38+ func DefaultSleepRecoveryConfig () SleepRecoveryConfig {
39+ return SleepRecoveryConfig {
40+ Enabled : true ,
41+ TimeDiscontinuityThreshold : 2 * time .Second ,
42+ MaxRecoveryAttempts : 3 ,
43+ RecoveryBackoff : 500 * time .Millisecond ,
44+ }
45+ }
46+
47+ // DetectSleep checks if the elapsed time since last poll indicates a system sleep.
48+ // Returns true if elapsed time exceeds (pollInterval + TimeDiscontinuityThreshold).
49+ func (cfg SleepRecoveryConfig ) DetectSleep (elapsed , pollInterval time.Duration ) bool {
50+ if ! cfg .Enabled {
51+ return false
52+ }
53+ expectedMax := pollInterval + cfg .TimeDiscontinuityThreshold
54+ return elapsed > expectedMax
55+ }
56+
2057// Config holds polling configuration options
2158type Config struct {
2259 PollInterval time.Duration
@@ -25,6 +62,8 @@ type Config struct {
2562 // 0x00 = immediate return, 0x01-0xFE = retry count (~150ms each), 0xFF = infinite
2663 // Higher values reduce LED blinking frequency but increase detection latency
2764 HardwareTimeoutRetries byte
65+ // SleepRecovery configures automatic recovery after host sleep/wake cycles
66+ SleepRecovery SleepRecoveryConfig
2867}
2968
3069// DefaultConfig returns the default polling configuration
@@ -33,5 +72,6 @@ func DefaultConfig() *Config {
3372 PollInterval : 250 * time .Millisecond ,
3473 CardRemovalTimeout : 600 * time .Millisecond ,
3574 HardwareTimeoutRetries : 0x20 , // ~4.8s timeout (32 * 150ms) for reduced LED blinking
75+ SleepRecovery : DefaultSleepRecoveryConfig (),
3676 }
3777}
0 commit comments