Skip to content

Commit 8822979

Browse files
authored
Merge pull request #31 from ZaparooProject/feature/sleep-wake-recovery
feat: add sleep/wake detection and automatic recovery
2 parents 924cf9a + 6890a6f commit 8822979

File tree

11 files changed

+755
-908
lines changed

11 files changed

+755
-908
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/ZaparooProject/go-pn532
22

3-
go 1.23.0
4-
5-
toolchain go1.24.11
3+
go 1.24.11
64

75
require (
86
github.com/sasha-s/go-deadlock v0.3.6
@@ -16,6 +14,7 @@ require (
1614
require (
1715
github.com/creack/goselect v0.1.3 // indirect
1816
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/jonboulle/clockwork v0.5.0 // indirect
1918
github.com/petermattis/goid v0.0.0-20250813065127-a731cc31b4fe // indirect
2019
github.com/pmezard/go-difflib v1.0.0 // indirect
2120
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/creack/goselect v0.1.3 h1:MaGNMclRo7P2Jl21hBpR1Cn33ITSbKP6E49RtfblLKc
22
github.com/creack/goselect v0.1.3/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5-
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
6-
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
5+
github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbdFz6I=
6+
github.com/jonboulle/clockwork v0.5.0/go.mod h1:3mZlmanh0g2NDKO5TWZVJAfofYk64M7XN3SzBPjZF60=
77
github.com/petermattis/goid v0.0.0-20250813065127-a731cc31b4fe h1:vHpqOnPlnkba8iSxU4j/CvDSS9J4+F4473esQsYLGoE=
88
github.com/petermattis/goid v0.0.0-20250813065127-a731cc31b4fe/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
99
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

polling/config.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,43 @@ package polling
1717

1818
import "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
2158
type 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
}

polling/device_actor.go

Lines changed: 0 additions & 209 deletions
This file was deleted.

0 commit comments

Comments
 (0)