serial_linux.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //go:build linux
  2. package thermalbackend
  3. import (
  4. "context"
  5. "errors"
  6. "sync"
  7. "time"
  8. "hth/internal/protocol6400"
  9. "hth/internal/thermaltransport"
  10. )
  11. type Serial struct {
  12. mu sync.Mutex
  13. path string
  14. profile string
  15. timeout time.Duration
  16. retries int
  17. client *thermaltransport.Client
  18. }
  19. func NewSerial(path, profile string, timeout time.Duration, retries int) *Serial {
  20. return &Serial{path: path, profile: profile, timeout: timeout, retries: retries}
  21. }
  22. func (s *Serial) Kind() string { return "serial" }
  23. func (s *Serial) Profile() string { return s.profile }
  24. // Open is intentionally lazy. hthd remains available while a configured
  25. // device is absent; the next RPC performs a bounded reconnect attempt.
  26. func (s *Serial) Open(context.Context) error { return nil }
  27. func (s *Serial) ensureLocked() error {
  28. if s.client != nil {
  29. return nil
  30. }
  31. client, err := thermaltransport.Open(s.path, s.profile, s.timeout, s.retries)
  32. if err != nil {
  33. return err
  34. }
  35. s.client = client
  36. return nil
  37. }
  38. func (s *Serial) invalidateLocked() {
  39. if s.client != nil {
  40. _ = s.client.Close()
  41. s.client = nil
  42. }
  43. }
  44. func (s *Serial) Close() error {
  45. s.mu.Lock()
  46. defer s.mu.Unlock()
  47. if s.client == nil {
  48. return nil
  49. }
  50. err := s.client.Close()
  51. s.client = nil
  52. return err
  53. }
  54. func (s *Serial) SetTarget(ctx context.Context, target byte) error {
  55. if _, err := protocol6400.SetTargetRequest(target); err != nil {
  56. return err
  57. }
  58. s.mu.Lock()
  59. defer s.mu.Unlock()
  60. if err := s.ensureLocked(); err != nil {
  61. return err
  62. }
  63. err := s.client.SetTarget(ctx, target)
  64. if err != nil && !errors.Is(err, protocol6400.ErrTargetRange) {
  65. s.invalidateLocked()
  66. }
  67. return err
  68. }
  69. func (s *Serial) GetSnapshot(ctx context.Context) (protocol6400.Snapshot, []byte, error) {
  70. s.mu.Lock()
  71. defer s.mu.Unlock()
  72. if err := s.ensureLocked(); err != nil {
  73. return protocol6400.Snapshot{}, nil, err
  74. }
  75. value, payload, err := s.client.GetSnapshot(ctx)
  76. if err != nil {
  77. s.invalidateLocked()
  78. }
  79. return value, payload, err
  80. }
  81. func (s *Serial) RunStimulus(ctx context.Context, value protocol6400.Stimulus) error {
  82. if _, err := protocol6400.StimulusRequest(s.profile, value); err != nil {
  83. return err
  84. }
  85. s.mu.Lock()
  86. defer s.mu.Unlock()
  87. if err := s.ensureLocked(); err != nil {
  88. return err
  89. }
  90. err := s.client.RunStimulus(ctx, value)
  91. if err != nil && !errors.Is(err, protocol6400.ErrNotSupported) &&
  92. !errors.Is(err, protocol6400.ErrDutyRange) && !errors.Is(err, protocol6400.ErrDuration) {
  93. s.invalidateLocked()
  94. }
  95. return err
  96. }